FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/ops_memcpy.c
Date: 2026-08-31 17:12:07
Exec Total Coverage
Lines: 58 69 84.1%
Functions: 2 2 100.0%
Branches: 44 58 75.9%

Line Branch Exec 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 #include "libavutil/mem.h"
23
24 #include "ops_internal.h"
25
26 typedef struct MemcpyPriv {
27 int num_planes;
28 int index[4]; /* or -1 to clear plane */
29 uint8_t clear_value[4];
30 } MemcpyPriv;
31
32 /**
33 * Switch to loop if total padding exceeds this number of bytes. Chosen to
34 * align with the typical L1 cache size of modern CPUs, as this avoids the
35 * risk of the implementation loading one extra unnecessary cache line.
36 */
37 #define SWS_MAX_PADDING 64
38
39 /* Memcpy backend for trivial cases */
40
41 5609 static void process(const SwsOpExec *exec, const void *priv,
42 int x_start, int y_start, int x_end, int y_end)
43 {
44 5609 const MemcpyPriv *p = priv;
45 5609 const int lines = y_end - y_start;
46 av_assert1(x_start == 0 && x_end == exec->width);
47
48
2/2
✓ Branch 0 taken 7641 times.
✓ Branch 1 taken 5609 times.
13250 for (int i = 0; i < p->num_planes; i++) {
49 7641 uint8_t *out = exec->out[i];
50 7641 const int idx = p->index[i];
51 7641 const int bytes = x_end * exec->block_size_out[i];
52 7641 const int use_loop = exec->out_stride[i] > bytes + SWS_MAX_PADDING;
53
3/4
✓ Branch 0 taken 7614 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 7614 times.
✗ Branch 3 not taken.
7641 if (idx < 0 && !use_loop) {
54 7614 memset(out, p->clear_value[i], exec->out_stride[i] * lines);
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 } else if (idx < 0) {
56 for (int y = y_start; y < y_end; y++) {
57 memset(out, p->clear_value[i], bytes);
58 out += exec->out_stride[i];
59 }
60
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 } else if (out == exec->in[idx]) {
61 av_assert1(exec->out_stride[i] == exec->in_stride[idx]);
62 27 continue; /* plane was already ref'd */
63 } else if (exec->out_stride[i] == exec->in_stride[idx] && !use_loop) {
64 memcpy(out, exec->in[idx], exec->out_stride[i] * lines);
65 } else {
66 const uint8_t *in = exec->in[idx];
67 for (int y = y_start; y < y_end; y++) {
68 memcpy(out, in, bytes);
69 out += exec->out_stride[i];
70 in += exec->in_stride[idx];
71 }
72 }
73 }
74 5609 }
75
76 117888 static int compile(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
77 {
78 117888 MemcpyPriv p = {0};
79
80
2/2
✓ Branch 0 taken 158340 times.
✓ Branch 1 taken 5461 times.
163801 for (int n = 0; n < ops->num_ops; n++) {
81 158340 const SwsOp *op = &ops->ops[n];
82
5/5
✓ Branch 0 taken 108948 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 8996 times.
✓ Branch 3 taken 5527 times.
✓ Branch 4 taken 34804 times.
158340 switch (op->op) {
83 108948 case SWS_OP_READ:
84
6/6
✓ Branch 1 taken 88274 times.
✓ Branch 2 taken 20674 times.
✓ Branch 3 taken 85562 times.
✓ Branch 4 taken 2712 times.
✓ Branch 5 taken 50603 times.
✓ Branch 6 taken 34959 times.
108948 if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op)
85 73989 return AVERROR(ENOTSUP);
86
2/2
✓ Branch 0 taken 70591 times.
✓ Branch 1 taken 34959 times.
105550 for (int i = 0; i < op->rw.elems; i++)
87 70591 p.index[i] = i;
88 34959 break;
89
90 65 case SWS_OP_SWIZZLE: {
91 65 const MemcpyPriv orig = p;
92
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 9 times.
161 for (int i = 0; i < 4; i++) {
93 /* Explicitly exclude swizzle masks that contain duplicates,
94 * because these are wasteful to implement as a memcpy */
95
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 96 times.
214 for (int j = 0; j < i; j++) {
96
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 62 times.
118 if (op->swizzle.in[i] == op->swizzle.in[j])
97 56 return AVERROR(ENOTSUP);
98 }
99 96 p.index[i] = orig.index[op->swizzle.in[i]];
100 }
101 9 break;
102 }
103
104 8996 case SWS_OP_CLEAR:
105
2/2
✓ Branch 0 taken 25565 times.
✓ Branch 1 taken 5484 times.
31049 for (int i = 0; i < 4; i++) {
106
2/2
✓ Branch 0 taken 14556 times.
✓ Branch 1 taken 11009 times.
25565 if (!SWS_COMP_TEST(op->clear.mask, i))
107 14556 continue;
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11009 times.
11009 if (op->clear.value[i].den != 1)
109 return AVERROR(ENOTSUP);
110
111 /* Ensure all bytes to be cleared are the same, because we
112 * can't memset on multi-byte sequences */
113 11009 uint8_t val = op->clear.value[i].num & 0xFF;
114 11009 uint32_t ref = val;
115
3/3
✓ Branch 0 taken 4176 times.
✓ Branch 1 taken 220 times.
✓ Branch 2 taken 6613 times.
11009 switch (ff_sws_pixel_type_size(op->type)) {
116 4176 case 2: ref *= 0x101; break;
117 220 case 4: ref *= 0x1010101; break;
118 }
119
2/2
✓ Branch 0 taken 3512 times.
✓ Branch 1 taken 7497 times.
11009 if (ref != op->clear.value[i].num)
120 3512 return AVERROR(ENOTSUP);
121 7497 p.clear_value[i] = val;
122 7497 p.index[i] = -1;
123 }
124 5484 break;
125
126 5527 case SWS_OP_WRITE:
127
5/6
✓ Branch 1 taken 5463 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 5461 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5461 times.
5527 if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op)
128 66 return AVERROR(ENOTSUP);
129 5461 p.num_planes = op->rw.elems;
130 5461 break;
131
132 34804 default:
133 34804 return AVERROR(ENOTSUP);
134 }
135 }
136
137 5461 *out = (SwsCompiledOp) {
138 .slice_align = 1,
139 .block_size = 1,
140 .func = process,
141 5461 .priv = av_memdup(&p, sizeof(p)),
142 .free = av_free,
143 };
144
1/2
✓ Branch 0 taken 5461 times.
✗ Branch 1 not taken.
5461 return out->priv ? 0 : AVERROR(ENOMEM);
145 }
146
147 const SwsOpBackend backend_murder = {
148 .name = "memcpy",
149 .flags = SWS_BACKEND_MEMCPY,
150 .compile = compile,
151 .hw_format = AV_PIX_FMT_NONE,
152 };
153