FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/av1.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 151 281 53.7%
Functions: 5 8 62.5%
Branches: 59 153 38.6%

Line Branch Exec Source
1 /*
2 * AV1 helper functions for muxers
3 * Copyright (c) 2018 James Almer <jamrial@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/mem.h"
24 #include "libavcodec/av1.h"
25 #include "libavcodec/av1_parse.h"
26 #include "libavcodec/avcodec.h"
27 #include "libavcodec/put_bits.h"
28 #include "av1.h"
29 #include "avio.h"
30 #include "avio_internal.h"
31
32 60 static int av1_filter_obus(AVIOContext *pb, const uint8_t *buf,
33 int size, int *offset)
34 {
35 60 const uint8_t *start = buf, *end = buf + size;
36 int off;
37 enum {
38 START_NOT_FOUND,
39 START_FOUND,
40 END_FOUND,
41 OFFSET_IMPOSSIBLE,
42 60 } state = START_NOT_FOUND;
43
44 60 off = size = 0;
45
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 60 times.
206 while (buf < end) {
46 int64_t obu_size;
47 int start_pos, type, temporal_id, spatial_id;
48 146 int len = parse_obu_header(buf, end - buf, &obu_size, &start_pos,
49 &type, &temporal_id, &spatial_id);
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (len < 0)
51 return len;
52
53
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 86 times.
146 switch (type) {
54 60 case AV1_OBU_TEMPORAL_DELIMITER:
55 case AV1_OBU_REDUNDANT_FRAME_HEADER:
56 case AV1_OBU_TILE_LIST:
57 case AV1_OBU_PADDING:
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (state == START_FOUND)
59 state = END_FOUND;
60 60 break;
61 86 default:
62
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 26 times.
86 if (state == START_NOT_FOUND) {
63 60 off = buf - start;
64 60 state = START_FOUND;
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 } else if (state == END_FOUND) {
66 state = OFFSET_IMPOSSIBLE;
67 }
68
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 40 times.
86 if (pb)
69 46 avio_write(pb, buf, len);
70 86 size += len;
71 86 break;
72 }
73 146 buf += len;
74 }
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (offset)
77 *offset = state != OFFSET_IMPOSSIBLE ? off : -1;
78
79 60 return size;
80 }
81
82 60 int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size)
83 {
84 60 return av1_filter_obus(pb, buf, size, NULL);
85 }
86
87 int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out,
88 int *size, int *offset)
89 {
90 FFIOContext pb;
91 uint8_t *buf;
92 int len, off, ret;
93
94 len = ret = av1_filter_obus(NULL, in, *size, &off);
95 if (ret < 0) {
96 return ret;
97 }
98 if (off >= 0) {
99 *out = (uint8_t *)in;
100 *size = len;
101 *offset = off;
102
103 return 0;
104 }
105
106 buf = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
107 if (!buf)
108 return AVERROR(ENOMEM);
109
110 ffio_init_context(&pb, buf, len, 1, NULL, NULL, NULL, NULL);
111
112 ret = av1_filter_obus(&pb.pub, in, *size, NULL);
113 av_assert1(ret == len);
114
115 memset(buf + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
116
117 *out = buf;
118 *size = len;
119 *offset = 0;
120
121 return 0;
122 }
123
124 static inline void uvlc(GetBitContext *gb)
125 {
126 int leading_zeros = 0;
127
128 while (get_bits_left(gb)) {
129 if (get_bits1(gb))
130 break;
131 leading_zeros++;
132 }
133
134 if (leading_zeros >= 32)
135 return;
136
137 skip_bits_long(gb, leading_zeros);
138 }
139
140 3 static int parse_color_config(AV1SequenceParameters *seq_params, GetBitContext *gb)
141 {
142 3 int twelve_bit = 0;
143 3 int high_bitdepth = get_bits1(gb);
144
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (seq_params->profile == FF_PROFILE_AV1_PROFESSIONAL && high_bitdepth)
145 twelve_bit = get_bits1(gb);
146
147 3 seq_params->bitdepth = 8 + (high_bitdepth * 2) + (twelve_bit * 2);
148
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (seq_params->profile == FF_PROFILE_AV1_HIGH)
150 seq_params->monochrome = 0;
151 else
152 3 seq_params->monochrome = get_bits1(gb);
153
154 3 seq_params->color_description_present_flag = get_bits1(gb);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (seq_params->color_description_present_flag) {
156 seq_params->color_primaries = get_bits(gb, 8);
157 seq_params->transfer_characteristics = get_bits(gb, 8);
158 seq_params->matrix_coefficients = get_bits(gb, 8);
159 } else {
160 3 seq_params->color_primaries = AVCOL_PRI_UNSPECIFIED;
161 3 seq_params->transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
162 3 seq_params->matrix_coefficients = AVCOL_SPC_UNSPECIFIED;
163 }
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (seq_params->monochrome) {
166 seq_params->color_range = get_bits1(gb);
167 seq_params->chroma_subsampling_x = 1;
168 seq_params->chroma_subsampling_y = 1;
169 seq_params->chroma_sample_position = 0;
170 return 0;
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (seq_params->color_primaries == AVCOL_PRI_BT709 &&
172 seq_params->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
173 seq_params->matrix_coefficients == AVCOL_SPC_RGB) {
174 seq_params->chroma_subsampling_x = 0;
175 seq_params->chroma_subsampling_y = 0;
176 } else {
177 3 seq_params->color_range = get_bits1(gb);
178
179
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (seq_params->profile == FF_PROFILE_AV1_MAIN) {
180 3 seq_params->chroma_subsampling_x = 1;
181 3 seq_params->chroma_subsampling_y = 1;
182 } else if (seq_params->profile == FF_PROFILE_AV1_HIGH) {
183 seq_params->chroma_subsampling_x = 0;
184 seq_params->chroma_subsampling_y = 0;
185 } else {
186 if (twelve_bit) {
187 seq_params->chroma_subsampling_x = get_bits1(gb);
188 if (seq_params->chroma_subsampling_x)
189 seq_params->chroma_subsampling_y = get_bits1(gb);
190 else
191 seq_params->chroma_subsampling_y = 0;
192 } else {
193 seq_params->chroma_subsampling_x = 1;
194 seq_params->chroma_subsampling_y = 0;
195 }
196 }
197
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (seq_params->chroma_subsampling_x && seq_params->chroma_subsampling_y)
198 3 seq_params->chroma_sample_position = get_bits(gb, 2);
199 }
200
201 3 skip_bits1(gb); // separate_uv_delta_q
202
203 3 return 0;
204 }
205
206 3 static int parse_sequence_header(AV1SequenceParameters *seq_params, const uint8_t *buf, int size)
207 {
208 GetBitContext gb;
209 int reduced_still_picture_header;
210 int frame_width_bits_minus_1, frame_height_bits_minus_1;
211 int size_bits, ret;
212
213 3 size_bits = get_obu_bit_length(buf, size, AV1_OBU_SEQUENCE_HEADER);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (size_bits < 0)
215 return size_bits;
216
217 3 ret = init_get_bits(&gb, buf, size_bits);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
219 return ret;
220
221 3 memset(seq_params, 0, sizeof(*seq_params));
222
223 3 seq_params->profile = get_bits(&gb, 3);
224
225 3 skip_bits1(&gb); // still_picture
226 3 reduced_still_picture_header = get_bits1(&gb);
227
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (reduced_still_picture_header) {
229 seq_params->level = get_bits(&gb, 5);
230 seq_params->tier = 0;
231 } else {
232 int initial_display_delay_present_flag, operating_points_cnt_minus_1;
233 int decoder_model_info_present_flag, buffer_delay_length_minus_1;
234
235
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (get_bits1(&gb)) { // timing_info_present_flag
236 1 skip_bits_long(&gb, 32); // num_units_in_display_tick
237 1 skip_bits_long(&gb, 32); // time_scale
238
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (get_bits1(&gb)) // equal_picture_interval
240 uvlc(&gb); // num_ticks_per_picture_minus_1
241
242 1 decoder_model_info_present_flag = get_bits1(&gb);
243
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (decoder_model_info_present_flag) {
244 1 buffer_delay_length_minus_1 = get_bits(&gb, 5);
245 1 skip_bits_long(&gb, 32); // num_units_in_decoding_tick
246 1 skip_bits(&gb, 10); // buffer_removal_time_length_minus_1 (5)
247 // frame_presentation_time_length_minus_1 (5)
248 }
249 } else
250 2 decoder_model_info_present_flag = 0;
251
252 3 initial_display_delay_present_flag = get_bits1(&gb);
253
254 3 operating_points_cnt_minus_1 = get_bits(&gb, 5);
255
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (int i = 0; i <= operating_points_cnt_minus_1; i++) {
256 int seq_level_idx, seq_tier;
257
258 3 skip_bits(&gb, 12); // operating_point_idc
259 3 seq_level_idx = get_bits(&gb, 5);
260
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (seq_level_idx > 7)
262 seq_tier = get_bits1(&gb);
263 else
264 3 seq_tier = 0;
265
266
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (decoder_model_info_present_flag) {
267
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (get_bits1(&gb)) { // decoder_model_present_for_this_op
268 1 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // decoder_buffer_delay
269 1 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // encoder_buffer_delay
270 1 skip_bits1(&gb); // low_delay_mode_flag
271 }
272 }
273
274
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (initial_display_delay_present_flag) {
275
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (get_bits1(&gb)) // initial_display_delay_present_for_this_op
276 1 skip_bits(&gb, 4); // initial_display_delay_minus_1
277 }
278
279
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (i == 0) {
280 3 seq_params->level = seq_level_idx;
281 3 seq_params->tier = seq_tier;
282 }
283 }
284 }
285
286 3 frame_width_bits_minus_1 = get_bits(&gb, 4);
287 3 frame_height_bits_minus_1 = get_bits(&gb, 4);
288
289 3 skip_bits(&gb, frame_width_bits_minus_1 + 1); // max_frame_width_minus_1
290 3 skip_bits(&gb, frame_height_bits_minus_1 + 1); // max_frame_height_minus_1
291
292
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!reduced_still_picture_header) {
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits1(&gb)) // frame_id_numbers_present_flag
294 skip_bits(&gb, 7); // delta_frame_id_length_minus_2 (4), additional_frame_id_length_minus_1 (3)
295 }
296
297 3 skip_bits(&gb, 3); // use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1)
298
299
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!reduced_still_picture_header) {
300 int enable_order_hint, seq_force_screen_content_tools;
301
302 3 skip_bits(&gb, 4); // enable_interintra_compound (1), enable_masked_compound (1)
303 // enable_warped_motion (1), enable_dual_filter (1)
304
305 3 enable_order_hint = get_bits1(&gb);
306
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (enable_order_hint)
307 3 skip_bits(&gb, 2); // enable_jnt_comp (1), enable_ref_frame_mvs (1)
308
309
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (get_bits1(&gb)) // seq_choose_screen_content_tools
310 3 seq_force_screen_content_tools = 2;
311 else
312 seq_force_screen_content_tools = get_bits1(&gb);
313
314
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (seq_force_screen_content_tools) {
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!get_bits1(&gb)) // seq_choose_integer_mv
316 skip_bits1(&gb); // seq_force_integer_mv
317 }
318
319
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (enable_order_hint)
320 3 skip_bits(&gb, 3); // order_hint_bits_minus_1
321 }
322
323 3 skip_bits(&gb, 3); // enable_superres (1), enable_cdef (1), enable_restoration (1)
324
325 3 parse_color_config(seq_params, &gb);
326
327 3 skip_bits1(&gb); // film_grain_params_present
328
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits_left(&gb))
330 return AVERROR_INVALIDDATA;
331
332 3 return 0;
333 }
334
335 int ff_av1_parse_seq_header(AV1SequenceParameters *seq, const uint8_t *buf, int size)
336 {
337 int is_av1c;
338
339 if (size <= 0)
340 return AVERROR_INVALIDDATA;
341
342 is_av1c = !!(buf[0] & 0x80);
343 if (is_av1c) {
344 GetBitContext gb;
345 int ret, version = buf[0] & 0x7F;
346
347 if (version != 1 || size < 4)
348 return AVERROR_INVALIDDATA;
349
350 ret = init_get_bits8(&gb, buf, 4);
351 if (ret < 0)
352 return ret;
353
354 memset(seq, 0, sizeof(*seq));
355
356 skip_bits(&gb, 8);
357 seq->profile = get_bits(&gb, 3);
358 seq->level = get_bits(&gb, 5);
359 seq->tier = get_bits(&gb, 1);
360 seq->bitdepth = get_bits(&gb, 1) * 2 + 8;
361 seq->bitdepth += get_bits(&gb, 1) * 2;
362 seq->monochrome = get_bits(&gb, 1);
363 seq->chroma_subsampling_x = get_bits(&gb, 1);
364 seq->chroma_subsampling_y = get_bits(&gb, 1);
365 seq->chroma_sample_position = get_bits(&gb, 2);
366 seq->color_primaries = AVCOL_PRI_UNSPECIFIED;
367 seq->transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
368 seq->matrix_coefficients = AVCOL_SPC_UNSPECIFIED;
369
370 size -= 4;
371 buf += 4;
372 }
373
374 while (size > 0) {
375 int64_t obu_size;
376 int start_pos, type, temporal_id, spatial_id;
377 int len = parse_obu_header(buf, size, &obu_size, &start_pos,
378 &type, &temporal_id, &spatial_id);
379 if (len < 0)
380 return len;
381
382 switch (type) {
383 case AV1_OBU_SEQUENCE_HEADER:
384 if (!obu_size)
385 return AVERROR_INVALIDDATA;
386
387 return parse_sequence_header(seq, buf + start_pos, obu_size);
388 default:
389 break;
390 }
391 size -= len;
392 buf += len;
393 }
394
395 return is_av1c ? 0 : AVERROR_INVALIDDATA;
396 }
397
398 3 int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
399 int write_seq_header)
400 {
401 AVIOContext *meta_pb;
402 AV1SequenceParameters seq_params;
403 PutBitContext pbc;
404 uint8_t header[4], *meta;
405 const uint8_t *seq;
406 3 int ret, nb_seq = 0, seq_size, meta_size;
407
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (size <= 0)
409 return AVERROR_INVALIDDATA;
410
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (buf[0] & 0x80) {
412 // first bit is nonzero, the passed data does not consist purely of
413 // OBUs. Expect that the data is already in AV1CodecConfigurationRecord
414 // format.
415 int config_record_version = buf[0] & 0x7f;
416 if (config_record_version != 1 || size < 4) {
417 return AVERROR_INVALIDDATA;
418 }
419
420 avio_write(pb, buf, size);
421
422 return 0;
423 }
424
425 3 ret = avio_open_dyn_buf(&meta_pb);
426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
427 return ret;
428
429
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 while (size > 0) {
430 int64_t obu_size;
431 int start_pos, type, temporal_id, spatial_id;
432 3 int len = parse_obu_header(buf, size, &obu_size, &start_pos,
433 &type, &temporal_id, &spatial_id);
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (len < 0) {
435 ret = len;
436 goto fail;
437 }
438
439
1/3
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
3 switch (type) {
440 3 case AV1_OBU_SEQUENCE_HEADER:
441 3 nb_seq++;
442
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (!obu_size || nb_seq > 1) {
443 ret = AVERROR_INVALIDDATA;
444 goto fail;
445 }
446 3 ret = parse_sequence_header(&seq_params, buf + start_pos, obu_size);
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
448 goto fail;
449
450 3 seq = buf;
451 3 seq_size = len;
452 3 break;
453 case AV1_OBU_METADATA:
454 if (!obu_size) {
455 ret = AVERROR_INVALIDDATA;
456 goto fail;
457 }
458 avio_write(meta_pb, buf, len);
459 break;
460 default:
461 break;
462 }
463 3 size -= len;
464 3 buf += len;
465 }
466
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!nb_seq) {
468 ret = AVERROR_INVALIDDATA;
469 goto fail;
470 }
471
472 3 init_put_bits(&pbc, header, sizeof(header));
473
474 3 put_bits(&pbc, 1, 1); // marker
475 3 put_bits(&pbc, 7, 1); // version
476 3 put_bits(&pbc, 3, seq_params.profile);
477 3 put_bits(&pbc, 5, seq_params.level);
478 3 put_bits(&pbc, 1, seq_params.tier);
479 3 put_bits(&pbc, 1, seq_params.bitdepth > 8);
480 3 put_bits(&pbc, 1, seq_params.bitdepth == 12);
481 3 put_bits(&pbc, 1, seq_params.monochrome);
482 3 put_bits(&pbc, 1, seq_params.chroma_subsampling_x);
483 3 put_bits(&pbc, 1, seq_params.chroma_subsampling_y);
484 3 put_bits(&pbc, 2, seq_params.chroma_sample_position);
485 3 put_bits(&pbc, 8, 0); // padding
486 3 flush_put_bits(&pbc);
487
488 3 avio_write(pb, header, sizeof(header));
489
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (write_seq_header) {
490 3 avio_write(pb, seq, seq_size);
491 }
492
493 3 meta_size = avio_get_dyn_buf(meta_pb, &meta);
494
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (meta_size)
495 avio_write(pb, meta, meta_size);
496
497 3 fail:
498 3 ffio_free_dyn_buf(&meta_pb);
499
500 3 return ret;
501 }
502