1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 module ffmpeg.libavutil.film_grain_params;
19 
20 import ffmpeg.libavutil.frame;
21 
22 extern (C) @nogc nothrow:
23 
24 enum AVFilmGrainParamsType
25 {
26     AV_FILM_GRAIN_PARAMS_NONE = 0,
27 
28     /**
29      * The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
30      */
31     AV_FILM_GRAIN_PARAMS_AV1 = 1
32 }
33 
34 /**
35  * This structure describes how to handle film grain synthesis for AOM codecs.
36  *
37  * @note The struct must be allocated as part of AVFilmGrainParams using
38  *       av_film_grain_params_alloc(). Its size is not a part of the public ABI.
39  */
40 struct AVFilmGrainAOMParams
41 {
42     /**
43      * Number of points, and the scale and value for each point of the
44      * piecewise linear scaling function for the uma plane.
45      */
46     int num_y_points;
47     /* value, scaling */
48     ubyte[/* value, scaling */][14] y_points;
49 
50     /**
51      * Signals whether to derive the chroma scaling function from the luma.
52      * Not equivalent to copying the luma values and scales.
53      */
54     int chroma_scaling_from_luma;
55 
56     /**
57      * If chroma_scaling_from_luma is set to 0, signals the chroma scaling
58      * function parameters.
59      */
60     /* cb, cr */
61     int[/* cb, cr */] num_uv_points;
62     /* cb, cr */ /* value, scaling */
63     ubyte[/* value, scaling */][10][/* cb, cr */] uv_points;
64 
65     /**
66      * Specifies the shift applied to the chroma components. For AV1, its within
67      * [8; 11] and determines the range and quantization of the film grain.
68      */
69     int scaling_shift;
70 
71     /**
72      * Specifies the auto-regression lag.
73      */
74     int ar_coeff_lag;
75 
76     /**
77      * Luma auto-regression coefficients. The number of coefficients is given by
78      * 2 * ar_coeff_lag * (ar_coeff_lag + 1).
79      */
80     byte[24] ar_coeffs_y;
81 
82     /**
83      * Chroma auto-regression coefficients. The number of coefficients is given by
84      * 2 * ar_coeff_lag * (ar_coeff_lag + 1) + !!num_y_points.
85      */
86     /* cb, cr */
87     byte[25][/* cb, cr */] ar_coeffs_uv;
88 
89     /**
90      * Specifies the range of the auto-regressive coefficients. Values of 6,
91      * 7, 8 and so on represent a range of [-2, 2), [-1, 1), [-0.5, 0.5) and
92      * so on. For AV1 must be between 6 and 9.
93      */
94     int ar_coeff_shift;
95 
96     /**
97      * Signals the down shift applied to the generated gaussian numbers during
98      * synthesis.
99      */
100     int grain_scale_shift;
101 
102     /**
103      * Specifies the luma/chroma multipliers for the index to the component
104      * scaling function.
105      */
106     /* cb, cr */
107     int[/* cb, cr */] uv_mult;
108     /* cb, cr */
109     int[/* cb, cr */] uv_mult_luma;
110 
111     /**
112      * Offset used for component scaling function. For AV1 its a 9-bit value
113      * with a range [-256, 255]
114      */
115     /* cb, cr */
116     int[/* cb, cr */] uv_offset;
117 
118     /**
119      * Signals whether to overlap film grain blocks.
120      */
121     int overlap_flag;
122 
123     /**
124      * Signals to clip to limited color levels after film grain application.
125      */
126     int limit_output_range;
127 }
128 
129 /**
130  * This structure describes how to handle film grain synthesis in video
131  * for specific codecs. Must be present on every frame where film grain is
132  * meant to be synthesised for correct presentation.
133  *
134  * @note The struct must be allocated with av_film_grain_params_alloc() and
135  *       its size is not a part of the public ABI.
136  */
137 struct AVFilmGrainParams
138 {
139     /**
140      * Specifies the codec for which this structure is valid.
141      */
142     AVFilmGrainParamsType type;
143 
144     /**
145      * Seed to use for the synthesis process, if the codec allows for it.
146      */
147     ulong seed;
148 
149     /**
150      * Additional fields may be added both here and in any structure included.
151      * If a codec's film grain structure differs slightly over another
152      * codec's, fields within may change meaning depending on the type.
153      */
154     union _Anonymous_0
155     {
156         AVFilmGrainAOMParams aom;
157     }
158 
159     _Anonymous_0 codec;
160 }
161 
162 /**
163  * Allocate an AVFilmGrainParams structure and set its fields to
164  * default values. The resulting struct can be freed using av_freep().
165  * If size is not NULL it will be set to the number of bytes allocated.
166  *
167  * @return An AVFilmGrainParams filled with default values or NULL
168  *         on failure.
169  */
170 AVFilmGrainParams* av_film_grain_params_alloc (size_t* size);
171 
172 /**
173  * Allocate a complete AVFilmGrainParams and add it to the frame.
174  *
175  * @param frame The frame which side data is added to.
176  *
177  * @return The AVFilmGrainParams structure to be filled by caller.
178  */
179 AVFilmGrainParams* av_film_grain_params_create_side_data (AVFrame* frame);
180 
181 /* AVUTIL_FILM_GRAIN_PARAMS_H */