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)
48 if (ctx->filter->close)
49 ctx->filter->close(ctx);
50 if (ctx->filter->priv_class && ctx->priv_data)
51 av_opt_free(ctx->priv_data);
54 av_packet_free(&ctx->internal->buffer_pkt);
55 av_freep(&ctx->internal);
56 av_freep(&ctx->priv_data);
58 avcodec_parameters_free(&ctx->par_in);
59 avcodec_parameters_free(&ctx->par_out);
64 static void *bsf_child_next(void *obj, void *prev)
66 AVBSFContext *ctx = obj;
67 if (!prev && ctx->filter->priv_class)
68 return ctx->priv_data;
72 static const char *bsf_to_name(void *bsf)
74 return ((AVBSFContext *)bsf)->filter->name;
77 static const AVClass bsf_class = {
78 .class_name = "AVBSFContext",
79 .item_name = bsf_to_name,
80 .version = LIBAVUTIL_VERSION_INT,
81 .child_next = bsf_child_next,
82 #if FF_API_CHILD_CLASS_NEXT
83 .child_class_next = ff_bsf_child_class_next,
85 .child_class_iterate = ff_bsf_child_class_iterate,
86 .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
89 const AVClass *av_bsf_get_class(void)
94 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
100 ctx = av_mallocz(sizeof(*ctx));
102 return AVERROR(ENOMEM);
104 ctx->av_class = &bsf_class;
105 ctx->filter = filter;
107 ctx->par_in = avcodec_parameters_alloc();
108 ctx->par_out = avcodec_parameters_alloc();
109 if (!ctx->par_in || !ctx->par_out) {
110 ret = AVERROR(ENOMEM);
114 bsfi = av_mallocz(sizeof(*bsfi));
116 ret = AVERROR(ENOMEM);
119 ctx->internal = bsfi;
121 bsfi->buffer_pkt = av_packet_alloc();
122 if (!bsfi->buffer_pkt) {
123 ret = AVERROR(ENOMEM);
127 /* allocate priv data and init private options */
128 if (filter->priv_data_size) {
129 ctx->priv_data = av_mallocz(filter->priv_data_size);
130 if (!ctx->priv_data) {
131 ret = AVERROR(ENOMEM);
134 if (filter->priv_class) {
135 *(const AVClass **)ctx->priv_data = filter->priv_class;
136 av_opt_set_defaults(ctx->priv_data);
147 int av_bsf_init(AVBSFContext *ctx)
151 /* check that the codec is supported */
152 if (ctx->filter->codec_ids) {
153 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
154 if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
156 if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
157 const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
158 av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
159 "bitstream filter '%s'. Supported codecs are: ",
160 desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
161 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
162 desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
163 av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
164 desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
166 av_log(ctx, AV_LOG_ERROR, "\n");
167 return AVERROR(EINVAL);
171 /* initialize output parameters to be the same as input
172 * init below might overwrite that */
173 ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
177 ctx->time_base_out = ctx->time_base_in;
179 if (ctx->filter->init) {
180 ret = ctx->filter->init(ctx);
188 void av_bsf_flush(AVBSFContext *ctx)
190 AVBSFInternal *bsfi = ctx->internal;
194 av_packet_unref(bsfi->buffer_pkt);
196 if (ctx->filter->flush)
197 ctx->filter->flush(ctx);
200 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
202 AVBSFInternal *bsfi = ctx->internal;
205 if (!pkt || IS_EMPTY(pkt)) {
211 av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
212 return AVERROR(EINVAL);
215 if (!IS_EMPTY(bsfi->buffer_pkt))
216 return AVERROR(EAGAIN);
218 ret = av_packet_make_refcounted(pkt);
221 av_packet_move_ref(bsfi->buffer_pkt, pkt);
226 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
228 return ctx->filter->filter(ctx, pkt);
231 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
233 AVBSFInternal *bsfi = ctx->internal;
239 if (IS_EMPTY(bsfi->buffer_pkt))
240 return AVERROR(EAGAIN);
242 tmp_pkt = av_packet_alloc();
244 return AVERROR(ENOMEM);
246 *pkt = bsfi->buffer_pkt;
247 bsfi->buffer_pkt = tmp_pkt;
252 int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
254 AVBSFInternal *bsfi = ctx->internal;
259 if (IS_EMPTY(bsfi->buffer_pkt))
260 return AVERROR(EAGAIN);
262 av_packet_move_ref(pkt, bsfi->buffer_pkt);
267 typedef struct BSFListContext {
268 const AVClass *class;
273 unsigned idx; // index of currently processed BSF
279 static int bsf_list_init(AVBSFContext *bsf)
281 BSFListContext *lst = bsf->priv_data;
283 const AVCodecParameters *cod_par = bsf->par_in;
284 AVRational tb = bsf->time_base_in;
286 for (i = 0; i < lst->nb_bsfs; ++i) {
287 ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
291 lst->bsfs[i]->time_base_in = tb;
293 ret = av_bsf_init(lst->bsfs[i]);
297 cod_par = lst->bsfs[i]->par_out;
298 tb = lst->bsfs[i]->time_base_out;
301 bsf->time_base_out = tb;
302 ret = avcodec_parameters_copy(bsf->par_out, cod_par);
308 static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
310 BSFListContext *lst = bsf->priv_data;
314 return ff_bsf_get_packet_ref(bsf, out);
317 /* get a packet from the previous filter up the chain */
319 ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
321 ret = ff_bsf_get_packet_ref(bsf, out);
322 if (ret == AVERROR(EAGAIN)) {
327 } else if (ret == AVERROR_EOF) {
332 /* send it to the next filter down the chain */
333 if (lst->idx < lst->nb_bsfs) {
334 ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
335 av_assert1(ret != AVERROR(EAGAIN));
337 av_packet_unref(out);
350 static void bsf_list_flush(AVBSFContext *bsf)
352 BSFListContext *lst = bsf->priv_data;
354 for (int i = 0; i < lst->nb_bsfs; i++)
355 av_bsf_flush(lst->bsfs[i]);
359 static void bsf_list_close(AVBSFContext *bsf)
361 BSFListContext *lst = bsf->priv_data;
364 for (i = 0; i < lst->nb_bsfs; ++i)
365 av_bsf_free(&lst->bsfs[i]);
366 av_freep(&lst->bsfs);
367 av_freep(&lst->item_name);
370 static const char *bsf_list_item_name(void *ctx)
372 static const char *null_filter_name = "null";
373 AVBSFContext *bsf_ctx = ctx;
374 BSFListContext *lst = bsf_ctx->priv_data;
377 return null_filter_name;
379 if (!lst->item_name) {
382 av_bprint_init(&bp, 16, 128);
384 av_bprintf(&bp, "bsf_list(");
385 for (i = 0; i < lst->nb_bsfs; i++)
386 av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
387 av_bprintf(&bp, ")");
389 av_bprint_finalize(&bp, &lst->item_name);
392 return lst->item_name;
395 static const AVClass bsf_list_class = {
396 .class_name = "bsf_list",
397 .item_name = bsf_list_item_name,
398 .version = LIBAVUTIL_VERSION_INT,
401 const AVBitStreamFilter ff_list_bsf = {
403 .priv_data_size = sizeof(BSFListContext),
404 .priv_class = &bsf_list_class,
405 .init = bsf_list_init,
406 .filter = bsf_list_filter,
407 .flush = bsf_list_flush,
408 .close = bsf_list_close,
416 AVBSFList *av_bsf_list_alloc(void)
418 return av_mallocz(sizeof(AVBSFList));
421 void av_bsf_list_free(AVBSFList **lst)
428 for (i = 0; i < (*lst)->nb_bsfs; ++i)
429 av_bsf_free(&(*lst)->bsfs[i]);
430 av_free((*lst)->bsfs);
434 int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
436 return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
439 static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
442 const AVBitStreamFilter *filter;
445 filter = av_bsf_get_by_name(bsf_name);
447 return AVERROR_BSF_NOT_FOUND;
449 ret = av_bsf_alloc(filter, &bsf);
453 if (options && filter->priv_class) {
454 const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
455 const char * shorthand[2] = {NULL};
458 shorthand[0] = opt->name;
460 ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
466 ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
471 ret = av_bsf_list_append(lst, bsf);
480 int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
482 return bsf_list_append_internal(lst, bsf_name, NULL, options);
485 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
490 if ((*lst)->nb_bsfs == 1) {
491 *bsf = (*lst)->bsfs[0];
492 av_freep(&(*lst)->bsfs);
497 ret = av_bsf_alloc(&ff_list_bsf, bsf);
501 ctx = (*bsf)->priv_data;
503 ctx->bsfs = (*lst)->bsfs;
504 ctx->nb_bsfs = (*lst)->nb_bsfs;
511 static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
513 char *bsf_name, *bsf_options_str;
515 bsf_name = av_strtok(str, "=", &bsf_options_str);
517 return AVERROR(EINVAL);
519 return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
522 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
525 char *bsf_str, *buf, *dup, *saveptr;
529 return av_bsf_get_null_filter(bsf_lst);
531 lst = av_bsf_list_alloc();
533 return AVERROR(ENOMEM);
535 if (!(dup = buf = av_strdup(str))) {
536 ret = AVERROR(ENOMEM);
540 while (bsf_str = av_strtok(buf, ",", &saveptr)) {
541 ret = bsf_parse_single(bsf_str, lst);
548 ret = av_bsf_list_finalize(&lst, bsf_lst);
551 av_bsf_list_free(&lst);
556 int av_bsf_get_null_filter(AVBSFContext **bsf)
558 return av_bsf_alloc(&ff_list_bsf, bsf);