Coverage Report

Created: 2026-12-01 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/obu.c
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
24.2k
{
51
24.2k
    const int trailing_one_bit = dav1d_get_bit(gb);
52
53
24.2k
    if (gb->error)
54
209
        return DAV1D_ERR(EINVAL);
55
56
24.0k
    if (!strict_std_compliance)
57
24.0k
        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
22.5k
{
76
22.5k
#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
22.5k
    memset(hdr, 0, sizeof(*hdr));
83
22.5k
    hdr->profile = dav1d_get_bits(gb, 3);
84
22.5k
    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
22.5k
    hdr->still_picture = dav1d_get_bit(gb);
91
22.5k
    hdr->reduced_still_picture_header = dav1d_get_bit(gb);
92
22.5k
    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
22.5k
    if (hdr->reduced_still_picture_header) {
99
13.9k
        hdr->num_operating_points = 1;
100
13.9k
        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);
101
13.9k
        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);
102
13.9k
        hdr->operating_points[0].initial_display_delay = 10;
103
13.9k
    } else {
104
8.53k
        hdr->timing_info_present = dav1d_get_bit(gb);
105
8.53k
        if (hdr->timing_info_present) {
106
194
            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);
107
194
            hdr->time_scale = dav1d_get_bits(gb, 32);
108
194
            if (strict_std_compliance && (!hdr->num_units_in_tick || !hdr->time_scale))
109
0
                goto error;
110
194
            hdr->equal_picture_interval = dav1d_get_bit(gb);
111
194
            if (hdr->equal_picture_interval) {
112
123
                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
113
123
                if (num_ticks_per_picture == UINT32_MAX)
114
0
                    goto error;
115
123
                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
116
123
            }
117
118
194
            hdr->decoder_model_info_present = dav1d_get_bit(gb);
119
194
            if (hdr->decoder_model_info_present) {
120
146
                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;
121
146
                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);
122
146
                if (strict_std_compliance && !hdr->num_units_in_decoding_tick)
123
0
                    goto error;
124
146
                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;
125
146
                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;
126
146
            }
127
194
        }
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
8.53k
        hdr->display_model_info_present = dav1d_get_bit(gb);
134
8.53k
        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;
135
19.1k
        for (int i = 0; i < hdr->num_operating_points; i++) {
136
10.7k
            struct Dav1dSequenceHeaderOperatingPoint *const op =
137
10.7k
                &hdr->operating_points[i];
138
10.7k
            op->idc = dav1d_get_bits(gb, 12);
139
10.7k
            if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00)))
140
43
                goto error;
141
10.6k
            op->major_level = 2 + dav1d_get_bits(gb, 3);
142
10.6k
            op->minor_level = dav1d_get_bits(gb, 2);
143
10.6k
            if (op->major_level > 3)
144
6.61k
                op->tier = dav1d_get_bit(gb);
145
10.6k
            if (hdr->decoder_model_info_present) {
146
678
                op->decoder_model_param_present = dav1d_get_bit(gb);
147
678
                if (op->decoder_model_param_present) {
148
309
                    struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
149
309
                        &hdr->operating_parameter_info[i];
150
309
                    opi->decoder_buffer_delay =
151
309
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
152
309
                    opi->encoder_buffer_delay =
153
309
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
154
309
                    opi->low_delay_mode = dav1d_get_bit(gb);
155
309
                }
156
678
            }
157
10.6k
            if (hdr->display_model_info_present)
158
2.72k
                op->display_model_param_present = dav1d_get_bit(gb);
159
10.6k
            op->initial_display_delay =
160
10.6k
                op->display_model_param_present ? dav1d_get_bits(gb, 4) + 1 : 10;
161
10.6k
        }
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
8.53k
    }
167
168
22.4k
    hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;
169
22.4k
    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;
170
22.4k
    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;
171
22.4k
    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
22.4k
    if (!hdr->reduced_still_picture_header) {
177
8.48k
        hdr->frame_id_numbers_present = dav1d_get_bit(gb);
178
8.48k
        if (hdr->frame_id_numbers_present) {
179
570
            hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;
180
570
            hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;
181
570
        }
182
8.48k
    }
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
22.4k
    hdr->sb128 = dav1d_get_bit(gb);
189
22.4k
    hdr->filter_intra = dav1d_get_bit(gb);
190
22.4k
    hdr->intra_edge_filter = dav1d_get_bit(gb);
191
22.4k
    if (hdr->reduced_still_picture_header) {
192
13.9k
        hdr->screen_content_tools = DAV1D_ADAPTIVE;
193
13.9k
        hdr->force_integer_mv = DAV1D_ADAPTIVE;
194
13.9k
    } else {
195
8.48k
        hdr->inter_intra = dav1d_get_bit(gb);
196
8.48k
        hdr->masked_compound = dav1d_get_bit(gb);
197
8.48k
        hdr->warped_motion = dav1d_get_bit(gb);
198
8.48k
        hdr->dual_filter = dav1d_get_bit(gb);
199
8.48k
        hdr->order_hint = dav1d_get_bit(gb);
200
8.48k
        if (hdr->order_hint) {
201
5.62k
            hdr->jnt_comp = dav1d_get_bit(gb);
202
5.62k
            hdr->ref_frame_mvs = dav1d_get_bit(gb);
203
5.62k
        }
204
8.48k
        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
8.48k
        hdr->force_integer_mv = hdr->screen_content_tools ?
210
7.41k
                                dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb) : 2;
211
8.48k
        if (hdr->order_hint)
212
5.62k
            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;
213
8.48k
    }
214
22.4k
    hdr->super_res = dav1d_get_bit(gb);
215
22.4k
    hdr->cdef = dav1d_get_bit(gb);
216
22.4k
    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
22.4k
    hdr->hbd = dav1d_get_bit(gb);
223
22.4k
    if (hdr->profile == 2 && hdr->hbd)
224
3.83k
        hdr->hbd += dav1d_get_bit(gb);
225
22.4k
    if (hdr->profile != 1)
226
15.8k
        hdr->monochrome = dav1d_get_bit(gb);
227
22.4k
    hdr->color_description_present = dav1d_get_bit(gb);
228
22.4k
    if (hdr->color_description_present) {
229
3.63k
        hdr->pri = dav1d_get_bits(gb, 8);
230
3.63k
        hdr->trc = dav1d_get_bits(gb, 8);
231
3.63k
        hdr->mtrx = dav1d_get_bits(gb, 8);
232
18.8k
    } else {
233
18.8k
        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;
234
18.8k
        hdr->trc = DAV1D_TRC_UNKNOWN;
235
18.8k
        hdr->mtrx = DAV1D_MC_UNKNOWN;
236
18.8k
    }
237
22.4k
    if (hdr->monochrome) {
238
6.24k
        hdr->color_range = dav1d_get_bit(gb);
239
6.24k
        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;
240
6.24k
        hdr->ss_hor = hdr->ss_ver = 1;
241
6.24k
        hdr->chr = DAV1D_CHR_UNKNOWN;
242
16.2k
    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&
243
79
               hdr->trc == DAV1D_TRC_SRGB &&
244
36
               hdr->mtrx == DAV1D_MC_IDENTITY)
245
9
    {
246
9
        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
247
9
        hdr->color_range = 1;
248
9
        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))
249
2
            goto error;
250
16.2k
    } else {
251
16.2k
        hdr->color_range = dav1d_get_bit(gb);
252
16.2k
        switch (hdr->profile) {
253
7.09k
        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;
254
7.09k
                hdr->ss_hor = hdr->ss_ver = 1;
255
7.09k
                break;
256
6.62k
        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
257
6.62k
                break;
258
2.50k
        case 2:
259
2.50k
            if (hdr->hbd == 2) {
260
1.11k
                hdr->ss_hor = dav1d_get_bit(gb);
261
1.11k
                if (hdr->ss_hor)
262
441
                    hdr->ss_ver = dav1d_get_bit(gb);
263
1.11k
            } else
264
1.38k
                hdr->ss_hor = 1;
265
2.50k
            hdr->layout = hdr->ss_hor ?
266
1.82k
                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :
267
1.82k
                                        DAV1D_PIXEL_LAYOUT_I422 :
268
2.50k
                                        DAV1D_PIXEL_LAYOUT_I444;
269
2.50k
            break;
270
16.2k
        }
271
16.2k
        hdr->chr = (hdr->ss_hor & hdr->ss_ver) ?
272
9.03k
                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;
273
16.2k
    }
274
22.4k
    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
22.4k
    if (!hdr->monochrome)
280
16.2k
        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
22.4k
    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
22.4k
    return check_trailing_bits(gb, strict_std_compliance);
297
298
81
error:
299
81
    return DAV1D_ERR(EINVAL);
300
22.4k
}
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
33.6k
{
344
33.6k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
345
33.6k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
346
347
33.6k
    if (use_ref) {
348
11.0k
        for (int i = 0; i < 7; i++) {
349
10.8k
            if (dav1d_get_bit(gb)) {
350
4.96k
                const Dav1dThreadPicture *const ref =
351
4.96k
                    &c->refs[c->frame_hdr->refidx[i]].p;
352
4.96k
                if (!ref->p.frame_hdr) return -1;
353
4.95k
                hdr->width[1] = ref->p.frame_hdr->width[1];
354
4.95k
                hdr->height = ref->p.frame_hdr->height;
355
4.95k
                if (hdr->width[1] > seqhdr->max_width ||
356
4.95k
                    hdr->height > seqhdr->max_height)
357
0
                {
358
0
                    return -1;
359
0
                }
360
4.95k
                hdr->render_width = ref->p.frame_hdr->render_width;
361
4.95k
                hdr->render_height = ref->p.frame_hdr->render_height;
362
4.95k
                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
363
4.95k
                if (hdr->super_res.enabled) {
364
1.35k
                    const int d = hdr->super_res.width_scale_denominator =
365
1.35k
                        9 + dav1d_get_bits(gb, 3);
366
1.35k
                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,
367
1.35k
                                         imin(16, hdr->width[1]));
368
3.59k
                } else {
369
3.59k
                    hdr->super_res.width_scale_denominator = 8;
370
3.59k
                    hdr->width[0] = hdr->width[1];
371
3.59k
                }
372
4.95k
                return 0;
373
4.95k
            }
374
10.8k
        }
375
5.08k
    }
376
377
28.6k
    if (hdr->frame_size_override) {
378
2.90k
        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;
379
2.90k
        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;
380
2.90k
        if (hdr->width[1] > seqhdr->max_width ||
381
2.89k
            hdr->height > seqhdr->max_height)
382
45
        {
383
45
            return -1;
384
45
        }
385
25.7k
    } else {
386
25.7k
        hdr->width[1] = seqhdr->max_width;
387
25.7k
        hdr->height = seqhdr->max_height;
388
25.7k
    }
389
28.6k
    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
390
28.6k
    if (hdr->super_res.enabled) {
391
7.21k
        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);
392
7.21k
        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));
393
21.4k
    } else {
394
21.4k
        hdr->super_res.width_scale_denominator = 8;
395
21.4k
        hdr->width[0] = hdr->width[1];
396
21.4k
    }
397
28.6k
    hdr->have_render_size = dav1d_get_bit(gb);
398
28.6k
    if (hdr->have_render_size) {
399
10.3k
        hdr->render_width = dav1d_get_bits(gb, 16) + 1;
400
10.3k
        hdr->render_height = dav1d_get_bits(gb, 16) + 1;
401
18.2k
    } else {
402
18.2k
        hdr->render_width = hdr->width[1];
403
18.2k
        hdr->render_height = hdr->height;
404
18.2k
    }
405
28.6k
    return 0;
406
28.6k
}
407
408
164k
static inline int tile_log2(const int sz, const int tgt) {
409
164k
    int k;
410
220k
    for (k = 0; (sz << k) < tgt; k++) ;
411
164k
    return k;
412
164k
}
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
34.2k
static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {
420
34.2k
#define DEBUG_FRAME_HDR 0
421
422
#if DEBUG_FRAME_HDR
423
    const uint8_t *const init_ptr = gb->ptr;
424
#endif
425
34.2k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
426
34.2k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
427
428
34.2k
    if (!seqhdr->reduced_still_picture_header)
429
20.1k
        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
34.2k
    if (hdr->show_existing_frame) {
435
586
        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);
436
586
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
437
3
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
438
586
        if (seqhdr->frame_id_numbers_present) {
439
18
            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
440
18
            Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr;
441
18
            if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) goto error;
442
18
        }
443
571
        return 0;
444
586
    }
445
446
33.6k
    if (seqhdr->reduced_still_picture_header) {
447
14.1k
        hdr->frame_type = DAV1D_FRAME_TYPE_KEY;
448
14.1k
        hdr->show_frame = 1;
449
19.5k
    } else {
450
19.5k
        hdr->frame_type = dav1d_get_bits(gb, 2);
451
19.5k
        hdr->show_frame = dav1d_get_bit(gb);
452
19.5k
    }
453
33.6k
    if (hdr->show_frame) {
454
23.8k
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
455
26
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
456
23.8k
        hdr->showable_frame = hdr->frame_type != DAV1D_FRAME_TYPE_KEY;
457
23.8k
    } else
458
9.83k
        hdr->showable_frame = dav1d_get_bit(gb);
459
33.6k
    hdr->error_resilient_mode =
460
33.6k
        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||
461
17.8k
        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||
462
17.6k
        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
33.6k
    hdr->disable_cdf_update = dav1d_get_bit(gb);
468
33.6k
    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?
469
26.2k
                                      dav1d_get_bit(gb) : seqhdr->screen_content_tools;
470
33.6k
    if (hdr->allow_screen_content_tools)
471
20.3k
        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?
472
18.5k
                                dav1d_get_bit(gb) : seqhdr->force_integer_mv;
473
474
33.6k
    if (IS_KEY_OR_INTRA(hdr))
475
22.9k
        hdr->force_integer_mv = 1;
476
477
33.6k
    if (seqhdr->frame_id_numbers_present)
478
575
        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
479
480
33.6k
    if (!seqhdr->reduced_still_picture_header)
481
19.5k
        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
33.6k
    if (seqhdr->order_hint)
487
13.5k
        hdr->frame_offset = dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
488
33.6k
    hdr->primary_ref_frame = !hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) ?
489
23.7k
                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;
490
491
33.6k
    if (seqhdr->decoder_model_info_present) {
492
142
        hdr->buffer_removal_time_present = dav1d_get_bit(gb);
493
142
        if (hdr->buffer_removal_time_present) {
494
237
            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {
495
194
                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];
496
194
                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];
497
194
                if (seqop->decoder_model_param_present) {
498
88
                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;
499
88
                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;
500
88
                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))
501
47
                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);
502
88
                }
503
194
            }
504
43
        }
505
142
    }
506
507
33.6k
    if (IS_KEY_OR_INTRA(hdr)) {
508
22.9k
        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&
509
20.0k
                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);
510
22.9k
        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
511
945
            for (int i = 0; i < 8; i++)
512
840
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
513
22.9k
        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
22.9k
        if (read_frame_size(c, gb, 0) < 0) goto error;
519
22.9k
        if (hdr->allow_screen_content_tools && !hdr->super_res.enabled)
520
10.8k
            hdr->allow_intrabc = dav1d_get_bit(gb);
521
22.9k
    } else {
522
10.7k
        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :
523
10.7k
                                   dav1d_get_bits(gb, 8);
524
10.7k
        if (hdr->error_resilient_mode && seqhdr->order_hint)
525
5.54k
            for (int i = 0; i < 8; i++)
526
4.92k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
527
10.7k
        if (seqhdr->order_hint) {
528
7.56k
            hdr->frame_ref_short_signaling = dav1d_get_bit(gb);
529
7.56k
            if (hdr->frame_ref_short_signaling) {
530
3.96k
                hdr->refidx[0] = dav1d_get_bits(gb, 3);
531
3.96k
                hdr->refidx[1] = hdr->refidx[2] = -1;
532
3.96k
                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.96k
                int frame_offset_mem[8+1];
537
3.96k
                int *const frame_offset = &frame_offset_mem[1];
538
3.96k
                int earliest_ref = -1;
539
35.6k
                for (int i = 0, earliest_offset = INT_MAX; i < 8; i++) {
540
31.6k
                    const Dav1dFrameHeader *const refhdr = c->refs[i].p.p.frame_hdr;
541
31.6k
                    if (!refhdr) goto error;
542
31.6k
                    const int diff = get_poc_diff(seqhdr->order_hint_n_bits,
543
31.6k
                                                  refhdr->frame_offset,
544
31.6k
                                                  hdr->frame_offset);
545
31.6k
                    frame_offset[i] = diff;
546
31.6k
                    if (diff < earliest_offset) {
547
4.85k
                        earliest_offset = diff;
548
4.85k
                        earliest_ref = i;
549
4.85k
                    }
550
31.6k
                }
551
3.95k
                frame_offset[hdr->refidx[0]] = INT_MIN; // = reference frame is used
552
3.95k
                frame_offset[hdr->refidx[3]] = INT_MIN;
553
3.95k
                assert(earliest_ref >= 0);
554
555
3.95k
                int refidx = -1;
556
35.5k
                for (int i = 0, latest_offset = 0; i < 8; i++) {
557
31.6k
                    const int hint = frame_offset[i];
558
31.6k
                    if (hint >= latest_offset) {
559
10.5k
                        latest_offset = hint;
560
10.5k
                        refidx = i;
561
10.5k
                    }
562
31.6k
                }
563
3.95k
                frame_offset[refidx] = INT_MIN;
564
3.95k
                hdr->refidx[6] = refidx;
565
566
11.8k
                for (int i = 4; i < 6; i++) {
567
                    /* Unsigned compares to handle negative values. */
568
7.90k
                    unsigned earliest_offset = UINT8_MAX;
569
7.90k
                    refidx = -1;
570
71.1k
                    for (int j = 0; j < 8; j++) {
571
63.2k
                        const unsigned hint = frame_offset[j];
572
63.2k
                        if (hint < earliest_offset) {
573
4.72k
                            earliest_offset = hint;
574
4.72k
                            refidx = j;
575
4.72k
                        }
576
63.2k
                    }
577
7.90k
                    frame_offset[refidx] = INT_MIN;
578
7.90k
                    hdr->refidx[i] = refidx;
579
7.90k
                }
580
581
27.6k
                for (int i = 1; i < 7; i++) {
582
23.7k
                    refidx = hdr->refidx[i];
583
23.7k
                    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
38.1k
                                latest_offset = hint;
589
38.1k
                                refidx = j;
590
38.1k
                            }
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.7k
                }
596
3.95k
            }
597
7.56k
        }
598
85.5k
        for (int i = 0; i < 7; i++) {
599
74.8k
            if (!hdr->frame_ref_short_signaling)
600
47.1k
                hdr->refidx[i] = dav1d_get_bits(gb, 3);
601
74.8k
            if (seqhdr->frame_id_numbers_present) {
602
48
                const unsigned delta_ref_frame_id = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits) + 1;
603
48
                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
48
                Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr;
605
48
                if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error;
606
48
            }
607
74.8k
        }
608
10.6k
        const int use_ref = !hdr->error_resilient_mode &&
609
9.86k
                            hdr->frame_size_override;
610
10.6k
        if (read_frame_size(c, gb, use_ref) < 0) goto error;
611
10.6k
        if (!hdr->force_integer_mv)
612
7.05k
            hdr->hp = dav1d_get_bit(gb);
613
10.6k
        hdr->subpel_filter_mode = dav1d_get_bit(gb) ? DAV1D_FILTER_SWITCHABLE :
614
10.6k
                                                      dav1d_get_bits(gb, 2);
615
10.6k
        hdr->switchable_motion_mode = dav1d_get_bit(gb);
616
10.6k
        if (!hdr->error_resilient_mode && seqhdr->ref_frame_mvs &&
617
5.81k
            seqhdr->order_hint && IS_INTER_OR_SWITCH(hdr))
618
5.81k
        {
619
5.81k
            hdr->use_ref_frame_mvs = dav1d_get_bit(gb);
620
5.81k
        }
621
10.6k
    }
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
33.5k
    if (!seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update)
628
15.8k
        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
33.5k
    hdr->tiling.uniform = dav1d_get_bit(gb);
636
33.5k
    const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
637
33.5k
    const int sbsz_log2 = 6 + seqhdr->sb128;
638
33.5k
    const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
639
33.5k
    const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
640
33.5k
    const int max_tile_width_sb = 4096 >> sbsz_log2;
641
33.5k
    const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
642
33.5k
    hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
643
33.5k
    hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
644
33.5k
    hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
645
33.5k
    const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
646
33.5k
                              hdr->tiling.min_log2_cols);
647
33.5k
    if (hdr->tiling.uniform) {
648
18.5k
        for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
649
19.1k
             hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bit(gb);
650
18.5k
             hdr->tiling.log2_cols++) ;
651
18.5k
        const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols);
652
18.5k
        hdr->tiling.cols = 0;
653
39.4k
        for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++)
654
20.8k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
655
18.5k
        hdr->tiling.min_log2_rows =
656
18.5k
            imax(min_log2_tiles - hdr->tiling.log2_cols, 0);
657
658
18.5k
        for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows;
659
19.1k
             hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bit(gb);
660
18.5k
             hdr->tiling.log2_rows++) ;
661
18.5k
        const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows);
662
18.5k
        hdr->tiling.rows = 0;
663
39.2k
        for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++)
664
20.6k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
665
18.5k
    } else {
666
15.0k
        hdr->tiling.cols = 0;
667
15.0k
        int widest_tile = 0, max_tile_area_sb = sbw * sbh;
668
34.7k
        for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) {
669
19.7k
            const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb);
670
19.7k
            const int tile_w = (tile_width_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_width_sb) : 1;
671
19.7k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
672
19.7k
            sbx += tile_w;
673
19.7k
            widest_tile = imax(widest_tile, tile_w);
674
19.7k
        }
675
15.0k
        hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
676
15.0k
        if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
677
15.0k
        const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
678
679
15.0k
        hdr->tiling.rows = 0;
680
33.2k
        for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
681
18.2k
            const int tile_height_sb = imin(sbh - sby, max_tile_height_sb);
682
18.2k
            const int tile_h = (tile_height_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_height_sb) : 1;
683
18.2k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
684
18.2k
            sby += tile_h;
685
18.2k
        }
686
15.0k
        hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows);
687
15.0k
    }
688
33.5k
    hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw;
689
33.5k
    hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh;
690
33.5k
    if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) {
691
2.55k
        hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols + hdr->tiling.log2_rows);
692
2.55k
        if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows)
693
21
            goto error;
694
2.52k
        hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1;
695
2.52k
    }
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
33.5k
    hdr->quant.yac = dav1d_get_bits(gb, 8);
703
33.5k
    if (dav1d_get_bit(gb))
704
16.3k
        hdr->quant.ydc_delta = dav1d_get_sbits(gb, 7);
705
33.5k
    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
23.4k
        const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bit(gb) : 0;
710
23.4k
        if (dav1d_get_bit(gb))
711
9.24k
            hdr->quant.udc_delta = dav1d_get_sbits(gb, 7);
712
23.4k
        if (dav1d_get_bit(gb))
713
9.33k
            hdr->quant.uac_delta = dav1d_get_sbits(gb, 7);
714
23.4k
        if (diff_uv_delta) {
715
3.57k
            if (dav1d_get_bit(gb))
716
2.01k
                hdr->quant.vdc_delta = dav1d_get_sbits(gb, 7);
717
3.57k
            if (dav1d_get_bit(gb))
718
1.82k
                hdr->quant.vac_delta = dav1d_get_sbits(gb, 7);
719
19.9k
        } else {
720
19.9k
            hdr->quant.vdc_delta = hdr->quant.udc_delta;
721
19.9k
            hdr->quant.vac_delta = hdr->quant.uac_delta;
722
19.9k
        }
723
23.4k
    }
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
33.5k
    hdr->quant.qm = dav1d_get_bit(gb);
729
33.5k
    if (hdr->quant.qm) {
730
14.7k
        hdr->quant.qm_y = dav1d_get_bits(gb, 4);
731
14.7k
        hdr->quant.qm_u = dav1d_get_bits(gb, 4);
732
14.7k
        hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 4) :
733
14.7k
                                                        hdr->quant.qm_u;
734
14.7k
    }
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
33.5k
    hdr->segmentation.enabled = dav1d_get_bit(gb);
742
33.5k
    if (hdr->segmentation.enabled) {
743
11.6k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
744
7.36k
            hdr->segmentation.update_map = 1;
745
7.36k
            hdr->segmentation.update_data = 1;
746
7.36k
        } else {
747
4.28k
            hdr->segmentation.update_map = dav1d_get_bit(gb);
748
4.28k
            if (hdr->segmentation.update_map)
749
1.36k
                hdr->segmentation.temporal = dav1d_get_bit(gb);
750
4.28k
            hdr->segmentation.update_data = dav1d_get_bit(gb);
751
4.28k
        }
752
753
11.6k
        if (hdr->segmentation.update_data) {
754
7.65k
            hdr->segmentation.seg_data.last_active_segid = -1;
755
68.8k
            for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
756
61.2k
                Dav1dSegmentationData *const seg =
757
61.2k
                    &hdr->segmentation.seg_data.d[i];
758
61.2k
                if (dav1d_get_bit(gb)) {
759
19.8k
                    seg->delta_q = dav1d_get_sbits(gb, 9);
760
19.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
761
19.8k
                }
762
61.2k
                if (dav1d_get_bit(gb)) {
763
17.6k
                    seg->delta_lf_y_v = dav1d_get_sbits(gb, 7);
764
17.6k
                    hdr->segmentation.seg_data.last_active_segid = i;
765
17.6k
                }
766
61.2k
                if (dav1d_get_bit(gb)) {
767
18.5k
                    seg->delta_lf_y_h = dav1d_get_sbits(gb, 7);
768
18.5k
                    hdr->segmentation.seg_data.last_active_segid = i;
769
18.5k
                }
770
61.2k
                if (dav1d_get_bit(gb)) {
771
18.7k
                    seg->delta_lf_u = dav1d_get_sbits(gb, 7);
772
18.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
773
18.7k
                }
774
61.2k
                if (dav1d_get_bit(gb)) {
775
18.8k
                    seg->delta_lf_v = dav1d_get_sbits(gb, 7);
776
18.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
777
18.8k
                }
778
61.2k
                if (dav1d_get_bit(gb)) {
779
17.8k
                    seg->ref = dav1d_get_bits(gb, 3);
780
17.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
781
17.8k
                    hdr->segmentation.seg_data.preskip = 1;
782
43.3k
                } else {
783
43.3k
                    seg->ref = -1;
784
43.3k
                }
785
61.2k
                if ((seg->skip = dav1d_get_bit(gb))) {
786
18.1k
                    hdr->segmentation.seg_data.last_active_segid = i;
787
18.1k
                    hdr->segmentation.seg_data.preskip = 1;
788
18.1k
                }
789
61.2k
                if ((seg->globalmv = dav1d_get_bit(gb))) {
790
18.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
791
18.7k
                    hdr->segmentation.seg_data.preskip = 1;
792
18.7k
                }
793
61.2k
            }
794
7.65k
        } else {
795
            // segmentation.update_data was false so we should copy
796
            // segmentation data from the reference frame.
797
4.00k
            assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
798
4.00k
            const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
799
4.00k
            if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
800
3.99k
            hdr->segmentation.seg_data =
801
3.99k
                c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data;
802
3.99k
        }
803
21.8k
    } else {
804
197k
        for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++)
805
175k
            hdr->segmentation.seg_data.d[i].ref = -1;
806
21.8k
    }
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
33.5k
    if (hdr->quant.yac) {
814
31.0k
        hdr->delta.q.present = dav1d_get_bit(gb);
815
31.0k
        if (hdr->delta.q.present) {
816
10.5k
            hdr->delta.q.res_log2 = dav1d_get_bits(gb, 2);
817
10.5k
            if (!hdr->allow_intrabc) {
818
9.39k
                hdr->delta.lf.present = dav1d_get_bit(gb);
819
9.39k
                if (hdr->delta.lf.present) {
820
3.78k
                    hdr->delta.lf.res_log2 = dav1d_get_bits(gb, 2);
821
3.78k
                    hdr->delta.lf.multi = dav1d_get_bit(gb);
822
3.78k
                }
823
9.39k
            }
824
10.5k
        }
825
31.0k
    }
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
33.5k
    const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta &&
833
14.3k
        !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta;
834
33.5k
    hdr->all_lossless = 1;
835
301k
    for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
836
268k
        hdr->segmentation.qidx[i] = hdr->segmentation.enabled ?
837
93.1k
            iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) :
838
268k
            hdr->quant.yac;
839
268k
        hdr->segmentation.lossless[i] =
840
268k
            !hdr->segmentation.qidx[i] && delta_lossless;
841
268k
        hdr->all_lossless &= hdr->segmentation.lossless[i];
842
268k
    }
843
844
    // loopfilter
845
33.5k
    if (hdr->all_lossless || hdr->allow_intrabc) {
846
7.28k
        hdr->loopfilter.mode_ref_delta_enabled = 1;
847
7.28k
        hdr->loopfilter.mode_ref_delta_update = 1;
848
7.28k
        hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
849
26.2k
    } else {
850
26.2k
        hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6);
851
26.2k
        hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6);
852
26.2k
        if (!seqhdr->monochrome &&
853
18.3k
            (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1]))
854
14.5k
        {
855
14.5k
            hdr->loopfilter.level_u = dav1d_get_bits(gb, 6);
856
14.5k
            hdr->loopfilter.level_v = dav1d_get_bits(gb, 6);
857
14.5k
        }
858
26.2k
        hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3);
859
860
26.2k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
861
17.4k
            hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
862
17.4k
        } else {
863
8.79k
            const int ref = hdr->refidx[hdr->primary_ref_frame];
864
8.79k
            if (!c->refs[ref].p.p.frame_hdr) goto error;
865
8.78k
            hdr->loopfilter.mode_ref_deltas =
866
8.78k
                c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas;
867
8.78k
        }
868
26.2k
        hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bit(gb);
869
26.2k
        if (hdr->loopfilter.mode_ref_delta_enabled) {
870
8.03k
            hdr->loopfilter.mode_ref_delta_update = dav1d_get_bit(gb);
871
8.03k
            if (hdr->loopfilter.mode_ref_delta_update) {
872
32.9k
                for (int i = 0; i < 8; i++)
873
29.2k
                    if (dav1d_get_bit(gb))
874
15.4k
                        hdr->loopfilter.mode_ref_deltas.ref_delta[i] =
875
15.4k
                            dav1d_get_sbits(gb, 7);
876
10.9k
                for (int i = 0; i < 2; i++)
877
7.31k
                    if (dav1d_get_bit(gb))
878
3.05k
                        hdr->loopfilter.mode_ref_deltas.mode_delta[i] =
879
3.05k
                            dav1d_get_sbits(gb, 7);
880
3.65k
            }
881
8.03k
        }
882
26.2k
    }
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
33.5k
    if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) {
890
13.3k
        hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3;
891
13.3k
        hdr->cdef.n_bits = dav1d_get_bits(gb, 2);
892
44.1k
        for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) {
893
30.7k
            hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6);
894
30.7k
            if (!seqhdr->monochrome)
895
18.2k
                hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6);
896
30.7k
        }
897
13.3k
    }
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
33.5k
    if ((!hdr->all_lossless || hdr->super_res.enabled) &&
905
32.0k
        seqhdr->restoration && !hdr->allow_intrabc)
906
15.6k
    {
907
15.6k
        hdr->restoration.type[0] = dav1d_get_bits(gb, 2);
908
15.6k
        if (!seqhdr->monochrome) {
909
10.6k
            hdr->restoration.type[1] = dav1d_get_bits(gb, 2);
910
10.6k
            hdr->restoration.type[2] = dav1d_get_bits(gb, 2);
911
10.6k
        }
912
913
15.6k
        if (hdr->restoration.type[0] || hdr->restoration.type[1] ||
914
5.31k
            hdr->restoration.type[2])
915
10.9k
        {
916
            // Log2 of the restoration unit size.
917
10.9k
            hdr->restoration.unit_size[0] = 6 + seqhdr->sb128;
918
10.9k
            if (dav1d_get_bit(gb)) {
919
4.83k
                hdr->restoration.unit_size[0]++;
920
4.83k
                if (!seqhdr->sb128)
921
2.74k
                    hdr->restoration.unit_size[0] += dav1d_get_bit(gb);
922
4.83k
            }
923
10.9k
            hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0];
924
10.9k
            if ((hdr->restoration.type[1] || hdr->restoration.type[2]) &&
925
7.74k
                seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1)
926
4.10k
            {
927
4.10k
                hdr->restoration.unit_size[1] -= dav1d_get_bit(gb);
928
4.10k
            }
929
10.9k
        } else {
930
4.62k
            hdr->restoration.unit_size[0] = 8;
931
4.62k
        }
932
15.6k
    }
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
33.5k
    if (!hdr->all_lossless)
939
31.7k
        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
33.5k
    if (IS_INTER_OR_SWITCH(hdr))
945
10.6k
        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
33.5k
    if (hdr->switchable_comp_refs && IS_INTER_OR_SWITCH(hdr) && seqhdr->order_hint) {
951
4.24k
        const int poc = hdr->frame_offset;
952
4.24k
        int off_before = -1, off_after = -1;
953
4.24k
        int off_before_idx, off_after_idx;
954
33.9k
        for (int i = 0; i < 7; i++) {
955
29.6k
            if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
956
29.6k
            const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
957
958
29.6k
            const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc);
959
29.6k
            if (diff > 0) {
960
8.61k
                if (off_after < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
961
6.86k
                                                  off_after, refpoc) > 0)
962
1.88k
                {
963
1.88k
                    off_after = refpoc;
964
1.88k
                    off_after_idx = i;
965
1.88k
                }
966
21.0k
            } else if (diff < 0 && (off_before < 0 ||
967
13.3k
                                    get_poc_diff(seqhdr->order_hint_n_bits,
968
13.3k
                                                 refpoc, off_before) > 0))
969
2.99k
            {
970
2.99k
                off_before = refpoc;
971
2.99k
                off_before_idx = i;
972
2.99k
            }
973
29.6k
        }
974
975
4.23k
        if ((off_before | off_after) >= 0) {
976
664
            hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx);
977
664
            hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx);
978
664
            hdr->skip_mode_allowed = 1;
979
3.57k
        } else if (off_before >= 0) {
980
2.12k
            int off_before2 = -1;
981
2.12k
            int off_before2_idx;
982
16.9k
            for (int i = 0; i < 7; i++) {
983
14.8k
                if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
984
14.8k
                const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
985
14.8k
                if (get_poc_diff(seqhdr->order_hint_n_bits,
986
14.8k
                                 refpoc, off_before) < 0) {
987
1.29k
                    if (off_before2 < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
988
861
                                                        refpoc, off_before2) > 0)
989
461
                    {
990
461
                        off_before2 = refpoc;
991
461
                        off_before2_idx = i;
992
461
                    }
993
1.29k
                }
994
14.8k
            }
995
996
2.12k
            if (off_before2 >= 0) {
997
432
                hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx);
998
432
                hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx);
999
432
                hdr->skip_mode_allowed = 1;
1000
432
            }
1001
2.12k
        }
1002
4.23k
    }
1003
33.5k
    if (hdr->skip_mode_allowed)
1004
1.09k
        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
33.5k
    if (!hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) && seqhdr->warped_motion)
1010
5.47k
        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
33.5k
    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
268k
    for (int i = 0; i < 7; i++)
1022
234k
        hdr->gmv[i] = dav1d_default_wm_params;
1023
1024
33.5k
    if (IS_INTER_OR_SWITCH(hdr)) {
1025
85.0k
        for (int i = 0; i < 7; i++) {
1026
74.4k
            hdr->gmv[i].type = !dav1d_get_bit(gb) ? DAV1D_WM_TYPE_IDENTITY :
1027
74.4k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_ROT_ZOOM :
1028
19.5k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_TRANSLATION :
1029
6.45k
                                                    DAV1D_WM_TYPE_AFFINE;
1030
1031
74.4k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue;
1032
1033
19.5k
            const Dav1dWarpedMotionParams *ref_gmv;
1034
19.5k
            if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
1035
2.17k
                ref_gmv = &dav1d_default_wm_params;
1036
17.3k
            } else {
1037
17.3k
                const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
1038
17.3k
                if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
1039
17.3k
                ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i];
1040
17.3k
            }
1041
19.5k
            int32_t *const mat = hdr->gmv[i].matrix;
1042
19.5k
            const int32_t *const ref_mat = ref_gmv->matrix;
1043
19.5k
            int bits, shift;
1044
1045
19.5k
            if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) {
1046
16.6k
                mat[2] = (1 << 16) + 2 *
1047
16.6k
                    dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12);
1048
16.6k
                mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12);
1049
1050
16.6k
                bits = 12;
1051
16.6k
                shift = 10;
1052
16.6k
            } else {
1053
2.93k
                bits = 9 - !hdr->hp;
1054
2.93k
                shift = 13 + !hdr->hp;
1055
2.93k
            }
1056
1057
19.5k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) {
1058
3.51k
                mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12);
1059
3.51k
                mat[5] = (1 << 16) + 2 *
1060
3.51k
                    dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12);
1061
16.0k
            } else {
1062
16.0k
                mat[4] = -mat[3];
1063
16.0k
                mat[5] = mat[2];
1064
16.0k
            }
1065
1066
19.5k
            mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift);
1067
19.5k
            mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift);
1068
19.5k
        }
1069
10.6k
    }
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
33.5k
    if (seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame)) {
1076
5.75k
        hdr->film_grain.present = dav1d_get_bit(gb);
1077
5.75k
        if (hdr->film_grain.present) {
1078
1.44k
            const unsigned seed = dav1d_get_bits(gb, 16);
1079
1.44k
            hdr->film_grain.update = hdr->frame_type != DAV1D_FRAME_TYPE_INTER || dav1d_get_bit(gb);
1080
1.44k
            if (!hdr->film_grain.update) {
1081
146
                const int refidx = dav1d_get_bits(gb, 3);
1082
146
                int i;
1083
592
                for (i = 0; i < 7; i++)
1084
584
                    if (hdr->refidx[i] == refidx)
1085
138
                        break;
1086
146
                if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error;
1087
132
                hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data;
1088
132
                hdr->film_grain.data.seed = seed;
1089
1.30k
            } else {
1090
1.30k
                Dav1dFilmGrainData *const fgd = &hdr->film_grain.data;
1091
1.30k
                fgd->seed = seed;
1092
1093
1.30k
                fgd->num_y_points = dav1d_get_bits(gb, 4);
1094
1.30k
                if (fgd->num_y_points > 14) goto error;
1095
2.41k
                for (int i = 0; i < fgd->num_y_points; i++) {
1096
1.14k
                    fgd->y_points[i][0] = dav1d_get_bits(gb, 8);
1097
1.14k
                    if (i && fgd->y_points[i - 1][0] >= fgd->y_points[i][0])
1098
22
                        goto error;
1099
1.12k
                    fgd->y_points[i][1] = dav1d_get_bits(gb, 8);
1100
1.12k
                }
1101
1102
1.27k
                if (!seqhdr->monochrome)
1103
952
                    fgd->chroma_scaling_from_luma = dav1d_get_bit(gb);
1104
1.27k
                if (seqhdr->monochrome || fgd->chroma_scaling_from_luma ||
1105
574
                    (seqhdr->ss_ver == 1 && seqhdr->ss_hor == 1 && !fgd->num_y_points))
1106
772
                {
1107
772
                    fgd->num_uv_points[0] = fgd->num_uv_points[1] = 0;
1108
1.47k
                } else for (int pl = 0; pl < 2; pl++) {
1109
991
                    fgd->num_uv_points[pl] = dav1d_get_bits(gb, 4);
1110
991
                    if (fgd->num_uv_points[pl] > 10) goto error;
1111
1.75k
                    for (int i = 0; i < fgd->num_uv_points[pl]; i++) {
1112
783
                        fgd->uv_points[pl][i][0] = dav1d_get_bits(gb, 8);
1113
783
                        if (i && fgd->uv_points[pl][i - 1][0] >= fgd->uv_points[pl][i][0])
1114
14
                            goto error;
1115
769
                        fgd->uv_points[pl][i][1] = dav1d_get_bits(gb, 8);
1116
769
                    }
1117
988
                }
1118
1119
1.25k
                if (seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1 &&
1120
668
                    !!fgd->num_uv_points[0] != !!fgd->num_uv_points[1])
1121
3
                {
1122
3
                    goto error;
1123
3
                }
1124
1125
1.25k
                fgd->scaling_shift = dav1d_get_bits(gb, 2) + 8;
1126
1.25k
                fgd->ar_coeff_lag = dav1d_get_bits(gb, 2);
1127
1.25k
                const int num_y_pos = 2 * fgd->ar_coeff_lag * (fgd->ar_coeff_lag + 1);
1128
1.25k
                if (fgd->num_y_points)
1129
4.39k
                    for (int i = 0; i < num_y_pos; i++)
1130
3.68k
                        fgd->ar_coeffs_y[i] = dav1d_get_bits(gb, 8) - 128;
1131
3.75k
                for (int pl = 0; pl < 2; pl++)
1132
2.50k
                    if (fgd->num_uv_points[pl] || fgd->chroma_scaling_from_luma) {
1133
1.20k
                        const int num_uv_pos = num_y_pos + !!fgd->num_y_points;
1134
6.88k
                        for (int i = 0; i < num_uv_pos; i++)
1135
5.68k
                            fgd->ar_coeffs_uv[pl][i] = dav1d_get_bits(gb, 8) - 128;
1136
1.20k
                        if (!fgd->num_y_points)
1137
454
                            fgd->ar_coeffs_uv[pl][num_uv_pos] = 0;
1138
1.20k
                    }
1139
1.25k
                fgd->ar_coeff_shift = dav1d_get_bits(gb, 2) + 6;
1140
1.25k
                fgd->grain_scale_shift = dav1d_get_bits(gb, 2);
1141
3.75k
                for (int pl = 0; pl < 2; pl++)
1142
2.50k
                    if (fgd->num_uv_points[pl]) {
1143
450
                        fgd->uv_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1144
450
                        fgd->uv_luma_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1145
450
                        fgd->uv_offset[pl] = dav1d_get_bits(gb, 9) - 256;
1146
450
                    }
1147
1.25k
                fgd->overlap_flag = dav1d_get_bit(gb);
1148
1.25k
                fgd->clip_to_restricted_range = dav1d_get_bit(gb);
1149
1.25k
            }
1150
1.44k
        }
1151
5.75k
    }
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
33.4k
    return 0;
1158
1159
217
error:
1160
217
    dav1d_log(c, "Error parsing frame header\n");
1161
217
    return DAV1D_ERR(EINVAL);
1162
33.5k
}
1163
1164
32.3k
static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) {
1165
32.3k
    const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows;
1166
32.3k
    const int have_tile_pos = n_tiles > 1 ? dav1d_get_bit(gb) : 0;
1167
1168
32.3k
    if (have_tile_pos) {
1169
140
        const int n_bits = c->frame_hdr->tiling.log2_cols +
1170
140
                           c->frame_hdr->tiling.log2_rows;
1171
140
        c->tile[c->n_tile_data].start = dav1d_get_bits(gb, n_bits);
1172
140
        c->tile[c->n_tile_data].end = dav1d_get_bits(gb, n_bits);
1173
32.2k
    } else {
1174
32.2k
        c->tile[c->n_tile_data].start = 0;
1175
32.2k
        c->tile[c->n_tile_data].end = n_tiles - 1;
1176
32.2k
    }
1177
32.3k
}
1178
1179
70.2k
ptrdiff_t dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in) {
1180
70.2k
    GetBits gb;
1181
70.2k
    int res;
1182
1183
70.2k
    dav1d_init_get_bits(&gb, in->data, in->sz);
1184
1185
    // obu header
1186
70.2k
    const int obu_forbidden_bit = dav1d_get_bit(&gb);
1187
70.2k
    if (c->strict_std_compliance && obu_forbidden_bit) goto error;
1188
70.2k
    const enum Dav1dObuType type = dav1d_get_bits(&gb, 4);
1189
70.2k
    const int has_extension = dav1d_get_bit(&gb);
1190
70.2k
    const int has_length_field = dav1d_get_bit(&gb);
1191
70.2k
    dav1d_get_bit(&gb); // reserved
1192
1193
70.2k
    int temporal_id = 0, spatial_id = 0;
1194
70.2k
    if (has_extension) {
1195
36.3k
        temporal_id = dav1d_get_bits(&gb, 3);
1196
36.3k
        spatial_id = dav1d_get_bits(&gb, 2);
1197
36.3k
        dav1d_get_bits(&gb, 3); // reserved
1198
36.3k
    }
1199
1200
70.2k
    if (has_length_field) {
1201
70.2k
        const size_t len = dav1d_get_uleb128(&gb);
1202
70.2k
        if (len > (size_t)(gb.ptr_end - gb.ptr)) goto error;
1203
70.2k
        gb.ptr_end = gb.ptr + len;
1204
70.2k
    }
1205
70.2k
    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
70.2k
    assert(gb.bits_left == 0);
1211
1212
    // skip obu not belonging to the selected temporal/spatial layer
1213
70.2k
    if (type != DAV1D_OBU_SEQ_HDR && type != DAV1D_OBU_TD &&
1214
46.6k
        has_extension && c->operating_point_idc != 0)
1215
1.01k
    {
1216
1.01k
        const int in_temporal_layer = (c->operating_point_idc >> temporal_id) & 1;
1217
1.01k
        const int in_spatial_layer = (c->operating_point_idc >> (spatial_id + 8)) & 1;
1218
1.01k
        if (!in_temporal_layer || !in_spatial_layer)
1219
612
            return gb.ptr_end - gb.ptr_start;
1220
1.01k
    }
1221
1222
69.6k
    switch (type) {
1223
22.5k
    case DAV1D_OBU_SEQ_HDR: {
1224
22.5k
        Dav1dRef *ref = dav1d_ref_create_using_pool(c->seq_hdr_pool,
1225
22.5k
                                                    sizeof(Dav1dSequenceHeader));
1226
22.5k
        if (!ref) return DAV1D_ERR(ENOMEM);
1227
22.5k
        Dav1dSequenceHeader *seq_hdr = ref->data;
1228
22.5k
        if ((res = parse_seq_hdr(seq_hdr, &gb, c->strict_std_compliance)) < 0) {
1229
125
            dav1d_log(c, "Error parsing sequence header\n");
1230
125
            dav1d_ref_dec(&ref);
1231
125
            goto error;
1232
125
        }
1233
1234
22.4k
        const int op_idx =
1235
22.4k
            c->operating_point < seq_hdr->num_operating_points ? c->operating_point : 0;
1236
22.4k
        c->operating_point_idc = seq_hdr->operating_points[op_idx].idc;
1237
22.4k
        const unsigned spatial_mask = c->operating_point_idc >> 8;
1238
22.4k
        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
22.4k
        if (!c->seq_hdr) {
1245
21.0k
            c->frame_hdr = NULL;
1246
21.0k
            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
21.0k
        } else if (memcmp(seq_hdr, c->seq_hdr, offsetof(Dav1dSequenceHeader, operating_parameter_info))) {
1250
1.33k
            c->frame_hdr = NULL;
1251
1.33k
            c->mastering_display = NULL;
1252
1.33k
            c->content_light = NULL;
1253
1.33k
            dav1d_ref_dec(&c->mastering_display_ref);
1254
1.33k
            dav1d_ref_dec(&c->content_light_ref);
1255
12.0k
            for (int i = 0; i < 8; i++) {
1256
10.6k
                if (c->refs[i].p.p.frame_hdr)
1257
577
                    dav1d_thread_picture_unref(&c->refs[i].p);
1258
10.6k
                dav1d_ref_dec(&c->refs[i].segmap);
1259
10.6k
                dav1d_ref_dec(&c->refs[i].refmvs);
1260
10.6k
                dav1d_cdf_thread_unref(&c->cdf[i]);
1261
10.6k
            }
1262
1.33k
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1263
        // If operating_parameter_info changed, signal it
1264
1.33k
        } else if (memcmp(seq_hdr->operating_parameter_info, c->seq_hdr->operating_parameter_info,
1265
65
                          sizeof(seq_hdr->operating_parameter_info)))
1266
2
        {
1267
2
            c->frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO;
1268
2
        }
1269
22.4k
        dav1d_ref_dec(&c->seq_hdr_ref);
1270
22.4k
        c->seq_hdr_ref = ref;
1271
22.4k
        c->seq_hdr = seq_hdr;
1272
22.4k
        break;
1273
22.5k
    }
1274
425
    case DAV1D_OBU_REDUNDANT_FRAME_HDR:
1275
425
        if (c->frame_hdr) break;
1276
        // fall-through
1277
33.0k
    case DAV1D_OBU_FRAME:
1278
34.2k
    case DAV1D_OBU_FRAME_HDR:
1279
34.2k
        if (!c->seq_hdr) goto error;
1280
34.2k
        if (!c->frame_hdr_ref) {
1281
33.2k
            c->frame_hdr_ref = dav1d_ref_create_using_pool(c->frame_hdr_pool,
1282
33.2k
                                                           sizeof(Dav1dFrameHeader));
1283
33.2k
            if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM);
1284
33.2k
        }
1285
#ifndef NDEBUG
1286
        // ensure that the reference is writable
1287
        assert(dav1d_ref_is_writable(c->frame_hdr_ref));
1288
#endif
1289
34.2k
        c->frame_hdr = c->frame_hdr_ref->data;
1290
34.2k
        memset(c->frame_hdr, 0, sizeof(*c->frame_hdr));
1291
34.2k
        c->frame_hdr->temporal_id = temporal_id;
1292
34.2k
        c->frame_hdr->spatial_id = spatial_id;
1293
34.2k
        if ((res = parse_frame_hdr(c, &gb)) < 0) {
1294
217
            c->frame_hdr = NULL;
1295
217
            goto error;
1296
217
        }
1297
34.0k
        for (int n = 0; n < c->n_tile_data; n++)
1298
12
            dav1d_data_unref_internal(&c->tile[n].data);
1299
34.0k
        c->n_tile_data = 0;
1300
34.0k
        c->n_tiles = 0;
1301
34.0k
        if (type != DAV1D_OBU_FRAME) {
1302
            // This is actually a frame header OBU so read the
1303
            // trailing bit and check for overrun.
1304
1.60k
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1305
154
                c->frame_hdr = NULL;
1306
154
                goto error;
1307
154
            }
1308
1.60k
        }
1309
1310
33.8k
        if (c->frame_size_limit && (int64_t)c->frame_hdr->width[1] *
1311
33.8k
            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
33.8k
        if (type != DAV1D_OBU_FRAME)
1320
1.45k
            break;
1321
        // OBU_FRAMEs shouldn't be signaled with show_existing_frame
1322
32.4k
        if (c->frame_hdr->show_existing_frame) {
1323
69
            c->frame_hdr = NULL;
1324
69
            goto error;
1325
69
        }
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
32.3k
        dav1d_bytealign_get_bits(&gb);
1331
        // fall-through
1332
32.4k
    case DAV1D_OBU_TILE_GRP: {
1333
32.4k
        if (!c->frame_hdr) goto error;
1334
32.3k
        if (c->n_tile_data_alloc < c->n_tile_data + 1) {
1335
20.6k
            if ((c->n_tile_data + 1) > INT_MAX / (int)sizeof(*c->tile)) goto error;
1336
20.6k
            struct Dav1dTileGroup *tile = dav1d_realloc(ALLOC_TILE, c->tile,
1337
20.6k
                                                        (c->n_tile_data + 1) * sizeof(*c->tile));
1338
20.6k
            if (!tile) goto error;
1339
20.6k
            c->tile = tile;
1340
20.6k
            memset(c->tile + c->n_tile_data, 0, sizeof(*c->tile));
1341
20.6k
            c->n_tile_data_alloc = c->n_tile_data + 1;
1342
20.6k
        }
1343
32.3k
        parse_tile_hdr(c, &gb);
1344
        // Align to the next byte boundary and check for overrun.
1345
32.3k
        dav1d_bytealign_get_bits(&gb);
1346
32.3k
        if (gb.error) goto error;
1347
1348
31.9k
        dav1d_data_ref(&c->tile[c->n_tile_data].data, in);
1349
31.9k
        c->tile[c->n_tile_data].data.data = gb.ptr;
1350
31.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
31.9k
        if (c->tile[c->n_tile_data].start > c->tile[c->n_tile_data].end ||
1353
31.8k
            c->tile[c->n_tile_data].start != c->n_tiles)
1354
17
        {
1355
38
            for (int i = 0; i <= c->n_tile_data; i++)
1356
21
                dav1d_data_unref_internal(&c->tile[i].data);
1357
17
            c->n_tile_data = 0;
1358
17
            c->n_tiles = 0;
1359
17
            goto error;
1360
17
        }
1361
31.8k
        c->n_tiles += 1 + c->tile[c->n_tile_data].end -
1362
31.8k
                          c->tile[c->n_tile_data].start;
1363
31.8k
        c->n_tile_data++;
1364
31.8k
        break;
1365
31.9k
    }
1366
664
    case DAV1D_OBU_METADATA: {
1367
664
#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
664
        const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb);
1373
664
        if (gb.error) goto error;
1374
1375
570
        switch (meta_type) {
1376
82
        case OBU_META_HDR_CLL: {
1377
82
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1378
82
                                             sizeof(Dav1dContentLightLevel));
1379
82
            if (!ref) return DAV1D_ERR(ENOMEM);
1380
82
            Dav1dContentLightLevel *const content_light = ref->data;
1381
1382
82
            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
82
            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
82
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1396
4
                dav1d_ref_dec(&ref);
1397
4
                goto error;
1398
4
            }
1399
1400
78
            dav1d_ref_dec(&c->content_light_ref);
1401
78
            c->content_light = content_light;
1402
78
            c->content_light_ref = ref;
1403
78
            break;
1404
82
        }
1405
69
        case OBU_META_HDR_MDCV: {
1406
69
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1407
69
                                             sizeof(Dav1dMasteringDisplay));
1408
69
            if (!ref) return DAV1D_ERR(ENOMEM);
1409
69
            Dav1dMasteringDisplay *const mastering_display = ref->data;
1410
1411
276
            for (int i = 0; i < 3; i++) {
1412
207
                mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16);
1413
207
                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
207
            }
1421
69
            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
69
            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
69
            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
69
            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
69
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1446
7
                dav1d_ref_dec(&ref);
1447
7
                goto error;
1448
7
            }
1449
1450
62
            dav1d_ref_dec(&c->mastering_display_ref);
1451
62
            c->mastering_display = mastering_display;
1452
62
            c->mastering_display_ref = ref;
1453
62
            break;
1454
69
        }
1455
71
        case OBU_META_ITUT_T35: {
1456
71
            ptrdiff_t payload_size = gb.ptr_end - gb.ptr;
1457
            // Don't take into account all the trailing bits for payload_size
1458
490
            while (payload_size > 0 && !gb.ptr[payload_size - 1])
1459
419
                payload_size--; // trailing_zero_bit x 8
1460
71
            payload_size--; // trailing_one_bit + trailing_zero_bit x 7
1461
1462
71
            int country_code_extension_byte = 0;
1463
71
            const int country_code = dav1d_get_bits(&gb, 8);
1464
71
            payload_size--;
1465
71
            if (country_code == 0xFF) {
1466
10
                country_code_extension_byte = dav1d_get_bits(&gb, 8);
1467
10
                payload_size--;
1468
10
            }
1469
1470
71
            if (payload_size <= 0 || gb.ptr[payload_size] != 0x80) {
1471
39
                dav1d_log(c, "Malformed ITU-T T.35 metadata message format\n");
1472
39
                break;
1473
39
            }
1474
1475
32
            if ((c->n_itut_t35 + 1) > INT_MAX / (int)sizeof(*c->itut_t35)) goto error;
1476
32
            uint8_t *const payload = dav1d_malloc(ALLOC_OBU_META, payload_size);
1477
32
            if (!payload) goto error;
1478
1479
32
            struct itut_t35_ctx_context *itut_t35_ctx = NULL;
1480
32
            if (!c->n_itut_t35) {
1481
26
                assert(!c->itut_t35_ref);
1482
26
                itut_t35_ctx = dav1d_malloc(ALLOC_OBU_META, sizeof(struct itut_t35_ctx_context));
1483
26
                if (!itut_t35_ctx) {
1484
0
                    dav1d_free(payload);
1485
0
                    goto error;
1486
0
                }
1487
26
            }
1488
1489
32
            struct Dav1dITUTT35 *itut_t35 = dav1d_realloc(ALLOC_OBU_META, c->itut_t35,
1490
32
                                                          (c->n_itut_t35 + 1) * sizeof(*c->itut_t35));
1491
32
            if (!itut_t35) {
1492
0
                dav1d_free(itut_t35_ctx);
1493
0
                dav1d_free(payload);
1494
0
                goto error;
1495
0
            }
1496
32
            c->itut_t35 = itut_t35;
1497
32
            memset(c->itut_t35 + c->n_itut_t35, 0, sizeof(*c->itut_t35));
1498
1499
32
            if (!c->n_itut_t35) {
1500
26
                c->itut_t35_ref = dav1d_ref_init(&itut_t35_ctx->ref, c->itut_t35,
1501
26
                                                 dav1d_picture_free_itut_t35, itut_t35_ctx, 0);
1502
26
            } else {
1503
6
                assert(c->itut_t35_ref &&
1504
6
                       atomic_load_explicit(&c->itut_t35_ref->ref_cnt, memory_order_relaxed) == 1);
1505
6
                itut_t35_ctx = c->itut_t35_ref->user_data;
1506
6
                c->itut_t35_ref->const_data = (uint8_t *)c->itut_t35;
1507
6
            }
1508
32
            itut_t35_ctx->itut_t35 = c->itut_t35;
1509
32
            itut_t35_ctx->n_itut_t35 = c->n_itut_t35 + 1;
1510
1511
32
            Dav1dITUTT35 *const itut_t35_metadata = &c->itut_t35[c->n_itut_t35];
1512
32
            itut_t35_metadata->payload = payload;
1513
1514
32
            itut_t35_metadata->country_code = country_code;
1515
32
            itut_t35_metadata->country_code_extension_byte = country_code_extension_byte;
1516
32
            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
32
            assert(gb.bits_left == 0);
1521
32
            memcpy(itut_t35_metadata->payload, gb.ptr, payload_size);
1522
1523
32
            c->n_itut_t35++;
1524
32
            break;
1525
32
        }
1526
16
        case OBU_META_SCALABILITY:
1527
27
        case OBU_META_TIMECODE:
1528
            // ignore metadata OBUs we don't care about
1529
27
            break;
1530
321
        default:
1531
            // print a warning but don't fail for unknown types
1532
321
            if (meta_type > 31) // Types 6 to 31 are "Unregistered user private", so ignore them.
1533
287
                dav1d_log(c, "Unknown Metadata OBU type %d\n", meta_type);
1534
321
            break;
1535
570
        }
1536
1537
559
        break;
1538
570
    }
1539
989
    case DAV1D_OBU_TD:
1540
989
        c->frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT;
1541
989
        break;
1542
3.38k
    case DAV1D_OBU_PADDING:
1543
        // ignore OBUs we don't care about
1544
3.38k
        break;
1545
7.60k
    default:
1546
        // print a warning but don't fail for unknown types
1547
7.60k
        dav1d_log(c, "Unknown OBU type %d of size %td\n", type, gb.ptr_end - gb.ptr);
1548
7.60k
        break;
1549
69.6k
    }
1550
1551
68.3k
    if (c->seq_hdr && c->frame_hdr) {
1552
33.8k
        if (c->frame_hdr->show_existing_frame) {
1553
502
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr) goto error;
1554
497
            switch (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type) {
1555
239
            case DAV1D_FRAME_TYPE_INTER:
1556
246
            case DAV1D_FRAME_TYPE_SWITCH:
1557
246
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE)
1558
0
                    goto skip;
1559
246
                break;
1560
246
            case DAV1D_FRAME_TYPE_INTRA:
1561
75
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA)
1562
0
                    goto skip;
1563
                // fall-through
1564
251
            default:
1565
251
                break;
1566
497
            }
1567
497
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.data[0]) goto error;
1568
497
            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
497
            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
497
            } else {
1587
497
                pthread_mutex_lock(&c->task_thread.lock);
1588
                // need to append this to the frame output queue
1589
497
                const unsigned next = c->frame_thread.next++;
1590
497
                if (c->frame_thread.next == c->n_fc)
1591
26
                    c->frame_thread.next = 0;
1592
1593
497
                Dav1dFrameContext *const f = &c->fc[next];
1594
506
                while (f->n_tile_data > 0)
1595
9
                    pthread_cond_wait(&f->task_thread.cond,
1596
9
                                      &f->task_thread.ttd->lock);
1597
497
                Dav1dThreadPicture *const out_delayed =
1598
497
                    &c->frame_thread.out_delayed[next];
1599
497
                if (out_delayed->p.data[0] ||
1600
497
                    atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
1601
12
                {
1602
12
                    unsigned first = atomic_load_explicit(&c->task_thread.first,
1603
12
                                                          memory_order_relaxed);
1604
12
                    if (first + 1U < c->n_fc)
1605
12
                        atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed);
1606
0
                    else
1607
12
                        atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed);
1608
12
                    atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur,
1609
12
                                                            &first, UINT_MAX,
1610
12
                                                            memory_order_relaxed,
1611
12
                                                            memory_order_relaxed);
1612
12
                    if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
1613
5
                        c->task_thread.cur--;
1614
12
                }
1615
497
                const int error = f->task_thread.retval;
1616
497
                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
497
                } else if (out_delayed->p.data[0]) {
1622
12
                    const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
1623
12
                                                                   memory_order_relaxed);
1624
12
                    if ((out_delayed->visible || c->output_invisible_frames) &&
1625
12
                        progress != FRAME_ERROR)
1626
12
                    {
1627
12
                        dav1d_thread_picture_ref(&c->out, out_delayed);
1628
12
                        c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
1629
12
                    }
1630
12
                    dav1d_thread_picture_unref(out_delayed);
1631
12
                }
1632
497
                dav1d_thread_picture_ref(out_delayed,
1633
497
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1634
497
                out_delayed->visible = 1;
1635
497
                dav1d_picture_copy_props(&out_delayed->p,
1636
497
                                         c->content_light, c->content_light_ref,
1637
497
                                         c->mastering_display, c->mastering_display_ref,
1638
497
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1639
497
                                         &in->m);
1640
                // Must be removed from the context after being attached to the frame
1641
497
                dav1d_ref_dec(&c->itut_t35_ref);
1642
497
                c->itut_t35 = NULL;
1643
497
                c->n_itut_t35 = 0;
1644
1645
497
                pthread_mutex_unlock(&c->task_thread.lock);
1646
497
            }
1647
497
            if (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY) {
1648
176
                const int r = c->frame_hdr->existing_frame_idx;
1649
176
                c->refs[r].p.showable = 0;
1650
1.58k
                for (int i = 0; i < 8; i++) {
1651
1.40k
                    if (i == r) continue;
1652
1653
1.23k
                    if (c->refs[i].p.p.frame_hdr)
1654
1.12k
                        dav1d_thread_picture_unref(&c->refs[i].p);
1655
1.23k
                    dav1d_thread_picture_ref(&c->refs[i].p, &c->refs[r].p);
1656
1657
1.23k
                    dav1d_cdf_thread_unref(&c->cdf[i]);
1658
1.23k
                    dav1d_cdf_thread_ref(&c->cdf[i], &c->cdf[r]);
1659
1660
1.23k
                    dav1d_ref_dec(&c->refs[i].segmap);
1661
1.23k
                    c->refs[i].segmap = c->refs[r].segmap;
1662
1.23k
                    if (c->refs[r].segmap)
1663
315
                        dav1d_ref_inc(c->refs[r].segmap);
1664
1.23k
                    dav1d_ref_dec(&c->refs[i].refmvs);
1665
1.23k
                }
1666
176
            }
1667
497
            c->frame_hdr = NULL;
1668
33.3k
        } else if (c->n_tiles == c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows) {
1669
31.8k
            switch (c->frame_hdr->frame_type) {
1670
10.0k
            case DAV1D_FRAME_TYPE_INTER:
1671
10.2k
            case DAV1D_FRAME_TYPE_SWITCH:
1672
10.2k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE ||
1673
10.2k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1674
0
                     !c->frame_hdr->refresh_frame_flags))
1675
0
                    goto skip;
1676
10.2k
                break;
1677
10.2k
            case DAV1D_FRAME_TYPE_INTRA:
1678
2.86k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA ||
1679
2.86k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1680
0
                     !c->frame_hdr->refresh_frame_flags))
1681
0
                    goto skip;
1682
                // fall-through
1683
21.6k
            default:
1684
21.6k
                break;
1685
31.8k
            }
1686
31.8k
            if (!c->n_tile_data)
1687
0
                goto error;
1688
31.8k
            if ((res = dav1d_submit_frame(c)) < 0)
1689
67
                return res;
1690
31.7k
            assert(!c->n_tile_data);
1691
31.7k
            c->frame_hdr = NULL;
1692
31.7k
            c->n_tiles = 0;
1693
31.7k
        }
1694
33.8k
    }
1695
1696
68.2k
    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
1.31k
error:
1719
1.31k
    dav1d_data_props_copy(&c->cached_error_props, &in->m);
1720
1.31k
    dav1d_log(c, gb.error ? "Overrun in OBU bit buffer\n" :
1721
1.31k
                            "Error parsing OBU data\n");
1722
1.31k
    return DAV1D_ERR(EINVAL);
1723
68.3k
}