FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/extract_extradata_bsf.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 152 197 77.2%
Branches: 99 138 71.7%

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 "av1.h"
27 #include "av1_parse.h"
28 #include "bsf.h"
29 #include "bsf_internal.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 4984 static int val_in_array(const int *arr, int len, int val)
53 {
54 int i;
55
2/2
✓ Branch 0 taken 11094 times.
✓ Branch 1 taken 2716 times.
13810 for (i = 0; i < len; i++)
56
2/2
✓ Branch 0 taken 2268 times.
✓ Branch 1 taken 8826 times.
11094 if (arr[i] == val)
57 2268 return 1;
58 2716 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
75 return ret;
76
77
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 19 times.
95 for (i = 0; i < s->av1_pkt.nb_obus; i++) {
78 76 AV1OBU *obu = &s->av1_pkt.obus[i];
79
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 53 times.
76 if (val_in_array(extradata_obu_types, nb_extradata_obu_types, obu->type)) {
80 23 extradata_size += obu->raw_size;
81
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 4 times.
23 if (obu->type == AV1_OBU_SEQUENCE_HEADER)
82 19 has_seq = 1;
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 } else if (s->remove) {
84 filtered_size += obu->raw_size;
85 }
86 }
87
88
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!extradata) {
103 av_buffer_unref(&filtered_buf);
104 return AVERROR(ENOMEM);
105 }
106
107 19 *data = extradata;
108 19 *size = extradata_size;
109
110 19 bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (s->remove)
112 bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size);
113
114
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 19 times.
95 for (i = 0; i < s->av1_pkt.nb_obus; i++) {
115 76 AV1OBU *obu = &s->av1_pkt.obus[i];
116
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 53 times.
76 if (val_in_array(extradata_obu_types, nb_extradata_obu_types,
117 obu->type)) {
118 23 bytestream2_put_bufferu(&pb_extradata, obu->raw_data, obu->raw_size);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 } else if (s->remove) {
120 bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size);
121 }
122 }
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (s->remove) {
125 av_buffer_unref(&pkt->buf);
126 pkt->buf = filtered_buf;
127 pkt->data = filtered_buf->data;
128 pkt->size = filtered_size;
129 }
130 }
131
132 19 return 0;
133 }
134
135 470 static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
136 uint8_t **data, int *size)
137 {
138 static const int extradata_nal_types_hevc[] = {
139 HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
140 };
141 static const int extradata_nal_types_h264[] = {
142 H264_NAL_SPS, H264_NAL_PPS,
143 };
144
145 470 ExtractExtradataContext *s = ctx->priv_data;
146
147 470 int extradata_size = 0, filtered_size = 0;
148 const int *extradata_nal_types;
149 int nb_extradata_nal_types;
150 470 int i, has_sps = 0, has_vps = 0, ret = 0;
151
152
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 257 times.
470 if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
153 213 extradata_nal_types = extradata_nal_types_hevc;
154 213 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
155 } else {
156 257 extradata_nal_types = extradata_nal_types_h264;
157 257 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
158 }
159
160 470 ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
161 470 ctx, 0, 0, ctx->par_in->codec_id, 1, 0);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470 times.
470 if (ret < 0)
163 return ret;
164
165
2/2
✓ Branch 0 taken 2552 times.
✓ Branch 1 taken 470 times.
3022 for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
166 2552 H2645NAL *nal = &s->h2645_pkt.nals[i];
167
2/2
✓ Branch 1 taken 1111 times.
✓ Branch 2 taken 1441 times.
2552 if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
168 1111 extradata_size += nal->raw_size + 3;
169
2/2
✓ Branch 0 taken 661 times.
✓ Branch 1 taken 450 times.
1111 if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
170
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 462 times.
661 if (nal->type == HEVC_NAL_SPS) has_sps = 1;
171
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 461 times.
661 if (nal->type == HEVC_NAL_VPS) has_vps = 1;
172 } else {
173
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 229 times.
450 if (nal->type == H264_NAL_SPS) has_sps = 1;
174 }
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 } else if (s->remove) {
176 filtered_size += nal->raw_size + 3;
177 }
178 }
179
180
2/2
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 58 times.
470 if (extradata_size &&
181
4/6
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 191 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 191 times.
412 ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
182
2/4
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 221 times.
✗ Branch 3 not taken.
221 (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
183 412 AVBufferRef *filtered_buf = NULL;
184 PutByteContext pb_filtered_data, pb_extradata;
185 uint8_t *extradata;
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (s->remove) {
188 filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
189 if (!filtered_buf) {
190 return AVERROR(ENOMEM);
191 }
192 memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
193 }
194
195 412 extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (!extradata) {
197 av_buffer_unref(&filtered_buf);
198 return AVERROR(ENOMEM);
199 }
200
201 412 *data = extradata;
202 412 *size = extradata_size;
203
204 412 bytestream2_init_writer(&pb_extradata, extradata, extradata_size);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (s->remove)
206 bytestream2_init_writer(&pb_filtered_data, filtered_buf->data, filtered_size);
207
208
2/2
✓ Branch 0 taken 2280 times.
✓ Branch 1 taken 412 times.
2692 for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
209 2280 H2645NAL *nal = &s->h2645_pkt.nals[i];
210
2/2
✓ Branch 1 taken 1111 times.
✓ Branch 2 taken 1169 times.
2280 if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
211 nal->type)) {
212 1111 bytestream2_put_be24u(&pb_extradata, 1); //startcode
213 1111 bytestream2_put_bufferu(&pb_extradata, nal->raw_data, nal->raw_size);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1169 times.
1169 } else if (s->remove) {
215 bytestream2_put_be24u(&pb_filtered_data, 1); // startcode
216 bytestream2_put_bufferu(&pb_filtered_data, nal->raw_data, nal->raw_size);
217 }
218 }
219
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (s->remove) {
221 av_buffer_unref(&pkt->buf);
222 pkt->buf = filtered_buf;
223 pkt->data = filtered_buf->data;
224 pkt->size = filtered_size;
225 }
226 }
227
228 470 return 0;
229 }
230
231 6 static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
232 uint8_t **data, int *size)
233 {
234 6 ExtractExtradataContext *s = ctx->priv_data;
235 6 const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
236 6 uint32_t state = UINT32_MAX;
237 6 int has_extradata = 0, extradata_size = 0;
238
239
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 while (ptr < end) {
240 23 ptr = avpriv_find_start_code(ptr, end, &state);
241
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 6 times.
23 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
242 17 has_extradata = 1;
243
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 } else if (has_extradata && IS_MARKER(state)) {
244 6 extradata_size = ptr - 4 - pkt->data;
245 6 break;
246 }
247 }
248
249
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (extradata_size) {
250 6 *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!*data)
252 return AVERROR(ENOMEM);
253
254 6 memcpy(*data, pkt->data, extradata_size);
255 6 *size = extradata_size;
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->remove) {
258 pkt->data += extradata_size;
259 pkt->size -= extradata_size;
260 }
261 }
262
263 6 return 0;
264 }
265
266 361 static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt,
267 uint8_t **data, int *size)
268 {
269 361 ExtractExtradataContext *s = ctx->priv_data;
270 361 uint32_t state = UINT32_MAX;
271 361 int i, found = 0;
272
273
2/2
✓ Branch 0 taken 2689803 times.
✓ Branch 1 taken 196 times.
2689999 for (i = 0; i < pkt->size; i++) {
274 2689803 state = (state << 8) | pkt->data[i];
275
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 2689638 times.
2689803 if (state == 0x1B3)
276 165 found = 1;
277
8/8
✓ Branch 0 taken 4640 times.
✓ Branch 1 taken 2684998 times.
✓ Branch 2 taken 4478 times.
✓ Branch 3 taken 162 times.
✓ Branch 4 taken 389 times.
✓ Branch 5 taken 4089 times.
✓ Branch 6 taken 165 times.
✓ Branch 7 taken 224 times.
2689638 else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
278 165 *size = i - 3;
279 165 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (!*data)
281 return AVERROR(ENOMEM);
282
283 165 memcpy(*data, pkt->data, *size);
284
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (s->remove) {
286 pkt->data += *size;
287 pkt->size -= *size;
288 }
289 165 break;
290 }
291 }
292 361 return 0;
293 }
294
295 118 static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt,
296 uint8_t **data, int *size)
297 {
298 118 ExtractExtradataContext *s = ctx->priv_data;
299 118 const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
300 118 uint32_t state = UINT32_MAX;
301
302
1/2
✓ Branch 0 taken 389 times.
✗ Branch 1 not taken.
389 while (ptr < end) {
303 389 ptr = avpriv_find_start_code(ptr, end, &state);
304
4/4
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 271 times.
389 if (state == 0x1B3 || state == 0x1B6) {
305
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 50 times.
118 if (ptr - pkt->data > 4) {
306 68 *size = ptr - 4 - pkt->data;
307 68 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!*data)
309 return AVERROR(ENOMEM);
310
311 68 memcpy(*data, pkt->data, *size);
312
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (s->remove) {
314 pkt->data += *size;
315 pkt->size -= *size;
316 }
317 }
318 118 break;
319 }
320 }
321 118 return 0;
322 }
323
324 static const struct {
325 enum AVCodecID id;
326 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
327 uint8_t **data, int *size);
328 } extract_tab[] = {
329 { AV_CODEC_ID_AV1, extract_extradata_av1 },
330 { AV_CODEC_ID_AVS2, extract_extradata_mpeg4 },
331 { AV_CODEC_ID_CAVS, extract_extradata_mpeg4 },
332 { AV_CODEC_ID_H264, extract_extradata_h2645 },
333 { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
334 { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12 },
335 { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12 },
336 { AV_CODEC_ID_MPEG4, extract_extradata_mpeg4 },
337 { AV_CODEC_ID_VC1, extract_extradata_vc1 },
338 };
339
340 676 static int extract_extradata_init(AVBSFContext *ctx)
341 {
342 676 ExtractExtradataContext *s = ctx->priv_data;
343 int i;
344
345
1/2
✓ Branch 0 taken 3630 times.
✗ Branch 1 not taken.
3630 for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
346
2/2
✓ Branch 0 taken 676 times.
✓ Branch 1 taken 2954 times.
3630 if (extract_tab[i].id == ctx->par_in->codec_id) {
347 676 s->extract = extract_tab[i].extract;
348 676 break;
349 }
350 }
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 676 times.
676 if (!s->extract)
352 return AVERROR_BUG;
353
354 676 return 0;
355 }
356
357 1278 static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
358 {
359 1278 ExtractExtradataContext *s = ctx->priv_data;
360 1278 uint8_t *extradata = NULL;
361 int extradata_size;
362 1278 int ret = 0;
363
364 1278 ret = ff_bsf_get_packet_ref(ctx, pkt);
365
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 974 times.
1278 if (ret < 0)
366 304 return ret;
367
368 974 ret = s->extract(ctx, pkt, &extradata, &extradata_size);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 974 times.
974 if (ret < 0)
370 goto fail;
371
372
2/2
✓ Branch 0 taken 670 times.
✓ Branch 1 taken 304 times.
974 if (extradata) {
373 670 memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
374 670 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
375 extradata, extradata_size);
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 if (ret < 0) {
377 av_freep(&extradata);
378 goto fail;
379 }
380 }
381
382 974 return 0;
383
384 fail:
385 av_packet_unref(pkt);
386 return ret;
387 }
388
389 676 static void extract_extradata_close(AVBSFContext *ctx)
390 {
391 676 ExtractExtradataContext *s = ctx->priv_data;
392 676 ff_av1_packet_uninit(&s->av1_pkt);
393 676 ff_h2645_packet_uninit(&s->h2645_pkt);
394 676 }
395
396 static const enum AVCodecID codec_ids[] = {
397 AV_CODEC_ID_AV1,
398 AV_CODEC_ID_AVS2,
399 AV_CODEC_ID_CAVS,
400 AV_CODEC_ID_H264,
401 AV_CODEC_ID_HEVC,
402 AV_CODEC_ID_MPEG1VIDEO,
403 AV_CODEC_ID_MPEG2VIDEO,
404 AV_CODEC_ID_MPEG4,
405 AV_CODEC_ID_VC1,
406 AV_CODEC_ID_NONE,
407 };
408
409 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
410 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
411 static const AVOption options[] = {
412 { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
413 { .i64 = 0 }, 0, 1, FLAGS },
414 { NULL },
415 };
416
417 static const AVClass extract_extradata_class = {
418 .class_name = "extract_extradata",
419 .item_name = av_default_item_name,
420 .option = options,
421 .version = LIBAVUTIL_VERSION_INT,
422 };
423
424 const AVBitStreamFilter ff_extract_extradata_bsf = {
425 .name = "extract_extradata",
426 .codec_ids = codec_ids,
427 .priv_data_size = sizeof(ExtractExtradataContext),
428 .priv_class = &extract_extradata_class,
429 .init = extract_extradata_init,
430 .filter = extract_extradata_filter,
431 .close = extract_extradata_close,
432 };
433