Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/obu.c
Line
Count
Source
1
/*
2
 * Copyright © 2018-2021, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <errno.h>
31
#include <limits.h>
32
#include <stdio.h>
33
34
#include "dav1d/data.h"
35
36
#include "common/frame.h"
37
#include "common/intops.h"
38
#include "common/validate.h"
39
40
#include "src/decode.h"
41
#include "src/getbits.h"
42
#include "src/levels.h"
43
#include "src/log.h"
44
#include "src/obu.h"
45
#include "src/ref.h"
46
#include "src/thread_task.h"
47
48
static int check_trailing_bits(GetBits *const gb,
49
                               const int strict_std_compliance)
50
29.5k
{
51
29.5k
    const int trailing_one_bit = dav1d_get_bit(gb);
52
53
29.5k
    if (gb->error)
54
146
        return DAV1D_ERR(EINVAL);
55
56
29.3k
    if (!strict_std_compliance)
57
29.3k
        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
28.0k
{
76
28.0k
#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
28.0k
    memset(hdr, 0, sizeof(*hdr));
83
28.0k
    hdr->profile = dav1d_get_bits(gb, 3);
84
28.0k
    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
28.0k
    hdr->still_picture = dav1d_get_bit(gb);
91
28.0k
    hdr->reduced_still_picture_header = dav1d_get_bit(gb);
92
28.0k
    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
27.9k
    if (hdr->reduced_still_picture_header) {
99
18.6k
        hdr->num_operating_points = 1;
100
18.6k
        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);
101
18.6k
        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);
102
18.6k
        hdr->operating_points[0].initial_display_delay = 10;
103
18.6k
    } else {
104
9.37k
        hdr->timing_info_present = dav1d_get_bit(gb);
105
9.37k
        if (hdr->timing_info_present) {
106
1.54k
            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);
107
1.54k
            hdr->time_scale = dav1d_get_bits(gb, 32);
108
1.54k
            if (strict_std_compliance && (!hdr->num_units_in_tick || !hdr->time_scale))
109
0
                goto error;
110
1.54k
            hdr->equal_picture_interval = dav1d_get_bit(gb);
111
1.54k
            if (hdr->equal_picture_interval) {
112
1.21k
                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
113
1.21k
                if (num_ticks_per_picture == UINT32_MAX)
114
0
                    goto error;
115
1.21k
                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
116
1.21k
            }
117
118
1.54k
            hdr->decoder_model_info_present = dav1d_get_bit(gb);
119
1.54k
            if (hdr->decoder_model_info_present) {
120
830
                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;
121
830
                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);
122
830
                if (strict_std_compliance && !hdr->num_units_in_decoding_tick)
123
0
                    goto error;
124
830
                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;
125
830
                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;
126
830
            }
127
1.54k
        }
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
9.37k
        hdr->display_model_info_present = dav1d_get_bit(gb);
134
9.37k
        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;
135
29.9k
        for (int i = 0; i < hdr->num_operating_points; i++) {
136
20.7k
            struct Dav1dSequenceHeaderOperatingPoint *const op =
137
20.7k
                &hdr->operating_points[i];
138
20.7k
            op->idc = dav1d_get_bits(gb, 12);
139
20.7k
            if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00)))
140
154
                goto error;
141
20.5k
            op->major_level = 2 + dav1d_get_bits(gb, 3);
142
20.5k
            op->minor_level = dav1d_get_bits(gb, 2);
143
20.5k
            if (op->major_level > 3)
144
9.18k
                op->tier = dav1d_get_bit(gb);
145
20.5k
            if (hdr->decoder_model_info_present) {
146
5.89k
                op->decoder_model_param_present = dav1d_get_bit(gb);
147
5.89k
                if (op->decoder_model_param_present) {
148
1.89k
                    struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
149
1.89k
                        &hdr->operating_parameter_info[i];
150
1.89k
                    opi->decoder_buffer_delay =
151
1.89k
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
152
1.89k
                    opi->encoder_buffer_delay =
153
1.89k
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
154
1.89k
                    opi->low_delay_mode = dav1d_get_bit(gb);
155
1.89k
                }
156
5.89k
            }
157
20.5k
            if (hdr->display_model_info_present)
158
1.62k
                op->display_model_param_present = dav1d_get_bit(gb);
159
20.5k
            op->initial_display_delay =
160
20.5k
                op->display_model_param_present ? dav1d_get_bits(gb, 4) + 1 : 10;
161
20.5k
        }
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
9.37k
    }
167
168
27.8k
    hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;
169
27.8k
    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;
170
27.8k
    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;
171
27.8k
    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
27.8k
    if (!hdr->reduced_still_picture_header) {
177
9.21k
        hdr->frame_id_numbers_present = dav1d_get_bit(gb);
178
9.21k
        if (hdr->frame_id_numbers_present) {
179
695
            hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;
180
695
            hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;
181
695
        }
182
9.21k
    }
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
27.8k
    hdr->sb128 = dav1d_get_bit(gb);
189
27.8k
    hdr->filter_intra = dav1d_get_bit(gb);
190
27.8k
    hdr->intra_edge_filter = dav1d_get_bit(gb);
191
27.8k
    if (hdr->reduced_still_picture_header) {
192
18.6k
        hdr->screen_content_tools = DAV1D_ADAPTIVE;
193
18.6k
        hdr->force_integer_mv = DAV1D_ADAPTIVE;
194
18.6k
    } else {
195
9.21k
        hdr->inter_intra = dav1d_get_bit(gb);
196
9.21k
        hdr->masked_compound = dav1d_get_bit(gb);
197
9.21k
        hdr->warped_motion = dav1d_get_bit(gb);
198
9.21k
        hdr->dual_filter = dav1d_get_bit(gb);
199
9.21k
        hdr->order_hint = dav1d_get_bit(gb);
200
9.21k
        if (hdr->order_hint) {
201
3.07k
            hdr->jnt_comp = dav1d_get_bit(gb);
202
3.07k
            hdr->ref_frame_mvs = dav1d_get_bit(gb);
203
3.07k
        }
204
9.21k
        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
9.21k
        hdr->force_integer_mv = hdr->screen_content_tools ?
210
6.61k
                                dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb) : 2;
211
9.21k
        if (hdr->order_hint)
212
3.07k
            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;
213
9.21k
    }
214
27.8k
    hdr->super_res = dav1d_get_bit(gb);
215
27.8k
    hdr->cdef = dav1d_get_bit(gb);
216
27.8k
    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
27.8k
    hdr->hbd = dav1d_get_bit(gb);
223
27.8k
    if (hdr->profile == 2 && hdr->hbd)
224
1.53k
        hdr->hbd += dav1d_get_bit(gb);
225
27.8k
    if (hdr->profile != 1)
226
14.3k
        hdr->monochrome = dav1d_get_bit(gb);
227
27.8k
    hdr->color_description_present = dav1d_get_bit(gb);
228
27.8k
    if (hdr->color_description_present) {
229
8.05k
        hdr->pri = dav1d_get_bits(gb, 8);
230
8.05k
        hdr->trc = dav1d_get_bits(gb, 8);
231
8.05k
        hdr->mtrx = dav1d_get_bits(gb, 8);
232
19.7k
    } else {
233
19.7k
        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;
234
19.7k
        hdr->trc = DAV1D_TRC_UNKNOWN;
235
19.7k
        hdr->mtrx = DAV1D_MC_UNKNOWN;
236
19.7k
    }
237
27.8k
    if (hdr->monochrome) {
238
3.92k
        hdr->color_range = dav1d_get_bit(gb);
239
3.92k
        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;
240
3.92k
        hdr->ss_hor = hdr->ss_ver = 1;
241
3.92k
        hdr->chr = DAV1D_CHR_UNKNOWN;
242
23.9k
    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&
243
1.61k
               hdr->trc == DAV1D_TRC_SRGB &&
244
1.50k
               hdr->mtrx == DAV1D_MC_IDENTITY)
245
38
    {
246
38
        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
247
38
        hdr->color_range = 1;
248
38
        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))
249
12
            goto error;
250
23.8k
    } else {
251
23.8k
        hdr->color_range = dav1d_get_bit(gb);
252
23.8k
        switch (hdr->profile) {
253
8.64k
        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;
254
8.64k
                hdr->ss_hor = hdr->ss_ver = 1;
255
8.64k
                break;
256
13.5k
        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
257
13.5k
                break;
258
1.70k
        case 2:
259
1.70k
            if (hdr->hbd == 2) {
260
902
                hdr->ss_hor = dav1d_get_bit(gb);
261
902
                if (hdr->ss_hor)
262
231
                    hdr->ss_ver = dav1d_get_bit(gb);
263
902
            } else
264
803
                hdr->ss_hor = 1;
265
1.70k
            hdr->layout = hdr->ss_hor ?
266
1.03k
                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :
267
1.03k
                                        DAV1D_PIXEL_LAYOUT_I422 :
268
1.70k
                                        DAV1D_PIXEL_LAYOUT_I444;
269
1.70k
            break;
270
23.8k
        }
271
23.8k
        hdr->chr = (hdr->ss_hor & hdr->ss_ver) ?
272
15.0k
                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;
273
23.8k
    }
274
27.8k
    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
27.8k
    if (!hdr->monochrome)
280
23.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
27.8k
    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
27.8k
    return check_trailing_bits(gb, strict_std_compliance);
297
298
244
error:
299
244
    return DAV1D_ERR(EINVAL);
300
27.8k
}
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
38.5k
{
344
38.5k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
345
38.5k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
346
347
38.5k
    if (use_ref) {
348
9.57k
        for (int i = 0; i < 7; i++) {
349
9.41k
            if (dav1d_get_bit(gb)) {
350
3.20k
                const Dav1dThreadPicture *const ref =
351
3.20k
                    &c->refs[c->frame_hdr->refidx[i]].p;
352
3.20k
                if (!ref->p.frame_hdr) return -1;
353
3.18k
                hdr->width[1] = ref->p.frame_hdr->width[1];
354
3.18k
                hdr->height = ref->p.frame_hdr->height;
355
3.18k
                if (hdr->width[1] > seqhdr->max_width ||
356
3.18k
                    hdr->height > seqhdr->max_height)
357
0
                {
358
0
                    return -1;
359
0
                }
360
3.18k
                hdr->render_width = ref->p.frame_hdr->render_width;
361
3.18k
                hdr->render_height = ref->p.frame_hdr->render_height;
362
3.18k
                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
363
3.18k
                if (hdr->super_res.enabled) {
364
600
                    const int d = hdr->super_res.width_scale_denominator =
365
600
                        9 + dav1d_get_bits(gb, 3);
366
600
                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,
367
600
                                         imin(16, hdr->width[1]));
368
2.58k
                } else {
369
2.58k
                    hdr->super_res.width_scale_denominator = 8;
370
2.58k
                    hdr->width[0] = hdr->width[1];
371
2.58k
                }
372
3.18k
                return 0;
373
3.18k
            }
374
9.41k
        }
375
3.35k
    }
376
377
35.3k
    if (hdr->frame_size_override) {
378
3.52k
        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;
379
3.52k
        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;
380
3.52k
        if (hdr->width[1] > seqhdr->max_width ||
381
3.46k
            hdr->height > seqhdr->max_height)
382
107
        {
383
107
            return -1;
384
107
        }
385
31.8k
    } else {
386
31.8k
        hdr->width[1] = seqhdr->max_width;
387
31.8k
        hdr->height = seqhdr->max_height;
388
31.8k
    }
389
35.2k
    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
390
35.2k
    if (hdr->super_res.enabled) {
391
9.04k
        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);
392
9.04k
        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));
393
26.2k
    } else {
394
26.2k
        hdr->super_res.width_scale_denominator = 8;
395
26.2k
        hdr->width[0] = hdr->width[1];
396
26.2k
    }
397
35.2k
    hdr->have_render_size = dav1d_get_bit(gb);
398
35.2k
    if (hdr->have_render_size) {
399
16.3k
        hdr->render_width = dav1d_get_bits(gb, 16) + 1;
400
16.3k
        hdr->render_height = dav1d_get_bits(gb, 16) + 1;
401
18.9k
    } else {
402
18.9k
        hdr->render_width = hdr->width[1];
403
18.9k
        hdr->render_height = hdr->height;
404
18.9k
    }
405
35.2k
    return 0;
406
35.3k
}
407
408
185k
static inline int tile_log2(const int sz, const int tgt) {
409
185k
    int k;
410
283k
    for (k = 0; (sz << k) < tgt; k++) ;
411
185k
    return k;
412
185k
}
413
414
static const Dav1dLoopfilterModeRefDeltas default_mode_ref_deltas = {
415
    .mode_delta = { 0, 0 },
416
    .ref_delta = { 1, 0, 0, 0, -1, 0, -1, -1 },
417
};
418
419
38.8k
static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {
420
38.8k
#define DEBUG_FRAME_HDR 0
421
422
#if DEBUG_FRAME_HDR
423
    const uint8_t *const init_ptr = gb->ptr;
424
#endif
425
38.8k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
426
38.8k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
427
428
38.8k
    if (!seqhdr->reduced_still_picture_header)
429
19.3k
        hdr->show_existing_frame = dav1d_get_bit(gb);
430
#if DEBUG_FRAME_HDR
431
    printf("HDR: post-show_existing_frame: off=%td\n",
432
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
433
#endif
434
38.8k
    if (hdr->show_existing_frame) {
435
225
        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);
436
225
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
437
23
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
438
225
        if (seqhdr->frame_id_numbers_present) {
439
34
            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
440
34
            Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr;
441
34
            if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) goto error;
442
34
        }
443
201
        return 0;
444
225
    }
445
446
38.6k
    if (seqhdr->reduced_still_picture_header) {
447
19.5k
        hdr->frame_type = DAV1D_FRAME_TYPE_KEY;
448
19.5k
        hdr->show_frame = 1;
449
19.5k
    } else {
450
19.1k
        hdr->frame_type = dav1d_get_bits(gb, 2);
451
19.1k
        hdr->show_frame = dav1d_get_bit(gb);
452
19.1k
    }
453
38.6k
    if (hdr->show_frame) {
454
34.3k
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
455
153
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
456
34.3k
        hdr->showable_frame = hdr->frame_type != DAV1D_FRAME_TYPE_KEY;
457
34.3k
    } else
458
4.28k
        hdr->showable_frame = dav1d_get_bit(gb);
459
38.6k
    hdr->error_resilient_mode =
460
38.6k
        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||
461
13.3k
        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||
462
13.1k
        seqhdr->reduced_still_picture_header || dav1d_get_bit(gb);
463
#if DEBUG_FRAME_HDR
464
    printf("HDR: post-frametype_bits: off=%td\n",
465
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
466
#endif
467
38.6k
    hdr->disable_cdf_update = dav1d_get_bit(gb);
468
38.6k
    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?
469
27.9k
                                      dav1d_get_bit(gb) : seqhdr->screen_content_tools;
470
38.6k
    if (hdr->allow_screen_content_tools)
471
18.4k
        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?
472
14.5k
                                dav1d_get_bit(gb) : seqhdr->force_integer_mv;
473
474
38.6k
    if (IS_KEY_OR_INTRA(hdr))
475
28.4k
        hdr->force_integer_mv = 1;
476
477
38.6k
    if (seqhdr->frame_id_numbers_present)
478
722
        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
479
480
38.6k
    if (!seqhdr->reduced_still_picture_header)
481
19.1k
        hdr->frame_size_override = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 1 : dav1d_get_bit(gb);
482
#if DEBUG_FRAME_HDR
483
    printf("HDR: post-frame_size_override_flag: off=%td\n",
484
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
485
#endif
486
38.6k
    if (seqhdr->order_hint)
487
7.34k
        hdr->frame_offset = dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
488
38.6k
    hdr->primary_ref_frame = !hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) ?
489
29.5k
                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;
490
491
38.6k
    if (seqhdr->decoder_model_info_present) {
492
2.10k
        hdr->buffer_removal_time_present = dav1d_get_bit(gb);
493
2.10k
        if (hdr->buffer_removal_time_present) {
494
7.55k
            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {
495
6.82k
                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];
496
6.82k
                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];
497
6.82k
                if (seqop->decoder_model_param_present) {
498
2.49k
                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;
499
2.49k
                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;
500
2.49k
                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))
501
1.01k
                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);
502
2.49k
                }
503
6.82k
            }
504
729
        }
505
2.10k
    }
506
507
38.6k
    if (IS_KEY_OR_INTRA(hdr)) {
508
28.4k
        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&
509
26.3k
                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);
510
28.4k
        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
511
1.61k
            for (int i = 0; i < 8; i++)
512
1.43k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
513
28.4k
        if (c->strict_std_compliance &&
514
0
            hdr->frame_type == DAV1D_FRAME_TYPE_INTRA && hdr->refresh_frame_flags == 0xff)
515
0
        {
516
0
            goto error;
517
0
        }
518
28.4k
        if (read_frame_size(c, gb, 0) < 0) goto error;
519
28.4k
        if (hdr->allow_screen_content_tools && !hdr->super_res.enabled)
520
11.1k
            hdr->allow_intrabc = dav1d_get_bit(gb);
521
28.4k
    } else {
522
10.1k
        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :
523
10.1k
                                   dav1d_get_bits(gb, 8);
524
10.1k
        if (hdr->error_resilient_mode && seqhdr->order_hint)
525
3.11k
            for (int i = 0; i < 8; i++)
526
2.76k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
527
10.1k
        if (seqhdr->order_hint) {
528
4.28k
            hdr->frame_ref_short_signaling = dav1d_get_bit(gb);
529
4.28k
            if (hdr->frame_ref_short_signaling) {
530
1.81k
                hdr->refidx[0] = dav1d_get_bits(gb, 3);
531
1.81k
                hdr->refidx[1] = hdr->refidx[2] = -1;
532
1.81k
                hdr->refidx[3] = dav1d_get_bits(gb, 3);
533
534
                /* +1 allows for unconditional stores, as unused
535
                 * values can be dumped into frame_offset[-1]. */
536
1.81k
                int frame_offset_mem[8+1];
537
1.81k
                int *const frame_offset = &frame_offset_mem[1];
538
1.81k
                int earliest_ref = -1;
539
16.1k
                for (int i = 0, earliest_offset = INT_MAX; i < 8; i++) {
540
14.4k
                    const Dav1dFrameHeader *const refhdr = c->refs[i].p.p.frame_hdr;
541
14.4k
                    if (!refhdr) goto error;
542
14.3k
                    const int diff = get_poc_diff(seqhdr->order_hint_n_bits,
543
14.3k
                                                  refhdr->frame_offset,
544
14.3k
                                                  hdr->frame_offset);
545
14.3k
                    frame_offset[i] = diff;
546
14.3k
                    if (diff < earliest_offset) {
547
2.54k
                        earliest_offset = diff;
548
2.54k
                        earliest_ref = i;
549
2.54k
                    }
550
14.3k
                }
551
1.79k
                frame_offset[hdr->refidx[0]] = INT_MIN; // = reference frame is used
552
1.79k
                frame_offset[hdr->refidx[3]] = INT_MIN;
553
1.79k
                assert(earliest_ref >= 0);
554
555
1.79k
                int refidx = -1;
556
16.1k
                for (int i = 0, latest_offset = 0; i < 8; i++) {
557
14.3k
                    const int hint = frame_offset[i];
558
14.3k
                    if (hint >= latest_offset) {
559
4.89k
                        latest_offset = hint;
560
4.89k
                        refidx = i;
561
4.89k
                    }
562
14.3k
                }
563
1.79k
                frame_offset[refidx] = INT_MIN;
564
1.79k
                hdr->refidx[6] = refidx;
565
566
5.37k
                for (int i = 4; i < 6; i++) {
567
                    /* Unsigned compares to handle negative values. */
568
3.58k
                    unsigned earliest_offset = UINT8_MAX;
569
3.58k
                    refidx = -1;
570
32.2k
                    for (int j = 0; j < 8; j++) {
571
28.6k
                        const unsigned hint = frame_offset[j];
572
28.6k
                        if (hint < earliest_offset) {
573
2.92k
                            earliest_offset = hint;
574
2.92k
                            refidx = j;
575
2.92k
                        }
576
28.6k
                    }
577
3.58k
                    frame_offset[refidx] = INT_MIN;
578
3.58k
                    hdr->refidx[i] = refidx;
579
3.58k
                }
580
581
12.5k
                for (int i = 1; i < 7; i++) {
582
10.7k
                    refidx = hdr->refidx[i];
583
10.7k
                    if (refidx < 0) {
584
5.33k
                        unsigned latest_offset = ~UINT8_MAX;
585
48.0k
                        for (int j = 0; j < 8; j++) {
586
42.6k
                            const unsigned hint = frame_offset[j];
587
42.6k
                            if (hint >= latest_offset) {
588
13.4k
                                latest_offset = hint;
589
13.4k
                                refidx = j;
590
13.4k
                            }
591
42.6k
                        }
592
5.33k
                        frame_offset[refidx] = INT_MIN;
593
5.33k
                        hdr->refidx[i] = refidx >= 0 ? refidx : earliest_ref;
594
5.33k
                    }
595
10.7k
                }
596
1.79k
            }
597
4.28k
        }
598
80.9k
        for (int i = 0; i < 7; i++) {
599
70.8k
            if (!hdr->frame_ref_short_signaling)
600
58.3k
                hdr->refidx[i] = dav1d_get_bits(gb, 3);
601
70.8k
            if (seqhdr->frame_id_numbers_present) {
602
101
                const unsigned delta_ref_frame_id = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits) + 1;
603
101
                const unsigned ref_frame_id = (hdr->frame_id + (1 << seqhdr->frame_id_n_bits) - delta_ref_frame_id) & ((1 << seqhdr->frame_id_n_bits) - 1);
604
101
                Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr;
605
101
                if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error;
606
101
            }
607
70.8k
        }
608
10.1k
        const int use_ref = !hdr->error_resilient_mode &&
609
9.01k
                            hdr->frame_size_override;
610
10.1k
        if (read_frame_size(c, gb, use_ref) < 0) goto error;
611
10.0k
        if (!hdr->force_integer_mv)
612
8.12k
            hdr->hp = dav1d_get_bit(gb);
613
10.0k
        hdr->subpel_filter_mode = dav1d_get_bit(gb) ? DAV1D_FILTER_SWITCHABLE :
614
10.0k
                                                      dav1d_get_bits(gb, 2);
615
10.0k
        hdr->switchable_motion_mode = dav1d_get_bit(gb);
616
10.0k
        if (!hdr->error_resilient_mode && seqhdr->ref_frame_mvs &&
617
3.08k
            seqhdr->order_hint && IS_INTER_OR_SWITCH(hdr))
618
3.08k
        {
619
3.08k
            hdr->use_ref_frame_mvs = dav1d_get_bit(gb);
620
3.08k
        }
621
10.0k
    }
622
#if DEBUG_FRAME_HDR
623
    printf("HDR: post-frametype-specific-bits: off=%td\n",
624
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
625
#endif
626
627
38.4k
    if (!seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update)
628
15.2k
        hdr->refresh_context = !dav1d_get_bit(gb);
629
#if DEBUG_FRAME_HDR
630
    printf("HDR: post-refresh_context: off=%td\n",
631
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
632
#endif
633
634
    // tile data
635
38.4k
    hdr->tiling.uniform = dav1d_get_bit(gb);
636
38.4k
    const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
637
38.4k
    const int sbsz_log2 = 6 + seqhdr->sb128;
638
38.4k
    const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
639
38.4k
    const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
640
38.4k
    const int max_tile_width_sb = 4096 >> sbsz_log2;
641
38.4k
    const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
642
38.4k
    hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
643
38.4k
    hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
644
38.4k
    hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
645
38.4k
    const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
646
38.4k
                              hdr->tiling.min_log2_cols);
647
38.4k
    if (hdr->tiling.uniform) {
648
22.5k
        for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
649
23.4k
             hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bit(gb);
650
22.5k
             hdr->tiling.log2_cols++) ;
651
22.5k
        const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols);
652
22.5k
        hdr->tiling.cols = 0;
653
48.8k
        for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++)
654
26.3k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
655
22.5k
        hdr->tiling.min_log2_rows =
656
22.5k
            imax(min_log2_tiles - hdr->tiling.log2_cols, 0);
657
658
22.5k
        for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows;
659
23.5k
             hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bit(gb);
660
22.5k
             hdr->tiling.log2_rows++) ;
661
22.5k
        const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows);
662
22.5k
        hdr->tiling.rows = 0;
663
47.6k
        for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++)
664
25.1k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
665
22.5k
    } else {
666
15.9k
        hdr->tiling.cols = 0;
667
15.9k
        int widest_tile = 0, max_tile_area_sb = sbw * sbh;
668
39.2k
        for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) {
669
23.3k
            const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb);
670
23.3k
            const int tile_w = (tile_width_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_width_sb) : 1;
671
23.3k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
672
23.3k
            sbx += tile_w;
673
23.3k
            widest_tile = imax(widest_tile, tile_w);
674
23.3k
        }
675
15.9k
        hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
676
15.9k
        if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
677
15.9k
        const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
678
679
15.9k
        hdr->tiling.rows = 0;
680
39.5k
        for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
681
23.6k
            const int tile_height_sb = imin(sbh - sby, max_tile_height_sb);
682
23.6k
            const int tile_h = (tile_height_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_height_sb) : 1;
683
23.6k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
684
23.6k
            sby += tile_h;
685
23.6k
        }
686
15.9k
        hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows);
687
15.9k
    }
688
38.4k
    hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw;
689
38.4k
    hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh;
690
38.4k
    if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) {
691
3.72k
        hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols + hdr->tiling.log2_rows);
692
3.72k
        if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows)
693
22
            goto error;
694
3.70k
        hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1;
695
3.70k
    }
696
#if DEBUG_FRAME_HDR
697
    printf("HDR: post-tiling: off=%td\n",
698
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
699
#endif
700
701
    // quant data
702
38.4k
    hdr->quant.yac = dav1d_get_bits(gb, 8);
703
38.4k
    if (dav1d_get_bit(gb))
704
17.3k
        hdr->quant.ydc_delta = dav1d_get_sbits(gb, 7);
705
38.4k
    if (!seqhdr->monochrome) {
706
        // If the sequence header says that delta_q might be different
707
        // for U, V, we must check whether it actually is for this
708
        // frame.
709
30.3k
        const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bit(gb) : 0;
710
30.3k
        if (dav1d_get_bit(gb))
711
13.8k
            hdr->quant.udc_delta = dav1d_get_sbits(gb, 7);
712
30.3k
        if (dav1d_get_bit(gb))
713
14.3k
            hdr->quant.uac_delta = dav1d_get_sbits(gb, 7);
714
30.3k
        if (diff_uv_delta) {
715
4.54k
            if (dav1d_get_bit(gb))
716
2.69k
                hdr->quant.vdc_delta = dav1d_get_sbits(gb, 7);
717
4.54k
            if (dav1d_get_bit(gb))
718
2.49k
                hdr->quant.vac_delta = dav1d_get_sbits(gb, 7);
719
25.7k
        } else {
720
25.7k
            hdr->quant.vdc_delta = hdr->quant.udc_delta;
721
25.7k
            hdr->quant.vac_delta = hdr->quant.uac_delta;
722
25.7k
        }
723
30.3k
    }
724
#if DEBUG_FRAME_HDR
725
    printf("HDR: post-quant: off=%td\n",
726
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
727
#endif
728
38.4k
    hdr->quant.qm = dav1d_get_bit(gb);
729
38.4k
    if (hdr->quant.qm) {
730
19.4k
        hdr->quant.qm_y = dav1d_get_bits(gb, 4);
731
19.4k
        hdr->quant.qm_u = dav1d_get_bits(gb, 4);
732
19.4k
        hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 4) :
733
19.4k
                                                        hdr->quant.qm_u;
734
19.4k
    }
735
#if DEBUG_FRAME_HDR
736
    printf("HDR: post-qm: off=%td\n",
737
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
738
#endif
739
740
    // segmentation data
741
38.4k
    hdr->segmentation.enabled = dav1d_get_bit(gb);
742
38.4k
    if (hdr->segmentation.enabled) {
743
12.4k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
744
8.83k
            hdr->segmentation.update_map = 1;
745
8.83k
            hdr->segmentation.update_data = 1;
746
8.83k
        } else {
747
3.62k
            hdr->segmentation.update_map = dav1d_get_bit(gb);
748
3.62k
            if (hdr->segmentation.update_map)
749
1.77k
                hdr->segmentation.temporal = dav1d_get_bit(gb);
750
3.62k
            hdr->segmentation.update_data = dav1d_get_bit(gb);
751
3.62k
        }
752
753
12.4k
        if (hdr->segmentation.update_data) {
754
9.80k
            hdr->segmentation.seg_data.last_active_segid = -1;
755
88.2k
            for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
756
78.4k
                Dav1dSegmentationData *const seg =
757
78.4k
                    &hdr->segmentation.seg_data.d[i];
758
78.4k
                if (dav1d_get_bit(gb)) {
759
25.5k
                    seg->delta_q = dav1d_get_sbits(gb, 9);
760
25.5k
                    hdr->segmentation.seg_data.last_active_segid = i;
761
25.5k
                }
762
78.4k
                if (dav1d_get_bit(gb)) {
763
24.2k
                    seg->delta_lf_y_v = dav1d_get_sbits(gb, 7);
764
24.2k
                    hdr->segmentation.seg_data.last_active_segid = i;
765
24.2k
                }
766
78.4k
                if (dav1d_get_bit(gb)) {
767
33.7k
                    seg->delta_lf_y_h = dav1d_get_sbits(gb, 7);
768
33.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
769
33.7k
                }
770
78.4k
                if (dav1d_get_bit(gb)) {
771
32.1k
                    seg->delta_lf_u = dav1d_get_sbits(gb, 7);
772
32.1k
                    hdr->segmentation.seg_data.last_active_segid = i;
773
32.1k
                }
774
78.4k
                if (dav1d_get_bit(gb)) {
775
32.4k
                    seg->delta_lf_v = dav1d_get_sbits(gb, 7);
776
32.4k
                    hdr->segmentation.seg_data.last_active_segid = i;
777
32.4k
                }
778
78.4k
                if (dav1d_get_bit(gb)) {
779
31.2k
                    seg->ref = dav1d_get_bits(gb, 3);
780
31.2k
                    hdr->segmentation.seg_data.last_active_segid = i;
781
31.2k
                    hdr->segmentation.seg_data.preskip = 1;
782
47.1k
                } else {
783
47.1k
                    seg->ref = -1;
784
47.1k
                }
785
78.4k
                if ((seg->skip = dav1d_get_bit(gb))) {
786
27.6k
                    hdr->segmentation.seg_data.last_active_segid = i;
787
27.6k
                    hdr->segmentation.seg_data.preskip = 1;
788
27.6k
                }
789
78.4k
                if ((seg->globalmv = dav1d_get_bit(gb))) {
790
27.4k
                    hdr->segmentation.seg_data.last_active_segid = i;
791
27.4k
                    hdr->segmentation.seg_data.preskip = 1;
792
27.4k
                }
793
78.4k
            }
794
9.80k
        } else {
795
            // segmentation.update_data was false so we should copy
796
            // segmentation data from the reference frame.
797
2.65k
            assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
798
2.65k
            const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
799
2.65k
            if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
800
2.63k
            hdr->segmentation.seg_data =
801
2.63k
                c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data;
802
2.63k
        }
803
25.9k
    } else {
804
233k
        for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++)
805
207k
            hdr->segmentation.seg_data.d[i].ref = -1;
806
25.9k
    }
807
#if DEBUG_FRAME_HDR
808
    printf("HDR: post-segmentation: off=%td\n",
809
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
810
#endif
811
812
    // delta q
813
38.4k
    if (hdr->quant.yac) {
814
36.2k
        hdr->delta.q.present = dav1d_get_bit(gb);
815
36.2k
        if (hdr->delta.q.present) {
816
10.2k
            hdr->delta.q.res_log2 = dav1d_get_bits(gb, 2);
817
10.2k
            if (!hdr->allow_intrabc) {
818
9.10k
                hdr->delta.lf.present = dav1d_get_bit(gb);
819
9.10k
                if (hdr->delta.lf.present) {
820
4.08k
                    hdr->delta.lf.res_log2 = dav1d_get_bits(gb, 2);
821
4.08k
                    hdr->delta.lf.multi = dav1d_get_bit(gb);
822
4.08k
                }
823
9.10k
            }
824
10.2k
        }
825
36.2k
    }
826
#if DEBUG_FRAME_HDR
827
    printf("HDR: post-delta_q_lf_flags: off=%td\n",
828
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
829
#endif
830
831
    // derive lossless flags
832
38.4k
    const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta &&
833
14.7k
        !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta;
834
38.4k
    hdr->all_lossless = 1;
835
345k
    for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
836
307k
        hdr->segmentation.qidx[i] = hdr->segmentation.enabled ?
837
99.4k
            iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) :
838
307k
            hdr->quant.yac;
839
307k
        hdr->segmentation.lossless[i] =
840
307k
            !hdr->segmentation.qidx[i] && delta_lossless;
841
307k
        hdr->all_lossless &= hdr->segmentation.lossless[i];
842
307k
    }
843
844
    // loopfilter
845
38.4k
    if (hdr->all_lossless || hdr->allow_intrabc) {
846
7.65k
        hdr->loopfilter.mode_ref_delta_enabled = 1;
847
7.65k
        hdr->loopfilter.mode_ref_delta_update = 1;
848
7.65k
        hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
849
30.7k
    } else {
850
30.7k
        hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6);
851
30.7k
        hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6);
852
30.7k
        if (!seqhdr->monochrome &&
853
23.5k
            (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1]))
854
20.9k
        {
855
20.9k
            hdr->loopfilter.level_u = dav1d_get_bits(gb, 6);
856
20.9k
            hdr->loopfilter.level_v = dav1d_get_bits(gb, 6);
857
20.9k
        }
858
30.7k
        hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3);
859
860
30.7k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
861
22.1k
            hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
862
22.1k
        } else {
863
8.55k
            const int ref = hdr->refidx[hdr->primary_ref_frame];
864
8.55k
            if (!c->refs[ref].p.p.frame_hdr) goto error;
865
8.52k
            hdr->loopfilter.mode_ref_deltas =
866
8.52k
                c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas;
867
8.52k
        }
868
30.7k
        hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bit(gb);
869
30.7k
        if (hdr->loopfilter.mode_ref_delta_enabled) {
870
9.48k
            hdr->loopfilter.mode_ref_delta_update = dav1d_get_bit(gb);
871
9.48k
            if (hdr->loopfilter.mode_ref_delta_update) {
872
48.9k
                for (int i = 0; i < 8; i++)
873
43.4k
                    if (dav1d_get_bit(gb))
874
22.8k
                        hdr->loopfilter.mode_ref_deltas.ref_delta[i] =
875
22.8k
                            dav1d_get_sbits(gb, 7);
876
16.3k
                for (int i = 0; i < 2; i++)
877
10.8k
                    if (dav1d_get_bit(gb))
878
5.03k
                        hdr->loopfilter.mode_ref_deltas.mode_delta[i] =
879
5.03k
                            dav1d_get_sbits(gb, 7);
880
5.43k
            }
881
9.48k
        }
882
30.7k
    }
883
#if DEBUG_FRAME_HDR
884
    printf("HDR: post-lpf: off=%td\n",
885
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
886
#endif
887
888
    // cdef
889
38.3k
    if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) {
890
12.6k
        hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3;
891
12.6k
        hdr->cdef.n_bits = dav1d_get_bits(gb, 2);
892
43.1k
        for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) {
893
30.4k
            hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6);
894
30.4k
            if (!seqhdr->monochrome)
895
21.6k
                hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6);
896
30.4k
        }
897
12.6k
    }
898
#if DEBUG_FRAME_HDR
899
    printf("HDR: post-cdef: off=%td\n",
900
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
901
#endif
902
903
    // restoration
904
38.3k
    if ((!hdr->all_lossless || hdr->super_res.enabled) &&
905
36.8k
        seqhdr->restoration && !hdr->allow_intrabc)
906
17.9k
    {
907
17.9k
        hdr->restoration.type[0] = dav1d_get_bits(gb, 2);
908
17.9k
        if (!seqhdr->monochrome) {
909
13.3k
            hdr->restoration.type[1] = dav1d_get_bits(gb, 2);
910
13.3k
            hdr->restoration.type[2] = dav1d_get_bits(gb, 2);
911
13.3k
        }
912
913
17.9k
        if (hdr->restoration.type[0] || hdr->restoration.type[1] ||
914
4.97k
            hdr->restoration.type[2])
915
14.0k
        {
916
            // Log2 of the restoration unit size.
917
14.0k
            hdr->restoration.unit_size[0] = 6 + seqhdr->sb128;
918
14.0k
            if (dav1d_get_bit(gb)) {
919
7.09k
                hdr->restoration.unit_size[0]++;
920
7.09k
                if (!seqhdr->sb128)
921
4.52k
                    hdr->restoration.unit_size[0] += dav1d_get_bit(gb);
922
7.09k
            }
923
14.0k
            hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0];
924
14.0k
            if ((hdr->restoration.type[1] || hdr->restoration.type[2]) &&
925
10.6k
                seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1)
926
4.91k
            {
927
4.91k
                hdr->restoration.unit_size[1] -= dav1d_get_bit(gb);
928
4.91k
            }
929
14.0k
        } else {
930
3.90k
            hdr->restoration.unit_size[0] = 8;
931
3.90k
        }
932
17.9k
    }
933
#if DEBUG_FRAME_HDR
934
    printf("HDR: post-restoration: off=%td\n",
935
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
936
#endif
937
938
38.3k
    if (!hdr->all_lossless)
939
36.7k
        hdr->txfm_mode = dav1d_get_bit(gb) ? DAV1D_TX_SWITCHABLE : DAV1D_TX_LARGEST;
940
#if DEBUG_FRAME_HDR
941
    printf("HDR: post-txfmmode: off=%td\n",
942
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
943
#endif
944
38.3k
    if (IS_INTER_OR_SWITCH(hdr))
945
9.96k
        hdr->switchable_comp_refs = dav1d_get_bit(gb);
946
#if DEBUG_FRAME_HDR
947
    printf("HDR: post-refmode: off=%td\n",
948
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
949
#endif
950
38.3k
    if (hdr->switchable_comp_refs && IS_INTER_OR_SWITCH(hdr) && seqhdr->order_hint) {
951
2.49k
        const int poc = hdr->frame_offset;
952
2.49k
        int off_before = -1, off_after = -1;
953
2.49k
        int off_before_idx, off_after_idx;
954
19.8k
        for (int i = 0; i < 7; i++) {
955
17.3k
            if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
956
17.3k
            const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
957
958
17.3k
            const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc);
959
17.3k
            if (diff > 0) {
960
4.10k
                if (off_after < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
961
3.17k
                                                  off_after, refpoc) > 0)
962
1.00k
                {
963
1.00k
                    off_after = refpoc;
964
1.00k
                    off_after_idx = i;
965
1.00k
                }
966
13.2k
            } else if (diff < 0 && (off_before < 0 ||
967
8.18k
                                    get_poc_diff(seqhdr->order_hint_n_bits,
968
8.18k
                                                 refpoc, off_before) > 0))
969
2.13k
            {
970
2.13k
                off_before = refpoc;
971
2.13k
                off_before_idx = i;
972
2.13k
            }
973
17.3k
        }
974
975
2.48k
        if ((off_before | off_after) >= 0) {
976
472
            hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx);
977
472
            hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx);
978
472
            hdr->skip_mode_allowed = 1;
979
2.00k
        } else if (off_before >= 0) {
980
1.42k
            int off_before2 = -1;
981
1.42k
            int off_before2_idx;
982
11.3k
            for (int i = 0; i < 7; i++) {
983
9.94k
                if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
984
9.94k
                const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
985
9.94k
                if (get_poc_diff(seqhdr->order_hint_n_bits,
986
9.94k
                                 refpoc, off_before) < 0) {
987
1.23k
                    if (off_before2 < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
988
842
                                                        refpoc, off_before2) > 0)
989
415
                    {
990
415
                        off_before2 = refpoc;
991
415
                        off_before2_idx = i;
992
415
                    }
993
1.23k
                }
994
9.94k
            }
995
996
1.42k
            if (off_before2 >= 0) {
997
394
                hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx);
998
394
                hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx);
999
394
                hdr->skip_mode_allowed = 1;
1000
394
            }
1001
1.42k
        }
1002
2.48k
    }
1003
38.3k
    if (hdr->skip_mode_allowed)
1004
866
        hdr->skip_mode_enabled = dav1d_get_bit(gb);
1005
#if DEBUG_FRAME_HDR
1006
    printf("HDR: post-extskip: off=%td\n",
1007
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1008
#endif
1009
38.3k
    if (!hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) && seqhdr->warped_motion)
1010
4.72k
        hdr->warp_motion = dav1d_get_bit(gb);
1011
#if DEBUG_FRAME_HDR
1012
    printf("HDR: post-warpmotionbit: off=%td\n",
1013
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1014
#endif
1015
38.3k
    hdr->reduced_txtp_set = dav1d_get_bit(gb);
1016
#if DEBUG_FRAME_HDR
1017
    printf("HDR: post-reducedtxtpset: off=%td\n",
1018
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1019
#endif
1020
1021
306k
    for (int i = 0; i < 7; i++)
1022
268k
        hdr->gmv[i] = dav1d_default_wm_params;
1023
1024
38.3k
    if (IS_INTER_OR_SWITCH(hdr)) {
1025
79.5k
        for (int i = 0; i < 7; i++) {
1026
69.6k
            hdr->gmv[i].type = !dav1d_get_bit(gb) ? DAV1D_WM_TYPE_IDENTITY :
1027
69.6k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_ROT_ZOOM :
1028
24.3k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_TRANSLATION :
1029
10.0k
                                                    DAV1D_WM_TYPE_AFFINE;
1030
1031
69.6k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue;
1032
1033
24.3k
            const Dav1dWarpedMotionParams *ref_gmv;
1034
24.3k
            if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
1035
2.90k
                ref_gmv = &dav1d_default_wm_params;
1036
21.4k
            } else {
1037
21.4k
                const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
1038
21.4k
                if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
1039
21.4k
                ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i];
1040
21.4k
            }
1041
24.3k
            int32_t *const mat = hdr->gmv[i].matrix;
1042
24.3k
            const int32_t *const ref_mat = ref_gmv->matrix;
1043
24.3k
            int bits, shift;
1044
1045
24.3k
            if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) {
1046
21.9k
                mat[2] = (1 << 16) + 2 *
1047
21.9k
                    dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12);
1048
21.9k
                mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12);
1049
1050
21.9k
                bits = 12;
1051
21.9k
                shift = 10;
1052
21.9k
            } else {
1053
2.38k
                bits = 9 - !hdr->hp;
1054
2.38k
                shift = 13 + !hdr->hp;
1055
2.38k
            }
1056
1057
24.3k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) {
1058
7.65k
                mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12);
1059
7.65k
                mat[5] = (1 << 16) + 2 *
1060
7.65k
                    dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12);
1061
16.7k
            } else {
1062
16.7k
                mat[4] = -mat[3];
1063
16.7k
                mat[5] = mat[2];
1064
16.7k
            }
1065
1066
24.3k
            mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift);
1067
24.3k
            mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift);
1068
24.3k
        }
1069
9.95k
    }
1070
#if DEBUG_FRAME_HDR
1071
    printf("HDR: post-gmv: off=%td\n",
1072
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1073
#endif
1074
1075
38.3k
    if (seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame)) {
1076
7.01k
        hdr->film_grain.present = dav1d_get_bit(gb);
1077
7.01k
        if (hdr->film_grain.present) {
1078
2.39k
            const unsigned seed = dav1d_get_bits(gb, 16);
1079
2.39k
            hdr->film_grain.update = hdr->frame_type != DAV1D_FRAME_TYPE_INTER || dav1d_get_bit(gb);
1080
2.39k
            if (!hdr->film_grain.update) {
1081
183
                const int refidx = dav1d_get_bits(gb, 3);
1082
183
                int i;
1083
690
                for (i = 0; i < 7; i++)
1084
674
                    if (hdr->refidx[i] == refidx)
1085
167
                        break;
1086
183
                if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error;
1087
155
                hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data;
1088
155
                hdr->film_grain.data.seed = seed;
1089
2.21k
            } else {
1090
2.21k
                Dav1dFilmGrainData *const fgd = &hdr->film_grain.data;
1091
2.21k
                fgd->seed = seed;
1092
1093
2.21k
                fgd->num_y_points = dav1d_get_bits(gb, 4);
1094
2.21k
                if (fgd->num_y_points > 14) goto error;
1095
4.52k
                for (int i = 0; i < fgd->num_y_points; i++) {
1096
2.37k
                    fgd->y_points[i][0] = dav1d_get_bits(gb, 8);
1097
2.37k
                    if (i && fgd->y_points[i - 1][0] >= fgd->y_points[i][0])
1098
40
                        goto error;
1099
2.33k
                    fgd->y_points[i][1] = dav1d_get_bits(gb, 8);
1100
2.33k
                }
1101
1102
2.15k
                if (!seqhdr->monochrome)
1103
1.87k
                    fgd->chroma_scaling_from_luma = dav1d_get_bit(gb);
1104
2.15k
                if (seqhdr->monochrome || fgd->chroma_scaling_from_luma ||
1105
930
                    (seqhdr->ss_ver == 1 && seqhdr->ss_hor == 1 && !fgd->num_y_points))
1106
1.25k
                {
1107
1.25k
                    fgd->num_uv_points[0] = fgd->num_uv_points[1] = 0;
1108
2.63k
                } else for (int pl = 0; pl < 2; pl++) {
1109
1.77k
                    fgd->num_uv_points[pl] = dav1d_get_bits(gb, 4);
1110
1.77k
                    if (fgd->num_uv_points[pl] > 10) goto error;
1111
3.00k
                    for (int i = 0; i < fgd->num_uv_points[pl]; i++) {
1112
1.26k
                        fgd->uv_points[pl][i][0] = dav1d_get_bits(gb, 8);
1113
1.26k
                        if (i && fgd->uv_points[pl][i - 1][0] >= fgd->uv_points[pl][i][0])
1114
19
                            goto error;
1115
1.25k
                        fgd->uv_points[pl][i][1] = dav1d_get_bits(gb, 8);
1116
1.25k
                    }
1117
1.75k
                }
1118
1119
2.11k
                if (seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1 &&
1120
651
                    !!fgd->num_uv_points[0] != !!fgd->num_uv_points[1])
1121
10
                {
1122
10
                    goto error;
1123
10
                }
1124
1125
2.10k
                fgd->scaling_shift = dav1d_get_bits(gb, 2) + 8;
1126
2.10k
                fgd->ar_coeff_lag = dav1d_get_bits(gb, 2);
1127
2.10k
                const int num_y_pos = 2 * fgd->ar_coeff_lag * (fgd->ar_coeff_lag + 1);
1128
2.10k
                if (fgd->num_y_points)
1129
14.5k
                    for (int i = 0; i < num_y_pos; i++)
1130
13.1k
                        fgd->ar_coeffs_y[i] = dav1d_get_bits(gb, 8) - 128;
1131
6.32k
                for (int pl = 0; pl < 2; pl++)
1132
4.21k
                    if (fgd->num_uv_points[pl] || fgd->chroma_scaling_from_luma) {
1133
2.64k
                        const int num_uv_pos = num_y_pos + !!fgd->num_y_points;
1134
29.7k
                        for (int i = 0; i < num_uv_pos; i++)
1135
27.1k
                            fgd->ar_coeffs_uv[pl][i] = dav1d_get_bits(gb, 8) - 128;
1136
2.64k
                        if (!fgd->num_y_points)
1137
778
                            fgd->ar_coeffs_uv[pl][num_uv_pos] = 0;
1138
2.64k
                    }
1139
2.10k
                fgd->ar_coeff_shift = dav1d_get_bits(gb, 2) + 6;
1140
2.10k
                fgd->grain_scale_shift = dav1d_get_bits(gb, 2);
1141
6.32k
                for (int pl = 0; pl < 2; pl++)
1142
4.21k
                    if (fgd->num_uv_points[pl]) {
1143
766
                        fgd->uv_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1144
766
                        fgd->uv_luma_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1145
766
                        fgd->uv_offset[pl] = dav1d_get_bits(gb, 9) - 256;
1146
766
                    }
1147
2.10k
                fgd->overlap_flag = dav1d_get_bit(gb);
1148
2.10k
                fgd->clip_to_restricted_range = dav1d_get_bit(gb);
1149
2.10k
            }
1150
2.39k
        }
1151
7.01k
    }
1152
#if DEBUG_FRAME_HDR
1153
    printf("HDR: post-filmgrain: off=%td\n",
1154
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
1155
#endif
1156
1157
38.2k
    return 0;
1158
1159
461
error:
1160
461
    dav1d_log(c, "Error parsing frame header\n");
1161
461
    return DAV1D_ERR(EINVAL);
1162
38.3k
}
1163
1164
37.0k
static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) {
1165
37.0k
    const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows;
1166
37.0k
    const int have_tile_pos = n_tiles > 1 ? dav1d_get_bit(gb) : 0;
1167
1168
37.0k
    if (have_tile_pos) {
1169
164
        const int n_bits = c->frame_hdr->tiling.log2_cols +
1170
164
                           c->frame_hdr->tiling.log2_rows;
1171
164
        c->tile[c->n_tile_data].start = dav1d_get_bits(gb, n_bits);
1172
164
        c->tile[c->n_tile_data].end = dav1d_get_bits(gb, n_bits);
1173
36.8k
    } else {
1174
36.8k
        c->tile[c->n_tile_data].start = 0;
1175
36.8k
        c->tile[c->n_tile_data].end = n_tiles - 1;
1176
36.8k
    }
1177
37.0k
}
1178
1179
90.3k
ptrdiff_t dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in) {
1180
90.3k
    GetBits gb;
1181
90.3k
    int res;
1182
1183
90.3k
    dav1d_init_get_bits(&gb, in->data, in->sz);
1184
1185
    // obu header
1186
90.3k
    const int obu_forbidden_bit = dav1d_get_bit(&gb);
1187
90.3k
    if (c->strict_std_compliance && obu_forbidden_bit) goto error;
1188
90.3k
    const enum Dav1dObuType type = dav1d_get_bits(&gb, 4);
1189
90.3k
    const int has_extension = dav1d_get_bit(&gb);
1190
90.3k
    const int has_length_field = dav1d_get_bit(&gb);
1191
90.3k
    dav1d_get_bit(&gb); // reserved
1192
1193
90.3k
    int temporal_id = 0, spatial_id = 0;
1194
90.3k
    if (has_extension) {
1195
22.3k
        temporal_id = dav1d_get_bits(&gb, 3);
1196
22.3k
        spatial_id = dav1d_get_bits(&gb, 2);
1197
22.3k
        dav1d_get_bits(&gb, 3); // reserved
1198
22.3k
    }
1199
1200
90.3k
    if (has_length_field) {
1201
90.3k
        const size_t len = dav1d_get_uleb128(&gb);
1202
90.3k
        if (len > (size_t)(gb.ptr_end - gb.ptr)) goto error;
1203
90.3k
        gb.ptr_end = gb.ptr + len;
1204
90.3k
    }
1205
90.3k
    if (gb.error) goto error;
1206
1207
    // We must have read a whole number of bytes at this point (1 byte
1208
    // for the header and whole bytes at a time when reading the
1209
    // leb128 length field).
1210
90.3k
    assert(gb.bits_left == 0);
1211
1212
    // skip obu not belonging to the selected temporal/spatial layer
1213
90.3k
    if (type != DAV1D_OBU_SEQ_HDR && type != DAV1D_OBU_TD &&
1214
54.4k
        has_extension && c->operating_point_idc != 0)
1215
2.66k
    {
1216
2.66k
        const int in_temporal_layer = (c->operating_point_idc >> temporal_id) & 1;
1217
2.66k
        const int in_spatial_layer = (c->operating_point_idc >> (spatial_id + 8)) & 1;
1218
2.66k
        if (!in_temporal_layer || !in_spatial_layer)
1219
1.99k
            return gb.ptr_end - gb.ptr_start;
1220
2.66k
    }
1221
1222
88.3k
    switch (type) {
1223
28.0k
    case DAV1D_OBU_SEQ_HDR: {
1224
28.0k
        Dav1dRef *ref = dav1d_ref_create_using_pool(c->seq_hdr_pool,
1225
28.0k
                                                    sizeof(Dav1dSequenceHeader));
1226
28.0k
        if (!ref) return DAV1D_ERR(ENOMEM);
1227
28.0k
        Dav1dSequenceHeader *seq_hdr = ref->data;
1228
28.0k
        if ((res = parse_seq_hdr(seq_hdr, &gb, c->strict_std_compliance)) < 0) {
1229
282
            dav1d_log(c, "Error parsing sequence header\n");
1230
282
            dav1d_ref_dec(&ref);
1231
282
            goto error;
1232
282
        }
1233
1234
27.7k
        const int op_idx =
1235
27.7k
            c->operating_point < seq_hdr->num_operating_points ? c->operating_point : 0;
1236
27.7k
        c->operating_point_idc = seq_hdr->operating_points[op_idx].idc;
1237
27.7k
        const unsigned spatial_mask = c->operating_point_idc >> 8;
1238
27.7k
        c->max_spatial_id = spatial_mask ? ulog2(spatial_mask) : 0;
1239
1240
        // If we have read a sequence header which is different from
1241
        // the old one, this is a new video sequence and can't use any
1242
        // previous state. Free that state.
1243
1244
27.7k
        if (!c->seq_hdr) {
1245
26.0k
            c->frame_hdr = NULL;
1246
26.0k
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1247
        // see 7.5, operating_parameter_info is allowed to change in
1248
        // sequence headers of a single sequence
1249
26.0k
        } else if (memcmp(seq_hdr, c->seq_hdr, offsetof(Dav1dSequenceHeader, operating_parameter_info))) {
1250
1.69k
            c->frame_hdr = NULL;
1251
1.69k
            c->mastering_display = NULL;
1252
1.69k
            c->content_light = NULL;
1253
1.69k
            dav1d_ref_dec(&c->mastering_display_ref);
1254
1.69k
            dav1d_ref_dec(&c->content_light_ref);
1255
15.2k
            for (int i = 0; i < 8; i++) {
1256
13.5k
                if (c->refs[i].p.p.frame_hdr)
1257
1.41k
                    dav1d_thread_picture_unref(&c->refs[i].p);
1258
13.5k
                dav1d_ref_dec(&c->refs[i].segmap);
1259
13.5k
                dav1d_ref_dec(&c->refs[i].refmvs);
1260
13.5k
                dav1d_cdf_thread_unref(&c->cdf[i]);
1261
13.5k
            }
1262
1.69k
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1263
        // If operating_parameter_info changed, signal it
1264
1.69k
        } else if (memcmp(seq_hdr->operating_parameter_info, c->seq_hdr->operating_parameter_info,
1265
71
                          sizeof(seq_hdr->operating_parameter_info)))
1266
0
        {
1267
0
            c->frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO;
1268
0
        }
1269
27.7k
        dav1d_ref_dec(&c->seq_hdr_ref);
1270
27.7k
        c->seq_hdr_ref = ref;
1271
27.7k
        c->seq_hdr = seq_hdr;
1272
27.7k
        break;
1273
28.0k
    }
1274
488
    case DAV1D_OBU_REDUNDANT_FRAME_HDR:
1275
488
        if (c->frame_hdr) break;
1276
        // fall-through
1277
37.6k
    case DAV1D_OBU_FRAME:
1278
38.9k
    case DAV1D_OBU_FRAME_HDR:
1279
38.9k
        if (!c->seq_hdr) goto error;
1280
38.8k
        if (!c->frame_hdr_ref) {
1281
37.8k
            c->frame_hdr_ref = dav1d_ref_create_using_pool(c->frame_hdr_pool,
1282
37.8k
                                                           sizeof(Dav1dFrameHeader));
1283
37.8k
            if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM);
1284
37.8k
        }
1285
#ifndef NDEBUG
1286
        // ensure that the reference is writable
1287
        assert(dav1d_ref_is_writable(c->frame_hdr_ref));
1288
#endif
1289
38.8k
        c->frame_hdr = c->frame_hdr_ref->data;
1290
38.8k
        memset(c->frame_hdr, 0, sizeof(*c->frame_hdr));
1291
38.8k
        c->frame_hdr->temporal_id = temporal_id;
1292
38.8k
        c->frame_hdr->spatial_id = spatial_id;
1293
38.8k
        if ((res = parse_frame_hdr(c, &gb)) < 0) {
1294
461
            c->frame_hdr = NULL;
1295
461
            goto error;
1296
461
        }
1297
38.4k
        for (int n = 0; n < c->n_tile_data; n++)
1298
31
            dav1d_data_unref_internal(&c->tile[n].data);
1299
38.4k
        c->n_tile_data = 0;
1300
38.4k
        c->n_tiles = 0;
1301
38.4k
        if (type != DAV1D_OBU_FRAME) {
1302
            // This is actually a frame header OBU so read the
1303
            // trailing bit and check for overrun.
1304
1.56k
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1305
92
                c->frame_hdr = NULL;
1306
92
                goto error;
1307
92
            }
1308
1.56k
        }
1309
1310
38.3k
        if (c->frame_size_limit && (int64_t)c->frame_hdr->width[1] *
1311
38.3k
            c->frame_hdr->height > c->frame_size_limit)
1312
0
        {
1313
0
            dav1d_log(c, "Frame size %dx%d exceeds limit %u\n", c->frame_hdr->width[1],
1314
0
                      c->frame_hdr->height, c->frame_size_limit);
1315
0
            c->frame_hdr = NULL;
1316
0
            return DAV1D_ERR(ERANGE);
1317
0
        }
1318
1319
38.3k
        if (type != DAV1D_OBU_FRAME)
1320
1.47k
            break;
1321
        // OBU_FRAMEs shouldn't be signaled with show_existing_frame
1322
36.8k
        if (c->frame_hdr->show_existing_frame) {
1323
30
            c->frame_hdr = NULL;
1324
30
            goto error;
1325
30
        }
1326
1327
        // This is the frame header at the start of a frame OBU.
1328
        // There's no trailing bit at the end to skip, but we do need
1329
        // to align to the next byte.
1330
36.8k
        dav1d_bytealign_get_bits(&gb);
1331
        // fall-through
1332
37.1k
    case DAV1D_OBU_TILE_GRP: {
1333
37.1k
        if (!c->frame_hdr) goto error;
1334
37.0k
        if (c->n_tile_data_alloc < c->n_tile_data + 1) {
1335
25.1k
            if ((c->n_tile_data + 1) > INT_MAX / (int)sizeof(*c->tile)) goto error;
1336
25.1k
            struct Dav1dTileGroup *tile = dav1d_realloc(ALLOC_TILE, c->tile,
1337
25.1k
                                                        (c->n_tile_data + 1) * sizeof(*c->tile));
1338
25.1k
            if (!tile) goto error;
1339
25.1k
            c->tile = tile;
1340
25.1k
            memset(c->tile + c->n_tile_data, 0, sizeof(*c->tile));
1341
25.1k
            c->n_tile_data_alloc = c->n_tile_data + 1;
1342
25.1k
        }
1343
37.0k
        parse_tile_hdr(c, &gb);
1344
        // Align to the next byte boundary and check for overrun.
1345
37.0k
        dav1d_bytealign_get_bits(&gb);
1346
37.0k
        if (gb.error) goto error;
1347
1348
36.8k
        dav1d_data_ref(&c->tile[c->n_tile_data].data, in);
1349
36.8k
        c->tile[c->n_tile_data].data.data = gb.ptr;
1350
36.8k
        c->tile[c->n_tile_data].data.sz = (size_t)(gb.ptr_end - gb.ptr);
1351
        // ensure tile groups are in order and sane, see 6.10.1
1352
36.8k
        if (c->tile[c->n_tile_data].start > c->tile[c->n_tile_data].end ||
1353
36.8k
            c->tile[c->n_tile_data].start != c->n_tiles)
1354
35
        {
1355
76
            for (int i = 0; i <= c->n_tile_data; i++)
1356
41
                dav1d_data_unref_internal(&c->tile[i].data);
1357
35
            c->n_tile_data = 0;
1358
35
            c->n_tiles = 0;
1359
35
            goto error;
1360
35
        }
1361
36.7k
        c->n_tiles += 1 + c->tile[c->n_tile_data].end -
1362
36.7k
                          c->tile[c->n_tile_data].start;
1363
36.7k
        c->n_tile_data++;
1364
36.7k
        break;
1365
36.8k
    }
1366
915
    case DAV1D_OBU_METADATA: {
1367
915
#define DEBUG_OBU_METADATA 0
1368
#if DEBUG_OBU_METADATA
1369
        const uint8_t *const init_ptr = gb.ptr;
1370
#endif
1371
        // obu metadta type field
1372
915
        const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb);
1373
915
        if (gb.error) goto error;
1374
1375
841
        switch (meta_type) {
1376
63
        case OBU_META_HDR_CLL: {
1377
63
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1378
63
                                             sizeof(Dav1dContentLightLevel));
1379
63
            if (!ref) return DAV1D_ERR(ENOMEM);
1380
63
            Dav1dContentLightLevel *const content_light = ref->data;
1381
1382
63
            content_light->max_content_light_level = dav1d_get_bits(&gb, 16);
1383
#if DEBUG_OBU_METADATA
1384
            printf("CLLOBU: max-content-light-level: %d [off=%td]\n",
1385
                   content_light->max_content_light_level,
1386
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1387
#endif
1388
63
            content_light->max_frame_average_light_level = dav1d_get_bits(&gb, 16);
1389
#if DEBUG_OBU_METADATA
1390
            printf("CLLOBU: max-frame-average-light-level: %d [off=%td]\n",
1391
                   content_light->max_frame_average_light_level,
1392
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1393
#endif
1394
1395
63
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1396
10
                dav1d_ref_dec(&ref);
1397
10
                goto error;
1398
10
            }
1399
1400
53
            dav1d_ref_dec(&c->content_light_ref);
1401
53
            c->content_light = content_light;
1402
53
            c->content_light_ref = ref;
1403
53
            break;
1404
63
        }
1405
40
        case OBU_META_HDR_MDCV: {
1406
40
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1407
40
                                             sizeof(Dav1dMasteringDisplay));
1408
40
            if (!ref) return DAV1D_ERR(ENOMEM);
1409
40
            Dav1dMasteringDisplay *const mastering_display = ref->data;
1410
1411
160
            for (int i = 0; i < 3; i++) {
1412
120
                mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16);
1413
120
                mastering_display->primaries[i][1] = dav1d_get_bits(&gb, 16);
1414
#if DEBUG_OBU_METADATA
1415
                printf("MDCVOBU: primaries[%d]: (%d, %d) [off=%td]\n", i,
1416
                       mastering_display->primaries[i][0],
1417
                       mastering_display->primaries[i][1],
1418
                       (gb.ptr - init_ptr) * 8 - gb.bits_left);
1419
#endif
1420
120
            }
1421
40
            mastering_display->white_point[0] = dav1d_get_bits(&gb, 16);
1422
#if DEBUG_OBU_METADATA
1423
            printf("MDCVOBU: white-point-x: %d [off=%td]\n",
1424
                   mastering_display->white_point[0],
1425
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1426
#endif
1427
40
            mastering_display->white_point[1] = dav1d_get_bits(&gb, 16);
1428
#if DEBUG_OBU_METADATA
1429
            printf("MDCVOBU: white-point-y: %d [off=%td]\n",
1430
                   mastering_display->white_point[1],
1431
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1432
#endif
1433
40
            mastering_display->max_luminance = dav1d_get_bits(&gb, 32);
1434
#if DEBUG_OBU_METADATA
1435
            printf("MDCVOBU: max-luminance: %d [off=%td]\n",
1436
                   mastering_display->max_luminance,
1437
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1438
#endif
1439
40
            mastering_display->min_luminance = dav1d_get_bits(&gb, 32);
1440
#if DEBUG_OBU_METADATA
1441
            printf("MDCVOBU: min-luminance: %d [off=%td]\n",
1442
                   mastering_display->min_luminance,
1443
                   (gb.ptr - init_ptr) * 8 - gb.bits_left);
1444
#endif
1445
40
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1446
6
                dav1d_ref_dec(&ref);
1447
6
                goto error;
1448
6
            }
1449
1450
34
            dav1d_ref_dec(&c->mastering_display_ref);
1451
34
            c->mastering_display = mastering_display;
1452
34
            c->mastering_display_ref = ref;
1453
34
            break;
1454
40
        }
1455
85
        case OBU_META_ITUT_T35: {
1456
85
            ptrdiff_t payload_size = gb.ptr_end - gb.ptr;
1457
            // Don't take into account all the trailing bits for payload_size
1458
927
            while (payload_size > 0 && !gb.ptr[payload_size - 1])
1459
842
                payload_size--; // trailing_zero_bit x 8
1460
85
            payload_size--; // trailing_one_bit + trailing_zero_bit x 7
1461
1462
85
            int country_code_extension_byte = 0;
1463
85
            const int country_code = dav1d_get_bits(&gb, 8);
1464
85
            payload_size--;
1465
85
            if (country_code == 0xFF) {
1466
18
                country_code_extension_byte = dav1d_get_bits(&gb, 8);
1467
18
                payload_size--;
1468
18
            }
1469
1470
85
            if (payload_size <= 0 || gb.ptr[payload_size] != 0x80) {
1471
69
                dav1d_log(c, "Malformed ITU-T T.35 metadata message format\n");
1472
69
                break;
1473
69
            }
1474
1475
16
            if ((c->n_itut_t35 + 1) > INT_MAX / (int)sizeof(*c->itut_t35)) goto error;
1476
16
            uint8_t *const payload = dav1d_malloc(ALLOC_OBU_META, payload_size);
1477
16
            if (!payload) goto error;
1478
1479
16
            struct itut_t35_ctx_context *itut_t35_ctx = NULL;
1480
16
            if (!c->n_itut_t35) {
1481
16
                assert(!c->itut_t35_ref);
1482
16
                itut_t35_ctx = dav1d_malloc(ALLOC_OBU_META, sizeof(struct itut_t35_ctx_context));
1483
16
                if (!itut_t35_ctx) {
1484
0
                    dav1d_free(payload);
1485
0
                    goto error;
1486
0
                }
1487
16
            }
1488
1489
16
            struct Dav1dITUTT35 *itut_t35 = dav1d_realloc(ALLOC_OBU_META, c->itut_t35,
1490
16
                                                          (c->n_itut_t35 + 1) * sizeof(*c->itut_t35));
1491
16
            if (!itut_t35) {
1492
0
                dav1d_free(itut_t35_ctx);
1493
0
                dav1d_free(payload);
1494
0
                goto error;
1495
0
            }
1496
16
            c->itut_t35 = itut_t35;
1497
16
            memset(c->itut_t35 + c->n_itut_t35, 0, sizeof(*c->itut_t35));
1498
1499
16
            if (!c->n_itut_t35) {
1500
16
                c->itut_t35_ref = dav1d_ref_init(&itut_t35_ctx->ref, c->itut_t35,
1501
16
                                                 dav1d_picture_free_itut_t35, itut_t35_ctx, 0);
1502
16
            } else {
1503
0
                assert(c->itut_t35_ref &&
1504
0
                       atomic_load_explicit(&c->itut_t35_ref->ref_cnt, memory_order_relaxed) == 1);
1505
0
                itut_t35_ctx = c->itut_t35_ref->user_data;
1506
0
                c->itut_t35_ref->const_data = (uint8_t *)c->itut_t35;
1507
0
            }
1508
16
            itut_t35_ctx->itut_t35 = c->itut_t35;
1509
16
            itut_t35_ctx->n_itut_t35 = c->n_itut_t35 + 1;
1510
1511
16
            Dav1dITUTT35 *const itut_t35_metadata = &c->itut_t35[c->n_itut_t35];
1512
16
            itut_t35_metadata->payload = payload;
1513
1514
16
            itut_t35_metadata->country_code = country_code;
1515
16
            itut_t35_metadata->country_code_extension_byte = country_code_extension_byte;
1516
16
            itut_t35_metadata->payload_size = payload_size;
1517
1518
            // We know that we've read a whole number of bytes and that the
1519
            // payload is within the OBU boundaries, so just use memcpy()
1520
16
            assert(gb.bits_left == 0);
1521
16
            memcpy(itut_t35_metadata->payload, gb.ptr, payload_size);
1522
1523
16
            c->n_itut_t35++;
1524
16
            break;
1525
16
        }
1526
22
        case OBU_META_SCALABILITY:
1527
34
        case OBU_META_TIMECODE:
1528
            // ignore metadata OBUs we don't care about
1529
34
            break;
1530
619
        default:
1531
            // print a warning but don't fail for unknown types
1532
619
            if (meta_type > 31) // Types 6 to 31 are "Unregistered user private", so ignore them.
1533
577
                dav1d_log(c, "Unknown Metadata OBU type %d\n", meta_type);
1534
619
            break;
1535
841
        }
1536
1537
825
        break;
1538
841
    }
1539
7.76k
    case DAV1D_OBU_TD:
1540
7.76k
        c->frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT;
1541
7.76k
        break;
1542
521
    case DAV1D_OBU_PADDING:
1543
        // ignore OBUs we don't care about
1544
521
        break;
1545
11.7k
    default:
1546
        // print a warning but don't fail for unknown types
1547
11.7k
        dav1d_log(c, "Unknown OBU type %d of size %td\n", type, gb.ptr_end - gb.ptr);
1548
11.7k
        break;
1549
88.3k
    }
1550
1551
86.9k
    if (c->seq_hdr && c->frame_hdr) {
1552
39.5k
        if (c->frame_hdr->show_existing_frame) {
1553
171
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr) goto error;
1554
160
            switch (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type) {
1555
31
            case DAV1D_FRAME_TYPE_INTER:
1556
34
            case DAV1D_FRAME_TYPE_SWITCH:
1557
34
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE)
1558
0
                    goto skip;
1559
34
                break;
1560
34
            case DAV1D_FRAME_TYPE_INTRA:
1561
23
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA)
1562
0
                    goto skip;
1563
                // fall-through
1564
126
            default:
1565
126
                break;
1566
160
            }
1567
160
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.data[0]) goto error;
1568
160
            if (c->strict_std_compliance &&
1569
0
                !c->refs[c->frame_hdr->existing_frame_idx].p.showable)
1570
0
            {
1571
0
                goto error;
1572
0
            }
1573
160
            if (c->n_fc == 1) {
1574
0
                dav1d_thread_picture_ref(&c->out,
1575
0
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1576
0
                dav1d_picture_copy_props(&c->out.p,
1577
0
                                         c->content_light, c->content_light_ref,
1578
0
                                         c->mastering_display, c->mastering_display_ref,
1579
0
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1580
0
                                         &in->m);
1581
                // Must be removed from the context after being attached to the frame
1582
0
                dav1d_ref_dec(&c->itut_t35_ref);
1583
0
                c->itut_t35 = NULL;
1584
0
                c->n_itut_t35 = 0;
1585
0
                c->event_flags |= dav1d_picture_get_event_flags(&c->refs[c->frame_hdr->existing_frame_idx].p);
1586
160
            } else {
1587
160
                pthread_mutex_lock(&c->task_thread.lock);
1588
                // need to append this to the frame output queue
1589
160
                const unsigned next = c->frame_thread.next++;
1590
160
                if (c->frame_thread.next == c->n_fc)
1591
9
                    c->frame_thread.next = 0;
1592
1593
160
                Dav1dFrameContext *const f = &c->fc[next];
1594
163
                while (f->n_tile_data > 0)
1595
3
                    pthread_cond_wait(&f->task_thread.cond,
1596
3
                                      &f->task_thread.ttd->lock);
1597
160
                Dav1dThreadPicture *const out_delayed =
1598
160
                    &c->frame_thread.out_delayed[next];
1599
160
                if (out_delayed->p.data[0] ||
1600
160
                    atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
1601
3
                {
1602
3
                    unsigned first = atomic_load_explicit(&c->task_thread.first,
1603
3
                                                          memory_order_relaxed);
1604
3
                    if (first + 1U < c->n_fc)
1605
3
                        atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed);
1606
0
                    else
1607
3
                        atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed);
1608
3
                    atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur,
1609
3
                                                            &first, UINT_MAX,
1610
3
                                                            memory_order_relaxed,
1611
3
                                                            memory_order_relaxed);
1612
3
                    if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
1613
1
                        c->task_thread.cur--;
1614
3
                }
1615
160
                const int error = f->task_thread.retval;
1616
160
                if (error) {
1617
0
                    c->cached_error = error;
1618
0
                    f->task_thread.retval = 0;
1619
0
                    dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
1620
0
                    dav1d_thread_picture_unref(out_delayed);
1621
160
                } else if (out_delayed->p.data[0]) {
1622
3
                    const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
1623
3
                                                                   memory_order_relaxed);
1624
3
                    if ((out_delayed->visible || c->output_invisible_frames) &&
1625
0
                        progress != FRAME_ERROR)
1626
0
                    {
1627
0
                        dav1d_thread_picture_ref(&c->out, out_delayed);
1628
0
                        c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
1629
0
                    }
1630
3
                    dav1d_thread_picture_unref(out_delayed);
1631
3
                }
1632
160
                dav1d_thread_picture_ref(out_delayed,
1633
160
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1634
160
                out_delayed->visible = 1;
1635
160
                dav1d_picture_copy_props(&out_delayed->p,
1636
160
                                         c->content_light, c->content_light_ref,
1637
160
                                         c->mastering_display, c->mastering_display_ref,
1638
160
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1639
160
                                         &in->m);
1640
                // Must be removed from the context after being attached to the frame
1641
160
                dav1d_ref_dec(&c->itut_t35_ref);
1642
160
                c->itut_t35 = NULL;
1643
160
                c->n_itut_t35 = 0;
1644
1645
160
                pthread_mutex_unlock(&c->task_thread.lock);
1646
160
            }
1647
160
            if (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY) {
1648
103
                const int r = c->frame_hdr->existing_frame_idx;
1649
103
                c->refs[r].p.showable = 0;
1650
927
                for (int i = 0; i < 8; i++) {
1651
824
                    if (i == r) continue;
1652
1653
721
                    if (c->refs[i].p.p.frame_hdr)
1654
583
                        dav1d_thread_picture_unref(&c->refs[i].p);
1655
721
                    dav1d_thread_picture_ref(&c->refs[i].p, &c->refs[r].p);
1656
1657
721
                    dav1d_cdf_thread_unref(&c->cdf[i]);
1658
721
                    dav1d_cdf_thread_ref(&c->cdf[i], &c->cdf[r]);
1659
1660
721
                    dav1d_ref_dec(&c->refs[i].segmap);
1661
721
                    c->refs[i].segmap = c->refs[r].segmap;
1662
721
                    if (c->refs[r].segmap)
1663
161
                        dav1d_ref_inc(c->refs[r].segmap);
1664
721
                    dav1d_ref_dec(&c->refs[i].refmvs);
1665
721
                }
1666
103
            }
1667
160
            c->frame_hdr = NULL;
1668
39.3k
        } else if (c->n_tiles == c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows) {
1669
36.7k
            switch (c->frame_hdr->frame_type) {
1670
9.44k
            case DAV1D_FRAME_TYPE_INTER:
1671
9.56k
            case DAV1D_FRAME_TYPE_SWITCH:
1672
9.56k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE ||
1673
9.56k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1674
0
                     !c->frame_hdr->refresh_frame_flags))
1675
0
                    goto skip;
1676
9.56k
                break;
1677
9.56k
            case DAV1D_FRAME_TYPE_INTRA:
1678
2.06k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA ||
1679
2.06k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1680
0
                     !c->frame_hdr->refresh_frame_flags))
1681
0
                    goto skip;
1682
                // fall-through
1683
27.1k
            default:
1684
27.1k
                break;
1685
36.7k
            }
1686
36.7k
            if (!c->n_tile_data)
1687
0
                goto error;
1688
36.7k
            if ((res = dav1d_submit_frame(c)) < 0)
1689
114
                return res;
1690
36.6k
            assert(!c->n_tile_data);
1691
36.6k
            c->frame_hdr = NULL;
1692
36.6k
            c->n_tiles = 0;
1693
36.6k
        }
1694
39.5k
    }
1695
1696
86.8k
    return gb.ptr_end - gb.ptr_start;
1697
1698
0
skip:
1699
    // update refs with only the headers in case we skip the frame
1700
0
    for (int i = 0; i < 8; i++) {
1701
0
        if (c->frame_hdr->refresh_frame_flags & (1 << i)) {
1702
0
            dav1d_thread_picture_unref(&c->refs[i].p);
1703
0
            c->refs[i].p.p.frame_hdr = c->frame_hdr;
1704
0
            c->refs[i].p.p.seq_hdr = c->seq_hdr;
1705
0
            c->refs[i].p.p.frame_hdr_ref = c->frame_hdr_ref;
1706
0
            c->refs[i].p.p.seq_hdr_ref = c->seq_hdr_ref;
1707
0
            dav1d_ref_inc(c->frame_hdr_ref);
1708
0
            dav1d_ref_inc(c->seq_hdr_ref);
1709
0
        }
1710
0
    }
1711
1712
0
    dav1d_ref_dec(&c->frame_hdr_ref);
1713
0
    c->frame_hdr = NULL;
1714
0
    c->n_tiles = 0;
1715
1716
0
    return gb.ptr_end - gb.ptr_start;
1717
1718
1.33k
error:
1719
1.33k
    dav1d_data_props_copy(&c->cached_error_props, &in->m);
1720
1.33k
    dav1d_log(c, gb.error ? "Overrun in OBU bit buffer\n" :
1721
1.33k
                            "Error parsing OBU data\n");
1722
1.33k
    return DAV1D_ERR(EINVAL);
1723
86.9k
}