obu.c 64.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include <errno.h>
#include <limits.h>
#include <stdio.h>

#include "dav1d/data.h"

#include "common/intops.h"

#include "src/decode.h"
#include "src/getbits.h"
#include "src/levels.h"
James Almer's avatar
James Almer committed
41
#include "src/log.h"
42
43
#include "src/obu.h"
#include "src/ref.h"
44
#include "src/thread_task.h"
45

46
static int parse_seq_hdr(Dav1dContext *const c, GetBits *const gb,
47
                         Dav1dSequenceHeader *const hdr)
48
{
49
50
#define DEBUG_SEQ_HDR 0

51
52
53
54
#if DEBUG_SEQ_HDR
    const unsigned init_bit_pos = dav1d_get_bits_pos(gb);
#endif

55
    hdr->profile = dav1d_get_bits(gb, 3);
56
    if (hdr->profile > 2) goto error;
57
58
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-profile: off=%ld\n",
59
           dav1d_get_bits_pos(gb) - init_bit_pos);
60
61
#endif

62
63
    hdr->still_picture = dav1d_get_bits(gb, 1);
    hdr->reduced_still_picture_header = dav1d_get_bits(gb, 1);
64
65
66
    if (hdr->reduced_still_picture_header && !hdr->still_picture) goto error;
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-stillpicture_flags: off=%ld\n",
67
           dav1d_get_bits_pos(gb) - init_bit_pos);
68
69
70
71
72
73
74
75
#endif

    if (hdr->reduced_still_picture_header) {
        hdr->timing_info_present = 0;
        hdr->decoder_model_info_present = 0;
        hdr->display_model_info_present = 0;
        hdr->num_operating_points = 1;
        hdr->operating_points[0].idc = 0;
76
77
        hdr->operating_points[0].major_level = dav1d_get_bits(gb, 3);
        hdr->operating_points[0].minor_level = dav1d_get_bits(gb, 2);
78
79
80
81
        hdr->operating_points[0].tier = 0;
        hdr->operating_points[0].decoder_model_param_present = 0;
        hdr->operating_points[0].display_model_param_present = 0;
    } else {
82
        hdr->timing_info_present = dav1d_get_bits(gb, 1);
83
        if (hdr->timing_info_present) {
84
85
86
            hdr->num_units_in_tick = dav1d_get_bits(gb, 32);
            hdr->time_scale = dav1d_get_bits(gb, 32);
            hdr->equal_picture_interval = dav1d_get_bits(gb, 1);
87
88
89
90
91
92
            if (hdr->equal_picture_interval) {
                unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
                if (num_ticks_per_picture == 0xFFFFFFFFU)
                    goto error;
                hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
            }
93

94
            hdr->decoder_model_info_present = dav1d_get_bits(gb, 1);
95
            if (hdr->decoder_model_info_present) {
96
97
98
99
                hdr->encoder_decoder_buffer_delay_length = dav1d_get_bits(gb, 5) + 1;
                hdr->num_units_in_decoding_tick = dav1d_get_bits(gb, 32);
                hdr->buffer_removal_delay_length = dav1d_get_bits(gb, 5) + 1;
                hdr->frame_presentation_delay_length = dav1d_get_bits(gb, 5) + 1;
100
101
102
103
104
105
            }
        } else {
            hdr->decoder_model_info_present = 0;
        }
#if DEBUG_SEQ_HDR
        printf("SEQHDR: post-timinginfo: off=%ld\n",
106
               dav1d_get_bits_pos(gb) - init_bit_pos);
107
108
#endif

109
110
        hdr->display_model_info_present = dav1d_get_bits(gb, 1);
        hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1;
111
        for (int i = 0; i < hdr->num_operating_points; i++) {
112
            struct Dav1dSequenceHeaderOperatingPoint *const op =
113
                &hdr->operating_points[i];
114
115
            struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
                &hdr->operating_parameter_info[i];
116
117
118
119
            op->idc = dav1d_get_bits(gb, 12);
            op->major_level = 2 + dav1d_get_bits(gb, 3);
            op->minor_level = dav1d_get_bits(gb, 2);
            op->tier = op->major_level > 3 ? dav1d_get_bits(gb, 1) : 0;
120
            op->decoder_model_param_present =
121
                hdr->decoder_model_info_present && dav1d_get_bits(gb, 1);
122
            if (op->decoder_model_param_present) {
123
                opi->decoder_buffer_delay =
124
                    dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
125
                opi->encoder_buffer_delay =
126
                    dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
127
                opi->low_delay_mode = dav1d_get_bits(gb, 1);
128
129
            }
            op->display_model_param_present =
130
                hdr->display_model_info_present && dav1d_get_bits(gb, 1);
131
            if (op->display_model_param_present) {
132
                op->initial_display_delay = dav1d_get_bits(gb, 4) + 1;
133
134
            }
        }
135
136
137
138
        if (c->operating_point < hdr->num_operating_points)
            c->operating_point_idc = hdr->operating_points[c->operating_point].idc;
        else
            c->operating_point_idc = hdr->operating_points[0].idc;
139
140
#if DEBUG_SEQ_HDR
        printf("SEQHDR: post-operating-points: off=%ld\n",
141
               dav1d_get_bits_pos(gb) - init_bit_pos);
142
143
144
#endif
    }

145
146
147
148
    hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1;
    hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1;
    hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1;
    hdr->max_height = dav1d_get_bits(gb, hdr->height_n_bits) + 1;
149
150
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-size: off=%ld\n",
151
           dav1d_get_bits_pos(gb) - init_bit_pos);
152
153
#endif
    hdr->frame_id_numbers_present =
154
        hdr->reduced_still_picture_header ? 0 : dav1d_get_bits(gb, 1);
155
    if (hdr->frame_id_numbers_present) {
156
157
        hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2;
        hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1;
158
159
160
    }
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-frame-id-numbers-present: off=%ld\n",
161
           dav1d_get_bits_pos(gb) - init_bit_pos);
162
163
#endif

164
165
166
    hdr->sb128 = dav1d_get_bits(gb, 1);
    hdr->filter_intra = dav1d_get_bits(gb, 1);
    hdr->intra_edge_filter = dav1d_get_bits(gb, 1);
167
168
169
170
171
172
173
174
175
    if (hdr->reduced_still_picture_header) {
        hdr->inter_intra = 0;
        hdr->masked_compound = 0;
        hdr->warped_motion = 0;
        hdr->dual_filter = 0;
        hdr->order_hint = 0;
        hdr->jnt_comp = 0;
        hdr->ref_frame_mvs = 0;
        hdr->order_hint_n_bits = 0;
176
177
        hdr->screen_content_tools = DAV1D_ADAPTIVE;
        hdr->force_integer_mv = DAV1D_ADAPTIVE;
178
    } else {
179
180
181
182
183
        hdr->inter_intra = dav1d_get_bits(gb, 1);
        hdr->masked_compound = dav1d_get_bits(gb, 1);
        hdr->warped_motion = dav1d_get_bits(gb, 1);
        hdr->dual_filter = dav1d_get_bits(gb, 1);
        hdr->order_hint = dav1d_get_bits(gb, 1);
184
        if (hdr->order_hint) {
185
186
            hdr->jnt_comp = dav1d_get_bits(gb, 1);
            hdr->ref_frame_mvs = dav1d_get_bits(gb, 1);
187
188
189
190
191
        } else {
            hdr->jnt_comp = 0;
            hdr->ref_frame_mvs = 0;
            hdr->order_hint_n_bits = 0;
        }
192
        hdr->screen_content_tools = dav1d_get_bits(gb, 1) ? DAV1D_ADAPTIVE : dav1d_get_bits(gb, 1);
193
194
    #if DEBUG_SEQ_HDR
        printf("SEQHDR: post-screentools: off=%ld\n",
195
               dav1d_get_bits_pos(gb) - init_bit_pos);
196
197
    #endif
        hdr->force_integer_mv = hdr->screen_content_tools ?
198
                                dav1d_get_bits(gb, 1) ? DAV1D_ADAPTIVE : dav1d_get_bits(gb, 1) : 2;
199
        if (hdr->order_hint)
200
            hdr->order_hint_n_bits = dav1d_get_bits(gb, 3) + 1;
201
    }
202
203
204
    hdr->super_res = dav1d_get_bits(gb, 1);
    hdr->cdef = dav1d_get_bits(gb, 1);
    hdr->restoration = dav1d_get_bits(gb, 1);
205
206
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-featurebits: off=%ld\n",
207
           dav1d_get_bits_pos(gb) - init_bit_pos);
208
209
#endif

210
211
212
    hdr->hbd = dav1d_get_bits(gb, 1);
    if (hdr->profile == 2 && hdr->hbd) hdr->hbd += dav1d_get_bits(gb, 1);
    hdr->monochrome = hdr->profile != 1 ? dav1d_get_bits(gb, 1) : 0;
213
    hdr->color_description_present = dav1d_get_bits(gb, 1);
214
    if (hdr->color_description_present) {
215
216
217
        hdr->pri = dav1d_get_bits(gb, 8);
        hdr->trc = dav1d_get_bits(gb, 8);
        hdr->mtrx = dav1d_get_bits(gb, 8);
218
219
220
221
222
    } else {
        hdr->pri = DAV1D_COLOR_PRI_UNKNOWN;
        hdr->trc = DAV1D_TRC_UNKNOWN;
        hdr->mtrx = DAV1D_MC_UNKNOWN;
    }
223
    if (hdr->monochrome) {
224
        hdr->color_range = dav1d_get_bits(gb, 1);
225
        hdr->layout = DAV1D_PIXEL_LAYOUT_I400;
226
        hdr->ss_hor = hdr->ss_ver = 1;
227
228
229
230
231
232
        hdr->chr = DAV1D_CHR_UNKNOWN;
        hdr->separate_uv_delta_q = 0;
    } else if (hdr->pri == DAV1D_COLOR_PRI_BT709 &&
               hdr->trc == DAV1D_TRC_SRGB &&
               hdr->mtrx == DAV1D_MC_IDENTITY)
    {
233
        hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
234
        hdr->ss_hor = hdr->ss_ver = 0;
235
        hdr->color_range = 1;
236
        if (hdr->profile != 1 && !(hdr->profile == 2 && hdr->hbd == 2))
237
238
            goto error;
    } else {
239
        hdr->color_range = dav1d_get_bits(gb, 1);
240
        switch (hdr->profile) {
241
242
243
244
245
246
        case 0: hdr->layout = DAV1D_PIXEL_LAYOUT_I420;
                hdr->ss_hor = hdr->ss_ver = 1;
                break;
        case 1: hdr->layout = DAV1D_PIXEL_LAYOUT_I444;
                hdr->ss_hor = hdr->ss_ver = 0;
                break;
247
        case 2:
248
249
250
251
252
253
254
            if (hdr->hbd == 2) {
                hdr->ss_hor = dav1d_get_bits(gb, 1);
                hdr->ss_ver = hdr->ss_hor && dav1d_get_bits(gb, 1);
            } else {
                hdr->ss_hor = 1;
                hdr->ss_ver = 0;
            }
255
256
257
258
            hdr->layout = hdr->ss_hor ?
                          hdr->ss_ver ? DAV1D_PIXEL_LAYOUT_I420 :
                                        DAV1D_PIXEL_LAYOUT_I422 :
                                        DAV1D_PIXEL_LAYOUT_I444;
259
260
            break;
        }
261
262
        hdr->chr = hdr->ss_hor == 1 && hdr->ss_ver == 1 ?
                   dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN;
263
    }
264
    hdr->separate_uv_delta_q = !hdr->monochrome && dav1d_get_bits(gb, 1);
265
266
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-colorinfo: off=%ld\n",
267
           dav1d_get_bits_pos(gb) - init_bit_pos);
268
269
#endif

270
    hdr->film_grain_present = dav1d_get_bits(gb, 1);
271
272
#if DEBUG_SEQ_HDR
    printf("SEQHDR: post-filmgrain: off=%ld\n",
273
           dav1d_get_bits_pos(gb) - init_bit_pos);
274
275
#endif

276
    dav1d_get_bits(gb, 1); // dummy bit
277

278
279
280
281
282
    // We needn't bother flushing the OBU here: we'll check we didn't
    // overrun in the caller and will then discard gb, so there's no
    // point in setting its position properly.

    return 0;
283
284

error:
285
    dav1d_log(c, "Error parsing sequence header\n");
286
    return DAV1D_ERR(EINVAL);
287
288
289
290
291
}

static int read_frame_size(Dav1dContext *const c, GetBits *const gb,
                           const int use_ref)
{
292
293
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
    Dav1dFrameHeader *const hdr = c->frame_hdr;
294
295
296

    if (use_ref) {
        for (int i = 0; i < 7; i++) {
297
            if (dav1d_get_bits(gb, 1)) {
298
                Dav1dThreadPicture *const ref =
Ronald S. Bultje's avatar
Ronald S. Bultje committed
299
                    &c->refs[c->frame_hdr->refidx[i]].p;
300
                if (!ref->p.data[0]) return -1;
301
302
303
304
                hdr->width[1] = ref->p.p.w;
                hdr->height = ref->p.p.h;
                hdr->render_width = ref->p.frame_hdr->render_width;
                hdr->render_height = ref->p.frame_hdr->render_height;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
305
306
307
308
309
310
311
312
313
314
                hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bits(gb, 1);
                if (hdr->super_res.enabled) {
                    const int d = hdr->super_res.width_scale_denominator =
                        9 + dav1d_get_bits(gb, 3);
                    hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d,
                                         imin(16, hdr->width[1]));
                } else {
                    hdr->super_res.width_scale_denominator = 8;
                    hdr->width[0] = hdr->width[1];
                }
315
316
317
318
319
320
                return 0;
            }
        }
    }

    if (hdr->frame_size_override) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
321
        hdr->width[1] = dav1d_get_bits(gb, seqhdr->width_n_bits) + 1;
322
        hdr->height = dav1d_get_bits(gb, seqhdr->height_n_bits) + 1;
323
    } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
324
        hdr->width[1] = seqhdr->max_width;
325
326
        hdr->height = seqhdr->max_height;
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
327
328
329
330
331
332
333
334
    hdr->super_res.enabled = seqhdr->super_res && dav1d_get_bits(gb, 1);
    if (hdr->super_res.enabled) {
        const int d = hdr->super_res.width_scale_denominator = 9 + dav1d_get_bits(gb, 3);
        hdr->width[0] = imax((hdr->width[1] * 8 + (d >> 1)) / d, imin(16, hdr->width[1]));
    } else {
        hdr->super_res.width_scale_denominator = 8;
        hdr->width[0] = hdr->width[1];
    }
335
    hdr->have_render_size = dav1d_get_bits(gb, 1);
336
    if (hdr->have_render_size) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
337
338
        hdr->render_width = dav1d_get_bits(gb, 16) + 1;
        hdr->render_height = dav1d_get_bits(gb, 16) + 1;
339
    } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
340
        hdr->render_width = hdr->width[1];
341
342
343
344
345
346
347
348
349
350
351
        hdr->render_height = hdr->height;
    }
    return 0;
}

static inline int tile_log2(int sz, int tgt) {
    int k;
    for (k = 0; (sz << k) < tgt; k++) ;
    return k;
}

352
static const Dav1dLoopfilterModeRefDeltas default_mode_ref_deltas = {
353
354
355
356
    .mode_delta = { 0, 0 },
    .ref_delta = { 1, 0, 0, 0, -1, 0, -1, -1 },
};

357
358
359
360
static int parse_frame_hdr(Dav1dContext *const c, GetBits *const gb) {
#define DEBUG_FRAME_HDR 0

#if DEBUG_FRAME_HDR
361
    const uint8_t *const init_ptr = gb->ptr;
362
#endif
363
364
    const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
    Dav1dFrameHeader *const hdr = c->frame_hdr;
365
366
367
    int res;

    hdr->show_existing_frame =
368
        !seqhdr->reduced_still_picture_header && dav1d_get_bits(gb, 1);
369
370
371
372
373
#if DEBUG_FRAME_HDR
    printf("HDR: post-show_existing_frame: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
    if (hdr->show_existing_frame) {
374
        hdr->existing_frame_idx = dav1d_get_bits(gb, 3);
375
376
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
377
        if (seqhdr->frame_id_numbers_present)
378
            hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
379
        return 0;
380
381
    }

382
383
    hdr->frame_type = seqhdr->reduced_still_picture_header ? DAV1D_FRAME_TYPE_KEY : dav1d_get_bits(gb, 2);
    hdr->show_frame = seqhdr->reduced_still_picture_header || dav1d_get_bits(gb, 1);
384
385
386
387
    if (hdr->show_frame) {
        if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval)
            hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length);
    } else
388
        hdr->showable_frame = dav1d_get_bits(gb, 1);
389
    hdr->error_resilient_mode =
390
391
        (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) ||
        hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ||
392
        seqhdr->reduced_still_picture_header || dav1d_get_bits(gb, 1);
393
394
395
396
#if DEBUG_FRAME_HDR
    printf("HDR: post-frametype_bits: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
397
    hdr->disable_cdf_update = dav1d_get_bits(gb, 1);
398
    hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ?
399
                                 dav1d_get_bits(gb, 1) : seqhdr->screen_content_tools;
400
    if (hdr->allow_screen_content_tools)
401
        hdr->force_integer_mv = seqhdr->force_integer_mv == DAV1D_ADAPTIVE ?
402
                                dav1d_get_bits(gb, 1) : seqhdr->force_integer_mv;
403
404
405
406
407
    else
        hdr->force_integer_mv = 0;

    if (!(hdr->frame_type & 1))
        hdr->force_integer_mv = 1;
408
409

    if (seqhdr->frame_id_numbers_present)
410
        hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits);
411
412

    hdr->frame_size_override = seqhdr->reduced_still_picture_header ? 0 :
413
                               hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 1 : dav1d_get_bits(gb, 1);
414
415
416
417
418
#if DEBUG_FRAME_HDR
    printf("HDR: post-frame_size_override_flag: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
    hdr->frame_offset = seqhdr->order_hint ?
419
                        dav1d_get_bits(gb, seqhdr->order_hint_n_bits) : 0;
420
    hdr->primary_ref_frame = !hdr->error_resilient_mode && hdr->frame_type & 1 ?
421
                             dav1d_get_bits(gb, 3) : DAV1D_PRIMARY_REF_NONE;
422

423
424
425
    if (seqhdr->decoder_model_info_present) {
        hdr->buffer_removal_time_present = dav1d_get_bits(gb, 1);
        if (hdr->buffer_removal_time_present) {
426
            for (int i = 0; i < c->seq_hdr->num_operating_points; i++) {
427
428
                const struct Dav1dSequenceHeaderOperatingPoint *const seqop = &seqhdr->operating_points[i];
                struct Dav1dFrameHeaderOperatingPoint *const op = &hdr->operating_points[i];
429
                if (seqop->decoder_model_param_present) {
430
431
432
                    int in_temporal_layer = (seqop->idc >> hdr->temporal_id) & 1;
                    int in_spatial_layer  = (seqop->idc >> (hdr->spatial_id + 8)) & 1;
                    if (!seqop->idc || (in_temporal_layer && in_spatial_layer))
433
434
435
436
437
438
                        op->buffer_removal_time = dav1d_get_bits(gb, seqhdr->buffer_removal_delay_length);
                }
            }
        }
    }

James Almer's avatar
James Almer committed
439
440
441
442
443
    if (hdr->frame_type == DAV1D_FRAME_TYPE_KEY ||
        hdr->frame_type == DAV1D_FRAME_TYPE_INTRA)
    {
        hdr->refresh_frame_flags = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY &&
                                    hdr->show_frame) ? 0xff : dav1d_get_bits(gb, 8);
444
445
446
        if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
            for (int i = 0; i < 8; i++)
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
447
448
        if ((res = read_frame_size(c, gb, 0)) < 0) goto error;
        hdr->allow_intrabc = hdr->allow_screen_content_tools &&
Ronald S. Bultje's avatar
Ronald S. Bultje committed
449
                             !hdr->super_res.enabled && dav1d_get_bits(gb, 1);
450
451
        hdr->use_ref_frame_mvs = 0;
    } else {
James Almer's avatar
James Almer committed
452
453
454
455
456
457
458
459
        hdr->allow_intrabc = 0;
        hdr->refresh_frame_flags = hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 0xff :
                                   dav1d_get_bits(gb, 8);
        if (hdr->error_resilient_mode && seqhdr->order_hint)
            for (int i = 0; i < 8; i++)
                dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
        hdr->frame_ref_short_signaling =
            seqhdr->order_hint && dav1d_get_bits(gb, 1);
460
461
462
463
464
465
466
467
        if (hdr->frame_ref_short_signaling) { // FIXME: Nearly verbatim copy from section 7.8
            hdr->refidx[0] = dav1d_get_bits(gb, 3);
            hdr->refidx[1] = hdr->refidx[2] = -1;
            hdr->refidx[3] = dav1d_get_bits(gb, 3);
            hdr->refidx[4] = hdr->refidx[5] = hdr->refidx[6] = -1;

            int shifted_frame_offset[8];
            const int current_frame_offset = 1 << (seqhdr->order_hint_n_bits - 1);
468
469
            for (int i = 0; i < 8; i++) {
                if (!c->refs[i].p.p.frame_hdr) goto error;
470
471
472
473
                shifted_frame_offset[i] = current_frame_offset +
                    get_poc_diff(seqhdr->order_hint_n_bits,
                                 c->refs[i].p.p.frame_hdr->frame_offset,
                                 hdr->frame_offset);
474
            }
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

            int used_frame[8] = { 0 };
            used_frame[hdr->refidx[0]] = 1;
            used_frame[hdr->refidx[3]] = 1;

            int latest_frame_offset = -1;
            for (int i = 0; i < 8; i++) {
                int hint = shifted_frame_offset[i];
                if (!used_frame[i] && hint >= current_frame_offset &&
                    hint >= latest_frame_offset)
                {
                    hdr->refidx[6] = i;
                    latest_frame_offset = hint;
                }
            }
            if (latest_frame_offset != -1)
                used_frame[hdr->refidx[6]] = 1;

            int earliest_frame_offset = INT_MAX;
            for (int i = 0; i < 8; i++) {
                int hint = shifted_frame_offset[i];
                if (!used_frame[i] && hint >= current_frame_offset &&
                    hint < earliest_frame_offset)
                {
                    hdr->refidx[4] = i;
                    earliest_frame_offset = hint;
                }
            }
            if (earliest_frame_offset != INT_MAX)
                used_frame[hdr->refidx[4]] = 1;

            earliest_frame_offset = INT_MAX;
            for (int i = 0; i < 8; i++) {
                int hint = shifted_frame_offset[i];
                if (!used_frame[i] && hint >= current_frame_offset &&
                    (hint < earliest_frame_offset))
                {
                    hdr->refidx[5] = i;
                    earliest_frame_offset = hint;
                }
            }
            if (earliest_frame_offset != INT_MAX)
                used_frame[hdr->refidx[5]] = 1;

            for (int i = 1; i < 7; i++) {
                if (hdr->refidx[i] < 0) {
                    latest_frame_offset = -1;
                    for (int j = 0; j < 8; j++) {
                        int hint = shifted_frame_offset[j];
                        if (!used_frame[j] && hint < current_frame_offset &&
                            hint >= latest_frame_offset)
                        {
                            hdr->refidx[i] = j;
                            latest_frame_offset = hint;
                        }
                    }
                    if (latest_frame_offset != -1)
                        used_frame[hdr->refidx[i]] = 1;
                }
            }

            earliest_frame_offset = INT_MAX;
            int ref = -1;
            for (int i = 0; i < 8; i++) {
                int hint = shifted_frame_offset[i];
                if (hint < earliest_frame_offset) {
                    ref = i;
                    earliest_frame_offset = hint;
                }
            }
            for (int i = 0; i < 7; i++) {
                if (hdr->refidx[i] < 0)
                    hdr->refidx[i] = ref;
            }
        }
James Almer's avatar
James Almer committed
550
        for (int i = 0; i < 7; i++) {
551
552
            if (!hdr->frame_ref_short_signaling)
                hdr->refidx[i] = dav1d_get_bits(gb, 3);
James Almer's avatar
James Almer committed
553
554
            if (seqhdr->frame_id_numbers_present)
                dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits);
555
        }
James Almer's avatar
James Almer committed
556
557
558
559
        const int use_ref = !hdr->error_resilient_mode &&
                            hdr->frame_size_override;
        if ((res = read_frame_size(c, gb, use_ref)) < 0) goto error;
        hdr->hp = !hdr->force_integer_mv && dav1d_get_bits(gb, 1);
560
        hdr->subpel_filter_mode = dav1d_get_bits(gb, 1) ? DAV1D_FILTER_SWITCHABLE :
James Almer's avatar
James Almer committed
561
562
563
564
565
                                                          dav1d_get_bits(gb, 2);
        hdr->switchable_motion_mode = dav1d_get_bits(gb, 1);
        hdr->use_ref_frame_mvs = !hdr->error_resilient_mode &&
            seqhdr->ref_frame_mvs && seqhdr->order_hint &&
            hdr->frame_type & 1 && dav1d_get_bits(gb, 1);
566
567
568
569
570
571
572
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-frametype-specific-bits: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    hdr->refresh_context = !seqhdr->reduced_still_picture_header &&
573
                           !hdr->disable_cdf_update && !dav1d_get_bits(gb, 1);
574
575
576
577
578
579
#if DEBUG_FRAME_HDR
    printf("HDR: post-refresh_context: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // tile data
580
    hdr->tiling.uniform = dav1d_get_bits(gb, 1);
581
582
    const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
    int sbsz_log2 = 6 + seqhdr->sb128;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
583
    int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
584
    int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
585
    int max_tile_width_sb = 4096 >> sbsz_log2;
586
587
    int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
    hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
588
589
    hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
    hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
590
591
592
593
    int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
                              hdr->tiling.min_log2_cols);
    if (hdr->tiling.uniform) {
        for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
594
             hdr->tiling.log2_cols < hdr->tiling.max_log2_cols && dav1d_get_bits(gb, 1);
595
596
597
598
599
600
601
602
603
             hdr->tiling.log2_cols++) ;
        const int tile_w = 1 + ((sbw - 1) >> hdr->tiling.log2_cols);
        hdr->tiling.cols = 0;
        for (int sbx = 0; sbx < sbw; sbx += tile_w, hdr->tiling.cols++)
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
        hdr->tiling.min_log2_rows =
            imax(min_log2_tiles - hdr->tiling.log2_cols, 0);

        for (hdr->tiling.log2_rows = hdr->tiling.min_log2_rows;
604
             hdr->tiling.log2_rows < hdr->tiling.max_log2_rows && dav1d_get_bits(gb, 1);
605
606
607
608
609
610
611
612
             hdr->tiling.log2_rows++) ;
        const int tile_h = 1 + ((sbh - 1) >> hdr->tiling.log2_rows);
        hdr->tiling.rows = 0;
        for (int sby = 0; sby < sbh; sby += tile_h, hdr->tiling.rows++)
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
    } else {
        hdr->tiling.cols = 0;
        int widest_tile = 0, max_tile_area_sb = sbw * sbh;
613
        for (int sbx = 0; sbx < sbw && hdr->tiling.cols < DAV1D_MAX_TILE_COLS; hdr->tiling.cols++) {
Luc Trudeau's avatar
Luc Trudeau committed
614
615
616
617
            const int tile_width_sb = imin(sbw - sbx, max_tile_width_sb);
            const int tile_w = (tile_width_sb > 1) ?
                                   1 + dav1d_get_uniform(gb, tile_width_sb) :
                                   1;
618
619
620
621
622
623
            hdr->tiling.col_start_sb[hdr->tiling.cols] = sbx;
            sbx += tile_w;
            widest_tile = imax(widest_tile, tile_w);
        }
        hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
        if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
624
        int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
625
626

        hdr->tiling.rows = 0;
627
        for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
Luc Trudeau's avatar
Luc Trudeau committed
628
629
630
631
            const int tile_height_sb = imin(sbh - sby, max_tile_height_sb);
            const int tile_h = (tile_height_sb > 1) ?
                                   1 + dav1d_get_uniform(gb, tile_height_sb) :
                                   1;
632
633
634
635
636
637
638
639
            hdr->tiling.row_start_sb[hdr->tiling.rows] = sby;
            sby += tile_h;
        }
        hdr->tiling.log2_rows = tile_log2(1, hdr->tiling.rows);
    }
    hdr->tiling.col_start_sb[hdr->tiling.cols] = sbw;
    hdr->tiling.row_start_sb[hdr->tiling.rows] = sbh;
    if (hdr->tiling.log2_cols || hdr->tiling.log2_rows) {
640
641
        hdr->tiling.update = dav1d_get_bits(gb, hdr->tiling.log2_cols +
                                                hdr->tiling.log2_rows);
642
643
        if (hdr->tiling.update >= hdr->tiling.cols * hdr->tiling.rows)
            goto error;
644
        hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1;
645
646
647
648
649
650
651
652
653
    } else {
        hdr->tiling.n_bytes = hdr->tiling.update = 0;
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-tiling: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // quant data
654
655
    hdr->quant.yac = dav1d_get_bits(gb, 8);
    hdr->quant.ydc_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
656
    if (!seqhdr->monochrome) {
657
658
659
660
        // If the sequence header says that delta_q might be different
        // for U, V, we must check whether it actually is for this
        // frame.
        int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 1) : 0;
661
662
        hdr->quant.udc_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
        hdr->quant.uac_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
663
        if (diff_uv_delta) {
664
665
            hdr->quant.vdc_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
            hdr->quant.vac_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
666
667
668
669
670
671
672
673
674
        } else {
            hdr->quant.vdc_delta = hdr->quant.udc_delta;
            hdr->quant.vac_delta = hdr->quant.uac_delta;
        }
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-quant: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
675
    hdr->quant.qm = dav1d_get_bits(gb, 1);
676
    if (hdr->quant.qm) {
677
678
        hdr->quant.qm_y = dav1d_get_bits(gb, 4);
        hdr->quant.qm_u = dav1d_get_bits(gb, 4);
skal's avatar
skal committed
679
680
681
        hdr->quant.qm_v =
            seqhdr->separate_uv_delta_q ? (int)dav1d_get_bits(gb, 4) :
                                          hdr->quant.qm_u;
682
683
684
685
686
687
688
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-qm: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // segmentation data
689
    hdr->segmentation.enabled = dav1d_get_bits(gb, 1);
690
    if (hdr->segmentation.enabled) {
691
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
692
693
694
695
            hdr->segmentation.update_map = 1;
            hdr->segmentation.temporal = 0;
            hdr->segmentation.update_data = 1;
        } else {
696
            hdr->segmentation.update_map = dav1d_get_bits(gb, 1);
697
            hdr->segmentation.temporal =
698
699
                hdr->segmentation.update_map ? dav1d_get_bits(gb, 1) : 0;
            hdr->segmentation.update_data = dav1d_get_bits(gb, 1);
700
701
702
703
704
        }

        if (hdr->segmentation.update_data) {
            hdr->segmentation.seg_data.preskip = 0;
            hdr->segmentation.seg_data.last_active_segid = -1;
705
            for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
706
                Dav1dSegmentationData *const seg =
707
                    &hdr->segmentation.seg_data.d[i];
708
709
                if (dav1d_get_bits(gb, 1)) {
                    seg->delta_q = dav1d_get_sbits(gb, 8);
710
711
712
713
                    hdr->segmentation.seg_data.last_active_segid = i;
                } else {
                    seg->delta_q = 0;
                }
714
715
                if (dav1d_get_bits(gb, 1)) {
                    seg->delta_lf_y_v = dav1d_get_sbits(gb, 6);
716
717
718
719
                    hdr->segmentation.seg_data.last_active_segid = i;
                } else {
                    seg->delta_lf_y_v = 0;
                }
720
721
                if (dav1d_get_bits(gb, 1)) {
                    seg->delta_lf_y_h = dav1d_get_sbits(gb, 6);
722
723
724
725
                    hdr->segmentation.seg_data.last_active_segid = i;
                } else {
                    seg->delta_lf_y_h = 0;
                }
726
727
                if (dav1d_get_bits(gb, 1)) {
                    seg->delta_lf_u = dav1d_get_sbits(gb, 6);
728
729
730
731
                    hdr->segmentation.seg_data.last_active_segid = i;
                } else {
                    seg->delta_lf_u = 0;
                }
732
733
                if (dav1d_get_bits(gb, 1)) {
                    seg->delta_lf_v = dav1d_get_sbits(gb, 6);
734
735
736
737
                    hdr->segmentation.seg_data.last_active_segid = i;
                } else {
                    seg->delta_lf_v = 0;
                }
738
739
                if (dav1d_get_bits(gb, 1)) {
                    seg->ref = dav1d_get_bits(gb, 3);
740
741
742
743
744
                    hdr->segmentation.seg_data.last_active_segid = i;
                    hdr->segmentation.seg_data.preskip = 1;
                } else {
                    seg->ref = -1;
                }
745
                if ((seg->skip = dav1d_get_bits(gb, 1))) {
746
747
748
                    hdr->segmentation.seg_data.last_active_segid = i;
                    hdr->segmentation.seg_data.preskip = 1;
                }
749
                if ((seg->globalmv = dav1d_get_bits(gb, 1))) {
750
751
752
753
754
                    hdr->segmentation.seg_data.last_active_segid = i;
                    hdr->segmentation.seg_data.preskip = 1;
                }
            }
        } else {
755
756
            // segmentation.update_data was false so we should copy
            // segmentation data from the reference frame.
757
            assert(hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
758
            const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
759
            if (!c->refs[pri_ref].p.p.frame_hdr) return DAV1D_ERR(EINVAL);
760
761
            hdr->segmentation.seg_data =
                c->refs[pri_ref].p.p.frame_hdr->segmentation.seg_data;
762
        }
763
    } else {
764
        memset(&hdr->segmentation.seg_data, 0, sizeof(Dav1dSegmentationDataSet));
765
        for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++)
766
            hdr->segmentation.seg_data.d[i].ref = -1;
767
768
769
770
771
772
773
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-segmentation: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // delta q
774
775
    hdr->delta.q.present = hdr->quant.yac ? dav1d_get_bits(gb, 1) : 0;
    hdr->delta.q.res_log2 = hdr->delta.q.present ? dav1d_get_bits(gb, 2) : 0;
776
    hdr->delta.lf.present = hdr->delta.q.present && !hdr->allow_intrabc &&
777
778
779
                            dav1d_get_bits(gb, 1);
    hdr->delta.lf.res_log2 = hdr->delta.lf.present ? dav1d_get_bits(gb, 2) : 0;
    hdr->delta.lf.multi = hdr->delta.lf.present ? dav1d_get_bits(gb, 1) : 0;
780
781
782
783
784
785
786
787
788
#if DEBUG_FRAME_HDR
    printf("HDR: post-delta_q_lf_flags: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // derive lossless flags
    const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta &&
        !hdr->quant.uac_delta && !hdr->quant.vdc_delta && !hdr->quant.vac_delta;
    hdr->all_lossless = 1;
789
    for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) {
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
        hdr->segmentation.qidx[i] = hdr->segmentation.enabled ?
            iclip_u8(hdr->quant.yac + hdr->segmentation.seg_data.d[i].delta_q) :
            hdr->quant.yac;
        hdr->segmentation.lossless[i] =
            !hdr->segmentation.qidx[i] && delta_lossless;
        hdr->all_lossless &= hdr->segmentation.lossless[i];
    }

    // loopfilter
    if (hdr->all_lossless || hdr->allow_intrabc) {
        hdr->loopfilter.level_y[0] = hdr->loopfilter.level_y[1] = 0;
        hdr->loopfilter.level_u = hdr->loopfilter.level_v = 0;
        hdr->loopfilter.sharpness = 0;
        hdr->loopfilter.mode_ref_delta_enabled = 1;
        hdr->loopfilter.mode_ref_delta_update = 1;
        hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
    } else {
807
808
        hdr->loopfilter.level_y[0] = dav1d_get_bits(gb, 6);
        hdr->loopfilter.level_y[1] = dav1d_get_bits(gb, 6);
809
        if (!seqhdr->monochrome &&
810
811
            (hdr->loopfilter.level_y[0] || hdr->loopfilter.level_y[1]))
        {
812
813
            hdr->loopfilter.level_u = dav1d_get_bits(gb, 6);
            hdr->loopfilter.level_v = dav1d_get_bits(gb, 6);
814
        }
815
        hdr->loopfilter.sharpness = dav1d_get_bits(gb, 3);
816

817
        if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
818
819
820
            hdr->loopfilter.mode_ref_deltas = default_mode_ref_deltas;
        } else {
            const int ref = hdr->refidx[hdr->primary_ref_frame];
821
            if (!c->refs[ref].p.p.frame_hdr) return DAV1D_ERR(EINVAL);
822
823
            hdr->loopfilter.mode_ref_deltas =
                c->refs[ref].p.p.frame_hdr->loopfilter.mode_ref_deltas;
824
        }
825
        hdr->loopfilter.mode_ref_delta_enabled = dav1d_get_bits(gb, 1);
826
        if (hdr->loopfilter.mode_ref_delta_enabled) {
827
            hdr->loopfilter.mode_ref_delta_update = dav1d_get_bits(gb, 1);
828
829
            if (hdr->loopfilter.mode_ref_delta_update) {
                for (int i = 0; i < 8; i++)
830
                    if (dav1d_get_bits(gb, 1))
831
                        hdr->loopfilter.mode_ref_deltas.ref_delta[i] =
832
                            dav1d_get_sbits(gb, 6);
833
                for (int i = 0; i < 2; i++)
834
                    if (dav1d_get_bits(gb, 1))
835
                        hdr->loopfilter.mode_ref_deltas.mode_delta[i] =
836
                            dav1d_get_sbits(gb, 6);
837
838
839
840
841
842
843
844
845
846
            }
        }
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-lpf: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // cdef
    if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) {
847
848
        hdr->cdef.damping = dav1d_get_bits(gb, 2) + 3;
        hdr->cdef.n_bits = dav1d_get_bits(gb, 2);
849
        for (int i = 0; i < (1 << hdr->cdef.n_bits); i++) {
850
            hdr->cdef.y_strength[i] = dav1d_get_bits(gb, 6);
851
            if (!seqhdr->monochrome)
852
                hdr->cdef.uv_strength[i] = dav1d_get_bits(gb, 6);
853
854
855
856
857
858
859
860
861
862
863
864
        }
    } else {
        hdr->cdef.n_bits = 0;
        hdr->cdef.y_strength[0] = 0;
        hdr->cdef.uv_strength[0] = 0;
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-cdef: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    // restoration
Ronald S. Bultje's avatar
Ronald S. Bultje committed
865
866
867
    if ((!hdr->all_lossless || hdr->super_res.enabled) &&
        seqhdr->restoration && !hdr->allow_intrabc)
    {
868
        hdr->restoration.type[0] = dav1d_get_bits(gb, 2);
869
        if (!seqhdr->monochrome) {
870
871
            hdr->restoration.type[1] = dav1d_get_bits(gb, 2);
            hdr->restoration.type[2] = dav1d_get_bits(gb, 2);
872
873
        } else {
            hdr->restoration.type[1] =
874
            hdr->restoration.type[2] = DAV1D_RESTORATION_NONE;
875
876
877
878
879
880
881
        }

        if (hdr->restoration.type[0] || hdr->restoration.type[1] ||
            hdr->restoration.type[2])
        {
            // Log2 of the restoration unit size.
            hdr->restoration.unit_size[0] = 6 + seqhdr->sb128;
882
            if (dav1d_get_bits(gb, 1)) {
883
884
                hdr->restoration.unit_size[0]++;
                if (!seqhdr->sb128)
885
                    hdr->restoration.unit_size[0] += dav1d_get_bits(gb, 1);
886
887
888
            }
            hdr->restoration.unit_size[1] = hdr->restoration.unit_size[0];
            if ((hdr->restoration.type[1] || hdr->restoration.type[2]) &&
889
                seqhdr->ss_hor == 1 && seqhdr->ss_ver == 1)
890
            {
891
                hdr->restoration.unit_size[1] -= dav1d_get_bits(gb, 1);
892
893
894
895
896
            }
        } else {
            hdr->restoration.unit_size[0] = 8;
        }
    } else {
897
898
899
        hdr->restoration.type[0] = DAV1D_RESTORATION_NONE;
        hdr->restoration.type[1] = DAV1D_RESTORATION_NONE;
        hdr->restoration.type[2] = DAV1D_RESTORATION_NONE;
900
901
902
903
904
905
    }
#if DEBUG_FRAME_HDR
    printf("HDR: post-restoration: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

906
907
    hdr->txfm_mode = hdr->all_lossless ? DAV1D_TX_4X4_ONLY :
                     dav1d_get_bits(gb, 1) ? DAV1D_TX_SWITCHABLE : DAV1D_TX_LARGEST;
908
909
910
911
#if DEBUG_FRAME_HDR
    printf("HDR: post-txfmmode: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
912
    hdr->switchable_comp_refs = hdr->frame_type & 1 ? dav1d_get_bits(gb, 1) : 0;
913
914
915
916
917
#if DEBUG_FRAME_HDR
    printf("HDR: post-refmode: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
    hdr->skip_mode_allowed = 0;
918
    if (hdr->switchable_comp_refs && hdr->frame_type & 1 && seqhdr->order_hint) {
919
        const unsigned poc = hdr->frame_offset;
920
        unsigned off_before = 0xFFFFFFFFU;
921
        int off_after = -1;
922
        int off_before_idx, off_after_idx;
923
        for (int i = 0; i < 7; i++) {
924
            if (!c->refs[hdr->refidx[i]].p.p.data[0]) return DAV1D_ERR(EINVAL);
925
            const unsigned refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
926
927
928
929
930
931
932
933
934

            const int diff = get_poc_diff(seqhdr->order_hint_n_bits, refpoc, poc);
            if (diff > 0) {
                if (off_after == -1 || get_poc_diff(seqhdr->order_hint_n_bits,
                                                    off_after, refpoc) > 0)
                {
                    off_after = refpoc;
                    off_after_idx = i;
                }
935
            } else if (diff < 0 && (off_before == 0xFFFFFFFFU ||
Michael Bradshaw's avatar
Michael Bradshaw committed
936
                                    get_poc_diff(seqhdr->order_hint_n_bits,
937
                                                 refpoc, off_before) > 0))
Michael Bradshaw's avatar
Michael Bradshaw committed
938
            {
939
940
                off_before = refpoc;
                off_before_idx = i;
941
942
943
            }
        }

944
945
946
        if (off_before != 0xFFFFFFFFU && off_after != -1) {
            hdr->skip_mode_refs[0] = imin(off_before_idx, off_after_idx);
            hdr->skip_mode_refs[1] = imax(off_before_idx, off_after_idx);
947
            hdr->skip_mode_allowed = 1;
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
        } else if (off_before != 0xFFFFFFFFU) {
            unsigned off_before2 = 0xFFFFFFFFU;
            int off_before2_idx;
            for (int i = 0; i < 7; i++) {
                if (!c->refs[hdr->refidx[i]].p.p.data[0]) return DAV1D_ERR(EINVAL);
                const unsigned refpoc = c->refs[hdr->refidx[i]].p.p.frame_hdr->frame_offset;
                if (get_poc_diff(seqhdr->order_hint_n_bits,
                                 refpoc, off_before) < 0) {
                    if (off_before2 == 0xFFFFFFFFU ||
                        get_poc_diff(seqhdr->order_hint_n_bits,
                                     refpoc, off_before2) > 0)
                    {
                        off_before2 = refpoc;
                        off_before2_idx = i;
                    }
                }
            }

            if (off_before2 != 0xFFFFFFFFU) {
                hdr->skip_mode_refs[0] = imin(off_before_idx, off_before2_idx);
                hdr->skip_mode_refs[1] = imax(off_before_idx, off_before2_idx);
                hdr->skip_mode_allowed = 1;
            }
971
972
        }
    }
973
    hdr->skip_mode_enabled = hdr->skip_mode_allowed ? dav1d_get_bits(gb, 1) : 0;
974
975
976
977
978
#if DEBUG_FRAME_HDR
    printf("HDR: post-extskip: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
    hdr->warp_motion = !hdr->error_resilient_mode && hdr->frame_type & 1 &&
979
        seqhdr->warped_motion && dav1d_get_bits(gb, 1);
980
981
982
983
#if DEBUG_FRAME_HDR
    printf("HDR: post-warpmotionbit: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif
984
    hdr->reduced_txtp_set = dav1d_get_bits(gb, 1);
985
986
987
988
989
990
#if DEBUG_FRAME_HDR
    printf("HDR: post-reducedtxtpset: off=%ld\n",
           (gb->ptr - init_ptr) * 8 - gb->bits_left);
#endif

    for (int i = 0; i < 7; i++)
991
        hdr->gmv[i] = dav1d_default_wm_params;
992
993
994

    if (hdr->frame_type & 1) {
        for (int i = 0; i < 7; i++) {
995
996
997
998
            hdr->gmv[i].type = !dav1d_get_bits(gb, 1) ? DAV1D_WM_TYPE_IDENTITY :
                                dav1d_get_bits(gb, 1) ? DAV1D_WM_TYPE_ROT_ZOOM :
                                dav1d_get_bits(gb, 1) ? DAV1D_WM_TYPE_TRANSLATION :
                                                  DAV1D_WM_TYPE_AFFINE;
999

1000
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_IDENTITY) continue;
1001

1002
1003
1004
1005
1006
            const Dav1dWarpedMotionParams *ref_gmv;
            if (hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
                ref_gmv = &dav1d_default_wm_params;
            } else {
                const int pri_ref = hdr->refidx[hdr->primary_ref_frame];
1007
                if (!c->refs[pri_ref].p.p.frame_hdr) return DAV1D_ERR(EINVAL);
1008
1009
                ref_gmv = &c->refs[pri_ref].p.p.frame_hdr->gmv[i];
            }
1010
1011
1012
1013
            int32_t *const mat = hdr->gmv[i].matrix;
            const int32_t *const ref_mat = ref_gmv->matrix;
            int bits, shift;

1014
            if (hdr->gmv[i].type >= DAV1D_WM_TYPE_ROT_ZOOM) {
1015
                mat[2] = (1 << 16) + 2 *
1016
1017
                    dav1d_get_bits_subexp(gb, (ref_mat[2] - (1 << 16)) >> 1, 12);
                mat[3] = 2 * dav1d_get_bits_subexp(gb, ref_mat[3] >> 1, 12);
1018
1019
1020
1021
1022
1023
1024
1025

                bits = 12;
                shift = 10;
            } else {
                bits = 9 - !hdr->hp;
                shift = 13 + !hdr->hp;
            }

1026
            if (hdr->gmv[i].type == DAV1D_WM_TYPE_AFFINE) {
1027
                mat[4] = 2 * dav1d_get_bits_subexp(gb, ref_mat[4] >> 1, 12);
1028
                mat[5] = (1 << 16) + 2 *
1029
                    dav1d_get_bits_subexp(gb, (ref_mat[5] - (1 << 16)) >> 1, 12);
1030
1031
1032
1033
1034
            } else {
                mat[4] = -mat[3];
                mat[5] = mat[2];
            }

1035
1036
            mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift);
            mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift);