GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/bsf.c Lines: 165 276 59.8 %
Date: 2020-07-11 02:49:52 Branches: 73 160 45.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 "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
57267
void av_bsf_free(AVBSFContext **pctx)
41
{
42
    AVBSFContext *ctx;
43
44

57267
    if (!pctx || !*pctx)
45
43882
        return;
46
13385
    ctx = *pctx;
47
48
13385
    if (ctx->filter->close)
49
13367
        ctx->filter->close(ctx);
50

13385
    if (ctx->filter->priv_class && ctx->priv_data)
51
12889
        av_opt_free(ctx->priv_data);
52
53
13385
    if (ctx->internal)
54
13385
        av_packet_free(&ctx->internal->buffer_pkt);
55
13385
    av_freep(&ctx->internal);
56
13385
    av_freep(&ctx->priv_data);
57
58
13385
    avcodec_parameters_free(&ctx->par_in);
59
13385
    avcodec_parameters_free(&ctx->par_out);
60
61
13385
    av_freep(pctx);
62
}
63
64
static void *bsf_child_next(void *obj, void *prev)
65
{
66
    AVBSFContext *ctx = obj;
67
    if (!prev && ctx->filter->priv_class)
68
        return ctx->priv_data;
69
    return NULL;
70
}
71
72
static const char *bsf_to_name(void *bsf)
73
{
74
    return ((AVBSFContext *)bsf)->filter->name;
75
}
76
77
static const AVClass bsf_class = {
78
    .class_name       = "AVBSFContext",
79
    .item_name        = bsf_to_name,
80
    .version          = LIBAVUTIL_VERSION_INT,
81
    .child_next       = bsf_child_next,
82
#if FF_API_CHILD_CLASS_NEXT
83
    .child_class_next = ff_bsf_child_class_next,
84
#endif
85
    .child_class_iterate = ff_bsf_child_class_iterate,
86
    .category         = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
87
};
88
89
const AVClass *av_bsf_get_class(void)
90
{
91
    return &bsf_class;
92
}
93
94
13385
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
95
{
96
    AVBSFContext *ctx;
97
    AVBSFInternal *bsfi;
98
    int ret;
99
100
13385
    ctx = av_mallocz(sizeof(*ctx));
101
13385
    if (!ctx)
102
        return AVERROR(ENOMEM);
103
104
13385
    ctx->av_class = &bsf_class;
105
13385
    ctx->filter   = filter;
106
107
13385
    ctx->par_in  = avcodec_parameters_alloc();
108
13385
    ctx->par_out = avcodec_parameters_alloc();
109

13385
    if (!ctx->par_in || !ctx->par_out) {
110
        ret = AVERROR(ENOMEM);
111
        goto fail;
112
    }
113
114
13385
    bsfi = av_mallocz(sizeof(*bsfi));
115
13385
    if (!bsfi) {
116
        ret = AVERROR(ENOMEM);
117
        goto fail;
118
    }
119
13385
    ctx->internal = bsfi;
120
121
13385
    bsfi->buffer_pkt = av_packet_alloc();
122
13385
    if (!bsfi->buffer_pkt) {
123
        ret = AVERROR(ENOMEM);
124
        goto fail;
125
    }
126
127
    /* allocate priv data and init private options */
128
13385
    if (filter->priv_data_size) {
129
13383
        ctx->priv_data = av_mallocz(filter->priv_data_size);
130
13383
        if (!ctx->priv_data) {
131
            ret = AVERROR(ENOMEM);
132
            goto fail;
133
        }
134
13383
        if (filter->priv_class) {
135
12889
            *(const AVClass **)ctx->priv_data = filter->priv_class;
136
12889
            av_opt_set_defaults(ctx->priv_data);
137
        }
138
    }
139
140
13385
    *pctx = ctx;
141
13385
    return 0;
142
fail:
143
    av_bsf_free(&ctx);
144
    return ret;
145
}
146
147
13385
int av_bsf_init(AVBSFContext *ctx)
148
{
149
    int ret, i;
150
151
    /* check that the codec is supported */
152
13385
    if (ctx->filter->codec_ids) {
153
4201
        for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
154
4201
            if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
155
1258
                break;
156
1258
        if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
157
            const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
158
            av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
159
                   "bitstream filter '%s'. Supported codecs are: ",
160
                   desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
161
            for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
162
                desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
163
                av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
164
                       desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
165
            }
166
            av_log(ctx, AV_LOG_ERROR, "\n");
167
            return AVERROR(EINVAL);
168
        }
169
    }
170
171
    /* initialize output parameters to be the same as input
172
     * init below might overwrite that */
173
13385
    ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
174
13385
    if (ret < 0)
175
        return ret;
176
177
13385
    ctx->time_base_out = ctx->time_base_in;
178
179
13385
    if (ctx->filter->init) {
180
13376
        ret = ctx->filter->init(ctx);
181
13376
        if (ret < 0)
182
            return ret;
183
    }
184
185
13385
    return 0;
186
}
187
188
28
void av_bsf_flush(AVBSFContext *ctx)
189
{
190
28
    AVBSFInternal *bsfi = ctx->internal;
191
192
28
    bsfi->eof = 0;
193
194
28
    av_packet_unref(bsfi->buffer_pkt);
195
196
28
    if (ctx->filter->flush)
197
28
        ctx->filter->flush(ctx);
198
28
}
199
200
424779
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
201
{
202
424779
    AVBSFInternal *bsfi = ctx->internal;
203
    int ret;
204
205

424779
    if (!pkt || IS_EMPTY(pkt)) {
206
5868
        bsfi->eof = 1;
207
5868
        return 0;
208
    }
209
210
418911
    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

418911
    if (!IS_EMPTY(bsfi->buffer_pkt))
216
        return AVERROR(EAGAIN);
217
218
418911
    ret = av_packet_make_refcounted(pkt);
219
418911
    if (ret < 0)
220
        return ret;
221
418911
    av_packet_move_ref(bsfi->buffer_pkt, pkt);
222
223
418911
    return 0;
224
}
225
226
843000
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
227
{
228
843000
    return ctx->filter->filter(ctx, pkt);
229
}
230
231
895
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
232
{
233
895
    AVBSFInternal *bsfi = ctx->internal;
234
    AVPacket *tmp_pkt;
235
236
895
    if (bsfi->eof)
237
4
        return AVERROR_EOF;
238
239

891
    if (IS_EMPTY(bsfi->buffer_pkt))
240
445
        return AVERROR(EAGAIN);
241
242
446
    tmp_pkt = av_packet_alloc();
243
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
842327
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
253
{
254
842327
    AVBSFInternal *bsfi = ctx->internal;
255
256
842327
    if (bsfi->eof)
257
5869
        return AVERROR_EOF;
258
259

836458
    if (IS_EMPTY(bsfi->buffer_pkt))
260
417993
        return AVERROR(EAGAIN);
261
262
418465
    av_packet_move_ref(pkt, bsfi->buffer_pkt);
263
264
418465
    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
12124
static int bsf_list_init(AVBSFContext *bsf)
280
{
281
12124
    BSFListContext *lst = bsf->priv_data;
282
    int ret, i;
283
12124
    const AVCodecParameters *cod_par = bsf->par_in;
284
12124
    AVRational tb = bsf->time_base_in;
285
286
12124
    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
12124
    bsf->time_base_out = tb;
302
12124
    ret = avcodec_parameters_copy(bsf->par_out, cod_par);
303
304
12124
fail:
305
12124
    return ret;
306
}
307
308
821724
static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
309
{
310
821724
    BSFListContext *lst = bsf->priv_data;
311
821724
    int ret, eof = 0;
312
313
821724
    if (!lst->nb_bsfs)
314
821724
        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
28
static void bsf_list_flush(AVBSFContext *bsf)
351
{
352
28
    BSFListContext *lst = bsf->priv_data;
353
354
28
    for (int i = 0; i < lst->nb_bsfs; i++)
355
        av_bsf_flush(lst->bsfs[i]);
356
28
    lst->idx = 0;
357
28
}
358
359
12124
static void bsf_list_close(AVBSFContext *bsf)
360
{
361
12124
    BSFListContext *lst = bsf->priv_data;
362
    int i;
363
364
12124
    for (i = 0; i < lst->nb_bsfs; ++i)
365
        av_bsf_free(&lst->bsfs[i]);
366
12124
    av_freep(&lst->bsfs);
367
12124
    av_freep(&lst->item_name);
368
12124
}
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
const AVBitStreamFilter ff_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
553
AVBSFList *av_bsf_list_alloc(void)
417
{
418
553
    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
553
int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
435
{
436
553
    return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
437
}
438
439
553
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
553
    filter = av_bsf_get_by_name(bsf_name);
446
553
    if (!filter)
447
        return AVERROR_BSF_NOT_FOUND;
448
449
553
    ret = av_bsf_alloc(filter, &bsf);
450
553
    if (ret < 0)
451
        return ret;
452
453

553
    if (options && filter->priv_class) {
454
10
        const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
455
10
        const char * shorthand[2] = {NULL};
456
457
10
        if (opt)
458
10
            shorthand[0] = opt->name;
459
460
10
        ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
461
10
        if (ret < 0)
462
            goto end;
463
    }
464
465
553
    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
553
    ret = av_bsf_list_append(lst, bsf);
472
473
553
end:
474
553
    if (ret < 0)
475
        av_bsf_free(&bsf);
476
477
553
    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
553
int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
486
{
487
553
    int ret = 0;
488
    BSFListContext *ctx;
489
490
553
    if ((*lst)->nb_bsfs == 1) {
491
553
        *bsf = (*lst)->bsfs[0];
492
553
        av_freep(&(*lst)->bsfs);
493
553
        (*lst)->nb_bsfs = 0;
494
553
        goto end;
495
    }
496
497
    ret = av_bsf_alloc(&ff_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
553
end:
507
553
    av_freep(lst);
508
553
    return ret;
509
}
510
511
553
static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
512
{
513
    char *bsf_name, *bsf_options_str;
514
515
553
    bsf_name = av_strtok(str, "=", &bsf_options_str);
516
553
    if (!bsf_name)
517
        return AVERROR(EINVAL);
518
519
553
    return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
520
}
521
522
12677
int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
523
{
524
    AVBSFList *lst;
525
    char *bsf_str, *buf, *dup, *saveptr;
526
    int ret;
527
528
12677
    if (!str)
529
12124
        return av_bsf_get_null_filter(bsf_lst);
530
531
553
    lst = av_bsf_list_alloc();
532
553
    if (!lst)
533
        return AVERROR(ENOMEM);
534
535
553
    if (!(dup = buf = av_strdup(str))) {
536
        ret = AVERROR(ENOMEM);
537
        goto end;
538
    }
539
540
1106
    while (bsf_str = av_strtok(buf, ",", &saveptr)) {
541
553
        ret = bsf_parse_single(bsf_str, lst);
542
553
        if (ret < 0)
543
            goto end;
544
545
553
        buf = NULL;
546
    }
547
548
553
    ret = av_bsf_list_finalize(&lst, bsf_lst);
549
553
end:
550
553
    if (ret < 0)
551
        av_bsf_list_free(&lst);
552
553
    av_free(dup);
553
553
    return ret;
554
}
555
556
12124
int av_bsf_get_null_filter(AVBSFContext **bsf)
557
{
558
12124
    return av_bsf_alloc(&ff_list_bsf, bsf);
559
}