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(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 const int lines = y_end - y_start;
46 av_assert1(x_start == 0 && x_end == exec->width);
47
48 for (int i = 0; i < p->num_planes; i++) {
49 uint8_t *out = exec->out[i];
50 const int idx = p->index[i];
51 const int bytes = x_end * exec->block_size_out[i];
52 const int use_loop = exec->out_stride[i] > bytes + SWS_MAX_PADDING;
53 if (idx < 0 && !use_loop) {
54 memset(out, p->clear_value[i], exec->out_stride[i] * lines);
55 } 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 } else if (out == exec->in[idx]) {
61 av_assert1(exec->out_stride[i] == exec->in_stride[idx]);
62 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}
75
76static int compile(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
77{
78 MemcpyPriv p = {0};
79
80 for (int n = 0; n < ops->num_ops; n++) {
81 const SwsOp *op = &ops->ops[n];
82 switch (op->op) {
83 case SWS_OP_READ:
84 if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op)
85 return AVERROR(ENOTSUP);
86 for (int i = 0; i < op->rw.elems; i++)
87 p.index[i] = i;
88 break;
89
90 case SWS_OP_SWIZZLE: {
91 const MemcpyPriv orig = p;
92 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 for (int j = 0; j < i; j++) {
96 if (op->swizzle.in[i] == op->swizzle.in[j])
97 return AVERROR(ENOTSUP);
98 }
99 p.index[i] = orig.index[op->swizzle.in[i]];
100 }
101 break;
102 }
103
104 case SWS_OP_CLEAR:
105 for (int i = 0; i < 4; i++) {
106 if (!SWS_COMP_TEST(op->clear.mask, i))
107 continue;
108 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 uint8_t val = op->clear.value[i].num & 0xFF;
114 uint32_t ref = val;
115 switch (ff_sws_pixel_type_size(op->type)) {
116 case 2: ref *= 0x101; break;
117 case 4: ref *= 0x1010101; break;
118 }
119 if (ref != op->clear.value[i].num)
120 return AVERROR(ENOTSUP);
121 p.clear_value[i] = val;
122 p.index[i] = -1;
123 }
124 break;
125
126 case SWS_OP_WRITE:
127 if (ff_sws_rw_op_planes(op) != op->rw.elems || op->rw.frac || op->rw.filter.op)
128 return AVERROR(ENOTSUP);
129 p.num_planes = op->rw.elems;
130 break;
131
132 default:
133 return AVERROR(ENOTSUP);
134 }
135 }
136
137 *out = (SwsCompiledOp) {
138 .slice_align = 1,
139 .block_size = 1,
140 .func = process,
141 .priv = av_memdup(&p, sizeof(p)),
142 .free = av_free,
143 };
144 return out->priv ? 0 : AVERROR(ENOMEM);
145}
146
148 .name = "memcpy",
149 .flags = SWS_BACKEND_MEMCPY,
150 .compile = compile,
151 .hw_format = AV_PIX_FMT_NONE,
152};
static double val(void *priv, double ch)
Definition aeval.c:77
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 AVERROR(e)
Definition error.h:45
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:302
@ SWS_BACKEND_MEMCPY
Fast path using libc memcpy() / memset()
Definition swscale.h:117
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:147
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 void process(const SwsOpExec *exec, const void *priv, int x_start, int y_start, int x_end, int y_end)
Definition ops_memcpy.c:41
static int compile(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Definition ops_memcpy.c:76
#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:293
SwsOp * ops
Definition ops.h:294
int num_ops
Definition ops.h:295
Definition ops.h:237
#define av_free(p)
static int ref[MAX_W *MAX_W]
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define SWS_COMP_TEST(mask, X)
Definition uops.h:98
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
Definition uops.h:50
static void process(NormalizeContext *s, AVFrame *in, AVFrame *out)