FFmpeg
ops_backend.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 "ops_backend.h"
22 
23 /**
24  * We want to disable FP contraction because this is a reference backend that
25  * establishes a bit-exact reference result.
26  */
27 #ifdef __clang__
28 #pragma STDC FP_CONTRACT OFF
29 #elif AV_GCC_VERSION_AT_LEAST(4, 8)
30 #pragma GCC optimize ("fp-contract=off")
31 #elif defined(_MSC_VER)
32 #pragma fp_contract (off)
33 #endif
34 
35 #if AV_GCC_VERSION_AT_LEAST(4, 4)
36 #pragma GCC optimize ("finite-math-only")
37 #endif
38 
39 /* Array-based reference implementation */
40 
41 #ifndef SWS_BLOCK_SIZE
42 # define SWS_BLOCK_SIZE 32
43 #endif
44 
45 typedef uint8_t u8block_t[SWS_BLOCK_SIZE];
46 typedef uint16_t u16block_t[SWS_BLOCK_SIZE];
47 typedef uint32_t u32block_t[SWS_BLOCK_SIZE];
48 typedef float f32block_t[SWS_BLOCK_SIZE];
49 
50 #define BIT_DEPTH 8
51 # include "ops_tmpl_int.c"
52 #undef BIT_DEPTH
53 
54 #define BIT_DEPTH 16
55 # include "ops_tmpl_int.c"
56 #undef BIT_DEPTH
57 
58 #define BIT_DEPTH 32
59 # include "ops_tmpl_int.c"
60 # include "ops_tmpl_float.c"
61 #undef BIT_DEPTH
62 
63 static const SwsOpTable *const tables[] = {
64  &bitfn(op_table_int, u8),
65  &bitfn(op_table_int, u16),
66  &bitfn(op_table_int, u32),
67  &bitfn(op_table_float, f32),
68 };
69 
71 {
72  int ret;
73 
75  if (!chain)
76  return AVERROR(ENOMEM);
77 
78  av_assert0(ops->num_ops > 0);
79  const SwsPixelType read_type = ops->ops[0].type;
80 
81  for (int i = 0; i < ops->num_ops; i++) {
83  ops, i, SWS_BLOCK_SIZE, chain);
84  if (ret < 0) {
85  av_log(ctx, AV_LOG_TRACE, "Failed to compile op %d\n", i);
86  ff_sws_op_chain_free(chain);
87  return ret;
88  }
89  }
90 
91  *out = (SwsCompiledOp) {
92  .slice_align = 1,
93  .block_size = SWS_BLOCK_SIZE,
94  .cpu_flags = chain->cpu_flags,
95  .over_read = chain->over_read,
96  .over_write = chain->over_write,
97  .priv = chain,
99  };
100 
101  switch (read_type) {
102  case SWS_PIXEL_U8: out->func = process_u8; break;
103  case SWS_PIXEL_U16: out->func = process_u16; break;
104  case SWS_PIXEL_U32: out->func = process_u32; break;
105  case SWS_PIXEL_F32: out->func = process_f32; break;
106  default: av_unreachable("Invalid pixel type!");
107  }
108 
109  return 0;
110 }
111 
113  .name = "c",
114  .compile = compile,
115  .hw_format = AV_PIX_FMT_NONE,
116 };
SwsOpTable
Copyright (C) 2025 Niklas Haas.
Definition: ops_chain.h:159
compile
static int compile(SwsContext *ctx, SwsOpList *ops, SwsCompiledOp *out)
Definition: ops_backend.c:70
SWS_PIXEL_U16
@ SWS_PIXEL_U16
Definition: ops.h:36
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ops_backend.h
f32block_t
float f32block_t[SWS_BLOCK_SIZE]
Definition: ops_backend.c:48
out
static FILE * out
Definition: movenc.c:55
backend_c
const SwsOpBackend backend_c
Copyright (C) 2025 Niklas Haas.
Definition: ops_backend.c:112
SWS_PIXEL_U32
@ SWS_PIXEL_U32
Definition: ops.h:37
SwsOpBackend::name
const char * name
Definition: ops_internal.h:56
SwsOpChain::cpu_flags
int cpu_flags
Definition: ops_chain.h:89
SwsPixelType
SwsPixelType
Copyright (C) 2025 Niklas Haas.
Definition: ops.h:33
SWS_PIXEL_F32
@ SWS_PIXEL_F32
Definition: ops.h:38
SwsOpList::num_ops
int num_ops
Definition: ops.h:290
SWS_PIXEL_U8
@ SWS_PIXEL_U8
Definition: ops.h:35
SwsOpChain::over_read
int over_read
Definition: ops_chain.h:90
SwsOpChain::free
void(* free[SWS_MAX_OPS+1])(SwsOpPriv *)
Definition: ops_chain.h:87
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
u8block_t
uint8_t u8block_t[SWS_BLOCK_SIZE]
Definition: ops_backend.c:45
ff_sws_op_chain_alloc
SwsOpChain * ff_sws_op_chain_alloc(void)
Definition: ops_chain.c:29
u16block_t
uint16_t u16block_t[SWS_BLOCK_SIZE]
Definition: ops_backend.c:46
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
u32block_t
uint32_t u32block_t[SWS_BLOCK_SIZE]
Definition: ops_backend.c:47
bitfn
#define bitfn(name, ext)
Definition: ops_backend.h:65
SwsOpBackend
Definition: ops_internal.h:55
ops_tmpl_int.c
SWS_BLOCK_SIZE
#define SWS_BLOCK_SIZE
Copyright (C) 2025 Niklas Haas.
Definition: ops_backend.c:42
SwsOpChain
Compiled "chain" of operations, which can be dispatched efficiently.
Definition: ops_chain.h:84
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:116
ops_tmpl_float.c
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
SwsOp::type
SwsPixelType type
Definition: ops.h:240
ff_sws_op_compile_tables
int ff_sws_op_compile_tables(SwsContext *ctx, const SwsOpTable *const tables[], int num_tables, SwsOpList *ops, int ops_index, const int block_size, SwsOpChain *chain)
"Compile" a single op by looking it up in a list of fixed size op tables.
Definition: ops_chain.c:181
ff_sws_op_chain_free_cb
void ff_sws_op_chain_free_cb(void *ptr)
Definition: ops_chain.c:34
ff_sws_op_chain_free
static void ff_sws_op_chain_free(SwsOpChain *chain)
Definition: ops_chain.h:96
SwsOpList::ops
SwsOp * ops
Definition: ops.h:289
ret
ret
Definition: filter_design.txt:187
SwsCompiledOp
Definition: ops_dispatch.h:100
tables
static const SwsOpTable *const tables[]
Definition: ops_backend.c:63
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:288
SwsContext
Main external API structure.
Definition: swscale.h:206
SwsOpChain::over_write
int over_write
Definition: ops_chain.h:91