Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2018-2021, 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 | | #include "config.h" |
29 | | |
30 | | #include <errno.h> |
31 | | #include <limits.h> |
32 | | #include <stdio.h> |
33 | | |
34 | | #include "dav1d/data.h" |
35 | | |
36 | | #include "common/frame.h" |
37 | | #include "common/intops.h" |
38 | | #include "common/validate.h" |
39 | | |
40 | | #include "src/decode.h" |
41 | | #include "src/getbits.h" |
42 | | #include "src/levels.h" |
43 | | #include "src/log.h" |
44 | | #include "src/obu.h" |
45 | | #include "src/ref.h" |
46 | | #include "src/thread_task.h" |
47 | | |
48 | | static int check_trailing_bits(GetBits *const gb, |
49 | | const int strict_std_compliance) |
50 | 49.0k | { |
51 | 49.0k | const int trailing_one_bit = dav1d_get_bit(gb); |
52 | | |
53 | 49.0k | if (gb->error) |
54 | 288 | return DAV1D_ERR(EINVAL); |
55 | | |
56 | 48.7k | if (!strict_std_compliance) |
57 | 48.7k | return 0; |
58 | | |
59 | 0 | if (!trailing_one_bit || gb->state) |
60 | 0 | return DAV1D_ERR(EINVAL); |
61 | | |
62 | 0 | ptrdiff_t size = gb->ptr_end - gb->ptr; |
63 | 0 | while (size > 0 && gb->ptr[size - 1] == 0) |
64 | 0 | size--; |
65 | |
|
66 | 0 | if (size) |
67 | 0 | return DAV1D_ERR(EINVAL); |
68 | | |
69 | 0 | return 0; |
70 | 0 | } |
71 | | |
72 | | static NOINLINE int parse_seq_hdr(Dav1dSequenceHeader *const hdr, |
73 | | GetBits *const gb, |
74 | | const int strict_std_compliance) |
75 | 44.3k | { |
76 | 44.3k | #define DEBUG_SEQ_HDR 0 |
77 | | |
78 | | #if DEBUG_SEQ_HDR |
79 | | const unsigned init_bit_pos = dav1d_get_bits_pos(gb); |
80 | | #endif |
81 | | |
82 | 44.3k | memset(hdr, 0, sizeof(*hdr)); |
83 | 44.3k | hdr->profile = dav1d_get_bits(gb, 3); |
84 | 44.3k | if (hdr->profile > 2) goto error; |
85 | | #if DEBUG_SEQ_HDR |
86 | | printf("SEQHDR: post-profile: off=%u\n", |
87 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
88 | | #endif |
89 | | |
90 | 44.2k | hdr->still_picture = dav1d_get_bit(gb); |
91 | 44.2k | hdr->reduced_still_picture_header = dav1d_get_bit(gb); |
92 | 44.2k | if (hdr->reduced_still_picture_header && !hdr->still_picture) goto error; |
93 | | #if DEBUG_SEQ_HDR |
94 | | printf("SEQHDR: post-stillpicture_flags: off=%u\n", |
95 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
96 | | #endif |
97 | | |
98 | 44.2k | if (hdr->reduced_still_picture_header) { |
99 | 29.8k | hdr->num_operating_points = 1; |
100 | 29.8k | hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3); |
101 | 29.8k | hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2); |
102 | 29.8k | hdr->operating_points[0].initial_display_delay = 10; |
103 | 29.8k | } else { |
104 | 14.4k | hdr->timing_info_present = dav1d_get_bit(gb); |
105 | 14.4k | if (hdr->timing_info_present) { |
106 | 481 | hdr->num_units_in_tick = dav1d_get_bits(gb, 32); |
107 | 481 | hdr->time_scale = dav1d_get_bits(gb, 32); |
108 | 481 | if (strict_std_compliance && (!hdr->num_units_in_tick || !hdr->time_scale)) |
109 | 0 | goto error; |
110 | 481 | hdr->equal_picture_interval = dav1d_get_bit(gb); |
111 | 481 | if (hdr->equal_picture_interval) { |
112 | 278 | const unsigned num_ticks_per_picture = dav1d_get_vlc(gb); |
113 | 278 | if (num_ticks_per_picture == UINT32_MAX) |
114 | 0 | goto error; |
115 | 278 | hdr->num_ticks_per_picture = num_ticks_per_picture + 1; |
116 | 278 | } |
117 | | |
118 | 481 | hdr->decoder_model_info_present = dav1d_get_bit(gb); |
119 | 481 | if (hdr->decoder_model_info_present) { |
120 | 260 | hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1; |
121 | 260 | hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32); |
122 | 260 | if (strict_std_compliance && !hdr->num_units_in_decoding_tick) |
123 | 0 | goto error; |
124 | 260 | hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1; |
125 | 260 | hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1; |
126 | 260 | } |
127 | 481 | } |
128 | | #if DEBUG_SEQ_HDR |
129 | | printf("SEQHDR: post-timinginfo: off=%u\n", |
130 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
131 | | #endif |
132 | | |
133 | 14.4k | hdr->display_model_info_present = dav1d_get_bit(gb); |
134 | 14.4k | hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1; |
135 | 45.1k | for (int i = 0; i < hdr->num_operating_points; i++) { |
136 | 30.8k | struct Dav1dSequenceHeaderOperatingPoint *const op = |
137 | 30.8k | &hdr->operating_points[i]; |
138 | 30.8k | op->idc = dav1d_get_bits(gb, 12); |
139 | 30.8k | if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00))) |
140 | 135 | goto error; |
141 | 30.7k | op->major_level = 2 + dav1d_get_bits(gb, 3); |
142 | 30.7k | op->minor_level = dav1d_get_bits(gb, 2); |
143 | 30.7k | if (op->major_level > 3) |
144 | 17.8k | op->tier = dav1d_get_bit(gb); |
145 | 30.7k | if (hdr->decoder_model_info_present) { |
146 | 1.86k | op->decoder_model_param_present = dav1d_get_bit(gb); |
147 | 1.86k | if (op->decoder_model_param_present) { |
148 | 805 | struct Dav1dSequenceHeaderOperatingParameterInfo *const opi = |
149 | 805 | &hdr->operating_parameter_info[i]; |
150 | 805 | opi->decoder_buffer_delay = |
151 | 805 | dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length); |
152 | 805 | opi->encoder_buffer_delay = |
153 | 805 | dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length); |
154 | 805 | opi->low_delay_mode = dav1d_get_bit(gb); |
155 | 805 | } |
156 | 1.86k | } |
157 | 30.7k | if (hdr->display_model_info_present) |
158 | 27.8k | op->display_model_param_present = dav1d_get_bit(gb); |
159 | 30.7k | op->initial_display_delay = |
160 | 30.7k | op->display_model_param_present ? dav1d_get_bits(gb, 4) + 1 : 10; |
161 | 30.7k | } |
162 | | #if DEBUG_SEQ_HDR |
163 | | printf("SEQHDR: post-operating-points: off=%u\n", |
164 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
165 | | #endif |
166 | 14.4k | } |
167 | | |
168 | 44.0k | hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1; |
169 | 44.0k | hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1; |
170 | 44.0k | hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1; |
171 | 44.0k | hdr->max_height = dav1d_get_bits(gb, hdr->height_n_bits) + 1; |
172 | | #if DEBUG_SEQ_HDR |
173 | | printf("SEQHDR: post-size: off=%u\n", |
174 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
175 | | #endif |
176 | 44.0k | if (!hdr->reduced_still_picture_header) { |
177 | 14.2k | hdr->frame_id_numbers_present = dav1d_get_bit(gb); |
178 | 14.2k | if (hdr->frame_id_numbers_present) { |
179 | 554 | hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2; |
180 | 554 | hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1; |
181 | 554 | } |
182 | 14.2k | } |
183 | | #if DEBUG_SEQ_HDR |
184 | | printf("SEQHDR: post-frame-id-numbers-present: off=%u\n", |
185 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
186 | | #endif |
187 | | |
188 | 44.0k | hdr->sb128 = dav1d_get_bit(gb); |
189 | 44.0k | hdr->filter_intra = dav1d_get_bit(gb); |
190 | 44.0k | hdr->intra_edge_filter = dav1d_get_bit(gb); |
191 | 44.0k | if (hdr->reduced_still_picture_header) { |
192 | 29.8k | hdr->screen_content_tools = DAV1D_ADAPTIVE; |
193 | 29.8k | hdr->force_integer_mv = DAV1D_ADAPTIVE; |
194 | 29.8k | } else { |
195 | 14.2k | hdr->inter_intra = dav1d_get_bit(gb); |
196 | 14.2k | hdr->masked_compound = dav1d_get_bit(gb); |
197 | 14.2k | hdr->warped_motion = dav1d_get_bit(gb); |
198 | 14.2k | hdr->dual_filter = dav1d_get_bit(gb); |
199 | 14.2k | hdr->order_hint = dav1d_get_bit(gb); |
200 | 14.2k | if (hdr->order_hint) { |
201 | 3.64k | hdr->jnt_comp = dav1d_get_bit(gb); |
202 | 3.64k | hdr->ref_frame_mvs = dav1d_get_bit(gb); |
203 | 3.64k | } |
204 | 14.2k | hdr->screen_content_tools = dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb); |
205 | | #if DEBUG_SEQ_HDR |
206 | | printf("SEQHDR: post-screentools: off=%u\n", |
207 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
208 | | #endif |
209 | 14.2k | hdr->force_integer_mv = hdr->screen_content_tools ? |
210 | 7.80k | dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb) : 2; |
211 | 14.2k | if (hdr->order_hint) |
212 | 3.64k | hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1; |
213 | 14.2k | } |
214 | 44.0k | hdr->super_res = dav1d_get_bit(gb); |
215 | 44.0k | hdr->cdef = dav1d_get_bit(gb); |
216 | 44.0k | hdr->restoration = dav1d_get_bit(gb); |
217 | | #if DEBUG_SEQ_HDR |
218 | | printf("SEQHDR: post-featurebits: off=%u\n", |
219 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
220 | | #endif |
221 | | |
222 | 44.0k | hdr->hbd = dav1d_get_bit(gb); |
223 | 44.0k | if (hdr->profile == 2 && hdr->hbd) |
224 | 2.10k | hdr->hbd += dav1d_get_bit(gb); |
225 | 44.0k | if (hdr->profile != 1) |
226 | 36.1k | hdr->monochrome = dav1d_get_bit(gb); |
227 | 44.0k | hdr->color_description_present = dav1d_get_bit(gb); |
228 | 44.0k | if (hdr->color_description_present) { |
229 | 7.42k | hdr->pri = dav1d_get_bits(gb, 8); |
230 | 7.42k | hdr->trc = dav1d_get_bits(gb, 8); |
231 | 7.42k | hdr->mtrx = dav1d_get_bits(gb, 8); |
232 | 36.6k | } else { |
233 | 36.6k | hdr->pri = DAV1D_COLOR_PRI_UNKNOWN; |
234 | 36.6k | hdr->trc = DAV1D_TRC_UNKNOWN; |
235 | 36.6k | hdr->mtrx = DAV1D_MC_UNKNOWN; |
236 | 36.6k | } |
237 | 44.0k | if (hdr->monochrome) { |
238 | 9.53k | hdr->color_range = dav1d_get_bit(gb); |
239 | 9.53k | hdr->layout = DAV1D_PIXEL_LAYOUT_I400; |
240 | 9.53k | hdr->ss_hor = hdr->ss_ver = 1; |
241 | 9.53k | hdr->chr = DAV1D_CHR_UNKNOWN; |
242 | 34.5k | } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 && |
243 | 98 | hdr->trc == DAV1D_TRC_SRGB && |
244 | 52 | hdr->mtrx == DAV1D_MC_IDENTITY) |
245 | 22 | { |
246 | 22 | hdr->layout = DAV1D_PIXEL_LAYOUT_I444; |
247 | 22 | hdr->color_range = 1; |
248 | 22 | if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2)) |
249 | 13 | goto error; |
250 | 34.5k | } else { |
251 | 34.5k | hdr->color_range = dav1d_get_bit(gb); |
252 | 34.5k | switch (hdr->profile) { |
253 | 23.9k | case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420; |
254 | 23.9k | hdr->ss_hor = hdr->ss_ver = 1; |
255 | 23.9k | break; |
256 | 7.95k | case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444; |
257 | 7.95k | break; |
258 | 2.63k | case 2: |
259 | 2.63k | if (hdr->hbd == 2) { |
260 | 933 | hdr->ss_hor = dav1d_get_bit(gb); |
261 | 933 | if (hdr->ss_hor) |
262 | 122 | hdr->ss_ver = dav1d_get_bit(gb); |
263 | 933 | } else |
264 | 1.70k | hdr->ss_hor = 1; |
265 | 2.63k | hdr->layout = hdr->ss_hor ? |
266 | 1.82k | hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 : |
267 | 1.82k | DAV1D_PIXEL_LAYOUT_I422 : |
268 | 2.63k | DAV1D_PIXEL_LAYOUT_I444; |
269 | 2.63k | break; |
270 | 34.5k | } |
271 | 34.5k | hdr->chr = (hdr->ss_hor & hdr->ss_ver) ? |
272 | 23.9k | dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN; |
273 | 34.5k | } |
274 | 44.0k | if (strict_std_compliance && |
275 | 0 | hdr->mtrx == DAV1D_MC_IDENTITY && hdr->layout != DAV1D_PIXEL_LAYOUT_I444) |
276 | 0 | { |
277 | 0 | goto error; |
278 | 0 | } |
279 | 44.0k | if (!hdr->monochrome) |
280 | 34.5k | hdr->separate_uv_delta_q = dav1d_get_bit(gb); |
281 | | #if DEBUG_SEQ_HDR |
282 | | printf("SEQHDR: post-colorinfo: off=%u\n", |
283 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
284 | | #endif |
285 | | |
286 | 44.0k | hdr->film_grain_present = dav1d_get_bit(gb); |
287 | | #if DEBUG_SEQ_HDR |
288 | | printf("SEQHDR: post-filmgrain: off=%u\n", |
289 | | dav1d_get_bits_pos(gb) - init_bit_pos); |
290 | | #endif |
291 | | |
292 | | // We needn't bother flushing the OBU here: we'll check we didn't |
293 | | // overrun in the caller and will then discard gb, so there's no |
294 | | // point in setting its position properly. |
295 | | |
296 | 44.0k | return check_trailing_bits(gb, strict_std_compliance); |
297 | | |
298 | 255 | error: |
299 | 255 | return DAV1D_ERR(EINVAL); |
300 | 44.0k | } |
301 | | |
302 | | int dav1d_parse_sequence_header(Dav1dSequenceHeader *const out, |
303 | | const uint8_t *const ptr, const size_t sz) |
304 | 0 | { |
305 | 0 | validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL)); |
306 | 0 | validate_input_or_ret(ptr != NULL, DAV1D_ERR(EINVAL)); |
307 | 0 | validate_input_or_ret(sz > 0 && sz <= SIZE_MAX / 2, DAV1D_ERR(EINVAL)); |
308 | |
|
309 | 0 | GetBits gb; |
310 | 0 | dav1d_init_get_bits(&gb, ptr, sz); |
311 | 0 | int res = DAV1D_ERR(ENOENT); |
312 | |
|
313 | 0 | do { |
314 | 0 | dav1d_get_bit(&gb); // obu_forbidden_bit |
315 | 0 | const enum Dav1dObuType type = dav1d_get_bits(&gb, 4); |
316 | 0 | const int has_extension = dav1d_get_bit(&gb); |
317 | 0 | const int has_length_field = dav1d_get_bit(&gb); |
318 | 0 | dav1d_get_bits(&gb, 1 + 8 * has_extension); // ignore |
319 | |
|
320 | 0 | const uint8_t *obu_end = gb.ptr_end; |
321 | 0 | if (has_length_field) { |
322 | 0 | const size_t len = dav1d_get_uleb128(&gb); |
323 | 0 | if (len > (size_t)(obu_end - gb.ptr)) return DAV1D_ERR(EINVAL); |
324 | 0 | obu_end = gb.ptr + len; |
325 | 0 | } |
326 | | |
327 | 0 | if (type == DAV1D_OBU_SEQ_HDR) { |
328 | 0 | if ((res = parse_seq_hdr(out, &gb, 0)) < 0) return res; |
329 | 0 | if (gb.ptr > obu_end) return DAV1D_ERR(EINVAL); |
330 | 0 | dav1d_bytealign_get_bits(&gb); |
331 | 0 | } |
332 | | |
333 | 0 | if (gb.error) return DAV1D_ERR(EINVAL); |
334 | 0 | assert(gb.state == 0 && gb.bits_left == 0); |
335 | 0 | gb.ptr = obu_end; |
336 | 0 | } while (gb.ptr < gb.ptr_end); |
337 | | |
338 | 0 | return res; |
339 | 0 | } |
340 | | |
341 | | static int read_frame_size(Dav1dContext *const c, GetBits *const gb, |
342 | | const int use_ref) |
343 | 64.6k | { |
344 | 64.6k | const Dav1dSequenceHeader *const seqhdr = c->seq_hdr; |
345 | 64.6k | Dav1dFrameHeader *const hdr = c->frame_hdr; |
346 | | |
347 | 64.6k | if (use_ref) { |
348 | 15.3k | for (int i = 0; i < 7; i++) { |
349 | 15.1k | if (dav1d_get_bit(gb)) { |
350 | 6.93k | const Dav1dThreadPicture *const ref = |
351 | 6.93k | &c->refs[c->frame_hdr->refidx[i]].p; |
352 | 6.93k | if (!ref->p.frame_hdr) return -1; |
353 | 6.90k | hdr->width[1] = ref->p.frame_hdr->width[1]; |
354 | 6.90k | hdr->height = ref->p.frame_hdr->height; |
355 | 6.90k | if (hdr->width[1] > seqhdr->max_width || |
356 | 6.90k | hdr->height > seqhdr->max_height) |
357 | 0 | { |
358 | 0 | return -1; |
359 | 0 | } |
360 | 6.90k | hdr->render_width = ref->p.frame_hdr->render_width; |
361 | 6.90k | hdr->render_height = ref->p.frame_hdr->render_height; |
362 | 6.90k | hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb); |
363 | 6.90k | if (hdr->super_res.enabled) { |
364 | 1.75k | const int d = hdr->super_res.width_scale_denominator = |
365 | 1.75k | 9 + dav1d_get_bits(gb, 3); |
366 | 1.75k | hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, |
367 | 1.75k | imin(16, hdr->width[1])); |
368 | 5.14k | } else { |
369 | 5.14k | hdr->super_res.width_scale_denominator = 8; |
370 | 5.14k | hdr->width[0] = hdr->width[1]; |
371 | 5.14k | } |
372 | 6.90k | return 0; |
373 | 6.90k | } |
374 | 15.1k | } |
375 | 7.12k | } |
376 | | |
377 | 57.7k | if (hdr->frame_size_override) { |
378 | 6.08k | hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1; |
379 | 6.08k | hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1; |
380 | 6.08k | if (hdr->width[1] > seqhdr->max_width || |
381 | 6.00k | hdr->height > seqhdr->max_height) |
382 | 114 | { |
383 | 114 | return -1; |
384 | 114 | } |
385 | 51.6k | } else { |
386 | 51.6k | hdr->width[1] = seqhdr->max_width; |
387 | 51.6k | hdr->height = seqhdr->max_height; |
388 | 51.6k | } |
389 | 57.6k | hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb); |
390 | 57.6k | if (hdr->super_res.enabled) { |
391 | 16.5k | const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3); |
392 | 16.5k | hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1])); |
393 | 41.1k | } else { |
394 | 41.1k | hdr->super_res.width_scale_denominator = 8; |
395 | 41.1k | hdr->width[0] = hdr->width[1]; |
396 | 41.1k | } |
397 | 57.6k | hdr->have_render_size = dav1d_get_bit(gb); |
398 | 57.6k | if (hdr->have_render_size) { |
399 | 22.2k | hdr->render_width = dav1d_get_bits(gb, 16) + 1; |
400 | 22.2k | hdr->render_height = dav1d_get_bits(gb, 16) + 1; |
401 | 35.3k | } else { |
402 | 35.3k | hdr->render_width = hdr->width[1]; |
403 | 35.3k | hdr->render_height = hdr->height; |
404 | 35.3k | } |
405 | 57.6k | return 0; |
406 | 57.7k | } |
407 | | |
408 | 309k | static inline int tile_log2(const int sz, const int tgt) { |
409 | 309k | int k; |
410 | 428k | for (k = 0; (sz << k) < tgt; k++) ; |
411 | 309k | return k; |
412 | 309k | } |
413 | | |
414 | | static const Dav1dLoopfilterModeRefDeltas default_mode_ref_deltas = { |
415 | | .mode_delta = { 0, 0 }, |
416 | | .ref_delta = { 1, 0, 0, 0, -1, 0, -1, -1 }, |
417 | | }; |
418 | | |
419 | 65.1k | static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) { |
420 | 65.1k | #define DEBUG_FRAME_HDR 0 |
421 | | |
422 | | #if DEBUG_FRAME_HDR |
423 | | const uint8_t *const init_ptr = gb->ptr; |
424 | | #endif |
425 | 65.1k | const Dav1dSequenceHeader *const seqhdr = c->seq_hdr; |
426 | 65.1k | Dav1dFrameHeader *const hdr = c->frame_hdr; |
427 | | |
428 | 65.1k | if (!seqhdr->reduced_still_picture_header) |
429 | 33.5k | hdr->show_existing_frame = dav1d_get_bit(gb); |
430 | | #if DEBUG_FRAME_HDR |
431 | | printf("HDR: post-show_existing_frame: off=%td\n", |
432 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
433 | | #endif |
434 | 65.1k | if (hdr->show_existing_frame) { |
435 | 366 | hdr->existing_frame_idx = dav1d_get_bits(gb, 3); |
436 | 366 | if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval) |
437 | 10 | hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length); |
438 | 366 | if (seqhdr->frame_id_numbers_present) { |
439 | 41 | hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits); |
440 | 41 | Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr; |
441 | 41 | if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) goto error; |
442 | 41 | } |
443 | 333 | return 0; |
444 | 366 | } |
445 | | |
446 | 64.8k | if (seqhdr->reduced_still_picture_header) { |
447 | 31.6k | hdr->frame_type = DAV1D_FRAME_TYPE_KEY; |
448 | 31.6k | hdr->show_frame = 1; |
449 | 33.1k | } else { |
450 | 33.1k | hdr->frame_type = dav1d_get_bits(gb, 2); |
451 | 33.1k | hdr->show_frame = dav1d_get_bit(gb); |
452 | 33.1k | } |
453 | 64.8k | if (hdr->show_frame) { |
454 | 61.3k | if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval) |
455 | 108 | hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length); |
456 | 61.3k | hdr->showable_frame = hdr->frame_type != DAV1D_FRAME_TYPE_KEY; |
457 | 61.3k | } else |
458 | 3.50k | hdr->showable_frame = dav1d_get_bit(gb); |
459 | 64.8k | hdr->error_resilient_mode = |
460 | 64.8k | (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) || |
461 | 19.1k | hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH || |
462 | 18.4k | seqhdr->reduced_still_picture_header || dav1d_get_bit(gb); |
463 | | #if DEBUG_FRAME_HDR |
464 | | printf("HDR: post-frametype_bits: off=%td\n", |
465 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
466 | | #endif |
467 | 64.8k | hdr->disable_cdf_update = dav1d_get_bit(gb); |
468 | 64.8k | hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ? |
469 | 39.5k | dav1d_get_bit(gb) : seqhdr->screen_content_tools; |
470 | 64.8k | if (hdr->allow_screen_content_tools) |
471 | 21.0k | hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ? |
472 | 17.8k | dav1d_get_bit(gb) : seqhdr->force_integer_mv; |
473 | | |
474 | 64.8k | if (IS_KEY_OR_INTRA(hdr)) |
475 | 46.5k | hdr->force_integer_mv = 1; |
476 | | |
477 | 64.8k | if (seqhdr->frame_id_numbers_present) |
478 | 662 | hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits); |
479 | | |
480 | 64.8k | if (!seqhdr->reduced_still_picture_header) |
481 | 33.1k | hdr->frame_size_override = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 1 : dav1d_get_bit(gb); |
482 | | #if DEBUG_FRAME_HDR |
483 | | printf("HDR: post-frame_size_override_flag: off=%td\n", |
484 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
485 | | #endif |
486 | 64.8k | if (seqhdr->order_hint) |
487 | 10.6k | hdr->frame_offset = dav1d_get_bits(gb, seqhdr->order_hint_n_bits); |
488 | 64.8k | hdr->primary_ref_frame = !hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) ? |
489 | 47.8k | dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE; |
490 | | |
491 | 64.8k | if (seqhdr->decoder_model_info_present) { |
492 | 223 | hdr->buffer_removal_time_present = dav1d_get_bit(gb); |
493 | 223 | if (hdr->buffer_removal_time_present) { |
494 | 932 | for (int i = 0; i < c->seq_hdr->num_operating_points; i++) { |
495 | 789 | const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i]; |
496 | 789 | struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i]; |
497 | 789 | if (seqop->decoder_model_param_present) { |
498 | 290 | int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1; |
499 | 290 | int in_spatial_layer = (seqop->idc >> (hdr->spatial_id + 8)) & 1; |
500 | 290 | if (!seqop->idc || (in_temporal_layer && in_spatial_layer)) |
501 | 108 | op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length); |
502 | 290 | } |
503 | 789 | } |
504 | 143 | } |
505 | 223 | } |
506 | | |
507 | 64.8k | if (IS_KEY_OR_INTRA(hdr)) { |
508 | 46.5k | hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && |
509 | 46.3k | hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8); |
510 | 46.5k | if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint) |
511 | 1.25k | for (int i = 0; i < 8; i++) |
512 | 1.11k | dav1d_get_bits(gb, seqhdr->order_hint_n_bits); |
513 | 46.5k | if (c->strict_std_compliance && |
514 | 0 | hdr->frame_type == DAV1D_FRAME_TYPE_INTRA && hdr->refresh_frame_flags == 0xff) |
515 | 0 | { |
516 | 0 | goto error; |
517 | 0 | } |
518 | 46.5k | if (read_frame_size(c, gb, 0) < 0) goto error; |
519 | 46.4k | if (hdr->allow_screen_content_tools && !hdr->super_res.enabled) |
520 | 13.6k | hdr->allow_intrabc = dav1d_get_bit(gb); |
521 | 46.4k | } else { |
522 | 18.2k | hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff : |
523 | 18.2k | dav1d_get_bits(gb, 8); |
524 | 18.2k | if (hdr->error_resilient_mode && seqhdr->order_hint) |
525 | 2.52k | for (int i = 0; i < 8; i++) |
526 | 2.24k | dav1d_get_bits(gb, seqhdr->order_hint_n_bits); |
527 | 18.2k | if (seqhdr->order_hint) { |
528 | 6.69k | hdr->frame_ref_short_signaling = dav1d_get_bit(gb); |
529 | 6.69k | if (hdr->frame_ref_short_signaling) { |
530 | 3.94k | hdr->refidx[0] = dav1d_get_bits(gb, 3); |
531 | 3.94k | hdr->refidx[1] = hdr->refidx[2] = -1; |
532 | 3.94k | hdr->refidx[3] = dav1d_get_bits(gb, 3); |
533 | | |
534 | | /* +1 allows for unconditional stores, as unused |
535 | | * values can be dumped into frame_offset[-1]. */ |
536 | 3.94k | int frame_offset_mem[8+1]; |
537 | 3.94k | int *const frame_offset = &frame_offset_mem[1]; |
538 | 3.94k | int earliest_ref = -1; |
539 | 35.2k | for (int i = 0, earliest_offset = INT_MAX; i < 8; i++) { |
540 | 31.3k | const Dav1dFrameHeader *const refhdr = c->refs[i].p.p.frame_hdr; |
541 | 31.3k | if (!refhdr) goto error; |
542 | 31.3k | const int diff = get_poc_diff(seqhdr->order_hint_n_bits, |
543 | 31.3k | refhdr->frame_offset, |
544 | 31.3k | hdr->frame_offset); |
545 | 31.3k | frame_offset[i] = diff; |
546 | 31.3k | if (diff < earliest_offset) { |
547 | 4.96k | earliest_offset = diff; |
548 | 4.96k | earliest_ref = i; |
549 | 4.96k | } |
550 | 31.3k | } |
551 | 3.90k | frame_offset[hdr->refidx[0]] = INT_MIN; // = reference frame is used |
552 | 3.90k | frame_offset[hdr->refidx[3]] = INT_MIN; |
553 | 3.90k | assert(earliest_ref >= 0); |
554 | | |
555 | 3.90k | int refidx = -1; |
556 | 35.1k | for (int i = 0, latest_offset = 0; i < 8; i++) { |
557 | 31.2k | const int hint = frame_offset[i]; |
558 | 31.2k | if (hint >= latest_offset) { |
559 | 9.39k | latest_offset = hint; |
560 | 9.39k | refidx = i; |
561 | 9.39k | } |
562 | 31.2k | } |
563 | 3.90k | frame_offset[refidx] = INT_MIN; |
564 | 3.90k | hdr->refidx[6] = refidx; |
565 | | |
566 | 11.7k | for (int i = 4; i < 6; i++) { |
567 | | /* Unsigned compares to handle negative values. */ |
568 | 7.81k | unsigned earliest_offset = UINT8_MAX; |
569 | 7.81k | refidx = -1; |
570 | 70.3k | for (int j = 0; j < 8; j++) { |
571 | 62.5k | const unsigned hint = frame_offset[j]; |
572 | 62.5k | if (hint < earliest_offset) { |
573 | 4.76k | earliest_offset = hint; |
574 | 4.76k | refidx = j; |
575 | 4.76k | } |
576 | 62.5k | } |
577 | 7.81k | frame_offset[refidx] = INT_MIN; |
578 | 7.81k | hdr->refidx[i] = refidx; |
579 | 7.81k | } |
580 | | |
581 | 27.3k | for (int i = 1; i < 7; i++) { |
582 | 23.4k | refidx = hdr->refidx[i]; |
583 | 23.4k | if (refidx < 0) { |
584 | 13.2k | unsigned latest_offset = ~UINT8_MAX; |
585 | 119k | for (int j = 0; j < 8; j++) { |
586 | 106k | const unsigned hint = frame_offset[j]; |
587 | 106k | if (hint >= latest_offset) { |
588 | 40.7k | latest_offset = hint; |
589 | 40.7k | refidx = j; |
590 | 40.7k | } |
591 | 106k | } |
592 | 13.2k | frame_offset[refidx] = INT_MIN; |
593 | 13.2k | hdr->refidx[i] = refidx >= 0 ? refidx : earliest_ref; |
594 | 13.2k | } |
595 | 23.4k | } |
596 | 3.90k | } |
597 | 6.69k | } |
598 | 145k | for (int i = 0; i < 7; i++) { |
599 | 127k | if (!hdr->frame_ref_short_signaling) |
600 | 100k | hdr->refidx[i] = dav1d_get_bits(gb, 3); |
601 | 127k | if (seqhdr->frame_id_numbers_present) { |
602 | 199 | const unsigned delta_ref_frame_id = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits) + 1; |
603 | 199 | const unsigned ref_frame_id = (hdr->frame_id + (1 << seqhdr->frame_id_n_bits) - delta_ref_frame_id) & ((1 << seqhdr->frame_id_n_bits) - 1); |
604 | 199 | Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr; |
605 | 199 | if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error; |
606 | 199 | } |
607 | 127k | } |
608 | 18.1k | const int use_ref = !hdr->error_resilient_mode && |
609 | 16.8k | hdr->frame_size_override; |
610 | 18.1k | if (read_frame_size(c, gb, use_ref) < 0) goto error; |
611 | 18.0k | if (!hdr->force_integer_mv) |
612 | 15.1k | hdr->hp = dav1d_get_bit(gb); |
613 | 18.0k | hdr->subpel_filter_mode = dav1d_get_bit(gb) ? DAV1D_FILTER_SWITCHABLE : |
614 | 18.0k | dav1d_get_bits(gb, 2); |
615 | 18.0k | hdr->switchable_motion_mode = dav1d_get_bit(gb); |
616 | 18.0k | if (!hdr->error_resilient_mode && seqhdr->ref_frame_mvs && |
617 | 5.20k | seqhdr->order_hint && IS_INTER_OR_SWITCH(hdr)) |
618 | 5.20k | { |
619 | 5.20k | hdr->use_ref_frame_mvs = dav1d_get_bit(gb); |
620 | 5.20k | } |
621 | 18.0k | } |
622 | | #if DEBUG_FRAME_HDR |
623 | | printf("HDR: post-frametype-specific-bits: off=%td\n", |
624 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
625 | | #endif |
626 | | |
627 | 64.5k | if (!seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update) |
628 | 12.5k | hdr->refresh_context = !dav1d_get_bit(gb); |
629 | | #if DEBUG_FRAME_HDR |
630 | | printf("HDR: post-refresh_context: off=%td\n", |
631 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
632 | | #endif |
633 | | |
634 | | // tile data |
635 | 64.5k | hdr->tiling.uniform = dav1d_get_bit(gb); |
636 | 64.5k | const int sbsz_min1 = (64 << seqhdr->sb128) - 1; |
637 | 64.5k | const int sbsz_log2 = 6 + seqhdr->sb128; |
638 | 64.5k | const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2; |
639 | 64.5k | const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2; |
640 | 64.5k | const int max_tile_width_sb = 4096 >> sbsz_log2; |
641 | 64.5k | const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2); |
642 | 64.5k | hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw); |
643 | 64.5k | hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS)); |
644 | 64.5k | hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS)); |
645 | 64.5k | const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh), |
646 | 64.5k | hdr->tiling.min_log2_cols); |
647 | 64.5k | if (hdr->tiling.uniform) { |
648 | 38.8k | for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols; |
649 | 40.9k | hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bit(gb); |
650 | 38.8k | hdr->tiling.log2_cols++) ; |
651 | 38.8k | const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols); |
652 | 38.8k | hdr->tiling.cols = 0; |
653 | 83.4k | for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++) |
654 | 44.6k | hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx; |
655 | 38.8k | hdr->tiling.min_log2_rows = |
656 | 38.8k | imax(min_log2_tiles - hdr->tiling.log2_cols, 0); |
657 | | |
658 | 38.8k | for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows; |
659 | 40.5k | hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bit(gb); |
660 | 38.8k | hdr->tiling.log2_rows++) ; |
661 | 38.8k | const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows); |
662 | 38.8k | hdr->tiling.rows = 0; |
663 | 81.0k | for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++) |
664 | 42.1k | hdr->tiling.row_start_sb[hdr->tiling.rows] = sby; |
665 | 38.8k | } else { |
666 | 25.6k | hdr->tiling.cols = 0; |
667 | 25.6k | int widest_tile = 0, max_tile_area_sb = sbw * sbh; |
668 | 58.5k | for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) { |
669 | 32.8k | const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb); |
670 | 32.8k | const int tile_w = (tile_width_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_width_sb) : 1; |
671 | 32.8k | hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx; |
672 | 32.8k | sbx += tile_w; |
673 | 32.8k | widest_tile = imax(widest_tile, tile_w); |
674 | 32.8k | } |
675 | 25.6k | hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols); |
676 | 25.6k | if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1; |
677 | 25.6k | const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1); |
678 | | |
679 | 25.6k | hdr->tiling.rows = 0; |
680 | 57.4k | for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) { |
681 | 31.8k | const int tile_height_sb = imin(sbh - sby, max_tile_height_sb); |
682 | 31.8k | const int tile_h = (tile_height_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_height_sb) : 1; |
683 | 31.8k | hdr->tiling.row_start_sb[hdr->tiling.rows] = sby; |
684 | 31.8k | sby += tile_h; |
685 | 31.8k | } |
686 | 25.6k | hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows); |
687 | 25.6k | } |
688 | 64.5k | hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw; |
689 | 64.5k | hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh; |
690 | 64.5k | if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) { |
691 | 6.54k | hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols + hdr->tiling.log2_rows); |
692 | 6.54k | if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows) |
693 | 53 | goto error; |
694 | 6.49k | hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1; |
695 | 6.49k | } |
696 | | #if DEBUG_FRAME_HDR |
697 | | printf("HDR: post-tiling: off=%td\n", |
698 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
699 | | #endif |
700 | | |
701 | | // quant data |
702 | 64.4k | hdr->quant.yac = dav1d_get_bits(gb, 8); |
703 | 64.4k | if (dav1d_get_bit(gb)) |
704 | 31.2k | hdr->quant.ydc_delta = dav1d_get_sbits(gb, 7); |
705 | 64.4k | if (!seqhdr->monochrome) { |
706 | | // If the sequence header says that delta_q might be different |
707 | | // for U, V, we must check whether it actually is for this |
708 | | // frame. |
709 | 48.6k | const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bit(gb) : 0; |
710 | 48.6k | if (dav1d_get_bit(gb)) |
711 | 21.7k | hdr->quant.udc_delta = dav1d_get_sbits(gb, 7); |
712 | 48.6k | if (dav1d_get_bit(gb)) |
713 | 21.2k | hdr->quant.uac_delta = dav1d_get_sbits(gb, 7); |
714 | 48.6k | if (diff_uv_delta) { |
715 | 8.90k | if (dav1d_get_bit(gb)) |
716 | 3.93k | hdr->quant.vdc_delta = dav1d_get_sbits(gb, 7); |
717 | 8.90k | if (dav1d_get_bit(gb)) |
718 | 3.94k | hdr->quant.vac_delta = dav1d_get_sbits(gb, 7); |
719 | 39.7k | } else { |
720 | 39.7k | hdr->quant.vdc_delta = hdr->quant.udc_delta; |
721 | 39.7k | hdr->quant.vac_delta = hdr->quant.uac_delta; |
722 | 39.7k | } |
723 | 48.6k | } |
724 | | #if DEBUG_FRAME_HDR |
725 | | printf("HDR: post-quant: off=%td\n", |
726 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
727 | | #endif |
728 | 64.4k | hdr->quant.qm = dav1d_get_bit(gb); |
729 | 64.4k | if (hdr->quant.qm) { |
730 | 29.1k | hdr->quant.qm_y = dav1d_get_bits(gb, 4); |
731 | 29.1k | hdr->quant.qm_u = dav1d_get_bits(gb, 4); |
732 | 29.1k | hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 4) : |
733 | 29.1k | hdr->quant.qm_u; |
734 | 29.1k | } |
735 | | #if DEBUG_FRAME_HDR |
736 | | printf("HDR: post-qm: off=%td\n", |
737 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
738 | | #endif |
739 | | |
740 | | // segmentation data |
741 | 64.4k | hdr->segmentation.enabled = dav1d_get_bit(gb); |
742 | 64.4k | if (hdr->segmentation.enabled) { |
743 | 18.9k | if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) { |
744 | 12.1k | hdr->segmentation.update_map = 1; |
745 | 12.1k | hdr->segmentation.update_data = 1; |
746 | 12.1k | } else { |
747 | 6.84k | hdr->segmentation.update_map = dav1d_get_bit(gb); |
748 | 6.84k | if (hdr->segmentation.update_map) |
749 | 4.19k | hdr->segmentation.temporal = dav1d_get_bit(gb); |
750 | 6.84k | hdr->segmentation.update_data = dav1d_get_bit(gb); |
751 | 6.84k | } |
752 | | |
753 | 18.9k | if (hdr->segmentation.update_data) { |
754 | 14.9k | hdr->segmentation.seg_data.last_active_segid = -1; |
755 | 134k | for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) { |
756 | 119k | Dav1dSegmentationData *const seg = |
757 | 119k | &hdr->segmentation.seg_data.d[i]; |
758 | 119k | if (dav1d_get_bit(gb)) { |
759 | 37.4k | seg->delta_q = dav1d_get_sbits(gb, 9); |
760 | 37.4k | hdr->segmentation.seg_data.last_active_segid = i; |
761 | 37.4k | } |
762 | 119k | if (dav1d_get_bit(gb)) { |
763 | 33.2k | seg->delta_lf_y_v = dav1d_get_sbits(gb, 7); |
764 | 33.2k | hdr->segmentation.seg_data.last_active_segid = i; |
765 | 33.2k | } |
766 | 119k | if (dav1d_get_bit(gb)) { |
767 | 38.7k | seg->delta_lf_y_h = dav1d_get_sbits(gb, 7); |
768 | 38.7k | hdr->segmentation.seg_data.last_active_segid = i; |
769 | 38.7k | } |
770 | 119k | if (dav1d_get_bit(gb)) { |
771 | 37.3k | seg->delta_lf_u = dav1d_get_sbits(gb, 7); |
772 | 37.3k | hdr->segmentation.seg_data.last_active_segid = i; |
773 | 37.3k | } |
774 | 119k | if (dav1d_get_bit(gb)) { |
775 | 35.3k | seg->delta_lf_v = dav1d_get_sbits(gb, 7); |
776 | 35.3k | hdr->segmentation.seg_data.last_active_segid = i; |
777 | 35.3k | } |
778 | 119k | if (dav1d_get_bit(gb)) { |
779 | 35.0k | seg->ref = dav1d_get_bits(gb, 3); |
780 | 35.0k | hdr->segmentation.seg_data.last_active_segid = i; |
781 | 35.0k | hdr->segmentation.seg_data.preskip = 1; |
782 | 84.8k | } else { |
783 | 84.8k | seg->ref = -1; |
784 | 84.8k | } |
785 | 119k | if ((seg->skip = dav1d_get_bit(gb))) { |
786 | 34.9k | hdr->segmentation.seg_data.last_active_segid = i; |
787 | 34.9k | hdr->segmentation.seg_data.preskip = 1; |
788 | 34.9k | } |
789 | 119k | if ((seg->globalmv = dav1d_get_bit(gb))) { |
790 | 36.7k | hdr->segmentation.seg_data.last_active_segid = i; |
791 | 36.7k | hdr->segmentation.seg_data.preskip = 1; |
792 | 36.7k | } |
793 | 119k | } |
794 | 14.9k | } else { |
795 | | // segmentation.update_data was false so we should copy |
796 | | // segmentation data from the reference frame. |
797 | 3.99k | assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE); |
798 | 3.99k | const int pri_ref = hdr->refidx[hdr->primary_ref_frame]; |
799 | 3.99k | if (!c->refs[pri_ref].p.p.frame_hdr) goto error; |
800 | 3.98k | hdr->segmentation.seg_data = |
801 | 3.98k | c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data; |
802 | 3.98k | } |
803 | 45.5k | } else { |
804 | 409k | for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) |
805 | 364k | hdr->segmentation.seg_data.d[i].ref = -1; |
806 | 45.5k | } |
807 | | #if DEBUG_FRAME_HDR |
808 | | printf("HDR: post-segmentation: off=%td\n", |
809 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
810 | | #endif |
811 | | |
812 | | // delta q |
813 | 64.4k | if (hdr->quant.yac) { |
814 | 60.7k | hdr->delta.q.present = dav1d_get_bit(gb); |
815 | 60.7k | if (hdr->delta.q.present) { |
816 | 15.5k | hdr->delta.q.res_log2 = dav1d_get_bits(gb, 2); |
817 | 15.5k | if (!hdr->allow_intrabc) { |
818 | 13.6k | hdr->delta.lf.present = dav1d_get_bit(gb); |
819 | 13.6k | if (hdr->delta.lf.present) { |
820 | 6.43k | hdr->delta.lf.res_log2 = dav1d_get_bits(gb, 2); |
821 | 6.43k | hdr->delta.lf.multi = dav1d_get_bit(gb); |
822 | 6.43k | } |
823 | 13.6k | } |
824 | 15.5k | } |
825 | 60.7k | } |
826 | | #if DEBUG_FRAME_HDR |
827 | | printf("HDR: post-delta_q_lf_flags: off=%td\n", |
828 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
829 | | #endif |
830 | | |
831 | | // derive lossless flags |
832 | 64.4k | const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta && |
833 | 26.4k | !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta; |
834 | 64.4k | hdr->all_lossless = 1; |
835 | 580k | for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) { |
836 | 515k | hdr->segmentation.qidx[i] = hdr->segmentation.enabled ? |
837 | 151k | iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) : |
838 | 515k | hdr->quant.yac; |
839 | 515k | hdr->segmentation.lossless[i] = |
840 | 515k | !hdr->segmentation.qidx[i] && delta_lossless; |
841 | 515k | hdr->all_lossless &= hdr->segmentation.lossless[i]; |
842 | 515k | } |
843 | | |
844 | | // loopfilter |
845 | 64.4k | if (hdr->all_lossless || hdr->allow_intrabc) { |
846 | 12.0k | hdr->loopfilter.mode_ref_delta_enabled = 1; |
847 | 12.0k | hdr->loopfilter.mode_ref_delta_update = 1; |
848 | 12.0k | hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas; |
849 | 52.4k | } else { |
850 | 52.4k | hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6); |
851 | 52.4k | hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6); |
852 | 52.4k | if (!seqhdr->monochrome && |
853 | 39.0k | (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1])) |
854 | 30.9k | { |
855 | 30.9k | hdr->loopfilter.level_u = dav1d_get_bits(gb, 6); |
856 | 30.9k | hdr->loopfilter.level_v = dav1d_get_bits(gb, 6); |
857 | 30.9k | } |
858 | 52.4k | hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3); |
859 | | |
860 | 52.4k | if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) { |
861 | 36.8k | hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas; |
862 | 36.8k | } else { |
863 | 15.5k | const int ref = hdr->refidx[hdr->primary_ref_frame]; |
864 | 15.5k | if (!c->refs[ref].p.p.frame_hdr) goto error; |
865 | 15.5k | hdr->loopfilter.mode_ref_deltas = |
866 | 15.5k | c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas; |
867 | 15.5k | } |
868 | 52.3k | hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bit(gb); |
869 | 52.3k | if (hdr->loopfilter.mode_ref_delta_enabled) { |
870 | 17.9k | hdr->loopfilter.mode_ref_delta_update = dav1d_get_bit(gb); |
871 | 17.9k | if (hdr->loopfilter.mode_ref_delta_update) { |
872 | 87.7k | for (int i = 0; i < 8; i++) |
873 | 77.9k | if (dav1d_get_bit(gb)) |
874 | 39.3k | hdr->loopfilter.mode_ref_deltas.ref_delta[i] = |
875 | 39.3k | dav1d_get_sbits(gb, 7); |
876 | 29.2k | for (int i = 0; i < 2; i++) |
877 | 19.4k | if (dav1d_get_bit(gb)) |
878 | 7.59k | hdr->loopfilter.mode_ref_deltas.mode_delta[i] = |
879 | 7.59k | dav1d_get_sbits(gb, 7); |
880 | 9.74k | } |
881 | 17.9k | } |
882 | 52.3k | } |
883 | | #if DEBUG_FRAME_HDR |
884 | | printf("HDR: post-lpf: off=%td\n", |
885 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
886 | | #endif |
887 | | |
888 | | // cdef |
889 | 64.4k | if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) { |
890 | 27.4k | hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3; |
891 | 27.4k | hdr->cdef.n_bits = dav1d_get_bits(gb, 2); |
892 | 84.3k | for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) { |
893 | 56.9k | hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6); |
894 | 56.9k | if (!seqhdr->monochrome) |
895 | 39.8k | hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6); |
896 | 56.9k | } |
897 | 27.4k | } |
898 | | #if DEBUG_FRAME_HDR |
899 | | printf("HDR: post-cdef: off=%td\n", |
900 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
901 | | #endif |
902 | | |
903 | | // restoration |
904 | 64.4k | if ((!hdr->all_lossless || hdr->super_res.enabled) && |
905 | 62.3k | seqhdr->restoration && !hdr->allow_intrabc) |
906 | 24.8k | { |
907 | 24.8k | hdr->restoration.type[0] = dav1d_get_bits(gb, 2); |
908 | 24.8k | if (!seqhdr->monochrome) { |
909 | 15.8k | hdr->restoration.type[1] = dav1d_get_bits(gb, 2); |
910 | 15.8k | hdr->restoration.type[2] = dav1d_get_bits(gb, 2); |
911 | 15.8k | } |
912 | | |
913 | 24.8k | if (hdr->restoration.type[0] || hdr->restoration.type[1] || |
914 | 9.64k | hdr->restoration.type[2]) |
915 | 16.3k | { |
916 | | // Log2 of the restoration unit size. |
917 | 16.3k | hdr->restoration.unit_size[0] = 6 + seqhdr->sb128; |
918 | 16.3k | if (dav1d_get_bit(gb)) { |
919 | 7.32k | hdr->restoration.unit_size[0]++; |
920 | 7.32k | if (!seqhdr->sb128) |
921 | 4.22k | hdr->restoration.unit_size[0] += dav1d_get_bit(gb); |
922 | 7.32k | } |
923 | 16.3k | hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0]; |
924 | 16.3k | if ((hdr->restoration.type[1] || hdr->restoration.type[2]) && |
925 | 11.2k | seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1) |
926 | 8.39k | { |
927 | 8.39k | hdr->restoration.unit_size[1] -= dav1d_get_bit(gb); |
928 | 8.39k | } |
929 | 16.3k | } else { |
930 | 8.47k | hdr->restoration.unit_size[0] = 8; |
931 | 8.47k | } |
932 | 24.8k | } |
933 | | #if DEBUG_FRAME_HDR |
934 | | printf("HDR: post-restoration: off=%td\n", |
935 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
936 | | #endif |
937 | | |
938 | 64.4k | if (!hdr->all_lossless) |
939 | 62.1k | hdr->txfm_mode = dav1d_get_bit(gb) ? DAV1D_TX_SWITCHABLE : DAV1D_TX_LARGEST; |
940 | | #if DEBUG_FRAME_HDR |
941 | | printf("HDR: post-txfmmode: off=%td\n", |
942 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
943 | | #endif |
944 | 64.4k | if (IS_INTER_OR_SWITCH(hdr)) |
945 | 18.0k | hdr->switchable_comp_refs = dav1d_get_bit(gb); |
946 | | #if DEBUG_FRAME_HDR |
947 | | printf("HDR: post-refmode: off=%td\n", |
948 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
949 | | #endif |
950 | 64.4k | if (hdr->switchable_comp_refs && IS_INTER_OR_SWITCH(hdr) && seqhdr->order_hint) { |
951 | 3.67k | const int poc = hdr->frame_offset; |
952 | 3.67k | int off_before = -1, off_after = -1; |
953 | 3.67k | int off_before_idx, off_after_idx; |
954 | 29.2k | for (int i = 0; i < 7; i++) { |
955 | 25.6k | if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error; |
956 | 25.6k | const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset; |
957 | | |
958 | 25.6k | const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc); |
959 | 25.6k | if (diff > 0) { |
960 | 7.31k | if (off_after < 0 || get_poc_diff(seqhdr->order_hint_n_bits, |
961 | 5.79k | off_after, refpoc) > 0) |
962 | 1.68k | { |
963 | 1.68k | off_after = refpoc; |
964 | 1.68k | off_after_idx = i; |
965 | 1.68k | } |
966 | 18.3k | } else if (diff < 0 && (off_before < 0 || |
967 | 11.0k | get_poc_diff(seqhdr->order_hint_n_bits, |
968 | 11.0k | refpoc, off_before) > 0)) |
969 | 2.81k | { |
970 | 2.81k | off_before = refpoc; |
971 | 2.81k | off_before_idx = i; |
972 | 2.81k | } |
973 | 25.6k | } |
974 | | |
975 | 3.65k | if ((off_before | off_after) >= 0) { |
976 | 738 | hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx); |
977 | 738 | hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx); |
978 | 738 | hdr->skip_mode_allowed = 1; |
979 | 2.92k | } else if (off_before >= 0) { |
980 | 1.86k | int off_before2 = -1; |
981 | 1.86k | int off_before2_idx; |
982 | 14.8k | for (int i = 0; i < 7; i++) { |
983 | 13.0k | if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error; |
984 | 13.0k | const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset; |
985 | 13.0k | if (get_poc_diff(seqhdr->order_hint_n_bits, |
986 | 13.0k | refpoc, off_before) < 0) { |
987 | 2.33k | if (off_before2 < 0 || get_poc_diff(seqhdr->order_hint_n_bits, |
988 | 1.71k | refpoc, off_before2) > 0) |
989 | 647 | { |
990 | 647 | off_before2 = refpoc; |
991 | 647 | off_before2_idx = i; |
992 | 647 | } |
993 | 2.33k | } |
994 | 13.0k | } |
995 | | |
996 | 1.86k | if (off_before2 >= 0) { |
997 | 619 | hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx); |
998 | 619 | hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx); |
999 | 619 | hdr->skip_mode_allowed = 1; |
1000 | 619 | } |
1001 | 1.86k | } |
1002 | 3.65k | } |
1003 | 64.4k | if (hdr->skip_mode_allowed) |
1004 | 1.35k | hdr->skip_mode_enabled = dav1d_get_bit(gb); |
1005 | | #if DEBUG_FRAME_HDR |
1006 | | printf("HDR: post-extskip: off=%td\n", |
1007 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
1008 | | #endif |
1009 | 64.4k | if (!hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) && seqhdr->warped_motion) |
1010 | 8.11k | hdr->warp_motion = dav1d_get_bit(gb); |
1011 | | #if DEBUG_FRAME_HDR |
1012 | | printf("HDR: post-warpmotionbit: off=%td\n", |
1013 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
1014 | | #endif |
1015 | 64.4k | hdr->reduced_txtp_set = dav1d_get_bit(gb); |
1016 | | #if DEBUG_FRAME_HDR |
1017 | | printf("HDR: post-reducedtxtpset: off=%td\n", |
1018 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
1019 | | #endif |
1020 | | |
1021 | 515k | for (int i = 0; i < 7; i++) |
1022 | 451k | hdr->gmv[i] = dav1d_default_wm_params; |
1023 | | |
1024 | 64.4k | if (IS_INTER_OR_SWITCH(hdr)) { |
1025 | 143k | for (int i = 0; i < 7; i++) { |
1026 | 125k | hdr->gmv[i].type = !dav1d_get_bit(gb) ? DAV1D_WM_TYPE_IDENTITY : |
1027 | 125k | dav1d_get_bit(gb) ? DAV1D_WM_TYPE_ROT_ZOOM : |
1028 | 39.7k | dav1d_get_bit(gb) ? DAV1D_WM_TYPE_TRANSLATION : |
1029 | 15.8k | DAV1D_WM_TYPE_AFFINE; |
1030 | | |
1031 | 125k | if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue; |
1032 | | |
1033 | 39.7k | const Dav1dWarpedMotionParams *ref_gmv; |
1034 | 39.7k | if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) { |
1035 | 3.97k | ref_gmv = &dav1d_default_wm_params; |
1036 | 35.7k | } else { |
1037 | 35.7k | const int pri_ref = hdr->refidx[hdr->primary_ref_frame]; |
1038 | 35.7k | if (!c->refs[pri_ref].p.p.frame_hdr) goto error; |
1039 | 35.7k | ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i]; |
1040 | 35.7k | } |
1041 | 39.6k | int32_t *const mat = hdr->gmv[i].matrix; |
1042 | 39.6k | const int32_t *const ref_mat = ref_gmv->matrix; |
1043 | 39.6k | int bits, shift; |
1044 | | |
1045 | 39.6k | if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) { |
1046 | 33.7k | mat[2] = (1 << 16) + 2 * |
1047 | 33.7k | dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12); |
1048 | 33.7k | mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12); |
1049 | | |
1050 | 33.7k | bits = 12; |
1051 | 33.7k | shift = 10; |
1052 | 33.7k | } else { |
1053 | 5.90k | bits = 9 - !hdr->hp; |
1054 | 5.90k | shift = 13 + !hdr->hp; |
1055 | 5.90k | } |
1056 | | |
1057 | 39.6k | if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) { |
1058 | 9.90k | mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12); |
1059 | 9.90k | mat[5] = (1 << 16) + 2 * |
1060 | 9.90k | dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12); |
1061 | 29.7k | } else { |
1062 | 29.7k | mat[4] = -mat[3]; |
1063 | 29.7k | mat[5] = mat[2]; |
1064 | 29.7k | } |
1065 | | |
1066 | 39.6k | mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift); |
1067 | 39.6k | mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift); |
1068 | 39.6k | } |
1069 | 17.9k | } |
1070 | | #if DEBUG_FRAME_HDR |
1071 | | printf("HDR: post-gmv: off=%td\n", |
1072 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
1073 | | #endif |
1074 | | |
1075 | 64.4k | if (seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame)) { |
1076 | 18.7k | hdr->film_grain.present = dav1d_get_bit(gb); |
1077 | 18.7k | if (hdr->film_grain.present) { |
1078 | 4.55k | const unsigned seed = dav1d_get_bits(gb, 16); |
1079 | 4.55k | hdr->film_grain.update = hdr->frame_type != DAV1D_FRAME_TYPE_INTER || dav1d_get_bit(gb); |
1080 | 4.55k | if (!hdr->film_grain.update) { |
1081 | 718 | const int refidx = dav1d_get_bits(gb, 3); |
1082 | 718 | int i; |
1083 | 2.16k | for (i = 0; i < 7; i++) |
1084 | 2.14k | if (hdr->refidx[i] == refidx) |
1085 | 702 | break; |
1086 | 718 | if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error; |
1087 | 690 | hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data; |
1088 | 690 | hdr->film_grain.data.seed = seed; |
1089 | 3.83k | } else { |
1090 | 3.83k | Dav1dFilmGrainData *const fgd = &hdr->film_grain.data; |
1091 | 3.83k | fgd->seed = seed; |
1092 | | |
1093 | 3.83k | fgd->num_y_points = dav1d_get_bits(gb, 4); |
1094 | 3.83k | if (fgd->num_y_points > 14) goto error; |
1095 | 6.28k | for (int i = 0; i < fgd->num_y_points; i++) { |
1096 | 2.53k | fgd->y_points[i][0] = dav1d_get_bits(gb, 8); |
1097 | 2.53k | if (i && fgd->y_points[i - 1][0] >= fgd->y_points[i][0]) |
1098 | 71 | goto error; |
1099 | 2.46k | fgd->y_points[i][1] = dav1d_get_bits(gb, 8); |
1100 | 2.46k | } |
1101 | | |
1102 | 3.74k | if (!seqhdr->monochrome) |
1103 | 3.16k | fgd->chroma_scaling_from_luma = dav1d_get_bit(gb); |
1104 | 3.74k | if (seqhdr->monochrome || fgd->chroma_scaling_from_luma || |
1105 | 2.00k | (seqhdr->ss_ver == 1 && seqhdr->ss_hor == 1 && !fgd->num_y_points)) |
1106 | 2.29k | { |
1107 | 2.29k | fgd->num_uv_points[0] = fgd->num_uv_points[1] = 0; |
1108 | 4.26k | } else for (int pl = 0; pl < 2; pl++) { |
1109 | 2.86k | fgd->num_uv_points[pl] = dav1d_get_bits(gb, 4); |
1110 | 2.86k | if (fgd->num_uv_points[pl] > 10) goto error; |
1111 | 4.93k | for (int i = 0; i < fgd->num_uv_points[pl]; i++) { |
1112 | 2.11k | fgd->uv_points[pl][i][0] = dav1d_get_bits(gb, 8); |
1113 | 2.11k | if (i && fgd->uv_points[pl][i - 1][0] >= fgd->uv_points[pl][i][0]) |
1114 | 44 | goto error; |
1115 | 2.07k | fgd->uv_points[pl][i][1] = dav1d_get_bits(gb, 8); |
1116 | 2.07k | } |
1117 | 2.85k | } |
1118 | | |
1119 | 3.69k | if (seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1 && |
1120 | 1.93k | !!fgd->num_uv_points[0] != !!fgd->num_uv_points[1]) |
1121 | 10 | { |
1122 | 10 | goto error; |
1123 | 10 | } |
1124 | | |
1125 | 3.68k | fgd->scaling_shift = dav1d_get_bits(gb, 2) + 8; |
1126 | 3.68k | fgd->ar_coeff_lag = dav1d_get_bits(gb, 2); |
1127 | 3.68k | const int num_y_pos = 2 * fgd->ar_coeff_lag * (fgd->ar_coeff_lag + 1); |
1128 | 3.68k | if (fgd->num_y_points) |
1129 | 14.3k | for (int i = 0; i < num_y_pos; i++) |
1130 | 12.7k | fgd->ar_coeffs_y[i] = dav1d_get_bits(gb, 8) - 128; |
1131 | 11.0k | for (int pl = 0; pl < 2; pl++) |
1132 | 7.36k | if (fgd->num_uv_points[pl] || fgd->chroma_scaling_from_luma) { |
1133 | 3.69k | const int num_uv_pos = num_y_pos + !!fgd->num_y_points; |
1134 | 35.0k | for (int i = 0; i < num_uv_pos; i++) |
1135 | 31.3k | fgd->ar_coeffs_uv[pl][i] = dav1d_get_bits(gb, 8) - 128; |
1136 | 3.69k | if (!fgd->num_y_points) |
1137 | 1.61k | fgd->ar_coeffs_uv[pl][num_uv_pos] = 0; |
1138 | 3.69k | } |
1139 | 3.68k | fgd->ar_coeff_shift = dav1d_get_bits(gb, 2) + 6; |
1140 | 3.68k | fgd->grain_scale_shift = dav1d_get_bits(gb, 2); |
1141 | 11.0k | for (int pl = 0; pl < 2; pl++) |
1142 | 7.36k | if (fgd->num_uv_points[pl]) { |
1143 | 1.36k | fgd->uv_mult[pl] = dav1d_get_bits(gb, 8) - 128; |
1144 | 1.36k | fgd->uv_luma_mult[pl] = dav1d_get_bits(gb, 8) - 128; |
1145 | 1.36k | fgd->uv_offset[pl] = dav1d_get_bits(gb, 9) - 256; |
1146 | 1.36k | } |
1147 | 3.68k | fgd->overlap_flag = dav1d_get_bit(gb); |
1148 | 3.68k | fgd->clip_to_restricted_range = dav1d_get_bit(gb); |
1149 | 3.68k | } |
1150 | 4.55k | } |
1151 | 18.7k | } |
1152 | | #if DEBUG_FRAME_HDR |
1153 | | printf("HDR: post-filmgrain: off=%td\n", |
1154 | | (gb->ptr - init_ptr) * 8 - gb->bits_left); |
1155 | | #endif |
1156 | | |
1157 | 64.2k | return 0; |
1158 | | |
1159 | 612 | error: |
1160 | 612 | dav1d_log(c, "Error parsing frame header\n"); |
1161 | 612 | return DAV1D_ERR(EINVAL); |
1162 | 64.4k | } |
1163 | | |
1164 | 62.5k | static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) { |
1165 | 62.5k | const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows; |
1166 | 62.5k | const int have_tile_pos = n_tiles > 1 ? dav1d_get_bit(gb) : 0; |
1167 | | |
1168 | 62.5k | if (have_tile_pos) { |
1169 | 342 | const int n_bits = c->frame_hdr->tiling.log2_cols + |
1170 | 342 | c->frame_hdr->tiling.log2_rows; |
1171 | 342 | c->tile[c->n_tile_data].start = dav1d_get_bits(gb, n_bits); |
1172 | 342 | c->tile[c->n_tile_data].end = dav1d_get_bits(gb, n_bits); |
1173 | 62.2k | } else { |
1174 | 62.2k | c->tile[c->n_tile_data].start = 0; |
1175 | 62.2k | c->tile[c->n_tile_data].end = n_tiles - 1; |
1176 | 62.2k | } |
1177 | 62.5k | } |
1178 | | |
1179 | 134k | ptrdiff_t dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in) { |
1180 | 134k | GetBits gb; |
1181 | 134k | int res; |
1182 | | |
1183 | 134k | dav1d_init_get_bits(&gb, in->data, in->sz); |
1184 | | |
1185 | | // obu header |
1186 | 134k | const int obu_forbidden_bit = dav1d_get_bit(&gb); |
1187 | 134k | if (c->strict_std_compliance && obu_forbidden_bit) goto error; |
1188 | 134k | const enum Dav1dObuType type = dav1d_get_bits(&gb, 4); |
1189 | 134k | const int has_extension = dav1d_get_bit(&gb); |
1190 | 134k | const int has_length_field = dav1d_get_bit(&gb); |
1191 | 134k | dav1d_get_bit(&gb); // reserved |
1192 | | |
1193 | 134k | int temporal_id = 0, spatial_id = 0; |
1194 | 134k | if (has_extension) { |
1195 | 65.1k | temporal_id = dav1d_get_bits(&gb, 3); |
1196 | 65.1k | spatial_id = dav1d_get_bits(&gb, 2); |
1197 | 65.1k | dav1d_get_bits(&gb, 3); // reserved |
1198 | 65.1k | } |
1199 | | |
1200 | 134k | if (has_length_field) { |
1201 | 134k | const size_t len = dav1d_get_uleb128(&gb); |
1202 | 134k | if (len > (size_t)(gb.ptr_end - gb.ptr)) goto error; |
1203 | 134k | gb.ptr_end = gb.ptr + len; |
1204 | 134k | } |
1205 | 134k | if (gb.error) goto error; |
1206 | | |
1207 | | // We must have read a whole number of bytes at this point (1 byte |
1208 | | // for the header and whole bytes at a time when reading the |
1209 | | // leb128 length field). |
1210 | 134k | assert(gb.bits_left == 0); |
1211 | | |
1212 | | // skip obu not belonging to the selected temporal/spatial layer |
1213 | 134k | if (type != DAV1D_OBU_SEQ_HDR && type != DAV1D_OBU_TD && |
1214 | 88.0k | has_extension && c->operating_point_idc != 0) |
1215 | 10.8k | { |
1216 | 10.8k | const int in_temporal_layer = (c->operating_point_idc >> temporal_id) & 1; |
1217 | 10.8k | const int in_spatial_layer = (c->operating_point_idc >> (spatial_id + 8)) & 1; |
1218 | 10.8k | if (!in_temporal_layer || !in_spatial_layer) |
1219 | 4.66k | return gb.ptr_end - gb.ptr_start; |
1220 | 10.8k | } |
1221 | | |
1222 | 130k | switch (type) { |
1223 | 44.3k | case DAV1D_OBU_SEQ_HDR: { |
1224 | 44.3k | Dav1dRef *ref = dav1d_ref_create_using_pool(c->seq_hdr_pool, |
1225 | 44.3k | sizeof(Dav1dSequenceHeader)); |
1226 | 44.3k | if (!ref) return DAV1D_ERR(ENOMEM); |
1227 | 44.3k | Dav1dSequenceHeader *seq_hdr = ref->data; |
1228 | 44.3k | if ((res = parse_seq_hdr(seq_hdr, &gb, c->strict_std_compliance)) < 0) { |
1229 | 329 | dav1d_log(c, "Error parsing sequence header\n"); |
1230 | 329 | dav1d_ref_dec(&ref); |
1231 | 329 | goto error; |
1232 | 329 | } |
1233 | | |
1234 | 43.9k | const int op_idx = |
1235 | 43.9k | c->operating_point < seq_hdr->num_operating_points ? c->operating_point : 0; |
1236 | 43.9k | c->operating_point_idc = seq_hdr->operating_points[op_idx].idc; |
1237 | 43.9k | const unsigned spatial_mask = c->operating_point_idc >> 8; |
1238 | 43.9k | c->max_spatial_id = spatial_mask ? ulog2(spatial_mask) : 0; |
1239 | | |
1240 | | // If we have read a sequence header which is different from |
1241 | | // the old one, this is a new video sequence and can't use any |
1242 | | // previous state. Free that state. |
1243 | | |
1244 | 43.9k | if (!c->seq_hdr) { |
1245 | 42.7k | c->frame_hdr = NULL; |
1246 | 42.7k | c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE; |
1247 | | // see 7.5, operating_parameter_info is allowed to change in |
1248 | | // sequence headers of a single sequence |
1249 | 42.7k | } else if (memcmp(seq_hdr, c->seq_hdr, offsetof(Dav1dSequenceHeader, operating_parameter_info))) { |
1250 | 1.06k | c->frame_hdr = NULL; |
1251 | 1.06k | c->mastering_display = NULL; |
1252 | 1.06k | c->content_light = NULL; |
1253 | 1.06k | dav1d_ref_dec(&c->mastering_display_ref); |
1254 | 1.06k | dav1d_ref_dec(&c->content_light_ref); |
1255 | 9.61k | for (int i = 0; i < 8; i++) { |
1256 | 8.54k | if (c->refs[i].p.p.frame_hdr) |
1257 | 3.63k | dav1d_thread_picture_unref(&c->refs[i].p); |
1258 | 8.54k | dav1d_ref_dec(&c->refs[i].segmap); |
1259 | 8.54k | dav1d_ref_dec(&c->refs[i].refmvs); |
1260 | 8.54k | dav1d_cdf_thread_unref(&c->cdf[i]); |
1261 | 8.54k | } |
1262 | 1.06k | c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE; |
1263 | | // If operating_parameter_info changed, signal it |
1264 | 1.06k | } else if (memcmp(seq_hdr->operating_parameter_info, c->seq_hdr->operating_parameter_info, |
1265 | 193 | sizeof(seq_hdr->operating_parameter_info))) |
1266 | 0 | { |
1267 | 0 | c->frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO; |
1268 | 0 | } |
1269 | 43.9k | dav1d_ref_dec(&c->seq_hdr_ref); |
1270 | 43.9k | c->seq_hdr_ref = ref; |
1271 | 43.9k | c->seq_hdr = seq_hdr; |
1272 | 43.9k | break; |
1273 | 44.3k | } |
1274 | 488 | case DAV1D_OBU_REDUNDANT_FRAME_HDR: |
1275 | 488 | if (c->frame_hdr) break; |
1276 | | // fall-through |
1277 | 63.5k | case DAV1D_OBU_FRAME: |
1278 | 65.2k | case DAV1D_OBU_FRAME_HDR: |
1279 | 65.2k | if (!c->seq_hdr) goto error; |
1280 | 65.1k | if (!c->frame_hdr_ref) { |
1281 | 63.7k | c->frame_hdr_ref = dav1d_ref_create_using_pool(c->frame_hdr_pool, |
1282 | 63.7k | sizeof(Dav1dFrameHeader)); |
1283 | 63.7k | if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM); |
1284 | 63.7k | } |
1285 | | #ifndef NDEBUG |
1286 | | // ensure that the reference is writable |
1287 | | assert(dav1d_ref_is_writable(c->frame_hdr_ref)); |
1288 | | #endif |
1289 | 65.1k | c->frame_hdr = c->frame_hdr_ref->data; |
1290 | 65.1k | memset(c->frame_hdr, 0, sizeof(*c->frame_hdr)); |
1291 | 65.1k | c->frame_hdr->temporal_id = temporal_id; |
1292 | 65.1k | c->frame_hdr->spatial_id = spatial_id; |
1293 | 65.1k | if ((res = parse_frame_hdr(c, &gb)) < 0) { |
1294 | 612 | c->frame_hdr = NULL; |
1295 | 612 | goto error; |
1296 | 612 | } |
1297 | 64.6k | for (int n = 0; n < c->n_tile_data; n++) |
1298 | 94 | dav1d_data_unref_internal(&c->tile[n].data); |
1299 | 64.5k | c->n_tile_data = 0; |
1300 | 64.5k | c->n_tiles = 0; |
1301 | 64.5k | if (type != DAV1D_OBU_FRAME) { |
1302 | | // This is actually a frame header OBU so read the |
1303 | | // trailing bit and check for overrun. |
1304 | 2.04k | if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) { |
1305 | 185 | c->frame_hdr = NULL; |
1306 | 185 | goto error; |
1307 | 185 | } |
1308 | 2.04k | } |
1309 | | |
1310 | 64.3k | if (c->frame_size_limit && (int64_t)c->frame_hdr->width[1] * |
1311 | 64.3k | c->frame_hdr->height > c->frame_size_limit) |
1312 | 0 | { |
1313 | 0 | dav1d_log(c, "Frame size %dx%d exceeds limit %u\n", c->frame_hdr->width[1], |
1314 | 0 | c->frame_hdr->height, c->frame_size_limit); |
1315 | 0 | c->frame_hdr = NULL; |
1316 | 0 | return DAV1D_ERR(ERANGE); |
1317 | 0 | } |
1318 | | |
1319 | 64.3k | if (type != DAV1D_OBU_FRAME) |
1320 | 1.85k | break; |
1321 | | // OBU_FRAMEs shouldn't be signaled with show_existing_frame |
1322 | 62.5k | if (c->frame_hdr->show_existing_frame) { |
1323 | 20 | c->frame_hdr = NULL; |
1324 | 20 | goto error; |
1325 | 20 | } |
1326 | | |
1327 | | // This is the frame header at the start of a frame OBU. |
1328 | | // There's no trailing bit at the end to skip, but we do need |
1329 | | // to align to the next byte. |
1330 | 62.5k | dav1d_bytealign_get_bits(&gb); |
1331 | | // fall-through |
1332 | 62.6k | case DAV1D_OBU_TILE_GRP: { |
1333 | 62.6k | if (!c->frame_hdr) goto error; |
1334 | 62.5k | if (c->n_tile_data_alloc < c->n_tile_data + 1) { |
1335 | 41.7k | if ((c->n_tile_data + 1) > INT_MAX / (int)sizeof(*c->tile)) goto error; |
1336 | 41.7k | struct Dav1dTileGroup *tile = dav1d_realloc(ALLOC_TILE, c->tile, |
1337 | 41.7k | (c->n_tile_data + 1) * sizeof(*c->tile)); |
1338 | 41.7k | if (!tile) goto error; |
1339 | 41.7k | c->tile = tile; |
1340 | 41.7k | memset(c->tile + c->n_tile_data, 0, sizeof(*c->tile)); |
1341 | 41.7k | c->n_tile_data_alloc = c->n_tile_data + 1; |
1342 | 41.7k | } |
1343 | 62.5k | parse_tile_hdr(c, &gb); |
1344 | | // Align to the next byte boundary and check for overrun. |
1345 | 62.5k | dav1d_bytealign_get_bits(&gb); |
1346 | 62.5k | if (gb.error) goto error; |
1347 | | |
1348 | 61.9k | dav1d_data_ref(&c->tile[c->n_tile_data].data, in); |
1349 | 61.9k | c->tile[c->n_tile_data].data.data = gb.ptr; |
1350 | 61.9k | c->tile[c->n_tile_data].data.sz = (size_t)(gb.ptr_end - gb.ptr); |
1351 | | // ensure tile groups are in order and sane, see 6.10.1 |
1352 | 61.9k | if (c->tile[c->n_tile_data].start > c->tile[c->n_tile_data].end || |
1353 | 61.9k | c->tile[c->n_tile_data].start != c->n_tiles) |
1354 | 83 | { |
1355 | 189 | for (int i = 0; i <= c->n_tile_data; i++) |
1356 | 106 | dav1d_data_unref_internal(&c->tile[i].data); |
1357 | 83 | c->n_tile_data = 0; |
1358 | 83 | c->n_tiles = 0; |
1359 | 83 | goto error; |
1360 | 83 | } |
1361 | 61.8k | c->n_tiles += 1 + c->tile[c->n_tile_data].end - |
1362 | 61.8k | c->tile[c->n_tile_data].start; |
1363 | 61.8k | c->n_tile_data++; |
1364 | 61.8k | break; |
1365 | 61.9k | } |
1366 | 6.15k | case DAV1D_OBU_METADATA: { |
1367 | 6.15k | #define DEBUG_OBU_METADATA 0 |
1368 | | #if DEBUG_OBU_METADATA |
1369 | | const uint8_t *const init_ptr = gb.ptr; |
1370 | | #endif |
1371 | | // obu metadta type field |
1372 | 6.15k | const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb); |
1373 | 6.15k | if (gb.error) goto error; |
1374 | | |
1375 | 6.07k | switch (meta_type) { |
1376 | 2.88k | case OBU_META_HDR_CLL: { |
1377 | 2.88k | Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META, |
1378 | 2.88k | sizeof(Dav1dContentLightLevel)); |
1379 | 2.88k | if (!ref) return DAV1D_ERR(ENOMEM); |
1380 | 2.88k | Dav1dContentLightLevel *const content_light = ref->data; |
1381 | | |
1382 | 2.88k | content_light->max_content_light_level = dav1d_get_bits(&gb, 16); |
1383 | | #if DEBUG_OBU_METADATA |
1384 | | printf("CLLOBU: max-content-light-level: %d [off=%td]\n", |
1385 | | content_light->max_content_light_level, |
1386 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1387 | | #endif |
1388 | 2.88k | content_light->max_frame_average_light_level = dav1d_get_bits(&gb, 16); |
1389 | | #if DEBUG_OBU_METADATA |
1390 | | printf("CLLOBU: max-frame-average-light-level: %d [off=%td]\n", |
1391 | | content_light->max_frame_average_light_level, |
1392 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1393 | | #endif |
1394 | | |
1395 | 2.88k | if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) { |
1396 | 16 | dav1d_ref_dec(&ref); |
1397 | 16 | goto error; |
1398 | 16 | } |
1399 | | |
1400 | 2.87k | dav1d_ref_dec(&c->content_light_ref); |
1401 | 2.87k | c->content_light = content_light; |
1402 | 2.87k | c->content_light_ref = ref; |
1403 | 2.87k | break; |
1404 | 2.88k | } |
1405 | 76 | case OBU_META_HDR_MDCV: { |
1406 | 76 | Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META, |
1407 | 76 | sizeof(Dav1dMasteringDisplay)); |
1408 | 76 | if (!ref) return DAV1D_ERR(ENOMEM); |
1409 | 76 | Dav1dMasteringDisplay *const mastering_display = ref->data; |
1410 | | |
1411 | 304 | for (int i = 0; i < 3; i++) { |
1412 | 228 | mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16); |
1413 | 228 | mastering_display->primaries[i][1] = dav1d_get_bits(&gb, 16); |
1414 | | #if DEBUG_OBU_METADATA |
1415 | | printf("MDCVOBU: primaries[%d]: (%d, %d) [off=%td]\n", i, |
1416 | | mastering_display->primaries[i][0], |
1417 | | mastering_display->primaries[i][1], |
1418 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1419 | | #endif |
1420 | 228 | } |
1421 | 76 | mastering_display->white_point[0] = dav1d_get_bits(&gb, 16); |
1422 | | #if DEBUG_OBU_METADATA |
1423 | | printf("MDCVOBU: white-point-x: %d [off=%td]\n", |
1424 | | mastering_display->white_point[0], |
1425 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1426 | | #endif |
1427 | 76 | mastering_display->white_point[1] = dav1d_get_bits(&gb, 16); |
1428 | | #if DEBUG_OBU_METADATA |
1429 | | printf("MDCVOBU: white-point-y: %d [off=%td]\n", |
1430 | | mastering_display->white_point[1], |
1431 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1432 | | #endif |
1433 | 76 | mastering_display->max_luminance = dav1d_get_bits(&gb, 32); |
1434 | | #if DEBUG_OBU_METADATA |
1435 | | printf("MDCVOBU: max-luminance: %d [off=%td]\n", |
1436 | | mastering_display->max_luminance, |
1437 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1438 | | #endif |
1439 | 76 | mastering_display->min_luminance = dav1d_get_bits(&gb, 32); |
1440 | | #if DEBUG_OBU_METADATA |
1441 | | printf("MDCVOBU: min-luminance: %d [off=%td]\n", |
1442 | | mastering_display->min_luminance, |
1443 | | (gb.ptr - init_ptr) * 8 - gb.bits_left); |
1444 | | #endif |
1445 | 76 | if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) { |
1446 | 13 | dav1d_ref_dec(&ref); |
1447 | 13 | goto error; |
1448 | 13 | } |
1449 | | |
1450 | 63 | dav1d_ref_dec(&c->mastering_display_ref); |
1451 | 63 | c->mastering_display = mastering_display; |
1452 | 63 | c->mastering_display_ref = ref; |
1453 | 63 | break; |
1454 | 76 | } |
1455 | 55 | case OBU_META_ITUT_T35: { |
1456 | 55 | ptrdiff_t payload_size = gb.ptr_end - gb.ptr; |
1457 | | // Don't take into account all the trailing bits for payload_size |
1458 | 315 | while (payload_size > 0 && !gb.ptr[payload_size - 1]) |
1459 | 260 | payload_size--; // trailing_zero_bit x 8 |
1460 | 55 | payload_size--; // trailing_one_bit + trailing_zero_bit x 7 |
1461 | | |
1462 | 55 | int country_code_extension_byte = 0; |
1463 | 55 | const int country_code = dav1d_get_bits(&gb, 8); |
1464 | 55 | payload_size--; |
1465 | 55 | if (country_code == 0xFF) { |
1466 | 10 | country_code_extension_byte = dav1d_get_bits(&gb, 8); |
1467 | 10 | payload_size--; |
1468 | 10 | } |
1469 | | |
1470 | 55 | if (payload_size <= 0 || gb.ptr[payload_size] != 0x80) { |
1471 | 42 | dav1d_log(c, "Malformed ITU-T T.35 metadata message format\n"); |
1472 | 42 | break; |
1473 | 42 | } |
1474 | | |
1475 | 13 | if ((c->n_itut_t35 + 1) > INT_MAX / (int)sizeof(*c->itut_t35)) goto error; |
1476 | 13 | uint8_t *const payload = dav1d_malloc(ALLOC_OBU_META, payload_size); |
1477 | 13 | if (!payload) goto error; |
1478 | | |
1479 | 13 | struct itut_t35_ctx_context *itut_t35_ctx = NULL; |
1480 | 13 | if (!c->n_itut_t35) { |
1481 | 13 | assert(!c->itut_t35_ref); |
1482 | 13 | itut_t35_ctx = dav1d_malloc(ALLOC_OBU_META, sizeof(struct itut_t35_ctx_context)); |
1483 | 13 | if (!itut_t35_ctx) { |
1484 | 0 | dav1d_free(payload); |
1485 | 0 | goto error; |
1486 | 0 | } |
1487 | 13 | } |
1488 | | |
1489 | 13 | struct Dav1dITUTT35 *itut_t35 = dav1d_realloc(ALLOC_OBU_META, c->itut_t35, |
1490 | 13 | (c->n_itut_t35 + 1) * sizeof(*c->itut_t35)); |
1491 | 13 | if (!itut_t35) { |
1492 | 0 | dav1d_free(itut_t35_ctx); |
1493 | 0 | dav1d_free(payload); |
1494 | 0 | goto error; |
1495 | 0 | } |
1496 | 13 | c->itut_t35 = itut_t35; |
1497 | 13 | memset(c->itut_t35 + c->n_itut_t35, 0, sizeof(*c->itut_t35)); |
1498 | | |
1499 | 13 | if (!c->n_itut_t35) { |
1500 | 13 | c->itut_t35_ref = dav1d_ref_init(&itut_t35_ctx->ref, c->itut_t35, |
1501 | 13 | dav1d_picture_free_itut_t35, itut_t35_ctx, 0); |
1502 | 13 | } else { |
1503 | 0 | assert(c->itut_t35_ref && |
1504 | 0 | atomic_load_explicit(&c->itut_t35_ref->ref_cnt, memory_order_relaxed) == 1); |
1505 | 0 | itut_t35_ctx = c->itut_t35_ref->user_data; |
1506 | 0 | c->itut_t35_ref->const_data = (uint8_t *)c->itut_t35; |
1507 | 0 | } |
1508 | 13 | itut_t35_ctx->itut_t35 = c->itut_t35; |
1509 | 13 | itut_t35_ctx->n_itut_t35 = c->n_itut_t35 + 1; |
1510 | | |
1511 | 13 | Dav1dITUTT35 *const itut_t35_metadata = &c->itut_t35[c->n_itut_t35]; |
1512 | 13 | itut_t35_metadata->payload = payload; |
1513 | | |
1514 | 13 | itut_t35_metadata->country_code = country_code; |
1515 | 13 | itut_t35_metadata->country_code_extension_byte = country_code_extension_byte; |
1516 | 13 | itut_t35_metadata->payload_size = payload_size; |
1517 | | |
1518 | | // We know that we've read a whole number of bytes and that the |
1519 | | // payload is within the OBU boundaries, so just use memcpy() |
1520 | 13 | assert(gb.bits_left == 0); |
1521 | 13 | memcpy(itut_t35_metadata->payload, gb.ptr, payload_size); |
1522 | | |
1523 | 13 | c->n_itut_t35++; |
1524 | 13 | break; |
1525 | 13 | } |
1526 | 2.39k | case OBU_META_SCALABILITY: |
1527 | 2.40k | case OBU_META_TIMECODE: |
1528 | | // ignore metadata OBUs we don't care about |
1529 | 2.40k | break; |
1530 | 651 | default: |
1531 | | // print a warning but don't fail for unknown types |
1532 | 651 | if (meta_type > 31) // Types 6 to 31 are "Unregistered user private", so ignore them. |
1533 | 574 | dav1d_log(c, "Unknown Metadata OBU type %d\n", meta_type); |
1534 | 651 | break; |
1535 | 6.07k | } |
1536 | | |
1537 | 6.04k | break; |
1538 | 6.07k | } |
1539 | 6.04k | case DAV1D_OBU_TD: |
1540 | 2.32k | c->frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT; |
1541 | 2.32k | break; |
1542 | 4.06k | case DAV1D_OBU_PADDING: |
1543 | | // ignore OBUs we don't care about |
1544 | 4.06k | break; |
1545 | 7.76k | default: |
1546 | | // print a warning but don't fail for unknown types |
1547 | 7.76k | dav1d_log(c, "Unknown OBU type %d of size %td\n", type, gb.ptr_end - gb.ptr); |
1548 | 7.76k | break; |
1549 | 130k | } |
1550 | | |
1551 | 127k | if (c->seq_hdr && c->frame_hdr) { |
1552 | 63.9k | if (c->frame_hdr->show_existing_frame) { |
1553 | 313 | if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr) goto error; |
1554 | 296 | switch (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type) { |
1555 | 57 | case DAV1D_FRAME_TYPE_INTER: |
1556 | 74 | case DAV1D_FRAME_TYPE_SWITCH: |
1557 | 74 | if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE) |
1558 | 0 | goto skip; |
1559 | 74 | break; |
1560 | 74 | case DAV1D_FRAME_TYPE_INTRA: |
1561 | 12 | if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA) |
1562 | 0 | goto skip; |
1563 | | // fall-through |
1564 | 222 | default: |
1565 | 222 | break; |
1566 | 296 | } |
1567 | 296 | if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.data[0]) goto error; |
1568 | 296 | if (c->strict_std_compliance && |
1569 | 0 | !c->refs[c->frame_hdr->existing_frame_idx].p.showable) |
1570 | 0 | { |
1571 | 0 | goto error; |
1572 | 0 | } |
1573 | 296 | if (c->n_fc == 1) { |
1574 | 0 | dav1d_thread_picture_ref(&c->out, |
1575 | 0 | &c->refs[c->frame_hdr->existing_frame_idx].p); |
1576 | 0 | dav1d_picture_copy_props(&c->out.p, |
1577 | 0 | c->content_light, c->content_light_ref, |
1578 | 0 | c->mastering_display, c->mastering_display_ref, |
1579 | 0 | c->itut_t35, c->itut_t35_ref, c->n_itut_t35, |
1580 | 0 | &in->m); |
1581 | | // Must be removed from the context after being attached to the frame |
1582 | 0 | dav1d_ref_dec(&c->itut_t35_ref); |
1583 | 0 | c->itut_t35 = NULL; |
1584 | 0 | c->n_itut_t35 = 0; |
1585 | 0 | c->event_flags |= dav1d_picture_get_event_flags(&c->refs[c->frame_hdr->existing_frame_idx].p); |
1586 | 296 | } else { |
1587 | 296 | pthread_mutex_lock(&c->task_thread.lock); |
1588 | | // need to append this to the frame output queue |
1589 | 296 | const unsigned next = c->frame_thread.next++; |
1590 | 296 | if (c->frame_thread.next == c->n_fc) |
1591 | 9 | c->frame_thread.next = 0; |
1592 | | |
1593 | 296 | Dav1dFrameContext *const f = &c->fc[next]; |
1594 | 296 | while (f->n_tile_data > 0) |
1595 | 0 | pthread_cond_wait(&f->task_thread.cond, |
1596 | 0 | &f->task_thread.ttd->lock); |
1597 | 296 | Dav1dThreadPicture *const out_delayed = |
1598 | 296 | &c->frame_thread.out_delayed[next]; |
1599 | 296 | if (out_delayed->p.data[0] || |
1600 | 296 | atomic_load_explicit(&f->task_thread.error, memory_order_relaxed)) |
1601 | 0 | { |
1602 | 0 | unsigned first = atomic_load_explicit(&c->task_thread.first, |
1603 | 0 | memory_order_relaxed); |
1604 | 0 | if (first + 1U < c->n_fc) |
1605 | 0 | atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed); |
1606 | 0 | else |
1607 | 0 | atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed); |
1608 | 0 | atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur, |
1609 | 0 | &first, UINT_MAX, |
1610 | 0 | memory_order_relaxed, |
1611 | 0 | memory_order_relaxed); |
1612 | 0 | if (c->task_thread.cur && c->task_thread.cur < c->n_fc) |
1613 | 0 | c->task_thread.cur--; |
1614 | 0 | } |
1615 | 296 | const int error = f->task_thread.retval; |
1616 | 296 | if (error) { |
1617 | 0 | c->cached_error = error; |
1618 | 0 | f->task_thread.retval = 0; |
1619 | 0 | dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m); |
1620 | 0 | dav1d_thread_picture_unref(out_delayed); |
1621 | 296 | } else if (out_delayed->p.data[0]) { |
1622 | 0 | const unsigned progress = atomic_load_explicit(&out_delayed->progress[1], |
1623 | 0 | memory_order_relaxed); |
1624 | 0 | if ((out_delayed->visible || c->output_invisible_frames) && |
1625 | 0 | progress != FRAME_ERROR) |
1626 | 0 | { |
1627 | 0 | dav1d_thread_picture_ref(&c->out, out_delayed); |
1628 | 0 | c->event_flags |= dav1d_picture_get_event_flags(out_delayed); |
1629 | 0 | } |
1630 | 0 | dav1d_thread_picture_unref(out_delayed); |
1631 | 0 | } |
1632 | 296 | dav1d_thread_picture_ref(out_delayed, |
1633 | 296 | &c->refs[c->frame_hdr->existing_frame_idx].p); |
1634 | 296 | out_delayed->visible = 1; |
1635 | 296 | dav1d_picture_copy_props(&out_delayed->p, |
1636 | 296 | c->content_light, c->content_light_ref, |
1637 | 296 | c->mastering_display, c->mastering_display_ref, |
1638 | 296 | c->itut_t35, c->itut_t35_ref, c->n_itut_t35, |
1639 | 296 | &in->m); |
1640 | | // Must be removed from the context after being attached to the frame |
1641 | 296 | dav1d_ref_dec(&c->itut_t35_ref); |
1642 | 296 | c->itut_t35 = NULL; |
1643 | 296 | c->n_itut_t35 = 0; |
1644 | | |
1645 | 296 | pthread_mutex_unlock(&c->task_thread.lock); |
1646 | 296 | } |
1647 | 296 | if (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY) { |
1648 | 210 | const int r = c->frame_hdr->existing_frame_idx; |
1649 | 210 | c->refs[r].p.showable = 0; |
1650 | 1.89k | for (int i = 0; i < 8; i++) { |
1651 | 1.68k | if (i == r) continue; |
1652 | | |
1653 | 1.47k | if (c->refs[i].p.p.frame_hdr) |
1654 | 1.40k | dav1d_thread_picture_unref(&c->refs[i].p); |
1655 | 1.47k | dav1d_thread_picture_ref(&c->refs[i].p, &c->refs[r].p); |
1656 | | |
1657 | 1.47k | dav1d_cdf_thread_unref(&c->cdf[i]); |
1658 | 1.47k | dav1d_cdf_thread_ref(&c->cdf[i], &c->cdf[r]); |
1659 | | |
1660 | 1.47k | dav1d_ref_dec(&c->refs[i].segmap); |
1661 | 1.47k | c->refs[i].segmap = c->refs[r].segmap; |
1662 | 1.47k | if (c->refs[r].segmap) |
1663 | 343 | dav1d_ref_inc(c->refs[r].segmap); |
1664 | 1.47k | dav1d_ref_dec(&c->refs[i].refmvs); |
1665 | 1.47k | } |
1666 | 210 | } |
1667 | 296 | c->frame_hdr = NULL; |
1668 | 63.6k | } else if (c->n_tiles == c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows) { |
1669 | 61.7k | switch (c->frame_hdr->frame_type) { |
1670 | 16.8k | case DAV1D_FRAME_TYPE_INTER: |
1671 | 17.4k | case DAV1D_FRAME_TYPE_SWITCH: |
1672 | 17.4k | if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE || |
1673 | 17.4k | (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE && |
1674 | 0 | !c->frame_hdr->refresh_frame_flags)) |
1675 | 0 | goto skip; |
1676 | 17.4k | break; |
1677 | 17.4k | case DAV1D_FRAME_TYPE_INTRA: |
1678 | 171 | if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA || |
1679 | 171 | (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE && |
1680 | 0 | !c->frame_hdr->refresh_frame_flags)) |
1681 | 0 | goto skip; |
1682 | | // fall-through |
1683 | 44.3k | default: |
1684 | 44.3k | break; |
1685 | 61.7k | } |
1686 | 61.7k | if (!c->n_tile_data) |
1687 | 0 | goto error; |
1688 | 61.7k | if ((res = dav1d_submit_frame(c)) < 0) |
1689 | 147 | return res; |
1690 | 61.6k | assert(!c->n_tile_data); |
1691 | 61.6k | c->frame_hdr = NULL; |
1692 | 61.6k | c->n_tiles = 0; |
1693 | 61.6k | } |
1694 | 63.9k | } |
1695 | | |
1696 | 127k | return gb.ptr_end - gb.ptr_start; |
1697 | | |
1698 | 0 | skip: |
1699 | | // update refs with only the headers in case we skip the frame |
1700 | 0 | for (int i = 0; i < 8; i++) { |
1701 | 0 | if (c->frame_hdr->refresh_frame_flags & (1 << i)) { |
1702 | 0 | dav1d_thread_picture_unref(&c->refs[i].p); |
1703 | 0 | c->refs[i].p.p.frame_hdr = c->frame_hdr; |
1704 | 0 | c->refs[i].p.p.seq_hdr = c->seq_hdr; |
1705 | 0 | c->refs[i].p.p.frame_hdr_ref = c->frame_hdr_ref; |
1706 | 0 | c->refs[i].p.p.seq_hdr_ref = c->seq_hdr_ref; |
1707 | 0 | dav1d_ref_inc(c->frame_hdr_ref); |
1708 | 0 | dav1d_ref_inc(c->seq_hdr_ref); |
1709 | 0 | } |
1710 | 0 | } |
1711 | |
|
1712 | 0 | dav1d_ref_dec(&c->frame_hdr_ref); |
1713 | 0 | c->frame_hdr = NULL; |
1714 | 0 | c->n_tiles = 0; |
1715 | |
|
1716 | 0 | return gb.ptr_end - gb.ptr_start; |
1717 | | |
1718 | 2.06k | error: |
1719 | 2.06k | dav1d_data_props_copy(&c->cached_error_props, &in->m); |
1720 | 2.06k | dav1d_log(c, gb.error ? "Overrun in OBU bit buffer\n" : |
1721 | 2.06k | "Error parsing OBU data\n"); |
1722 | 2.06k | return DAV1D_ERR(EINVAL); |
1723 | 127k | } |