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.libavfilter.formats;
19 //#ifndef AVFILTER_FORMATS_H
20 //#define AVFILTER_FORMATS_H
21 import std.stdint;
22 import ffmpeg.libavfilter.avfilter;
23 
24 @nogc nothrow extern(C):
25 /**
26  * A list of supported formats for one end of a filter link. This is used
27  * during the format negotiation process to try to pick the best format to
28  * use to minimize the number of necessary conversions. Each filter gives a
29  * list of the formats supported by each input and output pad. The list
30  * given for each pad need not be distinct - they may be references to the
31  * same list of formats, as is often the case when a filter supports multiple
32  * formats, but will always output the same format as it is given in input.
33  *
34  * In this way, a list of possible input formats and a list of possible
35  * output formats are associated with each link. When a set of formats is
36  * negotiated over a link, the input and output lists are merged to form a
37  * new list containing only the common elements of each list. In the case
38  * that there were no common elements, a format conversion is necessary.
39  * Otherwise, the lists are merged, and all other links which reference
40  * either of the format lists involved in the merge are also affected.
41  *
42  * For example, consider the filter chain:
43  * filter (a) --> (b) filter (b) --> (c) filter
44  *
45  * where the letters in parenthesis indicate a list of formats supported on
46  * the input or output of the link. Suppose the lists are as follows:
47  * (a) = {A, B}
48  * (b) = {A, B, C}
49  * (c) = {B, C}
50  *
51  * First, the first link's lists are merged, yielding:
52  * filter (a) --> (a) filter (a) --> (c) filter
53  *
54  * Notice that format list (b) now refers to the same list as filter list (a).
55  * Next, the lists for the second link are merged, yielding:
56  * filter (a) --> (a) filter (a) --> (a) filter
57  *
58  * where (a) = {B}.
59  *
60  * Unfortunately, when the format lists at the two ends of a link are merged,
61  * we must ensure that all links which reference either pre-merge format list
62  * get updated as well. Therefore, we have the format list structure store a
63  * pointer to each of the pointers to itself.
64  */
65 struct AVFilterFormats {
66     uint nb_formats;        ///< number of formats
67     int *formats;               ///< list of media formats
68     
69     uint refcount;          ///< number of references to this list
70     AVFilterFormats ***refs; ///< references to this list
71 };
72 
73 /**
74  * A list of supported channel layouts.
75  *
76  * The list works the same as AVFilterFormats, except for the following
77  * differences:
78  * - A list with all_layouts = 1 means all channel layouts with a known
79  *   disposition; nb_channel_layouts must then be 0.
80  * - A list with all_counts = 1 means all channel counts, with a known or
81  *   unknown disposition; nb_channel_layouts must then be 0 and all_layouts 1.
82  * - The list must not contain a layout with a known disposition and a
83  *   channel count with unknown disposition with the same number of channels
84  *   (e.g. AV_CH_LAYOUT_STEREO and FF_COUNT2LAYOUT(2).
85  */
86 struct AVFilterChannelLayouts {
87     uint64_t *channel_layouts;  ///< list of channel layouts
88     int    nb_channel_layouts;  ///< number of channel layouts
89     char all_layouts;           ///< accept any known channel layout
90     char all_counts;            ///< accept any channel layout or count
91     
92     uint refcount;          ///< number of references to this list
93     AVFilterChannelLayouts ***refs; ///< references to this list
94 } 
95 
96 /**
97  * Encode a channel count as a channel layout.
98  * FF_COUNT2LAYOUT(c) means any channel layout with c channels, with a known
99  * or unknown disposition.
100  * The result is only valid inside AVFilterChannelLayouts and immediately
101  * related functions.
102  */
103 template FF_COUNT2LAYOUT(int c) {
104     enum FF_COUNT2LAYOUT = (0x8000000000000000 | (c));
105 }
106 
107 /**
108  * Decode a channel count encoded as a channel layout.
109  * Return 0 if the channel layout was a real one.
110  */
111 template FF_LAYOUT2COUNT(int l) {
112     enum FF_LAYOUT2COUNT = (((l) & 0x8000000000000000) ? ((l) & 0x7FFFFFFF) : 0);
113 }
114 
115 /**
116  * Return a channel layouts/samplerates list which contains the intersection of
117  * the layouts/samplerates of a and b. Also, all the references of a, all the
118  * references of b, and a and b themselves will be deallocated.
119  *
120  * If a and b do not share any common elements, neither is modified, and NULL
121  * is returned.
122  */
123 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
124                                                  AVFilterChannelLayouts *b);
125 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
126                                       AVFilterFormats *b);
127 
128 /**
129  * Construct an empty AVFilterChannelLayouts/AVFilterFormats struct --
130  * representing any channel layout (with known disposition)/sample rate.
131  */
132 AVFilterChannelLayouts *ff_all_channel_layouts();
133 AVFilterFormats *ff_all_samplerates();
134 
135 /**
136  * Construct an AVFilterChannelLayouts coding for any channel layout, with
137  * known or unknown disposition.
138  */
139 AVFilterChannelLayouts *ff_all_channel_counts();
140 
141 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts);
142 
143 AVFilterChannelLayouts *ff_make_formatu64_list(const uint64_t *fmts);
144 
145 
146 /**
147  * A helper for query_formats() which sets all links to the same list of channel
148  * layouts/sample rates. If there are no links hooked to this filter, the list
149  * is freed.
150  */
151 void ff_set_common_channel_layouts(AVFilterContext *ctx,
152                                    AVFilterChannelLayouts *layouts);
153 void ff_set_common_samplerates(AVFilterContext *ctx,
154                                AVFilterFormats *samplerates);
155 
156 /**
157  * A helper for query_formats() which sets all links to the same list of
158  * formats. If there are no links hooked to this filter, the list of formats is
159  * freed.
160  */
161 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats);
162 
163 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout);
164 
165 /**
166  * Add *ref as a new reference to f.
167  */
168 void ff_channel_layouts_ref(AVFilterChannelLayouts *f,
169                             AVFilterChannelLayouts **_ref);
170 
171 /**
172  * Remove a reference to a channel layouts list.
173  */
174 void ff_channel_layouts_unref(AVFilterChannelLayouts **_ref);
175 
176 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
177                                   AVFilterChannelLayouts **newref);
178 
179 int ff_default_query_formats(AVFilterContext *ctx);
180 
181 /**
182  * Set the formats list to all existing formats.
183  * This function behaves like ff_default_query_formats(), except it also
184  * accepts channel layouts with unknown disposition. It should only be used
185  * with audio filters.
186  */
187 int ff_query_formats_all(AVFilterContext *ctx);
188 
189 
190 /**
191  * Create a list of supported formats. This is intended for use in
192  * AVFilter->query_formats().
193  *
194  * @param fmts list of media formats, terminated by -1
195  * @return the format list, with no existing references
196  */
197 AVFilterFormats *ff_make_format_list(const int *fmts);
198 
199 /**
200  * Add fmt to the list of media formats contained in *avff.
201  * If *avff is NULL the function allocates the filter formats struct
202  * and puts its pointer in *avff.
203  *
204  * @return a non negative value in case of success, or a negative
205  * value corresponding to an AVERROR code in case of error
206  */
207 int ff_add_format(AVFilterFormats **avff, int64_t fmt);
208 
209 /**
210  * Return a list of all formats supported by FFmpeg for the given media type.
211  */
212 AVFilterFormats *ff_all_formats(ffmpeg.libavutil.avutil.AVMediaType type);
213 
214 /**
215  * Construct a formats list containing all planar sample formats.
216  */
217 AVFilterFormats *ff_planar_sample_fmts();
218 
219 /**
220  * Return a format list which contains the intersection of the formats of
221  * a and b. Also, all the references of a, all the references of b, and
222  * a and b themselves will be deallocated.
223  *
224  * If a and b do not share any common formats, neither is modified, and NULL
225  * is returned.
226  */
227 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
228                                   ffmpeg.libavutil.avutil.AVMediaType type);
229 
230 /**
231  * Add *ref as a new reference to formats.
232  * That is the pointers will point like in the ascii art below:
233  *   ________
234  *  |formats |<--------.
235  *  |  ____  |     ____|___________________
236  *  | |refs| |    |  __|_
237  *  | |* * | |    | |  | |  AVFilterLink
238  *  | |* *--------->|*ref|
239  *  | |____| |    | |____|
240  *  |________|    |________________________
241  */
242 void ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **_ref);
243 
244 /**
245  * If *ref is non-NULL, remove *ref as a reference to the format list
246  * it currently points to, deallocates that list if this was the last
247  * reference, and sets *ref to NULL.
248  *
249  *         Before                                 After
250  *   ________                               ________         NULL
251  *  |formats |<--------.                   |formats |         ^
252  *  |  ____  |     ____|________________   |  ____  |     ____|________________
253  *  | |refs| |    |  __|_                  | |refs| |    |  __|_
254  *  | |* * | |    | |  | |  AVFilterLink   | |* * | |    | |  | |  AVFilterLink
255  *  | |* *--------->|*ref|                 | |*   | |    | |*ref|
256  *  | |____| |    | |____|                 | |____| |    | |____|
257  *  |________|    |_____________________   |________|    |_____________________
258  */
259 void ff_formats_unref(AVFilterFormats **_ref);
260 
261 /**
262  *
263  *         Before                                 After
264  *   ________                         ________
265  *  |formats |<---------.            |formats |<---------.
266  *  |  ____  |       ___|___         |  ____  |       ___|___
267  *  | |refs| |      |   |   |        | |refs| |      |   |   |   NULL
268  *  | |* *--------->|*oldref|        | |* *--------->|*newref|     ^
269  *  | |* * | |      |_______|        | |* * | |      |_______|  ___|___
270  *  | |____| |                       | |____| |                |   |   |
271  *  |________|                       |________|                |*oldref|
272  *                                                             |_______|
273  */
274 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref);
275 
276 //#endif /* AVFILTER_FORMATS_H */
277