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 struct AVBSFInternal {
36 void av_bsf_free(AVBSFContext **pctx)
44 if (ctx->filter->close)
45 ctx->filter->close(ctx);
46 if (ctx->filter->priv_class && ctx->priv_data)
47 av_opt_free(ctx->priv_data);
50 av_packet_free(&ctx->internal->buffer_pkt);
51 av_freep(&ctx->internal);
52 av_freep(&ctx->priv_data);
54 avcodec_parameters_free(&ctx->par_in);
55 avcodec_parameters_free(&ctx->par_out);
60 static void *bsf_child_next(void *obj, void *prev)
62 AVBSFContext *ctx = obj;
63 if (!prev && ctx->filter->priv_class)
64 return ctx->priv_data;
68 static const char *bsf_to_name(void *bsf)
70 return ((AVBSFContext *)bsf)->filter->name;
73 static const AVClass bsf_class = {
74 .class_name = "AVBSFContext",
75 .item_name = bsf_to_name,
76 .version = LIBAVUTIL_VERSION_INT,
77 .child_next = bsf_child_next,
78 .child_class_next = ff_bsf_child_class_next,
79 .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
82 const AVClass *av_bsf_get_class(void)
87 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
93 ctx = av_mallocz(sizeof(*ctx));
95 return AVERROR(ENOMEM);
97 ctx->av_class = &bsf_class;
100 ctx->par_in = avcodec_parameters_alloc();
101 ctx->par_out = avcodec_parameters_alloc();
102 if (!ctx->par_in || !ctx->par_out) {
103 ret = AVERROR(ENOMEM);
107 bsfi = av_mallocz(sizeof(*bsfi));
109 ret = AVERROR(ENOMEM);
112 ctx->internal = bsfi;
114 bsfi->buffer_pkt = av_packet_alloc();
115 if (!bsfi->buffer_pkt) {
116 ret = AVERROR(ENOMEM);
120 /* allocate priv data and init private options */
121 if (filter->priv_data_size) {
122 ctx->priv_data = av_mallocz(filter->priv_data_size);
123 if (!ctx->priv_data) {
124 ret = AVERROR(ENOMEM);
127 if (filter->priv_class) {
128 *(const AVClass **)ctx->priv_data = filter->priv_class;
129 av_opt_set_defaults(ctx->priv_data);
140 int av_bsf_init(AVBSFContext *ctx)
144 /* check that the codec is supported */
145 if (ctx->filter->codec_ids) {
146 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
147 if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
149 if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
150 const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
151 av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
152 "bitstream filter '%s'. Supported codecs are: ",
153 desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
154 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
155 desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
156 av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
157 desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
159 av_log(ctx, AV_LOG_ERROR, "\n");
160 return AVERROR(EINVAL);
164 /* initialize output parameters to be the same as input
165 * init below might overwrite that */
166 ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
170 ctx->time_base_out = ctx->time_base_in;
172 if (ctx->filter->init) {
173 ret = ctx->filter->init(ctx);
181 void av_bsf_flush(AVBSFContext *ctx)
183 AVBSFInternal *bsfi = ctx->internal;
187 av_packet_unref(bsfi->buffer_pkt);
189 if (ctx->filter->flush)
190 ctx->filter->flush(ctx);
193 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
195 AVBSFInternal *bsfi = ctx->internal;
198 if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
204 av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
205 return AVERROR(EINVAL);
208 if (bsfi->buffer_pkt->data ||
209 bsfi->buffer_pkt->side_data_elems)
210 return AVERROR(EAGAIN);
212 ret = av_packet_make_refcounted(pkt);
215 av_packet_move_ref(bsfi->buffer_pkt, pkt);
220 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
222 return ctx->filter->filter(ctx, pkt);
225 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
227 AVBSFInternal *bsfi = ctx->internal;
233 if (!bsfi->buffer_pkt->data &&
234 !bsfi->buffer_pkt->side_data_elems)
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 (!bsfi->buffer_pkt->data &&
255 !bsfi->buffer_pkt->side_data_elems)
256 return AVERROR(EAGAIN);
258 av_packet_move_ref(pkt, bsfi->buffer_pkt);
263 typedef struct BSFListContext {
264 const AVClass *class;
269 unsigned idx; // index of currently processed BSF
275 static int bsf_list_init(AVBSFContext *bsf)
277 BSFListContext *lst = bsf->priv_data;
279 const AVCodecParameters *cod_par = bsf->par_in;
280 AVRational tb = bsf->time_base_in;
282 for (i = 0; i < lst->nb_bsfs; ++i) {
283 ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
287 lst->bsfs[i]->time_base_in = tb;
289 ret = av_bsf_init(lst->bsfs[i]);
293 cod_par = lst->bsfs[i]->par_out;
294 tb = lst->bsfs[i]->time_base_out;
297 bsf->time_base_out = tb;
298 ret = avcodec_parameters_copy(bsf->par_out, cod_par);
304 static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
306 BSFListContext *lst = bsf->priv_data;
310 return ff_bsf_get_packet_ref(bsf, out);
313 /* get a packet from the previous filter up the chain */
315 ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
317 ret = ff_bsf_get_packet_ref(bsf, out);
318 if (ret == AVERROR(EAGAIN)) {
323 } else if (ret == AVERROR_EOF) {
328 /* send it to the next filter down the chain */
329 if (lst->idx < lst->nb_bsfs) {
330 ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
331 av_assert1(ret != AVERROR(EAGAIN));
333 av_packet_unref(out);
346 static void bsf_list_flush(AVBSFContext *bsf)
348 BSFListContext *lst = bsf->priv_data;
350 for (int i = 0; i < lst->nb_bsfs; i++)
351 av_bsf_flush(lst->bsfs[i]);
355 static void bsf_list_close(AVBSFContext *bsf)
357 BSFListContext *lst = bsf->priv_data;
360 for (i = 0; i < lst->nb_bsfs; ++i)
361 av_bsf_free(&lst->bsfs[i]);
362 av_freep(&lst->bsfs);
363 av_freep(&lst->item_name);
366 static const char *bsf_list_item_name(void *ctx)
368 static const char *null_filter_name = "null";
369 AVBSFContext *bsf_ctx = ctx;
370 BSFListContext *lst = bsf_ctx->priv_data;
373 return null_filter_name;
375 if (!lst->item_name) {
378 av_bprint_init(&bp, 16, 128);
380 av_bprintf(&bp, "bsf_list(");
381 for (i = 0; i < lst->nb_bsfs; i++)
382 av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
383 av_bprintf(&bp, ")");
385 av_bprint_finalize(&bp, &lst->item_name);
388 return lst->item_name;
391 static const AVClass bsf_list_class = {
392 .class_name = "bsf_list",
393 .item_name = bsf_list_item_name,
394 .version = LIBAVUTIL_VERSION_INT,
397 const AVBitStreamFilter ff_list_bsf = {
399 .priv_data_size = sizeof(BSFListContext),
400 .priv_class = &bsf_list_class,
401 .init = bsf_list_init,
402 .filter = bsf_list_filter,
403 .flush = bsf_list_flush,
404 .close = bsf_list_close,
412 AVBSFList *av_bsf_list_alloc(void)
414 return av_mallocz(sizeof(AVBSFList));
417 void av_bsf_list_free(AVBSFList **lst)
424 for (i = 0; i < (*lst)->nb_bsfs; ++i)
425 av_bsf_free(&(*lst)->bsfs[i]);
426 av_free((*lst)->bsfs);
430 int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
432 return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
435 int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
438 const AVBitStreamFilter *filter;
441 filter = av_bsf_get_by_name(bsf_name);
443 return AVERROR_BSF_NOT_FOUND;
445 ret = av_bsf_alloc(filter, &bsf);
450 ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
455 ret = av_bsf_list_append(lst, bsf);
464 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
469 if ((*lst)->nb_bsfs == 1) {
470 *bsf = (*lst)->bsfs[0];
471 av_freep(&(*lst)->bsfs);
476 ret = av_bsf_alloc(&ff_list_bsf, bsf);
480 ctx = (*bsf)->priv_data;
482 ctx->bsfs = (*lst)->bsfs;
483 ctx->nb_bsfs = (*lst)->nb_bsfs;
490 static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
492 char *bsf_name, *bsf_options_str, *buf;
493 AVDictionary *bsf_options = NULL;
496 if (!(buf = av_strdup(str)))
497 return AVERROR(ENOMEM);
499 bsf_name = av_strtok(buf, "=", &bsf_options_str);
501 ret = AVERROR(EINVAL);
505 if (bsf_options_str) {
506 ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
511 ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
514 av_dict_free(&bsf_options);
519 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
522 char *bsf_str, *buf, *dup, *saveptr;
526 return av_bsf_get_null_filter(bsf_lst);
528 lst = av_bsf_list_alloc();
530 return AVERROR(ENOMEM);
532 if (!(dup = buf = av_strdup(str))) {
533 ret = AVERROR(ENOMEM);
538 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);