Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2018, VideoLAN and dav1d authors |
3 | | * Copyright © 2018, Two Orioles, LLC |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions are met: |
8 | | * |
9 | | * 1. Redistributions of source code must retain the above copyright notice, this |
10 | | * list of conditions and the following disclaimer. |
11 | | * |
12 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
13 | | * this list of conditions and the following disclaimer in the documentation |
14 | | * and/or other materials provided with the distribution. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
20 | | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | // OBU parsing and bit magic all originally from dav1d's obu.c and getbits.c, |
29 | | // but heavily modified/reduced down to simply find the Sequence Header OBU |
30 | | // and pull a few interesting pieces from it. |
31 | | // |
32 | | // Any other code in here is under this license: |
33 | | // |
34 | | // Copyright 2020 Joe Drago. All rights reserved. |
35 | | // SPDX-License-Identifier: BSD-2-Clause |
36 | | |
37 | | #include "avif/internal.h" |
38 | | |
39 | | #include <stdint.h> |
40 | | #include <string.h> |
41 | | |
42 | | #if defined(AVIF_CODEC_AVM) |
43 | | #include "avm/avm_codec.h" |
44 | | #endif |
45 | | |
46 | | // --------------------------------------------------------------------------- |
47 | | // avifBits - Originally dav1d's GetBits struct (see dav1d's getbits.c) |
48 | | |
49 | | typedef struct avifBits |
50 | | { |
51 | | int error, eof; |
52 | | uint64_t state; |
53 | | uint32_t bitsLeft; |
54 | | const uint8_t *ptr, *start, *end; |
55 | | } avifBits; |
56 | | |
57 | | static inline uint32_t avifBitsReadPos(const avifBits * bits) |
58 | 95.3k | { |
59 | 95.3k | return (uint32_t)(bits->ptr - bits->start) * 8 - bits->bitsLeft; |
60 | 95.3k | } |
61 | | |
62 | | static void avifBitsInit(avifBits * const bits, const uint8_t * const data, const size_t size) |
63 | 116k | { |
64 | 116k | bits->ptr = bits->start = data; |
65 | 116k | bits->end = &bits->start[size]; |
66 | 116k | bits->bitsLeft = 0; |
67 | 116k | bits->state = 0; |
68 | 116k | bits->error = 0; |
69 | 116k | bits->eof = (size == 0); |
70 | 116k | } |
71 | | |
72 | | static void avifBitsRefill(avifBits * const bits, const uint32_t n) |
73 | 458k | { |
74 | 458k | uint64_t state = 0; |
75 | 511k | do { |
76 | 511k | state <<= 8; |
77 | 511k | bits->bitsLeft += 8; |
78 | 511k | if (!bits->eof) |
79 | 465k | state |= *bits->ptr++; |
80 | 511k | if (bits->ptr >= bits->end) { |
81 | 58.0k | bits->error = bits->eof; |
82 | 58.0k | bits->eof = 1; |
83 | 58.0k | } |
84 | 511k | } while (n > bits->bitsLeft); |
85 | 458k | bits->state |= state << (64 - bits->bitsLeft); |
86 | 458k | } |
87 | | |
88 | | static uint32_t avifBitsRead(avifBits * const bits, const uint32_t n) |
89 | 1.21M | { |
90 | 1.21M | if (n > bits->bitsLeft) |
91 | 458k | avifBitsRefill(bits, n); |
92 | | |
93 | 1.21M | const uint64_t state = bits->state; |
94 | 1.21M | bits->bitsLeft -= n; |
95 | 1.21M | bits->state <<= n; |
96 | | |
97 | 1.21M | return (uint32_t)(state >> (64 - n)); |
98 | 1.21M | } |
99 | | |
100 | | static uint32_t avifBitsReadUleb128(avifBits * bits) |
101 | 95.2k | { |
102 | 95.2k | uint64_t val = 0; |
103 | 95.2k | uint32_t more; |
104 | 95.2k | uint32_t i = 0; |
105 | | |
106 | 107k | do { |
107 | 107k | const uint32_t v = avifBitsRead(bits, 8); |
108 | 107k | more = v & 0x80; |
109 | 107k | val |= ((uint64_t)(v & 0x7F)) << i; |
110 | 107k | i += 7; |
111 | 107k | } while (more && i < 56); |
112 | | |
113 | 95.2k | if (val > UINT32_MAX || more) { |
114 | 203 | bits->error = 1; |
115 | 203 | return 0; |
116 | 203 | } |
117 | | |
118 | 95.0k | return (uint32_t)val; |
119 | 95.2k | } |
120 | | |
121 | | static uint32_t avifBitsReadVLC(avifBits * const bits) |
122 | 1.38k | { |
123 | 1.38k | int numBits = 0; |
124 | 17.4k | while (!avifBitsRead(bits, 1)) |
125 | 16.1k | if (++numBits == 32) |
126 | 69 | return 0xFFFFFFFFU; |
127 | 1.32k | return numBits ? ((1U << numBits) - 1) + avifBitsRead(bits, numBits) : 0; |
128 | 1.38k | } |
129 | | |
130 | | #if defined(AVIF_CODEC_AVM) |
131 | | // Rice-Golomb coding with parameter n. |
132 | | static uint32_t avifBitsReadRG(avifBits * const bits, const uint32_t n) |
133 | | { |
134 | | for (uint32_t q = 0; q < 32; q++) { |
135 | | const uint32_t rgBit = avifBitsRead(bits, 1); |
136 | | if (rgBit == 0) { |
137 | | const uint32_t remainder = avifBitsRead(bits, n); |
138 | | return (q << n) + remainder; |
139 | | } |
140 | | } |
141 | | return 0xFFFFFFFFU; |
142 | | } |
143 | | #endif // defined(AVIF_CODEC_AVM) |
144 | | |
145 | | // --------------------------------------------------------------------------- |
146 | | // Variables in here use snake_case to self-document from the AV1 spec and the draft AV2 spec: |
147 | | // |
148 | | // https://aomediacodec.github.io/av1-spec/av1-spec.pdf |
149 | | // |
150 | | // Originally dav1d's parse_seq_hdr() function (heavily modified and split) |
151 | | |
152 | | static avifBool parseAV1SequenceHeaderProfile(avifBits * bits, avifSequenceHeader * header) |
153 | 20.5k | { |
154 | 20.5k | uint32_t seq_profile = avifBitsRead(bits, 3); |
155 | 20.5k | if (seq_profile > 2) { |
156 | 115 | return AVIF_FALSE; |
157 | 115 | } |
158 | 20.4k | header->av1C.seqProfile = (uint8_t)seq_profile; |
159 | 20.4k | return !bits->error; |
160 | 20.5k | } |
161 | | |
162 | | #if defined(AVIF_CODEC_AVM) |
163 | | static avifBool parseAV2SequenceHeaderProfile(avifBits * bits, avifSequenceHeader * header) |
164 | | { |
165 | | uint32_t seq_profile = avifBitsRead(bits, 5); |
166 | | if (seq_profile >= 32) { |
167 | | return AVIF_FALSE; |
168 | | } |
169 | | header->av1C.seqProfile = (uint8_t)seq_profile; |
170 | | return !bits->error; |
171 | | } |
172 | | #endif // defined(AVIF_CODEC_AVM) |
173 | | |
174 | | static avifBool parseSequenceHeaderLevelIdxAndTier(avifBits * bits, avifSequenceHeader * header) |
175 | 20.3k | { |
176 | 20.3k | uint32_t still_picture = avifBitsRead(bits, 1); |
177 | 20.3k | header->reduced_still_picture_header = (uint8_t)avifBitsRead(bits, 1); |
178 | 20.3k | if (header->reduced_still_picture_header && !still_picture) { |
179 | 66 | return AVIF_FALSE; |
180 | 66 | } |
181 | | |
182 | 20.3k | if (header->reduced_still_picture_header) { |
183 | 9.58k | header->av1C.seqLevelIdx0 = (uint8_t)avifBitsRead(bits, 5); |
184 | 9.58k | header->av1C.seqTier0 = 0; |
185 | 10.7k | } else { |
186 | 10.7k | uint32_t timing_info_present_flag = avifBitsRead(bits, 1); |
187 | 10.7k | uint32_t decoder_model_info_present_flag = 0; |
188 | 10.7k | uint32_t buffer_delay_length = 0; |
189 | 10.7k | if (timing_info_present_flag) { // timing_info() |
190 | 2.52k | avifBitsRead(bits, 32); // num_units_in_display_tick |
191 | 2.52k | avifBitsRead(bits, 32); // time_scale |
192 | 2.52k | uint32_t equal_picture_interval = avifBitsRead(bits, 1); |
193 | 2.52k | if (equal_picture_interval) { |
194 | 1.38k | uint32_t num_ticks_per_picture_minus_1 = avifBitsReadVLC(bits); |
195 | 1.38k | if (num_ticks_per_picture_minus_1 == 0xFFFFFFFFU) |
196 | 69 | return AVIF_FALSE; |
197 | 1.38k | } |
198 | | |
199 | 2.45k | decoder_model_info_present_flag = avifBitsRead(bits, 1); |
200 | 2.45k | if (decoder_model_info_present_flag) { // decoder_model_info() |
201 | 1.35k | buffer_delay_length = avifBitsRead(bits, 5) + 1; |
202 | 1.35k | avifBitsRead(bits, 32); // num_units_in_decoding_tick |
203 | 1.35k | avifBitsRead(bits, 10); // buffer_removal_time_length_minus_1, frame_presentation_time_length_minus_1 |
204 | 1.35k | } |
205 | 2.45k | } |
206 | | |
207 | 10.6k | uint32_t initial_display_delay_present_flag = avifBitsRead(bits, 1); |
208 | 10.6k | uint32_t operating_points_cnt = avifBitsRead(bits, 5) + 1; |
209 | 54.7k | for (uint32_t i = 0; i < operating_points_cnt; i++) { |
210 | 44.1k | avifBitsRead(bits, 12); // operating_point_idc |
211 | 44.1k | uint32_t seq_level_idx = avifBitsRead(bits, 5); |
212 | 44.1k | if (i == 0) { |
213 | 10.6k | header->av1C.seqLevelIdx0 = (uint8_t)seq_level_idx; |
214 | 10.6k | header->av1C.seqTier0 = 0; |
215 | 10.6k | } |
216 | 44.1k | if (seq_level_idx > 7) { |
217 | 12.6k | uint32_t seq_tier = avifBitsRead(bits, 1); |
218 | 12.6k | if (i == 0) { |
219 | 1.81k | header->av1C.seqTier0 = (uint8_t)seq_tier; |
220 | 1.81k | } |
221 | 12.6k | } |
222 | 44.1k | if (decoder_model_info_present_flag) { |
223 | 23.9k | uint32_t decoder_model_present_for_this_op = avifBitsRead(bits, 1); |
224 | 23.9k | if (decoder_model_present_for_this_op) { // operating_parameters_info() |
225 | 5.67k | avifBitsRead(bits, buffer_delay_length); // decoder_buffer_delay |
226 | 5.67k | avifBitsRead(bits, buffer_delay_length); // encoder_buffer_delay |
227 | 5.67k | avifBitsRead(bits, 1); // low_delay_mode_flag |
228 | 5.67k | } |
229 | 23.9k | } |
230 | 44.1k | if (initial_display_delay_present_flag) { |
231 | 22.1k | uint32_t initial_display_delay_present_for_this_op = avifBitsRead(bits, 1); |
232 | 22.1k | if (initial_display_delay_present_for_this_op) { |
233 | 4.58k | avifBitsRead(bits, 4); // initial_display_delay_minus_1 |
234 | 4.58k | } |
235 | 22.1k | } |
236 | 44.1k | } |
237 | 10.6k | } |
238 | 20.2k | return !bits->error; |
239 | 20.3k | } |
240 | | |
241 | | static avifBool parseSequenceHeaderFrameMaxDimensions(avifBits * bits, avifSequenceHeader * header) |
242 | 18.2k | { |
243 | 18.2k | uint32_t frame_width_bits = avifBitsRead(bits, 4) + 1; |
244 | 18.2k | uint32_t frame_height_bits = avifBitsRead(bits, 4) + 1; |
245 | 18.2k | header->maxWidth = avifBitsRead(bits, frame_width_bits) + 1; // max_frame_width |
246 | 18.2k | header->maxHeight = avifBitsRead(bits, frame_height_bits) + 1; // max_frame_height |
247 | 18.2k | uint32_t frame_id_numbers_present_flag = 0; |
248 | 18.2k | if (!header->reduced_still_picture_header) { |
249 | 8.69k | frame_id_numbers_present_flag = avifBitsRead(bits, 1); |
250 | 8.69k | } |
251 | 18.2k | if (frame_id_numbers_present_flag) { |
252 | 876 | avifBitsRead(bits, 7); // delta_frame_id_length_minus_2, additional_frame_id_length_minus_1 |
253 | 876 | } |
254 | 18.2k | return !bits->error; |
255 | 18.2k | } |
256 | | |
257 | | static avifBool parseSequenceHeaderEnabledFeatures(avifBits * bits, avifSequenceHeader * header) |
258 | 17.3k | { |
259 | 17.3k | avifBitsRead(bits, 2); // enable_filter_intra, enable_intra_edge_filter |
260 | | |
261 | 17.3k | if (!header->reduced_still_picture_header) { |
262 | 8.06k | avifBitsRead(bits, 4); // enable_interintra_compound, enable_masked_compound, enable_warped_motion, enable_dual_filter |
263 | 8.06k | uint32_t enable_order_hint = avifBitsRead(bits, 1); |
264 | 8.06k | if (enable_order_hint) { |
265 | 5.52k | avifBitsRead(bits, 2); // enable_jnt_comp, enable_ref_frame_mvs |
266 | 5.52k | } |
267 | | |
268 | 8.06k | uint32_t seq_force_screen_content_tools = 0; |
269 | 8.06k | uint32_t seq_choose_screen_content_tools = avifBitsRead(bits, 1); |
270 | 8.06k | if (seq_choose_screen_content_tools) { |
271 | 3.04k | seq_force_screen_content_tools = 2; |
272 | 5.01k | } else { |
273 | 5.01k | seq_force_screen_content_tools = avifBitsRead(bits, 1); |
274 | 5.01k | } |
275 | 8.06k | if (seq_force_screen_content_tools > 0) { |
276 | 3.66k | uint32_t seq_choose_integer_mv = avifBitsRead(bits, 1); |
277 | 3.66k | if (!seq_choose_integer_mv) { |
278 | 1.38k | avifBitsRead(bits, 1); // seq_force_integer_mv |
279 | 1.38k | } |
280 | 3.66k | } |
281 | 8.06k | if (enable_order_hint) { |
282 | 5.52k | avifBitsRead(bits, 3); // order_hint_bits_minus_1 |
283 | 5.52k | } |
284 | 8.06k | } |
285 | | |
286 | 17.3k | return !bits->error; |
287 | 17.3k | } |
288 | | |
289 | | // Note: Does not parse separate_uv_delta_q. |
290 | | static avifBool parseAV1SequenceHeaderColorConfig(avifBits * bits, avifSequenceHeader * header) |
291 | 16.7k | { |
292 | 16.7k | header->bitDepth = 8; |
293 | 16.7k | header->chromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
294 | 16.7k | header->av1C.chromaSamplePosition = (uint8_t)header->chromaSamplePosition; |
295 | 16.7k | uint32_t high_bitdepth = avifBitsRead(bits, 1); |
296 | 16.7k | header->av1C.highBitdepth = (uint8_t)high_bitdepth; |
297 | 16.7k | if ((header->av1C.seqProfile == 2) && high_bitdepth) { |
298 | 2.50k | uint32_t twelve_bit = avifBitsRead(bits, 1); |
299 | 2.50k | header->bitDepth = twelve_bit ? 12 : 10; |
300 | 2.50k | header->av1C.twelveBit = (uint8_t)twelve_bit; |
301 | 14.2k | } else /* if (seq_profile <= 2) */ { |
302 | 14.2k | header->bitDepth = high_bitdepth ? 10 : 8; |
303 | 14.2k | header->av1C.twelveBit = 0; |
304 | 14.2k | } |
305 | 16.7k | uint32_t mono_chrome = 0; |
306 | 16.7k | if (header->av1C.seqProfile != 1) { |
307 | 13.1k | mono_chrome = avifBitsRead(bits, 1); |
308 | 13.1k | } |
309 | 16.7k | header->av1C.monochrome = (uint8_t)mono_chrome; |
310 | 16.7k | uint32_t color_description_present_flag = avifBitsRead(bits, 1); |
311 | 16.7k | if (color_description_present_flag) { |
312 | 3.19k | header->colorPrimaries = (avifColorPrimaries)avifBitsRead(bits, 8); // color_primaries |
313 | 3.19k | header->transferCharacteristics = (avifTransferCharacteristics)avifBitsRead(bits, 8); // transfer_characteristics |
314 | 3.19k | header->matrixCoefficients = (avifMatrixCoefficients)avifBitsRead(bits, 8); // matrix_coefficients |
315 | 13.6k | } else { |
316 | 13.6k | header->colorPrimaries = AVIF_COLOR_PRIMARIES_UNSPECIFIED; |
317 | 13.6k | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED; |
318 | 13.6k | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED; |
319 | 13.6k | } |
320 | 16.7k | if (mono_chrome) { |
321 | 4.18k | header->range = avifBitsRead(bits, 1) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; // color_range |
322 | 4.18k | header->av1C.chromaSubsamplingX = 1; |
323 | 4.18k | header->av1C.chromaSubsamplingY = 1; |
324 | 4.18k | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV400; |
325 | 12.6k | } else if (header->colorPrimaries == AVIF_COLOR_PRIMARIES_BT709 && |
326 | 873 | header->transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_SRGB && |
327 | 661 | header->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_IDENTITY) { |
328 | 191 | header->range = AVIF_RANGE_FULL; |
329 | 191 | header->av1C.chromaSubsamplingX = 0; |
330 | 191 | header->av1C.chromaSubsamplingY = 0; |
331 | 191 | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
332 | 12.4k | } else { |
333 | 12.4k | uint32_t subsampling_x = 0; |
334 | 12.4k | uint32_t subsampling_y = 0; |
335 | 12.4k | header->range = avifBitsRead(bits, 1) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; // color_range |
336 | 12.4k | switch (header->av1C.seqProfile) { |
337 | 5.69k | case 0: |
338 | 5.69k | subsampling_x = 1; |
339 | 5.69k | subsampling_y = 1; |
340 | 5.69k | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; |
341 | 5.69k | break; |
342 | 3.44k | case 1: |
343 | 3.44k | subsampling_x = 0; |
344 | 3.44k | subsampling_y = 0; |
345 | 3.44k | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
346 | 3.44k | break; |
347 | 3.28k | case 2: |
348 | 3.28k | if (header->bitDepth == 12) { |
349 | 2.09k | subsampling_x = avifBitsRead(bits, 1); |
350 | 2.09k | if (subsampling_x) { |
351 | 496 | subsampling_y = avifBitsRead(bits, 1); |
352 | 496 | } |
353 | 2.09k | } else { |
354 | 1.18k | subsampling_x = 1; |
355 | 1.18k | subsampling_y = 0; |
356 | 1.18k | } |
357 | 3.28k | if (subsampling_x) { |
358 | 1.68k | header->yuvFormat = subsampling_y ? AVIF_PIXEL_FORMAT_YUV420 : AVIF_PIXEL_FORMAT_YUV422; |
359 | 1.68k | } else { |
360 | 1.60k | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
361 | 1.60k | } |
362 | 3.28k | break; |
363 | 0 | default: |
364 | 0 | return AVIF_FALSE; |
365 | 12.4k | } |
366 | | |
367 | 12.4k | if (subsampling_x && subsampling_y) { |
368 | 5.78k | header->chromaSamplePosition = (avifChromaSamplePosition)avifBitsRead(bits, 2); // chroma_sample_position |
369 | 5.78k | header->av1C.chromaSamplePosition = (uint8_t)header->chromaSamplePosition; |
370 | 5.78k | } |
371 | 12.4k | header->av1C.chromaSubsamplingX = (uint8_t)subsampling_x; |
372 | 12.4k | header->av1C.chromaSubsamplingY = (uint8_t)subsampling_y; |
373 | 12.4k | } |
374 | | |
375 | 16.7k | return !bits->error; |
376 | 16.7k | } |
377 | | |
378 | | #if defined(AVIF_CODEC_AVM) |
379 | | enum |
380 | | { |
381 | | AV2_CHROMA_FORMAT_420 = 0, |
382 | | AV2_CHROMA_FORMAT_400 = 1, |
383 | | AV2_CHROMA_FORMAT_444 = 2, |
384 | | AV2_CHROMA_FORMAT_422 = 3, |
385 | | }; |
386 | | |
387 | | static avifChromaSamplePosition av2ChromaSamplePositionToAv1ChromaSamplePosition(uint32_t av2ChromaSamplePosition) |
388 | | { |
389 | | if (av2ChromaSamplePosition == 0) { |
390 | | // AVM_CSP_LEFT: Horizontal offset 0, vertical offset 0.5 |
391 | | return AVIF_CHROMA_SAMPLE_POSITION_VERTICAL; |
392 | | } else if (av2ChromaSamplePosition == 1) { |
393 | | // AVM_CSP_CENTER: Horizontal offset 0.5, vertical offset 0.5 |
394 | | return AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
395 | | } else if (av2ChromaSamplePosition == 2) { |
396 | | // AVM_CSP_TOPLEFT: Horizontal offset 0, vertical offset 0 |
397 | | return AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
398 | | } else { |
399 | | return AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
400 | | } |
401 | | } |
402 | | |
403 | | static avifBool parseAV2ChromaFormatBitdepth(avifBits * bits, avifSequenceHeader * header) |
404 | | { |
405 | | const uint32_t chromaFormatIdc = avifBitsReadVLC(bits); |
406 | | |
407 | | const uint32_t bitdepthIdx = avifBitsReadVLC(bits); |
408 | | if (bitdepthIdx == 0) { |
409 | | header->bitDepth = 10; |
410 | | } else if (bitdepthIdx == 1) { |
411 | | header->bitDepth = 8; |
412 | | } else if (bitdepthIdx == 2) { |
413 | | header->bitDepth = 12; |
414 | | } else { |
415 | | return AVIF_FALSE; |
416 | | } |
417 | | header->av1C.highBitdepth = header->bitDepth > 8; |
418 | | header->av1C.twelveBit = header->bitDepth == 12; |
419 | | header->av1C.monochrome = chromaFormatIdc == AV2_CHROMA_FORMAT_400; |
420 | | |
421 | | if (header->av1C.monochrome) { |
422 | | header->av1C.chromaSubsamplingX = 1; |
423 | | header->av1C.chromaSubsamplingY = 1; |
424 | | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV400; |
425 | | } else if (chromaFormatIdc == AV2_CHROMA_FORMAT_420) { |
426 | | header->av1C.chromaSubsamplingX = 1; |
427 | | header->av1C.chromaSubsamplingY = 1; |
428 | | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; |
429 | | } else if (chromaFormatIdc == AV2_CHROMA_FORMAT_444) { |
430 | | header->av1C.chromaSubsamplingX = 0; |
431 | | header->av1C.chromaSubsamplingY = 0; |
432 | | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
433 | | } else if (chromaFormatIdc == AV2_CHROMA_FORMAT_422) { |
434 | | header->av1C.chromaSubsamplingX = 1; |
435 | | header->av1C.chromaSubsamplingY = 0; |
436 | | header->yuvFormat = AVIF_PIXEL_FORMAT_YUV422; |
437 | | } else { |
438 | | return AVIF_FALSE; |
439 | | } |
440 | | |
441 | | return !bits->error; |
442 | | } |
443 | | #endif // defined(AVIF_CODEC_AVM) |
444 | | |
445 | | static avifBool parseAV1SequenceHeader(avifBits * bits, avifSequenceHeader * header) |
446 | 20.5k | { |
447 | 20.5k | AVIF_CHECK(parseAV1SequenceHeaderProfile(bits, header)); |
448 | 20.3k | AVIF_CHECK(parseSequenceHeaderLevelIdxAndTier(bits, header)); |
449 | | |
450 | 18.2k | AVIF_CHECK(parseSequenceHeaderFrameMaxDimensions(bits, header)); |
451 | 17.3k | avifBitsRead(bits, 1); // use_128x128_superblock |
452 | 17.3k | AVIF_CHECK(parseSequenceHeaderEnabledFeatures(bits, header)); |
453 | | |
454 | 16.7k | avifBitsRead(bits, 3); // enable_superres, enable_cdef, enable_restoration |
455 | | |
456 | 16.7k | AVIF_CHECK(parseAV1SequenceHeaderColorConfig(bits, header)); |
457 | 15.0k | if (!header->av1C.monochrome) { |
458 | 11.2k | avifBitsRead(bits, 1); // separate_uv_delta_q |
459 | 11.2k | } |
460 | | |
461 | 15.0k | avifBitsRead(bits, 1); // film_grain_params_present |
462 | 15.0k | return !bits->error; |
463 | 16.7k | } |
464 | | |
465 | | #if defined(AVIF_CODEC_AVM) |
466 | | static int avifCeilLog2(int x) |
467 | | { |
468 | | if (x < 2) |
469 | | return 0; |
470 | | int i = 1; |
471 | | int p = 2; |
472 | | while (p < x) { |
473 | | i++; |
474 | | p <<= 1; |
475 | | } |
476 | | return i; |
477 | | } |
478 | | |
479 | | // See read_sequence_header_obu() in av2/decoder/obu.c. |
480 | | static avifBool parseAV2SequenceHeader(avifBits * bits, avifSequenceHeader * header) |
481 | | { |
482 | | uint32_t seqHeaderId = avifBitsReadVLC(bits); |
483 | | if (seqHeaderId >= 16) { |
484 | | return AVIF_FALSE; |
485 | | } |
486 | | |
487 | | AVIF_CHECK(parseAV2SequenceHeaderProfile(bits, header)); |
488 | | header->reduced_still_picture_header = (uint8_t)avifBitsRead(bits, 1); // single_picture_header_flag |
489 | | header->av1C.seqLevelIdx0 = (uint8_t)avifBitsRead(bits, 5); |
490 | | if (header->av1C.seqLevelIdx0 >= 4 && !header->reduced_still_picture_header) { |
491 | | header->av1C.seqTier0 = avifBitsRead(bits, 1); |
492 | | } else { |
493 | | header->av1C.seqTier0 = 0; |
494 | | } |
495 | | |
496 | | AVIF_CHECK(parseAV2ChromaFormatBitdepth(bits, header)); |
497 | | |
498 | | if (!header->reduced_still_picture_header) { |
499 | | avifBitsRead(bits, 3); // seq_lcr_id |
500 | | avifBitsRead(bits, 1); // still_picture |
501 | | avifBitsRead(bits, 2); // max_tlayer_id |
502 | | const int maxMlayerId = avifBitsRead(bits, 3); // max_mlayer_id |
503 | | if (maxMlayerId > 0) { |
504 | | const int n = avifCeilLog2(maxMlayerId + 1); |
505 | | avifBitsRead(bits, n); // seq_max_mlayer_cnt_minus_1 |
506 | | } |
507 | | avifBitsRead(bits, 1); // monotonic_output_order_flag |
508 | | } |
509 | | |
510 | | uint32_t frame_width_bits = avifBitsRead(bits, 4) + 1; |
511 | | uint32_t frame_height_bits = avifBitsRead(bits, 4) + 1; |
512 | | header->maxWidth = avifBitsRead(bits, frame_width_bits) + 1; // max_frame_width |
513 | | header->maxHeight = avifBitsRead(bits, frame_height_bits) + 1; // max_frame_height |
514 | | |
515 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_UNSPECIFIED; |
516 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED; |
517 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED; |
518 | | header->range = AVIF_RANGE_LIMITED; |
519 | | |
520 | | header->chromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
521 | | header->av1C.chromaSamplePosition = (uint8_t)header->chromaSamplePosition; |
522 | | |
523 | | // Other ignored fields. |
524 | | return !bits->error; |
525 | | } |
526 | | |
527 | | // See av2_read_content_interpretation_obu() in av2/decoder/obu_ci.c. |
528 | | static avifBool parseAV2ContentInterpretation(avifBits * bits, avifSequenceHeader * header) |
529 | | { |
530 | | avifBitsRead(bits, 2); // ci_scan_type_idc |
531 | | const uint32_t colorDescriptionPresent = avifBitsRead(bits, 1); // ci_color_description_present_flag |
532 | | const uint32_t chromaSamplePositionPresent = avifBitsRead(bits, 1); // ci_chroma_sample_position_present_flag |
533 | | avifBitsRead(bits, 1); // ci_aspect_ratio_info_present_flag |
534 | | avifBitsRead(bits, 1); // ci_timing_info_present_flag |
535 | | avifBitsRead(bits, 1); // ci_extension_present_flag |
536 | | avifBitsRead(bits, 1); // reserved_bit |
537 | | |
538 | | if (colorDescriptionPresent) { |
539 | | // Override the default CICP values. |
540 | | const uint32_t colorDescriptionIdc = avifBitsReadRG(bits, 2); // color_description_idc |
541 | | if (colorDescriptionIdc == 0xFFFFFFFFU) { |
542 | | return AVIF_FALSE; |
543 | | } |
544 | | if (colorDescriptionIdc == 0) { |
545 | | // Explicitly signaled |
546 | | header->colorPrimaries = (avifColorPrimaries)avifBitsRead(bits, 8); // color_primaries |
547 | | header->transferCharacteristics = (avifTransferCharacteristics)avifBitsRead(bits, 8); // transfer_characteristics |
548 | | header->matrixCoefficients = (avifMatrixCoefficients)avifBitsRead(bits, 8); // matrix_coefficients |
549 | | } else if (colorDescriptionIdc == 1) { |
550 | | // BT.709 SDR |
551 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709; // 1 |
552 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_BT709; // 1 |
553 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT709; // 1 |
554 | | } else if (colorDescriptionIdc == 2) { |
555 | | // BT.2100 PQ |
556 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_BT2100; // 9 |
557 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_PQ; // 16 |
558 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT2020_NCL; // 9 |
559 | | } else if (colorDescriptionIdc == 3) { |
560 | | // BT.2100 HLG |
561 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_BT2100; // 9 |
562 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_HLG; // 18 |
563 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT2020_NCL; // 9 |
564 | | } else if (colorDescriptionIdc == 4) { |
565 | | // sRGB |
566 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709; // 1 |
567 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB; // 13 |
568 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_IDENTITY; // 0 |
569 | | } else if (colorDescriptionIdc == 5) { |
570 | | // sYCC |
571 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709; // 1 |
572 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB; // 13 |
573 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT470BG; // 5 |
574 | | } else { |
575 | | // Reserved |
576 | | header->colorPrimaries = AVIF_COLOR_PRIMARIES_UNSPECIFIED; // 2 |
577 | | header->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED; // 2 |
578 | | header->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED; // 2 |
579 | | } |
580 | | header->range = avifBitsRead(bits, 1) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; // color_range |
581 | | } else { |
582 | | // Keep the default CICP values. |
583 | | } |
584 | | |
585 | | if (chromaSamplePositionPresent) { |
586 | | const uint32_t chromaSamplePosition = avifBitsReadVLC(bits); // ci_chroma_sample_position_0 |
587 | | header->chromaSamplePosition = av2ChromaSamplePositionToAv1ChromaSamplePosition(chromaSamplePosition); |
588 | | header->av1C.chromaSamplePosition = (uint8_t)header->chromaSamplePosition; |
589 | | } |
590 | | |
591 | | // Other ignored fields. |
592 | | return !bits->error; |
593 | | } |
594 | | #endif // defined(AVIF_CODEC_AVM) |
595 | | |
596 | | static avifBool av1SequenceHeaderParse(avifSequenceHeader * header, const avifROData * sample) |
597 | 21.9k | { |
598 | 21.9k | avifROData obus = *sample; |
599 | | |
600 | | // Find the sequence header OBU |
601 | 96.2k | while (obus.size > 0) { |
602 | 95.8k | avifBits bits; |
603 | 95.8k | avifBitsInit(&bits, obus.data, obus.size); |
604 | | |
605 | | // obu_header() |
606 | 95.8k | const uint32_t obu_forbidden_bit = avifBitsRead(&bits, 1); |
607 | 95.8k | if (obu_forbidden_bit != 0) { |
608 | 94 | return AVIF_FALSE; |
609 | 94 | } |
610 | 95.7k | const uint32_t obu_type = avifBitsRead(&bits, 4); |
611 | 95.7k | const uint32_t obu_extension_flag = avifBitsRead(&bits, 1); |
612 | 95.7k | const uint32_t obu_has_size_field = avifBitsRead(&bits, 1); |
613 | 95.7k | avifBitsRead(&bits, 1); // obu_reserved_1bit |
614 | | |
615 | 95.7k | if (obu_extension_flag) { // obu_extension_header() |
616 | 31.5k | avifBitsRead(&bits, 8); // temporal_id, spatial_id, extension_header_reserved_3bits |
617 | 31.5k | } |
618 | | |
619 | 95.7k | uint32_t obu_size = 0; |
620 | 95.7k | if (obu_has_size_field) |
621 | 95.2k | obu_size = avifBitsReadUleb128(&bits); |
622 | 484 | else |
623 | 484 | obu_size = (int)obus.size - 1 - obu_extension_flag; |
624 | | |
625 | 95.7k | if (bits.error) { |
626 | 367 | return AVIF_FALSE; |
627 | 367 | } |
628 | | |
629 | 95.3k | const uint32_t init_bit_pos = avifBitsReadPos(&bits); |
630 | 95.3k | const uint32_t init_byte_pos = init_bit_pos >> 3; |
631 | 95.3k | if (obu_size > obus.size - init_byte_pos) |
632 | 433 | return AVIF_FALSE; |
633 | | |
634 | 94.9k | if (obu_type == 1) { // Sequence Header |
635 | 20.5k | avifBits seqHdrBits; |
636 | 20.5k | avifBitsInit(&seqHdrBits, obus.data + init_byte_pos, obu_size); |
637 | 20.5k | return parseAV1SequenceHeader(&seqHdrBits, header); |
638 | 20.5k | } |
639 | | |
640 | | // Skip this OBU |
641 | 74.3k | obus.data += (size_t)obu_size + init_byte_pos; |
642 | 74.3k | obus.size -= (size_t)obu_size + init_byte_pos; |
643 | 74.3k | } |
644 | 430 | return AVIF_FALSE; |
645 | 21.9k | } |
646 | | |
647 | | #if defined(AVIF_CODEC_AVM) |
648 | | static avifBool av2SequenceHeaderParse(avifSequenceHeader * header, const avifROData * sample) |
649 | | { |
650 | | avifBool sequenceHeaderFound = AVIF_FALSE; |
651 | | avifROData obus = *sample; |
652 | | |
653 | | // Find the Sequence Header OBU, and the Content Interpretation OBU if any. |
654 | | while (obus.size > 0) { |
655 | | avifBits bits; |
656 | | avifBitsInit(&bits, obus.data, obus.size); |
657 | | |
658 | | const uint32_t obuSize = avifBitsReadUleb128(&bits); |
659 | | |
660 | | // obu_header() |
661 | | const uint32_t obuHeaderExtensionFlag = avifBitsRead(&bits, 1); |
662 | | const uint32_t obuType = avifBitsRead(&bits, 5); |
663 | | avifBitsRead(&bits, 2); // obu_tlayer_id |
664 | | |
665 | | if (obuHeaderExtensionFlag) { |
666 | | avifBitsRead(&bits, 8); // obu_mlayer_id, obu_xlayer_id |
667 | | } |
668 | | |
669 | | if (bits.error) { |
670 | | return AVIF_FALSE; |
671 | | } |
672 | | |
673 | | const uint32_t obuHeaderSize = 1 + obuHeaderExtensionFlag; |
674 | | if (obuSize < obuHeaderSize) { |
675 | | return AVIF_FALSE; |
676 | | } |
677 | | const uint32_t obuPayloadSize = obuSize - obuHeaderSize; |
678 | | const uint32_t initBitPos = avifBitsReadPos(&bits); |
679 | | const uint32_t initBytePos = initBitPos >> 3; |
680 | | if (obuPayloadSize > obus.size - initBytePos) { |
681 | | return AVIF_FALSE; |
682 | | } |
683 | | |
684 | | if (obuType == OBU_SEQUENCE_HEADER) { |
685 | | if (sequenceHeaderFound) { |
686 | | return AVIF_FALSE; |
687 | | } |
688 | | avifBits seqHdrBits; |
689 | | avifBitsInit(&seqHdrBits, obus.data + initBytePos, obuPayloadSize); |
690 | | if (!parseAV2SequenceHeader(&seqHdrBits, header)) { |
691 | | return AVIF_FALSE; |
692 | | } |
693 | | sequenceHeaderFound = AVIF_TRUE; |
694 | | } else if (obuType == OBU_CONTENT_INTERPRETATION) { // optional |
695 | | if (!sequenceHeaderFound) { |
696 | | return AVIF_FALSE; |
697 | | } |
698 | | avifBits ciBits; |
699 | | avifBitsInit(&ciBits, obus.data + initBytePos, obuPayloadSize); |
700 | | if (!parseAV2ContentInterpretation(&ciBits, header)) { |
701 | | return AVIF_FALSE; |
702 | | } |
703 | | break; |
704 | | } |
705 | | obus.data += (size_t)obuPayloadSize + initBytePos; |
706 | | obus.size -= (size_t)obuPayloadSize + initBytePos; |
707 | | } |
708 | | return sequenceHeaderFound; |
709 | | } |
710 | | #endif // defined(AVIF_CODEC_AVM) |
711 | | |
712 | | avifBool avifSequenceHeaderParse(avifSequenceHeader * header, const avifROData * sample, avifCodecType codecType) |
713 | 21.9k | { |
714 | 21.9k | switch (codecType) { |
715 | 21.9k | case AVIF_CODEC_TYPE_AV1: |
716 | 21.9k | return av1SequenceHeaderParse(header, sample); |
717 | | #if defined(AVIF_CODEC_AVM) |
718 | | case AVIF_CODEC_TYPE_AV2: |
719 | | return av2SequenceHeaderParse(header, sample); |
720 | | #endif |
721 | 0 | default: |
722 | 0 | return AVIF_FALSE; |
723 | 21.9k | } |
724 | 21.9k | } |