• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include <string.h>
20 
21 #include "config_components.h"
22 
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"
29 
30 #include "bsf.h"
31 #include "bsf_internal.h"
32 #include "codec_desc.h"
33 #include "codec_par.h"
34 
35 #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
36 
ff_bsf(const AVBitStreamFilter * bsf)37 static av_always_inline const FFBitStreamFilter *ff_bsf(const AVBitStreamFilter *bsf)
38 {
39     return (const FFBitStreamFilter*)bsf;
40 }
41 
42 typedef struct FFBSFContext {
43     AVBSFContext pub;
44     AVPacket *buffer_pkt;
45     int eof;
46 } FFBSFContext;
47 
ffbsfcontext(AVBSFContext * ctx)48 static av_always_inline FFBSFContext *ffbsfcontext(AVBSFContext *ctx)
49 {
50     return (FFBSFContext *)ctx;
51 }
52 
av_bsf_free(AVBSFContext ** pctx)53 void av_bsf_free(AVBSFContext **pctx)
54 {
55     AVBSFContext *ctx;
56     FFBSFContext *bsfi;
57 
58     if (!pctx || !*pctx)
59         return;
60     ctx  = *pctx;
61     bsfi = ffbsfcontext(ctx);
62 
63     if (ctx->priv_data) {
64         if (ff_bsf(ctx->filter)->close)
65             ff_bsf(ctx->filter)->close(ctx);
66         if (ctx->filter->priv_class)
67             av_opt_free(ctx->priv_data);
68         av_freep(&ctx->priv_data);
69     }
70     av_packet_free(&bsfi->buffer_pkt);
71 
72     avcodec_parameters_free(&ctx->par_in);
73     avcodec_parameters_free(&ctx->par_out);
74 
75     av_freep(pctx);
76 }
77 
bsf_child_next(void * obj,void * prev)78 static void *bsf_child_next(void *obj, void *prev)
79 {
80     AVBSFContext *ctx = obj;
81     if (!prev && ctx->filter->priv_class)
82         return ctx->priv_data;
83     return NULL;
84 }
85 
bsf_to_name(void * bsf)86 static const char *bsf_to_name(void *bsf)
87 {
88     return ((AVBSFContext *)bsf)->filter->name;
89 }
90 
91 static const AVClass bsf_class = {
92     .class_name       = "AVBSFContext",
93     .item_name        = bsf_to_name,
94     .version          = LIBAVUTIL_VERSION_INT,
95     .child_next       = bsf_child_next,
96     .child_class_iterate = ff_bsf_child_class_iterate,
97     .category         = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
98 };
99 
av_bsf_get_class(void)100 const AVClass *av_bsf_get_class(void)
101 {
102     return &bsf_class;
103 }
104 
av_bsf_alloc(const AVBitStreamFilter * filter,AVBSFContext ** pctx)105 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
106 {
107     AVBSFContext *ctx;
108     FFBSFContext *bsfi;
109     int ret;
110 
111     bsfi = av_mallocz(sizeof(*bsfi));
112     if (!bsfi)
113         return AVERROR(ENOMEM);
114     ctx  = &bsfi->pub;
115 
116     ctx->av_class = &bsf_class;
117     ctx->filter   = filter;
118 
119     ctx->par_in  = avcodec_parameters_alloc();
120     ctx->par_out = avcodec_parameters_alloc();
121     if (!ctx->par_in || !ctx->par_out) {
122         ret = AVERROR(ENOMEM);
123         goto fail;
124     }
125     /* allocate priv data and init private options */
126     if (ff_bsf(filter)->priv_data_size) {
127         ctx->priv_data = av_mallocz(ff_bsf(filter)->priv_data_size);
128         if (!ctx->priv_data) {
129             ret = AVERROR(ENOMEM);
130             goto fail;
131         }
132         if (filter->priv_class) {
133             *(const AVClass **)ctx->priv_data = filter->priv_class;
134             av_opt_set_defaults(ctx->priv_data);
135         }
136     }
137     bsfi->buffer_pkt = av_packet_alloc();
138     if (!bsfi->buffer_pkt) {
139         ret = AVERROR(ENOMEM);
140         goto fail;
141     }
142 
143     *pctx = ctx;
144     return 0;
145 fail:
146     av_bsf_free(&ctx);
147     return ret;
148 }
149 
av_bsf_init(AVBSFContext * ctx)150 int av_bsf_init(AVBSFContext *ctx)
151 {
152     int ret, i;
153 
154     /* check that the codec is supported */
155     if (ctx->filter->codec_ids) {
156         for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
157             if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
158                 break;
159         if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
160             const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
161             av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
162                    "bitstream filter '%s'. Supported codecs are: ",
163                    desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
164             for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
165                 enum AVCodecID codec_id = ctx->filter->codec_ids[i];
166                 av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
167                        avcodec_get_name(codec_id), codec_id);
168             }
169             av_log(ctx, AV_LOG_ERROR, "\n");
170             return AVERROR(EINVAL);
171         }
172     }
173 
174     /* initialize output parameters to be the same as input
175      * init below might overwrite that */
176     ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
177     if (ret < 0)
178         return ret;
179 
180     ctx->time_base_out = ctx->time_base_in;
181 
182     if (ff_bsf(ctx->filter)->init) {
183         ret = ff_bsf(ctx->filter)->init(ctx);
184         if (ret < 0)
185             return ret;
186     }
187 
188     return 0;
189 }
190 
av_bsf_flush(AVBSFContext * ctx)191 void av_bsf_flush(AVBSFContext *ctx)
192 {
193     FFBSFContext *const bsfi = ffbsfcontext(ctx);
194 
195     bsfi->eof = 0;
196 
197     av_packet_unref(bsfi->buffer_pkt);
198 
199     if (ff_bsf(ctx->filter)->flush)
200         ff_bsf(ctx->filter)->flush(ctx);
201 }
202 
av_bsf_send_packet(AVBSFContext * ctx,AVPacket * pkt)203 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
204 {
205     FFBSFContext *const bsfi = ffbsfcontext(ctx);
206     int ret;
207 
208     if (!pkt || IS_EMPTY(pkt)) {
209         if (pkt)
210             av_packet_unref(pkt);
211         bsfi->eof = 1;
212         return 0;
213     }
214 
215     if (bsfi->eof) {
216         av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
217         return AVERROR(EINVAL);
218     }
219 
220     if (!IS_EMPTY(bsfi->buffer_pkt))
221         return AVERROR(EAGAIN);
222 
223     ret = av_packet_make_refcounted(pkt);
224     if (ret < 0)
225         return ret;
226     av_packet_move_ref(bsfi->buffer_pkt, pkt);
227 
228     return 0;
229 }
230 
av_bsf_receive_packet(AVBSFContext * ctx,AVPacket * pkt)231 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
232 {
233     return ff_bsf(ctx->filter)->filter(ctx, pkt);
234 }
235 
ff_bsf_get_packet(AVBSFContext * ctx,AVPacket ** pkt)236 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
237 {
238     FFBSFContext *const bsfi = ffbsfcontext(ctx);
239     AVPacket *tmp_pkt;
240 
241     if (bsfi->eof)
242         return AVERROR_EOF;
243 
244     if (IS_EMPTY(bsfi->buffer_pkt))
245         return AVERROR(EAGAIN);
246 
247     tmp_pkt = av_packet_alloc();
248     if (!tmp_pkt)
249         return AVERROR(ENOMEM);
250 
251     *pkt = bsfi->buffer_pkt;
252     bsfi->buffer_pkt = tmp_pkt;
253 
254     return 0;
255 }
256 
ff_bsf_get_packet_ref(AVBSFContext * ctx,AVPacket * pkt)257 int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
258 {
259     FFBSFContext *const bsfi = ffbsfcontext(ctx);
260 
261     if (bsfi->eof)
262         return AVERROR_EOF;
263 
264     if (IS_EMPTY(bsfi->buffer_pkt))
265         return AVERROR(EAGAIN);
266 
267     av_packet_move_ref(pkt, bsfi->buffer_pkt);
268 
269     return 0;
270 }
271 
272 typedef struct BSFListContext {
273     const AVClass *class;
274 
275     AVBSFContext **bsfs;
276     int nb_bsfs;
277 
278     unsigned idx;           // index of currently processed BSF
279 
280     char * item_name;
281 } BSFListContext;
282 
283 
bsf_list_init(AVBSFContext * bsf)284 static int bsf_list_init(AVBSFContext *bsf)
285 {
286     BSFListContext *lst = bsf->priv_data;
287     int ret, i;
288     const AVCodecParameters *cod_par = bsf->par_in;
289     AVRational tb = bsf->time_base_in;
290 
291     for (i = 0; i < lst->nb_bsfs; ++i) {
292         ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
293         if (ret < 0)
294             goto fail;
295 
296         lst->bsfs[i]->time_base_in = tb;
297 
298         ret = av_bsf_init(lst->bsfs[i]);
299         if (ret < 0)
300             goto fail;
301 
302         cod_par = lst->bsfs[i]->par_out;
303         tb = lst->bsfs[i]->time_base_out;
304     }
305 
306     bsf->time_base_out = tb;
307     ret = avcodec_parameters_copy(bsf->par_out, cod_par);
308 
309 fail:
310     return ret;
311 }
312 
bsf_list_filter(AVBSFContext * bsf,AVPacket * out)313 static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
314 {
315     BSFListContext *lst = bsf->priv_data;
316     int ret, eof = 0;
317 
318     if (!lst->nb_bsfs)
319         return ff_bsf_get_packet_ref(bsf, out);
320 
321     while (1) {
322         /* get a packet from the previous filter up the chain */
323         if (lst->idx)
324             ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
325         else
326             ret = ff_bsf_get_packet_ref(bsf, out);
327         if (ret == AVERROR(EAGAIN)) {
328             if (!lst->idx)
329                 return ret;
330             lst->idx--;
331             continue;
332         } else if (ret == AVERROR_EOF) {
333             eof = 1;
334         } else if (ret < 0)
335             return ret;
336 
337         /* send it to the next filter down the chain */
338         if (lst->idx < lst->nb_bsfs) {
339             ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
340             av_assert1(ret != AVERROR(EAGAIN));
341             if (ret < 0) {
342                 av_packet_unref(out);
343                 return ret;
344             }
345             lst->idx++;
346             eof = 0;
347         } else if (eof) {
348             return ret;
349         } else {
350             return 0;
351         }
352     }
353 }
354 
bsf_list_flush(AVBSFContext * bsf)355 static void bsf_list_flush(AVBSFContext *bsf)
356 {
357     BSFListContext *lst = bsf->priv_data;
358 
359     for (int i = 0; i < lst->nb_bsfs; i++)
360         av_bsf_flush(lst->bsfs[i]);
361     lst->idx = 0;
362 }
363 
bsf_list_close(AVBSFContext * bsf)364 static void bsf_list_close(AVBSFContext *bsf)
365 {
366     BSFListContext *lst = bsf->priv_data;
367     int i;
368 
369     for (i = 0; i < lst->nb_bsfs; ++i)
370         av_bsf_free(&lst->bsfs[i]);
371     av_freep(&lst->bsfs);
372     av_freep(&lst->item_name);
373 }
374 
bsf_list_item_name(void * ctx)375 static const char *bsf_list_item_name(void *ctx)
376 {
377     static const char *null_filter_name = "null";
378     AVBSFContext *bsf_ctx = ctx;
379     BSFListContext *lst = bsf_ctx->priv_data;
380 
381     if (!lst->nb_bsfs)
382         return null_filter_name;
383 
384     if (!lst->item_name) {
385         int i;
386         AVBPrint bp;
387         av_bprint_init(&bp, 16, 128);
388 
389         av_bprintf(&bp, "bsf_list(");
390         for (i = 0; i < lst->nb_bsfs; i++)
391             av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
392         av_bprintf(&bp, ")");
393 
394         av_bprint_finalize(&bp, &lst->item_name);
395     }
396 
397     return lst->item_name;
398 }
399 
400 static const AVClass bsf_list_class = {
401         .class_name = "bsf_list",
402         .item_name  = bsf_list_item_name,
403         .version    = LIBAVUTIL_VERSION_INT,
404 };
405 
406 static const FFBitStreamFilter list_bsf = {
407         .p.name         = "bsf_list",
408         .p.priv_class   = &bsf_list_class,
409         .priv_data_size = sizeof(BSFListContext),
410         .init           = bsf_list_init,
411         .filter         = bsf_list_filter,
412         .flush          = bsf_list_flush,
413         .close          = bsf_list_close,
414 };
415 
416 struct AVBSFList {
417     AVBSFContext **bsfs;
418     int nb_bsfs;
419 };
420 
av_bsf_list_alloc(void)421 AVBSFList *av_bsf_list_alloc(void)
422 {
423     return av_mallocz(sizeof(AVBSFList));
424 }
425 
av_bsf_list_free(AVBSFList ** lst)426 void av_bsf_list_free(AVBSFList **lst)
427 {
428     int i;
429 
430     if (!*lst)
431         return;
432 
433     for (i = 0; i < (*lst)->nb_bsfs; ++i)
434         av_bsf_free(&(*lst)->bsfs[i]);
435     av_free((*lst)->bsfs);
436     av_freep(lst);
437 }
438 
av_bsf_list_append(AVBSFList * lst,AVBSFContext * bsf)439 int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
440 {
441     return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
442 }
443 
bsf_list_append_internal(AVBSFList * lst,const char * bsf_name,const char * options,AVDictionary ** options_dict)444 static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
445 {
446     int ret;
447     const AVBitStreamFilter *filter;
448     AVBSFContext *bsf;
449 
450     filter = av_bsf_get_by_name(bsf_name);
451     if (!filter)
452         return AVERROR_BSF_NOT_FOUND;
453 
454     ret = av_bsf_alloc(filter, &bsf);
455     if (ret < 0)
456         return ret;
457 
458     if (options && filter->priv_class) {
459         const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
460         const char * shorthand[2] = {NULL};
461 
462         if (opt)
463             shorthand[0] = opt->name;
464 
465         ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
466         if (ret < 0)
467             goto end;
468     }
469 
470     if (options_dict) {
471         ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
472         if (ret < 0)
473             goto end;
474     }
475 
476     ret = av_bsf_list_append(lst, bsf);
477 
478 end:
479     if (ret < 0)
480         av_bsf_free(&bsf);
481 
482     return ret;
483 }
484 
av_bsf_list_append2(AVBSFList * lst,const char * bsf_name,AVDictionary ** options)485 int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
486 {
487     return bsf_list_append_internal(lst, bsf_name, NULL, options);
488 }
489 
av_bsf_list_finalize(AVBSFList ** lst,AVBSFContext ** bsf)490 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
491 {
492     int ret = 0;
493     BSFListContext *ctx;
494 
495     if ((*lst)->nb_bsfs == 1) {
496         *bsf = (*lst)->bsfs[0];
497         av_freep(&(*lst)->bsfs);
498         (*lst)->nb_bsfs = 0;
499         goto end;
500     }
501 
502     ret = av_bsf_alloc(&list_bsf.p, bsf);
503     if (ret < 0)
504         return ret;
505 
506     ctx = (*bsf)->priv_data;
507 
508     ctx->bsfs = (*lst)->bsfs;
509     ctx->nb_bsfs = (*lst)->nb_bsfs;
510 
511 end:
512     av_freep(lst);
513     return ret;
514 }
515 
bsf_parse_single(char * str,AVBSFList * bsf_lst)516 static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
517 {
518     char *bsf_name, *bsf_options_str;
519 
520     bsf_name = av_strtok(str, "=", &bsf_options_str);
521     if (!bsf_name)
522         return AVERROR(EINVAL);
523 
524     return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
525 }
526 
av_bsf_list_parse_str(const char * str,AVBSFContext ** bsf_lst)527 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
528 {
529     AVBSFList *lst;
530     int ret;
531 
532     if (!str)
533         return av_bsf_get_null_filter(bsf_lst);
534 
535     lst = av_bsf_list_alloc();
536     if (!lst)
537         return AVERROR(ENOMEM);
538 
539     do {
540         char *bsf_str = av_get_token(&str, ",");
541         ret = bsf_parse_single(bsf_str, lst);
542         av_free(bsf_str);
543         if (ret < 0)
544             goto end;
545     } while (*str && *++str);
546 
547     ret = av_bsf_list_finalize(&lst, bsf_lst);
548 end:
549     if (ret < 0)
550         av_bsf_list_free(&lst);
551     return ret;
552 }
553 
av_bsf_get_null_filter(AVBSFContext ** bsf)554 int av_bsf_get_null_filter(AVBSFContext **bsf)
555 {
556 #if CONFIG_NULL_BSF
557     extern const FFBitStreamFilter ff_null_bsf;
558     return av_bsf_alloc(&ff_null_bsf.p, bsf);
559 #else
560     return av_bsf_alloc(&list_bsf.p, bsf);
561 #endif
562 }
563