Coverage Report

Created: 2026-12-01 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/obu.c
Line
Count
Source
1
/*
2
 * Copyright © 2018-2021, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <errno.h>
31
#include <limits.h>
32
#include <stdio.h>
33
34
#include "dav1d/data.h"
35
36
#include "common/frame.h"
37
#include "common/intops.h"
38
#include "common/validate.h"
39
40
#include "src/decode.h"
41
#include "src/getbits.h"
42
#include "src/levels.h"
43
#include "src/log.h"
44
#include "src/obu.h"
45
#include "src/ref.h"
46
#include "src/thread_task.h"
47
48
static int check_trailing_bits(GetBits *const gb,
49
                               const int strict_std_compliance)
50
18.9k
{
51
18.9k
    const int trailing_one_bit = dav1d_get_bit(gb);
52
53
18.9k
    if (gb->error)
54
28
        return DAV1D_ERR(EINVAL);
55
56
18.9k
    if (!strict_std_compliance)
57
18.9k
        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
10.7k
{
76
10.7k
#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
10.7k
    memset(hdr, 0, sizeof(*hdr));
83
10.7k
    hdr->profile = dav1d_get_bits(gb, 3);
84
10.7k
    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
10.7k
    hdr->still_picture = dav1d_get_bit(gb);
91
10.7k
    hdr->reduced_still_picture_header = dav1d_get_bit(gb);
92
10.7k
    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
10.6k
    if (hdr->reduced_still_picture_header) {
99
2.73k
        hdr->num_operating_points = 1;
100
2.73k
        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);
101
2.73k
        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);
102
2.73k
        hdr->operating_points[0].initial_display_delay = 10;
103
7.96k
    } else {
104
7.96k
        hdr->timing_info_present = dav1d_get_bit(gb);
105
7.96k
        if (hdr->timing_info_present) {
106
6.60k
            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);
107
6.60k
            hdr->time_scale = dav1d_get_bits(gb, 32);
108
6.60k
            if (strict_std_compliance && (!hdr->num_units_in_tick || !hdr->time_scale))
109
0
                goto error;
110
6.60k
            hdr->equal_picture_interval = dav1d_get_bit(gb);
111
6.60k
            if (hdr->equal_picture_interval) {
112
1.78k
                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
113
1.78k
                if (num_ticks_per_picture == UINT32_MAX)
114
0
                    goto error;
115
1.78k
                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
116
1.78k
            }
117
118
6.60k
            hdr->decoder_model_info_present = dav1d_get_bit(gb);
119
6.60k
            if (hdr->decoder_model_info_present) {
120
1.28k
                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;
121
1.28k
                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);
122
1.28k
                if (strict_std_compliance && !hdr->num_units_in_decoding_tick)
123
0
                    goto error;
124
1.28k
                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;
125
1.28k
                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;
126
1.28k
            }
127
6.60k
        }
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
7.96k
        hdr->display_model_info_present = dav1d_get_bit(gb);
134
7.96k
        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;
135
21.7k
        for (int i = 0; i < hdr->num_operating_points; i++) {
136
13.8k
            struct Dav1dSequenceHeaderOperatingPoint *const op =
137
13.8k
                &hdr->operating_points[i];
138
13.8k
            op->idc = dav1d_get_bits(gb, 12);
139
13.8k
            if (op->idc && (!(op->idc & 0xff) || !(op->idc & 0xf00)))
140
22
                goto error;
141
13.8k
            op->major_level = 2 + dav1d_get_bits(gb, 3);
142
13.8k
            op->minor_level = dav1d_get_bits(gb, 2);
143
13.8k
            if (op->major_level > 3)
144
4.96k
                op->tier = dav1d_get_bit(gb);
145
13.8k
            if (hdr->decoder_model_info_present) {
146
4.16k
                op->decoder_model_param_present = dav1d_get_bit(gb);
147
4.16k
                if (op->decoder_model_param_present) {
148
1.45k
                    struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
149
1.45k
                        &hdr->operating_parameter_info[i];
150
1.45k
                    opi->decoder_buffer_delay =
151
1.45k
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
152
1.45k
                    opi->encoder_buffer_delay =
153
1.45k
                        dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
154
1.45k
                    opi->low_delay_mode = dav1d_get_bit(gb);
155
1.45k
                }
156
4.16k
            }
157
13.8k
            if (hdr->display_model_info_present)
158
4.20k
                op->display_model_param_present = dav1d_get_bit(gb);
159
13.8k
            op->initial_display_delay =
160
13.8k
                op->display_model_param_present ? dav1d_get_bits(gb, 4) + 1 : 10;
161
13.8k
        }
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
7.96k
    }
167
168
10.6k
    hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;
169
10.6k
    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;
170
10.6k
    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;
171
10.6k
    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
10.6k
    if (!hdr->reduced_still_picture_header) {
177
7.93k
        hdr->frame_id_numbers_present = dav1d_get_bit(gb);
178
7.93k
        if (hdr->frame_id_numbers_present) {
179
1.08k
            hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;
180
1.08k
            hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;
181
1.08k
        }
182
7.93k
    }
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
10.6k
    hdr->sb128 = dav1d_get_bit(gb);
189
10.6k
    hdr->filter_intra = dav1d_get_bit(gb);
190
10.6k
    hdr->intra_edge_filter = dav1d_get_bit(gb);
191
10.6k
    if (hdr->reduced_still_picture_header) {
192
2.73k
        hdr->screen_content_tools = DAV1D_ADAPTIVE;
193
2.73k
        hdr->force_integer_mv = DAV1D_ADAPTIVE;
194
7.93k
    } else {
195
7.93k
        hdr->inter_intra = dav1d_get_bit(gb);
196
7.93k
        hdr->masked_compound = dav1d_get_bit(gb);
197
7.93k
        hdr->warped_motion = dav1d_get_bit(gb);
198
7.93k
        hdr->dual_filter = dav1d_get_bit(gb);
199
7.93k
        hdr->order_hint = dav1d_get_bit(gb);
200
7.93k
        if (hdr->order_hint) {
201
4.28k
            hdr->jnt_comp = dav1d_get_bit(gb);
202
4.28k
            hdr->ref_frame_mvs = dav1d_get_bit(gb);
203
4.28k
        }
204
7.93k
        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
7.93k
        hdr->force_integer_mv = hdr->screen_content_tools ?
210
6.00k
                                dav1d_get_bit(gb) ? DAV1D_ADAPTIVE : dav1d_get_bit(gb) : 2;
211
7.93k
        if (hdr->order_hint)
212
4.28k
            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;
213
7.93k
    }
214
10.6k
    hdr->super_res = dav1d_get_bit(gb);
215
10.6k
    hdr->cdef = dav1d_get_bit(gb);
216
10.6k
    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
10.6k
    hdr->hbd = dav1d_get_bit(gb);
223
10.6k
    if (hdr->profile == 2 && hdr->hbd)
224
2.17k
        hdr->hbd += dav1d_get_bit(gb);
225
10.6k
    if (hdr->profile != 1)
226
7.15k
        hdr->monochrome = dav1d_get_bit(gb);
227
10.6k
    hdr->color_description_present = dav1d_get_bit(gb);
228
10.6k
    if (hdr->color_description_present) {
229
4.63k
        hdr->pri = dav1d_get_bits(gb, 8);
230
4.63k
        hdr->trc = dav1d_get_bits(gb, 8);
231
4.63k
        hdr->mtrx = dav1d_get_bits(gb, 8);
232
6.03k
    } else {
233
6.03k
        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;
234
6.03k
        hdr->trc = DAV1D_TRC_UNKNOWN;
235
6.03k
        hdr->mtrx = DAV1D_MC_UNKNOWN;
236
6.03k
    }
237
10.6k
    if (hdr->monochrome) {
238
2.29k
        hdr->color_range = dav1d_get_bit(gb);
239
2.29k
        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;
240
2.29k
        hdr->ss_hor = hdr->ss_ver = 1;
241
2.29k
        hdr->chr = DAV1D_CHR_UNKNOWN;
242
8.37k
    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&
243
187
               hdr->trc == DAV1D_TRC_SRGB &&
244
19
               hdr->mtrx == DAV1D_MC_IDENTITY)
245
5
    {
246
5
        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
247
5
        hdr->color_range = 1;
248
5
        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))
249
2
            goto error;
250
8.37k
    } else {
251
8.37k
        hdr->color_range = dav1d_get_bit(gb);
252
8.37k
        switch (hdr->profile) {
253
2.80k
        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;
254
2.80k
                hdr->ss_hor = hdr->ss_ver = 1;
255
2.80k
                break;
256
3.52k
        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
257
3.52k
                break;
258
2.04k
        case 2:
259
2.04k
            if (hdr->hbd == 2) {
260
766
                hdr->ss_hor = dav1d_get_bit(gb);
261
766
                if (hdr->ss_hor)
262
322
                    hdr->ss_ver = dav1d_get_bit(gb);
263
766
            } else
264
1.28k
                hdr->ss_hor = 1;
265
2.04k
            hdr->layout = hdr->ss_hor ?
266
1.60k
                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :
267
1.60k
                                        DAV1D_PIXEL_LAYOUT_I422 :
268
2.04k
                                        DAV1D_PIXEL_LAYOUT_I444;
269
2.04k
            break;
270
8.37k
        }
271
8.37k
        hdr->chr = (hdr->ss_hor & hdr->ss_ver) ?
272
5.26k
                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;
273
8.37k
    }
274
10.6k
    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
10.6k
    if (!hdr->monochrome)
280
8.37k
        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
10.6k
    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
10.6k
    return check_trailing_bits(gb, strict_std_compliance);
297
298
39
error:
299
39
    return DAV1D_ERR(EINVAL);
300
10.6k
}
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
56.9k
{
344
56.9k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
345
56.9k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
346
347
56.9k
    if (use_ref) {
348
28.3k
        for (int i = 0; i < 7; i++) {
349
28.2k
            if (dav1d_get_bit(gb)) {
350
11.3k
                const Dav1dThreadPicture *const ref =
351
11.3k
                    &c->refs[c->frame_hdr->refidx[i]].p;
352
11.3k
                if (!ref->p.frame_hdr) return -1;
353
11.3k
                hdr->width[1] = ref->p.frame_hdr->width[1];
354
11.3k
                hdr->height = ref->p.frame_hdr->height;
355
11.3k
                if (hdr->width[1] > seqhdr->max_width ||
356
11.3k
                    hdr->height > seqhdr->max_height)
357
0
                {
358
0
                    return -1;
359
0
                }
360
11.3k
                hdr->render_width = ref->p.frame_hdr->render_width;
361
11.3k
                hdr->render_height = ref->p.frame_hdr->render_height;
362
11.3k
                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
363
11.3k
                if (hdr->super_res.enabled) {
364
3.93k
                    const int d = hdr->super_res.width_scale_denominator =
365
3.93k
                        9 + dav1d_get_bits(gb, 3);
366
3.93k
                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,
367
3.93k
                                         imin(16, hdr->width[1]));
368
7.40k
                } else {
369
7.40k
                    hdr->super_res.width_scale_denominator = 8;
370
7.40k
                    hdr->width[0] = hdr->width[1];
371
7.40k
                }
372
11.3k
                return 0;
373
11.3k
            }
374
28.2k
        }
375
11.4k
    }
376
377
45.6k
    if (hdr->frame_size_override) {
378
1.76k
        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;
379
1.76k
        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;
380
1.76k
        if (hdr->width[1] > seqhdr->max_width ||
381
1.74k
            hdr->height > seqhdr->max_height)
382
28
        {
383
28
            return -1;
384
28
        }
385
43.8k
    } else {
386
43.8k
        hdr->width[1] = seqhdr->max_width;
387
43.8k
        hdr->height = seqhdr->max_height;
388
43.8k
    }
389
45.5k
    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bit(gb);
390
45.5k
    if (hdr->super_res.enabled) {
391
12.0k
        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);
392
12.0k
        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));
393
33.5k
    } else {
394
33.5k
        hdr->super_res.width_scale_denominator = 8;
395
33.5k
        hdr->width[0] = hdr->width[1];
396
33.5k
    }
397
45.5k
    hdr->have_render_size = dav1d_get_bit(gb);
398
45.5k
    if (hdr->have_render_size) {
399
17.1k
        hdr->render_width = dav1d_get_bits(gb, 16) + 1;
400
17.1k
        hdr->render_height = dav1d_get_bits(gb, 16) + 1;
401
28.4k
    } else {
402
28.4k
        hdr->render_width = hdr->width[1];
403
28.4k
        hdr->render_height = hdr->height;
404
28.4k
    }
405
45.5k
    return 0;
406
45.6k
}
407
408
293k
static inline int tile_log2(const int sz, const int tgt) {
409
293k
    int k;
410
360k
    for (k = 0; (sz << k) < tgt; k++) ;
411
293k
    return k;
412
293k
}
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
59.1k
static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {
420
59.1k
#define DEBUG_FRAME_HDR 0
421
422
#if DEBUG_FRAME_HDR
423
    const uint8_t *const init_ptr = gb->ptr;
424
#endif
425
59.1k
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
426
59.1k
    Dav1dFrameHeader *const hdr = c->frame_hdr;
427
428
59.1k
    if (!seqhdr->reduced_still_picture_header)
429
48.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
59.1k
    if (hdr->show_existing_frame) {
435
2.17k
        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);
436
2.17k
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
437
274
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
438
2.17k
        if (seqhdr->frame_id_numbers_present) {
439
38
            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
440
38
            Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr;
441
38
            if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) goto error;
442
38
        }
443
2.17k
        return 0;
444
2.17k
    }
445
446
56.9k
    if (seqhdr->reduced_still_picture_header) {
447
10.8k
        hdr->frame_type = DAV1D_FRAME_TYPE_KEY;
448
10.8k
        hdr->show_frame = 1;
449
46.1k
    } else {
450
46.1k
        hdr->frame_type = dav1d_get_bits(gb, 2);
451
46.1k
        hdr->show_frame = dav1d_get_bit(gb);
452
46.1k
    }
453
56.9k
    if (hdr->show_frame) {
454
53.7k
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
455
6.85k
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
456
53.7k
        hdr->showable_frame = hdr->frame_type != DAV1D_FRAME_TYPE_KEY;
457
53.7k
    } else
458
3.18k
        hdr->showable_frame = dav1d_get_bit(gb);
459
56.9k
    hdr->error_resilient_mode =
460
56.9k
        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||
461
39.2k
        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||
462
39.0k
        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
56.9k
    hdr->disable_cdf_update = dav1d_get_bit(gb);
468
56.9k
    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?
469
31.1k
                                      dav1d_get_bit(gb) : seqhdr->screen_content_tools;
470
56.9k
    if (hdr->allow_screen_content_tools)
471
18.8k
        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?
472
13.2k
                                dav1d_get_bit(gb) : seqhdr->force_integer_mv;
473
474
56.9k
    if (IS_KEY_OR_INTRA(hdr))
475
20.7k
        hdr->force_integer_mv = 1;
476
477
56.9k
    if (seqhdr->frame_id_numbers_present)
478
1.63k
        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
479
480
56.9k
    if (!seqhdr->reduced_still_picture_header)
481
46.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
56.9k
    if (seqhdr->order_hint)
487
25.4k
        hdr->frame_offset = dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
488
56.9k
    hdr->primary_ref_frame = !hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) ?
489
35.1k
                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;
490
491
56.9k
    if (seqhdr->decoder_model_info_present) {
492
10.6k
        hdr->buffer_removal_time_present = dav1d_get_bit(gb);
493
10.6k
        if (hdr->buffer_removal_time_present) {
494
7.38k
            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {
495
5.15k
                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];
496
5.15k
                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];
497
5.15k
                if (seqop->decoder_model_param_present) {
498
1.32k
                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;
499
1.32k
                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;
500
1.32k
                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))
501
803
                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);
502
1.32k
                }
503
5.15k
            }
504
2.23k
        }
505
10.6k
    }
506
507
56.9k
    if (IS_KEY_OR_INTRA(hdr)) {
508
20.7k
        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&
509
19.8k
                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);
510
20.7k
        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
511
3.27k
            for (int i = 0; i < 8; i++)
512
2.91k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
513
20.7k
        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
20.7k
        if (read_frame_size(c, gb, 0) < 0) goto error;
519
20.7k
        if (hdr->allow_screen_content_tools && !hdr->super_res.enabled)
520
4.25k
            hdr->allow_intrabc = dav1d_get_bit(gb);
521
36.2k
    } else {
522
36.2k
        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :
523
36.2k
                                   dav1d_get_bits(gb, 8);
524
36.2k
        if (hdr->error_resilient_mode && seqhdr->order_hint)
525
2.77k
            for (int i = 0; i < 8; i++)
526
2.46k
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
527
36.2k
        if (seqhdr->order_hint) {
528
20.0k
            hdr->frame_ref_short_signaling = dav1d_get_bit(gb);
529
20.0k
            if (hdr->frame_ref_short_signaling) {
530
12.1k
                hdr->refidx[0] = dav1d_get_bits(gb, 3);
531
12.1k
                hdr->refidx[1] = hdr->refidx[2] = -1;
532
12.1k
                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
12.1k
                int frame_offset_mem[8+1];
537
12.1k
                int *const frame_offset = &frame_offset_mem[1];
538
12.1k
                int earliest_ref = -1;
539
109k
                for (int i = 0, earliest_offset = INT_MAX; i < 8; i++) {
540
97.0k
                    const Dav1dFrameHeader *const refhdr = c->refs[i].p.p.frame_hdr;
541
97.0k
                    if (!refhdr) goto error;
542
97.0k
                    const int diff = get_poc_diff(seqhdr->order_hint_n_bits,
543
97.0k
                                                  refhdr->frame_offset,
544
97.0k
                                                  hdr->frame_offset);
545
97.0k
                    frame_offset[i] = diff;
546
97.0k
                    if (diff < earliest_offset) {
547
18.7k
                        earliest_offset = diff;
548
18.7k
                        earliest_ref = i;
549
18.7k
                    }
550
97.0k
                }
551
12.1k
                frame_offset[hdr->refidx[0]] = INT_MIN; // = reference frame is used
552
12.1k
                frame_offset[hdr->refidx[3]] = INT_MIN;
553
12.1k
                assert(earliest_ref >= 0);
554
555
12.1k
                int refidx = -1;
556
109k
                for (int i = 0, latest_offset = 0; i < 8; i++) {
557
97.0k
                    const int hint = frame_offset[i];
558
97.0k
                    if (hint >= latest_offset) {
559
44.5k
                        latest_offset = hint;
560
44.5k
                        refidx = i;
561
44.5k
                    }
562
97.0k
                }
563
12.1k
                frame_offset[refidx] = INT_MIN;
564
12.1k
                hdr->refidx[6] = refidx;
565
566
36.3k
                for (int i = 4; i < 6; i++) {
567
                    /* Unsigned compares to handle negative values. */
568
24.2k
                    unsigned earliest_offset = UINT8_MAX;
569
24.2k
                    refidx = -1;
570
218k
                    for (int j = 0; j < 8; j++) {
571
194k
                        const unsigned hint = frame_offset[j];
572
194k
                        if (hint < earliest_offset) {
573
19.3k
                            earliest_offset = hint;
574
19.3k
                            refidx = j;
575
19.3k
                        }
576
194k
                    }
577
24.2k
                    frame_offset[refidx] = INT_MIN;
578
24.2k
                    hdr->refidx[i] = refidx;
579
24.2k
                }
580
581
84.9k
                for (int i = 1; i < 7; i++) {
582
72.7k
                    refidx = hdr->refidx[i];
583
72.7k
                    if (refidx < 0) {
584
32.0k
                        unsigned latest_offset = ~UINT8_MAX;
585
288k
                        for (int j = 0; j < 8; j++) {
586
256k
                            const unsigned hint = frame_offset[j];
587
256k
                            if (hint >= latest_offset) {
588
70.5k
                                latest_offset = hint;
589
70.5k
                                refidx = j;
590
70.5k
                            }
591
256k
                        }
592
32.0k
                        frame_offset[refidx] = INT_MIN;
593
32.0k
                        hdr->refidx[i] = refidx >= 0 ? refidx : earliest_ref;
594
32.0k
                    }
595
72.7k
                }
596
12.1k
            }
597
20.0k
        }
598
289k
        for (int i = 0; i < 7; i++) {
599
253k
            if (!hdr->frame_ref_short_signaling)
600
168k
                hdr->refidx[i] = dav1d_get_bits(gb, 3);
601
253k
            if (seqhdr->frame_id_numbers_present) {
602
359
                const unsigned delta_ref_frame_id = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits) + 1;
603
359
                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
359
                Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr;
605
359
                if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error;
606
359
            }
607
253k
        }
608
36.1k
        const int use_ref = !hdr->error_resilient_mode &&
609
35.1k
                            hdr->frame_size_override;
610
36.1k
        if (read_frame_size(c, gb, use_ref) < 0) goto error;
611
36.1k
        if (!hdr->force_integer_mv)
612
34.2k
            hdr->hp = dav1d_get_bit(gb);
613
36.1k
        hdr->subpel_filter_mode = dav1d_get_bit(gb) ? DAV1D_FILTER_SWITCHABLE :
614
36.1k
                                                      dav1d_get_bits(gb, 2);
615
36.1k
        hdr->switchable_motion_mode = dav1d_get_bit(gb);
616
36.1k
        if (!hdr->error_resilient_mode && seqhdr->ref_frame_mvs &&
617
9.77k
            seqhdr->order_hint && IS_INTER_OR_SWITCH(hdr))
618
9.77k
        {
619
9.77k
            hdr->use_ref_frame_mvs = dav1d_get_bit(gb);
620
9.77k
        }
621
36.1k
    }
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
56.9k
    if (!seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update)
628
32.5k
        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
56.9k
    hdr->tiling.uniform = dav1d_get_bit(gb);
636
56.9k
    const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
637
56.9k
    const int sbsz_log2 = 6 + seqhdr->sb128;
638
56.9k
    const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
639
56.9k
    const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
640
56.9k
    const int max_tile_width_sb = 4096 >> sbsz_log2;
641
56.9k
    const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
642
56.9k
    hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
643
56.9k
    hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
644
56.9k
    hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
645
56.9k
    const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
646
56.9k
                              hdr->tiling.min_log2_cols);
647
56.9k
    if (hdr->tiling.uniform) {
648
24.2k
        for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
649
25.0k
             hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bit(gb);
650
24.2k
             hdr->tiling.log2_cols++) ;
651
24.2k
        const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols);
652
24.2k
        hdr->tiling.cols = 0;
653
56.9k
        for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++)
654
32.7k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
655
24.2k
        hdr->tiling.min_log2_rows =
656
24.2k
            imax(min_log2_tiles - hdr->tiling.log2_cols, 0);
657
658
24.2k
        for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows;
659
24.7k
             hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bit(gb);
660
24.2k
             hdr->tiling.log2_rows++) ;
661
24.2k
        const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows);
662
24.2k
        hdr->tiling.rows = 0;
663
49.6k
        for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++)
664
25.4k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
665
32.6k
    } else {
666
32.6k
        hdr->tiling.cols = 0;
667
32.6k
        int widest_tile = 0, max_tile_area_sb = sbw * sbh;
668
72.5k
        for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) {
669
39.8k
            const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb);
670
39.8k
            const int tile_w = (tile_width_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_width_sb) : 1;
671
39.8k
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
672
39.8k
            sbx += tile_w;
673
39.8k
            widest_tile = imax(widest_tile, tile_w);
674
39.8k
        }
675
32.6k
        hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
676
32.6k
        if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
677
32.6k
        const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
678
679
32.6k
        hdr->tiling.rows = 0;
680
72.2k
        for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
681
39.5k
            const int tile_height_sb = imin(sbh - sby, max_tile_height_sb);
682
39.5k
            const int tile_h = (tile_height_sb > 1) ? 1 + dav1d_get_uniform(gb, tile_height_sb) : 1;
683
39.5k
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
684
39.5k
            sby += tile_h;
685
39.5k
        }
686
32.6k
        hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows);
687
32.6k
    }
688
56.9k
    hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw;
689
56.9k
    hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh;
690
56.9k
    if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) {
691
3.56k
        hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols + hdr->tiling.log2_rows);
692
3.56k
        if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows)
693
7
            goto error;
694
3.55k
        hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1;
695
3.55k
    }
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
56.9k
    hdr->quant.yac = dav1d_get_bits(gb, 8);
703
56.9k
    if (dav1d_get_bit(gb))
704
22.0k
        hdr->quant.ydc_delta = dav1d_get_sbits(gb, 7);
705
56.9k
    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
46.8k
        const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bit(gb) : 0;
710
46.8k
        if (dav1d_get_bit(gb))
711
21.3k
            hdr->quant.udc_delta = dav1d_get_sbits(gb, 7);
712
46.8k
        if (dav1d_get_bit(gb))
713
25.9k
            hdr->quant.uac_delta = dav1d_get_sbits(gb, 7);
714
46.8k
        if (diff_uv_delta) {
715
8.79k
            if (dav1d_get_bit(gb))
716
7.33k
                hdr->quant.vdc_delta = dav1d_get_sbits(gb, 7);
717
8.79k
            if (dav1d_get_bit(gb))
718
7.43k
                hdr->quant.vac_delta = dav1d_get_sbits(gb, 7);
719
38.0k
        } else {
720
38.0k
            hdr->quant.vdc_delta = hdr->quant.udc_delta;
721
38.0k
            hdr->quant.vac_delta = hdr->quant.uac_delta;
722
38.0k
        }
723
46.8k
    }
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
56.9k
    hdr->quant.qm = dav1d_get_bit(gb);
729
56.9k
    if (hdr->quant.qm) {
730
43.1k
        hdr->quant.qm_y = dav1d_get_bits(gb, 4);
731
43.1k
        hdr->quant.qm_u = dav1d_get_bits(gb, 4);
732
43.1k
        hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 4) :
733
43.1k
                                                        hdr->quant.qm_u;
734
43.1k
    }
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
56.9k
    hdr->segmentation.enabled = dav1d_get_bit(gb);
742
56.9k
    if (hdr->segmentation.enabled) {
743
30.3k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
744
10.0k
            hdr->segmentation.update_map = 1;
745
10.0k
            hdr->segmentation.update_data = 1;
746
20.2k
        } else {
747
20.2k
            hdr->segmentation.update_map = dav1d_get_bit(gb);
748
20.2k
            if (hdr->segmentation.update_map)
749
3.04k
                hdr->segmentation.temporal = dav1d_get_bit(gb);
750
20.2k
            hdr->segmentation.update_data = dav1d_get_bit(gb);
751
20.2k
        }
752
753
30.3k
        if (hdr->segmentation.update_data) {
754
11.2k
            hdr->segmentation.seg_data.last_active_segid = -1;
755
101k
            for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
756
89.7k
                Dav1dSegmentationData *const seg =
757
89.7k
                    &hdr->segmentation.seg_data.d[i];
758
89.7k
                if (dav1d_get_bit(gb)) {
759
16.1k
                    seg->delta_q = dav1d_get_sbits(gb, 9);
760
16.1k
                    hdr->segmentation.seg_data.last_active_segid = i;
761
16.1k
                }
762
89.7k
                if (dav1d_get_bit(gb)) {
763
13.6k
                    seg->delta_lf_y_v = dav1d_get_sbits(gb, 7);
764
13.6k
                    hdr->segmentation.seg_data.last_active_segid = i;
765
13.6k
                }
766
89.7k
                if (dav1d_get_bit(gb)) {
767
38.8k
                    seg->delta_lf_y_h = dav1d_get_sbits(gb, 7);
768
38.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
769
38.8k
                }
770
89.7k
                if (dav1d_get_bit(gb)) {
771
40.8k
                    seg->delta_lf_u = dav1d_get_sbits(gb, 7);
772
40.8k
                    hdr->segmentation.seg_data.last_active_segid = i;
773
40.8k
                }
774
89.7k
                if (dav1d_get_bit(gb)) {
775
41.4k
                    seg->delta_lf_v = dav1d_get_sbits(gb, 7);
776
41.4k
                    hdr->segmentation.seg_data.last_active_segid = i;
777
41.4k
                }
778
89.7k
                if (dav1d_get_bit(gb)) {
779
40.3k
                    seg->ref = dav1d_get_bits(gb, 3);
780
40.3k
                    hdr->segmentation.seg_data.last_active_segid = i;
781
40.3k
                    hdr->segmentation.seg_data.preskip = 1;
782
49.4k
                } else {
783
49.4k
                    seg->ref = -1;
784
49.4k
                }
785
89.7k
                if ((seg->skip = dav1d_get_bit(gb))) {
786
57.0k
                    hdr->segmentation.seg_data.last_active_segid = i;
787
57.0k
                    hdr->segmentation.seg_data.preskip = 1;
788
57.0k
                }
789
89.7k
                if ((seg->globalmv = dav1d_get_bit(gb))) {
790
35.7k
                    hdr->segmentation.seg_data.last_active_segid = i;
791
35.7k
                    hdr->segmentation.seg_data.preskip = 1;
792
35.7k
                }
793
89.7k
            }
794
19.1k
        } else {
795
            // segmentation.update_data was false so we should copy
796
            // segmentation data from the reference frame.
797
19.1k
            assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
798
19.1k
            const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
799
19.1k
            if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
800
19.1k
            hdr->segmentation.seg_data =
801
19.1k
                c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data;
802
19.1k
        }
803
30.3k
    } else {
804
239k
        for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++)
805
212k
            hdr->segmentation.seg_data.d[i].ref = -1;
806
26.5k
    }
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
56.9k
    if (hdr->quant.yac) {
814
55.6k
        hdr->delta.q.present = dav1d_get_bit(gb);
815
55.6k
        if (hdr->delta.q.present) {
816
13.5k
            hdr->delta.q.res_log2 = dav1d_get_bits(gb, 2);
817
13.5k
            if (!hdr->allow_intrabc) {
818
12.8k
                hdr->delta.lf.present = dav1d_get_bit(gb);
819
12.8k
                if (hdr->delta.lf.present) {
820
5.84k
                    hdr->delta.lf.res_log2 = dav1d_get_bits(gb, 2);
821
5.84k
                    hdr->delta.lf.multi = dav1d_get_bit(gb);
822
5.84k
                }
823
12.8k
            }
824
13.5k
        }
825
55.6k
    }
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
56.9k
    const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta &&
833
27.8k
        !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta;
834
56.9k
    hdr->all_lossless = 1;
835
512k
    for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
836
455k
        hdr->segmentation.qidx[i] = hdr->segmentation.enabled ?
837
242k
            iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) :
838
455k
            hdr->quant.yac;
839
455k
        hdr->segmentation.lossless[i] =
840
455k
            !hdr->segmentation.qidx[i] && delta_lossless;
841
455k
        hdr->all_lossless &= hdr->segmentation.lossless[i];
842
455k
    }
843
844
    // loopfilter
845
56.9k
    if (hdr->all_lossless || hdr->allow_intrabc) {
846
3.54k
        hdr->loopfilter.mode_ref_delta_enabled = 1;
847
3.54k
        hdr->loopfilter.mode_ref_delta_update = 1;
848
3.54k
        hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
849
53.3k
    } else {
850
53.3k
        hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6);
851
53.3k
        hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6);
852
53.3k
        if (!seqhdr->monochrome &&
853
43.8k
            (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1]))
854
42.3k
        {
855
42.3k
            hdr->loopfilter.level_u = dav1d_get_bits(gb, 6);
856
42.3k
            hdr->loopfilter.level_v = dav1d_get_bits(gb, 6);
857
42.3k
        }
858
53.3k
        hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3);
859
860
53.3k
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
861
19.0k
            hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
862
34.3k
        } else {
863
34.3k
            const int ref = hdr->refidx[hdr->primary_ref_frame];
864
34.3k
            if (!c->refs[ref].p.p.frame_hdr) goto error;
865
34.3k
            hdr->loopfilter.mode_ref_deltas =
866
34.3k
                c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas;
867
34.3k
        }
868
53.3k
        hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bit(gb);
869
53.3k
        if (hdr->loopfilter.mode_ref_delta_enabled) {
870
15.2k
            hdr->loopfilter.mode_ref_delta_update = dav1d_get_bit(gb);
871
15.2k
            if (hdr->loopfilter.mode_ref_delta_update) {
872
109k
                for (int i = 0; i < 8; i++)
873
96.9k
                    if (dav1d_get_bit(gb))
874
42.6k
                        hdr->loopfilter.mode_ref_deltas.ref_delta[i] =
875
42.6k
                            dav1d_get_sbits(gb, 7);
876
36.3k
                for (int i = 0; i < 2; i++)
877
24.2k
                    if (dav1d_get_bit(gb))
878
21.4k
                        hdr->loopfilter.mode_ref_deltas.mode_delta[i] =
879
21.4k
                            dav1d_get_sbits(gb, 7);
880
12.1k
            }
881
15.2k
        }
882
53.3k
    }
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
56.9k
    if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) {
890
28.1k
        hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3;
891
28.1k
        hdr->cdef.n_bits = dav1d_get_bits(gb, 2);
892
92.4k
        for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) {
893
64.2k
            hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6);
894
64.2k
            if (!seqhdr->monochrome)
895
49.2k
                hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6);
896
64.2k
        }
897
28.1k
    }
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
56.9k
    if ((!hdr->all_lossless || hdr->super_res.enabled) &&
905
56.0k
        seqhdr->restoration && !hdr->allow_intrabc)
906
30.0k
    {
907
30.0k
        hdr->restoration.type[0] = dav1d_get_bits(gb, 2);
908
30.0k
        if (!seqhdr->monochrome) {
909
24.3k
            hdr->restoration.type[1] = dav1d_get_bits(gb, 2);
910
24.3k
            hdr->restoration.type[2] = dav1d_get_bits(gb, 2);
911
24.3k
        }
912
913
30.0k
        if (hdr->restoration.type[0] || hdr->restoration.type[1] ||
914
7.64k
            hdr->restoration.type[2])
915
25.8k
        {
916
            // Log2 of the restoration unit size.
917
25.8k
            hdr->restoration.unit_size[0] = 6 + seqhdr->sb128;
918
25.8k
            if (dav1d_get_bit(gb)) {
919
8.50k
                hdr->restoration.unit_size[0]++;
920
8.50k
                if (!seqhdr->sb128)
921
3.75k
                    hdr->restoration.unit_size[0] += dav1d_get_bit(gb);
922
8.50k
            }
923
25.8k
            hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0];
924
25.8k
            if ((hdr->restoration.type[1] || hdr->restoration.type[2]) &&
925
20.7k
                seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1)
926
10.8k
            {
927
10.8k
                hdr->restoration.unit_size[1] -= dav1d_get_bit(gb);
928
10.8k
            }
929
25.8k
        } else {
930
4.14k
            hdr->restoration.unit_size[0] = 8;
931
4.14k
        }
932
30.0k
    }
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
56.9k
    if (!hdr->all_lossless)
939
55.8k
        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
56.9k
    if (IS_INTER_OR_SWITCH(hdr))
945
36.1k
        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
56.9k
    if (hdr->switchable_comp_refs && IS_INTER_OR_SWITCH(hdr) && seqhdr->order_hint) {
951
12.6k
        const int poc = hdr->frame_offset;
952
12.6k
        int off_before = -1, off_after = -1;
953
12.6k
        int off_before_idx, off_after_idx;
954
100k
        for (int i = 0; i < 7; i++) {
955
88.3k
            if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
956
88.3k
            const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
957
958
88.3k
            const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc);
959
88.3k
            if (diff > 0) {
960
13.6k
                if (off_after < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
961
9.98k
                                                  off_after, refpoc) > 0)
962
3.95k
                {
963
3.95k
                    off_after = refpoc;
964
3.95k
                    off_after_idx = i;
965
3.95k
                }
966
74.6k
            } else if (diff < 0 && (off_before < 0 ||
967
21.0k
                                    get_poc_diff(seqhdr->order_hint_n_bits,
968
21.0k
                                                 refpoc, off_before) > 0))
969
6.82k
            {
970
6.82k
                off_before = refpoc;
971
6.82k
                off_before_idx = i;
972
6.82k
            }
973
88.3k
        }
974
975
12.6k
        if ((off_before | off_after) >= 0) {
976
1.28k
            hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx);
977
1.28k
            hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx);
978
1.28k
            hdr->skip_mode_allowed = 1;
979
11.3k
        } else if (off_before >= 0) {
980
5.22k
            int off_before2 = -1;
981
5.22k
            int off_before2_idx;
982
41.7k
            for (int i = 0; i < 7; i++) {
983
36.5k
                if (!c->refs[hdr->refidx[i]].p.p.frame_hdr) goto error;
984
36.5k
                const int refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
985
36.5k
                if (get_poc_diff(seqhdr->order_hint_n_bits,
986
36.5k
                                 refpoc, off_before) < 0) {
987
5.87k
                    if (off_before2 < 0 || get_poc_diff(seqhdr->order_hint_n_bits,
988
3.73k
                                                        refpoc, off_before2) > 0)
989
2.21k
                    {
990
2.21k
                        off_before2 = refpoc;
991
2.21k
                        off_before2_idx = i;
992
2.21k
                    }
993
5.87k
                }
994
36.5k
            }
995
996
5.22k
            if (off_before2 >= 0) {
997
2.13k
                hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx);
998
2.13k
                hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx);
999
2.13k
                hdr->skip_mode_allowed = 1;
1000
2.13k
            }
1001
5.22k
        }
1002
12.6k
    }
1003
56.9k
    if (hdr->skip_mode_allowed)
1004
3.42k
        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
56.9k
    if (!hdr->error_resilient_mode && IS_INTER_OR_SWITCH(hdr) && seqhdr->warped_motion)
1010
15.5k
        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
56.9k
    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
455k
    for (int i = 0; i < 7; i++)
1022
398k
        hdr->gmv[i] = dav1d_default_wm_params;
1023
1024
56.9k
    if (IS_INTER_OR_SWITCH(hdr)) {
1025
289k
        for (int i = 0; i < 7; i++) {
1026
253k
            hdr->gmv[i].type = !dav1d_get_bit(gb) ? DAV1D_WM_TYPE_IDENTITY :
1027
253k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_ROT_ZOOM :
1028
67.3k
                                dav1d_get_bit(gb) ? DAV1D_WM_TYPE_TRANSLATION :
1029
36.8k
                                                    DAV1D_WM_TYPE_AFFINE;
1030
1031
253k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue;
1032
1033
67.3k
            const Dav1dWarpedMotionParams *ref_gmv;
1034
67.3k
            if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
1035
2.57k
                ref_gmv = &dav1d_default_wm_params;
1036
64.8k
            } else {
1037
64.8k
                const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
1038
64.8k
                if (!c->refs[pri_ref].p.p.frame_hdr) goto error;
1039
64.7k
                ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i];
1040
64.7k
            }
1041
67.3k
            int32_t *const mat = hdr->gmv[i].matrix;
1042
67.3k
            const int32_t *const ref_mat = ref_gmv->matrix;
1043
67.3k
            int bits, shift;
1044
1045
67.3k
            if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) {
1046
58.8k
                mat[2] = (1 << 16) + 2 *
1047
58.8k
                    dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12);
1048
58.8k
                mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12);
1049
1050
58.8k
                bits = 12;
1051
58.8k
                shift = 10;
1052
58.8k
            } else {
1053
8.53k
                bits = 9 - !hdr->hp;
1054
8.53k
                shift = 13 + !hdr->hp;
1055
8.53k
            }
1056
1057
67.3k
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) {
1058
28.2k
                mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12);
1059
28.2k
                mat[5] = (1 << 16) + 2 *
1060
28.2k
                    dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12);
1061
39.0k
            } else {
1062
39.0k
                mat[4] = -mat[3];
1063
39.0k
                mat[5] = mat[2];
1064
39.0k
            }
1065
1066
67.3k
            mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift);
1067
67.3k
            mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift);
1068
67.3k
        }
1069
36.1k
    }
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
56.8k
    if (seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame)) {
1076
14.9k
        hdr->film_grain.present = dav1d_get_bit(gb);
1077
14.9k
        if (hdr->film_grain.present) {
1078
7.43k
            const unsigned seed = dav1d_get_bits(gb, 16);
1079
7.43k
            hdr->film_grain.update = hdr->frame_type != DAV1D_FRAME_TYPE_INTER || dav1d_get_bit(gb);
1080
7.43k
            if (!hdr->film_grain.update) {
1081
4.69k
                const int refidx = dav1d_get_bits(gb, 3);
1082
4.69k
                int i;
1083
18.5k
                for (i = 0; i < 7; i++)
1084
18.5k
                    if (hdr->refidx[i] == refidx)
1085
4.68k
                        break;
1086
4.69k
                if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error;
1087
4.68k
                hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data;
1088
4.68k
                hdr->film_grain.data.seed = seed;
1089
4.68k
            } else {
1090
2.74k
                Dav1dFilmGrainData *const fgd = &hdr->film_grain.data;
1091
2.74k
                fgd->seed = seed;
1092
1093
2.74k
                fgd->num_y_points = dav1d_get_bits(gb, 4);
1094
2.74k
                if (fgd->num_y_points > 14) goto error;
1095
3.97k
                for (int i = 0; i < fgd->num_y_points; i++) {
1096
1.24k
                    fgd->y_points[i][0] = dav1d_get_bits(gb, 8);
1097
1.24k
                    if (i && fgd->y_points[i - 1][0] >= fgd->y_points[i][0])
1098
16
                        goto error;
1099
1.22k
                    fgd->y_points[i][1] = dav1d_get_bits(gb, 8);
1100
1.22k
                }
1101
1102
2.73k
                if (!seqhdr->monochrome)
1103
2.32k
                    fgd->chroma_scaling_from_luma = dav1d_get_bit(gb);
1104
2.73k
                if (seqhdr->monochrome || fgd->chroma_scaling_from_luma ||
1105
1.37k
                    (seqhdr->ss_ver == 1 && seqhdr->ss_hor == 1 && !fgd->num_y_points))
1106
2.20k
                {
1107
2.20k
                    fgd->num_uv_points[0] = fgd->num_uv_points[1] = 0;
1108
2.20k
                } else for (int pl = 0; pl < 2; pl++) {
1109
1.05k
                    fgd->num_uv_points[pl] = dav1d_get_bits(gb, 4);
1110
1.05k
                    if (fgd->num_uv_points[pl] > 10) goto error;
1111
1.83k
                    for (int i = 0; i < fgd->num_uv_points[pl]; i++) {
1112
799
                        fgd->uv_points[pl][i][0] = dav1d_get_bits(gb, 8);
1113
799
                        if (i && fgd->uv_points[pl][i - 1][0] >= fgd->uv_points[pl][i][0])
1114
11
                            goto error;
1115
788
                        fgd->uv_points[pl][i][1] = dav1d_get_bits(gb, 8);
1116
788
                    }
1117
1.04k
                }
1118
1119
2.71k
                if (seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1 &&
1120
1.88k
                    !!fgd->num_uv_points[0] != !!fgd->num_uv_points[1])
1121
1
                {
1122
1
                    goto error;
1123
1
                }
1124
1125
2.71k
                fgd->scaling_shift = dav1d_get_bits(gb, 2) + 8;
1126
2.71k
                fgd->ar_coeff_lag = dav1d_get_bits(gb, 2);
1127
2.71k
                const int num_y_pos = 2 * fgd->ar_coeff_lag * (fgd->ar_coeff_lag + 1);
1128
2.71k
                if (fgd->num_y_points)
1129
9.27k
                    for (int i = 0; i < num_y_pos; i++)
1130
8.34k
                        fgd->ar_coeffs_y[i] = dav1d_get_bits(gb, 8) - 128;
1131
8.14k
                for (int pl = 0; pl < 2; pl++)
1132
5.43k
                    if (fgd->num_uv_points[pl] || fgd->chroma_scaling_from_luma) {
1133
2.35k
                        const int num_uv_pos = num_y_pos + !!fgd->num_y_points;
1134
27.5k
                        for (int i = 0; i < num_uv_pos; i++)
1135
25.1k
                            fgd->ar_coeffs_uv[pl][i] = dav1d_get_bits(gb, 8) - 128;
1136
2.35k
                        if (!fgd->num_y_points)
1137
1.12k
                            fgd->ar_coeffs_uv[pl][num_uv_pos] = 0;
1138
2.35k
                    }
1139
2.71k
                fgd->ar_coeff_shift = dav1d_get_bits(gb, 2) + 6;
1140
2.71k
                fgd->grain_scale_shift = dav1d_get_bits(gb, 2);
1141
8.14k
                for (int pl = 0; pl < 2; pl++)
1142
5.43k
                    if (fgd->num_uv_points[pl]) {
1143
458
                        fgd->uv_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1144
458
                        fgd->uv_luma_mult[pl] = dav1d_get_bits(gb, 8) - 128;
1145
458
                        fgd->uv_offset[pl] = dav1d_get_bits(gb, 9) - 256;
1146
458
                    }
1147
2.71k
                fgd->overlap_flag = dav1d_get_bit(gb);
1148
2.71k
                fgd->clip_to_restricted_range = dav1d_get_bit(gb);
1149
2.71k
            }
1150
7.43k
        }
1151
14.9k
    }
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
56.8k
    return 0;
1158
1159
116
error:
1160
116
    dav1d_log(c, "Error parsing frame header\n");
1161
116
    return DAV1D_ERR(EINVAL);
1162
56.8k
}
1163
1164
53.3k
static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) {
1165
53.3k
    const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows;
1166
53.3k
    const int have_tile_pos = n_tiles > 1 ? dav1d_get_bit(gb) : 0;
1167
1168
53.3k
    if (have_tile_pos) {
1169
309
        const int n_bits = c->frame_hdr->tiling.log2_cols +
1170
309
                           c->frame_hdr->tiling.log2_rows;
1171
309
        c->tile[c->n_tile_data].start = dav1d_get_bits(gb, n_bits);
1172
309
        c->tile[c->n_tile_data].end = dav1d_get_bits(gb, n_bits);
1173
53.0k
    } else {
1174
53.0k
        c->tile[c->n_tile_data].start = 0;
1175
53.0k
        c->tile[c->n_tile_data].end = n_tiles - 1;
1176
53.0k
    }
1177
53.3k
}
1178
1179
108k
ptrdiff_t dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in) {
1180
108k
    GetBits gb;
1181
108k
    int res;
1182
1183
108k
    dav1d_init_get_bits(&gb, in->data, in->sz);
1184
1185
    // obu header
1186
108k
    const int obu_forbidden_bit = dav1d_get_bit(&gb);
1187
108k
    if (c->strict_std_compliance && obu_forbidden_bit) goto error;
1188
108k
    const enum Dav1dObuType type = dav1d_get_bits(&gb, 4);
1189
108k
    const int has_extension = dav1d_get_bit(&gb);
1190
108k
    const int has_length_field = dav1d_get_bit(&gb);
1191
108k
    dav1d_get_bit(&gb); // reserved
1192
1193
108k
    int temporal_id = 0, spatial_id = 0;
1194
108k
    if (has_extension) {
1195
25.4k
        temporal_id = dav1d_get_bits(&gb, 3);
1196
25.4k
        spatial_id = dav1d_get_bits(&gb, 2);
1197
25.4k
        dav1d_get_bits(&gb, 3); // reserved
1198
25.4k
    }
1199
1200
108k
    if (has_length_field) {
1201
108k
        const size_t len = dav1d_get_uleb128(&gb);
1202
108k
        if (len > (size_t)(gb.ptr_end - gb.ptr)) goto error;
1203
108k
        gb.ptr_end = gb.ptr + len;
1204
108k
    }
1205
108k
    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
108k
    assert(gb.bits_left == 0);
1211
1212
    // skip obu not belonging to the selected temporal/spatial layer
1213
108k
    if (type != DAV1D_OBU_SEQ_HDR && type != DAV1D_OBU_TD &&
1214
97.2k
        has_extension && c->operating_point_idc != 0)
1215
4.77k
    {
1216
4.77k
        const int in_temporal_layer = (c->operating_point_idc >> temporal_id) & 1;
1217
4.77k
        const int in_spatial_layer = (c->operating_point_idc >> (spatial_id + 8)) & 1;
1218
4.77k
        if (!in_temporal_layer || !in_spatial_layer)
1219
1.89k
            return gb.ptr_end - gb.ptr_start;
1220
4.77k
    }
1221
1222
106k
    switch (type) {
1223
10.7k
    case DAV1D_OBU_SEQ_HDR: {
1224
10.7k
        Dav1dRef *ref = dav1d_ref_create_using_pool(c->seq_hdr_pool,
1225
10.7k
                                                    sizeof(Dav1dSequenceHeader));
1226
10.7k
        if (!ref) return DAV1D_ERR(ENOMEM);
1227
10.7k
        Dav1dSequenceHeader *seq_hdr = ref->data;
1228
10.7k
        if ((res = parse_seq_hdr(seq_hdr, &gb, c->strict_std_compliance)) < 0) {
1229
43
            dav1d_log(c, "Error parsing sequence header\n");
1230
43
            dav1d_ref_dec(&ref);
1231
43
            goto error;
1232
43
        }
1233
1234
10.6k
        const int op_idx =
1235
10.6k
            c->operating_point < seq_hdr->num_operating_points ? c->operating_point : 0;
1236
10.6k
        c->operating_point_idc = seq_hdr->operating_points[op_idx].idc;
1237
10.6k
        const unsigned spatial_mask = c->operating_point_idc >> 8;
1238
10.6k
        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
10.6k
        if (!c->seq_hdr) {
1245
9.45k
            c->frame_hdr = NULL;
1246
9.45k
            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
9.45k
        } else if (memcmp(seq_hdr, c->seq_hdr, offsetof(Dav1dSequenceHeader, operating_parameter_info))) {
1250
1.11k
            c->frame_hdr = NULL;
1251
1.11k
            c->mastering_display = NULL;
1252
1.11k
            c->content_light = NULL;
1253
1.11k
            dav1d_ref_dec(&c->mastering_display_ref);
1254
1.11k
            dav1d_ref_dec(&c->content_light_ref);
1255
9.99k
            for (int i = 0; i < 8; i++) {
1256
8.88k
                if (c->refs[i].p.p.frame_hdr)
1257
1.25k
                    dav1d_thread_picture_unref(&c->refs[i].p);
1258
8.88k
                dav1d_ref_dec(&c->refs[i].segmap);
1259
8.88k
                dav1d_ref_dec(&c->refs[i].refmvs);
1260
8.88k
                dav1d_cdf_thread_unref(&c->cdf[i]);
1261
8.88k
            }
1262
1.11k
            c->frame_flags |= PICTURE_FLAG_NEW_SEQUENCE;
1263
        // If operating_parameter_info changed, signal it
1264
1.11k
        } else if (memcmp(seq_hdr->operating_parameter_info, c->seq_hdr->operating_parameter_info,
1265
106
                          sizeof(seq_hdr->operating_parameter_info)))
1266
18
        {
1267
18
            c->frame_flags |= PICTURE_FLAG_NEW_OP_PARAMS_INFO;
1268
18
        }
1269
10.6k
        dav1d_ref_dec(&c->seq_hdr_ref);
1270
10.6k
        c->seq_hdr_ref = ref;
1271
10.6k
        c->seq_hdr = seq_hdr;
1272
10.6k
        break;
1273
10.7k
    }
1274
2.34k
    case DAV1D_OBU_REDUNDANT_FRAME_HDR:
1275
2.34k
        if (c->frame_hdr) break;
1276
        // fall-through
1277
53.2k
    case DAV1D_OBU_FRAME:
1278
59.1k
    case DAV1D_OBU_FRAME_HDR:
1279
59.1k
        if (!c->seq_hdr) goto error;
1280
59.1k
        if (!c->frame_hdr_ref) {
1281
53.8k
            c->frame_hdr_ref = dav1d_ref_create_using_pool(c->frame_hdr_pool,
1282
53.8k
                                                           sizeof(Dav1dFrameHeader));
1283
53.8k
            if (!c->frame_hdr_ref) return DAV1D_ERR(ENOMEM);
1284
53.8k
        }
1285
#ifndef NDEBUG
1286
        // ensure that the reference is writable
1287
        assert(dav1d_ref_is_writable(c->frame_hdr_ref));
1288
#endif
1289
59.1k
        c->frame_hdr = c->frame_hdr_ref->data;
1290
59.1k
        memset(c->frame_hdr, 0, sizeof(*c->frame_hdr));
1291
59.1k
        c->frame_hdr->temporal_id = temporal_id;
1292
59.1k
        c->frame_hdr->spatial_id = spatial_id;
1293
59.1k
        if ((res = parse_frame_hdr(c, &gb)) < 0) {
1294
116
            c->frame_hdr = NULL;
1295
116
            goto error;
1296
116
        }
1297
59.0k
        for (int n = 0; n < c->n_tile_data; n++)
1298
23
            dav1d_data_unref_internal(&c->tile[n].data);
1299
59.0k
        c->n_tile_data = 0;
1300
59.0k
        c->n_tiles = 0;
1301
59.0k
        if (type != DAV1D_OBU_FRAME) {
1302
            // This is actually a frame header OBU so read the
1303
            // trailing bit and check for overrun.
1304
8.09k
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1305
22
                c->frame_hdr = NULL;
1306
22
                goto error;
1307
22
            }
1308
8.09k
        }
1309
1310
59.0k
        if (c->frame_size_limit && (int64_t)c->frame_hdr->width[1] *
1311
59.0k
            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
59.0k
        if (type != DAV1D_OBU_FRAME)
1320
8.06k
            break;
1321
        // OBU_FRAMEs shouldn't be signaled with show_existing_frame
1322
50.9k
        if (c->frame_hdr->show_existing_frame) {
1323
11
            c->frame_hdr = NULL;
1324
11
            goto error;
1325
11
        }
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
50.9k
        dav1d_bytealign_get_bits(&gb);
1331
        // fall-through
1332
53.4k
    case DAV1D_OBU_TILE_GRP: {
1333
53.4k
        if (!c->frame_hdr) goto error;
1334
53.3k
        if (c->n_tile_data_alloc < c->n_tile_data + 1) {
1335
9.14k
            if ((c->n_tile_data + 1) > INT_MAX / (int)sizeof(*c->tile)) goto error;
1336
9.14k
            struct Dav1dTileGroup *tile = dav1d_realloc(ALLOC_TILE, c->tile,
1337
9.14k
                                                        (c->n_tile_data + 1) * sizeof(*c->tile));
1338
9.14k
            if (!tile) goto error;
1339
9.14k
            c->tile = tile;
1340
9.14k
            memset(c->tile + c->n_tile_data, 0, sizeof(*c->tile));
1341
9.14k
            c->n_tile_data_alloc = c->n_tile_data + 1;
1342
9.14k
        }
1343
53.3k
        parse_tile_hdr(c, &gb);
1344
        // Align to the next byte boundary and check for overrun.
1345
53.3k
        dav1d_bytealign_get_bits(&gb);
1346
53.3k
        if (gb.error) goto error;
1347
1348
53.3k
        dav1d_data_ref(&c->tile[c->n_tile_data].data, in);
1349
53.3k
        c->tile[c->n_tile_data].data.data = gb.ptr;
1350
53.3k
        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
53.3k
        if (c->tile[c->n_tile_data].start > c->tile[c->n_tile_data].end ||
1353
53.3k
            c->tile[c->n_tile_data].start != c->n_tiles)
1354
13
        {
1355
33
            for (int i = 0; i <= c->n_tile_data; i++)
1356
20
                dav1d_data_unref_internal(&c->tile[i].data);
1357
13
            c->n_tile_data = 0;
1358
13
            c->n_tiles = 0;
1359
13
            goto error;
1360
13
        }
1361
53.3k
        c->n_tiles += 1 + c->tile[c->n_tile_data].end -
1362
53.3k
                          c->tile[c->n_tile_data].start;
1363
53.3k
        c->n_tile_data++;
1364
53.3k
        break;
1365
53.3k
    }
1366
814
    case DAV1D_OBU_METADATA: {
1367
814
#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
814
        const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb);
1373
814
        if (gb.error) goto error;
1374
1375
799
        switch (meta_type) {
1376
166
        case OBU_META_HDR_CLL: {
1377
166
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1378
166
                                             sizeof(Dav1dContentLightLevel));
1379
166
            if (!ref) return DAV1D_ERR(ENOMEM);
1380
166
            Dav1dContentLightLevel *const content_light = ref->data;
1381
1382
166
            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
166
            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
166
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1396
1
                dav1d_ref_dec(&ref);
1397
1
                goto error;
1398
1
            }
1399
1400
165
            dav1d_ref_dec(&c->content_light_ref);
1401
165
            c->content_light = content_light;
1402
165
            c->content_light_ref = ref;
1403
165
            break;
1404
166
        }
1405
48
        case OBU_META_HDR_MDCV: {
1406
48
            Dav1dRef *ref = dav1d_ref_create(ALLOC_OBU_META,
1407
48
                                             sizeof(Dav1dMasteringDisplay));
1408
48
            if (!ref) return DAV1D_ERR(ENOMEM);
1409
48
            Dav1dMasteringDisplay *const mastering_display = ref->data;
1410
1411
192
            for (int i = 0; i < 3; i++) {
1412
144
                mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16);
1413
144
                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
144
            }
1421
48
            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
48
            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
48
            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
48
            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
48
            if (check_trailing_bits(&gb, c->strict_std_compliance) < 0) {
1446
1
                dav1d_ref_dec(&ref);
1447
1
                goto error;
1448
1
            }
1449
1450
47
            dav1d_ref_dec(&c->mastering_display_ref);
1451
47
            c->mastering_display = mastering_display;
1452
47
            c->mastering_display_ref = ref;
1453
47
            break;
1454
48
        }
1455
54
        case OBU_META_ITUT_T35: {
1456
54
            ptrdiff_t payload_size = gb.ptr_end - gb.ptr;
1457
            // Don't take into account all the trailing bits for payload_size
1458
383
            while (payload_size > 0 && !gb.ptr[payload_size - 1])
1459
329
                payload_size--; // trailing_zero_bit x 8
1460
54
            payload_size--; // trailing_one_bit + trailing_zero_bit x 7
1461
1462
54
            int country_code_extension_byte = 0;
1463
54
            const int country_code = dav1d_get_bits(&gb, 8);
1464
54
            payload_size--;
1465
54
            if (country_code == 0xFF) {
1466
7
                country_code_extension_byte = dav1d_get_bits(&gb, 8);
1467
7
                payload_size--;
1468
7
            }
1469
1470
54
            if (payload_size <= 0 || gb.ptr[payload_size] != 0x80) {
1471
21
                dav1d_log(c, "Malformed ITU-T T.35 metadata message format\n");
1472
21
                break;
1473
21
            }
1474
1475
33
            if ((c->n_itut_t35 + 1) > INT_MAX / (int)sizeof(*c->itut_t35)) goto error;
1476
33
            uint8_t *const payload = dav1d_malloc(ALLOC_OBU_META, payload_size);
1477
33
            if (!payload) goto error;
1478
1479
33
            struct itut_t35_ctx_context *itut_t35_ctx = NULL;
1480
33
            if (!c->n_itut_t35) {
1481
30
                assert(!c->itut_t35_ref);
1482
30
                itut_t35_ctx = dav1d_malloc(ALLOC_OBU_META, sizeof(struct itut_t35_ctx_context));
1483
30
                if (!itut_t35_ctx) {
1484
0
                    dav1d_free(payload);
1485
0
                    goto error;
1486
0
                }
1487
30
            }
1488
1489
33
            struct Dav1dITUTT35 *itut_t35 = dav1d_realloc(ALLOC_OBU_META, c->itut_t35,
1490
33
                                                          (c->n_itut_t35 + 1) * sizeof(*c->itut_t35));
1491
33
            if (!itut_t35) {
1492
0
                dav1d_free(itut_t35_ctx);
1493
0
                dav1d_free(payload);
1494
0
                goto error;
1495
0
            }
1496
33
            c->itut_t35 = itut_t35;
1497
33
            memset(c->itut_t35 + c->n_itut_t35, 0, sizeof(*c->itut_t35));
1498
1499
33
            if (!c->n_itut_t35) {
1500
30
                c->itut_t35_ref = dav1d_ref_init(&itut_t35_ctx->ref, c->itut_t35,
1501
30
                                                 dav1d_picture_free_itut_t35, itut_t35_ctx, 0);
1502
30
            } else {
1503
3
                assert(c->itut_t35_ref &&
1504
3
                       atomic_load_explicit(&c->itut_t35_ref->ref_cnt, memory_order_relaxed) == 1);
1505
3
                itut_t35_ctx = c->itut_t35_ref->user_data;
1506
3
                c->itut_t35_ref->const_data = (uint8_t *)c->itut_t35;
1507
3
            }
1508
33
            itut_t35_ctx->itut_t35 = c->itut_t35;
1509
33
            itut_t35_ctx->n_itut_t35 = c->n_itut_t35 + 1;
1510
1511
33
            Dav1dITUTT35 *const itut_t35_metadata = &c->itut_t35[c->n_itut_t35];
1512
33
            itut_t35_metadata->payload = payload;
1513
1514
33
            itut_t35_metadata->country_code = country_code;
1515
33
            itut_t35_metadata->country_code_extension_byte = country_code_extension_byte;
1516
33
            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
33
            assert(gb.bits_left == 0);
1521
33
            memcpy(itut_t35_metadata->payload, gb.ptr, payload_size);
1522
1523
33
            c->n_itut_t35++;
1524
33
            break;
1525
33
        }
1526
5
        case OBU_META_SCALABILITY:
1527
8
        case OBU_META_TIMECODE:
1528
            // ignore metadata OBUs we don't care about
1529
8
            break;
1530
523
        default:
1531
            // print a warning but don't fail for unknown types
1532
523
            if (meta_type > 31) // Types 6 to 31 are "Unregistered user private", so ignore them.
1533
496
                dav1d_log(c, "Unknown Metadata OBU type %d\n", meta_type);
1534
523
            break;
1535
799
        }
1536
1537
797
        break;
1538
799
    }
1539
797
    case DAV1D_OBU_TD:
1540
750
        c->frame_flags |= PICTURE_FLAG_NEW_TEMPORAL_UNIT;
1541
750
        break;
1542
4.16k
    case DAV1D_OBU_PADDING:
1543
        // ignore OBUs we don't care about
1544
4.16k
        break;
1545
28.5k
    default:
1546
        // print a warning but don't fail for unknown types
1547
28.5k
        dav1d_log(c, "Unknown OBU type %d of size %td\n", type, gb.ptr_end - gb.ptr);
1548
28.5k
        break;
1549
106k
    }
1550
1551
106k
    if (c->seq_hdr && c->frame_hdr) {
1552
63.9k
        if (c->frame_hdr->show_existing_frame) {
1553
2.16k
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr) goto error;
1554
2.15k
            switch (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type) {
1555
251
            case DAV1D_FRAME_TYPE_INTER:
1556
287
            case DAV1D_FRAME_TYPE_SWITCH:
1557
287
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE)
1558
0
                    goto skip;
1559
287
                break;
1560
287
            case DAV1D_FRAME_TYPE_INTRA:
1561
150
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA)
1562
0
                    goto skip;
1563
                // fall-through
1564
1.87k
            default:
1565
1.87k
                break;
1566
2.15k
            }
1567
2.15k
            if (!c->refs[c->frame_hdr->existing_frame_idx].p.p.data[0]) goto error;
1568
2.15k
            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
2.15k
            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
2.15k
            } else {
1587
2.15k
                pthread_mutex_lock(&c->task_thread.lock);
1588
                // need to append this to the frame output queue
1589
2.15k
                const unsigned next = c->frame_thread.next++;
1590
2.15k
                if (c->frame_thread.next == c->n_fc)
1591
282
                    c->frame_thread.next = 0;
1592
1593
2.15k
                Dav1dFrameContext *const f = &c->fc[next];
1594
2.31k
                while (f->n_tile_data > 0)
1595
154
                    pthread_cond_wait(&f->task_thread.cond,
1596
154
                                      &f->task_thread.ttd->lock);
1597
2.15k
                Dav1dThreadPicture *const out_delayed =
1598
2.15k
                    &c->frame_thread.out_delayed[next];
1599
2.15k
                if (out_delayed->p.data[0] ||
1600
2.15k
                    atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
1601
467
                {
1602
467
                    unsigned first = atomic_load_explicit(&c->task_thread.first,
1603
467
                                                          memory_order_relaxed);
1604
467
                    if (first + 1U < c->n_fc)
1605
467
                        atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed);
1606
44
                    else
1607
467
                        atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed);
1608
467
                    atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur,
1609
467
                                                            &first, UINT_MAX,
1610
467
                                                            memory_order_relaxed,
1611
467
                                                            memory_order_relaxed);
1612
467
                    if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
1613
93
                        c->task_thread.cur--;
1614
467
                }
1615
2.15k
                const int error = f->task_thread.retval;
1616
2.15k
                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
2.15k
                } else if (out_delayed->p.data[0]) {
1622
467
                    const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
1623
467
                                                                   memory_order_relaxed);
1624
467
                    if ((out_delayed->visible || c->output_invisible_frames) &&
1625
452
                        progress != FRAME_ERROR)
1626
452
                    {
1627
452
                        dav1d_thread_picture_ref(&c->out, out_delayed);
1628
452
                        c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
1629
452
                    }
1630
467
                    dav1d_thread_picture_unref(out_delayed);
1631
467
                }
1632
2.15k
                dav1d_thread_picture_ref(out_delayed,
1633
2.15k
                                         &c->refs[c->frame_hdr->existing_frame_idx].p);
1634
2.15k
                out_delayed->visible = 1;
1635
2.15k
                dav1d_picture_copy_props(&out_delayed->p,
1636
2.15k
                                         c->content_light, c->content_light_ref,
1637
2.15k
                                         c->mastering_display, c->mastering_display_ref,
1638
2.15k
                                         c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
1639
2.15k
                                         &in->m);
1640
                // Must be removed from the context after being attached to the frame
1641
2.15k
                dav1d_ref_dec(&c->itut_t35_ref);
1642
2.15k
                c->itut_t35 = NULL;
1643
2.15k
                c->n_itut_t35 = 0;
1644
1645
2.15k
                pthread_mutex_unlock(&c->task_thread.lock);
1646
2.15k
            }
1647
2.15k
            if (c->refs[c->frame_hdr->existing_frame_idx].p.p.frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY) {
1648
1.72k
                const int r = c->frame_hdr->existing_frame_idx;
1649
1.72k
                c->refs[r].p.showable = 0;
1650
15.4k
                for (int i = 0; i < 8; i++) {
1651
13.7k
                    if (i == r) continue;
1652
1653
12.0k
                    if (c->refs[i].p.p.frame_hdr)
1654
11.9k
                        dav1d_thread_picture_unref(&c->refs[i].p);
1655
12.0k
                    dav1d_thread_picture_ref(&c->refs[i].p, &c->refs[r].p);
1656
1657
12.0k
                    dav1d_cdf_thread_unref(&c->cdf[i]);
1658
12.0k
                    dav1d_cdf_thread_ref(&c->cdf[i], &c->cdf[r]);
1659
1660
12.0k
                    dav1d_ref_dec(&c->refs[i].segmap);
1661
12.0k
                    c->refs[i].segmap = c->refs[r].segmap;
1662
12.0k
                    if (c->refs[r].segmap)
1663
8.44k
                        dav1d_ref_inc(c->refs[r].segmap);
1664
12.0k
                    dav1d_ref_dec(&c->refs[i].refmvs);
1665
12.0k
                }
1666
1.72k
            }
1667
2.15k
            c->frame_hdr = NULL;
1668
61.7k
        } else if (c->n_tiles == c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows) {
1669
53.2k
            switch (c->frame_hdr->frame_type) {
1670
35.7k
            case DAV1D_FRAME_TYPE_INTER:
1671
35.8k
            case DAV1D_FRAME_TYPE_SWITCH:
1672
35.8k
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_REFERENCE ||
1673
35.8k
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1674
0
                     !c->frame_hdr->refresh_frame_flags))
1675
0
                    goto skip;
1676
35.8k
                break;
1677
35.8k
            case DAV1D_FRAME_TYPE_INTRA:
1678
834
                if (c->decode_frame_type > DAV1D_DECODEFRAMETYPE_INTRA ||
1679
834
                    (c->decode_frame_type == DAV1D_DECODEFRAMETYPE_REFERENCE &&
1680
0
                     !c->frame_hdr->refresh_frame_flags))
1681
0
                    goto skip;
1682
                // fall-through
1683
17.3k
            default:
1684
17.3k
                break;
1685
53.2k
            }
1686
53.2k
            if (!c->n_tile_data)
1687
0
                goto error;
1688
53.2k
            if ((res = dav1d_submit_frame(c)) < 0)
1689
58
                return res;
1690
53.2k
            assert(!c->n_tile_data);
1691
53.2k
            c->frame_hdr = NULL;
1692
53.2k
            c->n_tiles = 0;
1693
53.2k
        }
1694
63.9k
    }
1695
1696
106k
    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
325
error:
1719
325
    dav1d_data_props_copy(&c->cached_error_props, &in->m);
1720
325
    dav1d_log(c, gb.error ? "Overrun in OBU bit buffer\n" :
1721
325
                            "Error parsing OBU data\n");
1722
325
    return DAV1D_ERR(EINVAL);
1723
106k
}