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 "config_components.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/bprint.h"
31 #include "bsf_internal.h"
32 #include "codec_desc.h"
33 #include "codec_par.h"
34 #include "packet_internal.h"
36 static av_always_inline
const FFBitStreamFilter
*ff_bsf(const AVBitStreamFilter
*bsf
)
38 return (const FFBitStreamFilter
*)bsf
;
41 typedef struct FFBSFContext
{
47 static av_always_inline FFBSFContext
*ffbsfcontext(AVBSFContext
*ctx
)
49 return (FFBSFContext
*)ctx
;
52 void av_bsf_free(AVBSFContext
**pctx
)
60 bsfi
= ffbsfcontext(ctx
);
63 if (ff_bsf(ctx
->filter
)->close
)
64 ff_bsf(ctx
->filter
)->close(ctx
);
65 if (ctx
->filter
->priv_class
)
66 av_opt_free(ctx
->priv_data
);
67 av_freep(&ctx
->priv_data
);
69 av_packet_free(&bsfi
->buffer_pkt
);
71 avcodec_parameters_free(&ctx
->par_in
);
72 avcodec_parameters_free(&ctx
->par_out
);
77 static void *bsf_child_next(void *obj
, void *prev
)
79 AVBSFContext
*ctx
= obj
;
80 if (!prev
&& ctx
->filter
->priv_class
)
81 return ctx
->priv_data
;
85 static const char *bsf_to_name(void *bsf
)
87 return ((AVBSFContext
*)bsf
)->filter
->name
;
90 static const AVClass bsf_class
= {
91 .class_name
= "AVBSFContext",
92 .item_name
= bsf_to_name
,
93 .version
= LIBAVUTIL_VERSION_INT
,
94 .child_next
= bsf_child_next
,
95 .child_class_iterate
= ff_bsf_child_class_iterate
,
96 .category
= AV_CLASS_CATEGORY_BITSTREAM_FILTER
,
99 const AVClass
*av_bsf_get_class(void)
104 int av_bsf_alloc(const AVBitStreamFilter
*filter
, AVBSFContext
**pctx
)
110 bsfi
= av_mallocz(sizeof(*bsfi
));
112 return AVERROR(ENOMEM
);
115 ctx
->av_class
= &bsf_class
;
116 ctx
->filter
= filter
;
118 ctx
->par_in
= avcodec_parameters_alloc();
119 ctx
->par_out
= avcodec_parameters_alloc();
120 if (!ctx
->par_in
|| !ctx
->par_out
) {
121 ret
= AVERROR(ENOMEM
);
124 /* allocate priv data and init private options */
125 if (ff_bsf(filter
)->priv_data_size
) {
126 ctx
->priv_data
= av_mallocz(ff_bsf(filter
)->priv_data_size
);
127 if (!ctx
->priv_data
) {
128 ret
= AVERROR(ENOMEM
);
131 if (filter
->priv_class
) {
132 *(const AVClass
**)ctx
->priv_data
= filter
->priv_class
;
133 av_opt_set_defaults(ctx
->priv_data
);
136 bsfi
->buffer_pkt
= av_packet_alloc();
137 if (!bsfi
->buffer_pkt
) {
138 ret
= AVERROR(ENOMEM
);
149 int av_bsf_init(AVBSFContext
*ctx
)
153 /* check that the codec is supported */
154 if (ctx
->filter
->codec_ids
) {
155 for (i
= 0; ctx
->filter
->codec_ids
[i
] != AV_CODEC_ID_NONE
; i
++)
156 if (ctx
->par_in
->codec_id
== ctx
->filter
->codec_ids
[i
])
158 if (ctx
->filter
->codec_ids
[i
] == AV_CODEC_ID_NONE
) {
159 const AVCodecDescriptor
*desc
= avcodec_descriptor_get(ctx
->par_in
->codec_id
);
160 av_log(ctx
, AV_LOG_ERROR
, "Codec '%s' (%d) is not supported by the "
161 "bitstream filter '%s'. Supported codecs are: ",
162 desc
? desc
->name
: "unknown", ctx
->par_in
->codec_id
, ctx
->filter
->name
);
163 for (i
= 0; ctx
->filter
->codec_ids
[i
] != AV_CODEC_ID_NONE
; i
++) {
164 enum AVCodecID codec_id
= ctx
->filter
->codec_ids
[i
];
165 av_log(ctx
, AV_LOG_ERROR
, "%s (%d) ",
166 avcodec_get_name(codec_id
), codec_id
);
168 av_log(ctx
, AV_LOG_ERROR
, "\n");
169 return AVERROR(EINVAL
);
173 /* initialize output parameters to be the same as input
174 * init below might overwrite that */
175 ret
= avcodec_parameters_copy(ctx
->par_out
, ctx
->par_in
);
179 ctx
->time_base_out
= ctx
->time_base_in
;
181 if (ff_bsf(ctx
->filter
)->init
) {
182 ret
= ff_bsf(ctx
->filter
)->init(ctx
);
190 void av_bsf_flush(AVBSFContext
*ctx
)
192 FFBSFContext
*const bsfi
= ffbsfcontext(ctx
);
196 av_packet_unref(bsfi
->buffer_pkt
);
198 if (ff_bsf(ctx
->filter
)->flush
)
199 ff_bsf(ctx
->filter
)->flush(ctx
);
202 int av_bsf_send_packet(AVBSFContext
*ctx
, AVPacket
*pkt
)
204 FFBSFContext
*const bsfi
= ffbsfcontext(ctx
);
207 if (!pkt
|| AVPACKET_IS_EMPTY(pkt
)) {
209 av_packet_unref(pkt
);
215 av_log(ctx
, AV_LOG_ERROR
, "A non-NULL packet sent after an EOF.\n");
216 return AVERROR(EINVAL
);
219 if (!AVPACKET_IS_EMPTY(bsfi
->buffer_pkt
))
220 return AVERROR(EAGAIN
);
222 ret
= av_packet_make_refcounted(pkt
);
225 av_packet_move_ref(bsfi
->buffer_pkt
, pkt
);
230 int av_bsf_receive_packet(AVBSFContext
*ctx
, AVPacket
*pkt
)
232 return ff_bsf(ctx
->filter
)->filter(ctx
, pkt
);
235 int ff_bsf_get_packet(AVBSFContext
*ctx
, AVPacket
**pkt
)
237 FFBSFContext
*const bsfi
= ffbsfcontext(ctx
);
243 if (AVPACKET_IS_EMPTY(bsfi
->buffer_pkt
))
244 return AVERROR(EAGAIN
);
246 tmp_pkt
= av_packet_alloc();
248 return AVERROR(ENOMEM
);
250 *pkt
= bsfi
->buffer_pkt
;
251 bsfi
->buffer_pkt
= tmp_pkt
;
256 int ff_bsf_get_packet_ref(AVBSFContext
*ctx
, AVPacket
*pkt
)
258 FFBSFContext
*const bsfi
= ffbsfcontext(ctx
);
263 if (AVPACKET_IS_EMPTY(bsfi
->buffer_pkt
))
264 return AVERROR(EAGAIN
);
266 av_packet_move_ref(pkt
, bsfi
->buffer_pkt
);
271 typedef struct BSFListContext
{
272 const AVClass
*class;
277 unsigned idx
; // index of currently processed BSF
283 static int bsf_list_init(AVBSFContext
*bsf
)
285 BSFListContext
*lst
= bsf
->priv_data
;
287 const AVCodecParameters
*cod_par
= bsf
->par_in
;
288 AVRational tb
= bsf
->time_base_in
;
290 for (i
= 0; i
< lst
->nb_bsfs
; ++i
) {
291 ret
= avcodec_parameters_copy(lst
->bsfs
[i
]->par_in
, cod_par
);
295 lst
->bsfs
[i
]->time_base_in
= tb
;
297 ret
= av_bsf_init(lst
->bsfs
[i
]);
301 cod_par
= lst
->bsfs
[i
]->par_out
;
302 tb
= lst
->bsfs
[i
]->time_base_out
;
305 bsf
->time_base_out
= tb
;
306 ret
= avcodec_parameters_copy(bsf
->par_out
, cod_par
);
312 static int bsf_list_filter(AVBSFContext
*bsf
, AVPacket
*out
)
314 BSFListContext
*lst
= bsf
->priv_data
;
318 return ff_bsf_get_packet_ref(bsf
, out
);
321 /* get a packet from the previous filter up the chain */
323 ret
= av_bsf_receive_packet(lst
->bsfs
[lst
->idx
-1], out
);
325 ret
= ff_bsf_get_packet_ref(bsf
, out
);
326 if (ret
== AVERROR(EAGAIN
)) {
331 } else if (ret
== AVERROR_EOF
) {
336 /* send it to the next filter down the chain */
337 if (lst
->idx
< lst
->nb_bsfs
) {
338 ret
= av_bsf_send_packet(lst
->bsfs
[lst
->idx
], eof
? NULL
: out
);
339 av_assert1(ret
!= AVERROR(EAGAIN
));
341 av_packet_unref(out
);
354 static void bsf_list_flush(AVBSFContext
*bsf
)
356 BSFListContext
*lst
= bsf
->priv_data
;
358 for (int i
= 0; i
< lst
->nb_bsfs
; i
++)
359 av_bsf_flush(lst
->bsfs
[i
]);
363 static void bsf_list_close(AVBSFContext
*bsf
)
365 BSFListContext
*lst
= bsf
->priv_data
;
368 for (i
= 0; i
< lst
->nb_bsfs
; ++i
)
369 av_bsf_free(&lst
->bsfs
[i
]);
370 av_freep(&lst
->bsfs
);
371 av_freep(&lst
->item_name
);
374 static const char *bsf_list_item_name(void *ctx
)
376 static const char *null_filter_name
= "null";
377 AVBSFContext
*bsf_ctx
= ctx
;
378 BSFListContext
*lst
= bsf_ctx
->priv_data
;
381 return null_filter_name
;
383 if (!lst
->item_name
) {
386 av_bprint_init(&bp
, 16, 128);
388 av_bprintf(&bp
, "bsf_list(");
389 for (i
= 0; i
< lst
->nb_bsfs
; i
++)
390 av_bprintf(&bp
, i
? ",%s" : "%s", lst
->bsfs
[i
]->filter
->name
);
391 av_bprintf(&bp
, ")");
393 av_bprint_finalize(&bp
, &lst
->item_name
);
396 return lst
->item_name
;
399 static const AVClass bsf_list_class
= {
400 .class_name
= "bsf_list",
401 .item_name
= bsf_list_item_name
,
402 .version
= LIBAVUTIL_VERSION_INT
,
405 static const FFBitStreamFilter list_bsf
= {
406 .p
.name
= "bsf_list",
407 .p
.priv_class
= &bsf_list_class
,
408 .priv_data_size
= sizeof(BSFListContext
),
409 .init
= bsf_list_init
,
410 .filter
= bsf_list_filter
,
411 .flush
= bsf_list_flush
,
412 .close
= bsf_list_close
,
420 AVBSFList
*av_bsf_list_alloc(void)
422 return av_mallocz(sizeof(AVBSFList
));
425 void av_bsf_list_free(AVBSFList
**lst
)
432 for (i
= 0; i
< (*lst
)->nb_bsfs
; ++i
)
433 av_bsf_free(&(*lst
)->bsfs
[i
]);
434 av_free((*lst
)->bsfs
);
438 int av_bsf_list_append(AVBSFList
*lst
, AVBSFContext
*bsf
)
440 return av_dynarray_add_nofree(&lst
->bsfs
, &lst
->nb_bsfs
, bsf
);
443 static int bsf_list_append_internal(AVBSFList
*lst
, const char *bsf_name
, const char *options
, AVDictionary
** options_dict
)
446 const AVBitStreamFilter
*filter
;
449 filter
= av_bsf_get_by_name(bsf_name
);
451 return AVERROR_BSF_NOT_FOUND
;
453 ret
= av_bsf_alloc(filter
, &bsf
);
457 if (options
&& filter
->priv_class
) {
458 const AVOption
*opt
= av_opt_next(bsf
->priv_data
, NULL
);
459 const char * shorthand
[2] = {NULL
};
462 shorthand
[0] = opt
->name
;
464 ret
= av_opt_set_from_string(bsf
->priv_data
, options
, shorthand
, "=", ":");
470 ret
= av_opt_set_dict2(bsf
, options_dict
, AV_OPT_SEARCH_CHILDREN
);
475 ret
= av_bsf_list_append(lst
, bsf
);
484 int av_bsf_list_append2(AVBSFList
*lst
, const char *bsf_name
, AVDictionary
** options
)
486 return bsf_list_append_internal(lst
, bsf_name
, NULL
, options
);
489 int av_bsf_list_finalize(AVBSFList
**lst
, AVBSFContext
**bsf
)
494 if ((*lst
)->nb_bsfs
== 1) {
495 *bsf
= (*lst
)->bsfs
[0];
496 av_freep(&(*lst
)->bsfs
);
501 ret
= av_bsf_alloc(&list_bsf
.p
, bsf
);
505 ctx
= (*bsf
)->priv_data
;
507 ctx
->bsfs
= (*lst
)->bsfs
;
508 ctx
->nb_bsfs
= (*lst
)->nb_bsfs
;
515 static int bsf_parse_single(char *str
, AVBSFList
*bsf_lst
)
517 char *bsf_name
, *bsf_options_str
;
519 bsf_name
= av_strtok(str
, "=", &bsf_options_str
);
521 return AVERROR(EINVAL
);
523 return bsf_list_append_internal(bsf_lst
, bsf_name
, bsf_options_str
, NULL
);
526 int av_bsf_list_parse_str(const char *str
, AVBSFContext
**bsf_lst
)
532 return av_bsf_get_null_filter(bsf_lst
);
534 lst
= av_bsf_list_alloc();
536 return AVERROR(ENOMEM
);
539 char *bsf_str
= av_get_token(&str
, ",");
540 ret
= bsf_parse_single(bsf_str
, lst
);
544 } while (*str
&& *++str
);
546 ret
= av_bsf_list_finalize(&lst
, bsf_lst
);
549 av_bsf_list_free(&lst
);
553 int av_bsf_get_null_filter(AVBSFContext
**bsf
)
556 extern const FFBitStreamFilter ff_null_bsf
;
557 return av_bsf_alloc(&ff_null_bsf
.p
, bsf
);
559 return av_bsf_alloc(&list_bsf
.p
, bsf
);