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