Coverage Report

Created: 2026-08-31 06:21

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
20.0k
{
51
20.0k
    const int trailing_one_bit = dav1d_get_bit(gb);
52
53
20.0k
    if (gb->error)
54
135
        return DAV1D_ERR(EINVAL);
55
56
19.8k
    if (!strict_std_compliance)
57
19.8k
        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
17.2k
{
76
17.2k
#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
17.2k
    memset(hdr, 0, sizeof(*hdr));
83
17.2k
    hdr->profile = dav1d_get_bits(gb, 3);
84
17.2k
    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
17.2k
    hdr->still_picture = dav1d_get_bit(gb);
91
17.2k
    hdr->reduced_still_picture_header = dav1d_get_bit(gb);
92
17.2k
    if (hdr->reduced_still_picture_header && !hdr->still_picture) goto error;
93
#if DEBUG_SEQ_HDR
94
    printf("SEQHDR: post-stillpicture_flags: off=%u\n",
95
           dav1d_get_bits_pos(gb) - init_bit_pos);
96
#endif
97
98
17.1k
    if (hdr->reduced_still_picture_header) {
99
11.1k
        hdr->num_operating_points = 1;
100
11.1k
        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);
101
11.1k
        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);
102
11.1k
        hdr->operating_points[0].initial_display_delay = 10;
103
11.1k
    } else {
104
6.07k
        hdr->timing_info_present = dav1d_get_bit(gb);
105
6.07k
        if (hdr->timing_info_present) {
106
2.64k
            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);
107
2.64k
            hdr->time_scale = dav1d_get_bits(gb, 32);
108
2.64k
            if (strict_std_compliance && (!hdr->num_units_in_tick || !hdr->time_scale))
109
0
                goto error;
110
2.64k
            hdr->equal_picture_interval = dav1d_get_bit(gb);
111
2.64k
            if (hdr->equal_picture_interval) {
112
126
                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
113
126
                if (num_ticks_per_picture == UINT32_MAX)
114
5
                    goto error;
115
121
                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
116
121
            }
117
118
2.63k
            hdr->decoder_model_info_present = dav1d_get_bit(gb);
119
2.63k
            if (hdr->decoder_model_info_present) {
120
2.54k
                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;
121
2.54k
                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);
122
2.54k
                if (strict_std_compliance && !hdr->num_units_in_decoding_tick)
123
0
                    goto error;
124
2.54k
                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;
125
2.54k
                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;
126
2.54k
            }
127
2.63k
        }
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
6.06k
        hdr->display_model_info_present = dav1d_get_bit(gb);
134
6.06k
        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;
135
83.2k
        for (int i = 0; i < hdr->num_operating_points; i++) {
136
77.2k
            struct Dav1dSequenceHeaderOperatingPoint *const op =
137
77.2k
                &hdr->operating_points[i];
138
77.2k
            op->idc = dav1d_get_bits(gb, 12);
139
77.2k
            if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00)))
140
56
                goto error;
141
77.1k
            op->major_level = 2 + dav1d_get_bits(gb, 3);
142
77.1k
            op->minor_level = dav1d_get_bits(gb, 2);
143
77.1k
            if (op->major_level > 3)
144
43.7k
                op->tier = dav1d_get_bit(gb);
145
77.1k
            if (hdr->decoder_model_info_present) {
146
9.00k
                op->decoder_model_param_present = dav1d_get_bit(gb);
147
9.00k
                if (op->decoder_model_param_present) {
148
648
                    struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
149
648
                        &hdr->operating_parameter_info[i];
150
648
                    opi->decoder_buffer_delay =
151
648
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
152
648
                    opi->encoder_buffer_delay =
153
648
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
154
648
                    opi->low_delay_mode = dav1d_get_bit(gb);
155
648
                }
156
9.00k
            }
157
77.1k
            if (hdr->display_model_info_present)
158
67.6k
                op->display_model_param_present = dav1d_get_bit(gb);
159
77.1k
            op->initial_display_delay =
160
77.1k
                op->display_model_param_present ? dav1d_get_bits(gb, 4) + 1 : 10;
161
77.1k
        }
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
6.06k
    }
167
168
17.1k
    hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;
169
17.1k
    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;
170
17.1k
    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;
171
17.1k
    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
17.1k
    if (!hdr->reduced_still_picture_header) {
177
6.01k
        hdr->frame_id_numbers_present = dav1d_get_bit(gb);
178
6.01k
        if (hdr->frame_id_numbers_present) {
179
228
            hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;
180
228
            hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;
181
228
        }
182
6.01k
    }
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
17.1k
    hdr->sb128 = dav1d_get_bit(gb);
189
17.1k
    hdr->filter_intra = dav1d_get_bit(gb);
190
17.1k
    hdr->intra_edge_filter = dav1d_get_bit(gb);
191
17.1k
    if (hdr->reduced_still_picture_header) {
192
11.1k
        hdr->screen_content_tools = DAV1D_ADAPTIVE;
193
11.1k
        hdr->force_integer_mv = DAV1D_ADAPTIVE;
194
11.1k
    } else {
195
6.01k
        hdr->inter_intra = dav1d_get_bit(gb);
196
6.01k
        hdr->masked_compound = dav1d_get_bit(gb);
197
6.01k
        hdr->warped_motion = dav1d_get_bit(gb);
198
6.01k
        hdr->dual_filter = dav1d_get_bit(gb);
199
6.01k
        hdr->order_hint = dav1d_get_bit(gb);
200
6.01k
        if (hdr->order_hint) {
201
1.46k
            hdr->jnt_comp = dav1d_get_bit(gb);
202
1.46k
            hdr->ref_frame_mvs = dav1d_get_bit(gb);
203
1.46k
        }
204
6.01k
        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
6.01k
        hdr->force_integer_mv = hdr->screen_content_tools ?
210
4.82k
                                dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb) : 2;
211
6.01k
        if (hdr->order_hint)
212
1.46k
            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;
213
6.01k
    }
214
17.1k
    hdr->super_res = dav1d_get_bit(gb);
215
17.1k
    hdr->cdef = dav1d_get_bit(gb);
216
17.1k
    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
17.1k
    hdr->hbd = dav1d_get_bit(gb);
223
17.1k
    if (hdr->profile == 2 && hdr->hbd)
224
1.09k
        hdr->hbd += dav1d_get_bit(gb);
225
17.1k
    if (hdr->profile != 1)
226
9.26k
        hdr->monochrome = dav1d_get_bit(gb);
227
17.1k
    hdr->color_description_present = dav1d_get_bit(gb);
228
17.1k
    if (hdr->color_description_present) {
229
5.48k
        hdr->pri = dav1d_get_bits(gb, 8);
230
5.48k
        hdr->trc = dav1d_get_bits(gb, 8);
231
5.48k
        hdr->mtrx = dav1d_get_bits(gb, 8);
232
11.6k
    } else {
233
11.6k
        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;
234
11.6k
        hdr->trc = DAV1D_TRC_UNKNOWN;
235
11.6k
        hdr->mtrx = DAV1D_MC_UNKNOWN;
236
11.6k
    }
237
17.1k
    if (hdr->monochrome) {
238
2.14k
        hdr->color_range = dav1d_get_bit(gb);
239
2.14k
        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;
240
2.14k
        hdr->ss_hor = hdr->ss_ver = 1;
241
2.14k
        hdr->chr = DAV1D_CHR_UNKNOWN;
242
14.9k
    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&
243
1.20k
               hdr->trc == DAV1D_TRC_SRGB &&
244
1.12k
               hdr->mtrx == DAV1D_MC_IDENTITY)
245
13
    {
246
13
        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
247
13
        hdr->color_range = 1;
248
13
        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))
249
7
            goto error;
250
14.9k
    } else {
251
14.9k
        hdr->color_range = dav1d_get_bit(gb);
252
14.9k
        switch (hdr->profile) {
253
5.92k
        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;
254
5.92k
                hdr->ss_hor = hdr->ss_ver = 1;
255
5.92k
                break;
256
7.85k
        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
257
7.85k
                break;
258
1.19k
        case 2:
259
1.19k
            if (hdr->hbd == 2) {
260
648
                hdr->ss_hor = dav1d_get_bit(gb);
261
648
                if (hdr->ss_hor)
262
168
                    hdr->ss_ver = dav1d_get_bit(gb);
263
648
            } else
264
542
                hdr->ss_hor = 1;
265
1.19k
            hdr->layout = hdr->ss_hor ?
266
710
                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :
267
710
                                        DAV1D_PIXEL_LAYOUT_I422 :
268
1.19k
                                        DAV1D_PIXEL_LAYOUT_I444;
269
1.19k
            break;
270
14.9k
        }
271
14.9k
        hdr->chr = (hdr->ss_hor & hdr->ss_ver) ?
272
8.97k
                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;
273
14.9k
    }
274
17.1k
    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
17.1k
    if (!hdr->monochrome)
280
14.9k
        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
17.1k
    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
17.1k
    return check_trailing_bits(gb, strict_std_compliance);
297
298
104
error:
299
104
    return DAV1D_ERR(EINVAL);
300
17.1k
}
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
81.8k
{
344
81.8k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
345
81.8k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
346
347
81.8k
    if (use_ref) {
348
70.7k
        for (int i = 0; i < 7; i++) {
349
70.7k
            if (dav1d_get_bit(gb)) {
350
27.8k
                const Dav1dThreadPicture *const ref =
351
27.8k
                    &c->refs[c->frame_hdr->refidx[i]].p;
352
27.8k
                if (!ref->p.frame_hdr) return -1;
353
27.8k
                hdr->width[1] = ref->p.frame_hdr->width[1];
354
27.8k
                hdr->height = ref->p.frame_hdr->height;
355
27.8k
                hdr->render_width = ref->p.frame_hdr->render_width;
356
27.8k
                hdr->render_height = ref->p.frame_hdr->render_height;
357
27.8k
                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
358
27.8k
                if (hdr->super_res.enabled) {
359
9.81k
                    const int d = hdr->super_res.width_scale_denominator =
360
9.81k
                        9 + dav1d_get_bits(gb, 3);
361
9.81k
                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,
362
9.81k
                                         imin(16, hdr->width[1]));
363
18.0k
                } else {
364
18.0k
                    hdr->super_res.width_scale_denominator = 8;
365
18.0k
                    hdr->width[0] = hdr->width[1];
366
18.0k
                }
367
27.8k
                return 0;
368
27.8k
            }
369
70.7k
        }
370
27.9k
    }
371
372
53.9k
    if (hdr->frame_size_override) {
373
5.42k
        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;
374
5.42k
        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;
375
48.5k
    } else {
376
48.5k
        hdr->width[1] = seqhdr->max_width;
377
48.5k
        hdr->height = seqhdr->max_height;
378
48.5k
    }
379
53.9k
    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
380
53.9k
    if (hdr->super_res.enabled) {
381
10.4k
        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);
382
10.4k
        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));
383
43.4k
    } else {
384
43.4k
        hdr->super_res.width_scale_denominator = 8;
385
43.4k
        hdr->width[0] = hdr->width[1];
386
43.4k
    }
387
53.9k
    hdr->have_render_size = dav1d_get_bit(gb);
388
53.9k
    if (hdr->have_render_size) {
389
18.3k
        hdr->render_width = dav1d_get_bits(gb, 16) + 1;
390
18.3k
        hdr->render_height = dav1d_get_bits(gb, 16) + 1;
391
35.5k
    } else {
392
35.5k
        hdr->render_width = hdr->width[1];
393
35.5k
        hdr->render_height = hdr->height;
394
35.5k
    }
395
53.9k
    return 0;
396
81.8k
}
397
398
421k
static inline int tile_log2(const int sz, const int tgt) {
399
421k
    int k;
400
491k
    for (k = 0; (sz << k) < tgt; k++) ;
401
421k
    return k;
402
421k
}
403
404
static const Dav1dLoopfilterModeRefDeltas default_mode_ref_deltas = {
405
    .mode_delta = { 0, 0 },
406
    .ref_delta = { 1, 0, 0, 0, -1, 0, -1, -1 },
407
};
408
409
82.0k
static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {
410
82.0k
#define DEBUG_FRAME_HDR 0
411
412
#if DEBUG_FRAME_HDR
413
    const uint8_t *const init_ptr = gb->ptr;
414
#endif
415
82.0k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
416
82.0k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
417
418
82.0k
    if (!seqhdr->reduced_still_picture_header)
419
68.6k
        hdr->show_existing_frame = dav1d_get_bit(gb);
420
#if DEBUG_FRAME_HDR
421
    printf("HDR: post-show_existing_frame: off=%td\n",
422
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
423
#endif
424
82.0k
    if (hdr->show_existing_frame) {
425
170
        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);
426
170
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
427
93
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
428
170
        if (seqhdr->frame_id_numbers_present) {
429
13
            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
430
13
            Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr;
431
13
            if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) goto error;
432
13
        }
433
158
        return 0;
434
170
    }
435
436
81.9k
    if (seqhdr->reduced_still_picture_header) {
437
13.4k
        hdr->frame_type = DAV1D_FRAME_TYPE_KEY;
438
13.4k
        hdr->show_frame = 1;
439
68.4k
    } else {
440
68.4k
        hdr->frame_type = dav1d_get_bits(gb, 2);
441
68.4k
        hdr->show_frame = dav1d_get_bit(gb);
442
68.4k
    }
443
81.9k
    if (hdr->show_frame) {
444
78.5k
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
445
19.0k
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
446
78.5k
        hdr->showable_frame = hdr->frame_type != DAV1D_FRAME_TYPE_KEY;
447
78.5k
    } else
448
3.38k
        hdr->showable_frame = dav1d_get_bit(gb);
449
81.9k
    hdr->error_resilient_mode =
450
81.9k
        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||
451
63.0k
        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||
452
62.6k
        seqhdr->reduced_still_picture_header || dav1d_get_bit(gb);
453
#if DEBUG_FRAME_HDR
454
    printf("HDR: post-frametype_bits: off=%td\n",
455
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
456
#endif
457
81.9k
    hdr->disable_cdf_update = dav1d_get_bit(gb);
458
81.9k
    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?
459
44.2k
                                      dav1d_get_bit(gb) : seqhdr->screen_content_tools;
460
81.9k
    if (hdr->allow_screen_content_tools)
461
34.1k
        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?
462
22.6k
                                dav1d_get_bit(gb) : seqhdr->force_integer_mv;
463
464
81.9k
    if (IS_KEY_OR_INTRA(hdr))
465
20.9k
        hdr->force_integer_mv = 1;
466
467
81.9k
    if (seqhdr->frame_id_numbers_present)
468
726
        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
469
470
81.9k
    if (!seqhdr->reduced_still_picture_header)
471
68.4k
        hdr->frame_size_override = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 1 : dav1d_get_bit(gb);
472
#if DEBUG_FRAME_HDR
473
    printf("HDR: post-frame_size_override_flag: off=%td\n",
474
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
475
#endif
476
81.9k
    if (seqhdr->order_hint)
477
31.8k
        hdr->frame_offset = dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
478
81.9k
    hdr->primary_ref_frame = !hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) ?
479
58.4k
                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;
480
481
81.9k
    if (seqhdr->decoder_model_info_present) {
482
21.6k
        hdr->buffer_removal_time_present = dav1d_get_bit(gb);
483
21.6k
        if (hdr->buffer_removal_time_present) {
484
97.7k
            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {
485
85.8k
                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];
486
85.8k
                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];
487
85.8k
                if (seqop->decoder_model_param_present) {
488
3.30k
                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;
489
3.30k
                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;
490
3.30k
                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))
491
742
                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);
492
3.30k
                }
493
85.8k
            }
494
11.9k
        }
495
21.6k
    }
496
497
81.9k
    if (IS_KEY_OR_INTRA(hdr)) {
498
20.9k
        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&
499
20.4k
                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);
500
20.9k
        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
501
396
            for (int i = 0; i < 8; i++)
502
352
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
503
20.9k
        if (c->strict_std_compliance &&
504
0
            hdr->frame_type == DAV1D_FRAME_TYPE_INTRA && hdr->refresh_frame_flags == 0xff)
505
0
        {
506
0
            goto error;
507
0
        }
508
20.9k
        if (read_frame_size(c, gb, 0) < 0) goto error;
509
20.9k
        if (hdr->allow_screen_content_tools && !hdr->super_res.enabled)
510
4.59k
            hdr->allow_intrabc = dav1d_get_bit(gb);
511
60.9k
    } else {
512
60.9k
        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :
513
60.9k
                                   dav1d_get_bits(gb, 8);
514
60.9k
        if (hdr->error_resilient_mode && seqhdr->order_hint)
515
3.96k
            for (int i = 0; i < 8; i++)
516
3.52k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
517
60.9k
        if (seqhdr->order_hint) {
518
29.7k
            hdr->frame_ref_short_signaling = dav1d_get_bit(gb);
519
29.7k
            if (hdr->frame_ref_short_signaling) {
520
24.4k
                hdr->refidx[0] = dav1d_get_bits(gb, 3);
521
24.4k
                hdr->refidx[1] = hdr->refidx[2] = -1;
522
24.4k
                hdr->refidx[3] = dav1d_get_bits(gb, 3);
523
524
                /* +1 allows for unconditional stores, as unused
525
                 * values can be dumped into frame_offset[-1]. */
526
24.4k
                int frame_offset_mem[8+1];
527
24.4k
                int *const frame_offset = &frame_offset_mem[1];
528
24.4k
                int earliest_ref = -1;
529
220k
                for (int i = 0, earliest_offset = INT_MAX; i < 8; i++) {
530
195k
                    const Dav1dFrameHeader *const refhdr = c->refs[i].p.p.frame_hdr;
531
195k
                    if (!refhdr) goto error;
532
195k
                    const int diff = get_poc_diff(seqhdr->order_hint_n_bits,
533
195k
                                                  refhdr->frame_offset,
534
195k
                                                  hdr->frame_offset);
535
195k
                    frame_offset[i] = diff;
536
195k
                    if (diff < earliest_offset) {
537
42.1k
                        earliest_offset = diff;
538
42.1k
                        earliest_ref = i;
539
42.1k
                    }
540
195k
                }
541
24.4k
                frame_offset[hdr->refidx[0]] = INT_MIN; // = reference frame is used
542
24.4k
                frame_offset[hdr->refidx[3]] = INT_MIN;
543
24.4k
                assert(earliest_ref >= 0);
544
545
24.4k
                int refidx = -1;
546
220k
                for (int i = 0, latest_offset = 0; i < 8; i++) {
547
195k
                    const int hint = frame_offset[i];
548
195k
                    if (hint >= latest_offset) {
549
89.1k
                        latest_offset = hint;
550
89.1k
                        refidx = i;
551
89.1k
                    }
552
195k
                }
553
24.4k
                frame_offset[refidx] = INT_MIN;
554
24.4k
                hdr->refidx[6] = refidx;
555
556
73.3k
                for (int i = 4; i < 6; i++) {
557
                    /* Unsigned compares to handle negative values. */
558
48.9k
                    unsigned earliest_offset = UINT8_MAX;
559
48.9k
                    refidx = -1;
560
440k
                    for (int j = 0; j < 8; j++) {
561
391k
                        const unsigned hint = frame_offset[j];
562
391k
                        if (hint < earliest_offset) {
563
55.0k
                            earliest_offset = hint;
564
55.0k
                            refidx = j;
565
55.0k
                        }
566
391k
                    }
567
48.9k
                    frame_offset[refidx] = INT_MIN;
568
48.9k
                    hdr->refidx[i] = refidx;
569
48.9k
                }
570
571
171k
                for (int i = 1; i < 7; i++) {
572
146k
                    refidx = hdr->refidx[i];
573
146k
                    if (refidx < 0) {
574
53.0k
                        unsigned latest_offset = ~UINT8_MAX;
575
477k
                        for (int j = 0; j < 8; j++) {
576
424k
                            const unsigned hint = frame_offset[j];
577
424k
                            if (hint >= latest_offset) {
578
74.7k
                                latest_offset = hint;
579
74.7k
                                refidx = j;
580
74.7k
                            }
581
424k
                        }
582
53.0k
                        frame_offset[refidx] = INT_MIN;
583
53.0k
                        hdr->refidx[i] = refidx >= 0 ? refidx : earliest_ref;
584
53.0k
                    }
585
146k
                }
586
24.4k
            }
587
29.7k
        }
588
487k
        for (int i = 0; i < 7; i++) {
589
426k
            if (!hdr->frame_ref_short_signaling)
590
255k
                hdr->refidx[i] = dav1d_get_bits(gb, 3);
591
426k
            if (seqhdr->frame_id_numbers_present) {
592
61
                const unsigned delta_ref_frame_id = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits) + 1;
593
61
                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);
594
61
                Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr;
595
61
                if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error;
596
61
            }
597
426k
        }
598
60.8k
        const int use_ref = !hdr->error_resilient_mode &&
599
58.3k
                            hdr->frame_size_override;
600
60.8k
        if (read_frame_size(c, gb, use_ref) < 0) goto error;
601
60.8k
        if (!hdr->force_integer_mv)
602
48.1k
            hdr->hp = dav1d_get_bit(gb);
603
60.8k
        hdr->subpel_filter_mode = dav1d_get_bit(gb) ? DAV1D_FILTER_SWITCHABLE :
604
60.8k
                                                      dav1d_get_bits(gb, 2);
605
60.8k
        hdr->switchable_motion_mode = dav1d_get_bit(gb);
606
60.8k
        if (!hdr->error_resilient_mode && seqhdr->ref_frame_mvs &&
607
19.7k
            seqhdr->order_hint && IS_INTER_OR_SWITCH(hdr))
608
19.7k
        {
609
19.7k
            hdr->use_ref_frame_mvs = dav1d_get_bit(gb);
610
19.7k
        }
611
60.8k
    }
612
#if DEBUG_FRAME_HDR
613
    printf("HDR: post-frametype-specific-bits: off=%td\n",
614
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
615
#endif
616
617
81.8k
    if (!seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update)
618
55.3k
        hdr->refresh_context = !dav1d_get_bit(gb);
619
#if DEBUG_FRAME_HDR
620
    printf("HDR: post-refresh_context: off=%td\n",
621
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
622
#endif
623
624
    // tile data
625
81.8k
    hdr->tiling.uniform = dav1d_get_bit(gb);
626
81.8k
    const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
627
81.8k
    const int sbsz_log2 = 6 + seqhdr->sb128;
628
81.8k
    const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
629
81.8k
    const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
630
81.8k
    const int max_tile_width_sb = 4096 >> sbsz_log2;
631
81.8k
    const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
632
81.8k
    hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
633
81.8k
    hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
634
81.8k
    hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
635
81.8k
    const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
636
81.8k
                              hdr->tiling.min_log2_cols);
637
81.8k
    if (hdr->tiling.uniform) {
638
34.5k
        for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
639
35.4k
             hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bit(gb);
640
34.5k
             hdr->tiling.log2_cols++) ;
641
34.5k
        const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols);
642
34.5k
        hdr->tiling.cols = 0;
643
74.3k
        for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++)
644
39.7k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
645
34.5k
        hdr->tiling.min_log2_rows =
646
34.5k
            imax(min_log2_tiles - hdr->tiling.log2_cols, 0);
647
648
34.5k
        for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows;
649
36.2k
             hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bit(gb);
650
34.5k
             hdr->tiling.log2_rows++) ;
651
34.5k
        const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows);
652
34.5k
        hdr->tiling.rows = 0;
653
74.0k
        for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++)
654
39.5k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
655
47.2k
    } else {
656
47.2k
        hdr->tiling.cols = 0;
657
47.2k
        int widest_tile = 0, max_tile_area_sb = sbw * sbh;
658
100k
        for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) {
659
52.8k
            const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb);
660
52.8k
            const int tile_w = (tile_width_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_width_sb) : 1;
661
52.8k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
662
52.8k
            sbx += tile_w;
663
52.8k
            widest_tile = imax(widest_tile, tile_w);
664
52.8k
        }
665
47.2k
        hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
666
47.2k
        if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
667
47.2k
        const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
668
669
47.2k
        hdr->tiling.rows = 0;
670
100k
        for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
671
53.6k
            const int tile_height_sb = imin(sbh - sby, max_tile_height_sb);
672
53.6k
            const int tile_h = (tile_height_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_height_sb) : 1;
673
53.6k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
674
53.6k
            sby += tile_h;
675
53.6k
        }
676
47.2k
        hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows);
677
47.2k
    }
678
81.8k
    hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw;
679
81.8k
    hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh;
680
81.8k
    if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) {
681
3.20k
        hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols + hdr->tiling.log2_rows);
682
3.20k
        if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows)
683
42
            goto error;
684
3.16k
        hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1;
685
3.16k
    }
686
#if DEBUG_FRAME_HDR
687
    printf("HDR: post-tiling: off=%td\n",
688
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
689
#endif
690
691
    // quant data
692
81.7k
    hdr->quant.yac = dav1d_get_bits(gb, 8);
693
81.7k
    if (dav1d_get_bit(gb))
694
47.3k
        hdr->quant.ydc_delta = dav1d_get_sbits(gb, 7);
695
81.7k
    if (!seqhdr->monochrome) {
696
        // If the sequence header says that delta_q might be different
697
        // for U, V, we must check whether it actually is for this
698
        // frame.
699
64.8k
        const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bit(gb) : 0;
700
64.8k
        if (dav1d_get_bit(gb))
701
35.1k
            hdr->quant.udc_delta = dav1d_get_sbits(gb, 7);
702
64.8k
        if (dav1d_get_bit(gb))
703
36.1k
            hdr->quant.uac_delta = dav1d_get_sbits(gb, 7);
704
64.8k
        if (diff_uv_delta) {
705
18.4k
            if (dav1d_get_bit(gb))
706
5.77k
                hdr->quant.vdc_delta = dav1d_get_sbits(gb, 7);
707
18.4k
            if (dav1d_get_bit(gb))
708
16.4k
                hdr->quant.vac_delta = dav1d_get_sbits(gb, 7);
709
46.4k
        } else {
710
46.4k
            hdr->quant.vdc_delta = hdr->quant.udc_delta;
711
46.4k
            hdr->quant.vac_delta = hdr->quant.uac_delta;
712
46.4k
        }
713
64.8k
    }
714
#if DEBUG_FRAME_HDR
715
    printf("HDR: post-quant: off=%td\n",
716
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
717
#endif
718
81.7k
    hdr->quant.qm = dav1d_get_bit(gb);
719
81.7k
    if (hdr->quant.qm) {
720
62.8k
        hdr->quant.qm_y = dav1d_get_bits(gb, 4);
721
62.8k
        hdr->quant.qm_u = dav1d_get_bits(gb, 4);
722
62.8k
        hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 4) :
723
62.8k
                                                        hdr->quant.qm_u;
724
62.8k
    }
725
#if DEBUG_FRAME_HDR
726
    printf("HDR: post-qm: off=%td\n",
727
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
728
#endif
729
730
    // segmentation data
731
81.7k
    hdr->segmentation.enabled = dav1d_get_bit(gb);
732
81.7k
    if (hdr->segmentation.enabled) {
733
26.2k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
734
9.65k
            hdr->segmentation.update_map = 1;
735
9.65k
            hdr->segmentation.update_data = 1;
736
16.5k
        } else {
737
16.5k
            hdr->segmentation.update_map = dav1d_get_bit(gb);
738
16.5k
            if (hdr->segmentation.update_map)
739
1.69k
                hdr->segmentation.temporal = dav1d_get_bit(gb);
740
16.5k
            hdr->segmentation.update_data = dav1d_get_bit(gb);
741
16.5k
        }
742
743
26.2k
        if (hdr->segmentation.update_data) {
744
10.2k
            hdr->segmentation.seg_data.last_active_segid = -1;
745
92.0k
            for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
746
81.8k
                Dav1dSegmentationData *const seg =
747
81.8k
                    &hdr->segmentation.seg_data.d[i];
748
81.8k
                if (dav1d_get_bit(gb)) {
749
24.9k
                    seg->delta_q = dav1d_get_sbits(gb, 9);
750
24.9k
                    hdr->segmentation.seg_data.last_active_segid = i;
751
24.9k
                }
752
81.8k
                if (dav1d_get_bit(gb)) {
753
24.1k
                    seg->delta_lf_y_v = dav1d_get_sbits(gb, 7);
754
24.1k
                    hdr->segmentation.seg_data.last_active_segid = i;
755
24.1k
                }
756
81.8k
                if (dav1d_get_bit(gb)) {
757
41.4k
                    seg->delta_lf_y_h = dav1d_get_sbits(gb, 7);
758
41.4k
                    hdr->segmentation.seg_data.last_active_segid = i;
759
41.4k
                }
760
81.8k
                if (dav1d_get_bit(gb)) {
761
41.1k
                    seg->delta_lf_u = dav1d_get_sbits(gb, 7);
762
41.1k
                    hdr->segmentation.seg_data.last_active_segid = i;
763
41.1k
                }
764
81.8k
                if (dav1d_get_bit(gb)) {
765
40.7k
                    seg->delta_lf_v = dav1d_get_sbits(gb, 7);
766
40.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
767
40.7k
                }
768
81.8k
                if (dav1d_get_bit(gb)) {
769
44.3k
                    seg->ref = dav1d_get_bits(gb, 3);
770
44.3k
                    hdr->segmentation.seg_data.last_active_segid = i;
771
44.3k
                    hdr->segmentation.seg_data.preskip = 1;
772
44.3k
                } else {
773
37.4k
                    seg->ref = -1;
774
37.4k
                }
775
81.8k
                if ((seg->skip = dav1d_get_bit(gb))) {
776
39.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
777
39.8k
                    hdr->segmentation.seg_data.preskip = 1;
778
39.8k
                }
779
81.8k
                if ((seg->globalmv = dav1d_get_bit(gb))) {
780
33.6k
                    hdr->segmentation.seg_data.last_active_segid = i;
781
33.6k
                    hdr->segmentation.seg_data.preskip = 1;
782
33.6k
                }
783
81.8k
            }
784
16.0k
        } else {
785
            // segmentation.update_data was false so we should copy
786
            // segmentation data from the reference frame.
787
16.0k
            assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
788
16.0k
            const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
789
16.0k
            if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
790
15.9k
            hdr->segmentation.seg_data =
791
15.9k
                c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data;
792
15.9k
        }
793
55.5k
    } else {
794
499k
        for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++)
795
444k
            hdr->segmentation.seg_data.d[i].ref = -1;
796
55.5k
    }
797
#if DEBUG_FRAME_HDR
798
    printf("HDR: post-segmentation: off=%td\n",
799
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
800
#endif
801
802
    // delta q
803
81.7k
    if (hdr->quant.yac) {
804
80.6k
        hdr->delta.q.present = dav1d_get_bit(gb);
805
80.6k
        if (hdr->delta.q.present) {
806
29.4k
            hdr->delta.q.res_log2 = dav1d_get_bits(gb, 2);
807
29.4k
            if (!hdr->allow_intrabc) {
808
28.7k
                hdr->delta.lf.present = dav1d_get_bit(gb);
809
28.7k
                if (hdr->delta.lf.present) {
810
3.65k
                    hdr->delta.lf.res_log2 = dav1d_get_bits(gb, 2);
811
3.65k
                    hdr->delta.lf.multi = dav1d_get_bit(gb);
812
3.65k
                }
813
28.7k
            }
814
29.4k
        }
815
80.6k
    }
816
#if DEBUG_FRAME_HDR
817
    printf("HDR: post-delta_q_lf_flags: off=%td\n",
818
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
819
#endif
820
821
    // derive lossless flags
822
81.7k
    const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta &&
823
26.0k
        !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta;
824
81.7k
    hdr->all_lossless = 1;
825
735k
    for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
826
654k
        hdr->segmentation.qidx[i] = hdr->segmentation.enabled ?
827
209k
            iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) :
828
654k
            hdr->quant.yac;
829
654k
        hdr->segmentation.lossless[i] =
830
654k
            !hdr->segmentation.qidx[i] && delta_lossless;
831
654k
        hdr->all_lossless &= hdr->segmentation.lossless[i];
832
654k
    }
833
834
    // loopfilter
835
81.7k
    if (hdr->all_lossless || hdr->allow_intrabc) {
836
3.69k
        hdr->loopfilter.mode_ref_delta_enabled = 1;
837
3.69k
        hdr->loopfilter.mode_ref_delta_update = 1;
838
3.69k
        hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
839
78.0k
    } else {
840
78.0k
        hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6);
841
78.0k
        hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6);
842
78.0k
        if (!seqhdr->monochrome &&
843
61.8k
            (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1]))
844
59.5k
        {
845
59.5k
            hdr->loopfilter.level_u = dav1d_get_bits(gb, 6);
846
59.5k
            hdr->loopfilter.level_v = dav1d_get_bits(gb, 6);
847
59.5k
        }
848
78.0k
        hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3);
849
850
78.0k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
851
20.1k
            hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
852
57.9k
        } else {
853
57.9k
            const int ref = hdr->refidx[hdr->primary_ref_frame];
854
57.9k
            if (!c->refs[ref].p.p.frame_hdr) goto error;
855
57.8k
            hdr->loopfilter.mode_ref_deltas =
856
57.8k
                c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas;
857
57.8k
        }
858
78.0k
        hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bit(gb);
859
78.0k
        if (hdr->loopfilter.mode_ref_delta_enabled) {
860
8.91k
            hdr->loopfilter.mode_ref_delta_update = dav1d_get_bit(gb);
861
8.91k
            if (hdr->loopfilter.mode_ref_delta_update) {
862
49.9k
                for (int i = 0; i < 8; i++)
863
44.4k
                    if (dav1d_get_bit(gb))
864
28.9k
                        hdr->loopfilter.mode_ref_deltas.ref_delta[i] =
865
28.9k
                            dav1d_get_sbits(gb, 7);
866
16.6k
                for (int i = 0; i < 2; i++)
867
11.1k
                    if (dav1d_get_bit(gb))
868
7.44k
                        hdr->loopfilter.mode_ref_deltas.mode_delta[i] =
869
7.44k
                            dav1d_get_sbits(gb, 7);
870
5.55k
            }
871
8.91k
        }
872
78.0k
    }
873
#if DEBUG_FRAME_HDR
874
    printf("HDR: post-lpf: off=%td\n",
875
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
876
#endif
877
878
    // cdef
879
81.7k
    if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) {
880
36.3k
        hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3;
881
36.3k
        hdr->cdef.n_bits = dav1d_get_bits(gb, 2);
882
101k
        for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) {
883
65.4k
            hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6);
884
65.4k
            if (!seqhdr->monochrome)
885
37.8k
                hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6);
886
65.4k
        }
887
36.3k
    }
888
#if DEBUG_FRAME_HDR
889
    printf("HDR: post-cdef: off=%td\n",
890
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
891
#endif
892
893
    // restoration
894
81.7k
    if ((!hdr->all_lossless || hdr->super_res.enabled) &&
895
80.9k
        seqhdr->restoration && !hdr->allow_intrabc)
896
55.3k
    {
897
55.3k
        hdr->restoration.type[0] = dav1d_get_bits(gb, 2);
898
55.3k
        if (!seqhdr->monochrome) {
899
45.4k
            hdr->restoration.type[1] = dav1d_get_bits(gb, 2);
900
45.4k
            hdr->restoration.type[2] = dav1d_get_bits(gb, 2);
901
45.4k
        }
902
903
55.3k
        if (hdr->restoration.type[0] || hdr->restoration.type[1] ||
904
9.93k
            hdr->restoration.type[2])
905
46.7k
        {
906
            // Log2 of the restoration unit size.
907
46.7k
            hdr->restoration.unit_size[0] = 6 + seqhdr->sb128;
908
46.7k
            if (dav1d_get_bit(gb)) {
909
22.5k
                hdr->restoration.unit_size[0]++;
910
22.5k
                if (!seqhdr->sb128)
911
12.8k
                    hdr->restoration.unit_size[0] += dav1d_get_bit(gb);
912
22.5k
            }
913
46.7k
            hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0];
914
46.7k
            if ((hdr->restoration.type[1] || hdr->restoration.type[2]) &&
915
42.7k
                seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1)
916
19.9k
            {
917
19.9k
                hdr->restoration.unit_size[1] -= dav1d_get_bit(gb);
918
19.9k
            }
919
46.7k
        } else {
920
8.67k
            hdr->restoration.unit_size[0] = 8;
921
8.67k
        }
922
55.3k
    }
923
#if DEBUG_FRAME_HDR
924
    printf("HDR: post-restoration: off=%td\n",
925
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
926
#endif
927
928
81.7k
    if (!hdr->all_lossless)
929
80.8k
        hdr->txfm_mode = dav1d_get_bit(gb) ? DAV1D_TX_SWITCHABLE : DAV1D_TX_LARGEST;
930
#if DEBUG_FRAME_HDR
931
    printf("HDR: post-txfmmode: off=%td\n",
932
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
933
#endif
934
81.7k
    if (IS_INTER_OR_SWITCH(hdr))
935
60.8k
        hdr->switchable_comp_refs = dav1d_get_bit(gb);
936
#if DEBUG_FRAME_HDR
937
    printf("HDR: post-refmode: off=%td\n",
938
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
939
#endif
940
81.7k
    if (hdr->switchable_comp_refs && IS_INTER_OR_SWITCH(hdr) && seqhdr->order_hint) {
941
23.6k
        const int poc = hdr->frame_offset;
942
23.6k
        int off_before = -1, off_after = -1;
943
23.6k
        int off_before_idx, off_after_idx;
944
188k
        for (int i = 0; i < 7; i++) {
945
165k
            if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
946
165k
            const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
947
948
165k
            const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc);
949
165k
            if (diff > 0) {
950
22.7k
                if (off_after < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
951
14.4k
                                                  off_after, refpoc) > 0)
952
9.56k
                {
953
9.56k
                    off_after = refpoc;
954
9.56k
                    off_after_idx = i;
955
9.56k
                }
956
142k
            } else if (diff < 0 && (off_before < 0 ||
957
31.7k
                                    get_poc_diff(seqhdr->order_hint_n_bits,
958
31.7k
                                                 refpoc, off_before) > 0))
959
16.1k
            {
960
16.1k
                off_before = refpoc;
961
16.1k
                off_before_idx = i;
962
16.1k
            }
963
165k
        }
964
965
23.6k
        if ((off_before | off_after) >= 0) {
966
2.54k
            hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx);
967
2.54k
            hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx);
968
2.54k
            hdr->skip_mode_allowed = 1;
969
21.0k
        } else if (off_before >= 0) {
970
12.5k
            int off_before2 = -1;
971
12.5k
            int off_before2_idx;
972
100k
            for (int i = 0; i < 7; i++) {
973
87.5k
                if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
974
87.5k
                const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
975
87.5k
                if (get_poc_diff(seqhdr->order_hint_n_bits,
976
87.5k
                                 refpoc, off_before) < 0) {
977
36.5k
                    if (off_before2 < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
978
27.6k
                                                        refpoc, off_before2) > 0)
979
9.14k
                    {
980
9.14k
                        off_before2 = refpoc;
981
9.14k
                        off_before2_idx = i;
982
9.14k
                    }
983
36.5k
                }
984
87.5k
            }
985
986
12.5k
            if (off_before2 >= 0) {
987
8.87k
                hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx);
988
8.87k
                hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx);
989
8.87k
                hdr->skip_mode_allowed = 1;
990
8.87k
            }
991
12.5k
        }
992
23.6k
    }
993
81.7k
    if (hdr->skip_mode_allowed)
994
11.4k
        hdr->skip_mode_enabled = dav1d_get_bit(gb);
995
#if DEBUG_FRAME_HDR
996
    printf("HDR: post-extskip: off=%td\n",
997
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
998
#endif
999
81.7k
    if (!hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) && seqhdr->warped_motion)
1000
34.0k
        hdr->warp_motion = dav1d_get_bit(gb);
1001
#if DEBUG_FRAME_HDR
1002
    printf("HDR: post-warpmotionbit: off=%td\n",
1003
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1004
#endif
1005
81.7k
    hdr->reduced_txtp_set = dav1d_get_bit(gb);
1006
#if DEBUG_FRAME_HDR
1007
    printf("HDR: post-reducedtxtpset: off=%td\n",
1008
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1009
#endif
1010
1011
653k
    for (int i = 0; i < 7; i++)
1012
572k
        hdr->gmv[i] = dav1d_default_wm_params;
1013
1014
81.7k
    if (IS_INTER_OR_SWITCH(hdr)) {
1015
486k
        for (int i = 0; i < 7; i++) {
1016
425k
            hdr->gmv[i].type = !dav1d_get_bit(gb) ? DAV1D_WM_TYPE_IDENTITY :
1017
425k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_ROT_ZOOM :
1018
167k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_TRANSLATION :
1019
73.5k
                                                    DAV1D_WM_TYPE_AFFINE;
1020
1021
425k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue;
1022
1023
167k
            const Dav1dWarpedMotionParams *ref_gmv;
1024
167k
            if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
1025
6.79k
                ref_gmv = &dav1d_default_wm_params;
1026
160k
            } else {
1027
160k
                const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
1028
160k
                if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
1029
160k
                ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i];
1030
160k
            }
1031
167k
            int32_t *const mat = hdr->gmv[i].matrix;
1032
167k
            const int32_t *const ref_mat = ref_gmv->matrix;
1033
167k
            int bits, shift;
1034
1035
167k
            if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) {
1036
164k
                mat[2] = (1 << 16) + 2 *
1037
164k
                    dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12);
1038
164k
                mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12);
1039
1040
164k
                bits = 12;
1041
164k
                shift = 10;
1042
164k
            } else {
1043
2.83k
                bits = 9 - !hdr->hp;
1044
2.83k
                shift = 13 + !hdr->hp;
1045
2.83k
            }
1046
1047
167k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) {
1048
70.6k
                mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12);
1049
70.6k
                mat[5] = (1 << 16) + 2 *
1050
70.6k
                    dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12);
1051
96.6k
            } else {
1052
96.6k
                mat[4] = -mat[3];
1053
96.6k
                mat[5] = mat[2];
1054
96.6k
            }
1055
1056
167k
            mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift);
1057
167k
            mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift);
1058
167k
        }
1059
60.8k
    }
1060
#if DEBUG_FRAME_HDR
1061
    printf("HDR: post-gmv: off=%td\n",
1062
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1063
#endif
1064
1065
81.7k
    if (seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame)) {
1066
8.84k
        hdr->film_grain.present = dav1d_get_bit(gb);
1067
8.84k
        if (hdr->film_grain.present) {
1068
2.02k
            const unsigned seed = dav1d_get_bits(gb, 16);
1069
2.02k
            hdr->film_grain.update = hdr->frame_type != DAV1D_FRAME_TYPE_INTER || dav1d_get_bit(gb);
1070
2.02k
            if (!hdr->film_grain.update) {
1071
314
                const int refidx = dav1d_get_bits(gb, 3);
1072
314
                int i;
1073
1.18k
                for (i = 0; i < 7; i++)
1074
1.17k
                    if (hdr->refidx[i] == refidx)
1075
301
                        break;
1076
314
                if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error;
1077
298
                hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data;
1078
298
                hdr->film_grain.data.seed = seed;
1079
1.71k
            } else {
1080
1.71k
                Dav1dFilmGrainData *const fgd = &hdr->film_grain.data;
1081
1.71k
                fgd->seed = seed;
1082
1083
1.71k
                fgd->num_y_points = dav1d_get_bits(gb, 4);
1084
1.71k
                if (fgd->num_y_points > 14) goto error;
1085
3.34k
                for (int i = 0; i < fgd->num_y_points; i++) {
1086
1.68k
                    fgd->y_points[i][0] = dav1d_get_bits(gb, 8);
1087
1.68k
                    if (i && fgd->y_points[i - 1][0] >= fgd->y_points[i][0])
1088
40
                        goto error;
1089
1.64k
                    fgd->y_points[i][1] = dav1d_get_bits(gb, 8);
1090
1.64k
                }
1091
1092
1.66k
                if (!seqhdr->monochrome)
1093
1.42k
                    fgd->chroma_scaling_from_luma = dav1d_get_bit(gb);
1094
1.66k
                if (seqhdr->monochrome || fgd->chroma_scaling_from_luma ||
1095
886
                    (seqhdr->ss_ver == 1 && seqhdr->ss_hor == 1 && !fgd->num_y_points))
1096
847
                {
1097
847
                    fgd->num_uv_points[0] = fgd->num_uv_points[1] = 0;
1098
2.38k
                } else for (int pl = 0; pl < 2; pl++) {
1099
1.60k
                    fgd->num_uv_points[pl] = dav1d_get_bits(gb, 4);
1100
1.60k
                    if (fgd->num_uv_points[pl] > 10) goto error;
1101
2.41k
                    for (int i = 0; i < fgd->num_uv_points[pl]; i++) {
1102
845
                        fgd->uv_points[pl][i][0] = dav1d_get_bits(gb, 8);
1103
845
                        if (i && fgd->uv_points[pl][i - 1][0] >= fgd->uv_points[pl][i][0])
1104
20
                            goto error;
1105
825
                        fgd->uv_points[pl][i][1] = dav1d_get_bits(gb, 8);
1106
825
                    }
1107
1.59k
                }
1108
1109
1.62k
                if (seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1 &&
1110
782
                    !!fgd->num_uv_points[0] != !!fgd->num_uv_points[1])
1111
3
                {
1112
3
                    goto error;
1113
3
                }
1114
1115
1.62k
                fgd->scaling_shift = dav1d_get_bits(gb, 2) + 8;
1116
1.62k
                fgd->ar_coeff_lag = dav1d_get_bits(gb, 2);
1117
1.62k
                const int num_y_pos = 2 * fgd->ar_coeff_lag * (fgd->ar_coeff_lag + 1);
1118
1.62k
                if (fgd->num_y_points)
1119
9.86k
                    for (int i = 0; i < num_y_pos; i++)
1120
8.84k
                        fgd->ar_coeffs_y[i] = dav1d_get_bits(gb, 8) - 128;
1121
4.87k
                for (int pl = 0; pl < 2; pl++)
1122
3.25k
                    if (fgd->num_uv_points[pl] || fgd->chroma_scaling_from_luma) {
1123
1.70k
                        const int num_uv_pos = num_y_pos + !!fgd->num_y_points;
1124
19.8k
                        for (int i = 0; i < num_uv_pos; i++)
1125
18.1k
                            fgd->ar_coeffs_uv[pl][i] = dav1d_get_bits(gb, 8) - 128;
1126
1.70k
                        if (!fgd->num_y_points)
1127
384
                            fgd->ar_coeffs_uv[pl][num_uv_pos] = 0;
1128
1.70k
                    }
1129
1.62k
                fgd->ar_coeff_shift = dav1d_get_bits(gb, 2) + 6;
1130
1.62k
                fgd->grain_scale_shift = dav1d_get_bits(gb, 2);
1131
4.87k
                for (int pl = 0; pl < 2; pl++)
1132
3.25k
                    if (fgd->num_uv_points[pl]) {
1133
617
                        fgd->uv_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1134
617
                        fgd->uv_luma_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1135
617
                        fgd->uv_offset[pl] = dav1d_get_bits(gb, 9) - 256;
1136
617
                    }
1137
1.62k
                fgd->overlap_flag = dav1d_get_bit(gb);
1138
1.62k
                fgd->clip_to_restricted_range = dav1d_get_bit(gb);
1139
1.62k
            }
1140
2.02k
        }
1141
8.84k
    }
1142
#if DEBUG_FRAME_HDR
1143
    printf("HDR: post-filmgrain: off=%td\n",
1144
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1145
#endif
1146
1147
81.6k
    return 0;
1148
1149
284
error:
1150
284
    dav1d_log(c, "Error parsing frame header\n");
1151
284
    return DAV1D_ERR(EINVAL);
1152
81.7k
}
1153
1154
78.9k
static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) {
1155
78.9k
    const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows;
1156
78.9k
    const int have_tile_pos = n_tiles > 1 ? dav1d_get_bit(gb) : 0;
1157
1158
78.9k
    if (have_tile_pos) {
1159
86
        const int n_bits = c->frame_hdr->tiling.log2_cols +
1160
86
                           c->frame_hdr->tiling.log2_rows;
1161
86
        c->tile[c->n_tile_data].start = dav1d_get_bits(gb, n_bits);
1162
86
        c->tile[c->n_tile_data].end = dav1d_get_bits(gb, n_bits);
1163
78.8k
    } else {
1164
78.8k
        c->tile[c->n_tile_data].start = 0;
1165
78.8k
        c->tile[c->n_tile_data].end = n_tiles - 1;
1166
78.8k
    }
1167
78.9k
}
1168
1169
116k
ptrdiff_t dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in) {
1170
116k
    GetBits gb;
1171
116k
    int res;
1172
1173
116k
    dav1d_init_get_bits(&gb, in->data, in->sz);
1174
1175
    // obu header
1176
116k
    const int obu_forbidden_bit = dav1d_get_bit(&gb);
1177
116k
    if (c->strict_std_compliance && obu_forbidden_bit) goto error;
1178
116k
    const enum Dav1dObuType type = dav1d_get_bits(&gb, 4);
1179
116k
    const int has_extension = dav1d_get_bit(&gb);
1180
116k
    const int has_length_field = dav1d_get_bit(&gb);
1181
116k
    dav1d_get_bit(&gb); // reserved
1182
1183
116k
    int temporal_id = 0, spatial_id = 0;
1184
116k
    if (has_extension) {
1185
11.6k
        temporal_id = dav1d_get_bits(&gb, 3);
1186
11.6k
        spatial_id = dav1d_get_bits(&gb, 2);
1187
11.6k
        dav1d_get_bits(&gb, 3); // reserved
1188
11.6k
    }
1189
1190
116k
    if (has_length_field) {
1191
101k
        const size_t len = dav1d_get_uleb128(&gb);
1192
101k
        if (len > (size_t)(gb.ptr_end - gb.ptr)) goto error;
1193
100k
        gb.ptr_end = gb.ptr + len;
1194
100k
    }
1195
116k
    if (gb.error) goto error;
1196
1197
    // We must have read a whole number of bytes at this point (1 byte
1198
    // for the header and whole bytes at a time when reading the
1199
    // leb128 length field).
1200
116k
    assert(gb.bits_left == 0);
1201
1202
    // skip obu not belonging to the selected temporal/spatial layer
1203
116k
    if (type != DAV1D_OBU_SEQ_HDR && type != DAV1D_OBU_TD &&
1204
88.3k
        has_extension && c->operating_point_idc != 0)
1205
2.89k
    {
1206
2.89k
        const int in_temporal_layer = (c->operating_point_idc >> temporal_id) & 1;
1207
2.89k
        const int in_spatial_layer = (c->operating_point_idc >> (spatial_id + 8)) & 1;
1208
2.89k
        if (!in_temporal_layer || !in_spatial_layer)
1209
1.95k
            return gb.ptr_end - gb.ptr_start;
1210
2.89k
    }
1211
1212
114k
    switch (type) {
1213
17.2k
    case DAV1D_OBU_SEQ_HDR: {
1214
17.2k
        Dav1dRef *ref = dav1d_ref_create_using_pool(c->seq_hdr_pool,
1215
17.2k
                                                    sizeof(Dav1dSequenceHeader));
1216
17.2k
        if (!ref) return DAV1D_ERR(ENOMEM);
1217
17.2k
        Dav1dSequenceHeader *seq_hdr = ref->data;
1218
17.2k
        if ((res = parse_seq_hdr(seq_hdr, &gb, c->strict_std_compliance)) < 0) {
1219
167
            dav1d_log(c, "Error parsing sequence header\n");
1220
167
            dav1d_ref_dec(&ref);
1221
167
            goto error;
1222
167
        }
1223
1224
17.0k
        const int op_idx =
1225
17.0k
            c->operating_point < seq_hdr->num_operating_points ? c->operating_point : 0;
1226
17.0k
        c->operating_point_idc = seq_hdr->operating_points[op_idx].idc;
1227
17.0k
        const unsigned spatial_mask = c->operating_point_idc >> 8;
1228
17.0k
        c->max_spatial_id = spatial_mask ? ulog2(spatial_mask) : 0;
1229
1230
        // If we have read a sequence header which is different from
1231
        // the old one, this is a new video sequence and can't use any
1232
        // previous state. Free that state.
1233
1234
17.0k
        if (!c->seq_hdr) {
1235
16.9k
            c->frame_hdr = NULL;
1236
16.9k
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1237
        // see 7.5, operating_parameter_info is allowed to change in
1238
        // sequence headers of a single sequence
1239
16.9k
        } else if (memcmp(seq_hdr, c->seq_hdr, offsetof(Dav1dSequenceHeader, operating_parameter_info))) {
1240
145
            c->frame_hdr = NULL;
1241
145
            c->mastering_display = NULL;
1242
145
            c->content_light = NULL;
1243
145
            dav1d_ref_dec(&c->mastering_display_ref);
1244
145
            dav1d_ref_dec(&c->content_light_ref);
1245
1.30k
            for (int i = 0; i < 8; i++) {
1246
1.16k
                if (c->refs[i].p.p.frame_hdr)
1247
380
                    dav1d_thread_picture_unref(&c->refs[i].p);
1248
1.16k
                dav1d_ref_dec(&c->refs[i].segmap);
1249
1.16k
                dav1d_ref_dec(&c->refs[i].refmvs);
1250
1.16k
                dav1d_cdf_thread_unref(&c->cdf[i]);
1251
1.16k
            }
1252
145
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1253
        // If operating_parameter_info changed, signal it
1254
145
        } else if (memcmp(seq_hdr->operating_parameter_info, c->seq_hdr->operating_parameter_info,
1255
11
                          sizeof(seq_hdr->operating_parameter_info)))
1256
0
        {
1257
0
            c->frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO;
1258
0
        }
1259
17.0k
        dav1d_ref_dec(&c->seq_hdr_ref);
1260
17.0k
        c->seq_hdr_ref = ref;
1261
17.0k
        c->seq_hdr = seq_hdr;
1262
17.0k
        break;
1263
17.2k
    }
1264
198
    case DAV1D_OBU_REDUNDANT_FRAME_HDR:
1265
198
        if (c->frame_hdr) break;
1266
        // fall-through
1267
79.3k
    case DAV1D_OBU_FRAME:
1268
82.1k
    case DAV1D_OBU_FRAME_HDR:
1269
82.1k
        if (!c->seq_hdr) goto error;
1270
82.0k
        if (!c->frame_hdr_ref) {
1271
79.7k
            c->frame_hdr_ref = dav1d_ref_create_using_pool(c->frame_hdr_pool,
1272
79.7k
                                                           sizeof(Dav1dFrameHeader));
1273
79.7k
            if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM);
1274
79.7k
        }
1275
#ifndef NDEBUG
1276
        // ensure that the reference is writable
1277
        assert(dav1d_ref_is_writable(c->frame_hdr_ref));
1278
#endif
1279
82.0k
        c->frame_hdr = c->frame_hdr_ref->data;
1280
82.0k
        memset(c->frame_hdr, 0, sizeof(*c->frame_hdr));
1281
82.0k
        c->frame_hdr->temporal_id = temporal_id;
1282
82.0k
        c->frame_hdr->spatial_id = spatial_id;
1283
82.0k
        if ((res = parse_frame_hdr(c, &gb)) < 0) {
1284
284
            c->frame_hdr = NULL;
1285
284
            goto error;
1286
284
        }
1287
81.7k
        for (int n = 0; n < c->n_tile_data; n++)
1288
6
            dav1d_data_unref_internal(&c->tile[n].data);
1289
81.7k
        c->n_tile_data = 0;
1290
81.7k
        c->n_tiles = 0;
1291
81.7k
        if (type != DAV1D_OBU_FRAME) {
1292
            // This is actually a frame header OBU so read the
1293
            // trailing bit and check for overrun.
1294
2.83k
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1295
63
                c->frame_hdr = NULL;
1296
63
                goto error;
1297
63
            }
1298
2.83k
        }
1299
1300
81.7k
        if (c->frame_size_limit && (int64_t)c->frame_hdr->width[1] *
1301
81.7k
            c->frame_hdr->height > c->frame_size_limit)
1302
55
        {
1303
55
            dav1d_log(c, "Frame size %dx%d exceeds limit %u\n", c->frame_hdr->width[1],
1304
55
                      c->frame_hdr->height, c->frame_size_limit);
1305
55
            c->frame_hdr = NULL;
1306
55
            return DAV1D_ERR(ERANGE);
1307
55
        }
1308
1309
81.6k
        if (type != DAV1D_OBU_FRAME)
1310
2.76k
            break;
1311
        // OBU_FRAMEs shouldn't be signaled with show_existing_frame
1312
78.9k
        if (c->frame_hdr->show_existing_frame) {
1313
66
            c->frame_hdr = NULL;
1314
66
            goto error;
1315
66
        }
1316
1317
        // This is the frame header at the start of a frame OBU.
1318
        // There's no trailing bit at the end to skip, but we do need
1319
        // to align to the next byte.
1320
78.8k
        dav1d_bytealign_get_bits(&gb);
1321
        // fall-through
1322
79.0k
    case DAV1D_OBU_TILE_GRP: {
1323
79.0k
        if (!c->frame_hdr) goto error;
1324
78.9k
        if (c->n_tile_data_alloc < c->n_tile_data + 1) {
1325
16.3k
            if ((c->n_tile_data + 1) > INT_MAX / (int)sizeof(*c->tile)) goto error;
1326
16.3k
            struct Dav1dTileGroup *tile = dav1d_realloc(ALLOC_TILE, c->tile,
1327
16.3k
                                                        (c->n_tile_data + 1) * sizeof(*c->tile));
1328
16.3k
            if (!tile) goto error;
1329
16.3k
            c->tile = tile;
1330
16.3k
            memset(c->tile + c->n_tile_data, 0, sizeof(*c->tile));
1331
16.3k
            c->n_tile_data_alloc = c->n_tile_data + 1;
1332
16.3k
        }
1333
78.9k
        parse_tile_hdr(c, &gb);
1334
        // Align to the next byte boundary and check for overrun.
1335
78.9k
        dav1d_bytealign_get_bits(&gb);
1336
78.9k
        if (gb.error) goto error;
1337
1338
78.7k
        dav1d_data_ref(&c->tile[c->n_tile_data].data, in);
1339
78.7k
        c->tile[c->n_tile_data].data.data = gb.ptr;
1340
78.7k
        c->tile[c->n_tile_data].data.sz = (size_t)(gb.ptr_end - gb.ptr);
1341
        // ensure tile groups are in order and sane, see 6.10.1
1342
78.7k
        if (c->tile[c->n_tile_data].start > c->tile[c->n_tile_data].end ||
1343
78.7k
            c->tile[c->n_tile_data].start != c->n_tiles)
1344
45
        {
1345
93
            for (int i = 0; i <= c->n_tile_data; i++)
1346
48
                dav1d_data_unref_internal(&c->tile[i].data);
1347
45
            c->n_tile_data = 0;
1348
45
            c->n_tiles = 0;
1349
45
            goto error;
1350
45
        }
1351
78.7k
        c->n_tiles += 1 + c->tile[c->n_tile_data].end -
1352
78.7k
                          c->tile[c->n_tile_data].start;
1353
78.7k
        c->n_tile_data++;
1354
78.7k
        break;
1355
78.7k
    }
1356
340
    case DAV1D_OBU_METADATA: {
1357
340
#define DEBUG_OBU_METADATA 0
1358
#if DEBUG_OBU_METADATA
1359
        const uint8_t *const init_ptr = gb.ptr;
1360
#endif
1361
        // obu metadta type field
1362
340
        const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb);
1363
340
        if (gb.error) goto error;
1364
1365
317
        switch (meta_type) {
1366
35
        case OBU_META_HDR_CLL: {
1367
35
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1368
35
                                             sizeof(Dav1dContentLightLevel));
1369
35
            if (!ref) return DAV1D_ERR(ENOMEM);
1370
35
            Dav1dContentLightLevel *const content_light = ref->data;
1371
1372
35
            content_light->max_content_light_level = dav1d_get_bits(&gb, 16);
1373
#if DEBUG_OBU_METADATA
1374
            printf("CLLOBU: max-content-light-level: %d [off=%td]\n",
1375
                   content_light->max_content_light_level,
1376
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1377
#endif
1378
35
            content_light->max_frame_average_light_level = dav1d_get_bits(&gb, 16);
1379
#if DEBUG_OBU_METADATA
1380
            printf("CLLOBU: max-frame-average-light-level: %d [off=%td]\n",
1381
                   content_light->max_frame_average_light_level,
1382
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1383
#endif
1384
1385
35
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1386
3
                dav1d_ref_dec(&ref);
1387
3
                goto error;
1388
3
            }
1389
1390
32
            dav1d_ref_dec(&c->content_light_ref);
1391
32
            c->content_light = content_light;
1392
32
            c->content_light_ref = ref;
1393
32
            break;
1394
35
        }
1395
39
        case OBU_META_HDR_MDCV: {
1396
39
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1397
39
                                             sizeof(Dav1dMasteringDisplay));
1398
39
            if (!ref) return DAV1D_ERR(ENOMEM);
1399
39
            Dav1dMasteringDisplay *const mastering_display = ref->data;
1400
1401
156
            for (int i = 0; i < 3; i++) {
1402
117
                mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16);
1403
117
                mastering_display->primaries[i][1] = dav1d_get_bits(&gb, 16);
1404
#if DEBUG_OBU_METADATA
1405
                printf("MDCVOBU: primaries[%d]: (%d, %d) [off=%td]\n", i,
1406
                       mastering_display->primaries[i][0],
1407
                       mastering_display->primaries[i][1],
1408
                       (gb.ptr - init_ptr) * 8 - gb.bits_left);
1409
#endif
1410
117
            }
1411
39
            mastering_display->white_point[0] = dav1d_get_bits(&gb, 16);
1412
#if DEBUG_OBU_METADATA
1413
            printf("MDCVOBU: white-point-x: %d [off=%td]\n",
1414
                   mastering_display->white_point[0],
1415
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1416
#endif
1417
39
            mastering_display->white_point[1] = dav1d_get_bits(&gb, 16);
1418
#if DEBUG_OBU_METADATA
1419
            printf("MDCVOBU: white-point-y: %d [off=%td]\n",
1420
                   mastering_display->white_point[1],
1421
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1422
#endif
1423
39
            mastering_display->max_luminance = dav1d_get_bits(&gb, 32);
1424
#if DEBUG_OBU_METADATA
1425
            printf("MDCVOBU: max-luminance: %d [off=%td]\n",
1426
                   mastering_display->max_luminance,
1427
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1428
#endif
1429
39
            mastering_display->min_luminance = dav1d_get_bits(&gb, 32);
1430
#if DEBUG_OBU_METADATA
1431
            printf("MDCVOBU: min-luminance: %d [off=%td]\n",
1432
                   mastering_display->min_luminance,
1433
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1434
#endif
1435
39
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1436
6
                dav1d_ref_dec(&ref);
1437
6
                goto error;
1438
6
            }
1439
1440
33
            dav1d_ref_dec(&c->mastering_display_ref);
1441
33
            c->mastering_display = mastering_display;
1442
33
            c->mastering_display_ref = ref;
1443
33
            break;
1444
39
        }
1445
19
        case OBU_META_ITUT_T35: {
1446
19
            ptrdiff_t payload_size = gb.ptr_end - gb.ptr;
1447
            // Don't take into account all the trailing bits for payload_size
1448
517
            while (payload_size > 0 && !gb.ptr[payload_size - 1])
1449
498
                payload_size--; // trailing_zero_bit x 8
1450
19
            payload_size--; // trailing_one_bit + trailing_zero_bit x 7
1451
1452
19
            int country_code_extension_byte = 0;
1453
19
            const int country_code = dav1d_get_bits(&gb, 8);
1454
19
            payload_size--;
1455
19
            if (country_code == 0xFF) {
1456
9
                country_code_extension_byte = dav1d_get_bits(&gb, 8);
1457
9
                payload_size--;
1458
9
            }
1459
1460
19
            if (payload_size <= 0 || gb.ptr[payload_size] != 0x80) {
1461
15
                dav1d_log(c, "Malformed ITU-T T.35 metadata message format\n");
1462
15
                break;
1463
15
            }
1464
1465
4
            if ((c->n_itut_t35 + 1) > INT_MAX / (int)sizeof(*c->itut_t35)) goto error;
1466
4
            struct Dav1dITUTT35 *itut_t35 = dav1d_realloc(ALLOC_OBU_META, c->itut_t35,
1467
4
                                                          (c->n_itut_t35 + 1) * sizeof(*c->itut_t35));
1468
4
            if (!itut_t35) goto error;
1469
4
            c->itut_t35 = itut_t35;
1470
4
            memset(c->itut_t35 + c->n_itut_t35, 0, sizeof(*c->itut_t35));
1471
1472
4
            struct itut_t35_ctx_context *itut_t35_ctx;
1473
4
            if (!c->n_itut_t35) {
1474
4
                assert(!c->itut_t35_ref);
1475
4
                itut_t35_ctx = dav1d_malloc(ALLOC_OBU_META, sizeof(struct itut_t35_ctx_context));
1476
4
                if (!itut_t35_ctx) goto error;
1477
4
                c->itut_t35_ref = dav1d_ref_init(&itut_t35_ctx->ref, c->itut_t35,
1478
4
                                                 dav1d_picture_free_itut_t35, itut_t35_ctx, 0);
1479
4
            } else {
1480
0
                assert(c->itut_t35_ref && atomic_load(&c->itut_t35_ref->ref_cnt) == 1);
1481
0
                itut_t35_ctx = c->itut_t35_ref->user_data;
1482
0
                c->itut_t35_ref->const_data = (uint8_t *)c->itut_t35;
1483
0
            }
1484
4
            itut_t35_ctx->itut_t35 = c->itut_t35;
1485
4
            itut_t35_ctx->n_itut_t35 = c->n_itut_t35 + 1;
1486
1487
4
            Dav1dITUTT35 *const itut_t35_metadata = &c->itut_t35[c->n_itut_t35];
1488
4
            itut_t35_metadata->payload = dav1d_malloc(ALLOC_OBU_META, payload_size);
1489
4
            if (!itut_t35_metadata->payload) goto error;
1490
1491
4
            itut_t35_metadata->country_code = country_code;
1492
4
            itut_t35_metadata->country_code_extension_byte = country_code_extension_byte;
1493
4
            itut_t35_metadata->payload_size = payload_size;
1494
1495
            // We know that we've read a whole number of bytes and that the
1496
            // payload is within the OBU boundaries, so just use memcpy()
1497
4
            assert(gb.bits_left == 0);
1498
4
            memcpy(itut_t35_metadata->payload, gb.ptr, payload_size);
1499
1500
4
            c->n_itut_t35++;
1501
4
            break;
1502
4
        }
1503
10
        case OBU_META_SCALABILITY:
1504
20
        case OBU_META_TIMECODE:
1505
            // ignore metadata OBUs we don't care about
1506
20
            break;
1507
204
        default:
1508
            // print a warning but don't fail for unknown types
1509
204
            if (meta_type > 31) // Types 6 to 31 are "Unregistered user private", so ignore them.
1510
180
                dav1d_log(c, "Unknown Metadata OBU type %d\n", meta_type);
1511
204
            break;
1512
317
        }
1513
1514
308
        break;
1515
317
    }
1516
10.5k
    case DAV1D_OBU_TD:
1517
10.5k
        c->frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT;
1518
10.5k
        break;
1519
358
    case DAV1D_OBU_PADDING:
1520
        // ignore OBUs we don't care about
1521
358
        break;
1522
3.39k
    default:
1523
        // print a warning but don't fail for unknown types
1524
3.39k
        dav1d_log(c, "Unknown OBU type %d of size %td\n", type, gb.ptr_end - gb.ptr);
1525
3.39k
        break;
1526
114k
    }
1527
1528
113k
    if (c->seq_hdr && c->frame_hdr) {
1529
82.1k
        if (c->frame_hdr->show_existing_frame) {
1530
92
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr) goto error;
1531
80
            switch (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type) {
1532
28
            case DAV1D_FRAME_TYPE_INTER:
1533
37
            case DAV1D_FRAME_TYPE_SWITCH:
1534
37
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE)
1535
0
                    goto skip;
1536
37
                break;
1537
37
            case DAV1D_FRAME_TYPE_INTRA:
1538
4
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA)
1539
0
                    goto skip;
1540
                // fall-through
1541
43
            default:
1542
43
                break;
1543
80
            }
1544
80
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.data[0]) goto error;
1545
80
            if (c->strict_std_compliance &&
1546
0
                !c->refs[c->frame_hdr->existing_frame_idx].p.showable)
1547
0
            {
1548
0
                goto error;
1549
0
            }
1550
80
            if (c->n_fc == 1) {
1551
0
                dav1d_thread_picture_ref(&c->out,
1552
0
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1553
0
                dav1d_picture_copy_props(&c->out.p,
1554
0
                                         c->content_light, c->content_light_ref,
1555
0
                                         c->mastering_display, c->mastering_display_ref,
1556
0
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1557
0
                                         &in->m);
1558
                // Must be removed from the context after being attached to the frame
1559
0
                dav1d_ref_dec(&c->itut_t35_ref);
1560
0
                c->itut_t35 = NULL;
1561
0
                c->n_itut_t35 = 0;
1562
0
                c->event_flags |= dav1d_picture_get_event_flags(&c->refs[c->frame_hdr->existing_frame_idx].p);
1563
80
            } else {
1564
80
                pthread_mutex_lock(&c->task_thread.lock);
1565
                // need to append this to the frame output queue
1566
80
                const unsigned next = c->frame_thread.next++;
1567
80
                if (c->frame_thread.next == c->n_fc)
1568
14
                    c->frame_thread.next = 0;
1569
1570
80
                Dav1dFrameContext *const f = &c->fc[next];
1571
95
                while (f->n_tile_data > 0)
1572
15
                    pthread_cond_wait(&f->task_thread.cond,
1573
15
                                      &f->task_thread.ttd->lock);
1574
80
                Dav1dThreadPicture *const out_delayed =
1575
80
                    &c->frame_thread.out_delayed[next];
1576
80
                if (out_delayed->p.data[0] || atomic_load(&f->task_thread.error)) {
1577
53
                    unsigned first = atomic_load(&c->task_thread.first);
1578
53
                    if (first + 1U < c->n_fc)
1579
53
                        atomic_fetch_add(&c->task_thread.first, 1U);
1580
12
                    else
1581
53
                        atomic_store(&c->task_thread.first, 0);
1582
53
                    atomic_compare_exchange_strong(&c->task_thread.reset_task_cur,
1583
53
                                                   &first, UINT_MAX);
1584
53
                    if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
1585
22
                        c->task_thread.cur--;
1586
53
                }
1587
80
                const int error = f->task_thread.retval;
1588
80
                if (error) {
1589
0
                    c->cached_error = error;
1590
0
                    f->task_thread.retval = 0;
1591
0
                    dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
1592
0
                    dav1d_thread_picture_unref(out_delayed);
1593
80
                } else if (out_delayed->p.data[0]) {
1594
53
                    const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
1595
53
                                                                   memory_order_relaxed);
1596
53
                    if ((out_delayed->visible || c->output_invisible_frames) &&
1597
52
                        progress != FRAME_ERROR)
1598
52
                    {
1599
52
                        dav1d_thread_picture_ref(&c->out, out_delayed);
1600
52
                        c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
1601
52
                    }
1602
53
                    dav1d_thread_picture_unref(out_delayed);
1603
53
                }
1604
80
                dav1d_thread_picture_ref(out_delayed,
1605
80
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1606
80
                out_delayed->visible = 1;
1607
80
                dav1d_picture_copy_props(&out_delayed->p,
1608
80
                                         c->content_light, c->content_light_ref,
1609
80
                                         c->mastering_display, c->mastering_display_ref,
1610
80
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1611
80
                                         &in->m);
1612
                // Must be removed from the context after being attached to the frame
1613
80
                dav1d_ref_dec(&c->itut_t35_ref);
1614
80
                c->itut_t35 = NULL;
1615
80
                c->n_itut_t35 = 0;
1616
1617
80
                pthread_mutex_unlock(&c->task_thread.lock);
1618
80
            }
1619
80
            if (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY) {
1620
39
                const int r = c->frame_hdr->existing_frame_idx;
1621
39
                c->refs[r].p.showable = 0;
1622
351
                for (int i = 0; i < 8; i++) {
1623
312
                    if (i == r) continue;
1624
1625
273
                    if (c->refs[i].p.p.frame_hdr)
1626
248
                        dav1d_thread_picture_unref(&c->refs[i].p);
1627
273
                    dav1d_thread_picture_ref(&c->refs[i].p, &c->refs[r].p);
1628
1629
273
                    dav1d_cdf_thread_unref(&c->cdf[i]);
1630
273
                    dav1d_cdf_thread_ref(&c->cdf[i], &c->cdf[r]);
1631
1632
273
                    dav1d_ref_dec(&c->refs[i].segmap);
1633
273
                    c->refs[i].segmap = c->refs[r].segmap;
1634
273
                    if (c->refs[r].segmap)
1635
70
                        dav1d_ref_inc(c->refs[r].segmap);
1636
273
                    dav1d_ref_dec(&c->refs[i].refmvs);
1637
273
                }
1638
39
            }
1639
80
            c->frame_hdr = NULL;
1640
82.0k
        } else if (c->n_tiles == c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows) {
1641
78.7k
            switch (c->frame_hdr->frame_type) {
1642
60.2k
            case DAV1D_FRAME_TYPE_INTER:
1643
60.5k
            case DAV1D_FRAME_TYPE_SWITCH:
1644
60.5k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE ||
1645
60.5k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1646
0
                     !c->frame_hdr->refresh_frame_flags))
1647
0
                    goto skip;
1648
60.5k
                break;
1649
60.5k
            case DAV1D_FRAME_TYPE_INTRA:
1650
393
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA ||
1651
393
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1652
0
                     !c->frame_hdr->refresh_frame_flags))
1653
0
                    goto skip;
1654
                // fall-through
1655
18.1k
            default:
1656
18.1k
                break;
1657
78.7k
            }
1658
78.7k
            if (!c->n_tile_data)
1659
0
                goto error;
1660
78.7k
            if ((res = dav1d_submit_frame(c)) < 0)
1661
161
                return res;
1662
78.5k
            assert(!c->n_tile_data);
1663
78.5k
            c->frame_hdr = NULL;
1664
78.5k
            c->n_tiles = 0;
1665
78.5k
        }
1666
82.1k
    }
1667
1668
113k
    return gb.ptr_end - gb.ptr_start;
1669
1670
0
skip:
1671
    // update refs with only the headers in case we skip the frame
1672
0
    for (int i = 0; i < 8; i++) {
1673
0
        if (c->frame_hdr->refresh_frame_flags & (1 << i)) {
1674
0
            dav1d_thread_picture_unref(&c->refs[i].p);
1675
0
            c->refs[i].p.p.frame_hdr = c->frame_hdr;
1676
0
            c->refs[i].p.p.seq_hdr = c->seq_hdr;
1677
0
            c->refs[i].p.p.frame_hdr_ref = c->frame_hdr_ref;
1678
0
            c->refs[i].p.p.seq_hdr_ref = c->seq_hdr_ref;
1679
0
            dav1d_ref_inc(c->frame_hdr_ref);
1680
0
            dav1d_ref_inc(c->seq_hdr_ref);
1681
0
        }
1682
0
    }
1683
1684
0
    dav1d_ref_dec(&c->frame_hdr_ref);
1685
0
    c->frame_hdr = NULL;
1686
0
    c->n_tiles = 0;
1687
1688
0
    return gb.ptr_end - gb.ptr_start;
1689
1690
1.52k
error:
1691
1.52k
    dav1d_data_props_copy(&c->cached_error_props, &in->m);
1692
1.52k
    dav1d_log(c, gb.error ? "Overrun in OBU bit buffer\n" :
1693
1.52k
                            "Error parsing OBU data\n");
1694
1.52k
    return DAV1D_ERR(EINVAL);
1695
113k
}