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