/src/ffmpeg/libswscale/ops_memcpy.c
Line | Count | Source |
1 | | /** |
2 | | * Copyright (C) 2025 Niklas Haas |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include "libavutil/avassert.h" |
22 | | |
23 | | #include "ops_backend.h" |
24 | | |
25 | | typedef struct MemcpyPriv { |
26 | | int num_planes; |
27 | | int index[4]; /* or -1 to clear plane */ |
28 | | uint8_t clear_value[4]; |
29 | | } MemcpyPriv; |
30 | | |
31 | | /* Memcpy backend for trivial cases */ |
32 | | |
33 | | static void process(const SwsOpExec *exec, const void *priv, |
34 | | int x_start, int y_start, int x_end, int y_end) |
35 | 0 | { |
36 | 0 | const MemcpyPriv *p = priv; |
37 | 0 | const int lines = y_end - y_start; |
38 | 0 | av_assert1(x_start == 0 && x_end == exec->width); |
39 | |
|
40 | 0 | for (int i = 0; i < p->num_planes; i++) { |
41 | 0 | uint8_t *out = exec->out[i]; |
42 | 0 | const int idx = p->index[i]; |
43 | 0 | if (idx < 0) { |
44 | 0 | memset(out, p->clear_value[i], exec->out_stride[i] * lines); |
45 | 0 | } else if (exec->out_stride[i] == exec->in_stride[idx]) { |
46 | 0 | memcpy(out, exec->in[idx], exec->out_stride[i] * lines); |
47 | 0 | } else { |
48 | 0 | const int bytes = x_end * exec->block_size_out; |
49 | 0 | const uint8_t *in = exec->in[idx]; |
50 | 0 | for (int y = y_start; y < y_end; y++) { |
51 | 0 | memcpy(out, in, bytes); |
52 | 0 | out += exec->out_stride[i]; |
53 | 0 | in += exec->in_stride[idx]; |
54 | 0 | } |
55 | 0 | } |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | static int compile(SwsContext *ctx, SwsOpList *ops, SwsCompiledOp *out) |
60 | 0 | { |
61 | 0 | MemcpyPriv p = {0}; |
62 | |
|
63 | 0 | for (int n = 0; n < ops->num_ops; n++) { |
64 | 0 | const SwsOp *op = &ops->ops[n]; |
65 | 0 | switch (op->op) { |
66 | 0 | case SWS_OP_READ: |
67 | 0 | if ((op->rw.packed && op->rw.elems != 1) || op->rw.frac) |
68 | 0 | return AVERROR(ENOTSUP); |
69 | 0 | for (int i = 0; i < op->rw.elems; i++) |
70 | 0 | p.index[i] = i; |
71 | 0 | break; |
72 | | |
73 | 0 | case SWS_OP_SWIZZLE: { |
74 | 0 | const MemcpyPriv orig = p; |
75 | 0 | for (int i = 0; i < 4; i++) { |
76 | | /* Explicitly exclude swizzle masks that contain duplicates, |
77 | | * because these are wasteful to implement as a memcpy */ |
78 | 0 | for (int j = 0; j < i; j++) { |
79 | 0 | if (op->swizzle.in[i] == op->swizzle.in[j]) |
80 | 0 | return AVERROR(ENOTSUP); |
81 | 0 | } |
82 | 0 | p.index[i] = orig.index[op->swizzle.in[i]]; |
83 | 0 | } |
84 | 0 | break; |
85 | 0 | } |
86 | | |
87 | 0 | case SWS_OP_CLEAR: |
88 | 0 | for (int i = 0; i < 4; i++) { |
89 | 0 | if (!op->c.q4[i].den) |
90 | 0 | continue; |
91 | 0 | if (op->c.q4[i].den != 1) |
92 | 0 | return AVERROR(ENOTSUP); |
93 | | |
94 | | /* Ensure all bytes to be cleared are the same, because we |
95 | | * can't memset on multi-byte sequences */ |
96 | 0 | uint8_t val = op->c.q4[i].num & 0xFF; |
97 | 0 | uint32_t ref = val; |
98 | 0 | switch (ff_sws_pixel_type_size(op->type)) { |
99 | 0 | case 2: ref *= 0x101; break; |
100 | 0 | case 4: ref *= 0x1010101; break; |
101 | 0 | } |
102 | 0 | if (ref != op->c.q4[i].num) |
103 | 0 | return AVERROR(ENOTSUP); |
104 | 0 | p.clear_value[i] = val; |
105 | 0 | p.index[i] = -1; |
106 | 0 | } |
107 | 0 | break; |
108 | | |
109 | 0 | case SWS_OP_WRITE: |
110 | 0 | if ((op->rw.packed && op->rw.elems != 1) || op->rw.frac) |
111 | 0 | return AVERROR(ENOTSUP); |
112 | 0 | p.num_planes = op->rw.elems; |
113 | 0 | break; |
114 | | |
115 | 0 | default: |
116 | 0 | return AVERROR(ENOTSUP); |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | 0 | *out = (SwsCompiledOp) { |
121 | 0 | .block_size = 1, |
122 | 0 | .func = process, |
123 | 0 | .priv = av_memdup(&p, sizeof(p)), |
124 | 0 | .free = av_free, |
125 | 0 | }; |
126 | 0 | return out->priv ? 0 : AVERROR(ENOMEM); |
127 | 0 | } |
128 | | |
129 | | const SwsOpBackend backend_murder = { |
130 | | .name = "memcpy", |
131 | | .compile = compile, |
132 | | }; |