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"
29 #include "bsf_internal.h"
30 #include "codec_desc.h"
31 #include "codec_par.h"
33 #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
35 struct AVBSFInternal {
40 void av_bsf_free(AVBSFContext **pctx)
49 if (ctx->filter->close)
50 ctx->filter->close(ctx);
51 av_packet_free(&ctx->internal->buffer_pkt);
52 av_freep(&ctx->internal);
54 if (ctx->filter->priv_class && ctx->priv_data)
55 av_opt_free(ctx->priv_data);
57 av_freep(&ctx->priv_data);
59 avcodec_parameters_free(&ctx->par_in);
60 avcodec_parameters_free(&ctx->par_out);
65 static void *bsf_child_next(void *obj, void *prev)
67 AVBSFContext *ctx = obj;
68 if (!prev && ctx->filter->priv_class)
69 return ctx->priv_data;
73 static const char *bsf_to_name(void *bsf)
75 return ((AVBSFContext *)bsf)->filter->name;
78 static const AVClass bsf_class = {
79 .class_name = "AVBSFContext",
80 .item_name = bsf_to_name,
81 .version = LIBAVUTIL_VERSION_INT,
82 .child_next = bsf_child_next,
83 #if FF_API_CHILD_CLASS_NEXT
84 .child_class_next = ff_bsf_child_class_next,
86 .child_class_iterate = ff_bsf_child_class_iterate,
87 .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
90 const AVClass *av_bsf_get_class(void)
95 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
101 ctx = av_mallocz(sizeof(*ctx));
103 return AVERROR(ENOMEM);
105 ctx->av_class = &bsf_class;
106 ctx->filter = filter;
108 ctx->par_in = avcodec_parameters_alloc();
109 ctx->par_out = avcodec_parameters_alloc();
110 if (!ctx->par_in || !ctx->par_out) {
111 ret = AVERROR(ENOMEM);
114 /* allocate priv data and init private options */
115 if (filter->priv_data_size) {
116 ctx->priv_data = av_mallocz(filter->priv_data_size);
117 if (!ctx->priv_data) {
118 ret = AVERROR(ENOMEM);
121 if (filter->priv_class) {
122 *(const AVClass **)ctx->priv_data = filter->priv_class;
123 av_opt_set_defaults(ctx->priv_data);
126 /* Allocate AVBSFInternal; must happen after priv_data has been allocated
127 * so that a filter->close needing priv_data is never called without. */
128 bsfi = av_mallocz(sizeof(*bsfi));
130 ret = AVERROR(ENOMEM);
133 ctx->internal = bsfi;
135 bsfi->buffer_pkt = av_packet_alloc();
136 if (!bsfi->buffer_pkt) {
137 ret = AVERROR(ENOMEM);
148 int av_bsf_init(AVBSFContext *ctx)
152 /* check that the codec is supported */
153 if (ctx->filter->codec_ids) {
154 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
155 if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
157 if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
158 const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
159 av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
160 "bitstream filter '%s'. Supported codecs are: ",
161 desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
162 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
163 enum AVCodecID codec_id = ctx->filter->codec_ids[i];
164 av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
165 avcodec_get_name(codec_id), codec_id);
167 av_log(ctx, AV_LOG_ERROR, "\n");
168 return AVERROR(EINVAL);
172 /* initialize output parameters to be the same as input
173 * init below might overwrite that */
174 ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
178 ctx->time_base_out = ctx->time_base_in;
180 if (ctx->filter->init) {
181 ret = ctx->filter->init(ctx);
189 void av_bsf_flush(AVBSFContext *ctx)
191 AVBSFInternal *bsfi = ctx->internal;
195 av_packet_unref(bsfi->buffer_pkt);
197 if (ctx->filter->flush)
198 ctx->filter->flush(ctx);
201 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
203 AVBSFInternal *bsfi = ctx->internal;
206 if (!pkt || IS_EMPTY(pkt)) {
212 av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
213 return AVERROR(EINVAL);
216 if (!IS_EMPTY(bsfi->buffer_pkt))
217 return AVERROR(EAGAIN);
219 ret = av_packet_make_refcounted(pkt);
222 av_packet_move_ref(bsfi->buffer_pkt, pkt);
227 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
229 return ctx->filter->filter(ctx, pkt);
232 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
234 AVBSFInternal *bsfi = ctx->internal;
240 if (IS_EMPTY(bsfi->buffer_pkt))
241 return AVERROR(EAGAIN);
243 tmp_pkt = av_packet_alloc();
245 return AVERROR(ENOMEM);
247 *pkt = bsfi->buffer_pkt;
248 bsfi->buffer_pkt = tmp_pkt;
253 int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
255 AVBSFInternal *bsfi = ctx->internal;
260 if (IS_EMPTY(bsfi->buffer_pkt))
261 return AVERROR(EAGAIN);
263 av_packet_move_ref(pkt, bsfi->buffer_pkt);
268 typedef struct BSFListContext {
269 const AVClass *class;
274 unsigned idx; // index of currently processed BSF
280 static int bsf_list_init(AVBSFContext *bsf)
282 BSFListContext *lst = bsf->priv_data;
284 const AVCodecParameters *cod_par = bsf->par_in;
285 AVRational tb = bsf->time_base_in;
287 for (i = 0; i < lst->nb_bsfs; ++i) {
288 ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
292 lst->bsfs[i]->time_base_in = tb;
294 ret = av_bsf_init(lst->bsfs[i]);
298 cod_par = lst->bsfs[i]->par_out;
299 tb = lst->bsfs[i]->time_base_out;
302 bsf->time_base_out = tb;
303 ret = avcodec_parameters_copy(bsf->par_out, cod_par);
309 static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
311 BSFListContext *lst = bsf->priv_data;
315 return ff_bsf_get_packet_ref(bsf, out);
318 /* get a packet from the previous filter up the chain */
320 ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
322 ret = ff_bsf_get_packet_ref(bsf, out);
323 if (ret == AVERROR(EAGAIN)) {
328 } else if (ret == AVERROR_EOF) {
333 /* send it to the next filter down the chain */
334 if (lst->idx < lst->nb_bsfs) {
335 ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
336 av_assert1(ret != AVERROR(EAGAIN));
338 av_packet_unref(out);
351 static void bsf_list_flush(AVBSFContext *bsf)
353 BSFListContext *lst = bsf->priv_data;
355 for (int i = 0; i < lst->nb_bsfs; i++)
356 av_bsf_flush(lst->bsfs[i]);
360 static void bsf_list_close(AVBSFContext *bsf)
362 BSFListContext *lst = bsf->priv_data;
365 for (i = 0; i < lst->nb_bsfs; ++i)
366 av_bsf_free(&lst->bsfs[i]);
367 av_freep(&lst->bsfs);
368 av_freep(&lst->item_name);
371 static const char *bsf_list_item_name(void *ctx)
373 static const char *null_filter_name = "null";
374 AVBSFContext *bsf_ctx = ctx;
375 BSFListContext *lst = bsf_ctx->priv_data;
378 return null_filter_name;
380 if (!lst->item_name) {
383 av_bprint_init(&bp, 16, 128);
385 av_bprintf(&bp, "bsf_list(");
386 for (i = 0; i < lst->nb_bsfs; i++)
387 av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
388 av_bprintf(&bp, ")");
390 av_bprint_finalize(&bp, &lst->item_name);
393 return lst->item_name;
396 static const AVClass bsf_list_class = {
397 .class_name = "bsf_list",
398 .item_name = bsf_list_item_name,
399 .version = LIBAVUTIL_VERSION_INT,
402 const AVBitStreamFilter ff_list_bsf = {
404 .priv_data_size = sizeof(BSFListContext),
405 .priv_class = &bsf_list_class,
406 .init = bsf_list_init,
407 .filter = bsf_list_filter,
408 .flush = bsf_list_flush,
409 .close = bsf_list_close,
417 AVBSFList *av_bsf_list_alloc(void)
419 return av_mallocz(sizeof(AVBSFList));
422 void av_bsf_list_free(AVBSFList **lst)
429 for (i = 0; i < (*lst)->nb_bsfs; ++i)
430 av_bsf_free(&(*lst)->bsfs[i]);
431 av_free((*lst)->bsfs);
435 int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
437 return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
440 static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
443 const AVBitStreamFilter *filter;
446 filter = av_bsf_get_by_name(bsf_name);
448 return AVERROR_BSF_NOT_FOUND;
450 ret = av_bsf_alloc(filter, &bsf);
454 if (options && filter->priv_class) {
455 const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
456 const char * shorthand[2] = {NULL};
459 shorthand[0] = opt->name;
461 ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
467 ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
472 ret = av_bsf_list_append(lst, bsf);
481 int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
483 return bsf_list_append_internal(lst, bsf_name, NULL, options);
486 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
491 if ((*lst)->nb_bsfs == 1) {
492 *bsf = (*lst)->bsfs[0];
493 av_freep(&(*lst)->bsfs);
498 ret = av_bsf_alloc(&ff_list_bsf, bsf);
502 ctx = (*bsf)->priv_data;
504 ctx->bsfs = (*lst)->bsfs;
505 ctx->nb_bsfs = (*lst)->nb_bsfs;
512 static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
514 char *bsf_name, *bsf_options_str;
516 bsf_name = av_strtok(str, "=", &bsf_options_str);
518 return AVERROR(EINVAL);
520 return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
523 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
526 char *bsf_str, *buf, *dup, *saveptr;
530 return av_bsf_get_null_filter(bsf_lst);
532 lst = av_bsf_list_alloc();
534 return AVERROR(ENOMEM);
536 if (!(dup = buf = av_strdup(str))) {
537 ret = AVERROR(ENOMEM);
541 while (bsf_str = av_strtok(buf, ",", &saveptr)) {
542 ret = bsf_parse_single(bsf_str, lst);
549 ret = av_bsf_list_finalize(&lst, bsf_lst);
552 av_bsf_list_free(&lst);
557 int av_bsf_get_null_filter(AVBSFContext **bsf)
559 return av_bsf_alloc(&ff_list_bsf, bsf);