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
31
34 {
35 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 }
state = START_NOT_FOUND;
43
45 while (buf < end) {
46 int64_t obu_size;
47 int start_pos,
type, temporal_id, spatial_id;
49 &
type, &temporal_id, &spatial_id);
52
58 if (
state == START_FOUND)
60 break;
61 default:
62 if (
state == START_NOT_FOUND) {
63 off = buf - start;
65 }
else if (
state == END_FOUND) {
66 state = OFFSET_IMPOSSIBLE;
67 }
68 if (pb)
71 break;
72 }
74 }
75
78
80 }
81
83 {
85 }
86
89 {
91 uint8_t *buf;
93
97 }
98 if (off >= 0) {
102
103 return 0;
104 }
105
107 if (!buf)
109
111
114
116
120
121 return 0;
122 }
123
125 {
126 int leading_zeros = 0;
127
130 break;
131 leading_zeros++;
132 }
133
134 if (leading_zeros >= 32)
135 return;
136
138 }
139
141 {
142 int twelve_bit = 0;
146
147 seq_params->
bitdepth = 8 + (high_bitdepth * 2) + (twelve_bit * 2);
148
151 else
153
159 } else {
163 }
164
170 return 0;
176 } else {
178
185 } else {
186 if (twelve_bit) {
190 else
192 } else {
195 }
196 }
199 }
200
202
203 return 0;
204 }
205
207 {
209 int reduced_still_picture_header;
210 int frame_width_bits_minus_1, frame_height_bits_minus_1;
212
214 if (size_bits < 0)
215 return size_bits;
216
220
221 memset(seq_params, 0, sizeof(*seq_params));
222
224
226 reduced_still_picture_header =
get_bits1(&gb);
227
228 if (reduced_still_picture_header) {
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 if (
get_bits1(&gb)) {
// timing_info_present_flag
238
239 if (
get_bits1(&gb))
// equal_picture_interval
240 uvlc(&gb);
// num_ticks_per_picture_minus_1
241
242 decoder_model_info_present_flag =
get_bits1(&gb);
243 if (decoder_model_info_present_flag) {
244 buffer_delay_length_minus_1 =
get_bits(&gb, 5);
246 skip_bits(&gb, 10);
// buffer_removal_time_length_minus_1 (5)
247 // frame_presentation_time_length_minus_1 (5)
248 }
249 } else
250 decoder_model_info_present_flag = 0;
251
252 initial_display_delay_present_flag =
get_bits1(&gb);
253
254 operating_points_cnt_minus_1 =
get_bits(&gb, 5);
255 for (
int i = 0;
i <= operating_points_cnt_minus_1;
i++) {
256 int seq_level_idx, seq_tier;
257
258 skip_bits(&gb, 12);
// operating_point_idc
260
261 if (seq_level_idx > 7)
263 else
264 seq_tier = 0;
265
266 if (decoder_model_info_present_flag) {
267 if (
get_bits1(&gb)) {
// decoder_model_present_for_this_op
268 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1);
// decoder_buffer_delay
269 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1);
// encoder_buffer_delay
271 }
272 }
273
274 if (initial_display_delay_present_flag) {
275 if (
get_bits1(&gb))
// initial_display_delay_present_for_this_op
276 skip_bits(&gb, 4);
// initial_display_delay_minus_1
277 }
278
280 seq_params->
level = seq_level_idx;
281 seq_params->
tier = seq_tier;
282 }
283 }
284 }
285
286 frame_width_bits_minus_1 =
get_bits(&gb, 4);
287 frame_height_bits_minus_1 =
get_bits(&gb, 4);
288
289 skip_bits(&gb, frame_width_bits_minus_1 + 1);
// max_frame_width_minus_1
290 skip_bits(&gb, frame_height_bits_minus_1 + 1);
// max_frame_height_minus_1
291
292 if (!reduced_still_picture_header) {
293 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 skip_bits(&gb, 3);
// use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1)
298
299 if (!reduced_still_picture_header) {
300 int enable_order_hint, seq_force_screen_content_tools;
301
302 skip_bits(&gb, 4);
// enable_interintra_compound (1), enable_masked_compound (1)
303 // enable_warped_motion (1), enable_dual_filter (1)
304
306 if (enable_order_hint)
307 skip_bits(&gb, 2);
// enable_jnt_comp (1), enable_ref_frame_mvs (1)
308
309 if (
get_bits1(&gb))
// seq_choose_screen_content_tools
310 seq_force_screen_content_tools = 2;
311 else
312 seq_force_screen_content_tools =
get_bits1(&gb);
313
314 if (seq_force_screen_content_tools) {
315 if (!
get_bits1(&gb))
// seq_choose_integer_mv
317 }
318
319 if (enable_order_hint)
320 skip_bits(&gb, 3);
// order_hint_bits_minus_1
321 }
322
323 skip_bits(&gb, 3);
// enable_superres (1), enable_cdef (1), enable_restoration (1)
324
326
328
331
332 return 0;
333 }
334
336 {
337 int is_av1c;
338
341
342 is_av1c = !!(buf[0] & 0x80);
343 if (is_av1c) {
346
349
353
354 memset(seq, 0, sizeof(*seq));
355
369
371 buf += 4;
372 }
373
375 int64_t obu_size;
376 int start_pos,
type, temporal_id, spatial_id;
378 &
type, &temporal_id, &spatial_id);
381
384 if (!obu_size)
386
388 default:
389 break;
390 }
393 }
394
396 }
397
399 int write_seq_header)
400 {
405 const uint8_t *seq;
406 int ret, nb_seq = 0, seq_size, meta_size;
407
410
411 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) {
418 }
419
421
422 return 0;
423 }
424
428
430 int64_t obu_size;
431 int start_pos,
type, temporal_id, spatial_id;
433 &
type, &temporal_id, &spatial_id);
437 }
438
441 nb_seq++;
442 if (!obu_size || nb_seq > 1) {
445 }
449
450 seq = buf;
452 break;
454 if (!obu_size) {
457 }
459 break;
460 default:
461 break;
462 }
465 }
466
467 if (!nb_seq) {
470 }
471
473
487
489 if (write_seq_header) {
491 }
492
494 if (meta_size)
496
499
501 }