FFmpeg
filters.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 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 #ifndef SWSCALE_FILTERS_H
22 #define SWSCALE_FILTERS_H
23 
24 #include <libavutil/refstruct.h>
25 
26 #include "swscale.h"
27 
28 /* Design constants of the filtering subsystem */
29 enum {
30  /**
31  * 14-bit coefficients are picked to fit comfortably within int16_t
32  * for efficient SIMD processing (e.g. pmaddwd on x86). Conversely, this
33  * limits the maximum filter size to 256, to avoid excessive precision
34  * loss. (Consider that 14 - 8 = 6 bit effective weight resolution)
35  *
36  * Note that this limitation would not apply to floating-point filters,
37  * and in a future update to this code, we could gain the ability to
38  * generate unbounded floating point filters directly.
39  */
40  SWS_FILTER_SCALE = (1 << 14),
42 };
43 
44 /* Parameters for filter generation. */
45 typedef struct SwsFilterParams {
46  /**
47  * The filter kernel and parameters to use.
48  */
51 
52  /**
53  * The relative sizes of the input and output images. Used to determine
54  * the number of rows in the output, as well as the fractional offsets of
55  * the samples in each row.
56  */
57  int src_size;
58  int dst_size;
59 
60  /**
61  * The virtual output size. If zero, this is assumed to be the same as
62  * `dst_size`. Matters for e.g. chroma subsampling, where the the luma
63  * plane may be smaller than the dst_size. For example, a 99x99 input
64  * image has a chroma size of 50x50, which would be 100x100 after
65  * chroma upscaling; but is sampled only at 99x99 resolution. In this
66  * instance, dst_size is 99x99 and virtual_size is 100x100.
67  *
68  * The upscaling offset from this shift is implicit and does not need
69  * to be accounted for in `offset`. In other words, `offset` is taken
70  * relative to the virtual size, not the sampled size.
71  */
72  double virtual_size;
73 
74  /**
75  * The sample offset, in units of input pixels. This is added onto all
76  * sampled coordinates directly, i.e. a value of offset = 1.0 would shift
77  * the output to the top/left by one whole source pixel.
78  */
79  double offset;
81 
82 /**
83  * Represents a computed filter kernel.
84  */
85 typedef struct SwsFilterWeights {
86  /**
87  * The number of source texels to convolve over for each row.
88  */
90 
91  /**
92  * The computed look-up table (LUT). This is interpreted as a 2D array with
93  * dimensions [dst_height][row_size]. The inner rows contain the `row_size`
94  * samples to convolve with the corresponding input pixels. The outer
95  * coordinate is indexed by the position of the sample to reconstruct.
96  */
97  int *weights; /* refstruct */
98  size_t num_weights;
99 
100  /**
101  * The computed source pixel positions for each row of the filter. This
102  * indexes into the source image, and gives the position of the first
103  * source pixel to convolve with for each entry.
104  */
105  int *offsets; /* refstruct */
106 
107  /**
108  * Copy of the parameters used to generate this filter, for reference.
109  */
110  int src_size;
111  int dst_size;
112  double virtual_size;
113  double offset;
114 
115  /**
116  * Extra metadata about the filter, used to inform the optimizer / range
117  * tracker about the filter's behavior.
118  */
119  char name[16]; /* name of the configured filter kernel */
120  int sum_positive; /* (maximum) sum of all positive weights */
121  int sum_negative; /* (minimum) sum of all negative weights */
123 
124 /**
125  * Generate a filter kernel for the given parameters. The generated filter is
126  * allocated as a refstruct and must be unref'd by the caller.
127  *
128  * Returns 0 or a negative error code. In particular, this may return:
129  * - AVERROR(ENOMEM) if memory allocation fails.
130  * - AVERROR(EINVAL) if the provided parameters are invalid (e.g. out of range).
131  * - AVERROR(ENOTSUP) if the generated filter would exceed SWS_FILTER_SIZE_MAX.
132  **/
133 int ff_sws_filter_generate(void *log_ctx, const SwsFilterParams *params,
135 
136 #endif /* SWSCALE_FILTERS_H */
SwsFilterWeights::offset
double offset
Definition: filters.h:113
SwsFilterWeights::filter_size
int filter_size
The number of source texels to convolve over for each row.
Definition: filters.h:89
out
static FILE * out
Definition: movenc.c:55
SwsFilterParams::src_size
int src_size
The relative sizes of the input and output images.
Definition: filters.h:57
SwsFilterWeights
Represents a computed filter kernel.
Definition: filters.h:85
SwsFilterWeights::offsets
int * offsets
The computed source pixel positions for each row of the filter.
Definition: filters.h:105
SwsFilterParams
Definition: filters.h:45
SwsFilterParams::virtual_size
double virtual_size
The virtual output size.
Definition: filters.h:72
SwsFilterParams::dst_size
int dst_size
Definition: filters.h:58
refstruct.h
SwsFilterParams::offset
double offset
The sample offset, in units of input pixels.
Definition: filters.h:79
SwsFilterWeights::sum_positive
int sum_positive
Definition: filters.h:120
SWS_FILTER_SCALE
@ SWS_FILTER_SCALE
14-bit coefficients are picked to fit comfortably within int16_t for efficient SIMD processing (e....
Definition: filters.h:40
SwsFilterWeights::dst_size
int dst_size
Definition: filters.h:111
SwsScaler
SwsScaler
Definition: swscale.h:96
SwsFilterWeights::src_size
int src_size
Copy of the parameters used to generate this filter, for reference.
Definition: filters.h:110
SwsFilterWeights::virtual_size
double virtual_size
Definition: filters.h:112
SwsFilterWeights::sum_negative
int sum_negative
Definition: filters.h:121
ff_sws_filter_generate
int ff_sws_filter_generate(void *log_ctx, const SwsFilterParams *params, SwsFilterWeights **out)
Generate a filter kernel for the given parameters.
Definition: filters.c:187
SwsFilterWeights::name
char name[16]
Extra metadata about the filter, used to inform the optimizer / range tracker about the filter's beha...
Definition: filters.h:119
SwsFilterParams::scaler_params
double scaler_params[SWS_NUM_SCALER_PARAMS]
Definition: filters.h:50
SWS_NUM_SCALER_PARAMS
#define SWS_NUM_SCALER_PARAMS
Extra parameters for fine-tuning certain scalers.
Definition: swscale.h:247
SwsFilterWeights::num_weights
size_t num_weights
Definition: filters.h:98
SWS_FILTER_SIZE_MAX
@ SWS_FILTER_SIZE_MAX
Definition: filters.h:41
SwsFilterParams::scaler
SwsScaler scaler
The filter kernel and parameters to use.
Definition: filters.h:49
SwsFilterWeights::weights
int * weights
The computed look-up table (LUT).
Definition: filters.h:97
swscale.h