Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/bsf/extract_extradata.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 <stdint.h>
20
21
#include "libavutil/log.h"
22
#include "libavutil/mem.h"
23
#include "libavutil/opt.h"
24
25
#include "av1.h"
26
#include "av1_parse.h"
27
#include "bsf.h"
28
#include "bsf_internal.h"
29
#include "bytestream.h"
30
#include "h2645_parse.h"
31
#include "h264.h"
32
#include "startcode.h"
33
#include "vc1_common.h"
34
#include "vvc.h"
35
36
#include "hevc/hevc.h"
37
38
typedef struct ExtractExtradataContext {
39
    const AVClass *class;
40
41
    int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
42
                   uint8_t **data, int *size);
43
44
    /* AV1 specific fields */
45
    AV1Packet av1_pkt;
46
47
    /* H264/HEVC specific fields */
48
    H2645Packet h2645_pkt;
49
50
    /* AVOptions */
51
    int remove;
52
} ExtractExtradataContext;
53
54
static int val_in_array(const int *arr, size_t len, int val)
55
6.10M
{
56
18.9M
    for (size_t i = 0; i < len; i++)
57
12.9M
        if (arr[i] == val)
58
126k
            return 1;
59
5.97M
    return 0;
60
6.10M
}
61
62
static int metadata_is_global(const AV1OBU *obu)
63
1.35k
{
64
1.35k
    static const int metadata_obu_types[] = {
65
1.35k
        AV1_METADATA_TYPE_HDR_CLL, AV1_METADATA_TYPE_HDR_MDCV,
66
1.35k
    };
67
1.35k
    GetBitContext gb;
68
1.35k
    int metadata_type;
69
70
1.35k
    if (init_get_bits(&gb, obu->data, obu->size_bits) < 0)
71
0
        return 0;
72
73
1.35k
    metadata_type = get_leb(&gb);
74
75
1.35k
    return val_in_array(metadata_obu_types, FF_ARRAY_ELEMS(metadata_obu_types),
76
1.35k
                        metadata_type);
77
1.35k
}
78
79
static int obu_is_global(const AV1OBU *obu)
80
2.11M
{
81
2.11M
    static const int extradata_obu_types[] = {
82
2.11M
        AV1_OBU_SEQUENCE_HEADER, AV1_OBU_METADATA,
83
2.11M
    };
84
85
2.11M
    if (!val_in_array(extradata_obu_types, FF_ARRAY_ELEMS(extradata_obu_types),
86
2.11M
                      obu->type))
87
2.10M
        return 0;
88
3.61k
    if (obu->type != AV1_OBU_METADATA)
89
2.25k
        return 1;
90
91
1.35k
    return metadata_is_global(obu);
92
3.61k
}
93
94
static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
95
                                 uint8_t **data, int *size)
96
6.55k
{
97
98
6.55k
    ExtractExtradataContext *s = ctx->priv_data;
99
100
6.55k
    int extradata_size = 0, filtered_size = 0;
101
6.55k
    int i, has_seq = 0, ret = 0;
102
103
6.55k
    ret = ff_av1_packet_split(&s->av1_pkt, pkt->data, pkt->size, ctx);
104
6.55k
    if (ret < 0)
105
2.56k
        return ret;
106
107
1.06M
    for (i = 0; i < s->av1_pkt.nb_obus; i++) {
108
1.05M
        AV1OBU *obu = &s->av1_pkt.obus[i];
109
1.05M
        if (obu_is_global(obu)) {
110
1.48k
            extradata_size += obu->raw_size;
111
1.48k
            if (obu->type == AV1_OBU_SEQUENCE_HEADER)
112
1.12k
                has_seq = 1;
113
1.05M
        } else if (s->remove) {
114
592
            filtered_size += obu->raw_size;
115
592
        }
116
1.05M
    }
117
118
3.98k
    if (extradata_size && has_seq) {
119
946
        AVBufferRef *filtered_buf = NULL;
120
946
        PutByteContext pb_filtered_data, pb_extradata;
121
946
        uint8_t *extradata;
122
123
946
        if (s->remove) {
124
266
            filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
125
266
            if (!filtered_buf) {
126
0
                return AVERROR(ENOMEM);
127
0
            }
128
266
            memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
129
266
        }
130
131
946
        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
132
946
        if (!extradata) {
133
0
            av_buffer_unref(&filtered_buf);
134
0
            return AVERROR(ENOMEM);
135
0
        }
136
137
946
        *data = extradata;
138
946
        *size = extradata_size;
139
140
946
        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
141
946
        if (s->remove)
142
266
            bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size);
143
144
1.05M
        for (i = 0; i < s->av1_pkt.nb_obus; i++) {
145
1.05M
            AV1OBU *obu = &s->av1_pkt.obus[i];
146
1.05M
            if (obu_is_global(obu)) {
147
1.24k
                bytestream2_put_bufferu(&pb_extradata, obu->raw_data, obu->raw_size);
148
1.05M
            } else if (s->remove) {
149
412
                bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size);
150
412
            }
151
1.05M
        }
152
153
946
        if (s->remove) {
154
266
            av_buffer_unref(&pkt->buf);
155
266
            pkt->buf  = filtered_buf;
156
266
            pkt->data = filtered_buf->data;
157
266
            pkt->size = filtered_size;
158
266
        }
159
946
    }
160
161
3.98k
    return 0;
162
3.98k
}
163
164
static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
165
                                   uint8_t **data, int *size)
166
10.8k
{
167
10.8k
    static const int extradata_nal_types_vvc[] = {
168
10.8k
        VVC_VPS_NUT, VVC_SPS_NUT, VVC_PPS_NUT,
169
10.8k
    };
170
10.8k
    static const int extradata_nal_types_hevc[] = {
171
10.8k
        HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
172
10.8k
    };
173
10.8k
    static const int extradata_nal_types_h264[] = {
174
10.8k
        H264_NAL_SPS, H264_NAL_PPS,
175
10.8k
    };
176
177
10.8k
    ExtractExtradataContext *s = ctx->priv_data;
178
179
10.8k
    int extradata_size = 0, filtered_size = 0;
180
10.8k
    const int *extradata_nal_types;
181
10.8k
    size_t nb_extradata_nal_types;
182
10.8k
    int i, has_sps = 0, has_vps = 0, ret = 0;
183
184
10.8k
    if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) {
185
2.33k
        extradata_nal_types    = extradata_nal_types_vvc;
186
2.33k
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_vvc);
187
8.46k
    } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
188
4.23k
        extradata_nal_types    = extradata_nal_types_hevc;
189
4.23k
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
190
4.23k
    } else {
191
4.22k
        extradata_nal_types    = extradata_nal_types_h264;
192
4.22k
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
193
4.22k
    }
194
195
10.8k
    ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
196
10.8k
                                ctx, 0, ctx->par_in->codec_id, H2645_FLAG_SMALL_PADDING);
197
10.8k
    if (ret < 0)
198
1.15k
        return ret;
199
200
2.49M
    for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
201
2.48M
        H2645NAL *nal = &s->h2645_pkt.nals[i];
202
2.48M
        if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
203
62.9k
            extradata_size += nal->raw_size + 3;
204
62.9k
            if (ctx->par_in->codec_id == AV_CODEC_ID_VVC) {
205
1.25k
                if (nal->type == VVC_SPS_NUT) has_sps = 1;
206
1.25k
                if (nal->type == VVC_VPS_NUT) has_vps = 1;
207
61.6k
            } else if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
208
9.08k
                if (nal->type == HEVC_NAL_SPS) has_sps = 1;
209
9.08k
                if (nal->type == HEVC_NAL_VPS) has_vps = 1;
210
52.5k
            } else {
211
52.5k
                if (nal->type == H264_NAL_SPS) has_sps = 1;
212
52.5k
            }
213
2.41M
        } else if (s->remove) {
214
8.13k
            filtered_size += nal->raw_size + 3;
215
8.13k
        }
216
2.48M
    }
217
218
9.65k
    if (extradata_size &&
219
9.65k
        ((ctx->par_in->codec_id == AV_CODEC_ID_VVC  && has_sps) ||
220
4.06k
         (ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
221
4.06k
         (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
222
2.34k
        AVBufferRef *filtered_buf = NULL;
223
2.34k
        PutByteContext pb_filtered_data, pb_extradata;
224
2.34k
        uint8_t *extradata;
225
226
2.34k
        if (s->remove) {
227
319
            filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
228
319
            if (!filtered_buf) {
229
0
                return AVERROR(ENOMEM);
230
0
            }
231
319
            memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
232
319
        }
233
234
2.34k
        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
235
2.34k
        if (!extradata) {
236
0
            av_buffer_unref(&filtered_buf);
237
0
            return AVERROR(ENOMEM);
238
0
        }
239
240
2.34k
        *data = extradata;
241
2.34k
        *size = extradata_size;
242
243
2.34k
        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
244
2.34k
        if (s->remove)
245
319
            bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size);
246
247
1.50M
        for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
248
1.50M
            H2645NAL *nal = &s->h2645_pkt.nals[i];
249
1.50M
            if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
250
1.50M
                             nal->type)) {
251
59.7k
                bytestream2_put_be24u(&pb_extradata, 1); //startcode
252
59.7k
                bytestream2_put_bufferu(&pb_extradata, nal->raw_data, nal->raw_size);
253
1.44M
            } else if (s->remove) {
254
7.43k
                bytestream2_put_be24u(&pb_filtered_data, 1); // startcode
255
7.43k
                bytestream2_put_bufferu(&pb_filtered_data, nal->raw_data, nal->raw_size);
256
7.43k
            }
257
1.50M
        }
258
259
2.34k
        if (s->remove) {
260
319
            av_buffer_unref(&pkt->buf);
261
319
            pkt->buf  = filtered_buf;
262
319
            pkt->data = filtered_buf->data;
263
319
            pkt->size = filtered_size;
264
319
        }
265
2.34k
    }
266
267
9.65k
    return 0;
268
9.65k
}
269
270
static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
271
                                 uint8_t **data, int *size)
272
2.15k
{
273
2.15k
    ExtractExtradataContext *s = ctx->priv_data;
274
2.15k
    const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
275
2.15k
    uint32_t state = UINT32_MAX;
276
2.15k
    int has_extradata = 0, extradata_size = 0;
277
278
15.5k
    while (ptr < end) {
279
14.0k
        ptr = avpriv_find_start_code(ptr, end, &state);
280
14.0k
        if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
281
1.77k
            has_extradata = 1;
282
12.2k
        } else if (has_extradata && IS_MARKER(state)) {
283
680
            extradata_size = ptr - 4 - pkt->data;
284
680
            break;
285
680
        }
286
14.0k
    }
287
288
2.15k
    if (extradata_size) {
289
680
        *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
290
680
        if (!*data)
291
0
            return AVERROR(ENOMEM);
292
293
680
        memcpy(*data, pkt->data, extradata_size);
294
680
        *size = extradata_size;
295
296
680
        if (s->remove) {
297
250
            pkt->data += extradata_size;
298
250
            pkt->size -= extradata_size;
299
250
        }
300
680
    }
301
302
2.15k
    return 0;
303
2.15k
}
304
305
static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt,
306
                                     uint8_t **data, int *size)
307
3.30k
{
308
3.30k
    ExtractExtradataContext *s = ctx->priv_data;
309
3.30k
    uint32_t state = UINT32_MAX;
310
3.30k
    int i, found = 0;
311
312
5.02M
    for (i = 0; i < pkt->size; i++) {
313
5.01M
        state = (state << 8) | pkt->data[i];
314
5.01M
        if (state == 0x1B3)
315
999
            found = 1;
316
5.01M
        else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
317
655
            *size = i - 3;
318
655
            *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
319
655
            if (!*data)
320
0
                return AVERROR(ENOMEM);
321
322
655
            memcpy(*data, pkt->data, *size);
323
324
655
            if (s->remove) {
325
222
                pkt->data += *size;
326
222
                pkt->size -= *size;
327
222
            }
328
655
            break;
329
655
        }
330
5.01M
    }
331
3.30k
    return 0;
332
3.30k
}
333
334
static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt,
335
                                   uint8_t **data, int *size)
336
3.77k
{
337
3.77k
    ExtractExtradataContext *s = ctx->priv_data;
338
3.77k
    const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
339
3.77k
    uint32_t state = UINT32_MAX;
340
341
87.3k
    while (ptr < end) {
342
84.9k
        ptr = avpriv_find_start_code(ptr, end, &state);
343
84.9k
        if (state == 0x1B3 || state == 0x1B6) {
344
1.39k
            if (ptr - pkt->data > 4) {
345
1.14k
                *size = ptr - 4 - pkt->data;
346
1.14k
                *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
347
1.14k
                if (!*data)
348
0
                    return AVERROR(ENOMEM);
349
350
1.14k
                memcpy(*data, pkt->data, *size);
351
352
1.14k
                if (s->remove) {
353
216
                    pkt->data += *size;
354
216
                    pkt->size -= *size;
355
216
                }
356
1.14k
            }
357
1.39k
            break;
358
1.39k
        }
359
84.9k
    }
360
3.77k
    return 0;
361
3.77k
}
362
363
static const struct {
364
    enum AVCodecID id;
365
    int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
366
                   uint8_t **data, int *size);
367
} extract_tab[] = {
368
    { AV_CODEC_ID_AV1,        extract_extradata_av1     },
369
    { AV_CODEC_ID_AVS2,       extract_extradata_mpeg4   },
370
    { AV_CODEC_ID_AVS3,       extract_extradata_mpeg4   },
371
    { AV_CODEC_ID_CAVS,       extract_extradata_mpeg4   },
372
    { AV_CODEC_ID_H264,       extract_extradata_h2645   },
373
    { AV_CODEC_ID_HEVC,       extract_extradata_h2645   },
374
    { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12  },
375
    { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12  },
376
    { AV_CODEC_ID_MPEG4,      extract_extradata_mpeg4   },
377
    { AV_CODEC_ID_VC1,        extract_extradata_vc1     },
378
    { AV_CODEC_ID_VVC,        extract_extradata_h2645   },
379
};
380
381
static int extract_extradata_init(AVBSFContext *ctx)
382
3.52k
{
383
3.52k
    ExtractExtradataContext *s = ctx->priv_data;
384
3.52k
    int i;
385
386
22.9k
    for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
387
22.9k
        if (extract_tab[i].id == ctx->par_in->codec_id) {
388
3.52k
            s->extract = extract_tab[i].extract;
389
3.52k
            break;
390
3.52k
        }
391
22.9k
    }
392
3.52k
    if (!s->extract)
393
0
        return AVERROR_BUG;
394
395
3.52k
    return 0;
396
3.52k
}
397
398
static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
399
48.3k
{
400
48.3k
    ExtractExtradataContext *s = ctx->priv_data;
401
48.3k
    uint8_t *extradata = NULL;
402
48.3k
    int extradata_size;
403
48.3k
    int ret = 0;
404
405
48.3k
    ret = ff_bsf_get_packet_ref(ctx, pkt);
406
48.3k
    if (ret < 0)
407
21.7k
        return ret;
408
409
26.6k
    ret = s->extract(ctx, pkt, &extradata, &extradata_size);
410
26.6k
    if (ret < 0)
411
3.71k
        goto fail;
412
413
22.8k
    if (extradata) {
414
5.76k
        memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
415
5.76k
        ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
416
5.76k
                                      extradata, extradata_size);
417
5.76k
        if (ret < 0) {
418
0
            av_freep(&extradata);
419
0
            goto fail;
420
0
        }
421
5.76k
    }
422
423
22.8k
    return 0;
424
425
3.71k
fail:
426
3.71k
    av_packet_unref(pkt);
427
3.71k
    return ret;
428
22.8k
}
429
430
static void extract_extradata_close(AVBSFContext *ctx)
431
3.54k
{
432
3.54k
    ExtractExtradataContext *s = ctx->priv_data;
433
3.54k
    ff_av1_packet_uninit(&s->av1_pkt);
434
3.54k
    ff_h2645_packet_uninit(&s->h2645_pkt);
435
3.54k
}
436
437
static const enum AVCodecID codec_ids[] = {
438
    AV_CODEC_ID_AV1,
439
    AV_CODEC_ID_AVS2,
440
    AV_CODEC_ID_AVS3,
441
    AV_CODEC_ID_CAVS,
442
    AV_CODEC_ID_H264,
443
    AV_CODEC_ID_HEVC,
444
    AV_CODEC_ID_MPEG1VIDEO,
445
    AV_CODEC_ID_MPEG2VIDEO,
446
    AV_CODEC_ID_MPEG4,
447
    AV_CODEC_ID_VC1,
448
    AV_CODEC_ID_VVC,
449
    AV_CODEC_ID_NONE,
450
};
451
452
#define OFFSET(x) offsetof(ExtractExtradataContext, x)
453
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
454
static const AVOption options[] = {
455
    { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
456
        { .i64 = 0 }, 0, 1, FLAGS },
457
    { NULL },
458
};
459
460
static const AVClass extract_extradata_class = {
461
    .class_name = "extract_extradata",
462
    .item_name  = av_default_item_name,
463
    .option     = options,
464
    .version    = LIBAVUTIL_VERSION_INT,
465
};
466
467
const FFBitStreamFilter ff_extract_extradata_bsf = {
468
    .p.name         = "extract_extradata",
469
    .p.codec_ids    = codec_ids,
470
    .p.priv_class   = &extract_extradata_class,
471
    .priv_data_size = sizeof(ExtractExtradataContext),
472
    .init           = extract_extradata_init,
473
    .filter         = extract_extradata_filter,
474
    .close          = extract_extradata_close,
475
};