]> git.sesse.net Git - ffmpeg/blob - libavcodec/bsf.c
avcodec/bsf: use simplified algorithm for bsf_list chained filtering
[ffmpeg] / libavcodec / bsf.c
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 "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"
27
28 #include "avcodec.h"
29 #include "bsf.h"
30
31 struct AVBSFInternal {
32     AVPacket *buffer_pkt;
33     int eof;
34 };
35
36 void av_bsf_free(AVBSFContext **pctx)
37 {
38     AVBSFContext *ctx;
39
40     if (!pctx || !*pctx)
41         return;
42     ctx = *pctx;
43
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);
48
49     if (ctx->internal)
50         av_packet_free(&ctx->internal->buffer_pkt);
51     av_freep(&ctx->internal);
52     av_freep(&ctx->priv_data);
53
54     avcodec_parameters_free(&ctx->par_in);
55     avcodec_parameters_free(&ctx->par_out);
56
57     av_freep(pctx);
58 }
59
60 static void *bsf_child_next(void *obj, void *prev)
61 {
62     AVBSFContext *ctx = obj;
63     if (!prev && ctx->filter->priv_class)
64         return ctx->priv_data;
65     return NULL;
66 }
67
68 static const char *bsf_to_name(void *bsf)
69 {
70     return ((AVBSFContext *)bsf)->filter->name;
71 }
72
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,
80 };
81
82 const AVClass *av_bsf_get_class(void)
83 {
84     return &bsf_class;
85 }
86
87 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
88 {
89     AVBSFContext *ctx;
90     AVBSFInternal *bsfi;
91     int ret;
92
93     ctx = av_mallocz(sizeof(*ctx));
94     if (!ctx)
95         return AVERROR(ENOMEM);
96
97     ctx->av_class = &bsf_class;
98     ctx->filter   = filter;
99
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);
104         goto fail;
105     }
106
107     bsfi = av_mallocz(sizeof(*bsfi));
108     if (!bsfi) {
109         ret = AVERROR(ENOMEM);
110         goto fail;
111     }
112     ctx->internal = bsfi;
113
114     bsfi->buffer_pkt = av_packet_alloc();
115     if (!bsfi->buffer_pkt) {
116         ret = AVERROR(ENOMEM);
117         goto fail;
118     }
119
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);
125             goto fail;
126         }
127         if (filter->priv_class) {
128             *(const AVClass **)ctx->priv_data = filter->priv_class;
129             av_opt_set_defaults(ctx->priv_data);
130         }
131     }
132
133     *pctx = ctx;
134     return 0;
135 fail:
136     av_bsf_free(&ctx);
137     return ret;
138 }
139
140 int av_bsf_init(AVBSFContext *ctx)
141 {
142     int ret, i;
143
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])
148                 break;
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]);
158             }
159             av_log(ctx, AV_LOG_ERROR, "\n");
160             return AVERROR(EINVAL);
161         }
162     }
163
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);
167     if (ret < 0)
168         return ret;
169
170     ctx->time_base_out = ctx->time_base_in;
171
172     if (ctx->filter->init) {
173         ret = ctx->filter->init(ctx);
174         if (ret < 0)
175             return ret;
176     }
177
178     return 0;
179 }
180
181 void av_bsf_flush(AVBSFContext *ctx)
182 {
183     AVBSFInternal *bsfi = ctx->internal;
184
185     bsfi->eof = 0;
186
187     av_packet_unref(bsfi->buffer_pkt);
188
189     if (ctx->filter->flush)
190         ctx->filter->flush(ctx);
191 }
192
193 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
194 {
195     AVBSFInternal *bsfi = ctx->internal;
196     int ret;
197
198     if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
199         bsfi->eof = 1;
200         return 0;
201     }
202
203     if (bsfi->eof) {
204         av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
205         return AVERROR(EINVAL);
206     }
207
208     if (bsfi->buffer_pkt->data ||
209         bsfi->buffer_pkt->side_data_elems)
210         return AVERROR(EAGAIN);
211
212     ret = av_packet_make_refcounted(pkt);
213     if (ret < 0)
214         return ret;
215     av_packet_move_ref(bsfi->buffer_pkt, pkt);
216
217     return 0;
218 }
219
220 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
221 {
222     return ctx->filter->filter(ctx, pkt);
223 }
224
225 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
226 {
227     AVBSFInternal *bsfi = ctx->internal;
228     AVPacket *tmp_pkt;
229
230     if (bsfi->eof)
231         return AVERROR_EOF;
232
233     if (!bsfi->buffer_pkt->data &&
234         !bsfi->buffer_pkt->side_data_elems)
235         return AVERROR(EAGAIN);
236
237     tmp_pkt = av_packet_alloc();
238     if (!tmp_pkt)
239         return AVERROR(ENOMEM);
240
241     *pkt = bsfi->buffer_pkt;
242     bsfi->buffer_pkt = tmp_pkt;
243
244     return 0;
245 }
246
247 int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
248 {
249     AVBSFInternal *bsfi = ctx->internal;
250
251     if (bsfi->eof)
252         return AVERROR_EOF;
253
254     if (!bsfi->buffer_pkt->data &&
255         !bsfi->buffer_pkt->side_data_elems)
256         return AVERROR(EAGAIN);
257
258     av_packet_move_ref(pkt, bsfi->buffer_pkt);
259
260     return 0;
261 }
262
263 typedef struct BSFListContext {
264     const AVClass *class;
265
266     AVBSFContext **bsfs;
267     int nb_bsfs;
268
269     unsigned idx;           // index of currently processed BSF
270
271     char * item_name;
272 } BSFListContext;
273
274
275 static int bsf_list_init(AVBSFContext *bsf)
276 {
277     BSFListContext *lst = bsf->priv_data;
278     int ret, i;
279     const AVCodecParameters *cod_par = bsf->par_in;
280     AVRational tb = bsf->time_base_in;
281
282     for (i = 0; i < lst->nb_bsfs; ++i) {
283         ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
284         if (ret < 0)
285             goto fail;
286
287         lst->bsfs[i]->time_base_in = tb;
288
289         ret = av_bsf_init(lst->bsfs[i]);
290         if (ret < 0)
291             goto fail;
292
293         cod_par = lst->bsfs[i]->par_out;
294         tb = lst->bsfs[i]->time_base_out;
295     }
296
297     bsf->time_base_out = tb;
298     ret = avcodec_parameters_copy(bsf->par_out, cod_par);
299
300 fail:
301     return ret;
302 }
303
304 static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
305 {
306     BSFListContext *lst = bsf->priv_data;
307     int ret, eof = 0;
308
309     if (!lst->nb_bsfs)
310         return ff_bsf_get_packet_ref(bsf, out);
311
312     while (1) {
313         /* get a packet from the previous filter up the chain */
314         if (lst->idx)
315             ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
316         else
317             ret = ff_bsf_get_packet_ref(bsf, out);
318         if (ret == AVERROR(EAGAIN)) {
319             if (!lst->idx)
320                 return ret;
321             lst->idx--;
322             continue;
323         } else if (ret == AVERROR_EOF) {
324             eof = 1;
325         } else if (ret < 0)
326             return ret;
327
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));
332             if (ret < 0) {
333                 av_packet_unref(out);
334                 return ret;
335             }
336             lst->idx++;
337             eof = 0;
338         } else if (eof) {
339             return ret;
340         } else {
341             return 0;
342         }
343     }
344 }
345
346 static void bsf_list_flush(AVBSFContext *bsf)
347 {
348     BSFListContext *lst = bsf->priv_data;
349
350     for (int i = 0; i < lst->nb_bsfs; i++)
351         av_bsf_flush(lst->bsfs[i]);
352     lst->idx = 0;
353 }
354
355 static void bsf_list_close(AVBSFContext *bsf)
356 {
357     BSFListContext *lst = bsf->priv_data;
358     int i;
359
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);
364 }
365
366 static const char *bsf_list_item_name(void *ctx)
367 {
368     static const char *null_filter_name = "null";
369     AVBSFContext *bsf_ctx = ctx;
370     BSFListContext *lst = bsf_ctx->priv_data;
371
372     if (!lst->nb_bsfs)
373         return null_filter_name;
374
375     if (!lst->item_name) {
376         int i;
377         AVBPrint bp;
378         av_bprint_init(&bp, 16, 128);
379
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, ")");
384
385         av_bprint_finalize(&bp, &lst->item_name);
386     }
387
388     return lst->item_name;
389 }
390
391 static const AVClass bsf_list_class = {
392         .class_name = "bsf_list",
393         .item_name  = bsf_list_item_name,
394         .version    = LIBAVUTIL_VERSION_INT,
395 };
396
397 const AVBitStreamFilter ff_list_bsf = {
398         .name           = "bsf_list",
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,
405 };
406
407 struct AVBSFList {
408     AVBSFContext **bsfs;
409     int nb_bsfs;
410 };
411
412 AVBSFList *av_bsf_list_alloc(void)
413 {
414     return av_mallocz(sizeof(AVBSFList));
415 }
416
417 void av_bsf_list_free(AVBSFList **lst)
418 {
419     int i;
420
421     if (!*lst)
422         return;
423
424     for (i = 0; i < (*lst)->nb_bsfs; ++i)
425         av_bsf_free(&(*lst)->bsfs[i]);
426     av_free((*lst)->bsfs);
427     av_freep(lst);
428 }
429
430 int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
431 {
432     return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
433 }
434
435 int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
436 {
437     int ret;
438     const AVBitStreamFilter *filter;
439     AVBSFContext *bsf;
440
441     filter = av_bsf_get_by_name(bsf_name);
442     if (!filter)
443         return AVERROR_BSF_NOT_FOUND;
444
445     ret = av_bsf_alloc(filter, &bsf);
446     if (ret < 0)
447         return ret;
448
449     if (options) {
450         ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
451         if (ret < 0)
452             goto end;
453     }
454
455     ret = av_bsf_list_append(lst, bsf);
456
457 end:
458     if (ret < 0)
459         av_bsf_free(&bsf);
460
461     return ret;
462 }
463
464 int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
465 {
466     int ret = 0;
467     BSFListContext *ctx;
468
469     if ((*lst)->nb_bsfs == 1) {
470         *bsf = (*lst)->bsfs[0];
471         av_freep(&(*lst)->bsfs);
472         (*lst)->nb_bsfs = 0;
473         goto end;
474     }
475
476     ret = av_bsf_alloc(&ff_list_bsf, bsf);
477     if (ret < 0)
478         return ret;
479
480     ctx = (*bsf)->priv_data;
481
482     ctx->bsfs = (*lst)->bsfs;
483     ctx->nb_bsfs = (*lst)->nb_bsfs;
484
485 end:
486     av_freep(lst);
487     return ret;
488 }
489
490 static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
491 {
492     char *bsf_name, *bsf_options_str, *buf;
493     AVDictionary *bsf_options = NULL;
494     int ret = 0;
495
496     if (!(buf = av_strdup(str)))
497         return AVERROR(ENOMEM);
498
499     bsf_name = av_strtok(buf, "=", &bsf_options_str);
500     if (!bsf_name) {
501         ret = AVERROR(EINVAL);
502         goto end;
503     }
504
505     if (bsf_options_str) {
506         ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
507         if (ret < 0)
508             goto end;
509     }
510
511     ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
512
513 end:
514     av_dict_free(&bsf_options);
515     av_free(buf);
516     return ret;
517 }
518
519 int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
520 {
521     AVBSFList *lst;
522     char *bsf_str, *buf, *dup, *saveptr;
523     int ret;
524
525     if (!str)
526         return av_bsf_get_null_filter(bsf_lst);
527
528     lst = av_bsf_list_alloc();
529     if (!lst)
530         return AVERROR(ENOMEM);
531
532     if (!(dup = buf = av_strdup(str))) {
533         ret = AVERROR(ENOMEM);
534         goto end;
535     }
536
537     while (1) {
538         bsf_str = av_strtok(buf, ",", &saveptr);
539         if (!bsf_str)
540             break;
541
542         ret = bsf_parse_single(bsf_str, lst);
543         if (ret < 0)
544             goto end;
545
546         buf = NULL;
547     }
548
549     ret = av_bsf_list_finalize(&lst, bsf_lst);
550 end:
551     if (ret < 0)
552         av_bsf_list_free(&lst);
553     av_free(dup);
554     return ret;
555 }
556
557 int av_bsf_get_null_filter(AVBSFContext **bsf)
558 {
559     return av_bsf_alloc(&ff_list_bsf, bsf);
560 }