Coverage Report

Created: 2026-10-28 06:13

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
16.1k
{
51
16.1k
    const int trailing_one_bit = dav1d_get_bit(gb);
52
53
16.1k
    if (gb->error)
54
32
        return DAV1D_ERR(EINVAL);
55
56
16.1k
    if (!strict_std_compliance)
57
16.1k
        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
9.03k
{
76
9.03k
#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
9.03k
    memset(hdr, 0, sizeof(*hdr));
83
9.03k
    hdr->profile = dav1d_get_bits(gb, 3);
84
9.03k
    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
9.02k
    hdr->still_picture = dav1d_get_bit(gb);
91
9.02k
    hdr->reduced_still_picture_header = dav1d_get_bit(gb);
92
9.02k
    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
9.01k
    if (hdr->reduced_still_picture_header) {
99
2.67k
        hdr->num_operating_points = 1;
100
2.67k
        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);
101
2.67k
        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);
102
2.67k
        hdr->operating_points[0].initial_display_delay = 10;
103
6.34k
    } else {
104
6.34k
        hdr->timing_info_present = dav1d_get_bit(gb);
105
6.34k
        if (hdr->timing_info_present) {
106
4.61k
            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);
107
4.61k
            hdr->time_scale = dav1d_get_bits(gb, 32);
108
4.61k
            if (strict_std_compliance && (!hdr->num_units_in_tick || !hdr->time_scale))
109
0
                goto error;
110
4.61k
            hdr->equal_picture_interval = dav1d_get_bit(gb);
111
4.61k
            if (hdr->equal_picture_interval) {
112
1.31k
                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
113
1.31k
                if (num_ticks_per_picture == UINT32_MAX)
114
0
                    goto error;
115
1.31k
                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
116
1.31k
            }
117
118
4.61k
            hdr->decoder_model_info_present = dav1d_get_bit(gb);
119
4.61k
            if (hdr->decoder_model_info_present) {
120
1.07k
                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;
121
1.07k
                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);
122
1.07k
                if (strict_std_compliance && !hdr->num_units_in_decoding_tick)
123
0
                    goto error;
124
1.07k
                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;
125
1.07k
                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;
126
1.07k
            }
127
4.61k
        }
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.34k
        hdr->display_model_info_present = dav1d_get_bit(gb);
134
6.34k
        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;
135
16.9k
        for (int i = 0; i < hdr->num_operating_points; i++) {
136
10.6k
            struct Dav1dSequenceHeaderOperatingPoint *const op =
137
10.6k
                &hdr->operating_points[i];
138
10.6k
            op->idc = dav1d_get_bits(gb, 12);
139
10.6k
            if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00)))
140
15
                goto error;
141
10.6k
            op->major_level = 2 + dav1d_get_bits(gb, 3);
142
10.6k
            op->minor_level = dav1d_get_bits(gb, 2);
143
10.6k
            if (op->major_level > 3)
144
4.16k
                op->tier = dav1d_get_bit(gb);
145
10.6k
            if (hdr->decoder_model_info_present) {
146
3.67k
                op->decoder_model_param_present = dav1d_get_bit(gb);
147
3.67k
                if (op->decoder_model_param_present) {
148
1.37k
                    struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
149
1.37k
                        &hdr->operating_parameter_info[i];
150
1.37k
                    opi->decoder_buffer_delay =
151
1.37k
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
152
1.37k
                    opi->encoder_buffer_delay =
153
1.37k
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
154
1.37k
                    opi->low_delay_mode = dav1d_get_bit(gb);
155
1.37k
                }
156
3.67k
            }
157
10.6k
            if (hdr->display_model_info_present)
158
1.71k
                op->display_model_param_present = dav1d_get_bit(gb);
159
10.6k
            op->initial_display_delay =
160
10.6k
                op->display_model_param_present ? dav1d_get_bits(gb, 4) + 1 : 10;
161
10.6k
        }
162
#if DEBUG_SEQ_HDR
163
        printf("SEQHDR: post-operating-points: off=%u\n",
164
               dav1d_get_bits_pos(gb) - init_bit_pos);
165
#endif
166
6.34k
    }
167
168
9.00k
    hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;
169
9.00k
    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;
170
9.00k
    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;
171
9.00k
    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
9.00k
    if (!hdr->reduced_still_picture_header) {
177
6.33k
        hdr->frame_id_numbers_present = dav1d_get_bit(gb);
178
6.33k
        if (hdr->frame_id_numbers_present) {
179
841
            hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;
180
841
            hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;
181
841
        }
182
6.33k
    }
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
9.00k
    hdr->sb128 = dav1d_get_bit(gb);
189
9.00k
    hdr->filter_intra = dav1d_get_bit(gb);
190
9.00k
    hdr->intra_edge_filter = dav1d_get_bit(gb);
191
9.00k
    if (hdr->reduced_still_picture_header) {
192
2.67k
        hdr->screen_content_tools = DAV1D_ADAPTIVE;
193
2.67k
        hdr->force_integer_mv = DAV1D_ADAPTIVE;
194
6.33k
    } else {
195
6.33k
        hdr->inter_intra = dav1d_get_bit(gb);
196
6.33k
        hdr->masked_compound = dav1d_get_bit(gb);
197
6.33k
        hdr->warped_motion = dav1d_get_bit(gb);
198
6.33k
        hdr->dual_filter = dav1d_get_bit(gb);
199
6.33k
        hdr->order_hint = dav1d_get_bit(gb);
200
6.33k
        if (hdr->order_hint) {
201
2.96k
            hdr->jnt_comp = dav1d_get_bit(gb);
202
2.96k
            hdr->ref_frame_mvs = dav1d_get_bit(gb);
203
2.96k
        }
204
6.33k
        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.33k
        hdr->force_integer_mv = hdr->screen_content_tools ?
210
4.32k
                                dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb) : 2;
211
6.33k
        if (hdr->order_hint)
212
2.96k
            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;
213
6.33k
    }
214
9.00k
    hdr->super_res = dav1d_get_bit(gb);
215
9.00k
    hdr->cdef = dav1d_get_bit(gb);
216
9.00k
    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
9.00k
    hdr->hbd = dav1d_get_bit(gb);
223
9.00k
    if (hdr->profile == 2 && hdr->hbd)
224
2.06k
        hdr->hbd += dav1d_get_bit(gb);
225
9.00k
    if (hdr->profile != 1)
226
6.20k
        hdr->monochrome = dav1d_get_bit(gb);
227
9.00k
    hdr->color_description_present = dav1d_get_bit(gb);
228
9.00k
    if (hdr->color_description_present) {
229
3.80k
        hdr->pri = dav1d_get_bits(gb, 8);
230
3.80k
        hdr->trc = dav1d_get_bits(gb, 8);
231
3.80k
        hdr->mtrx = dav1d_get_bits(gb, 8);
232
5.19k
    } else {
233
5.19k
        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;
234
5.19k
        hdr->trc = DAV1D_TRC_UNKNOWN;
235
5.19k
        hdr->mtrx = DAV1D_MC_UNKNOWN;
236
5.19k
    }
237
9.00k
    if (hdr->monochrome) {
238
2.17k
        hdr->color_range = dav1d_get_bit(gb);
239
2.17k
        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;
240
2.17k
        hdr->ss_hor = hdr->ss_ver = 1;
241
2.17k
        hdr->chr = DAV1D_CHR_UNKNOWN;
242
6.83k
    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&
243
47
               hdr->trc == DAV1D_TRC_SRGB &&
244
37
               hdr->mtrx == DAV1D_MC_IDENTITY)
245
3
    {
246
3
        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
247
3
        hdr->color_range = 1;
248
3
        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))
249
2
            goto error;
250
6.82k
    } else {
251
6.82k
        hdr->color_range = dav1d_get_bit(gb);
252
6.82k
        switch (hdr->profile) {
253
2.38k
        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;
254
2.38k
                hdr->ss_hor = hdr->ss_ver = 1;
255
2.38k
                break;
256
2.79k
        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
257
2.79k
                break;
258
1.64k
        case 2:
259
1.64k
            if (hdr->hbd == 2) {
260
1.02k
                hdr->ss_hor = dav1d_get_bit(gb);
261
1.02k
                if (hdr->ss_hor)
262
459
                    hdr->ss_ver = dav1d_get_bit(gb);
263
1.02k
            } else
264
621
                hdr->ss_hor = 1;
265
1.64k
            hdr->layout = hdr->ss_hor ?
266
1.08k
                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :
267
1.08k
                                        DAV1D_PIXEL_LAYOUT_I422 :
268
1.64k
                                        DAV1D_PIXEL_LAYOUT_I444;
269
1.64k
            break;
270
6.82k
        }
271
6.82k
        hdr->chr = (hdr->ss_hor & hdr->ss_ver) ?
272
4.00k
                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;
273
6.82k
    }
274
8.99k
    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
8.99k
    if (!hdr->monochrome)
280
6.82k
        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
8.99k
    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
8.99k
    return check_trailing_bits(gb, strict_std_compliance);
297
298
36
error:
299
36
    return DAV1D_ERR(EINVAL);
300
8.99k
}
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
37.3k
{
344
37.3k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
345
37.3k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
346
347
37.3k
    if (use_ref) {
348
11.4k
        for (int i = 0; i < 7; i++) {
349
11.3k
            if (dav1d_get_bit(gb)) {
350
4.88k
                const Dav1dThreadPicture *const ref =
351
4.88k
                    &c->refs[c->frame_hdr->refidx[i]].p;
352
4.88k
                if (!ref->p.frame_hdr) return -1;
353
4.88k
                hdr->width[1] = ref->p.frame_hdr->width[1];
354
4.88k
                hdr->height = ref->p.frame_hdr->height;
355
4.88k
                hdr->render_width = ref->p.frame_hdr->render_width;
356
4.88k
                hdr->render_height = ref->p.frame_hdr->render_height;
357
4.88k
                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
358
4.88k
                if (hdr->super_res.enabled) {
359
3.20k
                    const int d = hdr->super_res.width_scale_denominator =
360
3.20k
                        9 + dav1d_get_bits(gb, 3);
361
3.20k
                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,
362
3.20k
                                         imin(16, hdr->width[1]));
363
3.20k
                } else {
364
1.67k
                    hdr->super_res.width_scale_denominator = 8;
365
1.67k
                    hdr->width[0] = hdr->width[1];
366
1.67k
                }
367
4.88k
                return 0;
368
4.88k
            }
369
11.3k
        }
370
4.96k
    }
371
372
32.5k
    if (hdr->frame_size_override) {
373
3.69k
        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;
374
3.69k
        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;
375
28.8k
    } else {
376
28.8k
        hdr->width[1] = seqhdr->max_width;
377
28.8k
        hdr->height = seqhdr->max_height;
378
28.8k
    }
379
32.5k
    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
380
32.5k
    if (hdr->super_res.enabled) {
381
7.62k
        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);
382
7.62k
        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));
383
24.8k
    } else {
384
24.8k
        hdr->super_res.width_scale_denominator = 8;
385
24.8k
        hdr->width[0] = hdr->width[1];
386
24.8k
    }
387
32.5k
    hdr->have_render_size = dav1d_get_bit(gb);
388
32.5k
    if (hdr->have_render_size) {
389
8.60k
        hdr->render_width = dav1d_get_bits(gb, 16) + 1;
390
8.60k
        hdr->render_height = dav1d_get_bits(gb, 16) + 1;
391
23.8k
    } else {
392
23.8k
        hdr->render_width = hdr->width[1];
393
23.8k
        hdr->render_height = hdr->height;
394
23.8k
    }
395
32.5k
    return 0;
396
37.3k
}
397
398
191k
static inline int tile_log2(const int sz, const int tgt) {
399
191k
    int k;
400
235k
    for (k = 0; (sz << k) < tgt; k++) ;
401
191k
    return k;
402
191k
}
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
38.2k
static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {
410
38.2k
#define DEBUG_FRAME_HDR 0
411
412
#if DEBUG_FRAME_HDR
413
    const uint8_t *const init_ptr = gb->ptr;
414
#endif
415
38.2k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
416
38.2k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
417
418
38.2k
    if (!seqhdr->reduced_still_picture_header)
419
30.3k
        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
38.2k
    if (hdr->show_existing_frame) {
425
823
        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);
426
823
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
427
173
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
428
823
        if (seqhdr->frame_id_numbers_present) {
429
6
            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
430
6
            Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr;
431
6
            if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) goto error;
432
6
        }
433
820
        return 0;
434
823
    }
435
436
37.4k
    if (seqhdr->reduced_still_picture_header) {
437
7.89k
        hdr->frame_type = DAV1D_FRAME_TYPE_KEY;
438
7.89k
        hdr->show_frame = 1;
439
29.5k
    } else {
440
29.5k
        hdr->frame_type = dav1d_get_bits(gb, 2);
441
29.5k
        hdr->show_frame = dav1d_get_bit(gb);
442
29.5k
    }
443
37.4k
    if (hdr->show_frame) {
444
33.6k
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
445
2.50k
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
446
33.6k
        hdr->showable_frame = hdr->frame_type != DAV1D_FRAME_TYPE_KEY;
447
33.6k
    } else
448
3.71k
        hdr->showable_frame = dav1d_get_bit(gb);
449
37.4k
    hdr->error_resilient_mode =
450
37.4k
        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||
451
23.0k
        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||
452
22.1k
        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
37.4k
    hdr->disable_cdf_update = dav1d_get_bit(gb);
458
37.4k
    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?
459
18.8k
                                      dav1d_get_bit(gb) : seqhdr->screen_content_tools;
460
37.4k
    if (hdr->allow_screen_content_tools)
461
11.6k
        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?
462
8.62k
                                dav1d_get_bit(gb) : seqhdr->force_integer_mv;
463
464
37.4k
    if (IS_KEY_OR_INTRA(hdr))
465
17.5k
        hdr->force_integer_mv = 1;
466
467
37.4k
    if (seqhdr->frame_id_numbers_present)
468
1.07k
        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
469
470
37.4k
    if (!seqhdr->reduced_still_picture_header)
471
29.5k
        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
37.4k
    if (seqhdr->order_hint)
477
16.6k
        hdr->frame_offset = dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
478
37.4k
    hdr->primary_ref_frame = !hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) ?
479
19.5k
                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;
480
481
37.4k
    if (seqhdr->decoder_model_info_present) {
482
8.58k
        hdr->buffer_removal_time_present = dav1d_get_bit(gb);
483
8.58k
        if (hdr->buffer_removal_time_present) {
484
9.43k
            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {
485
6.74k
                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];
486
6.74k
                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];
487
6.74k
                if (seqop->decoder_model_param_present) {
488
2.48k
                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;
489
2.48k
                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;
490
2.48k
                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))
491
1.62k
                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);
492
2.48k
                }
493
6.74k
            }
494
2.69k
        }
495
8.58k
    }
496
497
37.4k
    if (IS_KEY_OR_INTRA(hdr)) {
498
17.5k
        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&
499
16.7k
                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);
500
17.5k
        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
501
882
            for (int i = 0; i < 8; i++)
502
784
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
503
17.5k
        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
17.5k
        if (read_frame_size(c, gb, 0) < 0) goto error;
509
17.5k
        if (hdr->allow_screen_content_tools && !hdr->super_res.enabled)
510
3.70k
            hdr->allow_intrabc = dav1d_get_bit(gb);
511
19.8k
    } else {
512
19.8k
        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :
513
19.8k
                                   dav1d_get_bits(gb, 8);
514
19.8k
        if (hdr->error_resilient_mode && seqhdr->order_hint)
515
4.59k
            for (int i = 0; i < 8; i++)
516
4.08k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
517
19.8k
        if (seqhdr->order_hint) {
518
11.1k
            hdr->frame_ref_short_signaling = dav1d_get_bit(gb);
519
11.1k
            if (hdr->frame_ref_short_signaling) {
520
6.81k
                hdr->refidx[0] = dav1d_get_bits(gb, 3);
521
6.81k
                hdr->refidx[1] = hdr->refidx[2] = -1;
522
6.81k
                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
6.81k
                int frame_offset_mem[8+1];
527
6.81k
                int *const frame_offset = &frame_offset_mem[1];
528
6.81k
                int earliest_ref = -1;
529
61.2k
                for (int i = 0, earliest_offset = INT_MAX; i < 8; i++) {
530
54.4k
                    const Dav1dFrameHeader *const refhdr = c->refs[i].p.p.frame_hdr;
531
54.4k
                    if (!refhdr) goto error;
532
54.4k
                    const int diff = get_poc_diff(seqhdr->order_hint_n_bits,
533
54.4k
                                                  refhdr->frame_offset,
534
54.4k
                                                  hdr->frame_offset);
535
54.4k
                    frame_offset[i] = diff;
536
54.4k
                    if (diff < earliest_offset) {
537
10.8k
                        earliest_offset = diff;
538
10.8k
                        earliest_ref = i;
539
10.8k
                    }
540
54.4k
                }
541
6.80k
                frame_offset[hdr->refidx[0]] = INT_MIN; // = reference frame is used
542
6.80k
                frame_offset[hdr->refidx[3]] = INT_MIN;
543
6.80k
                assert(earliest_ref >= 0);
544
545
6.80k
                int refidx = -1;
546
61.2k
                for (int i = 0, latest_offset = 0; i < 8; i++) {
547
54.4k
                    const int hint = frame_offset[i];
548
54.4k
                    if (hint >= latest_offset) {
549
19.3k
                        latest_offset = hint;
550
19.3k
                        refidx = i;
551
19.3k
                    }
552
54.4k
                }
553
6.80k
                frame_offset[refidx] = INT_MIN;
554
6.80k
                hdr->refidx[6] = refidx;
555
556
20.4k
                for (int i = 4; i < 6; i++) {
557
                    /* Unsigned compares to handle negative values. */
558
13.6k
                    unsigned earliest_offset = UINT8_MAX;
559
13.6k
                    refidx = -1;
560
122k
                    for (int j = 0; j < 8; j++) {
561
108k
                        const unsigned hint = frame_offset[j];
562
108k
                        if (hint < earliest_offset) {
563
10.3k
                            earliest_offset = hint;
564
10.3k
                            refidx = j;
565
10.3k
                        }
566
108k
                    }
567
13.6k
                    frame_offset[refidx] = INT_MIN;
568
13.6k
                    hdr->refidx[i] = refidx;
569
13.6k
                }
570
571
47.6k
                for (int i = 1; i < 7; i++) {
572
40.8k
                    refidx = hdr->refidx[i];
573
40.8k
                    if (refidx < 0) {
574
19.3k
                        unsigned latest_offset = ~UINT8_MAX;
575
173k
                        for (int j = 0; j < 8; j++) {
576
154k
                            const unsigned hint = frame_offset[j];
577
154k
                            if (hint >= latest_offset) {
578
52.4k
                                latest_offset = hint;
579
52.4k
                                refidx = j;
580
52.4k
                            }
581
154k
                        }
582
19.3k
                        frame_offset[refidx] = INT_MIN;
583
19.3k
                        hdr->refidx[i] = refidx >= 0 ? refidx : earliest_ref;
584
19.3k
                    }
585
40.8k
                }
586
6.80k
            }
587
11.1k
        }
588
158k
        for (int i = 0; i < 7; i++) {
589
138k
            if (!hdr->frame_ref_short_signaling)
590
91.2k
                hdr->refidx[i] = dav1d_get_bits(gb, 3);
591
138k
            if (seqhdr->frame_id_numbers_present) {
592
42
                const unsigned delta_ref_frame_id = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits) + 1;
593
42
                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
42
                Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr;
595
42
                if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error;
596
42
            }
597
138k
        }
598
19.8k
        const int use_ref = !hdr->error_resilient_mode &&
599
17.8k
                            hdr->frame_size_override;
600
19.8k
        if (read_frame_size(c, gb, use_ref) < 0) goto error;
601
19.8k
        if (!hdr->force_integer_mv)
602
18.8k
            hdr->hp = dav1d_get_bit(gb);
603
19.8k
        hdr->subpel_filter_mode = dav1d_get_bit(gb) ? DAV1D_FILTER_SWITCHABLE :
604
19.8k
                                                      dav1d_get_bits(gb, 2);
605
19.8k
        hdr->switchable_motion_mode = dav1d_get_bit(gb);
606
19.8k
        if (!hdr->error_resilient_mode && seqhdr->ref_frame_mvs &&
607
5.36k
            seqhdr->order_hint && IS_INTER_OR_SWITCH(hdr))
608
5.36k
        {
609
5.36k
            hdr->use_ref_frame_mvs = dav1d_get_bit(gb);
610
5.36k
        }
611
19.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
37.3k
    if (!seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update)
618
18.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
37.3k
    hdr->tiling.uniform = dav1d_get_bit(gb);
626
37.3k
    const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
627
37.3k
    const int sbsz_log2 = 6 + seqhdr->sb128;
628
37.3k
    const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
629
37.3k
    const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
630
37.3k
    const int max_tile_width_sb = 4096 >> sbsz_log2;
631
37.3k
    const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
632
37.3k
    hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
633
37.3k
    hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
634
37.3k
    hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
635
37.3k
    const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
636
37.3k
                              hdr->tiling.min_log2_cols);
637
37.3k
    if (hdr->tiling.uniform) {
638
16.3k
        for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
639
16.7k
             hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bit(gb);
640
16.3k
             hdr->tiling.log2_cols++) ;
641
16.3k
        const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols);
642
16.3k
        hdr->tiling.cols = 0;
643
36.0k
        for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++)
644
19.7k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
645
16.3k
        hdr->tiling.min_log2_rows =
646
16.3k
            imax(min_log2_tiles - hdr->tiling.log2_cols, 0);
647
648
16.3k
        for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows;
649
16.9k
             hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bit(gb);
650
16.3k
             hdr->tiling.log2_rows++) ;
651
16.3k
        const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows);
652
16.3k
        hdr->tiling.rows = 0;
653
33.9k
        for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++)
654
17.6k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
655
21.0k
    } else {
656
21.0k
        hdr->tiling.cols = 0;
657
21.0k
        int widest_tile = 0, max_tile_area_sb = sbw * sbh;
658
49.0k
        for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) {
659
28.0k
            const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb);
660
28.0k
            const int tile_w = (tile_width_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_width_sb) : 1;
661
28.0k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
662
28.0k
            sbx += tile_w;
663
28.0k
            widest_tile = imax(widest_tile, tile_w);
664
28.0k
        }
665
21.0k
        hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
666
21.0k
        if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
667
21.0k
        const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
668
669
21.0k
        hdr->tiling.rows = 0;
670
44.8k
        for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
671
23.7k
            const int tile_height_sb = imin(sbh - sby, max_tile_height_sb);
672
23.7k
            const int tile_h = (tile_height_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_height_sb) : 1;
673
23.7k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
674
23.7k
            sby += tile_h;
675
23.7k
        }
676
21.0k
        hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows);
677
21.0k
    }
678
37.3k
    hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw;
679
37.3k
    hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh;
680
37.3k
    if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) {
681
3.17k
        hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols + hdr->tiling.log2_rows);
682
3.17k
        if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows)
683
9
            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
37.3k
    hdr->quant.yac = dav1d_get_bits(gb, 8);
693
37.3k
    if (dav1d_get_bit(gb))
694
10.2k
        hdr->quant.ydc_delta = dav1d_get_sbits(gb, 7);
695
37.3k
    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
28.7k
        const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bit(gb) : 0;
700
28.7k
        if (dav1d_get_bit(gb))
701
11.1k
            hdr->quant.udc_delta = dav1d_get_sbits(gb, 7);
702
28.7k
        if (dav1d_get_bit(gb))
703
13.6k
            hdr->quant.uac_delta = dav1d_get_sbits(gb, 7);
704
28.7k
        if (diff_uv_delta) {
705
5.49k
            if (dav1d_get_bit(gb))
706
3.33k
                hdr->quant.vdc_delta = dav1d_get_sbits(gb, 7);
707
5.49k
            if (dav1d_get_bit(gb))
708
3.39k
                hdr->quant.vac_delta = dav1d_get_sbits(gb, 7);
709
23.2k
        } else {
710
23.2k
            hdr->quant.vdc_delta = hdr->quant.udc_delta;
711
23.2k
            hdr->quant.vac_delta = hdr->quant.uac_delta;
712
23.2k
        }
713
28.7k
    }
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
37.3k
    hdr->quant.qm = dav1d_get_bit(gb);
719
37.3k
    if (hdr->quant.qm) {
720
25.5k
        hdr->quant.qm_y = dav1d_get_bits(gb, 4);
721
25.5k
        hdr->quant.qm_u = dav1d_get_bits(gb, 4);
722
25.5k
        hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 4) :
723
25.5k
                                                        hdr->quant.qm_u;
724
25.5k
    }
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
37.3k
    hdr->segmentation.enabled = dav1d_get_bit(gb);
732
37.3k
    if (hdr->segmentation.enabled) {
733
15.0k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
734
8.82k
            hdr->segmentation.update_map = 1;
735
8.82k
            hdr->segmentation.update_data = 1;
736
8.82k
        } else {
737
6.23k
            hdr->segmentation.update_map = dav1d_get_bit(gb);
738
6.23k
            if (hdr->segmentation.update_map)
739
1.65k
                hdr->segmentation.temporal = dav1d_get_bit(gb);
740
6.23k
            hdr->segmentation.update_data = dav1d_get_bit(gb);
741
6.23k
        }
742
743
15.0k
        if (hdr->segmentation.update_data) {
744
9.76k
            hdr->segmentation.seg_data.last_active_segid = -1;
745
87.8k
            for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
746
78.0k
                Dav1dSegmentationData *const seg =
747
78.0k
                    &hdr->segmentation.seg_data.d[i];
748
78.0k
                if (dav1d_get_bit(gb)) {
749
12.9k
                    seg->delta_q = dav1d_get_sbits(gb, 9);
750
12.9k
                    hdr->segmentation.seg_data.last_active_segid = i;
751
12.9k
                }
752
78.0k
                if (dav1d_get_bit(gb)) {
753
11.7k
                    seg->delta_lf_y_v = dav1d_get_sbits(gb, 7);
754
11.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
755
11.7k
                }
756
78.0k
                if (dav1d_get_bit(gb)) {
757
24.4k
                    seg->delta_lf_y_h = dav1d_get_sbits(gb, 7);
758
24.4k
                    hdr->segmentation.seg_data.last_active_segid = i;
759
24.4k
                }
760
78.0k
                if (dav1d_get_bit(gb)) {
761
24.7k
                    seg->delta_lf_u = dav1d_get_sbits(gb, 7);
762
24.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
763
24.7k
                }
764
78.0k
                if (dav1d_get_bit(gb)) {
765
25.1k
                    seg->delta_lf_v = dav1d_get_sbits(gb, 7);
766
25.1k
                    hdr->segmentation.seg_data.last_active_segid = i;
767
25.1k
                }
768
78.0k
                if (dav1d_get_bit(gb)) {
769
24.3k
                    seg->ref = dav1d_get_bits(gb, 3);
770
24.3k
                    hdr->segmentation.seg_data.last_active_segid = i;
771
24.3k
                    hdr->segmentation.seg_data.preskip = 1;
772
53.7k
                } else {
773
53.7k
                    seg->ref = -1;
774
53.7k
                }
775
78.0k
                if ((seg->skip = dav1d_get_bit(gb))) {
776
46.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
777
46.8k
                    hdr->segmentation.seg_data.preskip = 1;
778
46.8k
                }
779
78.0k
                if ((seg->globalmv = dav1d_get_bit(gb))) {
780
39.0k
                    hdr->segmentation.seg_data.last_active_segid = i;
781
39.0k
                    hdr->segmentation.seg_data.preskip = 1;
782
39.0k
                }
783
78.0k
            }
784
9.76k
        } else {
785
            // segmentation.update_data was false so we should copy
786
            // segmentation data from the reference frame.
787
5.30k
            assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
788
5.30k
            const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
789
5.30k
            if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
790
5.29k
            hdr->segmentation.seg_data =
791
5.29k
                c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data;
792
5.29k
        }
793
22.3k
    } else {
794
200k
        for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++)
795
178k
            hdr->segmentation.seg_data.d[i].ref = -1;
796
22.3k
    }
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
37.3k
    if (hdr->quant.yac) {
804
36.2k
        hdr->delta.q.present = dav1d_get_bit(gb);
805
36.2k
        if (hdr->delta.q.present) {
806
8.56k
            hdr->delta.q.res_log2 = dav1d_get_bits(gb, 2);
807
8.56k
            if (!hdr->allow_intrabc) {
808
7.99k
                hdr->delta.lf.present = dav1d_get_bit(gb);
809
7.99k
                if (hdr->delta.lf.present) {
810
3.38k
                    hdr->delta.lf.res_log2 = dav1d_get_bits(gb, 2);
811
3.38k
                    hdr->delta.lf.multi = dav1d_get_bit(gb);
812
3.38k
                }
813
7.99k
            }
814
8.56k
        }
815
36.2k
    }
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
37.3k
    const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta &&
823
21.7k
        !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta;
824
37.3k
    hdr->all_lossless = 1;
825
336k
    for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
826
298k
        hdr->segmentation.qidx[i] = hdr->segmentation.enabled ?
827
120k
            iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) :
828
298k
            hdr->quant.yac;
829
298k
        hdr->segmentation.lossless[i] =
830
298k
            !hdr->segmentation.qidx[i] && delta_lossless;
831
298k
        hdr->all_lossless &= hdr->segmentation.lossless[i];
832
298k
    }
833
834
    // loopfilter
835
37.3k
    if (hdr->all_lossless || hdr->allow_intrabc) {
836
2.89k
        hdr->loopfilter.mode_ref_delta_enabled = 1;
837
2.89k
        hdr->loopfilter.mode_ref_delta_update = 1;
838
2.89k
        hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
839
34.4k
    } else {
840
34.4k
        hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6);
841
34.4k
        hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6);
842
34.4k
        if (!seqhdr->monochrome &&
843
26.3k
            (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1]))
844
24.8k
        {
845
24.8k
            hdr->loopfilter.level_u = dav1d_get_bits(gb, 6);
846
24.8k
            hdr->loopfilter.level_v = dav1d_get_bits(gb, 6);
847
24.8k
        }
848
34.4k
        hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3);
849
850
34.4k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
851
17.1k
            hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
852
17.2k
        } else {
853
17.2k
            const int ref = hdr->refidx[hdr->primary_ref_frame];
854
17.2k
            if (!c->refs[ref].p.p.frame_hdr) goto error;
855
17.2k
            hdr->loopfilter.mode_ref_deltas =
856
17.2k
                c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas;
857
17.2k
        }
858
34.4k
        hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bit(gb);
859
34.4k
        if (hdr->loopfilter.mode_ref_delta_enabled) {
860
7.21k
            hdr->loopfilter.mode_ref_delta_update = dav1d_get_bit(gb);
861
7.21k
            if (hdr->loopfilter.mode_ref_delta_update) {
862
39.1k
                for (int i = 0; i < 8; i++)
863
34.7k
                    if (dav1d_get_bit(gb))
864
14.4k
                        hdr->loopfilter.mode_ref_deltas.ref_delta[i] =
865
14.4k
                            dav1d_get_sbits(gb, 7);
866
13.0k
                for (int i = 0; i < 2; i++)
867
8.69k
                    if (dav1d_get_bit(gb))
868
6.56k
                        hdr->loopfilter.mode_ref_deltas.mode_delta[i] =
869
6.56k
                            dav1d_get_sbits(gb, 7);
870
4.34k
            }
871
7.21k
        }
872
34.4k
    }
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
37.3k
    if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) {
880
17.5k
        hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3;
881
17.5k
        hdr->cdef.n_bits = dav1d_get_bits(gb, 2);
882
54.4k
        for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) {
883
36.8k
            hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6);
884
36.8k
            if (!seqhdr->monochrome)
885
27.0k
                hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6);
886
36.8k
        }
887
17.5k
    }
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
37.3k
    if ((!hdr->all_lossless || hdr->super_res.enabled) &&
895
36.6k
        seqhdr->restoration && !hdr->allow_intrabc)
896
15.5k
    {
897
15.5k
        hdr->restoration.type[0] = dav1d_get_bits(gb, 2);
898
15.5k
        if (!seqhdr->monochrome) {
899
11.5k
            hdr->restoration.type[1] = dav1d_get_bits(gb, 2);
900
11.5k
            hdr->restoration.type[2] = dav1d_get_bits(gb, 2);
901
11.5k
        }
902
903
15.5k
        if (hdr->restoration.type[0] || hdr->restoration.type[1] ||
904
6.24k
            hdr->restoration.type[2])
905
12.4k
        {
906
            // Log2 of the restoration unit size.
907
12.4k
            hdr->restoration.unit_size[0] = 6 + seqhdr->sb128;
908
12.4k
            if (dav1d_get_bit(gb)) {
909
5.28k
                hdr->restoration.unit_size[0]++;
910
5.28k
                if (!seqhdr->sb128)
911
3.20k
                    hdr->restoration.unit_size[0] += dav1d_get_bit(gb);
912
5.28k
            }
913
12.4k
            hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0];
914
12.4k
            if ((hdr->restoration.type[1] || hdr->restoration.type[2]) &&
915
9.75k
                seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1)
916
6.20k
            {
917
6.20k
                hdr->restoration.unit_size[1] -= dav1d_get_bit(gb);
918
6.20k
            }
919
12.4k
        } else {
920
3.13k
            hdr->restoration.unit_size[0] = 8;
921
3.13k
        }
922
15.5k
    }
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
37.3k
    if (!hdr->all_lossless)
929
36.5k
        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
37.3k
    if (IS_INTER_OR_SWITCH(hdr))
935
19.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
37.3k
    if (hdr->switchable_comp_refs && IS_INTER_OR_SWITCH(hdr) && seqhdr->order_hint) {
941
6.08k
        const int poc = hdr->frame_offset;
942
6.08k
        int off_before = -1, off_after = -1;
943
6.08k
        int off_before_idx, off_after_idx;
944
48.6k
        for (int i = 0; i < 7; i++) {
945
42.5k
            if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
946
42.5k
            const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
947
948
42.5k
            const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc);
949
42.5k
            if (diff > 0) {
950
6.70k
                if (off_after < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
951
4.41k
                                                  off_after, refpoc) > 0)
952
2.44k
                {
953
2.44k
                    off_after = refpoc;
954
2.44k
                    off_after_idx = i;
955
2.44k
                }
956
35.8k
            } else if (diff < 0 && (off_before < 0 ||
957
14.7k
                                    get_poc_diff(seqhdr->order_hint_n_bits,
958
14.7k
                                                 refpoc, off_before) > 0))
959
4.81k
            {
960
4.81k
                off_before = refpoc;
961
4.81k
                off_before_idx = i;
962
4.81k
            }
963
42.5k
        }
964
965
6.07k
        if ((off_before | off_after) >= 0) {
966
1.81k
            hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx);
967
1.81k
            hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx);
968
1.81k
            hdr->skip_mode_allowed = 1;
969
4.26k
        } else if (off_before >= 0) {
970
2.69k
            int off_before2 = -1;
971
2.69k
            int off_before2_idx;
972
21.5k
            for (int i = 0; i < 7; i++) {
973
18.8k
                if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
974
18.8k
                const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
975
18.8k
                if (get_poc_diff(seqhdr->order_hint_n_bits,
976
18.8k
                                 refpoc, off_before) < 0) {
977
2.52k
                    if (off_before2 < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
978
1.78k
                                                        refpoc, off_before2) > 0)
979
797
                    {
980
797
                        off_before2 = refpoc;
981
797
                        off_before2_idx = i;
982
797
                    }
983
2.52k
                }
984
18.8k
            }
985
986
2.69k
            if (off_before2 >= 0) {
987
744
                hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx);
988
744
                hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx);
989
744
                hdr->skip_mode_allowed = 1;
990
744
            }
991
2.69k
        }
992
6.07k
    }
993
37.3k
    if (hdr->skip_mode_allowed)
994
2.56k
        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
37.3k
    if (!hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) && seqhdr->warped_motion)
1000
6.88k
        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
37.3k
    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
298k
    for (int i = 0; i < 7; i++)
1012
261k
        hdr->gmv[i] = dav1d_default_wm_params;
1013
1014
37.3k
    if (IS_INTER_OR_SWITCH(hdr)) {
1015
158k
        for (int i = 0; i < 7; i++) {
1016
138k
            hdr->gmv[i].type = !dav1d_get_bit(gb) ? DAV1D_WM_TYPE_IDENTITY :
1017
138k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_ROT_ZOOM :
1018
35.6k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_TRANSLATION :
1019
16.3k
                                                    DAV1D_WM_TYPE_AFFINE;
1020
1021
138k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue;
1022
1023
35.6k
            const Dav1dWarpedMotionParams *ref_gmv;
1024
35.6k
            if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
1025
3.92k
                ref_gmv = &dav1d_default_wm_params;
1026
31.7k
            } else {
1027
31.7k
                const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
1028
31.7k
                if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
1029
31.7k
                ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i];
1030
31.7k
            }
1031
35.6k
            int32_t *const mat = hdr->gmv[i].matrix;
1032
35.6k
            const int32_t *const ref_mat = ref_gmv->matrix;
1033
35.6k
            int bits, shift;
1034
1035
35.6k
            if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) {
1036
32.4k
                mat[2] = (1 << 16) + 2 *
1037
32.4k
                    dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12);
1038
32.4k
                mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12);
1039
1040
32.4k
                bits = 12;
1041
32.4k
                shift = 10;
1042
32.4k
            } else {
1043
3.28k
                bits = 9 - !hdr->hp;
1044
3.28k
                shift = 13 + !hdr->hp;
1045
3.28k
            }
1046
1047
35.6k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) {
1048
13.0k
                mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12);
1049
13.0k
                mat[5] = (1 << 16) + 2 *
1050
13.0k
                    dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12);
1051
22.6k
            } else {
1052
22.6k
                mat[4] = -mat[3];
1053
22.6k
                mat[5] = mat[2];
1054
22.6k
            }
1055
1056
35.6k
            mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift);
1057
35.6k
            mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift);
1058
35.6k
        }
1059
19.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
37.3k
    if (seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame)) {
1066
9.69k
        hdr->film_grain.present = dav1d_get_bit(gb);
1067
9.69k
        if (hdr->film_grain.present) {
1068
3.44k
            const unsigned seed = dav1d_get_bits(gb, 16);
1069
3.44k
            hdr->film_grain.update = hdr->frame_type != DAV1D_FRAME_TYPE_INTER || dav1d_get_bit(gb);
1070
3.44k
            if (!hdr->film_grain.update) {
1071
2.00k
                const int refidx = dav1d_get_bits(gb, 3);
1072
2.00k
                int i;
1073
7.41k
                for (i = 0; i < 7; i++)
1074
7.41k
                    if (hdr->refidx[i] == refidx)
1075
1.99k
                        break;
1076
2.00k
                if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error;
1077
1.99k
                hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data;
1078
1.99k
                hdr->film_grain.data.seed = seed;
1079
1.99k
            } else {
1080
1.44k
                Dav1dFilmGrainData *const fgd = &hdr->film_grain.data;
1081
1.44k
                fgd->seed = seed;
1082
1083
1.44k
                fgd->num_y_points = dav1d_get_bits(gb, 4);
1084
1.44k
                if (fgd->num_y_points > 14) goto error;
1085
2.27k
                for (int i = 0; i < fgd->num_y_points; i++) {
1086
871
                    fgd->y_points[i][0] = dav1d_get_bits(gb, 8);
1087
871
                    if (i && fgd->y_points[i - 1][0] >= fgd->y_points[i][0])
1088
34
                        goto error;
1089
837
                    fgd->y_points[i][1] = dav1d_get_bits(gb, 8);
1090
837
                }
1091
1092
1.40k
                if (!seqhdr->monochrome)
1093
1.05k
                    fgd->chroma_scaling_from_luma = dav1d_get_bit(gb);
1094
1.40k
                if (seqhdr->monochrome || fgd->chroma_scaling_from_luma ||
1095
653
                    (seqhdr->ss_ver == 1 && seqhdr->ss_hor == 1 && !fgd->num_y_points))
1096
1.03k
                {
1097
1.03k
                    fgd->num_uv_points[0] = fgd->num_uv_points[1] = 0;
1098
1.08k
                } else for (int pl = 0; pl < 2; pl++) {
1099
724
                    fgd->num_uv_points[pl] = dav1d_get_bits(gb, 4);
1100
724
                    if (fgd->num_uv_points[pl] > 10) goto error;
1101
1.32k
                    for (int i = 0; i < fgd->num_uv_points[pl]; i++) {
1102
605
                        fgd->uv_points[pl][i][0] = dav1d_get_bits(gb, 8);
1103
605
                        if (i && fgd->uv_points[pl][i - 1][0] >= fgd->uv_points[pl][i][0])
1104
6
                            goto error;
1105
599
                        fgd->uv_points[pl][i][1] = dav1d_get_bits(gb, 8);
1106
599
                    }
1107
721
                }
1108
1109
1.39k
                if (seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1 &&
1110
867
                    !!fgd->num_uv_points[0] != !!fgd->num_uv_points[1])
1111
1
                {
1112
1
                    goto error;
1113
1
                }
1114
1115
1.39k
                fgd->scaling_shift = dav1d_get_bits(gb, 2) + 8;
1116
1.39k
                fgd->ar_coeff_lag = dav1d_get_bits(gb, 2);
1117
1.39k
                const int num_y_pos = 2 * fgd->ar_coeff_lag * (fgd->ar_coeff_lag + 1);
1118
1.39k
                if (fgd->num_y_points)
1119
6.66k
                    for (int i = 0; i < num_y_pos; i++)
1120
6.00k
                        fgd->ar_coeffs_y[i] = dav1d_get_bits(gb, 8) - 128;
1121
4.18k
                for (int pl = 0; pl < 2; pl++)
1122
2.79k
                    if (fgd->num_uv_points[pl] || fgd->chroma_scaling_from_luma) {
1123
1.15k
                        const int num_uv_pos = num_y_pos + !!fgd->num_y_points;
1124
13.6k
                        for (int i = 0; i < num_uv_pos; i++)
1125
12.4k
                            fgd->ar_coeffs_uv[pl][i] = dav1d_get_bits(gb, 8) - 128;
1126
1.15k
                        if (!fgd->num_y_points)
1127
284
                            fgd->ar_coeffs_uv[pl][num_uv_pos] = 0;
1128
1.15k
                    }
1129
1.39k
                fgd->ar_coeff_shift = dav1d_get_bits(gb, 2) + 6;
1130
1.39k
                fgd->grain_scale_shift = dav1d_get_bits(gb, 2);
1131
4.18k
                for (int pl = 0; pl < 2; pl++)
1132
2.79k
                    if (fgd->num_uv_points[pl]) {
1133
343
                        fgd->uv_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1134
343
                        fgd->uv_luma_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1135
343
                        fgd->uv_offset[pl] = dav1d_get_bits(gb, 9) - 256;
1136
343
                    }
1137
1.39k
                fgd->overlap_flag = dav1d_get_bit(gb);
1138
1.39k
                fgd->clip_to_restricted_range = dav1d_get_bit(gb);
1139
1.39k
            }
1140
3.44k
        }
1141
9.69k
    }
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
37.3k
    return 0;
1148
1149
117
error:
1150
117
    dav1d_log(c, "Error parsing frame header\n");
1151
117
    return DAV1D_ERR(EINVAL);
1152
37.3k
}
1153
1154
32.4k
static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) {
1155
32.4k
    const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows;
1156
32.4k
    const int have_tile_pos = n_tiles > 1 ? dav1d_get_bit(gb) : 0;
1157
1158
32.4k
    if (have_tile_pos) {
1159
79
        const int n_bits = c->frame_hdr->tiling.log2_cols +
1160
79
                           c->frame_hdr->tiling.log2_rows;
1161
79
        c->tile[c->n_tile_data].start = dav1d_get_bits(gb, n_bits);
1162
79
        c->tile[c->n_tile_data].end = dav1d_get_bits(gb, n_bits);
1163
32.3k
    } else {
1164
32.3k
        c->tile[c->n_tile_data].start = 0;
1165
32.3k
        c->tile[c->n_tile_data].end = n_tiles - 1;
1166
32.3k
    }
1167
32.4k
}
1168
1169
81.9k
ptrdiff_t dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in) {
1170
81.9k
    GetBits gb;
1171
81.9k
    int res;
1172
1173
81.9k
    dav1d_init_get_bits(&gb, in->data, in->sz);
1174
1175
    // obu header
1176
81.9k
    const int obu_forbidden_bit = dav1d_get_bit(&gb);
1177
81.9k
    if (c->strict_std_compliance && obu_forbidden_bit) goto error;
1178
81.9k
    const enum Dav1dObuType type = dav1d_get_bits(&gb, 4);
1179
81.9k
    const int has_extension = dav1d_get_bit(&gb);
1180
81.9k
    const int has_length_field = dav1d_get_bit(&gb);
1181
81.9k
    dav1d_get_bit(&gb); // reserved
1182
1183
81.9k
    int temporal_id = 0, spatial_id = 0;
1184
81.9k
    if (has_extension) {
1185
21.9k
        temporal_id = dav1d_get_bits(&gb, 3);
1186
21.9k
        spatial_id = dav1d_get_bits(&gb, 2);
1187
21.9k
        dav1d_get_bits(&gb, 3); // reserved
1188
21.9k
    }
1189
1190
81.9k
    if (has_length_field) {
1191
81.9k
        const size_t len = dav1d_get_uleb128(&gb);
1192
81.9k
        if (len > (size_t)(gb.ptr_end - gb.ptr)) goto error;
1193
81.9k
        gb.ptr_end = gb.ptr + len;
1194
81.9k
    }
1195
81.9k
    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
81.9k
    assert(gb.bits_left == 0);
1201
1202
    // skip obu not belonging to the selected temporal/spatial layer
1203
81.9k
    if (type != DAV1D_OBU_SEQ_HDR && type != DAV1D_OBU_TD &&
1204
71.9k
        has_extension && c->operating_point_idc != 0)
1205
4.84k
    {
1206
4.84k
        const int in_temporal_layer = (c->operating_point_idc >> temporal_id) & 1;
1207
4.84k
        const int in_spatial_layer = (c->operating_point_idc >> (spatial_id + 8)) & 1;
1208
4.84k
        if (!in_temporal_layer || !in_spatial_layer)
1209
1.88k
            return gb.ptr_end - gb.ptr_start;
1210
4.84k
    }
1211
1212
80.0k
    switch (type) {
1213
9.03k
    case DAV1D_OBU_SEQ_HDR: {
1214
9.03k
        Dav1dRef *ref = dav1d_ref_create_using_pool(c->seq_hdr_pool,
1215
9.03k
                                                    sizeof(Dav1dSequenceHeader));
1216
9.03k
        if (!ref) return DAV1D_ERR(ENOMEM);
1217
9.03k
        Dav1dSequenceHeader *seq_hdr = ref->data;
1218
9.03k
        if ((res = parse_seq_hdr(seq_hdr, &gb, c->strict_std_compliance)) < 0) {
1219
37
            dav1d_log(c, "Error parsing sequence header\n");
1220
37
            dav1d_ref_dec(&ref);
1221
37
            goto error;
1222
37
        }
1223
1224
8.99k
        const int op_idx =
1225
8.99k
            c->operating_point < seq_hdr->num_operating_points ? c->operating_point : 0;
1226
8.99k
        c->operating_point_idc = seq_hdr->operating_points[op_idx].idc;
1227
8.99k
        const unsigned spatial_mask = c->operating_point_idc >> 8;
1228
8.99k
        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
8.99k
        if (!c->seq_hdr) {
1235
8.36k
            c->frame_hdr = NULL;
1236
8.36k
            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
8.36k
        } else if (memcmp(seq_hdr, c->seq_hdr, offsetof(Dav1dSequenceHeader, operating_parameter_info))) {
1240
592
            c->frame_hdr = NULL;
1241
592
            c->mastering_display = NULL;
1242
592
            c->content_light = NULL;
1243
592
            dav1d_ref_dec(&c->mastering_display_ref);
1244
592
            dav1d_ref_dec(&c->content_light_ref);
1245
5.32k
            for (int i = 0; i < 8; i++) {
1246
4.73k
                if (c->refs[i].p.p.frame_hdr)
1247
1.13k
                    dav1d_thread_picture_unref(&c->refs[i].p);
1248
4.73k
                dav1d_ref_dec(&c->refs[i].segmap);
1249
4.73k
                dav1d_ref_dec(&c->refs[i].refmvs);
1250
4.73k
                dav1d_cdf_thread_unref(&c->cdf[i]);
1251
4.73k
            }
1252
592
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1253
        // If operating_parameter_info changed, signal it
1254
592
        } else if (memcmp(seq_hdr->operating_parameter_info, c->seq_hdr->operating_parameter_info,
1255
41
                          sizeof(seq_hdr->operating_parameter_info)))
1256
8
        {
1257
8
            c->frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO;
1258
8
        }
1259
8.99k
        dav1d_ref_dec(&c->seq_hdr_ref);
1260
8.99k
        c->seq_hdr_ref = ref;
1261
8.99k
        c->seq_hdr = seq_hdr;
1262
8.99k
        break;
1263
9.03k
    }
1264
1.09k
    case DAV1D_OBU_REDUNDANT_FRAME_HDR:
1265
1.09k
        if (c->frame_hdr) break;
1266
        // fall-through
1267
32.0k
    case DAV1D_OBU_FRAME:
1268
38.2k
    case DAV1D_OBU_FRAME_HDR:
1269
38.2k
        if (!c->seq_hdr) goto error;
1270
38.2k
        if (!c->frame_hdr_ref) {
1271
32.8k
            c->frame_hdr_ref = dav1d_ref_create_using_pool(c->frame_hdr_pool,
1272
32.8k
                                                           sizeof(Dav1dFrameHeader));
1273
32.8k
            if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM);
1274
32.8k
        }
1275
#ifndef NDEBUG
1276
        // ensure that the reference is writable
1277
        assert(dav1d_ref_is_writable(c->frame_hdr_ref));
1278
#endif
1279
38.2k
        c->frame_hdr = c->frame_hdr_ref->data;
1280
38.2k
        memset(c->frame_hdr, 0, sizeof(*c->frame_hdr));
1281
38.2k
        c->frame_hdr->temporal_id = temporal_id;
1282
38.2k
        c->frame_hdr->spatial_id = spatial_id;
1283
38.2k
        if ((res = parse_frame_hdr(c, &gb)) < 0) {
1284
117
            c->frame_hdr = NULL;
1285
117
            goto error;
1286
117
        }
1287
38.1k
        for (int n = 0; n < c->n_tile_data; n++)
1288
24
            dav1d_data_unref_internal(&c->tile[n].data);
1289
38.1k
        c->n_tile_data = 0;
1290
38.1k
        c->n_tiles = 0;
1291
38.1k
        if (type != DAV1D_OBU_FRAME) {
1292
            // This is actually a frame header OBU so read the
1293
            // trailing bit and check for overrun.
1294
7.08k
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1295
29
                c->frame_hdr = NULL;
1296
29
                goto error;
1297
29
            }
1298
7.08k
        }
1299
1300
38.0k
        if (c->frame_size_limit && (int64_t)c->frame_hdr->width[1] *
1301
38.0k
            c->frame_hdr->height > c->frame_size_limit)
1302
6
        {
1303
6
            dav1d_log(c, "Frame size %dx%d exceeds limit %u\n", c->frame_hdr->width[1],
1304
6
                      c->frame_hdr->height, c->frame_size_limit);
1305
6
            c->frame_hdr = NULL;
1306
6
            return DAV1D_ERR(ERANGE);
1307
6
        }
1308
1309
38.0k
        if (type != DAV1D_OBU_FRAME)
1310
7.05k
            break;
1311
        // OBU_FRAMEs shouldn't be signaled with show_existing_frame
1312
31.0k
        if (c->frame_hdr->show_existing_frame) {
1313
14
            c->frame_hdr = NULL;
1314
14
            goto error;
1315
14
        }
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
31.0k
        dav1d_bytealign_get_bits(&gb);
1321
        // fall-through
1322
32.4k
    case DAV1D_OBU_TILE_GRP: {
1323
32.4k
        if (!c->frame_hdr) goto error;
1324
32.4k
        if (c->n_tile_data_alloc < c->n_tile_data + 1) {
1325
8.13k
            if ((c->n_tile_data + 1) > INT_MAX / (int)sizeof(*c->tile)) goto error;
1326
8.13k
            struct Dav1dTileGroup *tile = dav1d_realloc(ALLOC_TILE, c->tile,
1327
8.13k
                                                        (c->n_tile_data + 1) * sizeof(*c->tile));
1328
8.13k
            if (!tile) goto error;
1329
8.13k
            c->tile = tile;
1330
8.13k
            memset(c->tile + c->n_tile_data, 0, sizeof(*c->tile));
1331
8.13k
            c->n_tile_data_alloc = c->n_tile_data + 1;
1332
8.13k
        }
1333
32.4k
        parse_tile_hdr(c, &gb);
1334
        // Align to the next byte boundary and check for overrun.
1335
32.4k
        dav1d_bytealign_get_bits(&gb);
1336
32.4k
        if (gb.error) goto error;
1337
1338
32.3k
        dav1d_data_ref(&c->tile[c->n_tile_data].data, in);
1339
32.3k
        c->tile[c->n_tile_data].data.data = gb.ptr;
1340
32.3k
        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
32.3k
        if (c->tile[c->n_tile_data].start > c->tile[c->n_tile_data].end ||
1343
32.3k
            c->tile[c->n_tile_data].start != c->n_tiles)
1344
6
        {
1345
13
            for (int i = 0; i <= c->n_tile_data; i++)
1346
7
                dav1d_data_unref_internal(&c->tile[i].data);
1347
6
            c->n_tile_data = 0;
1348
6
            c->n_tiles = 0;
1349
6
            goto error;
1350
6
        }
1351
32.3k
        c->n_tiles += 1 + c->tile[c->n_tile_data].end -
1352
32.3k
                          c->tile[c->n_tile_data].start;
1353
32.3k
        c->n_tile_data++;
1354
32.3k
        break;
1355
32.3k
    }
1356
678
    case DAV1D_OBU_METADATA: {
1357
678
#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
678
        const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb);
1363
678
        if (gb.error) goto error;
1364
1365
671
        switch (meta_type) {
1366
28
        case OBU_META_HDR_CLL: {
1367
28
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1368
28
                                             sizeof(Dav1dContentLightLevel));
1369
28
            if (!ref) return DAV1D_ERR(ENOMEM);
1370
28
            Dav1dContentLightLevel *const content_light = ref->data;
1371
1372
28
            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
28
            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
28
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1386
1
                dav1d_ref_dec(&ref);
1387
1
                goto error;
1388
1
            }
1389
1390
27
            dav1d_ref_dec(&c->content_light_ref);
1391
27
            c->content_light = content_light;
1392
27
            c->content_light_ref = ref;
1393
27
            break;
1394
28
        }
1395
22
        case OBU_META_HDR_MDCV: {
1396
22
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1397
22
                                             sizeof(Dav1dMasteringDisplay));
1398
22
            if (!ref) return DAV1D_ERR(ENOMEM);
1399
22
            Dav1dMasteringDisplay *const mastering_display = ref->data;
1400
1401
88
            for (int i = 0; i < 3; i++) {
1402
66
                mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16);
1403
66
                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
66
            }
1411
22
            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
22
            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
22
            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
22
            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
22
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1436
1
                dav1d_ref_dec(&ref);
1437
1
                goto error;
1438
1
            }
1439
1440
21
            dav1d_ref_dec(&c->mastering_display_ref);
1441
21
            c->mastering_display = mastering_display;
1442
21
            c->mastering_display_ref = ref;
1443
21
            break;
1444
22
        }
1445
15
        case OBU_META_ITUT_T35: {
1446
15
            ptrdiff_t payload_size = gb.ptr_end - gb.ptr;
1447
            // Don't take into account all the trailing bits for payload_size
1448
93
            while (payload_size > 0 && !gb.ptr[payload_size - 1])
1449
78
                payload_size--; // trailing_zero_bit x 8
1450
15
            payload_size--; // trailing_one_bit + trailing_zero_bit x 7
1451
1452
15
            int country_code_extension_byte = 0;
1453
15
            const int country_code = dav1d_get_bits(&gb, 8);
1454
15
            payload_size--;
1455
15
            if (country_code == 0xFF) {
1456
4
                country_code_extension_byte = dav1d_get_bits(&gb, 8);
1457
4
                payload_size--;
1458
4
            }
1459
1460
15
            if (payload_size <= 0 || gb.ptr[payload_size] != 0x80) {
1461
8
                dav1d_log(c, "Malformed ITU-T T.35 metadata message format\n");
1462
8
                break;
1463
8
            }
1464
1465
7
            if ((c->n_itut_t35 + 1) > INT_MAX / (int)sizeof(*c->itut_t35)) goto error;
1466
7
            uint8_t *const payload = dav1d_malloc(ALLOC_OBU_META, payload_size);
1467
7
            if (!payload) goto error;
1468
1469
7
            struct itut_t35_ctx_context *itut_t35_ctx = NULL;
1470
7
            if (!c->n_itut_t35) {
1471
6
                assert(!c->itut_t35_ref);
1472
6
                itut_t35_ctx = dav1d_malloc(ALLOC_OBU_META, sizeof(struct itut_t35_ctx_context));
1473
6
                if (!itut_t35_ctx) {
1474
0
                    dav1d_free(payload);
1475
0
                    goto error;
1476
0
                }
1477
6
            }
1478
1479
7
            struct Dav1dITUTT35 *itut_t35 = dav1d_realloc(ALLOC_OBU_META, c->itut_t35,
1480
7
                                                          (c->n_itut_t35 + 1) * sizeof(*c->itut_t35));
1481
7
            if (!itut_t35) {
1482
0
                dav1d_free(itut_t35_ctx);
1483
0
                dav1d_free(payload);
1484
0
                goto error;
1485
0
            }
1486
7
            c->itut_t35 = itut_t35;
1487
7
            memset(c->itut_t35 + c->n_itut_t35, 0, sizeof(*c->itut_t35));
1488
1489
7
            if (!c->n_itut_t35) {
1490
6
                c->itut_t35_ref = dav1d_ref_init(&itut_t35_ctx->ref, c->itut_t35,
1491
6
                                                 dav1d_picture_free_itut_t35, itut_t35_ctx, 0);
1492
6
            } else {
1493
1
                assert(c->itut_t35_ref && atomic_load(&c->itut_t35_ref->ref_cnt) == 1);
1494
1
                itut_t35_ctx = c->itut_t35_ref->user_data;
1495
1
                c->itut_t35_ref->const_data = (uint8_t *)c->itut_t35;
1496
1
            }
1497
7
            itut_t35_ctx->itut_t35 = c->itut_t35;
1498
7
            itut_t35_ctx->n_itut_t35 = c->n_itut_t35 + 1;
1499
1500
7
            Dav1dITUTT35 *const itut_t35_metadata = &c->itut_t35[c->n_itut_t35];
1501
7
            itut_t35_metadata->payload = payload;
1502
1503
7
            itut_t35_metadata->country_code = country_code;
1504
7
            itut_t35_metadata->country_code_extension_byte = country_code_extension_byte;
1505
7
            itut_t35_metadata->payload_size = payload_size;
1506
1507
            // We know that we've read a whole number of bytes and that the
1508
            // payload is within the OBU boundaries, so just use memcpy()
1509
7
            assert(gb.bits_left == 0);
1510
7
            memcpy(itut_t35_metadata->payload, gb.ptr, payload_size);
1511
1512
7
            c->n_itut_t35++;
1513
7
            break;
1514
7
        }
1515
14
        case OBU_META_SCALABILITY:
1516
17
        case OBU_META_TIMECODE:
1517
            // ignore metadata OBUs we don't care about
1518
17
            break;
1519
589
        default:
1520
            // print a warning but don't fail for unknown types
1521
589
            if (meta_type > 31) // Types 6 to 31 are "Unregistered user private", so ignore them.
1522
484
                dav1d_log(c, "Unknown Metadata OBU type %d\n", meta_type);
1523
589
            break;
1524
671
        }
1525
1526
669
        break;
1527
671
    }
1528
939
    case DAV1D_OBU_TD:
1529
939
        c->frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT;
1530
939
        break;
1531
4.69k
    case DAV1D_OBU_PADDING:
1532
        // ignore OBUs we don't care about
1533
4.69k
        break;
1534
24.8k
    default:
1535
        // print a warning but don't fail for unknown types
1536
24.8k
        dav1d_log(c, "Unknown OBU type %d of size %td\n", type, gb.ptr_end - gb.ptr);
1537
24.8k
        break;
1538
80.0k
    }
1539
1540
79.7k
    if (c->seq_hdr && c->frame_hdr) {
1541
42.0k
        if (c->frame_hdr->show_existing_frame) {
1542
806
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr) goto error;
1543
799
            switch (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type) {
1544
165
            case DAV1D_FRAME_TYPE_INTER:
1545
193
            case DAV1D_FRAME_TYPE_SWITCH:
1546
193
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE)
1547
0
                    goto skip;
1548
193
                break;
1549
193
            case DAV1D_FRAME_TYPE_INTRA:
1550
69
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA)
1551
0
                    goto skip;
1552
                // fall-through
1553
606
            default:
1554
606
                break;
1555
799
            }
1556
799
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.data[0]) goto error;
1557
799
            if (c->strict_std_compliance &&
1558
0
                !c->refs[c->frame_hdr->existing_frame_idx].p.showable)
1559
0
            {
1560
0
                goto error;
1561
0
            }
1562
799
            if (c->n_fc == 1) {
1563
0
                dav1d_thread_picture_ref(&c->out,
1564
0
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1565
0
                dav1d_picture_copy_props(&c->out.p,
1566
0
                                         c->content_light, c->content_light_ref,
1567
0
                                         c->mastering_display, c->mastering_display_ref,
1568
0
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1569
0
                                         &in->m);
1570
                // Must be removed from the context after being attached to the frame
1571
0
                dav1d_ref_dec(&c->itut_t35_ref);
1572
0
                c->itut_t35 = NULL;
1573
0
                c->n_itut_t35 = 0;
1574
0
                c->event_flags |= dav1d_picture_get_event_flags(&c->refs[c->frame_hdr->existing_frame_idx].p);
1575
799
            } else {
1576
799
                pthread_mutex_lock(&c->task_thread.lock);
1577
                // need to append this to the frame output queue
1578
799
                const unsigned next = c->frame_thread.next++;
1579
799
                if (c->frame_thread.next == c->n_fc)
1580
98
                    c->frame_thread.next = 0;
1581
1582
799
                Dav1dFrameContext *const f = &c->fc[next];
1583
857
                while (f->n_tile_data > 0)
1584
58
                    pthread_cond_wait(&f->task_thread.cond,
1585
58
                                      &f->task_thread.ttd->lock);
1586
799
                Dav1dThreadPicture *const out_delayed =
1587
799
                    &c->frame_thread.out_delayed[next];
1588
799
                if (out_delayed->p.data[0] || atomic_load(&f->task_thread.error)) {
1589
181
                    unsigned first = atomic_load(&c->task_thread.first);
1590
181
                    if (first + 1U < c->n_fc)
1591
181
                        atomic_fetch_add(&c->task_thread.first, 1U);
1592
13
                    else
1593
181
                        atomic_store(&c->task_thread.first, 0);
1594
181
                    atomic_compare_exchange_strong(&c->task_thread.reset_task_cur,
1595
181
                                                   &first, UINT_MAX);
1596
181
                    if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
1597
22
                        c->task_thread.cur--;
1598
181
                }
1599
799
                const int error = f->task_thread.retval;
1600
799
                if (error) {
1601
0
                    c->cached_error = error;
1602
0
                    f->task_thread.retval = 0;
1603
0
                    dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
1604
0
                    dav1d_thread_picture_unref(out_delayed);
1605
799
                } else if (out_delayed->p.data[0]) {
1606
181
                    const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
1607
181
                                                                   memory_order_relaxed);
1608
181
                    if ((out_delayed->visible || c->output_invisible_frames) &&
1609
166
                        progress != FRAME_ERROR)
1610
166
                    {
1611
166
                        dav1d_thread_picture_ref(&c->out, out_delayed);
1612
166
                        c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
1613
166
                    }
1614
181
                    dav1d_thread_picture_unref(out_delayed);
1615
181
                }
1616
799
                dav1d_thread_picture_ref(out_delayed,
1617
799
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1618
799
                out_delayed->visible = 1;
1619
799
                dav1d_picture_copy_props(&out_delayed->p,
1620
799
                                         c->content_light, c->content_light_ref,
1621
799
                                         c->mastering_display, c->mastering_display_ref,
1622
799
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1623
799
                                         &in->m);
1624
                // Must be removed from the context after being attached to the frame
1625
799
                dav1d_ref_dec(&c->itut_t35_ref);
1626
799
                c->itut_t35 = NULL;
1627
799
                c->n_itut_t35 = 0;
1628
1629
799
                pthread_mutex_unlock(&c->task_thread.lock);
1630
799
            }
1631
799
            if (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY) {
1632
537
                const int r = c->frame_hdr->existing_frame_idx;
1633
537
                c->refs[r].p.showable = 0;
1634
4.83k
                for (int i = 0; i < 8; i++) {
1635
4.29k
                    if (i == r) continue;
1636
1637
3.75k
                    if (c->refs[i].p.p.frame_hdr)
1638
3.73k
                        dav1d_thread_picture_unref(&c->refs[i].p);
1639
3.75k
                    dav1d_thread_picture_ref(&c->refs[i].p, &c->refs[r].p);
1640
1641
3.75k
                    dav1d_cdf_thread_unref(&c->cdf[i]);
1642
3.75k
                    dav1d_cdf_thread_ref(&c->cdf[i], &c->cdf[r]);
1643
1644
3.75k
                    dav1d_ref_dec(&c->refs[i].segmap);
1645
3.75k
                    c->refs[i].segmap = c->refs[r].segmap;
1646
3.75k
                    if (c->refs[r].segmap)
1647
2.11k
                        dav1d_ref_inc(c->refs[r].segmap);
1648
3.75k
                    dav1d_ref_dec(&c->refs[i].refmvs);
1649
3.75k
                }
1650
537
            }
1651
799
            c->frame_hdr = NULL;
1652
41.2k
        } else if (c->n_tiles == c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows) {
1653
32.3k
            switch (c->frame_hdr->frame_type) {
1654
18.6k
            case DAV1D_FRAME_TYPE_INTER:
1655
19.5k
            case DAV1D_FRAME_TYPE_SWITCH:
1656
19.5k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE ||
1657
19.5k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1658
0
                     !c->frame_hdr->refresh_frame_flags))
1659
0
                    goto skip;
1660
19.5k
                break;
1661
19.5k
            case DAV1D_FRAME_TYPE_INTRA:
1662
778
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA ||
1663
778
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1664
0
                     !c->frame_hdr->refresh_frame_flags))
1665
0
                    goto skip;
1666
                // fall-through
1667
12.7k
            default:
1668
12.7k
                break;
1669
32.3k
            }
1670
32.3k
            if (!c->n_tile_data)
1671
0
                goto error;
1672
32.3k
            if ((res = dav1d_submit_frame(c)) < 0)
1673
74
                return res;
1674
32.2k
            assert(!c->n_tile_data);
1675
32.2k
            c->frame_hdr = NULL;
1676
32.2k
            c->n_tiles = 0;
1677
32.2k
        }
1678
42.0k
    }
1679
1680
79.6k
    return gb.ptr_end - gb.ptr_start;
1681
1682
0
skip:
1683
    // update refs with only the headers in case we skip the frame
1684
0
    for (int i = 0; i < 8; i++) {
1685
0
        if (c->frame_hdr->refresh_frame_flags & (1 << i)) {
1686
0
            dav1d_thread_picture_unref(&c->refs[i].p);
1687
0
            c->refs[i].p.p.frame_hdr = c->frame_hdr;
1688
0
            c->refs[i].p.p.seq_hdr = c->seq_hdr;
1689
0
            c->refs[i].p.p.frame_hdr_ref = c->frame_hdr_ref;
1690
0
            c->refs[i].p.p.seq_hdr_ref = c->seq_hdr_ref;
1691
0
            dav1d_ref_inc(c->frame_hdr_ref);
1692
0
            dav1d_ref_inc(c->seq_hdr_ref);
1693
0
        }
1694
0
    }
1695
1696
0
    dav1d_ref_dec(&c->frame_hdr_ref);
1697
0
    c->frame_hdr = NULL;
1698
0
    c->n_tiles = 0;
1699
1700
0
    return gb.ptr_end - gb.ptr_start;
1701
1702
301
error:
1703
301
    dav1d_data_props_copy(&c->cached_error_props, &in->m);
1704
301
    dav1d_log(c, gb.error ? "Overrun in OBU bit buffer\n" :
1705
301
                            "Error parsing OBU data\n");
1706
301
    return DAV1D_ERR(EINVAL);
1707
79.7k
}