2 * Copyright © 2018-2021, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
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.
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.
34 #include "dav1d/data.h"
36 #include "common/frame.h"
37 #include "common/intops.h"
38 #include "common/validate.h"
40 #include "src/decode.h"
41 #include "src/getbits.h"
42 #include "src/levels.h"
46 #include "src/thread_task.h"
48 static int check_trailing_bits(GetBits
*const gb
,
49 const int strict_std_compliance
)
51 const int trailing_one_bit
= dav1d_get_bit(gb
);
54 return DAV1D_ERR(EINVAL
);
56 if (!strict_std_compliance
)
59 if (!trailing_one_bit
|| gb
->state
)
60 return DAV1D_ERR(EINVAL
);
62 ptrdiff_t size
= gb
->ptr_end
- gb
->ptr
;
63 while (size
> 0 && gb
->ptr
[size
- 1] == 0)
67 return DAV1D_ERR(EINVAL
);
72 static NOINLINE
int parse_seq_hdr(Dav1dSequenceHeader
*const hdr
,
74 const int strict_std_compliance
)
76 #define DEBUG_SEQ_HDR 0
79 const unsigned init_bit_pos
= dav1d_get_bits_pos(gb
);
82 memset(hdr
, 0, sizeof(*hdr
));
83 hdr
->profile
= dav1d_get_bits(gb
, 3);
84 if (hdr
->profile
> 2) goto error
;
86 printf("SEQHDR: post-profile: off=%u\n",
87 dav1d_get_bits_pos(gb
) - init_bit_pos
);
90 hdr
->still_picture
= dav1d_get_bit(gb
);
91 hdr
->reduced_still_picture_header
= dav1d_get_bit(gb
);
92 if (hdr
->reduced_still_picture_header
&& !hdr
->still_picture
) goto error
;
94 printf("SEQHDR: post-stillpicture_flags: off=%u\n",
95 dav1d_get_bits_pos(gb
) - init_bit_pos
);
98 if (hdr
->reduced_still_picture_header
) {
99 hdr
->num_operating_points
= 1;
100 hdr
->operating_points
[0].major_level
= dav1d_get_bits(gb
, 3);
101 hdr
->operating_points
[0].minor_level
= dav1d_get_bits(gb
, 2);
102 hdr
->operating_points
[0].initial_display_delay
= 10;
104 hdr
->timing_info_present
= dav1d_get_bit(gb
);
105 if (hdr
->timing_info_present
) {
106 hdr
->num_units_in_tick
= dav1d_get_bits(gb
, 32);
107 hdr
->time_scale
= dav1d_get_bits(gb
, 32);
108 if (strict_std_compliance
&& (!hdr
->num_units_in_tick
|| !hdr
->time_scale
))
110 hdr
->equal_picture_interval
= dav1d_get_bit(gb
);
111 if (hdr
->equal_picture_interval
) {
112 const unsigned num_ticks_per_picture
= dav1d_get_vlc(gb
);
113 if (num_ticks_per_picture
== 0xFFFFFFFFU
)
115 hdr
->num_ticks_per_picture
= num_ticks_per_picture
+ 1;
118 hdr
->decoder_model_info_present
= dav1d_get_bit(gb
);
119 if (hdr
->decoder_model_info_present
) {
120 hdr
->encoder_decoder_buffer_delay_length
= dav1d_get_bits(gb
, 5) + 1;
121 hdr
->num_units_in_decoding_tick
= dav1d_get_bits(gb
, 32);
122 if (strict_std_compliance
&& !hdr
->num_units_in_decoding_tick
)
124 hdr
->buffer_removal_delay_length
= dav1d_get_bits(gb
, 5) + 1;
125 hdr
->frame_presentation_delay_length
= dav1d_get_bits(gb
, 5) + 1;
129 printf("SEQHDR: post-timinginfo: off=%u\n",
130 dav1d_get_bits_pos(gb
) - init_bit_pos
);
133 hdr
->display_model_info_present
= dav1d_get_bit(gb
);
134 hdr
->num_operating_points
= dav1d_get_bits(gb
, 5) + 1;
135 for (int i
= 0; i
< hdr
->num_operating_points
; i
++) {
136 struct Dav1dSequenceHeaderOperatingPoint
*const op
=
137 &hdr
->operating_points
[i
];
138 op
->idc
= dav1d_get_bits(gb
, 12);
139 if (op
->idc
&& (!(op
->idc
& 0xff) || !(op
->idc
& 0xf00)))
141 op
->major_level
= 2 + dav1d_get_bits(gb
, 3);
142 op
->minor_level
= dav1d_get_bits(gb
, 2);
143 if (op
->major_level
> 3)
144 op
->tier
= dav1d_get_bit(gb
);
145 if (hdr
->decoder_model_info_present
) {
146 op
->decoder_model_param_present
= dav1d_get_bit(gb
);
147 if (op
->decoder_model_param_present
) {
148 struct Dav1dSequenceHeaderOperatingParameterInfo
*const opi
=
149 &hdr
->operating_parameter_info
[i
];
150 opi
->decoder_buffer_delay
=
151 dav1d_get_bits(gb
, hdr
->encoder_decoder_buffer_delay_length
);
152 opi
->encoder_buffer_delay
=
153 dav1d_get_bits(gb
, hdr
->encoder_decoder_buffer_delay_length
);
154 opi
->low_delay_mode
= dav1d_get_bit(gb
);
157 if (hdr
->display_model_info_present
)
158 op
->display_model_param_present
= dav1d_get_bit(gb
);
159 op
->initial_display_delay
=
160 op
->display_model_param_present
? dav1d_get_bits(gb
, 4) + 1 : 10;
163 printf("SEQHDR: post-operating-points: off=%u\n",
164 dav1d_get_bits_pos(gb
) - init_bit_pos
);
168 hdr
->width_n_bits
= dav1d_get_bits(gb
, 4) + 1;
169 hdr
->height_n_bits
= dav1d_get_bits(gb
, 4) + 1;
170 hdr
->max_width
= dav1d_get_bits(gb
, hdr
->width_n_bits
) + 1;
171 hdr
->max_height
= dav1d_get_bits(gb
, hdr
->height_n_bits
) + 1;
173 printf("SEQHDR: post-size: off=%u\n",
174 dav1d_get_bits_pos(gb
) - init_bit_pos
);
176 if (!hdr
->reduced_still_picture_header
) {
177 hdr
->frame_id_numbers_present
= dav1d_get_bit(gb
);
178 if (hdr
->frame_id_numbers_present
) {
179 hdr
->delta_frame_id_n_bits
= dav1d_get_bits(gb
, 4) + 2;
180 hdr
->frame_id_n_bits
= dav1d_get_bits(gb
, 3) + hdr
->delta_frame_id_n_bits
+ 1;
184 printf("SEQHDR: post-frame-id-numbers-present: off=%u\n",
185 dav1d_get_bits_pos(gb
) - init_bit_pos
);
188 hdr
->sb128
= dav1d_get_bit(gb
);
189 hdr
->filter_intra
= dav1d_get_bit(gb
);
190 hdr
->intra_edge_filter
= dav1d_get_bit(gb
);
191 if (hdr
->reduced_still_picture_header
) {
192 hdr
->screen_content_tools
= DAV1D_ADAPTIVE
;
193 hdr
->force_integer_mv
= DAV1D_ADAPTIVE
;
195 hdr
->inter_intra
= dav1d_get_bit(gb
);
196 hdr
->masked_compound
= dav1d_get_bit(gb
);
197 hdr
->warped_motion
= dav1d_get_bit(gb
);
198 hdr
->dual_filter
= dav1d_get_bit(gb
);
199 hdr
->order_hint
= dav1d_get_bit(gb
);
200 if (hdr
->order_hint
) {
201 hdr
->jnt_comp
= dav1d_get_bit(gb
);
202 hdr
->ref_frame_mvs
= dav1d_get_bit(gb
);
204 hdr
->screen_content_tools
= dav1d_get_bit(gb
) ? DAV1D_ADAPTIVE
: dav1d_get_bit(gb
);
206 printf("SEQHDR: post-screentools: off=%u\n",
207 dav1d_get_bits_pos(gb
) - init_bit_pos
);
209 hdr
->force_integer_mv
= hdr
->screen_content_tools
?
210 dav1d_get_bit(gb
) ? DAV1D_ADAPTIVE
: dav1d_get_bit(gb
) : 2;
212 hdr
->order_hint_n_bits
= dav1d_get_bits(gb
, 3) + 1;
214 hdr
->super_res
= dav1d_get_bit(gb
);
215 hdr
->cdef
= dav1d_get_bit(gb
);
216 hdr
->restoration
= dav1d_get_bit(gb
);
218 printf("SEQHDR: post-featurebits: off=%u\n",
219 dav1d_get_bits_pos(gb
) - init_bit_pos
);
222 hdr
->hbd
= dav1d_get_bit(gb
);
223 if (hdr
->profile
== 2 && hdr
->hbd
)
224 hdr
->hbd
+= dav1d_get_bit(gb
);
225 if (hdr
->profile
!= 1)
226 hdr
->monochrome
= dav1d_get_bit(gb
);
227 hdr
->color_description_present
= dav1d_get_bit(gb
);
228 if (hdr
->color_description_present
) {
229 hdr
->pri
= dav1d_get_bits(gb
, 8);
230 hdr
->trc
= dav1d_get_bits(gb
, 8);
231 hdr
->mtrx
= dav1d_get_bits(gb
, 8);
233 hdr
->pri
= DAV1D_COLOR_PRI_UNKNOWN
;
234 hdr
->trc
= DAV1D_TRC_UNKNOWN
;
235 hdr
->mtrx
= DAV1D_MC_UNKNOWN
;
237 if (hdr
->monochrome
) {
238 hdr
->color_range
= dav1d_get_bit(gb
);
239 hdr
->layout
= DAV1D_PIXEL_LAYOUT_I400
;
240 hdr
->ss_hor
= hdr
->ss_ver
= 1;
241 hdr
->chr
= DAV1D_CHR_UNKNOWN
;
242 } else if (hdr
->pri
== DAV1D_COLOR_PRI_BT709
&&
243 hdr
->trc
== DAV1D_TRC_SRGB
&&
244 hdr
->mtrx
== DAV1D_MC_IDENTITY
)
246 hdr
->layout
= DAV1D_PIXEL_LAYOUT_I444
;
247 hdr
->color_range
= 1;
248 if (hdr
->profile
!= 1 && !(hdr
->profile
== 2 && hdr
->hbd
== 2))
251 hdr
->color_range
= dav1d_get_bit(gb
);
252 switch (hdr
->profile
) {
253 case 0: hdr
->layout
= DAV1D_PIXEL_LAYOUT_I420
;
254 hdr
->ss_hor
= hdr
->ss_ver
= 1;
256 case 1: hdr
->layout
= DAV1D_PIXEL_LAYOUT_I444
;
260 hdr
->ss_hor
= dav1d_get_bit(gb
);
262 hdr
->ss_ver
= dav1d_get_bit(gb
);
265 hdr
->layout
= hdr
->ss_hor
?
266 hdr
->ss_ver
? DAV1D_PIXEL_LAYOUT_I420
:
267 DAV1D_PIXEL_LAYOUT_I422
:
268 DAV1D_PIXEL_LAYOUT_I444
;
271 hdr
->chr
= (hdr
->ss_hor
& hdr
->ss_ver
) ?
272 dav1d_get_bits(gb
, 2) : DAV1D_CHR_UNKNOWN
;
274 if (strict_std_compliance
&&
275 hdr
->mtrx
== DAV1D_MC_IDENTITY
&& hdr
->layout
!= DAV1D_PIXEL_LAYOUT_I444
)
279 if (!hdr
->monochrome
)
280 hdr
->separate_uv_delta_q
= dav1d_get_bit(gb
);
282 printf("SEQHDR: post-colorinfo: off=%u\n",
283 dav1d_get_bits_pos(gb
) - init_bit_pos
);
286 hdr
->film_grain_present
= dav1d_get_bit(gb
);
288 printf("SEQHDR: post-filmgrain: off=%u\n",
289 dav1d_get_bits_pos(gb
) - init_bit_pos
);
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.
296 return check_trailing_bits(gb
, strict_std_compliance
);
299 return DAV1D_ERR(EINVAL
);
302 int dav1d_parse_sequence_header(Dav1dSequenceHeader
*const out
,
303 const uint8_t *const ptr
, const size_t sz
)
305 validate_input_or_ret(out
!= NULL
, DAV1D_ERR(EINVAL
));
306 validate_input_or_ret(ptr
!= NULL
, DAV1D_ERR(EINVAL
));
307 validate_input_or_ret(sz
> 0 && sz
<= SIZE_MAX
/ 2, DAV1D_ERR(EINVAL
));
310 dav1d_init_get_bits(&gb
, ptr
, sz
);
311 int res
= DAV1D_ERR(ENOENT
);
314 dav1d_get_bit(&gb
); // obu_forbidden_bit
315 const enum Dav1dObuType type
= dav1d_get_bits(&gb
, 4);
316 const int has_extension
= dav1d_get_bit(&gb
);
317 const int has_length_field
= dav1d_get_bit(&gb
);
318 dav1d_get_bits(&gb
, 1 + 8 * has_extension
); // ignore
320 const uint8_t *obu_end
= gb
.ptr_end
;
321 if (has_length_field
) {
322 const size_t len
= dav1d_get_uleb128(&gb
);
323 if (len
> (size_t)(obu_end
- gb
.ptr
)) return DAV1D_ERR(EINVAL
);
324 obu_end
= gb
.ptr
+ len
;
327 if (type
== DAV1D_OBU_SEQ_HDR
) {
328 if ((res
= parse_seq_hdr(out
, &gb
, 0)) < 0) return res
;
329 if (gb
.ptr
> obu_end
) return DAV1D_ERR(EINVAL
);
330 dav1d_bytealign_get_bits(&gb
);
333 if (gb
.error
) return DAV1D_ERR(EINVAL
);
334 assert(gb
.state
== 0 && gb
.bits_left
== 0);
336 } while (gb
.ptr
< gb
.ptr_end
);
341 static int read_frame_size(Dav1dContext
*const c
, GetBits
*const gb
,
344 const Dav1dSequenceHeader
*const seqhdr
= c
->seq_hdr
;
345 Dav1dFrameHeader
*const hdr
= c
->frame_hdr
;
348 for (int i
= 0; i
< 7; i
++) {
349 if (dav1d_get_bit(gb
)) {
350 const Dav1dThreadPicture
*const ref
=
351 &c
->refs
[c
->frame_hdr
->refidx
[i
]].p
;
352 if (!ref
->p
.frame_hdr
) return -1;
353 hdr
->width
[1] = ref
->p
.frame_hdr
->width
[1];
354 hdr
->height
= ref
->p
.frame_hdr
->height
;
355 hdr
->render_width
= ref
->p
.frame_hdr
->render_width
;
356 hdr
->render_height
= ref
->p
.frame_hdr
->render_height
;
357 hdr
->super_res
.enabled
= seqhdr
->super_res
&& dav1d_get_bit(gb
);
358 if (hdr
->super_res
.enabled
) {
359 const int d
= hdr
->super_res
.width_scale_denominator
=
360 9 + dav1d_get_bits(gb
, 3);
361 hdr
->width
[0] = imax((hdr
->width
[1] * 8 + (d
>> 1)) / d
,
362 imin(16, hdr
->width
[1]));
364 hdr
->super_res
.width_scale_denominator
= 8;
365 hdr
->width
[0] = hdr
->width
[1];
372 if (hdr
->frame_size_override
) {
373 hdr
->width
[1] = dav1d_get_bits(gb
, seqhdr
->width_n_bits
) + 1;
374 hdr
->height
= dav1d_get_bits(gb
, seqhdr
->height_n_bits
) + 1;
376 hdr
->width
[1] = seqhdr
->max_width
;
377 hdr
->height
= seqhdr
->max_height
;
379 hdr
->super_res
.enabled
= seqhdr
->super_res
&& dav1d_get_bit(gb
);
380 if (hdr
->super_res
.enabled
) {
381 const int d
= hdr
->super_res
.width_scale_denominator
= 9 + dav1d_get_bits(gb
, 3);
382 hdr
->width
[0] = imax((hdr
->width
[1] * 8 + (d
>> 1)) / d
, imin(16, hdr
->width
[1]));
384 hdr
->super_res
.width_scale_denominator
= 8;
385 hdr
->width
[0] = hdr
->width
[1];
387 hdr
->have_render_size
= dav1d_get_bit(gb
);
388 if (hdr
->have_render_size
) {
389 hdr
->render_width
= dav1d_get_bits(gb
, 16) + 1;
390 hdr
->render_height
= dav1d_get_bits(gb
, 16) + 1;
392 hdr
->render_width
= hdr
->width
[1];
393 hdr
->render_height
= hdr
->height
;
398 static inline int tile_log2(const int sz
, const int tgt
) {
400 for (k
= 0; (sz
<< k
) < tgt
; k
++) ;
404 static const Dav1dLoopfilterModeRefDeltas default_mode_ref_deltas
= {
405 .mode_delta
= { 0, 0 },
406 .ref_delta
= { 1, 0, 0, 0, -1, 0, -1, -1 },
409 static int parse_frame_hdr(Dav1dContext
*const c
, GetBits
*const gb
) {
410 #define DEBUG_FRAME_HDR 0
413 const uint8_t *const init_ptr
= gb
->ptr
;
415 const Dav1dSequenceHeader
*const seqhdr
= c
->seq_hdr
;
416 Dav1dFrameHeader
*const hdr
= c
->frame_hdr
;
418 hdr
->show_existing_frame
=
419 !seqhdr
->reduced_still_picture_header
&& dav1d_get_bit(gb
);
421 printf("HDR: post-show_existing_frame: off=%td\n",
422 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
424 if (hdr
->show_existing_frame
) {
425 hdr
->existing_frame_idx
= dav1d_get_bits(gb
, 3);
426 if (seqhdr
->decoder_model_info_present
&& !seqhdr
->equal_picture_interval
)
427 hdr
->frame_presentation_delay
= dav1d_get_bits(gb
, seqhdr
->frame_presentation_delay_length
);
428 if (seqhdr
->frame_id_numbers_present
) {
429 hdr
->frame_id
= dav1d_get_bits(gb
, seqhdr
->frame_id_n_bits
);
430 Dav1dFrameHeader
*const ref_frame_hdr
= c
->refs
[hdr
->existing_frame_idx
].p
.p
.frame_hdr
;
431 if (!ref_frame_hdr
|| ref_frame_hdr
->frame_id
!= hdr
->frame_id
) goto error
;
436 hdr
->frame_type
= seqhdr
->reduced_still_picture_header
? DAV1D_FRAME_TYPE_KEY
: dav1d_get_bits(gb
, 2);
437 hdr
->show_frame
= seqhdr
->reduced_still_picture_header
|| dav1d_get_bit(gb
);
438 if (hdr
->show_frame
) {
439 if (seqhdr
->decoder_model_info_present
&& !seqhdr
->equal_picture_interval
)
440 hdr
->frame_presentation_delay
= dav1d_get_bits(gb
, seqhdr
->frame_presentation_delay_length
);
441 hdr
->showable_frame
= hdr
->frame_type
!= DAV1D_FRAME_TYPE_KEY
;
443 hdr
->showable_frame
= dav1d_get_bit(gb
);
444 hdr
->error_resilient_mode
=
445 (hdr
->frame_type
== DAV1D_FRAME_TYPE_KEY
&& hdr
->show_frame
) ||
446 hdr
->frame_type
== DAV1D_FRAME_TYPE_SWITCH
||
447 seqhdr
->reduced_still_picture_header
|| dav1d_get_bit(gb
);
449 printf("HDR: post-frametype_bits: off=%td\n",
450 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
452 hdr
->disable_cdf_update
= dav1d_get_bit(gb
);
453 hdr
->allow_screen_content_tools
= seqhdr
->screen_content_tools
== DAV1D_ADAPTIVE
?
454 dav1d_get_bit(gb
) : seqhdr
->screen_content_tools
;
455 if (hdr
->allow_screen_content_tools
)
456 hdr
->force_integer_mv
= seqhdr
->force_integer_mv
== DAV1D_ADAPTIVE
?
457 dav1d_get_bit(gb
) : seqhdr
->force_integer_mv
;
459 hdr
->force_integer_mv
= 0;
461 if (IS_KEY_OR_INTRA(hdr
))
462 hdr
->force_integer_mv
= 1;
464 if (seqhdr
->frame_id_numbers_present
)
465 hdr
->frame_id
= dav1d_get_bits(gb
, seqhdr
->frame_id_n_bits
);
467 hdr
->frame_size_override
= seqhdr
->reduced_still_picture_header
? 0 :
468 hdr
->frame_type
== DAV1D_FRAME_TYPE_SWITCH
? 1 : dav1d_get_bit(gb
);
470 printf("HDR: post-frame_size_override_flag: off=%td\n",
471 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
473 hdr
->frame_offset
= seqhdr
->order_hint
?
474 dav1d_get_bits(gb
, seqhdr
->order_hint_n_bits
) : 0;
475 hdr
->primary_ref_frame
= !hdr
->error_resilient_mode
&& IS_INTER_OR_SWITCH(hdr
) ?
476 dav1d_get_bits(gb
, 3) : DAV1D_PRIMARY_REF_NONE
;
478 if (seqhdr
->decoder_model_info_present
) {
479 hdr
->buffer_removal_time_present
= dav1d_get_bit(gb
);
480 if (hdr
->buffer_removal_time_present
) {
481 for (int i
= 0; i
< c
->seq_hdr
->num_operating_points
; i
++) {
482 const struct Dav1dSequenceHeaderOperatingPoint
*const seqop
= &seqhdr
->operating_points
[i
];
483 struct Dav1dFrameHeaderOperatingPoint
*const op
= &hdr
->operating_points
[i
];
484 if (seqop
->decoder_model_param_present
) {
485 int in_temporal_layer
= (seqop
->idc
>> hdr
->temporal_id
) & 1;
486 int in_spatial_layer
= (seqop
->idc
>> (hdr
->spatial_id
+ 8)) & 1;
487 if (!seqop
->idc
|| (in_temporal_layer
&& in_spatial_layer
))
488 op
->buffer_removal_time
= dav1d_get_bits(gb
, seqhdr
->buffer_removal_delay_length
);
494 if (IS_KEY_OR_INTRA(hdr
)) {
495 hdr
->refresh_frame_flags
= (hdr
->frame_type
== DAV1D_FRAME_TYPE_KEY
&&
496 hdr
->show_frame
) ? 0xff : dav1d_get_bits(gb
, 8);
497 if (hdr
->refresh_frame_flags
!= 0xff && hdr
->error_resilient_mode
&& seqhdr
->order_hint
)
498 for (int i
= 0; i
< 8; i
++)
499 dav1d_get_bits(gb
, seqhdr
->order_hint_n_bits
);
500 if (c
->strict_std_compliance
&&
501 hdr
->frame_type
== DAV1D_FRAME_TYPE_INTRA
&& hdr
->refresh_frame_flags
== 0xff)
505 if (read_frame_size(c
, gb
, 0) < 0) goto error
;
506 hdr
->allow_intrabc
= hdr
->allow_screen_content_tools
&&
507 !hdr
->super_res
.enabled
&& dav1d_get_bit(gb
);
508 hdr
->use_ref_frame_mvs
= 0;
510 hdr
->allow_intrabc
= 0;
511 hdr
->refresh_frame_flags
= hdr
->frame_type
== DAV1D_FRAME_TYPE_SWITCH
? 0xff :
512 dav1d_get_bits(gb
, 8);
513 if (hdr
->error_resilient_mode
&& seqhdr
->order_hint
)
514 for (int i
= 0; i
< 8; i
++)
515 dav1d_get_bits(gb
, seqhdr
->order_hint_n_bits
);
516 hdr
->frame_ref_short_signaling
=
517 seqhdr
->order_hint
&& dav1d_get_bit(gb
);
518 if (hdr
->frame_ref_short_signaling
) { // FIXME: Nearly verbatim copy from section 7.8
519 hdr
->refidx
[0] = dav1d_get_bits(gb
, 3);
520 hdr
->refidx
[1] = hdr
->refidx
[2] = -1;
521 hdr
->refidx
[3] = dav1d_get_bits(gb
, 3);
522 hdr
->refidx
[4] = hdr
->refidx
[5] = hdr
->refidx
[6] = -1;
524 int shifted_frame_offset
[8];
525 const int current_frame_offset
= 1 << (seqhdr
->order_hint_n_bits
- 1);
526 for (int i
= 0; i
< 8; i
++) {
527 if (!c
->refs
[i
].p
.p
.frame_hdr
) goto error
;
528 shifted_frame_offset
[i
] = current_frame_offset
+
529 get_poc_diff(seqhdr
->order_hint_n_bits
,
530 c
->refs
[i
].p
.p
.frame_hdr
->frame_offset
,
534 int used_frame
[8] = { 0 };
535 used_frame
[hdr
->refidx
[0]] = 1;
536 used_frame
[hdr
->refidx
[3]] = 1;
538 int latest_frame_offset
= -1;
539 for (int i
= 0; i
< 8; i
++) {
540 const int hint
= shifted_frame_offset
[i
];
541 if (!used_frame
[i
] && hint
>= current_frame_offset
&&
542 hint
>= latest_frame_offset
)
545 latest_frame_offset
= hint
;
548 if (latest_frame_offset
!= -1)
549 used_frame
[hdr
->refidx
[6]] = 1;
551 int earliest_frame_offset
= INT_MAX
;
552 for (int i
= 0; i
< 8; i
++) {
553 const int hint
= shifted_frame_offset
[i
];
554 if (!used_frame
[i
] && hint
>= current_frame_offset
&&
555 hint
< earliest_frame_offset
)
558 earliest_frame_offset
= hint
;
561 if (earliest_frame_offset
!= INT_MAX
)
562 used_frame
[hdr
->refidx
[4]] = 1;
564 earliest_frame_offset
= INT_MAX
;
565 for (int i
= 0; i
< 8; i
++) {
566 const int hint
= shifted_frame_offset
[i
];
567 if (!used_frame
[i
] && hint
>= current_frame_offset
&&
568 (hint
< earliest_frame_offset
))
571 earliest_frame_offset
= hint
;
574 if (earliest_frame_offset
!= INT_MAX
)
575 used_frame
[hdr
->refidx
[5]] = 1;
577 for (int i
= 1; i
< 7; i
++) {
578 if (hdr
->refidx
[i
] < 0) {
579 latest_frame_offset
= -1;
580 for (int j
= 0; j
< 8; j
++) {
581 const int hint
= shifted_frame_offset
[j
];
582 if (!used_frame
[j
] && hint
< current_frame_offset
&&
583 hint
>= latest_frame_offset
)
586 latest_frame_offset
= hint
;
589 if (latest_frame_offset
!= -1)
590 used_frame
[hdr
->refidx
[i
]] = 1;
594 earliest_frame_offset
= INT_MAX
;
596 for (int i
= 0; i
< 8; i
++) {
597 const int hint
= shifted_frame_offset
[i
];
598 if (hint
< earliest_frame_offset
) {
600 earliest_frame_offset
= hint
;
603 for (int i
= 0; i
< 7; i
++) {
604 if (hdr
->refidx
[i
] < 0)
605 hdr
->refidx
[i
] = ref
;
608 for (int i
= 0; i
< 7; i
++) {
609 if (!hdr
->frame_ref_short_signaling
)
610 hdr
->refidx
[i
] = dav1d_get_bits(gb
, 3);
611 if (seqhdr
->frame_id_numbers_present
) {
612 const unsigned delta_ref_frame_id
= dav1d_get_bits(gb
, seqhdr
->delta_frame_id_n_bits
) + 1;
613 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);
614 Dav1dFrameHeader
*const ref_frame_hdr
= c
->refs
[hdr
->refidx
[i
]].p
.p
.frame_hdr
;
615 if (!ref_frame_hdr
|| ref_frame_hdr
->frame_id
!= ref_frame_id
) goto error
;
618 const int use_ref
= !hdr
->error_resilient_mode
&&
619 hdr
->frame_size_override
;
620 if (read_frame_size(c
, gb
, use_ref
) < 0) goto error
;
621 hdr
->hp
= !hdr
->force_integer_mv
&& dav1d_get_bit(gb
);
622 hdr
->subpel_filter_mode
= dav1d_get_bit(gb
) ? DAV1D_FILTER_SWITCHABLE
:
623 dav1d_get_bits(gb
, 2);
624 hdr
->switchable_motion_mode
= dav1d_get_bit(gb
);
625 hdr
->use_ref_frame_mvs
= !hdr
->error_resilient_mode
&&
626 seqhdr
->ref_frame_mvs
&& seqhdr
->order_hint
&&
627 IS_INTER_OR_SWITCH(hdr
) && dav1d_get_bit(gb
);
630 printf("HDR: post-frametype-specific-bits: off=%td\n",
631 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
634 hdr
->refresh_context
= !seqhdr
->reduced_still_picture_header
&&
635 !hdr
->disable_cdf_update
&& !dav1d_get_bit(gb
);
637 printf("HDR: post-refresh_context: off=%td\n",
638 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
642 hdr
->tiling
.uniform
= dav1d_get_bit(gb
);
643 const int sbsz_min1
= (64 << seqhdr
->sb128
) - 1;
644 const int sbsz_log2
= 6 + seqhdr
->sb128
;
645 const int sbw
= (hdr
->width
[0] + sbsz_min1
) >> sbsz_log2
;
646 const int sbh
= (hdr
->height
+ sbsz_min1
) >> sbsz_log2
;
647 const int max_tile_width_sb
= 4096 >> sbsz_log2
;
648 const int max_tile_area_sb
= 4096 * 2304 >> (2 * sbsz_log2
);
649 hdr
->tiling
.min_log2_cols
= tile_log2(max_tile_width_sb
, sbw
);
650 hdr
->tiling
.max_log2_cols
= tile_log2(1, imin(sbw
, DAV1D_MAX_TILE_COLS
));
651 hdr
->tiling
.max_log2_rows
= tile_log2(1, imin(sbh
, DAV1D_MAX_TILE_ROWS
));
652 const int min_log2_tiles
= imax(tile_log2(max_tile_area_sb
, sbw
* sbh
),
653 hdr
->tiling
.min_log2_cols
);
654 if (hdr
->tiling
.uniform
) {
655 for (hdr
->tiling
.log2_cols
= hdr
->tiling
.min_log2_cols
;
656 hdr
->tiling
.log2_cols
< hdr
->tiling
.max_log2_cols
&& dav1d_get_bit(gb
);
657 hdr
->tiling
.log2_cols
++) ;
658 const int tile_w
= 1 + ((sbw
- 1) >> hdr
->tiling
.log2_cols
);
659 hdr
->tiling
.cols
= 0;
660 for (int sbx
= 0; sbx
< sbw
; sbx
+= tile_w
, hdr
->tiling
.cols
++)
661 hdr
->tiling
.col_start_sb
[hdr
->tiling
.cols
] = sbx
;
662 hdr
->tiling
.min_log2_rows
=
663 imax(min_log2_tiles
- hdr
->tiling
.log2_cols
, 0);
665 for (hdr
->tiling
.log2_rows
= hdr
->tiling
.min_log2_rows
;
666 hdr
->tiling
.log2_rows
< hdr
->tiling
.max_log2_rows
&& dav1d_get_bit(gb
);
667 hdr
->tiling
.log2_rows
++) ;
668 const int tile_h
= 1 + ((sbh
- 1) >> hdr
->tiling
.log2_rows
);
669 hdr
->tiling
.rows
= 0;
670 for (int sby
= 0; sby
< sbh
; sby
+= tile_h
, hdr
->tiling
.rows
++)
671 hdr
->tiling
.row_start_sb
[hdr
->tiling
.rows
] = sby
;
673 hdr
->tiling
.cols
= 0;
674 int widest_tile
= 0, max_tile_area_sb
= sbw
* sbh
;
675 for (int sbx
= 0; sbx
< sbw
&& hdr
->tiling
.cols
< DAV1D_MAX_TILE_COLS
; hdr
->tiling
.cols
++) {
676 const int tile_width_sb
= imin(sbw
- sbx
, max_tile_width_sb
);
677 const int tile_w
= (tile_width_sb
> 1) ?
678 1 + dav1d_get_uniform(gb
, tile_width_sb
) :
680 hdr
->tiling
.col_start_sb
[hdr
->tiling
.cols
] = sbx
;
682 widest_tile
= imax(widest_tile
, tile_w
);
684 hdr
->tiling
.log2_cols
= tile_log2(1, hdr
->tiling
.cols
);
685 if (min_log2_tiles
) max_tile_area_sb
>>= min_log2_tiles
+ 1;
686 const int max_tile_height_sb
= imax(max_tile_area_sb
/ widest_tile
, 1);
688 hdr
->tiling
.rows
= 0;
689 for (int sby
= 0; sby
< sbh
&& hdr
->tiling
.rows
< DAV1D_MAX_TILE_ROWS
; hdr
->tiling
.rows
++) {
690 const int tile_height_sb
= imin(sbh
- sby
, max_tile_height_sb
);
691 const int tile_h
= (tile_height_sb
> 1) ?
692 1 + dav1d_get_uniform(gb
, tile_height_sb
) :
694 hdr
->tiling
.row_start_sb
[hdr
->tiling
.rows
] = sby
;
697 hdr
->tiling
.log2_rows
= tile_log2(1, hdr
->tiling
.rows
);
699 hdr
->tiling
.col_start_sb
[hdr
->tiling
.cols
] = sbw
;
700 hdr
->tiling
.row_start_sb
[hdr
->tiling
.rows
] = sbh
;
701 if (hdr
->tiling
.log2_cols
|| hdr
->tiling
.log2_rows
) {
702 hdr
->tiling
.update
= dav1d_get_bits(gb
, hdr
->tiling
.log2_cols
+
703 hdr
->tiling
.log2_rows
);
704 if (hdr
->tiling
.update
>= hdr
->tiling
.cols
* hdr
->tiling
.rows
)
706 hdr
->tiling
.n_bytes
= dav1d_get_bits(gb
, 2) + 1;
708 hdr
->tiling
.n_bytes
= 0;
709 hdr
->tiling
.update
= 0;
712 printf("HDR: post-tiling: off=%td\n",
713 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
717 hdr
->quant
.yac
= dav1d_get_bits(gb
, 8);
718 hdr
->quant
.ydc_delta
= dav1d_get_bit(gb
) ? dav1d_get_sbits(gb
, 7) : 0;
719 if (!seqhdr
->monochrome
) {
720 // If the sequence header says that delta_q might be different
721 // for U, V, we must check whether it actually is for this
723 const int diff_uv_delta
= seqhdr
->separate_uv_delta_q
? dav1d_get_bit(gb
) : 0;
724 hdr
->quant
.udc_delta
= dav1d_get_bit(gb
) ? dav1d_get_sbits(gb
, 7) : 0;
725 hdr
->quant
.uac_delta
= dav1d_get_bit(gb
) ? dav1d_get_sbits(gb
, 7) : 0;
727 hdr
->quant
.vdc_delta
= dav1d_get_bit(gb
) ? dav1d_get_sbits(gb
, 7) : 0;
728 hdr
->quant
.vac_delta
= dav1d_get_bit(gb
) ? dav1d_get_sbits(gb
, 7) : 0;
730 hdr
->quant
.vdc_delta
= hdr
->quant
.udc_delta
;
731 hdr
->quant
.vac_delta
= hdr
->quant
.uac_delta
;
735 printf("HDR: post-quant: off=%td\n",
736 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
738 hdr
->quant
.qm
= dav1d_get_bit(gb
);
740 hdr
->quant
.qm_y
= dav1d_get_bits(gb
, 4);
741 hdr
->quant
.qm_u
= dav1d_get_bits(gb
, 4);
743 seqhdr
->separate_uv_delta_q
? dav1d_get_bits(gb
, 4) :
747 printf("HDR: post-qm: off=%td\n",
748 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
752 hdr
->segmentation
.enabled
= dav1d_get_bit(gb
);
753 if (hdr
->segmentation
.enabled
) {
754 if (hdr
->primary_ref_frame
== DAV1D_PRIMARY_REF_NONE
) {
755 hdr
->segmentation
.update_map
= 1;
756 hdr
->segmentation
.temporal
= 0;
757 hdr
->segmentation
.update_data
= 1;
759 hdr
->segmentation
.update_map
= dav1d_get_bit(gb
);
760 hdr
->segmentation
.temporal
=
761 hdr
->segmentation
.update_map
? dav1d_get_bit(gb
) : 0;
762 hdr
->segmentation
.update_data
= dav1d_get_bit(gb
);
765 if (hdr
->segmentation
.update_data
) {
766 hdr
->segmentation
.seg_data
.preskip
= 0;
767 hdr
->segmentation
.seg_data
.last_active_segid
= -1;
768 for (int i
= 0; i
< DAV1D_MAX_SEGMENTS
; i
++) {
769 Dav1dSegmentationData
*const seg
=
770 &hdr
->segmentation
.seg_data
.d
[i
];
771 if (dav1d_get_bit(gb
)) {
772 seg
->delta_q
= dav1d_get_sbits(gb
, 9);
773 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
777 if (dav1d_get_bit(gb
)) {
778 seg
->delta_lf_y_v
= dav1d_get_sbits(gb
, 7);
779 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
781 seg
->delta_lf_y_v
= 0;
783 if (dav1d_get_bit(gb
)) {
784 seg
->delta_lf_y_h
= dav1d_get_sbits(gb
, 7);
785 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
787 seg
->delta_lf_y_h
= 0;
789 if (dav1d_get_bit(gb
)) {
790 seg
->delta_lf_u
= dav1d_get_sbits(gb
, 7);
791 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
795 if (dav1d_get_bit(gb
)) {
796 seg
->delta_lf_v
= dav1d_get_sbits(gb
, 7);
797 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
801 if (dav1d_get_bit(gb
)) {
802 seg
->ref
= dav1d_get_bits(gb
, 3);
803 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
804 hdr
->segmentation
.seg_data
.preskip
= 1;
808 if ((seg
->skip
= dav1d_get_bit(gb
))) {
809 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
810 hdr
->segmentation
.seg_data
.preskip
= 1;
812 if ((seg
->globalmv
= dav1d_get_bit(gb
))) {
813 hdr
->segmentation
.seg_data
.last_active_segid
= i
;
814 hdr
->segmentation
.seg_data
.preskip
= 1;
818 // segmentation.update_data was false so we should copy
819 // segmentation data from the reference frame.
820 assert(hdr
->primary_ref_frame
!= DAV1D_PRIMARY_REF_NONE
);
821 const int pri_ref
= hdr
->refidx
[hdr
->primary_ref_frame
];
822 if (!c
->refs
[pri_ref
].p
.p
.frame_hdr
) goto error
;
823 hdr
->segmentation
.seg_data
=
824 c
->refs
[pri_ref
].p
.p
.frame_hdr
->segmentation
.seg_data
;
827 memset(&hdr
->segmentation
.seg_data
, 0, sizeof(Dav1dSegmentationDataSet
));
828 for (int i
= 0; i
< DAV1D_MAX_SEGMENTS
; i
++)
829 hdr
->segmentation
.seg_data
.d
[i
].ref
= -1;
832 printf("HDR: post-segmentation: off=%td\n",
833 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
837 hdr
->delta
.q
.present
= hdr
->quant
.yac
? dav1d_get_bit(gb
) : 0;
838 hdr
->delta
.q
.res_log2
= hdr
->delta
.q
.present
? dav1d_get_bits(gb
, 2) : 0;
839 hdr
->delta
.lf
.present
= hdr
->delta
.q
.present
&& !hdr
->allow_intrabc
&&
841 hdr
->delta
.lf
.res_log2
= hdr
->delta
.lf
.present
? dav1d_get_bits(gb
, 2) : 0;
842 hdr
->delta
.lf
.multi
= hdr
->delta
.lf
.present
? dav1d_get_bit(gb
) : 0;
844 printf("HDR: post-delta_q_lf_flags: off=%td\n",
845 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
848 // derive lossless flags
849 const int delta_lossless
= !hdr
->quant
.ydc_delta
&& !hdr
->quant
.udc_delta
&&
850 !hdr
->quant
.uac_delta
&& !hdr
->quant
.vdc_delta
&& !hdr
->quant
.vac_delta
;
851 hdr
->all_lossless
= 1;
852 for (int i
= 0; i
< DAV1D_MAX_SEGMENTS
; i
++) {
853 hdr
->segmentation
.qidx
[i
] = hdr
->segmentation
.enabled
?
854 iclip_u8(hdr
->quant
.yac
+ hdr
->segmentation
.seg_data
.d
[i
].delta_q
) :
856 hdr
->segmentation
.lossless
[i
] =
857 !hdr
->segmentation
.qidx
[i
] && delta_lossless
;
858 hdr
->all_lossless
&= hdr
->segmentation
.lossless
[i
];
862 if (hdr
->all_lossless
|| hdr
->allow_intrabc
) {
863 hdr
->loopfilter
.level_y
[0] = hdr
->loopfilter
.level_y
[1] = 0;
864 hdr
->loopfilter
.level_u
= hdr
->loopfilter
.level_v
= 0;
865 hdr
->loopfilter
.sharpness
= 0;
866 hdr
->loopfilter
.mode_ref_delta_enabled
= 1;
867 hdr
->loopfilter
.mode_ref_delta_update
= 1;
868 hdr
->loopfilter
.mode_ref_deltas
= default_mode_ref_deltas
;
870 hdr
->loopfilter
.level_y
[0] = dav1d_get_bits(gb
, 6);
871 hdr
->loopfilter
.level_y
[1] = dav1d_get_bits(gb
, 6);
872 if (!seqhdr
->monochrome
&&
873 (hdr
->loopfilter
.level_y
[0] || hdr
->loopfilter
.level_y
[1]))
875 hdr
->loopfilter
.level_u
= dav1d_get_bits(gb
, 6);
876 hdr
->loopfilter
.level_v
= dav1d_get_bits(gb
, 6);
878 hdr
->loopfilter
.sharpness
= dav1d_get_bits(gb
, 3);
880 if (hdr
->primary_ref_frame
== DAV1D_PRIMARY_REF_NONE
) {
881 hdr
->loopfilter
.mode_ref_deltas
= default_mode_ref_deltas
;
883 const int ref
= hdr
->refidx
[hdr
->primary_ref_frame
];
884 if (!c
->refs
[ref
].p
.p
.frame_hdr
) goto error
;
885 hdr
->loopfilter
.mode_ref_deltas
=
886 c
->refs
[ref
].p
.p
.frame_hdr
->loopfilter
.mode_ref_deltas
;
888 hdr
->loopfilter
.mode_ref_delta_enabled
= dav1d_get_bit(gb
);
889 if (hdr
->loopfilter
.mode_ref_delta_enabled
) {
890 hdr
->loopfilter
.mode_ref_delta_update
= dav1d_get_bit(gb
);
891 if (hdr
->loopfilter
.mode_ref_delta_update
) {
892 for (int i
= 0; i
< 8; i
++)
893 if (dav1d_get_bit(gb
))
894 hdr
->loopfilter
.mode_ref_deltas
.ref_delta
[i
] =
895 dav1d_get_sbits(gb
, 7);
896 for (int i
= 0; i
< 2; i
++)
897 if (dav1d_get_bit(gb
))
898 hdr
->loopfilter
.mode_ref_deltas
.mode_delta
[i
] =
899 dav1d_get_sbits(gb
, 7);
904 printf("HDR: post-lpf: off=%td\n",
905 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
909 if (!hdr
->all_lossless
&& seqhdr
->cdef
&& !hdr
->allow_intrabc
) {
910 hdr
->cdef
.damping
= dav1d_get_bits(gb
, 2) + 3;
911 hdr
->cdef
.n_bits
= dav1d_get_bits(gb
, 2);
912 for (int i
= 0; i
< (1 << hdr
->cdef
.n_bits
); i
++) {
913 hdr
->cdef
.y_strength
[i
] = dav1d_get_bits(gb
, 6);
914 if (!seqhdr
->monochrome
)
915 hdr
->cdef
.uv_strength
[i
] = dav1d_get_bits(gb
, 6);
918 hdr
->cdef
.n_bits
= 0;
919 hdr
->cdef
.y_strength
[0] = 0;
920 hdr
->cdef
.uv_strength
[0] = 0;
923 printf("HDR: post-cdef: off=%td\n",
924 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
928 if ((!hdr
->all_lossless
|| hdr
->super_res
.enabled
) &&
929 seqhdr
->restoration
&& !hdr
->allow_intrabc
)
931 hdr
->restoration
.type
[0] = dav1d_get_bits(gb
, 2);
932 if (!seqhdr
->monochrome
) {
933 hdr
->restoration
.type
[1] = dav1d_get_bits(gb
, 2);
934 hdr
->restoration
.type
[2] = dav1d_get_bits(gb
, 2);
936 hdr
->restoration
.type
[1] =
937 hdr
->restoration
.type
[2] = DAV1D_RESTORATION_NONE
;
940 if (hdr
->restoration
.type
[0] || hdr
->restoration
.type
[1] ||
941 hdr
->restoration
.type
[2])
943 // Log2 of the restoration unit size.
944 hdr
->restoration
.unit_size
[0] = 6 + seqhdr
->sb128
;
945 if (dav1d_get_bit(gb
)) {
946 hdr
->restoration
.unit_size
[0]++;
948 hdr
->restoration
.unit_size
[0] += dav1d_get_bit(gb
);
950 hdr
->restoration
.unit_size
[1] = hdr
->restoration
.unit_size
[0];
951 if ((hdr
->restoration
.type
[1] || hdr
->restoration
.type
[2]) &&
952 seqhdr
->ss_hor
== 1 && seqhdr
->ss_ver
== 1)
954 hdr
->restoration
.unit_size
[1] -= dav1d_get_bit(gb
);
957 hdr
->restoration
.unit_size
[0] = 8;
960 hdr
->restoration
.type
[0] = DAV1D_RESTORATION_NONE
;
961 hdr
->restoration
.type
[1] = DAV1D_RESTORATION_NONE
;
962 hdr
->restoration
.type
[2] = DAV1D_RESTORATION_NONE
;
965 printf("HDR: post-restoration: off=%td\n",
966 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
969 hdr
->txfm_mode
= hdr
->all_lossless
? DAV1D_TX_4X4_ONLY
:
970 dav1d_get_bit(gb
) ? DAV1D_TX_SWITCHABLE
: DAV1D_TX_LARGEST
;
972 printf("HDR: post-txfmmode: off=%td\n",
973 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
975 hdr
->switchable_comp_refs
= IS_INTER_OR_SWITCH(hdr
) ? dav1d_get_bit(gb
) : 0;
977 printf("HDR: post-refmode: off=%td\n",
978 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
980 hdr
->skip_mode_allowed
= 0;
981 if (hdr
->switchable_comp_refs
&& IS_INTER_OR_SWITCH(hdr
) && seqhdr
->order_hint
) {
982 const unsigned poc
= hdr
->frame_offset
;
983 unsigned off_before
= 0xFFFFFFFFU
;
985 int off_before_idx
, off_after_idx
;
986 for (int i
= 0; i
< 7; i
++) {
987 if (!c
->refs
[hdr
->refidx
[i
]].p
.p
.frame_hdr
) goto error
;
988 const unsigned refpoc
= c
->refs
[hdr
->refidx
[i
]].p
.p
.frame_hdr
->frame_offset
;
990 const int diff
= get_poc_diff(seqhdr
->order_hint_n_bits
, refpoc
, poc
);
992 if (off_after
== -1 || get_poc_diff(seqhdr
->order_hint_n_bits
,
993 off_after
, refpoc
) > 0)
998 } else if (diff
< 0 && (off_before
== 0xFFFFFFFFU
||
999 get_poc_diff(seqhdr
->order_hint_n_bits
,
1000 refpoc
, off_before
) > 0))
1002 off_before
= refpoc
;
1007 if (off_before
!= 0xFFFFFFFFU
&& off_after
!= -1) {
1008 hdr
->skip_mode_refs
[0] = imin(off_before_idx
, off_after_idx
);
1009 hdr
->skip_mode_refs
[1] = imax(off_before_idx
, off_after_idx
);
1010 hdr
->skip_mode_allowed
= 1;
1011 } else if (off_before
!= 0xFFFFFFFFU
) {
1012 unsigned off_before2
= 0xFFFFFFFFU
;
1013 int off_before2_idx
;
1014 for (int i
= 0; i
< 7; i
++) {
1015 if (!c
->refs
[hdr
->refidx
[i
]].p
.p
.frame_hdr
) goto error
;
1016 const unsigned refpoc
= c
->refs
[hdr
->refidx
[i
]].p
.p
.frame_hdr
->frame_offset
;
1017 if (get_poc_diff(seqhdr
->order_hint_n_bits
,
1018 refpoc
, off_before
) < 0) {
1019 if (off_before2
== 0xFFFFFFFFU
||
1020 get_poc_diff(seqhdr
->order_hint_n_bits
,
1021 refpoc
, off_before2
) > 0)
1023 off_before2
= refpoc
;
1024 off_before2_idx
= i
;
1029 if (off_before2
!= 0xFFFFFFFFU
) {
1030 hdr
->skip_mode_refs
[0] = imin(off_before_idx
, off_before2_idx
);
1031 hdr
->skip_mode_refs
[1] = imax(off_before_idx
, off_before2_idx
);
1032 hdr
->skip_mode_allowed
= 1;
1036 hdr
->skip_mode_enabled
= hdr
->skip_mode_allowed
? dav1d_get_bit(gb
) : 0;
1038 printf("HDR: post-extskip: off=%td\n",
1039 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
1041 hdr
->warp_motion
= !hdr
->error_resilient_mode
&& IS_INTER_OR_SWITCH(hdr
) &&
1042 seqhdr
->warped_motion
&& dav1d_get_bit(gb
);
1044 printf("HDR: post-warpmotionbit: off=%td\n",
1045 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
1047 hdr
->reduced_txtp_set
= dav1d_get_bit(gb
);
1049 printf("HDR: post-reducedtxtpset: off=%td\n",
1050 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
1053 for (int i
= 0; i
< 7; i
++)
1054 hdr
->gmv
[i
] = dav1d_default_wm_params
;
1056 if (IS_INTER_OR_SWITCH(hdr
)) {
1057 for (int i
= 0; i
< 7; i
++) {
1058 hdr
->gmv
[i
].type
= !dav1d_get_bit(gb
) ? DAV1D_WM_TYPE_IDENTITY
:
1059 dav1d_get_bit(gb
) ? DAV1D_WM_TYPE_ROT_ZOOM
:
1060 dav1d_get_bit(gb
) ? DAV1D_WM_TYPE_TRANSLATION
:
1061 DAV1D_WM_TYPE_AFFINE
;
1063 if (hdr
->gmv
[i
].type
== DAV1D_WM_TYPE_IDENTITY
) continue;
1065 const Dav1dWarpedMotionParams
*ref_gmv
;
1066 if (hdr
->primary_ref_frame
== DAV1D_PRIMARY_REF_NONE
) {
1067 ref_gmv
= &dav1d_default_wm_params
;
1069 const int pri_ref
= hdr
->refidx
[hdr
->primary_ref_frame
];
1070 if (!c
->refs
[pri_ref
].p
.p
.frame_hdr
) goto error
;
1071 ref_gmv
= &c
->refs
[pri_ref
].p
.p
.frame_hdr
->gmv
[i
];
1073 int32_t *const mat
= hdr
->gmv
[i
].matrix
;
1074 const int32_t *const ref_mat
= ref_gmv
->matrix
;
1077 if (hdr
->gmv
[i
].type
>= DAV1D_WM_TYPE_ROT_ZOOM
) {
1078 mat
[2] = (1 << 16) + 2 *
1079 dav1d_get_bits_subexp(gb
, (ref_mat
[2] - (1 << 16)) >> 1, 12);
1080 mat
[3] = 2 * dav1d_get_bits_subexp(gb
, ref_mat
[3] >> 1, 12);
1085 bits
= 9 - !hdr
->hp
;
1086 shift
= 13 + !hdr
->hp
;
1089 if (hdr
->gmv
[i
].type
== DAV1D_WM_TYPE_AFFINE
) {
1090 mat
[4] = 2 * dav1d_get_bits_subexp(gb
, ref_mat
[4] >> 1, 12);
1091 mat
[5] = (1 << 16) + 2 *
1092 dav1d_get_bits_subexp(gb
, (ref_mat
[5] - (1 << 16)) >> 1, 12);
1098 mat
[0] = dav1d_get_bits_subexp(gb
, ref_mat
[0] >> shift
, bits
) * (1 << shift
);
1099 mat
[1] = dav1d_get_bits_subexp(gb
, ref_mat
[1] >> shift
, bits
) * (1 << shift
);
1103 printf("HDR: post-gmv: off=%td\n",
1104 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
1107 hdr
->film_grain
.present
= seqhdr
->film_grain_present
&&
1108 (hdr
->show_frame
|| hdr
->showable_frame
) &&
1110 if (hdr
->film_grain
.present
) {
1111 const unsigned seed
= dav1d_get_bits(gb
, 16);
1112 hdr
->film_grain
.update
= hdr
->frame_type
!= DAV1D_FRAME_TYPE_INTER
|| dav1d_get_bit(gb
);
1113 if (!hdr
->film_grain
.update
) {
1114 const int refidx
= dav1d_get_bits(gb
, 3);
1116 for (i
= 0; i
< 7; i
++)
1117 if (hdr
->refidx
[i
] == refidx
)
1119 if (i
== 7 || !c
->refs
[refidx
].p
.p
.frame_hdr
) goto error
;
1120 hdr
->film_grain
.data
= c
->refs
[refidx
].p
.p
.frame_hdr
->film_grain
.data
;
1121 hdr
->film_grain
.data
.seed
= seed
;
1123 Dav1dFilmGrainData
*const fgd
= &hdr
->film_grain
.data
;
1126 fgd
->num_y_points
= dav1d_get_bits(gb
, 4);
1127 if (fgd
->num_y_points
> 14) goto error
;
1128 for (int i
= 0; i
< fgd
->num_y_points
; i
++) {
1129 fgd
->y_points
[i
][0] = dav1d_get_bits(gb
, 8);
1130 if (i
&& fgd
->y_points
[i
- 1][0] >= fgd
->y_points
[i
][0])
1132 fgd
->y_points
[i
][1] = dav1d_get_bits(gb
, 8);
1135 fgd
->chroma_scaling_from_luma
=
1136 !seqhdr
->monochrome
&& dav1d_get_bit(gb
);
1137 if (seqhdr
->monochrome
|| fgd
->chroma_scaling_from_luma
||
1138 (seqhdr
->ss_ver
== 1 && seqhdr
->ss_hor
== 1 && !fgd
->num_y_points
))
1140 fgd
->num_uv_points
[0] = fgd
->num_uv_points
[1] = 0;
1141 } else for (int pl
= 0; pl
< 2; pl
++) {
1142 fgd
->num_uv_points
[pl
] = dav1d_get_bits(gb
, 4);
1143 if (fgd
->num_uv_points
[pl
] > 10) goto error
;
1144 for (int i
= 0; i
< fgd
->num_uv_points
[pl
]; i
++) {
1145 fgd
->uv_points
[pl
][i
][0] = dav1d_get_bits(gb
, 8);
1146 if (i
&& fgd
->uv_points
[pl
][i
- 1][0] >= fgd
->uv_points
[pl
][i
][0])
1148 fgd
->uv_points
[pl
][i
][1] = dav1d_get_bits(gb
, 8);
1152 if (seqhdr
->ss_hor
== 1 && seqhdr
->ss_ver
== 1 &&
1153 !!fgd
->num_uv_points
[0] != !!fgd
->num_uv_points
[1])
1158 fgd
->scaling_shift
= dav1d_get_bits(gb
, 2) + 8;
1159 fgd
->ar_coeff_lag
= dav1d_get_bits(gb
, 2);
1160 const int num_y_pos
= 2 * fgd
->ar_coeff_lag
* (fgd
->ar_coeff_lag
+ 1);
1161 if (fgd
->num_y_points
)
1162 for (int i
= 0; i
< num_y_pos
; i
++)
1163 fgd
->ar_coeffs_y
[i
] = dav1d_get_bits(gb
, 8) - 128;
1164 for (int pl
= 0; pl
< 2; pl
++)
1165 if (fgd
->num_uv_points
[pl
] || fgd
->chroma_scaling_from_luma
) {
1166 const int num_uv_pos
= num_y_pos
+ !!fgd
->num_y_points
;
1167 for (int i
= 0; i
< num_uv_pos
; i
++)
1168 fgd
->ar_coeffs_uv
[pl
][i
] = dav1d_get_bits(gb
, 8) - 128;
1169 if (!fgd
->num_y_points
)
1170 fgd
->ar_coeffs_uv
[pl
][num_uv_pos
] = 0;
1172 fgd
->ar_coeff_shift
= dav1d_get_bits(gb
, 2) + 6;
1173 fgd
->grain_scale_shift
= dav1d_get_bits(gb
, 2);
1174 for (int pl
= 0; pl
< 2; pl
++)
1175 if (fgd
->num_uv_points
[pl
]) {
1176 fgd
->uv_mult
[pl
] = dav1d_get_bits(gb
, 8) - 128;
1177 fgd
->uv_luma_mult
[pl
] = dav1d_get_bits(gb
, 8) - 128;
1178 fgd
->uv_offset
[pl
] = dav1d_get_bits(gb
, 9) - 256;
1180 fgd
->overlap_flag
= dav1d_get_bit(gb
);
1181 fgd
->clip_to_restricted_range
= dav1d_get_bit(gb
);
1184 memset(&hdr
->film_grain
.data
, 0, sizeof(hdr
->film_grain
.data
));
1187 printf("HDR: post-filmgrain: off=%td\n",
1188 (gb
->ptr
- init_ptr
) * 8 - gb
->bits_left
);
1194 dav1d_log(c
, "Error parsing frame header\n");
1195 return DAV1D_ERR(EINVAL
);
1198 static void parse_tile_hdr(Dav1dContext
*const c
, GetBits
*const gb
) {
1199 const int n_tiles
= c
->frame_hdr
->tiling
.cols
* c
->frame_hdr
->tiling
.rows
;
1200 const int have_tile_pos
= n_tiles
> 1 ? dav1d_get_bit(gb
) : 0;
1202 if (have_tile_pos
) {
1203 const int n_bits
= c
->frame_hdr
->tiling
.log2_cols
+
1204 c
->frame_hdr
->tiling
.log2_rows
;
1205 c
->tile
[c
->n_tile_data
].start
= dav1d_get_bits(gb
, n_bits
);
1206 c
->tile
[c
->n_tile_data
].end
= dav1d_get_bits(gb
, n_bits
);
1208 c
->tile
[c
->n_tile_data
].start
= 0;
1209 c
->tile
[c
->n_tile_data
].end
= n_tiles
- 1;
1213 ptrdiff_t dav1d_parse_obus(Dav1dContext
*const c
, Dav1dData
*const in
) {
1217 dav1d_init_get_bits(&gb
, in
->data
, in
->sz
);
1220 const int obu_forbidden_bit
= dav1d_get_bit(&gb
);
1221 if (c
->strict_std_compliance
&& obu_forbidden_bit
) goto error
;
1222 const enum Dav1dObuType type
= dav1d_get_bits(&gb
, 4);
1223 const int has_extension
= dav1d_get_bit(&gb
);
1224 const int has_length_field
= dav1d_get_bit(&gb
);
1225 dav1d_get_bit(&gb
); // reserved
1227 int temporal_id
= 0, spatial_id
= 0;
1228 if (has_extension
) {
1229 temporal_id
= dav1d_get_bits(&gb
, 3);
1230 spatial_id
= dav1d_get_bits(&gb
, 2);
1231 dav1d_get_bits(&gb
, 3); // reserved
1234 if (has_length_field
) {
1235 const size_t len
= dav1d_get_uleb128(&gb
);
1236 if (len
> (size_t)(gb
.ptr_end
- gb
.ptr
)) goto error
;
1237 gb
.ptr_end
= gb
.ptr
+ len
;
1239 if (gb
.error
) goto error
;
1241 // We must have read a whole number of bytes at this point (1 byte
1242 // for the header and whole bytes at a time when reading the
1243 // leb128 length field).
1244 assert(gb
.bits_left
== 0);
1246 // skip obu not belonging to the selected temporal/spatial layer
1247 if (type
!= DAV1D_OBU_SEQ_HDR
&& type
!= DAV1D_OBU_TD
&&
1248 has_extension
&& c
->operating_point_idc
!= 0)
1250 const int in_temporal_layer
= (c
->operating_point_idc
>> temporal_id
) & 1;
1251 const int in_spatial_layer
= (c
->operating_point_idc
>> (spatial_id
+ 8)) & 1;
1252 if (!in_temporal_layer
|| !in_spatial_layer
)
1253 return gb
.ptr_end
- gb
.ptr_start
;
1257 case DAV1D_OBU_SEQ_HDR
: {
1258 Dav1dRef
*ref
= dav1d_ref_create_using_pool(c
->seq_hdr_pool
,
1259 sizeof(Dav1dSequenceHeader
));
1260 if (!ref
) return DAV1D_ERR(ENOMEM
);
1261 Dav1dSequenceHeader
*seq_hdr
= ref
->data
;
1262 if ((res
= parse_seq_hdr(seq_hdr
, &gb
, c
->strict_std_compliance
)) < 0) {
1263 dav1d_log(c
, "Error parsing sequence header\n");
1264 dav1d_ref_dec(&ref
);
1269 c
->operating_point
< seq_hdr
->num_operating_points
? c
->operating_point
: 0;
1270 c
->operating_point_idc
= seq_hdr
->operating_points
[op_idx
].idc
;
1271 const unsigned spatial_mask
= c
->operating_point_idc
>> 8;
1272 c
->max_spatial_id
= spatial_mask
? ulog2(spatial_mask
) : 0;
1274 // If we have read a sequence header which is different from
1275 // the old one, this is a new video sequence and can't use any
1276 // previous state. Free that state.
1279 c
->frame_hdr
= NULL
;
1280 c
->frame_flags
|= PICTURE_FLAG_NEW_SEQUENCE
;
1281 // see 7.5, operating_parameter_info is allowed to change in
1282 // sequence headers of a single sequence
1283 } else if (memcmp(seq_hdr
, c
->seq_hdr
, offsetof(Dav1dSequenceHeader
, operating_parameter_info
))) {
1284 c
->frame_hdr
= NULL
;
1285 c
->mastering_display
= NULL
;
1286 c
->content_light
= NULL
;
1287 dav1d_ref_dec(&c
->mastering_display_ref
);
1288 dav1d_ref_dec(&c
->content_light_ref
);
1289 for (int i
= 0; i
< 8; i
++) {
1290 if (c
->refs
[i
].p
.p
.frame_hdr
)
1291 dav1d_thread_picture_unref(&c
->refs
[i
].p
);
1292 dav1d_ref_dec(&c
->refs
[i
].segmap
);
1293 dav1d_ref_dec(&c
->refs
[i
].refmvs
);
1294 dav1d_cdf_thread_unref(&c
->cdf
[i
]);
1296 c
->frame_flags
|= PICTURE_FLAG_NEW_SEQUENCE
;
1297 // If operating_parameter_info changed, signal it
1298 } else if (memcmp(seq_hdr
->operating_parameter_info
, c
->seq_hdr
->operating_parameter_info
,
1299 sizeof(seq_hdr
->operating_parameter_info
)))
1301 c
->frame_flags
|= PICTURE_FLAG_NEW_OP_PARAMS_INFO
;
1303 dav1d_ref_dec(&c
->seq_hdr_ref
);
1304 c
->seq_hdr_ref
= ref
;
1305 c
->seq_hdr
= seq_hdr
;
1308 case DAV1D_OBU_REDUNDANT_FRAME_HDR
:
1309 if (c
->frame_hdr
) break;
1311 case DAV1D_OBU_FRAME
:
1312 case DAV1D_OBU_FRAME_HDR
:
1313 if (!c
->seq_hdr
) goto error
;
1314 if (!c
->frame_hdr_ref
) {
1315 c
->frame_hdr_ref
= dav1d_ref_create_using_pool(c
->frame_hdr_pool
,
1316 sizeof(Dav1dFrameHeader
));
1317 if (!c
->frame_hdr_ref
) return DAV1D_ERR(ENOMEM
);
1320 // ensure that the reference is writable
1321 assert(dav1d_ref_is_writable(c
->frame_hdr_ref
));
1323 c
->frame_hdr
= c
->frame_hdr_ref
->data
;
1324 memset(c
->frame_hdr
, 0, sizeof(*c
->frame_hdr
));
1325 c
->frame_hdr
->temporal_id
= temporal_id
;
1326 c
->frame_hdr
->spatial_id
= spatial_id
;
1327 if ((res
= parse_frame_hdr(c
, &gb
)) < 0) {
1328 c
->frame_hdr
= NULL
;
1331 for (int n
= 0; n
< c
->n_tile_data
; n
++)
1332 dav1d_data_unref_internal(&c
->tile
[n
].data
);
1335 if (type
!= DAV1D_OBU_FRAME
) {
1336 // This is actually a frame header OBU so read the
1337 // trailing bit and check for overrun.
1338 if (check_trailing_bits(&gb
, c
->strict_std_compliance
) < 0) {
1339 c
->frame_hdr
= NULL
;
1344 if (c
->frame_size_limit
&& (int64_t)c
->frame_hdr
->width
[1] *
1345 c
->frame_hdr
->height
> c
->frame_size_limit
)
1347 dav1d_log(c
, "Frame size %dx%d exceeds limit %u\n", c
->frame_hdr
->width
[1],
1348 c
->frame_hdr
->height
, c
->frame_size_limit
);
1349 c
->frame_hdr
= NULL
;
1350 return DAV1D_ERR(ERANGE
);
1353 if (type
!= DAV1D_OBU_FRAME
)
1355 // OBU_FRAMEs shouldn't be signaled with show_existing_frame
1356 if (c
->frame_hdr
->show_existing_frame
) {
1357 c
->frame_hdr
= NULL
;
1361 // This is the frame header at the start of a frame OBU.
1362 // There's no trailing bit at the end to skip, but we do need
1363 // to align to the next byte.
1364 dav1d_bytealign_get_bits(&gb
);
1366 case DAV1D_OBU_TILE_GRP
: {
1367 if (!c
->frame_hdr
) goto error
;
1368 if (c
->n_tile_data_alloc
< c
->n_tile_data
+ 1) {
1369 if ((c
->n_tile_data
+ 1) > INT_MAX
/ (int)sizeof(*c
->tile
)) goto error
;
1370 struct Dav1dTileGroup
*tile
= dav1d_realloc(ALLOC_TILE
, c
->tile
,
1371 (c
->n_tile_data
+ 1) * sizeof(*c
->tile
));
1372 if (!tile
) goto error
;
1374 memset(c
->tile
+ c
->n_tile_data
, 0, sizeof(*c
->tile
));
1375 c
->n_tile_data_alloc
= c
->n_tile_data
+ 1;
1377 parse_tile_hdr(c
, &gb
);
1378 // Align to the next byte boundary and check for overrun.
1379 dav1d_bytealign_get_bits(&gb
);
1380 if (gb
.error
) goto error
;
1382 dav1d_data_ref(&c
->tile
[c
->n_tile_data
].data
, in
);
1383 c
->tile
[c
->n_tile_data
].data
.data
= gb
.ptr
;
1384 c
->tile
[c
->n_tile_data
].data
.sz
= (size_t)(gb
.ptr_end
- gb
.ptr
);
1385 // ensure tile groups are in order and sane, see 6.10.1
1386 if (c
->tile
[c
->n_tile_data
].start
> c
->tile
[c
->n_tile_data
].end
||
1387 c
->tile
[c
->n_tile_data
].start
!= c
->n_tiles
)
1389 for (int i
= 0; i
<= c
->n_tile_data
; i
++)
1390 dav1d_data_unref_internal(&c
->tile
[i
].data
);
1395 c
->n_tiles
+= 1 + c
->tile
[c
->n_tile_data
].end
-
1396 c
->tile
[c
->n_tile_data
].start
;
1400 case DAV1D_OBU_METADATA
: {
1401 #define DEBUG_OBU_METADATA 0
1402 #if DEBUG_OBU_METADATA
1403 const uint8_t *const init_ptr
= gb
.ptr
;
1405 // obu metadta type field
1406 const enum ObuMetaType meta_type
= dav1d_get_uleb128(&gb
);
1407 if (gb
.error
) goto error
;
1409 switch (meta_type
) {
1410 case OBU_META_HDR_CLL
: {
1411 Dav1dRef
*ref
= dav1d_ref_create(ALLOC_OBU_META
,
1412 sizeof(Dav1dContentLightLevel
));
1413 if (!ref
) return DAV1D_ERR(ENOMEM
);
1414 Dav1dContentLightLevel
*const content_light
= ref
->data
;
1416 content_light
->max_content_light_level
= dav1d_get_bits(&gb
, 16);
1417 #if DEBUG_OBU_METADATA
1418 printf("CLLOBU: max-content-light-level: %d [off=%td]\n",
1419 content_light
->max_content_light_level
,
1420 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1422 content_light
->max_frame_average_light_level
= dav1d_get_bits(&gb
, 16);
1423 #if DEBUG_OBU_METADATA
1424 printf("CLLOBU: max-frame-average-light-level: %d [off=%td]\n",
1425 content_light
->max_frame_average_light_level
,
1426 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1429 if (check_trailing_bits(&gb
, c
->strict_std_compliance
) < 0) {
1430 dav1d_ref_dec(&ref
);
1434 dav1d_ref_dec(&c
->content_light_ref
);
1435 c
->content_light
= content_light
;
1436 c
->content_light_ref
= ref
;
1439 case OBU_META_HDR_MDCV
: {
1440 Dav1dRef
*ref
= dav1d_ref_create(ALLOC_OBU_META
,
1441 sizeof(Dav1dMasteringDisplay
));
1442 if (!ref
) return DAV1D_ERR(ENOMEM
);
1443 Dav1dMasteringDisplay
*const mastering_display
= ref
->data
;
1445 for (int i
= 0; i
< 3; i
++) {
1446 mastering_display
->primaries
[i
][0] = dav1d_get_bits(&gb
, 16);
1447 mastering_display
->primaries
[i
][1] = dav1d_get_bits(&gb
, 16);
1448 #if DEBUG_OBU_METADATA
1449 printf("MDCVOBU: primaries[%d]: (%d, %d) [off=%td]\n", i
,
1450 mastering_display
->primaries
[i
][0],
1451 mastering_display
->primaries
[i
][1],
1452 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1455 mastering_display
->white_point
[0] = dav1d_get_bits(&gb
, 16);
1456 #if DEBUG_OBU_METADATA
1457 printf("MDCVOBU: white-point-x: %d [off=%td]\n",
1458 mastering_display
->white_point
[0],
1459 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1461 mastering_display
->white_point
[1] = dav1d_get_bits(&gb
, 16);
1462 #if DEBUG_OBU_METADATA
1463 printf("MDCVOBU: white-point-y: %d [off=%td]\n",
1464 mastering_display
->white_point
[1],
1465 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1467 mastering_display
->max_luminance
= dav1d_get_bits(&gb
, 32);
1468 #if DEBUG_OBU_METADATA
1469 printf("MDCVOBU: max-luminance: %d [off=%td]\n",
1470 mastering_display
->max_luminance
,
1471 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1473 mastering_display
->min_luminance
= dav1d_get_bits(&gb
, 32);
1474 #if DEBUG_OBU_METADATA
1475 printf("MDCVOBU: min-luminance: %d [off=%td]\n",
1476 mastering_display
->min_luminance
,
1477 (gb
.ptr
- init_ptr
) * 8 - gb
.bits_left
);
1479 if (check_trailing_bits(&gb
, c
->strict_std_compliance
) < 0) {
1480 dav1d_ref_dec(&ref
);
1484 dav1d_ref_dec(&c
->mastering_display_ref
);
1485 c
->mastering_display
= mastering_display
;
1486 c
->mastering_display_ref
= ref
;
1489 case OBU_META_ITUT_T35
: {
1490 ptrdiff_t payload_size
= gb
.ptr_end
- gb
.ptr
;
1491 // Don't take into account all the trailing bits for payload_size
1492 while (payload_size
> 0 && !gb
.ptr
[payload_size
- 1])
1493 payload_size
--; // trailing_zero_bit x 8
1494 payload_size
--; // trailing_one_bit + trailing_zero_bit x 7
1496 int country_code_extension_byte
= 0;
1497 const int country_code
= dav1d_get_bits(&gb
, 8);
1499 if (country_code
== 0xFF) {
1500 country_code_extension_byte
= dav1d_get_bits(&gb
, 8);
1504 if (payload_size
<= 0 || gb
.ptr
[payload_size
] != 0x80) {
1505 dav1d_log(c
, "Malformed ITU-T T.35 metadata message format\n");
1509 if ((c
->n_itut_t35
+ 1) > INT_MAX
/ (int)sizeof(*c
->itut_t35
)) goto error
;
1510 struct Dav1dITUTT35
*itut_t35
= dav1d_realloc(ALLOC_OBU_META
, c
->itut_t35
,
1511 (c
->n_itut_t35
+ 1) * sizeof(*c
->itut_t35
));
1512 if (!itut_t35
) goto error
;
1513 c
->itut_t35
= itut_t35
;
1514 memset(c
->itut_t35
+ c
->n_itut_t35
, 0, sizeof(*c
->itut_t35
));
1516 struct itut_t35_ctx_context
*itut_t35_ctx
;
1517 if (!c
->n_itut_t35
) {
1518 assert(!c
->itut_t35_ref
);
1519 itut_t35_ctx
= dav1d_malloc(ALLOC_OBU_META
, sizeof(struct itut_t35_ctx_context
));
1520 if (!itut_t35_ctx
) goto error
;
1521 c
->itut_t35_ref
= dav1d_ref_init(&itut_t35_ctx
->ref
, c
->itut_t35
,
1522 dav1d_picture_free_itut_t35
, itut_t35_ctx
, 0);
1524 assert(c
->itut_t35_ref
&& atomic_load(&c
->itut_t35_ref
->ref_cnt
) == 1);
1525 itut_t35_ctx
= c
->itut_t35_ref
->user_data
;
1526 c
->itut_t35_ref
->const_data
= (uint8_t *)c
->itut_t35
;
1528 itut_t35_ctx
->itut_t35
= c
->itut_t35
;
1529 itut_t35_ctx
->n_itut_t35
= c
->n_itut_t35
+ 1;
1531 Dav1dITUTT35
*const itut_t35_metadata
= &c
->itut_t35
[c
->n_itut_t35
];
1532 itut_t35_metadata
->payload
= dav1d_malloc(ALLOC_OBU_META
, payload_size
);
1533 if (!itut_t35_metadata
->payload
) goto error
;
1535 itut_t35_metadata
->country_code
= country_code
;
1536 itut_t35_metadata
->country_code_extension_byte
= country_code_extension_byte
;
1537 itut_t35_metadata
->payload_size
= payload_size
;
1539 // We know that we've read a whole number of bytes and that the
1540 // payload is within the OBU boundaries, so just use memcpy()
1541 assert(gb
.bits_left
== 0);
1542 memcpy(itut_t35_metadata
->payload
, gb
.ptr
, payload_size
);
1547 case OBU_META_SCALABILITY
:
1548 case OBU_META_TIMECODE
:
1549 // ignore metadata OBUs we don't care about
1552 // print a warning but don't fail for unknown types
1553 dav1d_log(c
, "Unknown Metadata OBU type %d\n", meta_type
);
1560 c
->frame_flags
|= PICTURE_FLAG_NEW_TEMPORAL_UNIT
;
1562 case DAV1D_OBU_PADDING
:
1563 // ignore OBUs we don't care about
1566 // print a warning but don't fail for unknown types
1567 dav1d_log(c
, "Unknown OBU type %d of size %td\n", type
, gb
.ptr_end
- gb
.ptr
);
1571 if (c
->seq_hdr
&& c
->frame_hdr
) {
1572 if (c
->frame_hdr
->show_existing_frame
) {
1573 if (!c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
.p
.frame_hdr
) goto error
;
1574 switch (c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
.p
.frame_hdr
->frame_type
) {
1575 case DAV1D_FRAME_TYPE_INTER
:
1576 case DAV1D_FRAME_TYPE_SWITCH
:
1577 if (c
->decode_frame_type
> DAV1D_DECODEFRAMETYPE_REFERENCE
)
1580 case DAV1D_FRAME_TYPE_INTRA
:
1581 if (c
->decode_frame_type
> DAV1D_DECODEFRAMETYPE_INTRA
)
1587 if (!c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
.p
.data
[0]) goto error
;
1588 if (c
->strict_std_compliance
&&
1589 !c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
.showable
)
1594 dav1d_thread_picture_ref(&c
->out
,
1595 &c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
);
1596 dav1d_picture_copy_props(&c
->out
.p
,
1597 c
->content_light
, c
->content_light_ref
,
1598 c
->mastering_display
, c
->mastering_display_ref
,
1599 c
->itut_t35
, c
->itut_t35_ref
, c
->n_itut_t35
,
1601 // Must be removed from the context after being attached to the frame
1602 dav1d_ref_dec(&c
->itut_t35_ref
);
1605 c
->event_flags
|= dav1d_picture_get_event_flags(&c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
);
1607 pthread_mutex_lock(&c
->task_thread
.lock
);
1608 // need to append this to the frame output queue
1609 const unsigned next
= c
->frame_thread
.next
++;
1610 if (c
->frame_thread
.next
== c
->n_fc
)
1611 c
->frame_thread
.next
= 0;
1613 Dav1dFrameContext
*const f
= &c
->fc
[next
];
1614 while (f
->n_tile_data
> 0)
1615 pthread_cond_wait(&f
->task_thread
.cond
,
1616 &f
->task_thread
.ttd
->lock
);
1617 Dav1dThreadPicture
*const out_delayed
=
1618 &c
->frame_thread
.out_delayed
[next
];
1619 if (out_delayed
->p
.data
[0] || atomic_load(&f
->task_thread
.error
)) {
1620 unsigned first
= atomic_load(&c
->task_thread
.first
);
1621 if (first
+ 1U < c
->n_fc
)
1622 atomic_fetch_add(&c
->task_thread
.first
, 1U);
1624 atomic_store(&c
->task_thread
.first
, 0);
1625 atomic_compare_exchange_strong(&c
->task_thread
.reset_task_cur
,
1627 if (c
->task_thread
.cur
&& c
->task_thread
.cur
< c
->n_fc
)
1628 c
->task_thread
.cur
--;
1630 const int error
= f
->task_thread
.retval
;
1632 c
->cached_error
= error
;
1633 f
->task_thread
.retval
= 0;
1634 dav1d_data_props_copy(&c
->cached_error_props
, &out_delayed
->p
.m
);
1635 dav1d_thread_picture_unref(out_delayed
);
1636 } else if (out_delayed
->p
.data
[0]) {
1637 const unsigned progress
= atomic_load_explicit(&out_delayed
->progress
[1],
1638 memory_order_relaxed
);
1639 if ((out_delayed
->visible
|| c
->output_invisible_frames
) &&
1640 progress
!= FRAME_ERROR
)
1642 dav1d_thread_picture_ref(&c
->out
, out_delayed
);
1643 c
->event_flags
|= dav1d_picture_get_event_flags(out_delayed
);
1645 dav1d_thread_picture_unref(out_delayed
);
1647 dav1d_thread_picture_ref(out_delayed
,
1648 &c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
);
1649 out_delayed
->visible
= 1;
1650 dav1d_picture_copy_props(&out_delayed
->p
,
1651 c
->content_light
, c
->content_light_ref
,
1652 c
->mastering_display
, c
->mastering_display_ref
,
1653 c
->itut_t35
, c
->itut_t35_ref
, c
->n_itut_t35
,
1655 // Must be removed from the context after being attached to the frame
1656 dav1d_ref_dec(&c
->itut_t35_ref
);
1660 pthread_mutex_unlock(&c
->task_thread
.lock
);
1662 if (c
->refs
[c
->frame_hdr
->existing_frame_idx
].p
.p
.frame_hdr
->frame_type
== DAV1D_FRAME_TYPE_KEY
) {
1663 const int r
= c
->frame_hdr
->existing_frame_idx
;
1664 c
->refs
[r
].p
.showable
= 0;
1665 for (int i
= 0; i
< 8; i
++) {
1666 if (i
== r
) continue;
1668 if (c
->refs
[i
].p
.p
.frame_hdr
)
1669 dav1d_thread_picture_unref(&c
->refs
[i
].p
);
1670 dav1d_thread_picture_ref(&c
->refs
[i
].p
, &c
->refs
[r
].p
);
1672 dav1d_cdf_thread_unref(&c
->cdf
[i
]);
1673 dav1d_cdf_thread_ref(&c
->cdf
[i
], &c
->cdf
[r
]);
1675 dav1d_ref_dec(&c
->refs
[i
].segmap
);
1676 c
->refs
[i
].segmap
= c
->refs
[r
].segmap
;
1677 if (c
->refs
[r
].segmap
)
1678 dav1d_ref_inc(c
->refs
[r
].segmap
);
1679 dav1d_ref_dec(&c
->refs
[i
].refmvs
);
1682 c
->frame_hdr
= NULL
;
1683 } else if (c
->n_tiles
== c
->frame_hdr
->tiling
.cols
* c
->frame_hdr
->tiling
.rows
) {
1684 switch (c
->frame_hdr
->frame_type
) {
1685 case DAV1D_FRAME_TYPE_INTER
:
1686 case DAV1D_FRAME_TYPE_SWITCH
:
1687 if (c
->decode_frame_type
> DAV1D_DECODEFRAMETYPE_REFERENCE
||
1688 (c
->decode_frame_type
== DAV1D_DECODEFRAMETYPE_REFERENCE
&&
1689 !c
->frame_hdr
->refresh_frame_flags
))
1692 case DAV1D_FRAME_TYPE_INTRA
:
1693 if (c
->decode_frame_type
> DAV1D_DECODEFRAMETYPE_INTRA
||
1694 (c
->decode_frame_type
== DAV1D_DECODEFRAMETYPE_REFERENCE
&&
1695 !c
->frame_hdr
->refresh_frame_flags
))
1701 if (!c
->n_tile_data
)
1703 if ((res
= dav1d_submit_frame(c
)) < 0)
1705 assert(!c
->n_tile_data
);
1706 c
->frame_hdr
= NULL
;
1711 return gb
.ptr_end
- gb
.ptr_start
;
1714 // update refs with only the headers in case we skip the frame
1715 for (int i
= 0; i
< 8; i
++) {
1716 if (c
->frame_hdr
->refresh_frame_flags
& (1 << i
)) {
1717 dav1d_thread_picture_unref(&c
->refs
[i
].p
);
1718 c
->refs
[i
].p
.p
.frame_hdr
= c
->frame_hdr
;
1719 c
->refs
[i
].p
.p
.seq_hdr
= c
->seq_hdr
;
1720 c
->refs
[i
].p
.p
.frame_hdr_ref
= c
->frame_hdr_ref
;
1721 c
->refs
[i
].p
.p
.seq_hdr_ref
= c
->seq_hdr_ref
;
1722 dav1d_ref_inc(c
->frame_hdr_ref
);
1723 dav1d_ref_inc(c
->seq_hdr_ref
);
1727 dav1d_ref_dec(&c
->frame_hdr_ref
);
1728 c
->frame_hdr
= NULL
;
1731 return gb
.ptr_end
- gb
.ptr_start
;
1734 dav1d_data_props_copy(&c
->cached_error_props
, &in
->m
);
1735 dav1d_log(c
, gb
.error
? "Overrun in OBU bit buffer\n" :
1736 "Error parsing OBU data\n");
1737 return DAV1D_ERR(EINVAL
);