GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/bsf.c Lines: 167 275 60.7 %
Date: 2021-04-20 15:25:36 Branches: 73 158 46.2 %

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
58184
void av_bsf_free(AVBSFContext **pctx)
41
{
42
    AVBSFContext *ctx;
43
44

58184
    if (!pctx || !*pctx)
45
44562
        return;
46
13622
    ctx = *pctx;
47
48
13622
    if (ctx->internal) {
49
13622
        if (ctx->filter->close)
50
13602
            ctx->filter->close(ctx);
51
13622
        av_packet_free(&ctx->internal->buffer_pkt);
52
13622
        av_freep(&ctx->internal);
53
    }
54

13622
    if (ctx->filter->priv_class && ctx->priv_data)
55
13126
        av_opt_free(ctx->priv_data);
56
57
13622
    av_freep(&ctx->priv_data);
58
59
13622
    avcodec_parameters_free(&ctx->par_in);
60
13622
    avcodec_parameters_free(&ctx->par_out);
61
62
13622
    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
3
static const char *bsf_to_name(void *bsf)
74
{
75
3
    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
#if FF_API_CHILD_CLASS_NEXT
84
    .child_class_next = ff_bsf_child_class_next,
85
#endif
86
    .child_class_iterate = ff_bsf_child_class_iterate,
87
    .category         = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
88
};
89
90
const AVClass *av_bsf_get_class(void)
91
{
92
    return &bsf_class;
93
}
94
95
13622
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
96
{
97
    AVBSFContext *ctx;
98
    AVBSFInternal *bsfi;
99
    int ret;
100
101
13622
    ctx = av_mallocz(sizeof(*ctx));
102
13622
    if (!ctx)
103
        return AVERROR(ENOMEM);
104
105
13622
    ctx->av_class = &bsf_class;
106
13622
    ctx->filter   = filter;
107
108
13622
    ctx->par_in  = avcodec_parameters_alloc();
109
13622
    ctx->par_out = avcodec_parameters_alloc();
110

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

389444
    if (!pkt || IS_EMPTY(pkt)) {
207
5931
        bsfi->eof = 1;
208
5931
        return 0;
209
    }
210
211
383513
    if (bsfi->eof) {
212
        av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
213
        return AVERROR(EINVAL);
214
    }
215
216

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

891
    if (IS_EMPTY(bsfi->buffer_pkt))
241
445
        return AVERROR(EAGAIN);
242
243
446
    tmp_pkt = av_packet_alloc();
244
446
    if (!tmp_pkt)
245
        return AVERROR(ENOMEM);
246
247
446
    *pkt = bsfi->buffer_pkt;
248
446
    bsfi->buffer_pkt = tmp_pkt;
249
250
446
    return 0;
251
}
252
253
768260
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
254
{
255
768260
    AVBSFInternal *bsfi = ctx->internal;
256
257
768260
    if (bsfi->eof)
258
5932
        return AVERROR_EOF;
259
260

762328
    if (IS_EMPTY(bsfi->buffer_pkt))
261
379261
        return AVERROR(EAGAIN);
262
263
383067
    av_packet_move_ref(pkt, bsfi->buffer_pkt);
264
265
383067
    return 0;
266
}
267
268
typedef struct BSFListContext {
269
    const AVClass *class;
270
271
    AVBSFContext **bsfs;
272
    int nb_bsfs;
273
274
    unsigned idx;           // index of currently processed BSF
275
276
    char * item_name;
277
} BSFListContext;
278
279
280
12353
static int bsf_list_init(AVBSFContext *bsf)
281
{
282
12353
    BSFListContext *lst = bsf->priv_data;
283
    int ret, i;
284
12353
    const AVCodecParameters *cod_par = bsf->par_in;
285
12353
    AVRational tb = bsf->time_base_in;
286
287
12353
    for (i = 0; i < lst->nb_bsfs; ++i) {
288
        ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
289
        if (ret < 0)
290
            goto fail;
291
292
        lst->bsfs[i]->time_base_in = tb;
293
294
        ret = av_bsf_init(lst->bsfs[i]);
295
        if (ret < 0)
296
            goto fail;
297
298
        cod_par = lst->bsfs[i]->par_out;
299
        tb = lst->bsfs[i]->time_base_out;
300
    }
301
302
12353
    bsf->time_base_out = tb;
303
12353
    ret = avcodec_parameters_copy(bsf->par_out, cod_par);
304
305
12353
fail:
306
12353
    return ret;
307
}
308
309
747518
static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
310
{
311
747518
    BSFListContext *lst = bsf->priv_data;
312
747518
    int ret, eof = 0;
313
314
747518
    if (!lst->nb_bsfs)
315
747518
        return ff_bsf_get_packet_ref(bsf, out);
316
317
    while (1) {
318
        /* get a packet from the previous filter up the chain */
319
        if (lst->idx)
320
            ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
321
        else
322
            ret = ff_bsf_get_packet_ref(bsf, out);
323
        if (ret == AVERROR(EAGAIN)) {
324
            if (!lst->idx)
325
                return ret;
326
            lst->idx--;
327
            continue;
328
        } else if (ret == AVERROR_EOF) {
329
            eof = 1;
330
        } else if (ret < 0)
331
            return ret;
332
333
        /* send it to the next filter down the chain */
334
        if (lst->idx < lst->nb_bsfs) {
335
            ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
336
            av_assert1(ret != AVERROR(EAGAIN));
337
            if (ret < 0) {
338
                av_packet_unref(out);
339
                return ret;
340
            }
341
            lst->idx++;
342
            eof = 0;
343
        } else if (eof) {
344
            return ret;
345
        } else {
346
            return 0;
347
        }
348
    }
349
}
350
351
28
static void bsf_list_flush(AVBSFContext *bsf)
352
{
353
28
    BSFListContext *lst = bsf->priv_data;
354
355
28
    for (int i = 0; i < lst->nb_bsfs; i++)
356
        av_bsf_flush(lst->bsfs[i]);
357
28
    lst->idx = 0;
358
28
}
359
360
12353
static void bsf_list_close(AVBSFContext *bsf)
361
{
362
12353
    BSFListContext *lst = bsf->priv_data;
363
    int i;
364
365
12353
    for (i = 0; i < lst->nb_bsfs; ++i)
366
        av_bsf_free(&lst->bsfs[i]);
367
12353
    av_freep(&lst->bsfs);
368
12353
    av_freep(&lst->item_name);
369
12353
}
370
371
static const char *bsf_list_item_name(void *ctx)
372
{
373
    static const char *null_filter_name = "null";
374
    AVBSFContext *bsf_ctx = ctx;
375
    BSFListContext *lst = bsf_ctx->priv_data;
376
377
    if (!lst->nb_bsfs)
378
        return null_filter_name;
379
380
    if (!lst->item_name) {
381
        int i;
382
        AVBPrint bp;
383
        av_bprint_init(&bp, 16, 128);
384
385
        av_bprintf(&bp, "bsf_list(");
386
        for (i = 0; i < lst->nb_bsfs; i++)
387
            av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
388
        av_bprintf(&bp, ")");
389
390
        av_bprint_finalize(&bp, &lst->item_name);
391
    }
392
393
    return lst->item_name;
394
}
395
396
static const AVClass bsf_list_class = {
397
        .class_name = "bsf_list",
398
        .item_name  = bsf_list_item_name,
399
        .version    = LIBAVUTIL_VERSION_INT,
400
};
401
402
const AVBitStreamFilter ff_list_bsf = {
403
        .name           = "bsf_list",
404
        .priv_data_size = sizeof(BSFListContext),
405
        .priv_class     = &bsf_list_class,
406
        .init           = bsf_list_init,
407
        .filter         = bsf_list_filter,
408
        .flush          = bsf_list_flush,
409
        .close          = bsf_list_close,
410
};
411
412
struct AVBSFList {
413
    AVBSFContext **bsfs;
414
    int nb_bsfs;
415
};
416
417
556
AVBSFList *av_bsf_list_alloc(void)
418
{
419
556
    return av_mallocz(sizeof(AVBSFList));
420
}
421
422
void av_bsf_list_free(AVBSFList **lst)
423
{
424
    int i;
425
426
    if (!*lst)
427
        return;
428
429
    for (i = 0; i < (*lst)->nb_bsfs; ++i)
430
        av_bsf_free(&(*lst)->bsfs[i]);
431
    av_free((*lst)->bsfs);
432
    av_freep(lst);
433
}
434
435
556
int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
436
{
437
556
    return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
438
}
439
440
556
static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
441
{
442
    int ret;
443
    const AVBitStreamFilter *filter;
444
    AVBSFContext *bsf;
445
446
556
    filter = av_bsf_get_by_name(bsf_name);
447
556
    if (!filter)
448
        return AVERROR_BSF_NOT_FOUND;
449
450
556
    ret = av_bsf_alloc(filter, &bsf);
451
556
    if (ret < 0)
452
        return ret;
453
454

556
    if (options && filter->priv_class) {
455
13
        const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
456
13
        const char * shorthand[2] = {NULL};
457
458
13
        if (opt)
459
13
            shorthand[0] = opt->name;
460
461
13
        ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
462
13
        if (ret < 0)
463
            goto end;
464
    }
465
466
556
    if (options_dict) {
467
        ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
468
        if (ret < 0)
469
            goto end;
470
    }
471
472
556
    ret = av_bsf_list_append(lst, bsf);
473
474
556
end:
475
556
    if (ret < 0)
476
        av_bsf_free(&bsf);
477
478
556
    return ret;
479
}
480
481
int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
482
{
483
    return bsf_list_append_internal(lst, bsf_name, NULL, options);
484
}
485
486
556
int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
487
{
488
556
    int ret = 0;
489
    BSFListContext *ctx;
490
491
556
    if ((*lst)->nb_bsfs == 1) {
492
556
        *bsf = (*lst)->bsfs[0];
493
556
        av_freep(&(*lst)->bsfs);
494
556
        (*lst)->nb_bsfs = 0;
495
556
        goto end;
496
    }
497
498
    ret = av_bsf_alloc(&ff_list_bsf, bsf);
499
    if (ret < 0)
500
        return ret;
501
502
    ctx = (*bsf)->priv_data;
503
504
    ctx->bsfs = (*lst)->bsfs;
505
    ctx->nb_bsfs = (*lst)->nb_bsfs;
506
507
556
end:
508
556
    av_freep(lst);
509
556
    return ret;
510
}
511
512
556
static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
513
{
514
    char *bsf_name, *bsf_options_str;
515
516
556
    bsf_name = av_strtok(str, "=", &bsf_options_str);
517
556
    if (!bsf_name)
518
        return AVERROR(EINVAL);
519
520
556
    return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
521
}
522
523
12909
int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
524
{
525
    AVBSFList *lst;
526
    char *bsf_str, *buf, *dup, *saveptr;
527
    int ret;
528
529
12909
    if (!str)
530
12353
        return av_bsf_get_null_filter(bsf_lst);
531
532
556
    lst = av_bsf_list_alloc();
533
556
    if (!lst)
534
        return AVERROR(ENOMEM);
535
536
556
    if (!(dup = buf = av_strdup(str))) {
537
        ret = AVERROR(ENOMEM);
538
        goto end;
539
    }
540
541
1112
    while (bsf_str = av_strtok(buf, ",", &saveptr)) {
542
556
        ret = bsf_parse_single(bsf_str, lst);
543
556
        if (ret < 0)
544
            goto end;
545
546
556
        buf = NULL;
547
    }
548
549
556
    ret = av_bsf_list_finalize(&lst, bsf_lst);
550
556
end:
551
556
    if (ret < 0)
552
        av_bsf_list_free(&lst);
553
556
    av_free(dup);
554
556
    return ret;
555
}
556
557
12353
int av_bsf_get_null_filter(AVBSFContext **bsf)
558
{
559
12353
    return av_bsf_alloc(&ff_list_bsf, bsf);
560
}