]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.h
lavfi: make formats API private on next bump.
[ffmpeg] / libavfilter / formats.h
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef AVFILTER_FORMATS_H
20 #define AVFILTER_FORMATS_H
21
22 #include "avfilter.h"
23
24 /**
25  * A list of supported formats for one end of a filter link. This is used
26  * during the format negotiation process to try to pick the best format to
27  * use to minimize the number of necessary conversions. Each filter gives a
28  * list of the formats supported by each input and output pad. The list
29  * given for each pad need not be distinct - they may be references to the
30  * same list of formats, as is often the case when a filter supports multiple
31  * formats, but will always output the same format as it is given in input.
32  *
33  * In this way, a list of possible input formats and a list of possible
34  * output formats are associated with each link. When a set of formats is
35  * negotiated over a link, the input and output lists are merged to form a
36  * new list containing only the common elements of each list. In the case
37  * that there were no common elements, a format conversion is necessary.
38  * Otherwise, the lists are merged, and all other links which reference
39  * either of the format lists involved in the merge are also affected.
40  *
41  * For example, consider the filter chain:
42  * filter (a) --> (b) filter (b) --> (c) filter
43  *
44  * where the letters in parenthesis indicate a list of formats supported on
45  * the input or output of the link. Suppose the lists are as follows:
46  * (a) = {A, B}
47  * (b) = {A, B, C}
48  * (c) = {B, C}
49  *
50  * First, the first link's lists are merged, yielding:
51  * filter (a) --> (a) filter (a) --> (c) filter
52  *
53  * Notice that format list (b) now refers to the same list as filter list (a).
54  * Next, the lists for the second link are merged, yielding:
55  * filter (a) --> (a) filter (a) --> (a) filter
56  *
57  * where (a) = {B}.
58  *
59  * Unfortunately, when the format lists at the two ends of a link are merged,
60  * we must ensure that all links which reference either pre-merge format list
61  * get updated as well. Therefore, we have the format list structure store a
62  * pointer to each of the pointers to itself.
63  */
64 #if !FF_API_FILTERS_PUBLIC
65 struct AVFilterFormats {
66     unsigned format_count;      ///< number of formats
67     int *formats;               ///< list of media formats
68
69     unsigned refcount;          ///< number of references to this list
70     struct AVFilterFormats ***refs; ///< references to this list
71 };
72 #endif
73
74 typedef struct AVFilterChannelLayouts {
75     uint64_t *channel_layouts;  ///< list of channel layouts
76     int    nb_channel_layouts;  ///< number of channel layouts
77
78     unsigned refcount;          ///< number of references to this list
79     struct AVFilterChannelLayouts ***refs; ///< references to this list
80 } AVFilterChannelLayouts;
81
82 /**
83  * Return a channel layouts/samplerates list which contains the intersection of
84  * the layouts/samplerates of a and b. Also, all the references of a, all the
85  * references of b, and a and b themselves will be deallocated.
86  *
87  * If a and b do not share any common elements, neither is modified, and NULL
88  * is returned.
89  */
90 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
91                                                  AVFilterChannelLayouts *b);
92 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
93                                       AVFilterFormats *b);
94
95 /**
96  * Construct an empty AVFilterChannelLayouts/AVFilterFormats struct --
97  * representing any channel layout/sample rate.
98  */
99 AVFilterChannelLayouts *ff_all_channel_layouts(void);
100 AVFilterFormats *ff_all_samplerates(void);
101
102 /**
103  * A helper for query_formats() which sets all links to the same list of channel
104  * layouts/sample rates. If there are no links hooked to this filter, the list
105  * is freed.
106  */
107 void ff_set_common_channel_layouts(AVFilterContext *ctx,
108                                    AVFilterChannelLayouts *layouts);
109 void ff_set_common_samplerates(AVFilterContext *ctx,
110                                AVFilterFormats *samplerates);
111
112 /**
113  * A helper for query_formats() which sets all links to the same list of
114  * formats. If there are no links hooked to this filter, the list of formats is
115  * freed.
116  */
117 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats);
118
119 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout);
120
121 /**
122  * Add *ref as a new reference to f.
123  */
124 void ff_channel_layouts_ref(AVFilterChannelLayouts *f,
125                             AVFilterChannelLayouts **ref);
126
127 /**
128  * Remove a reference to a channel layouts list.
129  */
130 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref);
131
132 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
133                                   AVFilterChannelLayouts **newref);
134
135 int ff_default_query_formats(AVFilterContext *ctx);
136
137
138 /**
139  * Create a list of supported formats. This is intended for use in
140  * AVFilter->query_formats().
141  *
142  * @param fmts list of media formats, terminated by -1
143  * @return the format list, with no existing references
144  */
145 AVFilterFormats *ff_make_format_list(const int *fmts);
146
147 /**
148  * Add fmt to the list of media formats contained in *avff.
149  * If *avff is NULL the function allocates the filter formats struct
150  * and puts its pointer in *avff.
151  *
152  * @return a non negative value in case of success, or a negative
153  * value corresponding to an AVERROR code in case of error
154  */
155 int ff_add_format(AVFilterFormats **avff, int fmt);
156
157 /**
158  * Return a list of all formats supported by Libav for the given media type.
159  */
160 AVFilterFormats *ff_all_formats(enum AVMediaType type);
161
162 /**
163  * Return a format list which contains the intersection of the formats of
164  * a and b. Also, all the references of a, all the references of b, and
165  * a and b themselves will be deallocated.
166  *
167  * If a and b do not share any common formats, neither is modified, and NULL
168  * is returned.
169  */
170 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b);
171
172 /**
173  * Add *ref as a new reference to formats.
174  * That is the pointers will point like in the ascii art below:
175  *   ________
176  *  |formats |<--------.
177  *  |  ____  |     ____|___________________
178  *  | |refs| |    |  __|_
179  *  | |* * | |    | |  | |  AVFilterLink
180  *  | |* *--------->|*ref|
181  *  | |____| |    | |____|
182  *  |________|    |________________________
183  */
184 void ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
185
186 /**
187  * If *ref is non-NULL, remove *ref as a reference to the format list
188  * it currently points to, deallocates that list if this was the last
189  * reference, and sets *ref to NULL.
190  *
191  *         Before                                 After
192  *   ________                               ________         NULL
193  *  |formats |<--------.                   |formats |         ^
194  *  |  ____  |     ____|________________   |  ____  |     ____|________________
195  *  | |refs| |    |  __|_                  | |refs| |    |  __|_
196  *  | |* * | |    | |  | |  AVFilterLink   | |* * | |    | |  | |  AVFilterLink
197  *  | |* *--------->|*ref|                 | |*   | |    | |*ref|
198  *  | |____| |    | |____|                 | |____| |    | |____|
199  *  |________|    |_____________________   |________|    |_____________________
200  */
201 void ff_formats_unref(AVFilterFormats **ref);
202
203 /**
204  *
205  *         Before                                 After
206  *   ________                         ________
207  *  |formats |<---------.            |formats |<---------.
208  *  |  ____  |       ___|___         |  ____  |       ___|___
209  *  | |refs| |      |   |   |        | |refs| |      |   |   |   NULL
210  *  | |* *--------->|*oldref|        | |* *--------->|*newref|     ^
211  *  | |* * | |      |_______|        | |* * | |      |_______|  ___|___
212  *  | |____| |                       | |____| |                |   |   |
213  *  |________|                       |________|                |*oldref|
214  *                                                             |_______|
215  */
216 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref);
217
218 #endif // AVFILTER_FORMATS_H