FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 142 270 52.6%
Branches: 68 156 43.6%

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