| 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 | 11704 | static void process_memcpy(const SwsOpExec *exec, const void *priv, | |
| 42 | int x_start, int y_start, int x_end, int y_end) | ||
| 43 | { | ||
| 44 | 11704 | const MemcpyPriv *p = priv; | |
| 45 | 11704 | const int lines = y_end - y_start; | |
| 46 | av_assert1(x_start == 0 && x_end == exec->width); | ||
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 17291 times.
✓ Branch 1 taken 11704 times.
|
28995 | for (int i = 0; i < p->num_planes; i++) { |
| 49 | 17291 | uint8_t *out = exec->out[i]; | |
| 50 | 17291 | const int idx = p->index[i]; | |
| 51 | 17291 | const int bytes = x_end * exec->block_size_out[i]; | |
| 52 |
2/2✓ Branch 0 taken 17287 times.
✓ Branch 1 taken 4 times.
|
34578 | const int use_loop = exec->out_stride[i] < 0 || |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17287 times.
|
17287 | exec->out_stride[i] > bytes + SWS_MAX_PADDING; |
| 54 | 17291 | const size_t span = (size_t) (lines - 1) * exec->out_stride[i] + bytes; | |
| 55 |
4/4✓ Branch 0 taken 17263 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 17262 times.
✓ Branch 3 taken 1 times.
|
17291 | if (idx < 0 && !use_loop) { |
| 56 | 17262 | memset(out, p->clear_value[i], span); | |
| 57 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
|
29 | } else if (idx < 0) { |
| 58 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1 times.
|
97 | for (int y = y_start; y < y_end; y++) { |
| 59 | 96 | memset(out, p->clear_value[i], bytes); | |
| 60 | 96 | out += exec->out_stride[i]; | |
| 61 | } | ||
| 62 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 6 times.
|
28 | } else if (out == exec->in[idx]) { |
| 63 | av_assert1(exec->out_stride[i] == exec->in_stride[idx]); | ||
| 64 | 22 | continue; /* plane was already ref'd */ | |
| 65 |
3/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
|
6 | } else if (exec->out_stride[i] == exec->in_stride[idx] && !use_loop) { |
| 66 | 3 | memcpy(out, exec->in[idx], span); | |
| 67 | } else { | ||
| 68 | 3 | const uint8_t *in = exec->in[idx]; | |
| 69 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 3 times.
|
291 | for (int y = y_start; y < y_end; y++) { |
| 70 | 288 | memcpy(out, in, bytes); | |
| 71 | 288 | out += exec->out_stride[i]; | |
| 72 | 288 | in += exec->in_stride[idx]; | |
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | 11704 | } | |
| 77 | |||
| 78 | 154330 | static int compile_ops_memcpy(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out) | |
| 79 | { | ||
| 80 | 154330 | MemcpyPriv p = {0}; | |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 225266 times.
✓ Branch 1 taken 13444 times.
|
238710 | for (int n = 0; n < ops->num_ops; n++) { |
| 83 | 225266 | const SwsOp *op = &ops->ops[n]; | |
| 84 |
5/5✓ Branch 0 taken 134957 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 19433 times.
✓ Branch 3 taken 13519 times.
✓ Branch 4 taken 57264 times.
|
225266 | switch (op->op) { |
| 85 | 134957 | case SWS_OP_READ: | |
| 86 |
6/6✓ Branch 1 taken 96514 times.
✓ Branch 2 taken 38443 times.
✓ Branch 3 taken 90497 times.
✓ Branch 4 taken 6017 times.
✓ Branch 5 taken 33035 times.
✓ Branch 6 taken 57462 times.
|
134957 | if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op) |
| 87 | 77495 | return AVERROR(ENOTSUP); | |
| 88 |
2/2✓ Branch 0 taken 117335 times.
✓ Branch 1 taken 57462 times.
|
174797 | for (int i = 0; i < op->rw.elems; i++) |
| 89 | 117335 | p.index[i] = i; | |
| 90 | 57462 | break; | |
| 91 | |||
| 92 | 93 | case SWS_OP_SWIZZLE: { | |
| 93 | 93 | const MemcpyPriv orig = p; | |
| 94 |
2/2✓ Branch 0 taken 211 times.
✓ Branch 1 taken 10 times.
|
221 | for (int i = 0; i < 4; i++) { |
| 95 | /* Explicitly exclude swizzle masks that contain duplicates, | ||
| 96 | * because these are wasteful to implement as a memcpy */ | ||
| 97 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 128 times.
|
281 | for (int j = 0; j < i; j++) { |
| 98 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 70 times.
|
153 | if (op->swizzle.in[i] == op->swizzle.in[j]) |
| 99 | 83 | return AVERROR(ENOTSUP); | |
| 100 | } | ||
| 101 | 128 | p.index[i] = orig.index[op->swizzle.in[i]]; | |
| 102 | } | ||
| 103 | 10 | break; | |
| 104 | } | ||
| 105 | |||
| 106 | 19433 | case SWS_OP_CLEAR: | |
| 107 |
2/2✓ Branch 0 taken 60002 times.
✓ Branch 1 taken 13464 times.
|
73466 | for (int i = 0; i < 4; i++) { |
| 108 |
2/2✓ Branch 0 taken 34825 times.
✓ Branch 1 taken 25177 times.
|
60002 | if (!SWS_COMP_TEST(op->clear.mask, i)) |
| 109 | 34825 | continue; | |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25177 times.
|
25177 | if (op->clear.value[i].den != 1) |
| 111 | ✗ | return AVERROR(ENOTSUP); | |
| 112 | |||
| 113 | /* Ensure all bytes to be cleared are the same, because we | ||
| 114 | * can't memset on multi-byte sequences */ | ||
| 115 | 25177 | uint8_t val = op->clear.value[i].num & 0xFF; | |
| 116 | 25177 | uint32_t ref = val; | |
| 117 |
3/3✓ Branch 0 taken 7038 times.
✓ Branch 1 taken 383 times.
✓ Branch 2 taken 17756 times.
|
25177 | switch (ff_sws_pixel_type_size(op->clear.type[i])) { |
| 118 | 7038 | case 2: ref *= 0x101; break; | |
| 119 | 383 | case 4: ref *= 0x1010101; break; | |
| 120 | } | ||
| 121 |
2/2✓ Branch 0 taken 5969 times.
✓ Branch 1 taken 19208 times.
|
25177 | if (ref != op->clear.value[i].num) |
| 122 | 5969 | return AVERROR(ENOTSUP); | |
| 123 | 19208 | p.clear_value[i] = val; | |
| 124 | 19208 | p.index[i] = -1; | |
| 125 | } | ||
| 126 | 13464 | break; | |
| 127 | |||
| 128 | 13519 | case SWS_OP_WRITE: | |
| 129 |
5/6✓ Branch 1 taken 13449 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 13444 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13444 times.
|
13519 | if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op) |
| 130 | 75 | return AVERROR(ENOTSUP); | |
| 131 | 13444 | p.num_planes = op->rw.elems; | |
| 132 | 13444 | break; | |
| 133 | |||
| 134 | 57264 | default: | |
| 135 | 57264 | return AVERROR(ENOTSUP); | |
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | 13444 | *out = (SwsCompiledOp) { | |
| 140 | .slice_align = 1, | ||
| 141 | .block_size = 1, | ||
| 142 | .func = process_memcpy, | ||
| 143 | 13444 | .priv = av_memdup(&p, sizeof(p)), | |
| 144 | .free = av_free, | ||
| 145 | }; | ||
| 146 |
1/2✓ Branch 0 taken 13444 times.
✗ Branch 1 not taken.
|
13444 | return out->priv ? 0 : AVERROR(ENOMEM); |
| 147 | } | ||
| 148 | |||
| 149 | const SwsOpBackend backend_murder = { | ||
| 150 | .name = "memcpy", | ||
| 151 | .flags = SWS_BACKEND_MEMCPY, | ||
| 152 | .compile = compile_ops_memcpy, | ||
| 153 | .hw_format = AV_PIX_FMT_NONE, | ||
| 154 | }; | ||
| 155 |