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