Coverage Report

Created: 2026-12-01 06:52

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