FFmpeg coverage


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