[FFmpeg-devel] [PATCH v3 06/17] swscale/ops: introduce new low level framework
Niklas Haas
ffmpeg at haasn.xyz
Tue May 27 10:55:25 EEST 2025
From: Niklas Haas <git at haasn.dev>
See docs/swscale-v2.txt for an in-depth introduction to the new approach.
This commit merely introduces the ops definitions and boilerplate functions.
The subsequent commits will flesh out the underlying implementation.
---
libswscale/Makefile | 1 +
libswscale/ops.c | 522 ++++++++++++++++++++++++++++++++++++++++++++
libswscale/ops.h | 240 ++++++++++++++++++++
3 files changed, 763 insertions(+)
create mode 100644 libswscale/ops.c
create mode 100644 libswscale/ops.h
diff --git a/libswscale/Makefile b/libswscale/Makefile
index d5e10d17dc..e0beef4e69 100644
--- a/libswscale/Makefile
+++ b/libswscale/Makefile
@@ -15,6 +15,7 @@ OBJS = alphablend.o \
graph.o \
input.o \
lut3d.o \
+ ops.o \
options.o \
output.o \
rgb2rgb.o \
diff --git a/libswscale/ops.c b/libswscale/ops.c
new file mode 100644
index 0000000000..004686147d
--- /dev/null
+++ b/libswscale/ops.c
@@ -0,0 +1,522 @@
+/**
+ * Copyright (C) 2025 Niklas Haas
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/bswap.h"
+#include "libavutil/mem.h"
+#include "libavutil/rational.h"
+#include "libavutil/refstruct.h"
+
+#include "ops.h"
+
+#define Q(N) ((AVRational) { N, 1 })
+
+const char *ff_sws_pixel_type_name(SwsPixelType type)
+{
+ switch (type) {
+ case SWS_PIXEL_U8: return "u8";
+ case SWS_PIXEL_U16: return "u16";
+ case SWS_PIXEL_U32: return "u32";
+ case SWS_PIXEL_F32: return "f32";
+ case SWS_PIXEL_NONE: return "none";
+ case SWS_PIXEL_TYPE_NB: break;
+ }
+
+ av_assert0(!"Invalid pixel type!");
+ return "ERR";
+}
+
+int ff_sws_pixel_type_size(SwsPixelType type)
+{
+ switch (type) {
+ case SWS_PIXEL_U8: return sizeof(uint8_t);
+ case SWS_PIXEL_U16: return sizeof(uint16_t);
+ case SWS_PIXEL_U32: return sizeof(uint32_t);
+ case SWS_PIXEL_F32: return sizeof(float);
+ case SWS_PIXEL_NONE: break;
+ case SWS_PIXEL_TYPE_NB: break;
+ }
+
+ av_assert0(!"Invalid pixel type!");
+ return 0;
+}
+
+bool ff_sws_pixel_type_is_int(SwsPixelType type)
+{
+ switch (type) {
+ case SWS_PIXEL_U8:
+ case SWS_PIXEL_U16:
+ case SWS_PIXEL_U32:
+ return true;
+ case SWS_PIXEL_F32:
+ return false;
+ case SWS_PIXEL_NONE:
+ case SWS_PIXEL_TYPE_NB: break;
+ }
+
+ av_assert0(!"Invalid pixel type!");
+ return false;
+}
+
+SwsPixelType ff_sws_pixel_type_to_uint(SwsPixelType type)
+{
+ if (!type)
+ return type;
+
+ switch (ff_sws_pixel_type_size(type)) {
+ case 8: return SWS_PIXEL_U8;
+ case 16: return SWS_PIXEL_U16;
+ case 32: return SWS_PIXEL_U32;
+ }
+
+ av_assert0(!"Invalid pixel type!");
+ return SWS_PIXEL_NONE;
+}
+
+/* biased towards `a` */
+static AVRational av_min_q(AVRational a, AVRational b)
+{
+ return av_cmp_q(a, b) == 1 ? b : a;
+}
+
+static AVRational av_max_q(AVRational a, AVRational b)
+{
+ return av_cmp_q(a, b) == -1 ? b : a;
+}
+
+static AVRational expand_factor(SwsPixelType from, SwsPixelType to)
+{
+ const int src = ff_sws_pixel_type_size(from);
+ const int dst = ff_sws_pixel_type_size(to);
+ int scale = 0;
+ for (int i = 0; i < dst / src; i++)
+ scale = scale << src * 8 | 1;
+ return Q(scale);
+}
+
+void ff_sws_apply_op_q(const SwsOp *op, AVRational x[4])
+{
+ switch (op->op) {
+ case SWS_OP_READ:
+ case SWS_OP_WRITE:
+ return;
+ case SWS_OP_UNPACK: {
+ unsigned val = x[0].num;
+ int shift = ff_sws_pixel_type_size(op->type) * 8;
+ for (int i = 0; i < 4; i++) {
+ const unsigned mask = (1 << op->pack.pattern[i]) - 1;
+ shift -= op->pack.pattern[i];
+ x[i] = Q((val >> shift) & mask);
+ }
+ return;
+ }
+ case SWS_OP_PACK: {
+ unsigned val = 0;
+ int shift = ff_sws_pixel_type_size(op->type) * 8;
+ for (int i = 0; i < 4; i++) {
+ const unsigned mask = (1 << op->pack.pattern[i]) - 1;
+ shift -= op->pack.pattern[i];
+ val |= (x[i].num & mask) << shift;
+ }
+ x[0] = Q(val);
+ return;
+ }
+ case SWS_OP_SWAP_BYTES:
+ switch (ff_sws_pixel_type_size(op->type)) {
+ case 2:
+ for (int i = 0; i < 4; i++)
+ x[i].num = av_bswap16(x[i].num);
+ break;
+ case 4:
+ for (int i = 0; i < 4; i++)
+ x[i].num = av_bswap32(x[i].num);
+ break;
+ }
+ return;
+ case SWS_OP_CLEAR:
+ for (int i = 0; i < 4; i++) {
+ if (op->c.q4[i].den)
+ x[i] = op->c.q4[i];
+ }
+ return;
+ case SWS_OP_LSHIFT: {
+ AVRational mult = Q(1 << op->c.u);
+ for (int i = 0; i < 4; i++)
+ x[i] = x[i].den ? av_mul_q(x[i], mult) : x[i];
+ return;
+ }
+ case SWS_OP_RSHIFT: {
+ AVRational mult = Q(1 << op->c.u);
+ for (int i = 0; i < 4; i++)
+ x[i] = x[i].den ? av_div_q(x[i], mult) : x[i];
+ return;
+ }
+ case SWS_OP_SWIZZLE: {
+ const AVRational orig[4] = { x[0], x[1], x[2], x[3] };
+ for (int i = 0; i < 4; i++)
+ x[i] = orig[op->swizzle.in[i]];
+ return;
+ }
+ case SWS_OP_CONVERT:
+ if (ff_sws_pixel_type_is_int(op->convert.to)) {
+ const AVRational scale = expand_factor(op->type, op->convert.to);
+ for (int i = 0; i < 4; i++) {
+ x[i] = x[i].den ? Q(x[i].num / x[i].den) : x[i];
+ if (op->convert.expand)
+ x[i] = av_mul_q(x[i], scale);
+ }
+ }
+ return;
+ case SWS_OP_DITHER:
+ for (int i = 0; i < 4; i++)
+ x[i] = x[i].den ? av_add_q(x[i], av_make_q(1, 2)) : x[i];
+ return;
+ case SWS_OP_MIN:
+ for (int i = 0; i < 4; i++)
+ x[i] = av_min_q(x[i], op->c.q4[i]);
+ return;
+ case SWS_OP_MAX:
+ for (int i = 0; i < 4; i++)
+ x[i] = av_max_q(x[i], op->c.q4[i]);
+ return;
+ case SWS_OP_LINEAR: {
+ const AVRational orig[4] = { x[0], x[1], x[2], x[3] };
+ for (int i = 0; i < 4; i++) {
+ AVRational sum = op->lin.m[i][4];
+ for (int j = 0; j < 4; j++)
+ sum = av_add_q(sum, av_mul_q(orig[j], op->lin.m[i][j]));
+ x[i] = sum;
+ }
+ return;
+ }
+ case SWS_OP_SCALE:
+ for (int i = 0; i < 4; i++)
+ x[i] = x[i].den ? av_mul_q(x[i], op->c.q) : x[i];
+ return;
+ }
+
+ av_assert0(!"Invalid operation type!");
+}
+
+static void op_uninit(SwsOp *op)
+{
+ switch (op->op) {
+ case SWS_OP_DITHER:
+ av_refstruct_unref(&op->dither.matrix);
+ break;
+ }
+
+ *op = (SwsOp) {0};
+}
+
+SwsOpList *ff_sws_op_list_alloc(void)
+{
+ SwsOpList *ops = av_mallocz(sizeof(SwsOpList));
+ if (!ops)
+ return NULL;
+
+ ff_fmt_clear(&ops->src);
+ ff_fmt_clear(&ops->dst);
+ return ops;
+}
+
+void ff_sws_op_list_free(SwsOpList **p_ops)
+{
+ SwsOpList *ops = *p_ops;
+ if (!ops)
+ return;
+
+ for (int i = 0; i < ops->num_ops; i++)
+ op_uninit(&ops->ops[i]);
+
+ av_freep(&ops->ops);
+ av_free(ops);
+ *p_ops = NULL;
+}
+
+SwsOpList *ff_sws_op_list_duplicate(const SwsOpList *ops)
+{
+ SwsOpList *copy = av_malloc(sizeof(*copy));
+ if (!copy)
+ return NULL;
+
+ *copy = *ops;
+ copy->ops = av_memdup(ops->ops, ops->num_ops * sizeof(ops->ops[0]));
+ if (!copy->ops) {
+ av_free(copy);
+ return NULL;
+ }
+
+ for (int i = 0; i < ops->num_ops; i++) {
+ const SwsOp *op = &ops->ops[i];
+ switch (op->op) {
+ case SWS_OP_DITHER:
+ av_refstruct_ref(copy->ops[i].dither.matrix);
+ break;
+ }
+ }
+
+ return copy;
+}
+
+void ff_sws_op_list_remove_at(SwsOpList *ops, int index, int count)
+{
+ const int end = ops->num_ops - count;
+ av_assert2(index >= 0 && count >= 0 && index + count <= ops->num_ops);
+ op_uninit(&ops->ops[index]);
+ for (int i = index; i < end; i++)
+ ops->ops[i] = ops->ops[i + count];
+ ops->num_ops = end;
+}
+
+int ff_sws_op_list_insert_at(SwsOpList *ops, int index, SwsOp *op)
+{
+ void *ret;
+ ret = av_dynarray2_add((void **) &ops->ops, &ops->num_ops, sizeof(*op),
+ (const void *) op);
+ if (!ret) {
+ op_uninit(op);
+ return AVERROR(ENOMEM);
+ }
+
+ for (int i = ops->num_ops - 1; i > index; i--)
+ ops->ops[i] = ops->ops[i - 1];
+ ops->ops[index] = *op;
+ *op = (SwsOp) {0};
+ return 0;
+}
+
+int ff_sws_op_list_append(SwsOpList *ops, SwsOp *op)
+{
+ return ff_sws_op_list_insert_at(ops, ops->num_ops, op);
+}
+
+int ff_sws_op_list_max_size(const SwsOpList *ops)
+{
+ int max_size = 0;
+ for (int i = 0; i < ops->num_ops; i++) {
+ const int size = ff_sws_pixel_type_size(ops->ops[i].type);
+ max_size = FFMAX(max_size, size);
+ }
+
+ return max_size;
+}
+
+uint32_t ff_sws_linear_mask(const SwsLinearOp c)
+{
+ uint32_t mask = 0;
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 5; j++) {
+ if (av_cmp_q(c.m[i][j], Q(i == j)))
+ mask |= SWS_MASK(i, j);
+ }
+ }
+ return mask;
+}
+
+static const char *describe_lin_mask(uint32_t mask)
+{
+ /* Try to be fairly descriptive without assuming too much */
+ static const struct {
+ const char *name;
+ uint32_t mask;
+ } patterns[] = {
+ { "noop", 0 },
+ { "luma", SWS_MASK_LUMA },
+ { "alpha", SWS_MASK_ALPHA },
+ { "luma+alpha", SWS_MASK_LUMA | SWS_MASK_ALPHA },
+ { "dot3", 0b111 },
+ { "dot4", 0b1111 },
+ { "row0", SWS_MASK_ROW(0) },
+ { "row0+alpha", SWS_MASK_ROW(0) | SWS_MASK_ALPHA },
+ { "col0", SWS_MASK_COL(0) },
+ { "col0+off3", SWS_MASK_COL(0) | SWS_MASK_OFF3 },
+ { "off3", SWS_MASK_OFF3 },
+ { "off3+alpha", SWS_MASK_OFF3 | SWS_MASK_ALPHA },
+ { "diag3", SWS_MASK_DIAG3 },
+ { "diag4", SWS_MASK_DIAG4 },
+ { "diag3+alpha", SWS_MASK_DIAG3 | SWS_MASK_ALPHA },
+ { "diag3+off3", SWS_MASK_DIAG3 | SWS_MASK_OFF3 },
+ { "diag3+off3+alpha", SWS_MASK_DIAG3 | SWS_MASK_OFF3 | SWS_MASK_ALPHA },
+ { "diag4+off4", SWS_MASK_DIAG4 | SWS_MASK_OFF4 },
+ { "matrix3", SWS_MASK_MAT3 },
+ { "matrix3+off3", SWS_MASK_MAT3 | SWS_MASK_OFF3 },
+ { "matrix3+off3+alpha", SWS_MASK_MAT3 | SWS_MASK_OFF3 | SWS_MASK_ALPHA },
+ { "matrix4", SWS_MASK_MAT4 },
+ { "matrix4+off4", SWS_MASK_MAT4 | SWS_MASK_OFF4 },
+ };
+
+ for (int i = 0; i < FF_ARRAY_ELEMS(patterns); i++) {
+ if (!(mask & ~patterns[i].mask))
+ return patterns[i].name;
+ }
+
+ return "full";
+}
+
+static char describe_comp_flags(unsigned flags)
+{
+ if (flags & SWS_COMP_GARBAGE)
+ return 'X';
+ else if (flags & SWS_COMP_ZERO)
+ return '0';
+ else if (flags & SWS_COMP_EXACT)
+ return '+';
+ else
+ return '.';
+}
+
+static const char *print_q(const AVRational q, char buf[], int buf_len)
+{
+ if (!q.den) {
+ return q.num > 0 ? "inf" : q.num < 0 ? "-inf" : "nan";
+ } else if (q.den == 1) {
+ snprintf(buf, buf_len, "%d", q.num);
+ return buf;
+ } else if (abs(q.num) > 1000 || abs(q.den) > 1000) {
+ snprintf(buf, buf_len, "%f", av_q2d(q));
+ return buf;
+ } else {
+ snprintf(buf, buf_len, "%d/%d", q.num, q.den);
+ return buf;
+ }
+}
+
+#define PRINTQ(q) print_q(q, (char[32]){0}, sizeof(char[32]) - 1)
+
+void ff_sws_op_list_print(void *log, int lev, const SwsOpList *ops)
+{
+ if (!ops->num_ops) {
+ av_log(log, lev, " (empty)\n");
+ return;
+ }
+
+ for (int i = 0; i < ops->num_ops; i++) {
+ const SwsOp *op = &ops->ops[i];
+ av_log(log, lev, " [%3s %c%c%c%c -> %c%c%c%c] ",
+ ff_sws_pixel_type_name(op->type),
+ op->comps.unused[0] ? 'X' : '.',
+ op->comps.unused[1] ? 'X' : '.',
+ op->comps.unused[2] ? 'X' : '.',
+ op->comps.unused[3] ? 'X' : '.',
+ describe_comp_flags(op->comps.flags[0]),
+ describe_comp_flags(op->comps.flags[1]),
+ describe_comp_flags(op->comps.flags[2]),
+ describe_comp_flags(op->comps.flags[3]));
+
+ switch (op->op) {
+ case SWS_OP_INVALID:
+ av_log(log, lev, "SWS_OP_INVALID\n");
+ break;
+ case SWS_OP_READ:
+ case SWS_OP_WRITE:
+ av_log(log, lev, "%-20s: %d elem(s) %s >> %d\n",
+ op->op == SWS_OP_READ ? "SWS_OP_READ"
+ : "SWS_OP_WRITE",
+ op->rw.elems, op->rw.packed ? "packed" : "planar",
+ op->rw.frac);
+ break;
+ case SWS_OP_SWAP_BYTES:
+ av_log(log, lev, "SWS_OP_SWAP_BYTES\n");
+ break;
+ case SWS_OP_LSHIFT:
+ av_log(log, lev, "%-20s: << %u\n", "SWS_OP_LSHIFT", op->c.u);
+ break;
+ case SWS_OP_RSHIFT:
+ av_log(log, lev, "%-20s: >> %u\n", "SWS_OP_RSHIFT", op->c.u);
+ break;
+ case SWS_OP_PACK:
+ case SWS_OP_UNPACK:
+ av_log(log, lev, "%-20s: {%d %d %d %d}\n",
+ op->op == SWS_OP_PACK ? "SWS_OP_PACK"
+ : "SWS_OP_UNPACK",
+ op->pack.pattern[0], op->pack.pattern[1],
+ op->pack.pattern[2], op->pack.pattern[3]);
+ break;
+ case SWS_OP_CLEAR:
+ av_log(log, lev, "%-20s: {%s %s %s %s}\n", "SWS_OP_CLEAR",
+ op->c.q4[0].den ? PRINTQ(op->c.q4[0]) : "_",
+ op->c.q4[1].den ? PRINTQ(op->c.q4[1]) : "_",
+ op->c.q4[2].den ? PRINTQ(op->c.q4[2]) : "_",
+ op->c.q4[3].den ? PRINTQ(op->c.q4[3]) : "_");
+ break;
+ case SWS_OP_SWIZZLE:
+ av_log(log, lev, "%-20s: %d%d%d%d\n", "SWS_OP_SWIZZLE",
+ op->swizzle.x, op->swizzle.y, op->swizzle.z, op->swizzle.w);
+ break;
+ case SWS_OP_CONVERT:
+ av_log(log, lev, "%-20s: %s -> %s%s\n", "SWS_OP_CONVERT",
+ ff_sws_pixel_type_name(op->type),
+ ff_sws_pixel_type_name(op->convert.to),
+ op->convert.expand ? " (expand)" : "");
+ break;
+ case SWS_OP_DITHER:
+ av_log(log, lev, "%-20s: %dx%d matrix\n", "SWS_OP_DITHER",
+ 1 << op->dither.size_log2, 1 << op->dither.size_log2);
+ break;
+ case SWS_OP_MIN:
+ av_log(log, lev, "%-20s: x <= {%s %s %s %s}\n", "SWS_OP_MIN",
+ op->c.q4[0].den ? PRINTQ(op->c.q4[0]) : "_",
+ op->c.q4[1].den ? PRINTQ(op->c.q4[1]) : "_",
+ op->c.q4[2].den ? PRINTQ(op->c.q4[2]) : "_",
+ op->c.q4[3].den ? PRINTQ(op->c.q4[3]) : "_");
+ break;
+ case SWS_OP_MAX:
+ av_log(log, lev, "%-20s: {%s %s %s %s} <= x\n", "SWS_OP_MAX",
+ op->c.q4[0].den ? PRINTQ(op->c.q4[0]) : "_",
+ op->c.q4[1].den ? PRINTQ(op->c.q4[1]) : "_",
+ op->c.q4[2].den ? PRINTQ(op->c.q4[2]) : "_",
+ op->c.q4[3].den ? PRINTQ(op->c.q4[3]) : "_");
+ break;
+ case SWS_OP_LINEAR:
+ av_log(log, lev, "%-20s: %s [[%s %s %s %s %s] "
+ "[%s %s %s %s %s] "
+ "[%s %s %s %s %s] "
+ "[%s %s %s %s %s]]\n",
+ "SWS_OP_LINEAR", describe_lin_mask(op->lin.mask),
+ PRINTQ(op->lin.m[0][0]), PRINTQ(op->lin.m[0][1]), PRINTQ(op->lin.m[0][2]), PRINTQ(op->lin.m[0][3]), PRINTQ(op->lin.m[0][4]),
+ PRINTQ(op->lin.m[1][0]), PRINTQ(op->lin.m[1][1]), PRINTQ(op->lin.m[1][2]), PRINTQ(op->lin.m[1][3]), PRINTQ(op->lin.m[1][4]),
+ PRINTQ(op->lin.m[2][0]), PRINTQ(op->lin.m[2][1]), PRINTQ(op->lin.m[2][2]), PRINTQ(op->lin.m[2][3]), PRINTQ(op->lin.m[2][4]),
+ PRINTQ(op->lin.m[3][0]), PRINTQ(op->lin.m[3][1]), PRINTQ(op->lin.m[3][2]), PRINTQ(op->lin.m[3][3]), PRINTQ(op->lin.m[3][4]));
+ break;
+ case SWS_OP_SCALE:
+ av_log(log, lev, "%-20s: * %s\n", "SWS_OP_SCALE",
+ PRINTQ(op->c.q));
+ break;
+ case SWS_OP_TYPE_NB:
+ break;
+ }
+
+ if (op->comps.min[0].den || op->comps.min[1].den ||
+ op->comps.min[2].den || op->comps.min[3].den ||
+ op->comps.max[0].den || op->comps.max[1].den ||
+ op->comps.max[2].den || op->comps.max[3].den)
+ {
+ av_log(log, AV_LOG_TRACE, " min: {%s, %s, %s, %s}, max: {%s, %s, %s, %s}\n",
+ PRINTQ(op->comps.min[0]), PRINTQ(op->comps.min[1]),
+ PRINTQ(op->comps.min[2]), PRINTQ(op->comps.min[3]),
+ PRINTQ(op->comps.max[0]), PRINTQ(op->comps.max[1]),
+ PRINTQ(op->comps.max[2]), PRINTQ(op->comps.max[3]));
+ }
+
+ }
+
+ av_log(log, lev, " (X = unused, + = exact, 0 = zero)\n");
+}
diff --git a/libswscale/ops.h b/libswscale/ops.h
new file mode 100644
index 0000000000..71841c1572
--- /dev/null
+++ b/libswscale/ops.h
@@ -0,0 +1,240 @@
+/**
+ * Copyright (C) 2025 Niklas Haas
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef SWSCALE_OPS_H
+#define SWSCALE_OPS_H
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdalign.h>
+
+#include "graph.h"
+
+typedef enum SwsPixelType {
+ SWS_PIXEL_NONE = 0,
+ SWS_PIXEL_U8,
+ SWS_PIXEL_U16,
+ SWS_PIXEL_U32,
+ SWS_PIXEL_F32,
+ SWS_PIXEL_TYPE_NB
+} SwsPixelType;
+
+const char *ff_sws_pixel_type_name(SwsPixelType type);
+int ff_sws_pixel_type_size(SwsPixelType type) av_const;
+bool ff_sws_pixel_type_is_int(SwsPixelType type) av_const;
+SwsPixelType ff_sws_pixel_type_to_uint(SwsPixelType type) av_const;
+
+typedef enum SwsOpType {
+ SWS_OP_INVALID = 0,
+
+ /* Input/output handling */
+ SWS_OP_READ, /* gather raw pixels from planes */
+ SWS_OP_WRITE, /* write raw pixels to planes */
+ SWS_OP_SWAP_BYTES, /* swap byte order (for differing endianness) */
+ SWS_OP_UNPACK, /* split tightly packed data into components */
+ SWS_OP_PACK, /* compress components into tightly packed data */
+
+ /* Pixel manipulation */
+ SWS_OP_CLEAR, /* clear pixel values */
+ SWS_OP_LSHIFT, /* logical left shift of raw pixel values by (u8) */
+ SWS_OP_RSHIFT, /* right shift of raw pixel values by (u8) */
+ SWS_OP_SWIZZLE, /* rearrange channel order, or duplicate channels */
+ SWS_OP_CONVERT, /* convert (cast) between formats */
+ SWS_OP_DITHER, /* add dithering noise */
+
+ /* Arithmetic operations */
+ SWS_OP_LINEAR, /* generalized linear affine transform */
+ SWS_OP_SCALE, /* multiplication by scalar (q) */
+ SWS_OP_MIN, /* numeric minimum (q4) */
+ SWS_OP_MAX, /* numeric maximum (q4) */
+
+ SWS_OP_TYPE_NB,
+} SwsOpType;
+
+enum SwsCompFlags {
+ SWS_COMP_GARBAGE = 1 << 0, /* contents are undefined / garbage data */
+ SWS_COMP_EXACT = 1 << 1, /* value is an in-range, exact, integer */
+ SWS_COMP_ZERO = 1 << 2, /* known to be a constant zero */
+};
+
+typedef union SwsConst {
+ /* Generic constant value */
+ AVRational q;
+ AVRational q4[4];
+ unsigned u;
+} SwsConst;
+
+typedef struct SwsComps {
+ unsigned flags[4]; /* knowledge about (output) component contents */
+ bool unused[4]; /* which input components are definitely unused */
+
+ /* Keeps track of the known possible value range, or {0, 0} for undefined
+ * or (unknown range) floating point inputs */
+ AVRational min[4], max[4];
+} SwsComps;
+
+typedef struct SwsReadWriteOp {
+ uint8_t elems; /* number of elements (of type `op.type`) to read/write */
+ uint8_t frac; /* fractional pixel step factor (log2) */
+ bool packed; /* read multiple elements from a single plane */
+
+ /** Examples:
+ * rgba = 4x u8 packed
+ * yuv444p = 3x u8
+ * rgb565 = 1x u16 <- use SWS_OP_UNPACK to unpack
+ * monow = 1x u8 (frac 3)
+ * rgb4 = 1x u8 (frac 1)
+ */
+} SwsReadWriteOp;
+
+typedef struct SwsPackOp {
+ uint8_t pattern[4]; /* bit depth pattern, from MSB to LSB */
+} SwsPackOp;
+
+typedef struct SwsSwizzleOp {
+ /**
+ * Input component for each output component:
+ * Out[x] := In[swizzle.in[x]]
+ */
+ union {
+ uint32_t mask;
+ uint8_t in[4];
+ struct { uint8_t x, y, z, w; };
+ };
+} SwsSwizzleOp;
+
+#define SWS_SWIZZLE(X,Y,Z,W) ((SwsSwizzleOp) { .in = {X, Y, Z, W} })
+
+typedef struct SwsConvertOp {
+ SwsPixelType to; /* type of pixel to convert to */
+ bool expand; /* if true, integers are expanded to the full range */
+} SwsConvertOp;
+
+typedef struct SwsDitherOp {
+ AVRational *matrix; /* tightly packed dither matrix (refstruct) */
+ int size_log2; /* size (in bits) of the dither matrix */
+} SwsDitherOp;
+
+typedef struct SwsLinearOp {
+ /**
+ * Generalized 5x5 affine transformation:
+ * [ Out.x ] = [ A B C D E ]
+ * [ Out.y ] = [ F G H I J ] * [ x y z w 1 ]
+ * [ Out.z ] = [ K L M N O ]
+ * [ Out.w ] = [ P Q R S T ]
+ *
+ * The mask keeps track of which components differ from an identity matrix.
+ * There may be more efficient implementations of particular subsets, for
+ * example the common subset of {A, E, G, J, M, O} can be implemented with
+ * just three fused multiply-add operations.
+ */
+ AVRational m[4][5];
+ uint32_t mask; /* m[i][j] <-> 1 << (5 * i + j) */
+} SwsLinearOp;
+
+#define SWS_MASK(I, J) (1 << (5 * (I) + (J)))
+#define SWS_MASK_OFF(I) SWS_MASK(I, 4)
+#define SWS_MASK_ROW(I) (0b11111 << (5 * (I)))
+#define SWS_MASK_COL(J) (0b1000010000100001 << J)
+
+enum {
+ SWS_MASK_ALL = (1 << 20) - 1,
+ SWS_MASK_LUMA = SWS_MASK(0, 0) | SWS_MASK_OFF(0),
+ SWS_MASK_ALPHA = SWS_MASK(3, 3) | SWS_MASK_OFF(3),
+
+ SWS_MASK_DIAG3 = SWS_MASK(0, 0) | SWS_MASK(1, 1) | SWS_MASK(2, 2),
+ SWS_MASK_OFF3 = SWS_MASK_OFF(0) | SWS_MASK_OFF(1) | SWS_MASK_OFF(2),
+ SWS_MASK_MAT3 = SWS_MASK(0, 0) | SWS_MASK(0, 1) | SWS_MASK(0, 2) |
+ SWS_MASK(1, 0) | SWS_MASK(1, 1) | SWS_MASK(1, 2) |
+ SWS_MASK(2, 0) | SWS_MASK(2, 1) | SWS_MASK(2, 2),
+
+ SWS_MASK_DIAG4 = SWS_MASK_DIAG3 | SWS_MASK(3, 3),
+ SWS_MASK_OFF4 = SWS_MASK_OFF3 | SWS_MASK_OFF(3),
+ SWS_MASK_MAT4 = SWS_MASK_ALL & ~SWS_MASK_OFF4,
+};
+
+/* Helper function to compute the correct mask */
+uint32_t ff_sws_linear_mask(SwsLinearOp);
+
+typedef struct SwsOp {
+ SwsOpType op; /* operation to perform */
+ SwsPixelType type; /* pixel type to operate on */
+ union {
+ SwsReadWriteOp rw;
+ SwsPackOp pack;
+ SwsSwizzleOp swizzle;
+ SwsConvertOp convert;
+ SwsDitherOp dither;
+ SwsLinearOp lin;
+ SwsConst c;
+ };
+
+ /* For use internal use inside ff_sws_*() functions */
+ SwsComps comps;
+} SwsOp;
+
+/**
+ * Frees any allocations associated with an SwsOp and sets it to {0}.
+ */
+void ff_sws_op_uninit(SwsOp *op);
+
+/**
+ * Apply an operation to an AVRational. No-op for read/write operations.
+ */
+void ff_sws_apply_op_q(const SwsOp *op, AVRational x[4]);
+
+/**
+ * Helper struct for representing a list of operations.
+ */
+typedef struct SwsOpList {
+ SwsOp *ops;
+ int num_ops;
+
+ /* Purely informative metadata associated with this operation list */
+ SwsFormat src, dst;
+} SwsOpList;
+
+SwsOpList *ff_sws_op_list_alloc(void);
+void ff_sws_op_list_free(SwsOpList **ops);
+
+/**
+ * Returns a duplicate of `ops`, or NULL on OOM.
+ */
+SwsOpList *ff_sws_op_list_duplicate(const SwsOpList *ops);
+
+/**
+ * Returns the size of the largest pixel type used in `ops`.
+ */
+int ff_sws_op_list_max_size(const SwsOpList *ops);
+
+/**
+ * These will take over ownership of `op` and set it to {0}, even on failure.
+ */
+int ff_sws_op_list_append(SwsOpList *ops, SwsOp *op);
+int ff_sws_op_list_insert_at(SwsOpList *ops, int index, SwsOp *op);
+
+void ff_sws_op_list_remove_at(SwsOpList *ops, int index, int count);
+
+/**
+ * Print out the contents of an operation list.
+ */
+void ff_sws_op_list_print(void *log_ctx, int log_level, const SwsOpList *ops);
+
+#endif
--
2.49.0
More information about the ffmpeg-devel
mailing list