GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/extract_extradata_bsf.c Lines: 157 202 77.7 %
Date: 2020-04-02 05:41:20 Branches: 100 140 71.4 %

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 <stdint.h>
20
21
#include "libavutil/common.h"
22
#include "libavutil/intreadwrite.h"
23
#include "libavutil/log.h"
24
#include "libavutil/opt.h"
25
26
#include "avcodec.h"
27
#include "av1.h"
28
#include "av1_parse.h"
29
#include "bsf.h"
30
#include "bytestream.h"
31
#include "h2645_parse.h"
32
#include "h264.h"
33
#include "hevc.h"
34
#include "vc1_common.h"
35
36
typedef struct ExtractExtradataContext {
37
    const AVClass *class;
38
39
    int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
40
                   uint8_t **data, int *size);
41
42
    /* AV1 specific fields */
43
    AV1Packet av1_pkt;
44
45
    /* H264/HEVC specific fields */
46
    H2645Packet h2645_pkt;
47
48
    /* AVOptions */
49
    int remove;
50
} ExtractExtradataContext;
51
52
4876
static int val_in_array(const int *arr, int len, int val)
53
{
54
    int i;
55
13480
    for (i = 0; i < len; i++)
56
10856
        if (arr[i] == val)
57
2252
            return 1;
58
2624
    return 0;
59
}
60
61
19
static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
62
                                 uint8_t **data, int *size)
63
{
64
    static const int extradata_obu_types[] = {
65
        AV1_OBU_SEQUENCE_HEADER, AV1_OBU_METADATA,
66
    };
67
19
    ExtractExtradataContext *s = ctx->priv_data;
68
69
19
    int extradata_size = 0, filtered_size = 0;
70
19
    int nb_extradata_obu_types = FF_ARRAY_ELEMS(extradata_obu_types);
71
19
    int i, has_seq = 0, ret = 0;
72
73
19
    ret = ff_av1_packet_split(&s->av1_pkt, pkt->data, pkt->size, ctx);
74
19
    if (ret < 0)
75
        return ret;
76
77
95
    for (i = 0; i < s->av1_pkt.nb_obus; i++) {
78
76
        AV1OBU *obu = &s->av1_pkt.obus[i];
79
76
        if (val_in_array(extradata_obu_types, nb_extradata_obu_types, obu->type)) {
80
23
            extradata_size += obu->raw_size;
81
23
            if (obu->type == AV1_OBU_SEQUENCE_HEADER)
82
19
                has_seq = 1;
83
53
        } else if (s->remove) {
84
            filtered_size += obu->raw_size;
85
        }
86
    }
87
88

19
    if (extradata_size && has_seq) {
89
19
        AVBufferRef *filtered_buf = NULL;
90
        PutByteContext pb_filtered_data, pb_extradata;
91
        uint8_t *extradata;
92
93
19
        if (s->remove) {
94
            filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
95
            if (!filtered_buf) {
96
                return AVERROR(ENOMEM);
97
            }
98
            memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
99
        }
100
101
19
        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
102
19
        if (!extradata) {
103
            av_buffer_unref(&filtered_buf);
104
            return AVERROR(ENOMEM);
105
        }
106
19
        memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
107
108
19
        *data = extradata;
109
19
        *size = extradata_size;
110
111
19
        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
112
19
        if (s->remove)
113
            bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size);
114
115
95
        for (i = 0; i < s->av1_pkt.nb_obus; i++) {
116
76
            AV1OBU *obu = &s->av1_pkt.obus[i];
117
76
            if (val_in_array(extradata_obu_types, nb_extradata_obu_types,
118
                             obu->type)) {
119
23
                bytestream2_put_bufferu(&pb_extradata, obu->raw_data, obu->raw_size);
120
53
            } else if (s->remove) {
121
                bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size);
122
            }
123
        }
124
125
19
        if (s->remove) {
126
            av_buffer_unref(&pkt->buf);
127
            pkt->buf  = filtered_buf;
128
            pkt->data = filtered_buf->data;
129
            pkt->size = filtered_size;
130
        }
131
    }
132
133
19
    return 0;
134
}
135
136
455
static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
137
                                   uint8_t **data, int *size)
138
{
139
    static const int extradata_nal_types_hevc[] = {
140
        HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
141
    };
142
    static const int extradata_nal_types_h264[] = {
143
        H264_NAL_SPS, H264_NAL_PPS,
144
    };
145
146
455
    ExtractExtradataContext *s = ctx->priv_data;
147
148
455
    int extradata_size = 0, filtered_size = 0;
149
    const int *extradata_nal_types;
150
    int nb_extradata_nal_types;
151
455
    int i, has_sps = 0, has_vps = 0, ret = 0;
152
153
455
    if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
154
211
        extradata_nal_types    = extradata_nal_types_hevc;
155
211
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
156
    } else {
157
244
        extradata_nal_types    = extradata_nal_types_h264;
158
244
        nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
159
    }
160
161
455
    ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
162
455
                                ctx, 0, 0, ctx->par_in->codec_id, 1, 0);
163
455
    if (ret < 0)
164
        return ret;
165
166
2926
    for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
167
2471
        H2645NAL *nal = &s->h2645_pkt.nals[i];
168
2471
        if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
169
1103
            extradata_size += nal->raw_size + 3;
170
1103
            if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
171
655
                if (nal->type == HEVC_NAL_SPS) has_sps = 1;
172
655
                if (nal->type == HEVC_NAL_VPS) has_vps = 1;
173
            } else {
174
448
                if (nal->type == H264_NAL_SPS) has_sps = 1;
175
            }
176
1368
        } else if (s->remove) {
177
            filtered_size += nal->raw_size + 3;
178
        }
179
    }
180
181
455
    if (extradata_size &&
182

409
        ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
183

220
         (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
184
409
        AVBufferRef *filtered_buf = NULL;
185
        PutByteContext pb_filtered_data, pb_extradata;
186
        uint8_t *extradata;
187
188
409
        if (s->remove) {
189
            filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
190
            if (!filtered_buf) {
191
                return AVERROR(ENOMEM);
192
            }
193
            memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
194
        }
195
196
409
        extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
197
409
        if (!extradata) {
198
            av_buffer_unref(&filtered_buf);
199
            return AVERROR(ENOMEM);
200
        }
201
409
        memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
202
203
409
        *data = extradata;
204
409
        *size = extradata_size;
205
206
409
        bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
207
409
        if (s->remove)
208
            bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size);
209
210
2662
        for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
211
2253
            H2645NAL *nal = &s->h2645_pkt.nals[i];
212
2253
            if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
213
                             nal->type)) {
214
1103
                bytestream2_put_be24u(&pb_extradata, 1); //startcode
215
1103
                bytestream2_put_bufferu(&pb_extradata, nal->raw_data, nal->raw_size);
216
1150
            } else if (s->remove) {
217
                bytestream2_put_be24u(&pb_filtered_data, 1); // startcode
218
                bytestream2_put_bufferu(&pb_filtered_data, nal->raw_data, nal->raw_size);
219
            }
220
        }
221
222
409
        if (s->remove) {
223
            av_buffer_unref(&pkt->buf);
224
            pkt->buf  = filtered_buf;
225
            pkt->data = filtered_buf->data;
226
            pkt->size = filtered_size;
227
        }
228
    }
229
230
455
    return 0;
231
}
232
233
6
static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
234
                                 uint8_t **data, int *size)
235
{
236
6
    ExtractExtradataContext *s = ctx->priv_data;
237
6
    const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
238
6
    uint32_t state = UINT32_MAX;
239
6
    int has_extradata = 0, extradata_size = 0;
240
241
23
    while (ptr < end) {
242
23
        ptr = avpriv_find_start_code(ptr, end, &state);
243

23
        if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
244
17
            has_extradata = 1;
245

6
        } else if (has_extradata && IS_MARKER(state)) {
246
6
            extradata_size = ptr - 4 - pkt->data;
247
6
            break;
248
        }
249
    }
250
251
6
    if (extradata_size) {
252
6
        *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
253
6
        if (!*data)
254
            return AVERROR(ENOMEM);
255
256
6
        memcpy(*data, pkt->data, extradata_size);
257
6
        memset(*data + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
258
6
        *size = extradata_size;
259
260
6
        if (s->remove) {
261
            pkt->data += extradata_size;
262
            pkt->size -= extradata_size;
263
        }
264
    }
265
266
6
    return 0;
267
}
268
269
376
static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt,
270
                                     uint8_t **data, int *size)
271
{
272
376
    ExtractExtradataContext *s = ctx->priv_data;
273
376
    uint32_t state = UINT32_MAX;
274
376
    int i, found = 0;
275
276
2699357
    for (i = 0; i < pkt->size; i++) {
277
2699148
        state = (state << 8) | pkt->data[i];
278
2699148
        if (state == 0x1B3)
279
167
            found = 1;
280


2698981
        else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
281
167
            if (i > 3) {
282
167
                *size = i - 3;
283
167
                *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
284
167
                if (!*data)
285
                    return AVERROR(ENOMEM);
286
287
167
                memcpy(*data, pkt->data, *size);
288
167
                memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
289
290
167
                if (s->remove) {
291
                    pkt->data += *size;
292
                    pkt->size -= *size;
293
                }
294
            }
295
167
            break;
296
        }
297
    }
298
376
    return 0;
299
}
300
301
115
static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt,
302
                                   uint8_t **data, int *size)
303
{
304
115
    ExtractExtradataContext *s = ctx->priv_data;
305
115
    const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
306
115
    uint32_t state = UINT32_MAX;
307
308
374
    while (ptr < end) {
309
374
        ptr = avpriv_find_start_code(ptr, end, &state);
310

374
        if (state == 0x1B3 || state == 0x1B6) {
311
115
            if (ptr - pkt->data > 4) {
312
65
                *size = ptr - 4 - pkt->data;
313
65
                *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
314
65
                if (!*data)
315
                    return AVERROR(ENOMEM);
316
317
65
                memcpy(*data, pkt->data, *size);
318
65
                memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
319
320
65
                if (s->remove) {
321
                    pkt->data += *size;
322
                    pkt->size -= *size;
323
                }
324
            }
325
115
            break;
326
        }
327
    }
328
115
    return 0;
329
}
330
331
static const struct {
332
    enum AVCodecID id;
333
    int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
334
                   uint8_t **data, int *size);
335
} extract_tab[] = {
336
    { AV_CODEC_ID_AV1,        extract_extradata_av1     },
337
    { AV_CODEC_ID_AVS2,       extract_extradata_mpeg4   },
338
    { AV_CODEC_ID_CAVS,       extract_extradata_mpeg4   },
339
    { AV_CODEC_ID_H264,       extract_extradata_h2645   },
340
    { AV_CODEC_ID_HEVC,       extract_extradata_h2645   },
341
    { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12  },
342
    { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12  },
343
    { AV_CODEC_ID_MPEG4,      extract_extradata_mpeg4   },
344
    { AV_CODEC_ID_VC1,        extract_extradata_vc1     },
345
};
346
347
670
static int extract_extradata_init(AVBSFContext *ctx)
348
{
349
670
    ExtractExtradataContext *s = ctx->priv_data;
350
    int i;
351
352
3592
    for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
353
3592
        if (extract_tab[i].id == ctx->par_in->codec_id) {
354
670
            s->extract = extract_tab[i].extract;
355
670
            break;
356
        }
357
    }
358
670
    if (!s->extract)
359
        return AVERROR_BUG;
360
361
670
    return 0;
362
}
363
364
1276
static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
365
{
366
1276
    ExtractExtradataContext *s = ctx->priv_data;
367
1276
    uint8_t *extradata = NULL;
368
    int extradata_size;
369
1276
    int ret = 0;
370
371
1276
    ret = ff_bsf_get_packet_ref(ctx, pkt);
372
1276
    if (ret < 0)
373
305
        return ret;
374
375
971
    ret = s->extract(ctx, pkt, &extradata, &extradata_size);
376
971
    if (ret < 0)
377
        goto fail;
378
379
971
    if (extradata) {
380
666
        ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
381
                                      extradata, extradata_size);
382
666
        if (ret < 0) {
383
            av_freep(&extradata);
384
            goto fail;
385
        }
386
    }
387
388
971
    return 0;
389
390
fail:
391
    av_packet_unref(pkt);
392
    return ret;
393
}
394
395
670
static void extract_extradata_close(AVBSFContext *ctx)
396
{
397
670
    ExtractExtradataContext *s = ctx->priv_data;
398
670
    ff_av1_packet_uninit(&s->av1_pkt);
399
670
    ff_h2645_packet_uninit(&s->h2645_pkt);
400
670
}
401
402
static const enum AVCodecID codec_ids[] = {
403
    AV_CODEC_ID_AV1,
404
    AV_CODEC_ID_AVS2,
405
    AV_CODEC_ID_CAVS,
406
    AV_CODEC_ID_H264,
407
    AV_CODEC_ID_HEVC,
408
    AV_CODEC_ID_MPEG1VIDEO,
409
    AV_CODEC_ID_MPEG2VIDEO,
410
    AV_CODEC_ID_MPEG4,
411
    AV_CODEC_ID_VC1,
412
    AV_CODEC_ID_NONE,
413
};
414
415
#define OFFSET(x) offsetof(ExtractExtradataContext, x)
416
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
417
static const AVOption options[] = {
418
    { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
419
        { .i64 = 0 }, 0, 1, FLAGS },
420
    { NULL },
421
};
422
423
static const AVClass extract_extradata_class = {
424
    .class_name = "extract_extradata",
425
    .item_name  = av_default_item_name,
426
    .option     = options,
427
    .version    = LIBAVUTIL_VERSION_INT,
428
};
429
430
const AVBitStreamFilter ff_extract_extradata_bsf = {
431
    .name           = "extract_extradata",
432
    .codec_ids      = codec_ids,
433
    .priv_data_size = sizeof(ExtractExtradataContext),
434
    .priv_class     = &extract_extradata_class,
435
    .init           = extract_extradata_init,
436
    .filter         = extract_extradata_filter,
437
    .close          = extract_extradata_close,
438
};