FFmpeg
Loading...
Searching...
No Matches
ops_memcpy.c
Go to the documentation of this file.
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
26typedef struct MemcpyPriv {
28 int index[4]; /* or -1 to clear plane */
29 uint8_t clear_value[4];
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
41static void process_memcpy(const SwsOpExec *exec, const void *priv,
42 int x_start, int y_start, int x_end, int y_end)
43{
44 const MemcpyPriv *p = priv;
45 av_assert1(x_start == 0 && x_end == exec->width);
46
47 for (int i = 0; i < p->num_planes; i++) {
48 uint8_t *out = exec->out[i];
49 const ptrdiff_t out_stride = exec->out_stride[i];
50 const int idx = p->index[i];
51 const uint8_t *in = (idx >= 0) ? exec->in[idx] : NULL;
52 const ptrdiff_t in_stride = (idx >= 0) ? exec->in_stride[idx] : 0;
53
54 if (out == in) {
55 av_assert1(out_stride == in_stride);
56 continue; /* plane was already ref'd */
57 }
58
59 size_t bytes = x_end * exec->block_size_out[i];
60 int lines = y_end - y_start;
61
62 /* Replace the per-line loop with a bulk memset/memcpy when possible. */
63 const size_t abs_stride = FFABS(out_stride);
64 if ((!in || out_stride == in_stride) &&
65 abs_stride <= bytes + SWS_MAX_PADDING) {
66 if (out_stride < 0) {
67 const ptrdiff_t offset = (lines - 1) * out_stride;
68 out += offset;
69 if (in)
70 in += offset;
71 }
72 bytes += (lines - 1) * abs_stride;
73 lines = 1;
74 }
75
76 if (!in) {
77 for (int y = 0; y < lines; y++) {
78 memset(out, p->clear_value[i], bytes);
79 out += out_stride;
80 }
81 } else {
82 for (int y = 0; y < lines; y++) {
83 memcpy(out, in, bytes);
84 out += out_stride;
85 in += in_stride;
86 }
87 }
88 }
89}
90
92{
93 MemcpyPriv p = {0};
94
95 for (int n = 0; n < ops->num_ops; n++) {
96 const SwsOp *op = &ops->ops[n];
97 switch (op->op) {
98 case SWS_OP_READ:
99 if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op)
100 return AVERROR(ENOTSUP);
101 for (int i = 0; i < op->rw.elems; i++)
102 p.index[i] = i;
103 break;
104
105 case SWS_OP_SWIZZLE: {
106 const MemcpyPriv orig = p;
107 for (int i = 0; i < 4; i++) {
108 /* Explicitly exclude swizzle masks that contain duplicates,
109 * because these are wasteful to implement as a memcpy */
110 for (int j = 0; j < i; j++) {
111 if (op->swizzle.in[i] == op->swizzle.in[j])
112 return AVERROR(ENOTSUP);
113 }
114 p.index[i] = orig.index[op->swizzle.in[i]];
115 }
116 break;
117 }
118
119 case SWS_OP_CLEAR:
120 for (int i = 0; i < 4; i++) {
121 if (!SWS_COMP_TEST(op->clear.mask, i))
122 continue;
123 if (op->clear.value[i].den != 1)
124 return AVERROR(ENOTSUP);
125
126 /* Ensure all bytes to be cleared are the same, because we
127 * can't memset on multi-byte sequences */
128 uint8_t val = op->clear.value[i].num & 0xFF;
129 uint32_t ref = val;
130 switch (ff_sws_pixel_type_size(op->clear.type[i])) {
131 case 2: ref *= 0x101; break;
132 case 4: ref *= 0x1010101; break;
133 }
134 if (ref != op->clear.value[i].num)
135 return AVERROR(ENOTSUP);
136 p.clear_value[i] = val;
137 p.index[i] = -1;
138 }
139 break;
140
141 case SWS_OP_WRITE:
142 if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op)
143 return AVERROR(ENOTSUP);
144 p.num_planes = op->rw.elems;
145 break;
146
147 default:
148 return AVERROR(ENOTSUP);
149 }
150 }
151
152 *out = (SwsCompiledOp) {
153 .slice_align = 1,
154 .block_size = 1,
155 .func = process_memcpy,
156 .priv = av_memdup(&p, sizeof(p)),
157 .free = av_free,
158 };
159 return out->priv ? 0 : AVERROR(ENOMEM);
160}
161
163 .name = "memcpy",
164 .flags = SWS_BACKEND_MEMCPY,
165 .compile = compile_ops_memcpy,
166 .hw_format = AV_PIX_FMT_NONE,
167};
static double val(void *priv, double ch)
Definition aeval.c:77
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
#define AVERROR(e)
Definition error.h:45
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
@ SWS_BACKEND_MEMCPY
Fast path using libc memcpy() / memset()
Definition swscale.h:117
unsigned offset
Definition libaomenc.c:763
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition anm.c:76
Memory handling functions.
const SwsOpBackend backend_murder
Definition ops_memcpy.c:162
int ff_sws_rw_op_planes(const SwsOp *op)
Return the number of planes involved in a read/write operation.
Definition ops.c:133
@ SWS_OP_SWIZZLE
Definition ops.h:43
@ SWS_OP_WRITE
Definition ops.h:41
@ SWS_OP_READ
Definition ops.h:40
@ SWS_OP_CLEAR
Definition ops.h:52
static int compile_ops_memcpy(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Definition ops_memcpy.c:91
static void process_memcpy(const SwsOpExec *exec, const void *priv, int x_start, int y_start, int x_end, int y_end)
Definition ops_memcpy.c:41
#define SWS_MAX_PADDING
Switch to loop if total padding exceeds this number of bytes.
Definition ops_memcpy.c:37
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
Copyright (C) 2025 Niklas Haas.
Definition ops_memcpy.c:26
uint8_t clear_value[4]
Definition ops_memcpy.c:29
int num_planes
Definition ops_memcpy.c:27
int index[4]
Definition ops_memcpy.c:28
Main external API structure.
Definition swscale.h:227
Copyright (C) 2026 Niklas Haas.
int32_t block_size_out[4]
ptrdiff_t out_stride[4]
uint8_t * out[4]
const uint8_t * in[4]
ptrdiff_t in_stride[4]
int32_t width
Helper struct for representing a list of operations.
Definition ops.h:318
SwsOp * ops
Definition ops.h:319
int num_ops
Definition ops.h:320
Definition ops.h:252
#define av_free(p)
static int ref[MAX_W *MAX_W]
#define SWS_COMP_TEST(mask, X)
Definition uops.h:134
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
Definition uops.h:51