Coverage Report

Created: 2025-08-28 07:12

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