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
32
35 {
36 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 }
state = START_NOT_FOUND;
44
46 while (buf < end) {
48 int start_pos,
type, temporal_id, spatial_id;
50 &
type, &temporal_id, &spatial_id);
53
59 if (
state == START_FOUND)
61 break;
62 default:
63 if (
state == START_NOT_FOUND) {
64 off = buf - start;
66 }
else if (
state == END_FOUND) {
67 state = OFFSET_IMPOSSIBLE;
68 }
69 if (pb)
72 break;
73 }
75 }
76
79
81 }
82
84 {
86 }
87
90 {
92 uint8_t *buf;
94
98 }
99 if (off >= 0) {
100 *
out = (uint8_t *)in;
103
104 return 0;
105 }
106
108 if (!buf)
110
112
115
117
121
122 return 0;
123 }
124
126 {
127 int leading_zeros = 0;
128
131 break;
132 leading_zeros++;
133 }
134
135 if (leading_zeros >= 32)
136 return;
137
139 }
140
142 {
143 int twelve_bit = 0;
147
148 seq_params->
bitdepth = 8 + (high_bitdepth * 2) + (twelve_bit * 2);
149
152 else
154
160 } else {
164 }
165
171 return 0;
177 } else {
179
186 } else {
187 if (twelve_bit) {
191 else
193 } else {
196 }
197 }
200 }
201
203
204 return 0;
205 }
206
208 {
210 int reduced_still_picture_header;
211 int frame_width_bits_minus_1, frame_height_bits_minus_1;
213
215 if (size_bits < 0)
216 return size_bits;
217
221
222 memset(seq_params, 0, sizeof(*seq_params));
223
225
227 reduced_still_picture_header =
get_bits1(&gb);
228
229 if (reduced_still_picture_header) {
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 if (
get_bits1(&gb)) {
// timing_info_present_flag
239
240 if (
get_bits1(&gb))
// equal_picture_interval
241 uvlc(&gb);
// num_ticks_per_picture_minus_1
242
243 decoder_model_info_present_flag =
get_bits1(&gb);
244 if (decoder_model_info_present_flag) {
245 buffer_delay_length_minus_1 =
get_bits(&gb, 5);
247 skip_bits(&gb, 10);
// buffer_removal_time_length_minus_1 (5)
248 // frame_presentation_time_length_minus_1 (5)
249 }
250 } else
251 decoder_model_info_present_flag = 0;
252
253 initial_display_delay_present_flag =
get_bits1(&gb);
254
255 operating_points_cnt_minus_1 =
get_bits(&gb, 5);
256 for (
int i = 0;
i <= operating_points_cnt_minus_1;
i++) {
257 int seq_level_idx, seq_tier;
258
259 skip_bits(&gb, 12);
// operating_point_idc
261
262 if (seq_level_idx > 7)
264 else
265 seq_tier = 0;
266
267 if (decoder_model_info_present_flag) {
268 if (
get_bits1(&gb)) {
// decoder_model_present_for_this_op
269 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1);
// decoder_buffer_delay
270 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1);
// encoder_buffer_delay
272 }
273 }
274
275 if (initial_display_delay_present_flag) {
276 if (
get_bits1(&gb))
// initial_display_delay_present_for_this_op
277 skip_bits(&gb, 4);
// initial_display_delay_minus_1
278 }
279
281 seq_params->
level = seq_level_idx;
282 seq_params->
tier = seq_tier;
283 }
284 }
285 }
286
287 frame_width_bits_minus_1 =
get_bits(&gb, 4);
288 frame_height_bits_minus_1 =
get_bits(&gb, 4);
289
290 skip_bits(&gb, frame_width_bits_minus_1 + 1);
// max_frame_width_minus_1
291 skip_bits(&gb, frame_height_bits_minus_1 + 1);
// max_frame_height_minus_1
292
293 if (!reduced_still_picture_header) {
294 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 skip_bits(&gb, 3);
// use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1)
299
300 if (!reduced_still_picture_header) {
301 int enable_order_hint, seq_force_screen_content_tools;
302
303 skip_bits(&gb, 4);
// enable_interintra_compound (1), enable_masked_compound (1)
304 // enable_warped_motion (1), enable_dual_filter (1)
305
307 if (enable_order_hint)
308 skip_bits(&gb, 2);
// enable_jnt_comp (1), enable_ref_frame_mvs (1)
309
310 if (
get_bits1(&gb))
// seq_choose_screen_content_tools
311 seq_force_screen_content_tools = 2;
312 else
313 seq_force_screen_content_tools =
get_bits1(&gb);
314
315 if (seq_force_screen_content_tools) {
316 if (!
get_bits1(&gb))
// seq_choose_integer_mv
318 }
319
320 if (enable_order_hint)
321 skip_bits(&gb, 3);
// order_hint_bits_minus_1
322 }
323
324 skip_bits(&gb, 3);
// enable_superres (1), enable_cdef (1), enable_restoration (1)
325
327
329
332
333 return 0;
334 }
335
337 {
338 int is_av1c;
339
342
343 is_av1c = !!(buf[0] & 0x80);
344 if (is_av1c) {
347
350
354
355 memset(seq, 0, sizeof(*seq));
356
370
372 buf += 4;
373 }
374
377 int start_pos,
type, temporal_id, spatial_id;
379 &
type, &temporal_id, &spatial_id);
382
385 if (!obu_size)
387
389 default:
390 break;
391 }
394 }
395
397 }
398
400 int write_seq_header)
401 {
406 const uint8_t *seq;
407 int ret, nb_seq = 0, seq_size, meta_size;
408
411
412 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) {
419 }
420
422
423 return 0;
424 }
425
429
432 int start_pos,
type, temporal_id, spatial_id;
434 &
type, &temporal_id, &spatial_id);
438 }
439
442 nb_seq++;
443 if (!obu_size || nb_seq > 1) {
446 }
450
451 seq = buf;
453 break;
455 if (!obu_size) {
458 }
460 break;
461 default:
462 break;
463 }
466 }
467
468 if (!nb_seq) {
471 }
472
474
488
490 if (write_seq_header) {
492 }
493
495 if (meta_size)
497
500
502 }