1 /*
2  * Bitstream filters public API
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 module ffmpeg.libavcodec.bsf;
21 
22 import ffmpeg.libavcodec;
23 import ffmpeg.libavutil;
24 
25 extern (C) @nogc nothrow:
26 
27 /**
28  * @addtogroup lavc_core
29  * @{
30  */
31 
32 struct AVBSFInternal;
33 
34 /**
35  * The bitstream filter state.
36  *
37  * This struct must be allocated with av_bsf_alloc() and freed with
38  * av_bsf_free().
39  *
40  * The fields in the struct will only be changed (by the caller or by the
41  * filter) as described in their documentation, and are to be considered
42  * immutable otherwise.
43  */
44 struct AVBSFContext
45 {
46     /**
47      * A class for logging and AVOptions
48      */
49     const(AVClass)* av_class;
50 
51     /**
52      * The bitstream filter this context is an instance of.
53      */
54     const(AVBitStreamFilter)* filter;
55 
56     /**
57      * Opaque libavcodec internal data. Must not be touched by the caller in any
58      * way.
59      */
60     AVBSFInternal* internal;
61 
62     /**
63      * Opaque filter-specific private data. If filter->priv_class is non-NULL,
64      * this is an AVOptions-enabled struct.
65      */
66     void* priv_data;
67 
68     /**
69      * Parameters of the input stream. This field is allocated in
70      * av_bsf_alloc(), it needs to be filled by the caller before
71      * av_bsf_init().
72      */
73     AVCodecParameters* par_in;
74 
75     /**
76      * Parameters of the output stream. This field is allocated in
77      * av_bsf_alloc(), it is set by the filter in av_bsf_init().
78      */
79     AVCodecParameters* par_out;
80 
81     /**
82      * The timebase used for the timestamps of the input packets. Set by the
83      * caller before av_bsf_init().
84      */
85     AVRational time_base_in;
86 
87     /**
88      * The timebase used for the timestamps of the output packets. Set by the
89      * filter in av_bsf_init().
90      */
91     AVRational time_base_out;
92 }
93 
94 struct AVBitStreamFilter
95 {
96     const(char)* name;
97 
98     /**
99      * A list of codec ids supported by the filter, terminated by
100      * AV_CODEC_ID_NONE.
101      * May be NULL, in that case the bitstream filter works with any codec id.
102      */
103     const(AVCodecID)* codec_ids;
104 
105     /**
106      * A class for the private data, used to declare bitstream filter private
107      * AVOptions. This field is NULL for bitstream filters that do not declare
108      * any options.
109      *
110      * If this field is non-NULL, the first member of the filter private data
111      * must be a pointer to AVClass, which will be set by libavcodec generic
112      * code to this class.
113      */
114     const(AVClass)* priv_class;
115 
116     /*****************************************************************
117      * No fields below this line are part of the public API. They
118      * may not be used outside of libavcodec and can be changed and
119      * removed at will.
120      * New public fields should be added right above.
121      *****************************************************************
122      */
123 
124     int priv_data_size;
125     int function (AVBSFContext* ctx) init;
126     int function (AVBSFContext* ctx, AVPacket* pkt) filter;
127     void function (AVBSFContext* ctx) close;
128     void function (AVBSFContext* ctx) flush;
129 }
130 
131 /**
132  * @return a bitstream filter with the specified name or NULL if no such
133  *         bitstream filter exists.
134  */
135 const(AVBitStreamFilter)* av_bsf_get_by_name (const(char)* name);
136 
137 /**
138  * Iterate over all registered bitstream filters.
139  *
140  * @param opaque a pointer where libavcodec will store the iteration state. Must
141  *               point to NULL to start the iteration.
142  *
143  * @return the next registered bitstream filter or NULL when the iteration is
144  *         finished
145  */
146 const(AVBitStreamFilter)* av_bsf_iterate (void** opaque);
147 
148 /**
149  * Allocate a context for a given bitstream filter. The caller must fill in the
150  * context parameters as described in the documentation and then call
151  * av_bsf_init() before sending any data to the filter.
152  *
153  * @param filter the filter for which to allocate an instance.
154  * @param ctx a pointer into which the pointer to the newly-allocated context
155  *            will be written. It must be freed with av_bsf_free() after the
156  *            filtering is done.
157  *
158  * @return 0 on success, a negative AVERROR code on failure
159  */
160 int av_bsf_alloc (const(AVBitStreamFilter)* filter, AVBSFContext** ctx);
161 
162 /**
163  * Prepare the filter for use, after all the parameters and options have been
164  * set.
165  */
166 int av_bsf_init (AVBSFContext* ctx);
167 
168 /**
169  * Submit a packet for filtering.
170  *
171  * After sending each packet, the filter must be completely drained by calling
172  * av_bsf_receive_packet() repeatedly until it returns AVERROR(EAGAIN) or
173  * AVERROR_EOF.
174  *
175  * @param pkt the packet to filter. The bitstream filter will take ownership of
176  * the packet and reset the contents of pkt. pkt is not touched if an error occurs.
177  * If pkt is empty (i.e. NULL, or pkt->data is NULL and pkt->side_data_elems zero),
178  * it signals the end of the stream (i.e. no more non-empty packets will be sent;
179  * sending more empty packets does nothing) and will cause the filter to output
180  * any packets it may have buffered internally.
181  *
182  * @return 0 on success. AVERROR(EAGAIN) if packets need to be retrieved from the
183  * filter (using av_bsf_receive_packet()) before new input can be consumed. Another
184  * negative AVERROR value if an error occurs.
185  */
186 int av_bsf_send_packet (AVBSFContext* ctx, AVPacket* pkt);
187 
188 /**
189  * Retrieve a filtered packet.
190  *
191  * @param[out] pkt this struct will be filled with the contents of the filtered
192  *                 packet. It is owned by the caller and must be freed using
193  *                 av_packet_unref() when it is no longer needed.
194  *                 This parameter should be "clean" (i.e. freshly allocated
195  *                 with av_packet_alloc() or unreffed with av_packet_unref())
196  *                 when this function is called. If this function returns
197  *                 successfully, the contents of pkt will be completely
198  *                 overwritten by the returned data. On failure, pkt is not
199  *                 touched.
200  *
201  * @return 0 on success. AVERROR(EAGAIN) if more packets need to be sent to the
202  * filter (using av_bsf_send_packet()) to get more output. AVERROR_EOF if there
203  * will be no further output from the filter. Another negative AVERROR value if
204  * an error occurs.
205  *
206  * @note one input packet may result in several output packets, so after sending
207  * a packet with av_bsf_send_packet(), this function needs to be called
208  * repeatedly until it stops returning 0. It is also possible for a filter to
209  * output fewer packets than were sent to it, so this function may return
210  * AVERROR(EAGAIN) immediately after a successful av_bsf_send_packet() call.
211  */
212 int av_bsf_receive_packet (AVBSFContext* ctx, AVPacket* pkt);
213 
214 /**
215  * Reset the internal bitstream filter state. Should be called e.g. when seeking.
216  */
217 void av_bsf_flush (AVBSFContext* ctx);
218 
219 /**
220  * Free a bitstream filter context and everything associated with it; write NULL
221  * into the supplied pointer.
222  */
223 void av_bsf_free (AVBSFContext** ctx);
224 
225 /**
226  * Get the AVClass for AVBSFContext. It can be used in combination with
227  * AV_OPT_SEARCH_FAKE_OBJ for examining options.
228  *
229  * @see av_opt_find().
230  */
231 const(AVClass)* av_bsf_get_class ();
232 
233 /**
234  * Structure for chain/list of bitstream filters.
235  * Empty list can be allocated by av_bsf_list_alloc().
236  */
237 struct AVBSFList;
238 
239 /**
240  * Allocate empty list of bitstream filters.
241  * The list must be later freed by av_bsf_list_free()
242  * or finalized by av_bsf_list_finalize().
243  *
244  * @return Pointer to @ref AVBSFList on success, NULL in case of failure
245  */
246 AVBSFList* av_bsf_list_alloc ();
247 
248 /**
249  * Free list of bitstream filters.
250  *
251  * @param lst Pointer to pointer returned by av_bsf_list_alloc()
252  */
253 void av_bsf_list_free (AVBSFList** lst);
254 
255 /**
256  * Append bitstream filter to the list of bitstream filters.
257  *
258  * @param lst List to append to
259  * @param bsf Filter context to be appended
260  *
261  * @return >=0 on success, negative AVERROR in case of failure
262  */
263 int av_bsf_list_append (AVBSFList* lst, AVBSFContext* bsf);
264 
265 /**
266  * Construct new bitstream filter context given it's name and options
267  * and append it to the list of bitstream filters.
268  *
269  * @param lst      List to append to
270  * @param bsf_name Name of the bitstream filter
271  * @param options  Options for the bitstream filter, can be set to NULL
272  *
273  * @return >=0 on success, negative AVERROR in case of failure
274  */
275 int av_bsf_list_append2 (AVBSFList* lst, const(char)* bsf_name, AVDictionary** options);
276 /**
277  * Finalize list of bitstream filters.
278  *
279  * This function will transform @ref AVBSFList to single @ref AVBSFContext,
280  * so the whole chain of bitstream filters can be treated as single filter
281  * freshly allocated by av_bsf_alloc().
282  * If the call is successful, @ref AVBSFList structure is freed and lst
283  * will be set to NULL. In case of failure, caller is responsible for
284  * freeing the structure by av_bsf_list_free()
285  *
286  * @param      lst Filter list structure to be transformed
287  * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure
288  *                 representing the chain of bitstream filters
289  *
290  * @return >=0 on success, negative AVERROR in case of failure
291  */
292 int av_bsf_list_finalize (AVBSFList** lst, AVBSFContext** bsf);
293 
294 /**
295  * Parse string describing list of bitstream filters and create single
296  * @ref AVBSFContext describing the whole chain of bitstream filters.
297  * Resulting @ref AVBSFContext can be treated as any other @ref AVBSFContext freshly
298  * allocated by av_bsf_alloc().
299  *
300  * @param      str String describing chain of bitstream filters in format
301  *                 `bsf1[=opt1=val1:opt2=val2][,bsf2]`
302  * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure
303  *                 representing the chain of bitstream filters
304  *
305  * @return >=0 on success, negative AVERROR in case of failure
306  */
307 int av_bsf_list_parse_str (const(char)* str, AVBSFContext** bsf);
308 
309 /**
310  * Get null/pass-through bitstream filter.
311  *
312  * @param[out] bsf Pointer to be set to new instance of pass-through bitstream filter
313  *
314  * @return
315  */
316 int av_bsf_get_null_filter (AVBSFContext** bsf);
317 
318 /**
319  * @}
320  */
321 
322 // AVCODEC_BSF_H