1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Bitstream filters public API
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * This file is part of FFmpeg.
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
7cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
8cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
9cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
10cabdff1aSopenharmony_ci *
11cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
12cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14cabdff1aSopenharmony_ci * Lesser General Public License for more details.
15cabdff1aSopenharmony_ci *
16cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
17cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
18cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19cabdff1aSopenharmony_ci */
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#ifndef AVCODEC_BSF_H
22cabdff1aSopenharmony_ci#define AVCODEC_BSF_H
23cabdff1aSopenharmony_ci
24cabdff1aSopenharmony_ci#include "libavutil/dict.h"
25cabdff1aSopenharmony_ci#include "libavutil/log.h"
26cabdff1aSopenharmony_ci#include "libavutil/rational.h"
27cabdff1aSopenharmony_ci
28cabdff1aSopenharmony_ci#include "codec_id.h"
29cabdff1aSopenharmony_ci#include "codec_par.h"
30cabdff1aSopenharmony_ci#include "packet.h"
31cabdff1aSopenharmony_ci
32cabdff1aSopenharmony_ci/**
33cabdff1aSopenharmony_ci * @defgroup lavc_bsf Bitstream filters
34cabdff1aSopenharmony_ci * @ingroup libavc
35cabdff1aSopenharmony_ci *
36cabdff1aSopenharmony_ci * Bitstream filters transform encoded media data without decoding it. This
37cabdff1aSopenharmony_ci * allows e.g. manipulating various header values. Bitstream filters operate on
38cabdff1aSopenharmony_ci * @ref AVPacket "AVPackets".
39cabdff1aSopenharmony_ci *
40cabdff1aSopenharmony_ci * The bitstream filtering API is centered around two structures:
41cabdff1aSopenharmony_ci * AVBitStreamFilter and AVBSFContext. The former represents a bitstream filter
42cabdff1aSopenharmony_ci * in abstract, the latter a specific filtering process. Obtain an
43cabdff1aSopenharmony_ci * AVBitStreamFilter using av_bsf_get_by_name() or av_bsf_iterate(), then pass
44cabdff1aSopenharmony_ci * it to av_bsf_alloc() to create an AVBSFContext. Fill in the user-settable
45cabdff1aSopenharmony_ci * AVBSFContext fields, as described in its documentation, then call
46cabdff1aSopenharmony_ci * av_bsf_init() to prepare the filter context for use.
47cabdff1aSopenharmony_ci *
48cabdff1aSopenharmony_ci * Submit packets for filtering using av_bsf_send_packet(), obtain filtered
49cabdff1aSopenharmony_ci * results with av_bsf_receive_packet(). When no more input packets will be
50cabdff1aSopenharmony_ci * sent, submit a NULL AVPacket to signal the end of the stream to the filter.
51cabdff1aSopenharmony_ci * av_bsf_receive_packet() will then return trailing packets, if any are
52cabdff1aSopenharmony_ci * produced by the filter.
53cabdff1aSopenharmony_ci *
54cabdff1aSopenharmony_ci * Finally, free the filter context with av_bsf_free().
55cabdff1aSopenharmony_ci * @{
56cabdff1aSopenharmony_ci */
57cabdff1aSopenharmony_ci
58cabdff1aSopenharmony_ci/**
59cabdff1aSopenharmony_ci * The bitstream filter state.
60cabdff1aSopenharmony_ci *
61cabdff1aSopenharmony_ci * This struct must be allocated with av_bsf_alloc() and freed with
62cabdff1aSopenharmony_ci * av_bsf_free().
63cabdff1aSopenharmony_ci *
64cabdff1aSopenharmony_ci * The fields in the struct will only be changed (by the caller or by the
65cabdff1aSopenharmony_ci * filter) as described in their documentation, and are to be considered
66cabdff1aSopenharmony_ci * immutable otherwise.
67cabdff1aSopenharmony_ci */
68cabdff1aSopenharmony_citypedef struct AVBSFContext {
69cabdff1aSopenharmony_ci    /**
70cabdff1aSopenharmony_ci     * A class for logging and AVOptions
71cabdff1aSopenharmony_ci     */
72cabdff1aSopenharmony_ci    const AVClass *av_class;
73cabdff1aSopenharmony_ci
74cabdff1aSopenharmony_ci    /**
75cabdff1aSopenharmony_ci     * The bitstream filter this context is an instance of.
76cabdff1aSopenharmony_ci     */
77cabdff1aSopenharmony_ci    const struct AVBitStreamFilter *filter;
78cabdff1aSopenharmony_ci
79cabdff1aSopenharmony_ci    /**
80cabdff1aSopenharmony_ci     * Opaque filter-specific private data. If filter->priv_class is non-NULL,
81cabdff1aSopenharmony_ci     * this is an AVOptions-enabled struct.
82cabdff1aSopenharmony_ci     */
83cabdff1aSopenharmony_ci    void *priv_data;
84cabdff1aSopenharmony_ci
85cabdff1aSopenharmony_ci    /**
86cabdff1aSopenharmony_ci     * Parameters of the input stream. This field is allocated in
87cabdff1aSopenharmony_ci     * av_bsf_alloc(), it needs to be filled by the caller before
88cabdff1aSopenharmony_ci     * av_bsf_init().
89cabdff1aSopenharmony_ci     */
90cabdff1aSopenharmony_ci    AVCodecParameters *par_in;
91cabdff1aSopenharmony_ci
92cabdff1aSopenharmony_ci    /**
93cabdff1aSopenharmony_ci     * Parameters of the output stream. This field is allocated in
94cabdff1aSopenharmony_ci     * av_bsf_alloc(), it is set by the filter in av_bsf_init().
95cabdff1aSopenharmony_ci     */
96cabdff1aSopenharmony_ci    AVCodecParameters *par_out;
97cabdff1aSopenharmony_ci
98cabdff1aSopenharmony_ci    /**
99cabdff1aSopenharmony_ci     * The timebase used for the timestamps of the input packets. Set by the
100cabdff1aSopenharmony_ci     * caller before av_bsf_init().
101cabdff1aSopenharmony_ci     */
102cabdff1aSopenharmony_ci    AVRational time_base_in;
103cabdff1aSopenharmony_ci
104cabdff1aSopenharmony_ci    /**
105cabdff1aSopenharmony_ci     * The timebase used for the timestamps of the output packets. Set by the
106cabdff1aSopenharmony_ci     * filter in av_bsf_init().
107cabdff1aSopenharmony_ci     */
108cabdff1aSopenharmony_ci    AVRational time_base_out;
109cabdff1aSopenharmony_ci} AVBSFContext;
110cabdff1aSopenharmony_ci
111cabdff1aSopenharmony_citypedef struct AVBitStreamFilter {
112cabdff1aSopenharmony_ci    const char *name;
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_ci    /**
115cabdff1aSopenharmony_ci     * A list of codec ids supported by the filter, terminated by
116cabdff1aSopenharmony_ci     * AV_CODEC_ID_NONE.
117cabdff1aSopenharmony_ci     * May be NULL, in that case the bitstream filter works with any codec id.
118cabdff1aSopenharmony_ci     */
119cabdff1aSopenharmony_ci    const enum AVCodecID *codec_ids;
120cabdff1aSopenharmony_ci
121cabdff1aSopenharmony_ci    /**
122cabdff1aSopenharmony_ci     * A class for the private data, used to declare bitstream filter private
123cabdff1aSopenharmony_ci     * AVOptions. This field is NULL for bitstream filters that do not declare
124cabdff1aSopenharmony_ci     * any options.
125cabdff1aSopenharmony_ci     *
126cabdff1aSopenharmony_ci     * If this field is non-NULL, the first member of the filter private data
127cabdff1aSopenharmony_ci     * must be a pointer to AVClass, which will be set by libavcodec generic
128cabdff1aSopenharmony_ci     * code to this class.
129cabdff1aSopenharmony_ci     */
130cabdff1aSopenharmony_ci    const AVClass *priv_class;
131cabdff1aSopenharmony_ci} AVBitStreamFilter;
132cabdff1aSopenharmony_ci
133cabdff1aSopenharmony_ci/**
134cabdff1aSopenharmony_ci * @return a bitstream filter with the specified name or NULL if no such
135cabdff1aSopenharmony_ci *         bitstream filter exists.
136cabdff1aSopenharmony_ci */
137cabdff1aSopenharmony_ciconst AVBitStreamFilter *av_bsf_get_by_name(const char *name);
138cabdff1aSopenharmony_ci
139cabdff1aSopenharmony_ci/**
140cabdff1aSopenharmony_ci * Iterate over all registered bitstream filters.
141cabdff1aSopenharmony_ci *
142cabdff1aSopenharmony_ci * @param opaque a pointer where libavcodec will store the iteration state. Must
143cabdff1aSopenharmony_ci *               point to NULL to start the iteration.
144cabdff1aSopenharmony_ci *
145cabdff1aSopenharmony_ci * @return the next registered bitstream filter or NULL when the iteration is
146cabdff1aSopenharmony_ci *         finished
147cabdff1aSopenharmony_ci */
148cabdff1aSopenharmony_ciconst AVBitStreamFilter *av_bsf_iterate(void **opaque);
149cabdff1aSopenharmony_ci
150cabdff1aSopenharmony_ci/**
151cabdff1aSopenharmony_ci * Allocate a context for a given bitstream filter. The caller must fill in the
152cabdff1aSopenharmony_ci * context parameters as described in the documentation and then call
153cabdff1aSopenharmony_ci * av_bsf_init() before sending any data to the filter.
154cabdff1aSopenharmony_ci *
155cabdff1aSopenharmony_ci * @param filter the filter for which to allocate an instance.
156cabdff1aSopenharmony_ci * @param[out] ctx a pointer into which the pointer to the newly-allocated context
157cabdff1aSopenharmony_ci *                 will be written. It must be freed with av_bsf_free() after the
158cabdff1aSopenharmony_ci *                 filtering is done.
159cabdff1aSopenharmony_ci *
160cabdff1aSopenharmony_ci * @return 0 on success, a negative AVERROR code on failure
161cabdff1aSopenharmony_ci */
162cabdff1aSopenharmony_ciint av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx);
163cabdff1aSopenharmony_ci
164cabdff1aSopenharmony_ci/**
165cabdff1aSopenharmony_ci * Prepare the filter for use, after all the parameters and options have been
166cabdff1aSopenharmony_ci * set.
167cabdff1aSopenharmony_ci */
168cabdff1aSopenharmony_ciint av_bsf_init(AVBSFContext *ctx);
169cabdff1aSopenharmony_ci
170cabdff1aSopenharmony_ci/**
171cabdff1aSopenharmony_ci * Submit a packet for filtering.
172cabdff1aSopenharmony_ci *
173cabdff1aSopenharmony_ci * After sending each packet, the filter must be completely drained by calling
174cabdff1aSopenharmony_ci * av_bsf_receive_packet() repeatedly until it returns AVERROR(EAGAIN) or
175cabdff1aSopenharmony_ci * AVERROR_EOF.
176cabdff1aSopenharmony_ci *
177cabdff1aSopenharmony_ci * @param pkt the packet to filter. The bitstream filter will take ownership of
178cabdff1aSopenharmony_ci * the packet and reset the contents of pkt. pkt is not touched if an error occurs.
179cabdff1aSopenharmony_ci * If pkt is empty (i.e. NULL, or pkt->data is NULL and pkt->side_data_elems zero),
180cabdff1aSopenharmony_ci * it signals the end of the stream (i.e. no more non-empty packets will be sent;
181cabdff1aSopenharmony_ci * sending more empty packets does nothing) and will cause the filter to output
182cabdff1aSopenharmony_ci * any packets it may have buffered internally.
183cabdff1aSopenharmony_ci *
184cabdff1aSopenharmony_ci * @return
185cabdff1aSopenharmony_ci *  - 0 on success.
186cabdff1aSopenharmony_ci *  - AVERROR(EAGAIN) if packets need to be retrieved from the filter (using
187cabdff1aSopenharmony_ci *    av_bsf_receive_packet()) before new input can be consumed.
188cabdff1aSopenharmony_ci *  - Another negative AVERROR value if an error occurs.
189cabdff1aSopenharmony_ci */
190cabdff1aSopenharmony_ciint av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt);
191cabdff1aSopenharmony_ci
192cabdff1aSopenharmony_ci/**
193cabdff1aSopenharmony_ci * Retrieve a filtered packet.
194cabdff1aSopenharmony_ci *
195cabdff1aSopenharmony_ci * @param[out] pkt this struct will be filled with the contents of the filtered
196cabdff1aSopenharmony_ci *                 packet. It is owned by the caller and must be freed using
197cabdff1aSopenharmony_ci *                 av_packet_unref() when it is no longer needed.
198cabdff1aSopenharmony_ci *                 This parameter should be "clean" (i.e. freshly allocated
199cabdff1aSopenharmony_ci *                 with av_packet_alloc() or unreffed with av_packet_unref())
200cabdff1aSopenharmony_ci *                 when this function is called. If this function returns
201cabdff1aSopenharmony_ci *                 successfully, the contents of pkt will be completely
202cabdff1aSopenharmony_ci *                 overwritten by the returned data. On failure, pkt is not
203cabdff1aSopenharmony_ci *                 touched.
204cabdff1aSopenharmony_ci *
205cabdff1aSopenharmony_ci * @return
206cabdff1aSopenharmony_ci *  - 0 on success.
207cabdff1aSopenharmony_ci *  - AVERROR(EAGAIN) if more packets need to be sent to the filter (using
208cabdff1aSopenharmony_ci *    av_bsf_send_packet()) to get more output.
209cabdff1aSopenharmony_ci *  - AVERROR_EOF if there will be no further output from the filter.
210cabdff1aSopenharmony_ci *  - Another negative AVERROR value if an error occurs.
211cabdff1aSopenharmony_ci *
212cabdff1aSopenharmony_ci * @note one input packet may result in several output packets, so after sending
213cabdff1aSopenharmony_ci * a packet with av_bsf_send_packet(), this function needs to be called
214cabdff1aSopenharmony_ci * repeatedly until it stops returning 0. It is also possible for a filter to
215cabdff1aSopenharmony_ci * output fewer packets than were sent to it, so this function may return
216cabdff1aSopenharmony_ci * AVERROR(EAGAIN) immediately after a successful av_bsf_send_packet() call.
217cabdff1aSopenharmony_ci */
218cabdff1aSopenharmony_ciint av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt);
219cabdff1aSopenharmony_ci
220cabdff1aSopenharmony_ci/**
221cabdff1aSopenharmony_ci * Reset the internal bitstream filter state. Should be called e.g. when seeking.
222cabdff1aSopenharmony_ci */
223cabdff1aSopenharmony_civoid av_bsf_flush(AVBSFContext *ctx);
224cabdff1aSopenharmony_ci
225cabdff1aSopenharmony_ci/**
226cabdff1aSopenharmony_ci * Free a bitstream filter context and everything associated with it; write NULL
227cabdff1aSopenharmony_ci * into the supplied pointer.
228cabdff1aSopenharmony_ci */
229cabdff1aSopenharmony_civoid av_bsf_free(AVBSFContext **ctx);
230cabdff1aSopenharmony_ci
231cabdff1aSopenharmony_ci/**
232cabdff1aSopenharmony_ci * Get the AVClass for AVBSFContext. It can be used in combination with
233cabdff1aSopenharmony_ci * AV_OPT_SEARCH_FAKE_OBJ for examining options.
234cabdff1aSopenharmony_ci *
235cabdff1aSopenharmony_ci * @see av_opt_find().
236cabdff1aSopenharmony_ci */
237cabdff1aSopenharmony_ciconst AVClass *av_bsf_get_class(void);
238cabdff1aSopenharmony_ci
239cabdff1aSopenharmony_ci/**
240cabdff1aSopenharmony_ci * Structure for chain/list of bitstream filters.
241cabdff1aSopenharmony_ci * Empty list can be allocated by av_bsf_list_alloc().
242cabdff1aSopenharmony_ci */
243cabdff1aSopenharmony_citypedef struct AVBSFList AVBSFList;
244cabdff1aSopenharmony_ci
245cabdff1aSopenharmony_ci/**
246cabdff1aSopenharmony_ci * Allocate empty list of bitstream filters.
247cabdff1aSopenharmony_ci * The list must be later freed by av_bsf_list_free()
248cabdff1aSopenharmony_ci * or finalized by av_bsf_list_finalize().
249cabdff1aSopenharmony_ci *
250cabdff1aSopenharmony_ci * @return Pointer to @ref AVBSFList on success, NULL in case of failure
251cabdff1aSopenharmony_ci */
252cabdff1aSopenharmony_ciAVBSFList *av_bsf_list_alloc(void);
253cabdff1aSopenharmony_ci
254cabdff1aSopenharmony_ci/**
255cabdff1aSopenharmony_ci * Free list of bitstream filters.
256cabdff1aSopenharmony_ci *
257cabdff1aSopenharmony_ci * @param lst Pointer to pointer returned by av_bsf_list_alloc()
258cabdff1aSopenharmony_ci */
259cabdff1aSopenharmony_civoid av_bsf_list_free(AVBSFList **lst);
260cabdff1aSopenharmony_ci
261cabdff1aSopenharmony_ci/**
262cabdff1aSopenharmony_ci * Append bitstream filter to the list of bitstream filters.
263cabdff1aSopenharmony_ci *
264cabdff1aSopenharmony_ci * @param lst List to append to
265cabdff1aSopenharmony_ci * @param bsf Filter context to be appended
266cabdff1aSopenharmony_ci *
267cabdff1aSopenharmony_ci * @return >=0 on success, negative AVERROR in case of failure
268cabdff1aSopenharmony_ci */
269cabdff1aSopenharmony_ciint av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf);
270cabdff1aSopenharmony_ci
271cabdff1aSopenharmony_ci/**
272cabdff1aSopenharmony_ci * Construct new bitstream filter context given it's name and options
273cabdff1aSopenharmony_ci * and append it to the list of bitstream filters.
274cabdff1aSopenharmony_ci *
275cabdff1aSopenharmony_ci * @param lst      List to append to
276cabdff1aSopenharmony_ci * @param bsf_name Name of the bitstream filter
277cabdff1aSopenharmony_ci * @param options  Options for the bitstream filter, can be set to NULL
278cabdff1aSopenharmony_ci *
279cabdff1aSopenharmony_ci * @return >=0 on success, negative AVERROR in case of failure
280cabdff1aSopenharmony_ci */
281cabdff1aSopenharmony_ciint av_bsf_list_append2(AVBSFList *lst, const char * bsf_name, AVDictionary **options);
282cabdff1aSopenharmony_ci/**
283cabdff1aSopenharmony_ci * Finalize list of bitstream filters.
284cabdff1aSopenharmony_ci *
285cabdff1aSopenharmony_ci * This function will transform @ref AVBSFList to single @ref AVBSFContext,
286cabdff1aSopenharmony_ci * so the whole chain of bitstream filters can be treated as single filter
287cabdff1aSopenharmony_ci * freshly allocated by av_bsf_alloc().
288cabdff1aSopenharmony_ci * If the call is successful, @ref AVBSFList structure is freed and lst
289cabdff1aSopenharmony_ci * will be set to NULL. In case of failure, caller is responsible for
290cabdff1aSopenharmony_ci * freeing the structure by av_bsf_list_free()
291cabdff1aSopenharmony_ci *
292cabdff1aSopenharmony_ci * @param      lst Filter list structure to be transformed
293cabdff1aSopenharmony_ci * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure
294cabdff1aSopenharmony_ci *                 representing the chain of bitstream filters
295cabdff1aSopenharmony_ci *
296cabdff1aSopenharmony_ci * @return >=0 on success, negative AVERROR in case of failure
297cabdff1aSopenharmony_ci */
298cabdff1aSopenharmony_ciint av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf);
299cabdff1aSopenharmony_ci
300cabdff1aSopenharmony_ci/**
301cabdff1aSopenharmony_ci * Parse string describing list of bitstream filters and create single
302cabdff1aSopenharmony_ci * @ref AVBSFContext describing the whole chain of bitstream filters.
303cabdff1aSopenharmony_ci * Resulting @ref AVBSFContext can be treated as any other @ref AVBSFContext freshly
304cabdff1aSopenharmony_ci * allocated by av_bsf_alloc().
305cabdff1aSopenharmony_ci *
306cabdff1aSopenharmony_ci * @param      str String describing chain of bitstream filters in format
307cabdff1aSopenharmony_ci *                 `bsf1[=opt1=val1:opt2=val2][,bsf2]`
308cabdff1aSopenharmony_ci * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure
309cabdff1aSopenharmony_ci *                 representing the chain of bitstream filters
310cabdff1aSopenharmony_ci *
311cabdff1aSopenharmony_ci * @return >=0 on success, negative AVERROR in case of failure
312cabdff1aSopenharmony_ci */
313cabdff1aSopenharmony_ciint av_bsf_list_parse_str(const char *str, AVBSFContext **bsf);
314cabdff1aSopenharmony_ci
315cabdff1aSopenharmony_ci/**
316cabdff1aSopenharmony_ci * Get null/pass-through bitstream filter.
317cabdff1aSopenharmony_ci *
318cabdff1aSopenharmony_ci * @param[out] bsf Pointer to be set to new instance of pass-through bitstream filter
319cabdff1aSopenharmony_ci *
320cabdff1aSopenharmony_ci * @return
321cabdff1aSopenharmony_ci */
322cabdff1aSopenharmony_ciint av_bsf_get_null_filter(AVBSFContext **bsf);
323cabdff1aSopenharmony_ci
324cabdff1aSopenharmony_ci/**
325cabdff1aSopenharmony_ci * @}
326cabdff1aSopenharmony_ci */
327cabdff1aSopenharmony_ci
328cabdff1aSopenharmony_ci#endif // AVCODEC_BSF_H
329