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;
60 
61 /**
62  * Represents a computed filter kernel.
63  */
64 typedef struct SwsFilterWeights {
65  /**
66  * The number of source texels to convolve over for each row.
67  */
69 
70  /**
71  * The computed look-up table (LUT). This is interpreted as a 2D array with
72  * dimensions [dst_height][row_size]. The inner rows contain the `row_size`
73  * samples to convolve with the corresponding input pixels. The outer
74  * coordinate is indexed by the position of the sample to reconstruct.
75  */
76  int *weights; /* refstruct */
77  size_t num_weights;
78 
79  /**
80  * The computed source pixel positions for each row of the filter. This
81  * indexes into the source image, and gives the position of the first
82  * source pixel to convolve with for each entry.
83  */
84  int *offsets; /* refstruct */
85 
86  /**
87  * Copy of the parameters used to generate this filter, for reference.
88  */
89  int src_size;
90  int dst_size;
91 
92  /**
93  * Extra metadata about the filter, used to inform the optimizer / range
94  * tracker about the filter's behavior.
95  */
96  char name[16]; /* name of the configured filter kernel */
97  int sum_positive; /* (maximum) sum of all positive weights */
98  int sum_negative; /* (minimum) sum of all negative weights */
100 
101 /**
102  * Generate a filter kernel for the given parameters. The generated filter is
103  * allocated as a refstruct and must be unref'd by the caller.
104  *
105  * Returns 0 or a negative error code. In particular, this may return:
106  * - AVERROR(ENOMEM) if memory allocation fails.
107  * - AVERROR(EINVAL) if the provided parameters are invalid (e.g. out of range).
108  * - AVERROR(ENOTSUP) if the generated filter would exceed SWS_FILTER_SIZE_MAX.
109  **/
110 int ff_sws_filter_generate(void *log_ctx, const SwsFilterParams *params,
112 
113 #endif /* SWSCALE_FILTERS_H */
SwsFilterWeights::filter_size
int filter_size
The number of source texels to convolve over for each row.
Definition: filters.h:68
SWS_FILTER_SIZE_MAX
@ SWS_FILTER_SIZE_MAX
Definition: filters.h:41
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:64
SwsFilterWeights::offsets
int * offsets
The computed source pixel positions for each row of the filter.
Definition: filters.h:84
SwsFilterParams
Definition: filters.h:45
SwsFilterParams::dst_size
int dst_size
Definition: filters.h:58
refstruct.h
SwsFilterWeights::sum_positive
int sum_positive
Definition: filters.h:97
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:90
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:89
SwsFilterWeights::sum_negative
int sum_negative
Definition: filters.h:98
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:183
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:96
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:224
SwsFilterWeights::num_weights
size_t num_weights
Definition: filters.h:77
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:76
swscale.h