2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/log.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/bprint.h"
31 #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
33 struct AVBSFInternal {
38 void av_bsf_free(AVBSFContext **pctx)
46 if (ctx->filter->close)
47 ctx->filter->close(ctx);
48 if (ctx->filter->priv_class && ctx->priv_data)
49 av_opt_free(ctx->priv_data);
52 av_packet_free(&ctx->internal->buffer_pkt);
53 av_freep(&ctx->internal);
54 av_freep(&ctx->priv_data);
56 avcodec_parameters_free(&ctx->par_in);
57 avcodec_parameters_free(&ctx->par_out);
62 static void *bsf_child_next(void *obj, void *prev)
64 AVBSFContext *ctx = obj;
65 if (!prev && ctx->filter->priv_class)
66 return ctx->priv_data;
70 static const char *bsf_to_name(void *bsf)
72 return ((AVBSFContext *)bsf)->filter->name;
75 static const AVClass bsf_class = {
76 .class_name = "AVBSFContext",
77 .item_name = bsf_to_name,
78 .version = LIBAVUTIL_VERSION_INT,
79 .child_next = bsf_child_next,
80 .child_class_next = ff_bsf_child_class_next,
81 .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
84 const AVClass *av_bsf_get_class(void)
89 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
95 ctx = av_mallocz(sizeof(*ctx));
97 return AVERROR(ENOMEM);
99 ctx->av_class = &bsf_class;
100 ctx->filter = filter;
102 ctx->par_in = avcodec_parameters_alloc();
103 ctx->par_out = avcodec_parameters_alloc();
104 if (!ctx->par_in || !ctx->par_out) {
105 ret = AVERROR(ENOMEM);
109 bsfi = av_mallocz(sizeof(*bsfi));
111 ret = AVERROR(ENOMEM);
114 ctx->internal = bsfi;
116 bsfi->buffer_pkt = av_packet_alloc();
117 if (!bsfi->buffer_pkt) {
118 ret = AVERROR(ENOMEM);
122 /* allocate priv data and init private options */
123 if (filter->priv_data_size) {
124 ctx->priv_data = av_mallocz(filter->priv_data_size);
125 if (!ctx->priv_data) {
126 ret = AVERROR(ENOMEM);
129 if (filter->priv_class) {
130 *(const AVClass **)ctx->priv_data = filter->priv_class;
131 av_opt_set_defaults(ctx->priv_data);
142 int av_bsf_init(AVBSFContext *ctx)
146 /* check that the codec is supported */
147 if (ctx->filter->codec_ids) {
148 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
149 if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
151 if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
152 const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
153 av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
154 "bitstream filter '%s'. Supported codecs are: ",
155 desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
156 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
157 desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
158 av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
159 desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
161 av_log(ctx, AV_LOG_ERROR, "\n");
162 return AVERROR(EINVAL);
166 /* initialize output parameters to be the same as input
167 * init below might overwrite that */
168 ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
172 ctx->time_base_out = ctx->time_base_in;
174 if (ctx->filter->init) {
175 ret = ctx->filter->init(ctx);
183 void av_bsf_flush(AVBSFContext *ctx)
185 AVBSFInternal *bsfi = ctx->internal;
189 av_packet_unref(bsfi->buffer_pkt);
191 if (ctx->filter->flush)
192 ctx->filter->flush(ctx);
195 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
197 AVBSFInternal *bsfi = ctx->internal;
200 if (!pkt || IS_EMPTY(pkt)) {
206 av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
207 return AVERROR(EINVAL);
210 if (!IS_EMPTY(bsfi->buffer_pkt))
211 return AVERROR(EAGAIN);
213 ret = av_packet_make_refcounted(pkt);
216 av_packet_move_ref(bsfi->buffer_pkt, pkt);
221 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
223 return ctx->filter->filter(ctx, pkt);
226 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
228 AVBSFInternal *bsfi = ctx->internal;
234 if (IS_EMPTY(bsfi->buffer_pkt))
235 return AVERROR(EAGAIN);
237 tmp_pkt = av_packet_alloc();
239 return AVERROR(ENOMEM);
241 *pkt = bsfi->buffer_pkt;
242 bsfi->buffer_pkt = tmp_pkt;
247 int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
249 AVBSFInternal *bsfi = ctx->internal;
254 if (IS_EMPTY(bsfi->buffer_pkt))
255 return AVERROR(EAGAIN);
257 av_packet_move_ref(pkt, bsfi->buffer_pkt);
262 typedef struct BSFListContext {
263 const AVClass *class;
268 unsigned idx; // index of currently processed BSF
274 static int bsf_list_init(AVBSFContext *bsf)
276 BSFListContext *lst = bsf->priv_data;
278 const AVCodecParameters *cod_par = bsf->par_in;
279 AVRational tb = bsf->time_base_in;
281 for (i = 0; i < lst->nb_bsfs; ++i) {
282 ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
286 lst->bsfs[i]->time_base_in = tb;
288 ret = av_bsf_init(lst->bsfs[i]);
292 cod_par = lst->bsfs[i]->par_out;
293 tb = lst->bsfs[i]->time_base_out;
296 bsf->time_base_out = tb;
297 ret = avcodec_parameters_copy(bsf->par_out, cod_par);
303 static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
305 BSFListContext *lst = bsf->priv_data;
309 return ff_bsf_get_packet_ref(bsf, out);
312 /* get a packet from the previous filter up the chain */
314 ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
316 ret = ff_bsf_get_packet_ref(bsf, out);
317 if (ret == AVERROR(EAGAIN)) {
322 } else if (ret == AVERROR_EOF) {
327 /* send it to the next filter down the chain */
328 if (lst->idx < lst->nb_bsfs) {
329 ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
330 av_assert1(ret != AVERROR(EAGAIN));
332 av_packet_unref(out);
345 static void bsf_list_flush(AVBSFContext *bsf)
347 BSFListContext *lst = bsf->priv_data;
349 for (int i = 0; i < lst->nb_bsfs; i++)
350 av_bsf_flush(lst->bsfs[i]);
354 static void bsf_list_close(AVBSFContext *bsf)
356 BSFListContext *lst = bsf->priv_data;
359 for (i = 0; i < lst->nb_bsfs; ++i)
360 av_bsf_free(&lst->bsfs[i]);
361 av_freep(&lst->bsfs);
362 av_freep(&lst->item_name);
365 static const char *bsf_list_item_name(void *ctx)
367 static const char *null_filter_name = "null";
368 AVBSFContext *bsf_ctx = ctx;
369 BSFListContext *lst = bsf_ctx->priv_data;
372 return null_filter_name;
374 if (!lst->item_name) {
377 av_bprint_init(&bp, 16, 128);
379 av_bprintf(&bp, "bsf_list(");
380 for (i = 0; i < lst->nb_bsfs; i++)
381 av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
382 av_bprintf(&bp, ")");
384 av_bprint_finalize(&bp, &lst->item_name);
387 return lst->item_name;
390 static const AVClass bsf_list_class = {
391 .class_name = "bsf_list",
392 .item_name = bsf_list_item_name,
393 .version = LIBAVUTIL_VERSION_INT,
396 const AVBitStreamFilter ff_list_bsf = {
398 .priv_data_size = sizeof(BSFListContext),
399 .priv_class = &bsf_list_class,
400 .init = bsf_list_init,
401 .filter = bsf_list_filter,
402 .flush = bsf_list_flush,
403 .close = bsf_list_close,
411 AVBSFList *av_bsf_list_alloc(void)
413 return av_mallocz(sizeof(AVBSFList));
416 void av_bsf_list_free(AVBSFList **lst)
423 for (i = 0; i < (*lst)->nb_bsfs; ++i)
424 av_bsf_free(&(*lst)->bsfs[i]);
425 av_free((*lst)->bsfs);
429 int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
431 return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
434 int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
437 const AVBitStreamFilter *filter;
440 filter = av_bsf_get_by_name(bsf_name);
442 return AVERROR_BSF_NOT_FOUND;
444 ret = av_bsf_alloc(filter, &bsf);
449 ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
454 ret = av_bsf_list_append(lst, bsf);
463 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
468 if ((*lst)->nb_bsfs == 1) {
469 *bsf = (*lst)->bsfs[0];
470 av_freep(&(*lst)->bsfs);
475 ret = av_bsf_alloc(&ff_list_bsf, bsf);
479 ctx = (*bsf)->priv_data;
481 ctx->bsfs = (*lst)->bsfs;
482 ctx->nb_bsfs = (*lst)->nb_bsfs;
489 static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
491 char *bsf_name, *bsf_options_str, *buf;
492 AVDictionary *bsf_options = NULL;
495 if (!(buf = av_strdup(str)))
496 return AVERROR(ENOMEM);
498 bsf_name = av_strtok(buf, "=", &bsf_options_str);
500 ret = AVERROR(EINVAL);
504 if (bsf_options_str) {
505 ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
510 ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
513 av_dict_free(&bsf_options);
518 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
521 char *bsf_str, *buf, *dup, *saveptr;
525 return av_bsf_get_null_filter(bsf_lst);
527 lst = av_bsf_list_alloc();
529 return AVERROR(ENOMEM);
531 if (!(dup = buf = av_strdup(str))) {
532 ret = AVERROR(ENOMEM);
536 while (bsf_str = av_strtok(buf, ",", &saveptr)) {
537 ret = bsf_parse_single(bsf_str, lst);
544 ret = av_bsf_list_finalize(&lst, bsf_lst);
547 av_bsf_list_free(&lst);
552 int av_bsf_get_null_filter(AVBSFContext **bsf)
554 return av_bsf_alloc(&ff_list_bsf, bsf);