Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/vp9.c
Line
Count
Source
1
/*
2
 * VP9 compatible video decoder
3
 *
4
 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5
 * Copyright (C) 2013 Clément Bœsch <u pkh me>
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
#include "config_components.h"
25
26
#include "avcodec.h"
27
#include "codec_internal.h"
28
#include "decode.h"
29
#include "get_bits.h"
30
#include "hwaccel_internal.h"
31
#include "hwconfig.h"
32
#include "profiles.h"
33
#include "progressframe.h"
34
#include "libavutil/refstruct.h"
35
#include "thread.h"
36
#include "pthread_internal.h"
37
38
#include "videodsp.h"
39
#include "vp89_rac.h"
40
#include "vp9.h"
41
#include "vp9data.h"
42
#include "vp9dec.h"
43
#include "vpx_rac.h"
44
#include "libavutil/attributes.h"
45
#include "libavutil/avassert.h"
46
#include "libavutil/mem.h"
47
#include "libavutil/pixdesc.h"
48
#include "libavutil/video_enc_params.h"
49
50
33.5k
#define VP9_SYNCCODE 0x498342
51
52
#if HAVE_THREADS
53
DEFINE_OFFSET_ARRAY(VP9Context, vp9_context, pthread_init_cnt,
54
                    (offsetof(VP9Context, progress_mutex)),
55
                    (offsetof(VP9Context, progress_cond)));
56
57
38.4k
static int vp9_alloc_entries(AVCodecContext *avctx, int n) {
58
38.4k
    VP9Context *s = avctx->priv_data;
59
60
38.4k
    if (avctx->active_thread_type & FF_THREAD_SLICE)  {
61
0
        if (s->entries)
62
0
            av_freep(&s->entries);
63
64
0
        s->entries = av_malloc_array(n, sizeof(atomic_int));
65
0
        if (!s->entries)
66
0
            return AVERROR(ENOMEM);
67
0
    }
68
38.4k
    return 0;
69
38.4k
}
70
71
0
static void vp9_report_tile_progress(VP9Context *s, int field, int n) {
72
0
    pthread_mutex_lock(&s->progress_mutex);
73
0
    atomic_fetch_add_explicit(&s->entries[field], n, memory_order_release);
74
0
    pthread_cond_signal(&s->progress_cond);
75
0
    pthread_mutex_unlock(&s->progress_mutex);
76
0
}
77
78
0
static void vp9_await_tile_progress(VP9Context *s, int field, int n) {
79
0
    if (atomic_load_explicit(&s->entries[field], memory_order_acquire) >= n)
80
0
        return;
81
82
0
    pthread_mutex_lock(&s->progress_mutex);
83
0
    while (atomic_load_explicit(&s->entries[field], memory_order_relaxed) != n)
84
0
        pthread_cond_wait(&s->progress_cond, &s->progress_mutex);
85
0
    pthread_mutex_unlock(&s->progress_mutex);
86
0
}
87
#else
88
static int vp9_alloc_entries(AVCodecContext *avctx, int n) { return 0; }
89
#endif
90
91
static void vp9_tile_data_free(VP9TileData *td)
92
92.2k
{
93
92.2k
    av_freep(&td->b_base);
94
92.2k
    av_freep(&td->block_base);
95
92.2k
    av_freep(&td->block_structure);
96
92.2k
}
97
98
static void vp9_frame_unref(VP9Frame *f)
99
1.62M
{
100
1.62M
    ff_progress_frame_unref(&f->tf);
101
1.62M
    av_refstruct_unref(&f->header_ref);
102
1.62M
    av_refstruct_unref(&f->extradata);
103
1.62M
    av_refstruct_unref(&f->hwaccel_picture_private);
104
1.62M
    f->segmentation_map = NULL;
105
1.62M
}
106
107
static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
108
296k
{
109
296k
    VP9Context *s = avctx->priv_data;
110
296k
    int ret, sz;
111
112
296k
    ret = ff_progress_frame_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF);
113
296k
    if (ret < 0)
114
1.24k
        return ret;
115
116
295k
    sz = 64 * s->sb_cols * s->sb_rows;
117
295k
    if (sz != s->frame_extradata_pool_size) {
118
20.1k
        av_refstruct_pool_uninit(&s->frame_extradata_pool);
119
20.1k
        s->frame_extradata_pool = av_refstruct_pool_alloc(sz * (1 + sizeof(VP9mvrefPair)),
120
20.1k
                                                          AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
121
20.1k
        if (!s->frame_extradata_pool) {
122
0
            s->frame_extradata_pool_size = 0;
123
0
            ret = AVERROR(ENOMEM);
124
0
            goto fail;
125
0
        }
126
20.1k
        s->frame_extradata_pool_size = sz;
127
20.1k
    }
128
295k
    f->extradata = av_refstruct_pool_get(s->frame_extradata_pool);
129
295k
    if (!f->extradata) {
130
0
        ret = AVERROR(ENOMEM);
131
0
        goto fail;
132
0
    }
133
134
295k
    f->segmentation_map = f->extradata;
135
295k
    f->mv = (VP9mvrefPair *) ((char*)f->extradata + sz);
136
137
295k
    ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
138
295k
    if (ret < 0)
139
0
        goto fail;
140
141
295k
    return 0;
142
143
0
fail:
144
0
    vp9_frame_unref(f);
145
0
    return ret;
146
295k
}
147
148
static void vp9_frame_replace(VP9Frame *dst, const VP9Frame *src)
149
539k
{
150
539k
    av_refstruct_replace(&dst->header_ref, src->header_ref);
151
539k
    dst->frame_header = src->frame_header;
152
153
539k
    ff_progress_frame_replace(&dst->tf, &src->tf);
154
155
539k
    av_refstruct_replace(&dst->extradata, src->extradata);
156
157
539k
    dst->segmentation_map = src->segmentation_map;
158
539k
    dst->mv = src->mv;
159
539k
    dst->uses_2pass = src->uses_2pass;
160
161
539k
    av_refstruct_replace(&dst->hwaccel_picture_private,
162
539k
                          src->hwaccel_picture_private);
163
539k
}
164
165
static int update_size(AVCodecContext *avctx, int w, int h)
166
322k
{
167
322k
#define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + \
168
322k
                     CONFIG_VP9_D3D11VA_HWACCEL * 2 + \
169
322k
                     CONFIG_VP9_D3D12VA_HWACCEL + \
170
322k
                     CONFIG_VP9_NVDEC_HWACCEL + \
171
322k
                     CONFIG_VP9_VAAPI_HWACCEL + \
172
322k
                     CONFIG_VP9_VDPAU_HWACCEL + \
173
322k
                     CONFIG_VP9_VIDEOTOOLBOX_HWACCEL + \
174
322k
                     CONFIG_VP9_VULKAN_HWACCEL)
175
322k
    enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
176
322k
    VP9Context *s = avctx->priv_data;
177
322k
    uint8_t *p;
178
322k
    int bytesperpixel = s->bytesperpixel, ret, cols, rows;
179
322k
    int lflvl_len, i;
180
322k
    int changed = 0;
181
182
322k
    av_assert0(w > 0 && h > 0);
183
184
322k
    if (!(s->pix_fmt == s->gf_fmt && w == s->w && h == s->h)) {
185
38.0k
        changed = 1;
186
38.0k
        if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
187
1.07k
            return ret;
188
189
36.9k
        switch (s->pix_fmt) {
190
6.33k
        case AV_PIX_FMT_YUV420P:
191
8.21k
        case AV_PIX_FMT_YUV420P10:
192
#if CONFIG_VP9_DXVA2_HWACCEL
193
            *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
194
#endif
195
#if CONFIG_VP9_D3D11VA_HWACCEL
196
            *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
197
            *fmtp++ = AV_PIX_FMT_D3D11;
198
#endif
199
#if CONFIG_VP9_D3D12VA_HWACCEL
200
            *fmtp++ = AV_PIX_FMT_D3D12;
201
#endif
202
#if CONFIG_VP9_NVDEC_HWACCEL
203
            *fmtp++ = AV_PIX_FMT_CUDA;
204
#endif
205
#if CONFIG_VP9_VAAPI_HWACCEL
206
            *fmtp++ = AV_PIX_FMT_VAAPI;
207
#endif
208
#if CONFIG_VP9_VDPAU_HWACCEL
209
            *fmtp++ = AV_PIX_FMT_VDPAU;
210
#endif
211
#if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
212
            *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
213
#endif
214
#if CONFIG_VP9_VULKAN_HWACCEL
215
            *fmtp++ = AV_PIX_FMT_VULKAN;
216
#endif
217
8.21k
            break;
218
1.17k
        case AV_PIX_FMT_YUV420P12:
219
#if CONFIG_VP9_NVDEC_HWACCEL
220
            *fmtp++ = AV_PIX_FMT_CUDA;
221
#endif
222
#if CONFIG_VP9_VAAPI_HWACCEL
223
            *fmtp++ = AV_PIX_FMT_VAAPI;
224
#endif
225
#if CONFIG_VP9_VDPAU_HWACCEL
226
            *fmtp++ = AV_PIX_FMT_VDPAU;
227
#endif
228
#if CONFIG_VP9_VULKAN_HWACCEL
229
            *fmtp++ = AV_PIX_FMT_VULKAN;
230
#endif
231
1.17k
            break;
232
6.04k
        case AV_PIX_FMT_YUV444P:
233
8.20k
        case AV_PIX_FMT_YUV444P10:
234
10.9k
        case AV_PIX_FMT_YUV444P12:
235
#if CONFIG_VP9_VAAPI_HWACCEL
236
            *fmtp++ = AV_PIX_FMT_VAAPI;
237
#endif
238
#if CONFIG_VP9_VULKAN_HWACCEL
239
            *fmtp++ = AV_PIX_FMT_VULKAN;
240
#endif
241
10.9k
            break;
242
30
        case AV_PIX_FMT_GBRP:
243
59
        case AV_PIX_FMT_GBRP10:
244
103
        case AV_PIX_FMT_GBRP12:
245
#if CONFIG_VP9_VAAPI_HWACCEL
246
            *fmtp++ = AV_PIX_FMT_VAAPI;
247
#endif
248
#if CONFIG_VP9_VULKAN_HWACCEL
249
            *fmtp++ = AV_PIX_FMT_VULKAN;
250
#endif
251
103
            break;
252
36.9k
        }
253
254
36.9k
        *fmtp++ = s->pix_fmt;
255
36.9k
        *fmtp = AV_PIX_FMT_NONE;
256
257
36.9k
        ret = ff_get_format(avctx, pix_fmts);
258
36.9k
        if (ret < 0)
259
0
            return ret;
260
261
36.9k
        avctx->pix_fmt = ret;
262
36.9k
        s->gf_fmt  = s->pix_fmt;
263
36.9k
        s->w = w;
264
36.9k
        s->h = h;
265
36.9k
    }
266
267
321k
    cols = (w + 7) >> 3;
268
321k
    rows = (h + 7) >> 3;
269
270
321k
    if (s->intra_pred_data[0] && cols == s->cols && rows == s->rows && s->pix_fmt == s->last_fmt)
271
285k
        return changed;
272
273
36.2k
    s->last_fmt  = s->pix_fmt;
274
36.2k
    s->sb_cols   = (w + 63) >> 6;
275
36.2k
    s->sb_rows   = (h + 63) >> 6;
276
36.2k
    s->cols      = (w + 7) >> 3;
277
36.2k
    s->rows      = (h + 7) >> 3;
278
36.2k
    lflvl_len    = avctx->active_thread_type == FF_THREAD_SLICE ? s->sb_rows : 1;
279
280
615k
#define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
281
36.2k
    av_freep(&s->intra_pred_data[0]);
282
    // FIXME we slightly over-allocate here for subsampled chroma, but a little
283
    // bit of padding shouldn't affect performance...
284
36.2k
    p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
285
36.2k
                                lflvl_len * sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
286
36.2k
    if (!p)
287
0
        return AVERROR(ENOMEM);
288
36.2k
    assign(s->intra_pred_data[0],  uint8_t *,             64 * bytesperpixel);
289
36.2k
    assign(s->intra_pred_data[1],  uint8_t *,             64 * bytesperpixel);
290
36.2k
    assign(s->intra_pred_data[2],  uint8_t *,             64 * bytesperpixel);
291
36.2k
    assign(s->above_y_nnz_ctx,     uint8_t *,             16);
292
36.2k
    assign(s->above_mode_ctx,      uint8_t *,             16);
293
36.2k
    assign(s->above_mv_ctx,        VP9mv(*)[2],           16);
294
36.2k
    assign(s->above_uv_nnz_ctx[0], uint8_t *,             16);
295
36.2k
    assign(s->above_uv_nnz_ctx[1], uint8_t *,             16);
296
36.2k
    assign(s->above_partition_ctx, uint8_t *,              8);
297
36.2k
    assign(s->above_skip_ctx,      uint8_t *,              8);
298
36.2k
    assign(s->above_txfm_ctx,      uint8_t *,              8);
299
36.2k
    assign(s->above_segpred_ctx,   uint8_t *,              8);
300
36.2k
    assign(s->above_intra_ctx,     uint8_t *,              8);
301
36.2k
    assign(s->above_comp_ctx,      uint8_t *,              8);
302
36.2k
    assign(s->above_ref_ctx,       uint8_t *,              8);
303
36.2k
    assign(s->above_filter_ctx,    uint8_t *,              8);
304
36.2k
    assign(s->lflvl,               VP9Filter *,            lflvl_len);
305
36.2k
#undef assign
306
307
36.2k
    if (s->td) {
308
50.5k
        for (i = 0; i < s->active_tile_cols; i++)
309
25.2k
            vp9_tile_data_free(&s->td[i]);
310
25.2k
    }
311
312
36.2k
    if (s->s.h.bpp != s->last_bpp) {
313
12.2k
        ff_vp9dsp_init(&s->dsp, s->s.h.bpp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
314
12.2k
        ff_videodsp_init(&s->vdsp, s->s.h.bpp);
315
12.2k
        s->last_bpp = s->s.h.bpp;
316
12.2k
        changed = 1;
317
12.2k
    }
318
319
36.2k
    return changed;
320
36.2k
}
321
322
static int update_block_buffers(AVCodecContext *avctx)
323
295k
{
324
295k
    int i;
325
295k
    VP9Context *s = avctx->priv_data;
326
295k
    int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
327
295k
    VP9TileData *td = &s->td[0];
328
329
295k
    if (td->b_base && td->block_base && s->block_alloc_using_2pass == s->s.frames[CUR_FRAME].uses_2pass)
330
267k
        return 0;
331
332
28.5k
    vp9_tile_data_free(td);
333
28.5k
    chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
334
28.5k
    chroma_eobs   = 16 * 16 >> (s->ss_h + s->ss_v);
335
28.5k
    if (s->s.frames[CUR_FRAME].uses_2pass) {
336
0
        int sbs = s->sb_cols * s->sb_rows;
337
338
0
        td->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
339
0
        td->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
340
0
                                    16 * 16 + 2 * chroma_eobs) * sbs);
341
0
        if (!td->b_base || !td->block_base)
342
0
            return AVERROR(ENOMEM);
343
0
        td->uvblock_base[0] = td->block_base + sbs * 64 * 64 * bytesperpixel;
344
0
        td->uvblock_base[1] = td->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
345
0
        td->eob_base = (uint8_t *) (td->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
346
0
        td->uveob_base[0] = td->eob_base + 16 * 16 * sbs;
347
0
        td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs;
348
349
0
        if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
350
0
            td->block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure));
351
0
            if (!td->block_structure)
352
0
                return AVERROR(ENOMEM);
353
0
        }
354
28.5k
    } else {
355
28.5k
        for (i = 1; i < s->active_tile_cols; i++)
356
0
            vp9_tile_data_free(&s->td[i]);
357
358
57.0k
        for (i = 0; i < s->active_tile_cols; i++) {
359
28.5k
            s->td[i].b_base = av_malloc(sizeof(VP9Block));
360
28.5k
            s->td[i].block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
361
28.5k
                                       16 * 16 + 2 * chroma_eobs);
362
28.5k
            if (!s->td[i].b_base || !s->td[i].block_base)
363
0
                return AVERROR(ENOMEM);
364
28.5k
            s->td[i].uvblock_base[0] = s->td[i].block_base + 64 * 64 * bytesperpixel;
365
28.5k
            s->td[i].uvblock_base[1] = s->td[i].uvblock_base[0] + chroma_blocks * bytesperpixel;
366
28.5k
            s->td[i].eob_base = (uint8_t *) (s->td[i].uvblock_base[1] + chroma_blocks * bytesperpixel);
367
28.5k
            s->td[i].uveob_base[0] = s->td[i].eob_base + 16 * 16;
368
28.5k
            s->td[i].uveob_base[1] = s->td[i].uveob_base[0] + chroma_eobs;
369
370
28.5k
            if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
371
10.9k
                s->td[i].block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure));
372
10.9k
                if (!s->td[i].block_structure)
373
0
                    return AVERROR(ENOMEM);
374
10.9k
            }
375
28.5k
        }
376
28.5k
    }
377
28.5k
    s->block_alloc_using_2pass = s->s.frames[CUR_FRAME].uses_2pass;
378
379
28.5k
    return 0;
380
28.5k
}
381
382
// The sign bit is at the end, not the start, of a bit sequence
383
static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
384
941k
{
385
941k
    int v = get_bits(gb, n);
386
941k
    return get_bits1(gb) ? -v : v;
387
941k
}
388
389
static av_always_inline int inv_recenter_nonneg(int v, int m)
390
1.75M
{
391
1.75M
    if (v > 2 * m)
392
722k
        return v;
393
1.03M
    if (v & 1)
394
265k
        return m - ((v + 1) >> 1);
395
769k
    return m + (v >> 1);
396
1.03M
}
397
398
// differential forward probability updates
399
static int update_prob(VPXRangeCoder *c, int p)
400
1.75M
{
401
1.75M
    static const uint8_t inv_map_table[255] = {
402
1.75M
          7,  20,  33,  46,  59,  72,  85,  98, 111, 124, 137, 150, 163, 176,
403
1.75M
        189, 202, 215, 228, 241, 254,   1,   2,   3,   4,   5,   6,   8,   9,
404
1.75M
         10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  21,  22,  23,  24,
405
1.75M
         25,  26,  27,  28,  29,  30,  31,  32,  34,  35,  36,  37,  38,  39,
406
1.75M
         40,  41,  42,  43,  44,  45,  47,  48,  49,  50,  51,  52,  53,  54,
407
1.75M
         55,  56,  57,  58,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,
408
1.75M
         70,  71,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
409
1.75M
         86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  99, 100,
410
1.75M
        101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
411
1.75M
        116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
412
1.75M
        131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
413
1.75M
        146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
414
1.75M
        161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
415
1.75M
        177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
416
1.75M
        192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
417
1.75M
        207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
418
1.75M
        222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
419
1.75M
        237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
420
1.75M
        252, 253, 253,
421
1.75M
    };
422
1.75M
    int d;
423
424
    /* This code is trying to do a differential probability update. For a
425
     * current probability A in the range [1, 255], the difference to a new
426
     * probability of any value can be expressed differentially as 1-A, 255-A
427
     * where some part of this (absolute range) exists both in positive as
428
     * well as the negative part, whereas another part only exists in one
429
     * half. We're trying to code this shared part differentially, i.e.
430
     * times two where the value of the lowest bit specifies the sign, and
431
     * the single part is then coded on top of this. This absolute difference
432
     * then again has a value of [0, 254], but a bigger value in this range
433
     * indicates that we're further away from the original value A, so we
434
     * can code this as a VLC code, since higher values are increasingly
435
     * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
436
     * updates vs. the 'fine, exact' updates further down the range, which
437
     * adds one extra dimension to this differential update model. */
438
439
1.75M
    if (!vp89_rac_get(c)) {
440
513k
        d = vp89_rac_get_uint(c, 4) + 0;
441
1.24M
    } else if (!vp89_rac_get(c)) {
442
542k
        d = vp89_rac_get_uint(c, 4) + 16;
443
701k
    } else if (!vp89_rac_get(c)) {
444
306k
        d = vp89_rac_get_uint(c, 5) + 32;
445
394k
    } else {
446
394k
        d = vp89_rac_get_uint(c, 7);
447
394k
        if (d >= 65)
448
356k
            d = (d << 1) - 65 + vp89_rac_get(c);
449
394k
        d += 64;
450
394k
        av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
451
394k
    }
452
453
1.75M
    return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
454
1.75M
                    255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
455
1.75M
}
456
457
static int read_colorspace_details(AVCodecContext *avctx)
458
30.0k
{
459
30.0k
    static const enum AVColorSpace colorspaces[8] = {
460
30.0k
        AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
461
30.0k
        AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
462
30.0k
    };
463
30.0k
    VP9Context *s = avctx->priv_data;
464
30.0k
    int bits = avctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
465
466
30.0k
    s->bpp_index = bits;
467
30.0k
    s->s.h.bpp = 8 + bits * 2;
468
30.0k
    s->bytesperpixel = (7 + s->s.h.bpp) >> 3;
469
30.0k
    avctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
470
30.0k
    if (avctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
471
1.97k
        static const enum AVPixelFormat pix_fmt_rgb[3] = {
472
1.97k
            AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12
473
1.97k
        };
474
1.97k
        s->ss_h = s->ss_v = 0;
475
1.97k
        avctx->color_range = AVCOL_RANGE_JPEG;
476
1.97k
        s->pix_fmt = pix_fmt_rgb[bits];
477
1.97k
        if (avctx->profile & 1) {
478
200
            if (get_bits1(&s->gb)) {
479
0
                av_log(avctx, AV_LOG_ERROR, "Reserved bit set in RGB\n");
480
0
                return AVERROR_INVALIDDATA;
481
0
            }
482
1.77k
        } else {
483
1.77k
            av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
484
1.77k
                   avctx->profile);
485
1.77k
            return AVERROR_INVALIDDATA;
486
1.77k
        }
487
28.0k
    } else {
488
28.0k
        static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
489
28.0k
            { { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P },
490
28.0k
              { AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV420P } },
491
28.0k
            { { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10 },
492
28.0k
              { AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV420P10 } },
493
28.0k
            { { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12 },
494
28.0k
              { AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV420P12 } }
495
28.0k
        };
496
28.0k
        avctx->color_range = get_bits1(&s->gb) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
497
28.0k
        if (avctx->profile & 1) {
498
23.5k
            s->ss_h = get_bits1(&s->gb);
499
23.5k
            s->ss_v = get_bits1(&s->gb);
500
23.5k
            s->pix_fmt = pix_fmt_for_ss[bits][s->ss_v][s->ss_h];
501
23.5k
            if (s->pix_fmt == AV_PIX_FMT_YUV420P) {
502
49
                av_log(avctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
503
49
                       avctx->profile);
504
49
                return AVERROR_INVALIDDATA;
505
23.4k
            } else if (get_bits1(&s->gb)) {
506
0
                av_log(avctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
507
0
                       avctx->profile);
508
0
                return AVERROR_INVALIDDATA;
509
0
            }
510
23.5k
        } else {
511
4.52k
            s->ss_h = s->ss_v = 1;
512
4.52k
            s->pix_fmt = pix_fmt_for_ss[bits][1][1];
513
4.52k
        }
514
28.0k
    }
515
516
28.1k
    return 0;
517
30.0k
}
518
519
static int decode_frame_header(AVCodecContext *avctx,
520
                               const uint8_t *data, int size, int *ref)
521
392k
{
522
392k
    VP9Context *s = avctx->priv_data;
523
392k
    int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
524
392k
    int last_invisible;
525
392k
    const uint8_t *data2;
526
392k
    int changed;
527
528
    /* general header */
529
392k
    if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
530
0
        av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
531
0
        return ret;
532
0
    }
533
392k
    if (get_bits(&s->gb, 2) != 0x2) { // frame marker
534
51.4k
        av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
535
51.4k
        return AVERROR_INVALIDDATA;
536
51.4k
    }
537
341k
    avctx->profile  = get_bits1(&s->gb);
538
341k
    avctx->profile |= get_bits1(&s->gb) << 1;
539
341k
    if (avctx->profile == 3) avctx->profile += get_bits1(&s->gb);
540
341k
    if (avctx->profile > 3) {
541
0
        av_log(avctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", avctx->profile);
542
0
        return AVERROR_INVALIDDATA;
543
0
    }
544
341k
    s->s.h.profile = avctx->profile;
545
341k
    if (get_bits1(&s->gb)) {
546
8.09k
        *ref = get_bits(&s->gb, 3);
547
8.09k
        return 0;
548
8.09k
    }
549
550
333k
    s->last_keyframe  = s->s.h.keyframe;
551
333k
    s->s.h.keyframe   = !get_bits1(&s->gb);
552
553
333k
    last_invisible   = s->s.h.invisible;
554
333k
    s->s.h.invisible = !get_bits1(&s->gb);
555
333k
    s->s.h.errorres  = get_bits1(&s->gb);
556
333k
    s->s.h.use_last_frame_mvs = !s->s.h.errorres && !last_invisible;
557
558
333k
    if (s->s.h.keyframe) {
559
24.1k
        if (get_bits(&s->gb, 24) != VP9_SYNCCODE) { // synccode
560
0
            av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
561
0
            return AVERROR_INVALIDDATA;
562
0
        }
563
24.1k
        if ((ret = read_colorspace_details(avctx)) < 0)
564
21
            return ret;
565
        // for profile 1, here follows the subsampling bits
566
24.0k
        s->s.h.refreshrefmask = 0xff;
567
24.0k
        w = get_bits(&s->gb, 16) + 1;
568
24.0k
        h = get_bits(&s->gb, 16) + 1;
569
24.0k
        if (get_bits1(&s->gb)) // display size
570
2.06k
            skip_bits(&s->gb, 32);
571
309k
    } else {
572
309k
        s->s.h.intraonly = s->s.h.invisible ? get_bits1(&s->gb) : 0;
573
309k
        s->s.h.resetctx  = s->s.h.errorres ? 0 : get_bits(&s->gb, 2);
574
309k
        if (s->s.h.intraonly) {
575
9.40k
            if (get_bits(&s->gb, 24) != VP9_SYNCCODE) { // synccode
576
0
                av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
577
0
                return AVERROR_INVALIDDATA;
578
0
            }
579
9.40k
            if (avctx->profile >= 1) {
580
5.90k
                if ((ret = read_colorspace_details(avctx)) < 0)
581
1.80k
                    return ret;
582
5.90k
            } else {
583
3.50k
                s->ss_h = s->ss_v = 1;
584
3.50k
                s->s.h.bpp = 8;
585
3.50k
                s->bpp_index = 0;
586
3.50k
                s->bytesperpixel = 1;
587
3.50k
                s->pix_fmt = AV_PIX_FMT_YUV420P;
588
3.50k
                avctx->colorspace = AVCOL_SPC_BT470BG;
589
3.50k
                avctx->color_range = AVCOL_RANGE_MPEG;
590
3.50k
            }
591
7.60k
            s->s.h.refreshrefmask = get_bits(&s->gb, 8);
592
7.60k
            w = get_bits(&s->gb, 16) + 1;
593
7.60k
            h = get_bits(&s->gb, 16) + 1;
594
7.60k
            if (get_bits1(&s->gb)) // display size
595
1.43k
                skip_bits(&s->gb, 32);
596
299k
        } else {
597
299k
            s->s.h.refreshrefmask = get_bits(&s->gb, 8);
598
299k
            s->s.h.refidx[0]      = get_bits(&s->gb, 3);
599
299k
            s->s.h.signbias[0]    = get_bits1(&s->gb) && !s->s.h.errorres;
600
299k
            s->s.h.refidx[1]      = get_bits(&s->gb, 3);
601
299k
            s->s.h.signbias[1]    = get_bits1(&s->gb) && !s->s.h.errorres;
602
299k
            s->s.h.refidx[2]      = get_bits(&s->gb, 3);
603
299k
            s->s.h.signbias[2]    = get_bits1(&s->gb) && !s->s.h.errorres;
604
299k
            if (!s->s.refs[s->s.h.refidx[0]].f ||
605
292k
                !s->s.refs[s->s.h.refidx[1]].f ||
606
291k
                !s->s.refs[s->s.h.refidx[2]].f) {
607
8.85k
                av_log(avctx, AV_LOG_ERROR, "Not all references are available\n");
608
8.85k
                return AVERROR_INVALIDDATA;
609
8.85k
            }
610
291k
            if (get_bits1(&s->gb)) {
611
194k
                w = s->s.refs[s->s.h.refidx[0]].f->width;
612
194k
                h = s->s.refs[s->s.h.refidx[0]].f->height;
613
194k
            } else if (get_bits1(&s->gb)) {
614
50.9k
                w = s->s.refs[s->s.h.refidx[1]].f->width;
615
50.9k
                h = s->s.refs[s->s.h.refidx[1]].f->height;
616
50.9k
            } else if (get_bits1(&s->gb)) {
617
11.8k
                w = s->s.refs[s->s.h.refidx[2]].f->width;
618
11.8k
                h = s->s.refs[s->s.h.refidx[2]].f->height;
619
33.8k
            } else {
620
33.8k
                w = get_bits(&s->gb, 16) + 1;
621
33.8k
                h = get_bits(&s->gb, 16) + 1;
622
33.8k
            }
623
            // Note that in this code, "CUR_FRAME" is actually before we
624
            // have formally allocated a frame, and thus actually represents
625
            // the _last_ frame
626
291k
            s->s.h.use_last_frame_mvs &= s->s.frames[CUR_FRAME].tf.f &&
627
289k
                                         s->s.frames[CUR_FRAME].tf.f->width == w &&
628
273k
                                       s->s.frames[CUR_FRAME].tf.f->height == h;
629
291k
            if (get_bits1(&s->gb)) // display size
630
268k
                skip_bits(&s->gb, 32);
631
291k
            s->s.h.highprecisionmvs = get_bits1(&s->gb);
632
291k
            s->s.h.filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
633
291k
                                                  get_bits(&s->gb, 2);
634
291k
            s->s.h.allowcompinter = s->s.h.signbias[0] != s->s.h.signbias[1] ||
635
22.9k
                                  s->s.h.signbias[0] != s->s.h.signbias[2];
636
291k
            if (s->s.h.allowcompinter) {
637
286k
                if (s->s.h.signbias[0] == s->s.h.signbias[1]) {
638
18.0k
                    s->s.h.fixcompref    = 2;
639
18.0k
                    s->s.h.varcompref[0] = 0;
640
18.0k
                    s->s.h.varcompref[1] = 1;
641
268k
                } else if (s->s.h.signbias[0] == s->s.h.signbias[2]) {
642
69.2k
                    s->s.h.fixcompref    = 1;
643
69.2k
                    s->s.h.varcompref[0] = 0;
644
69.2k
                    s->s.h.varcompref[1] = 2;
645
198k
                } else {
646
198k
                    s->s.h.fixcompref    = 0;
647
198k
                    s->s.h.varcompref[0] = 1;
648
198k
                    s->s.h.varcompref[1] = 2;
649
198k
                }
650
286k
            }
651
291k
        }
652
309k
    }
653
322k
    s->s.h.refreshctx   = s->s.h.errorres ? 0 : get_bits1(&s->gb);
654
322k
    s->s.h.parallelmode = s->s.h.errorres ? 1 : get_bits1(&s->gb);
655
322k
    s->s.h.framectxid   = c = get_bits(&s->gb, 2);
656
322k
    if (s->s.h.keyframe || s->s.h.intraonly)
657
31.6k
        s->s.h.framectxid = 0; // BUG: libvpx ignores this field in keyframes
658
659
    /* loopfilter header data */
660
322k
    if (s->s.h.keyframe || s->s.h.errorres || s->s.h.intraonly) {
661
        // reset loopfilter defaults
662
31.7k
        s->s.h.lf_delta.ref[0] = 1;
663
31.7k
        s->s.h.lf_delta.ref[1] = 0;
664
31.7k
        s->s.h.lf_delta.ref[2] = -1;
665
31.7k
        s->s.h.lf_delta.ref[3] = -1;
666
31.7k
        s->s.h.lf_delta.mode[0] = 0;
667
31.7k
        s->s.h.lf_delta.mode[1] = 0;
668
31.7k
        memset(s->s.h.segmentation.feat, 0, sizeof(s->s.h.segmentation.feat));
669
31.7k
    }
670
322k
    s->s.h.filter.level = get_bits(&s->gb, 6);
671
322k
    sharp = get_bits(&s->gb, 3);
672
    // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
673
    // the old cache values since they are still valid
674
322k
    if (s->s.h.filter.sharpness != sharp) {
675
2.80M
        for (i = 1; i <= 63; i++) {
676
2.75M
            int limit = i;
677
678
2.75M
            if (sharp > 0) {
679
1.89M
                limit >>= (sharp + 3) >> 2;
680
1.89M
                limit = FFMIN(limit, 9 - sharp);
681
1.89M
            }
682
2.75M
            limit = FFMAX(limit, 1);
683
684
2.75M
            s->filter_lut.lim_lut[i] = limit;
685
2.75M
            s->filter_lut.mblim_lut[i] = 2 * (i + 2) + limit;
686
2.75M
        }
687
43.7k
    }
688
322k
    s->s.h.filter.sharpness = sharp;
689
322k
    if ((s->s.h.lf_delta.enabled = get_bits1(&s->gb))) {
690
86.4k
        if ((s->s.h.lf_delta.updated = get_bits1(&s->gb))) {
691
165k
            for (i = 0; i < 4; i++)
692
132k
                if (get_bits1(&s->gb))
693
100k
                    s->s.h.lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
694
99.1k
            for (i = 0; i < 2; i++)
695
66.1k
                if (get_bits1(&s->gb))
696
36.2k
                    s->s.h.lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
697
33.0k
        }
698
86.4k
    }
699
700
    /* quantization header data */
701
322k
    s->s.h.yac_qi      = get_bits(&s->gb, 8);
702
322k
    s->s.h.ydc_qdelta  = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
703
322k
    s->s.h.uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
704
322k
    s->s.h.uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
705
322k
    s->s.h.lossless    = s->s.h.yac_qi == 0 && s->s.h.ydc_qdelta == 0 &&
706
16.8k
                       s->s.h.uvdc_qdelta == 0 && s->s.h.uvac_qdelta == 0;
707
322k
#if FF_API_CODEC_PROPS
708
322k
FF_DISABLE_DEPRECATION_WARNINGS
709
322k
    if (s->s.h.lossless)
710
15.5k
        avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
711
322k
FF_ENABLE_DEPRECATION_WARNINGS
712
322k
#endif
713
714
    /* segmentation header info */
715
322k
    if ((s->s.h.segmentation.enabled = get_bits1(&s->gb))) {
716
242k
        if ((s->s.h.segmentation.update_map = get_bits1(&s->gb))) {
717
1.82M
            for (i = 0; i < 7; i++)
718
1.59M
                s->s.h.segmentation.prob[i] = get_bits1(&s->gb) ?
719
1.12M
                                 get_bits(&s->gb, 8) : 255;
720
228k
            if ((s->s.h.segmentation.temporal = get_bits1(&s->gb)))
721
134k
                for (i = 0; i < 3; i++)
722
100k
                    s->s.h.segmentation.pred_prob[i] = get_bits1(&s->gb) ?
723
61.3k
                                         get_bits(&s->gb, 8) : 255;
724
228k
        }
725
726
242k
        if (get_bits1(&s->gb)) {
727
33.5k
            s->s.h.segmentation.absolute_vals = get_bits1(&s->gb);
728
302k
            for (i = 0; i < 8; i++) {
729
268k
                if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
730
157k
                    s->s.h.segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
731
268k
                if ((s->s.h.segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
732
117k
                    s->s.h.segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
733
268k
                if ((s->s.h.segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
734
88.0k
                    s->s.h.segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
735
268k
                s->s.h.segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
736
268k
            }
737
33.5k
        }
738
242k
    } else {
739
        // Reset fields under segmentation switch if segmentation is disabled.
740
        // This is necessary because some hwaccels don't ignore these fields
741
        // if segmentation is disabled.
742
79.7k
        s->s.h.segmentation.temporal = 0;
743
79.7k
        s->s.h.segmentation.update_map = 0;
744
79.7k
    }
745
746
    // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
747
2.34M
    for (i = 0; i < (s->s.h.segmentation.enabled ? 8 : 1); i++) {
748
2.02M
        int qyac, qydc, quvac, quvdc, lflvl, sh;
749
750
2.02M
        if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].q_enabled) {
751
666k
            if (s->s.h.segmentation.absolute_vals)
752
555k
                qyac = av_clip_uintp2(s->s.h.segmentation.feat[i].q_val, 8);
753
111k
            else
754
111k
                qyac = av_clip_uintp2(s->s.h.yac_qi + s->s.h.segmentation.feat[i].q_val, 8);
755
1.35M
        } else {
756
1.35M
            qyac  = s->s.h.yac_qi;
757
1.35M
        }
758
2.02M
        qydc  = av_clip_uintp2(qyac + s->s.h.ydc_qdelta, 8);
759
2.02M
        quvdc = av_clip_uintp2(qyac + s->s.h.uvdc_qdelta, 8);
760
2.02M
        quvac = av_clip_uintp2(qyac + s->s.h.uvac_qdelta, 8);
761
2.02M
        qyac  = av_clip_uintp2(qyac, 8);
762
763
2.02M
        s->s.h.segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[s->bpp_index][qydc];
764
2.02M
        s->s.h.segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[s->bpp_index][qyac];
765
2.02M
        s->s.h.segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[s->bpp_index][quvdc];
766
2.02M
        s->s.h.segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[s->bpp_index][quvac];
767
768
2.02M
        sh = s->s.h.filter.level >= 32;
769
2.02M
        if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].lf_enabled) {
770
607k
            if (s->s.h.segmentation.absolute_vals)
771
449k
                lflvl = av_clip_uintp2(s->s.h.segmentation.feat[i].lf_val, 6);
772
157k
            else
773
157k
                lflvl = av_clip_uintp2(s->s.h.filter.level + s->s.h.segmentation.feat[i].lf_val, 6);
774
1.41M
        } else {
775
1.41M
            lflvl  = s->s.h.filter.level;
776
1.41M
        }
777
2.02M
        if (s->s.h.lf_delta.enabled) {
778
383k
            s->s.h.segmentation.feat[i].lflvl[0][0] =
779
383k
            s->s.h.segmentation.feat[i].lflvl[0][1] =
780
383k
                av_clip_uintp2(lflvl + (s->s.h.lf_delta.ref[0] * (1 << sh)), 6);
781
1.53M
            for (j = 1; j < 4; j++) {
782
1.15M
                s->s.h.segmentation.feat[i].lflvl[j][0] =
783
1.15M
                    av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
784
1.15M
                                             s->s.h.lf_delta.mode[0]) * (1 << sh)), 6);
785
1.15M
                s->s.h.segmentation.feat[i].lflvl[j][1] =
786
1.15M
                    av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
787
1.15M
                                             s->s.h.lf_delta.mode[1]) * (1 << sh)), 6);
788
1.15M
            }
789
1.63M
        } else {
790
1.63M
            memset(s->s.h.segmentation.feat[i].lflvl, lflvl,
791
1.63M
                   sizeof(s->s.h.segmentation.feat[i].lflvl));
792
1.63M
        }
793
2.02M
    }
794
795
    /* tiling info */
796
322k
    if ((changed = update_size(avctx, w, h)) < 0) {
797
1.07k
        av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n",
798
1.07k
               w, h, s->pix_fmt);
799
1.07k
        return changed;
800
1.07k
    }
801
321k
    for (s->s.h.tiling.log2_tile_cols = 0;
802
322k
         s->sb_cols > (64 << s->s.h.tiling.log2_tile_cols);
803
321k
         s->s.h.tiling.log2_tile_cols++) ;
804
712k
    for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
805
321k
    max = FFMAX(0, max - 1);
806
323k
    while (max > s->s.h.tiling.log2_tile_cols) {
807
139k
        if (get_bits1(&s->gb))
808
1.37k
            s->s.h.tiling.log2_tile_cols++;
809
137k
        else
810
137k
            break;
811
139k
    }
812
321k
    s->s.h.tiling.log2_tile_rows = decode012(&s->gb);
813
321k
    s->s.h.tiling.tile_rows = 1 << s->s.h.tiling.log2_tile_rows;
814
321k
    if (s->s.h.tiling.tile_cols != (1 << s->s.h.tiling.log2_tile_cols) || changed) {
815
38.4k
        int n_range_coders;
816
38.4k
        VPXRangeCoder *rc;
817
818
38.4k
        if (s->td) {
819
55.0k
            for (i = 0; i < s->active_tile_cols; i++)
820
27.5k
                vp9_tile_data_free(&s->td[i]);
821
27.5k
            av_freep(&s->td);
822
27.5k
        }
823
824
38.4k
        s->s.h.tiling.tile_cols = 1 << s->s.h.tiling.log2_tile_cols;
825
38.4k
        s->active_tile_cols = avctx->active_thread_type == FF_THREAD_SLICE ?
826
38.4k
                              s->s.h.tiling.tile_cols : 1;
827
38.4k
        vp9_alloc_entries(avctx, s->sb_rows);
828
38.4k
        if (avctx->active_thread_type == FF_THREAD_SLICE) {
829
0
            n_range_coders = 4; // max_tile_rows
830
38.4k
        } else {
831
38.4k
            n_range_coders = s->s.h.tiling.tile_cols;
832
38.4k
        }
833
38.4k
        s->td = av_calloc(s->active_tile_cols, sizeof(VP9TileData) +
834
38.4k
                                 n_range_coders * sizeof(VPXRangeCoder));
835
38.4k
        if (!s->td)
836
0
            return AVERROR(ENOMEM);
837
38.4k
        rc = (VPXRangeCoder *) &s->td[s->active_tile_cols];
838
76.8k
        for (i = 0; i < s->active_tile_cols; i++) {
839
38.4k
            s->td[i].s = s;
840
38.4k
            s->td[i].c_b = rc;
841
38.4k
            rc += n_range_coders;
842
38.4k
        }
843
38.4k
    }
844
845
    /* check reference frames */
846
321k
    if (!s->s.h.keyframe && !s->s.h.intraonly) {
847
290k
        int valid_ref_frame = 0;
848
1.16M
        for (i = 0; i < 3; i++) {
849
870k
            AVFrame *ref = s->s.refs[s->s.h.refidx[i]].f;
850
870k
            int refw = ref->width, refh = ref->height;
851
852
870k
            if (ref->format != avctx->pix_fmt) {
853
694
                av_log(avctx, AV_LOG_ERROR,
854
694
                       "Ref pixfmt (%s) did not match current frame (%s)",
855
694
                       av_get_pix_fmt_name(ref->format),
856
694
                       av_get_pix_fmt_name(avctx->pix_fmt));
857
694
                return AVERROR_INVALIDDATA;
858
869k
            } else if (refw == w && refh == h) {
859
708k
                s->mvscale[i][0] = s->mvscale[i][1] = 0;
860
708k
            } else {
861
                /* Check to make sure at least one of frames that */
862
                /* this frame references has valid dimensions     */
863
161k
                if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
864
27.5k
                    av_log(avctx, AV_LOG_WARNING,
865
27.5k
                           "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
866
27.5k
                           refw, refh, w, h);
867
27.5k
                    s->mvscale[i][0] = s->mvscale[i][1] = REF_INVALID_SCALE;
868
27.5k
                    continue;
869
27.5k
                }
870
134k
                s->mvscale[i][0] = (refw << 14) / w;
871
134k
                s->mvscale[i][1] = (refh << 14) / h;
872
134k
                s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
873
134k
                s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
874
134k
            }
875
842k
            valid_ref_frame++;
876
842k
        }
877
289k
        if (!valid_ref_frame) {
878
7.81k
            av_log(avctx, AV_LOG_ERROR, "No valid reference frame is found, bitstream not supported\n");
879
7.81k
            return AVERROR_INVALIDDATA;
880
7.81k
        }
881
289k
    }
882
883
313k
    if (s->s.h.keyframe || s->s.h.errorres || (s->s.h.intraonly && s->s.h.resetctx == 3)) {
884
30.7k
        s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
885
30.7k
                           s->prob_ctx[3].p = ff_vp9_default_probs;
886
30.7k
        memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
887
30.7k
               sizeof(ff_vp9_default_coef_probs));
888
30.7k
        memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
889
30.7k
               sizeof(ff_vp9_default_coef_probs));
890
30.7k
        memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
891
30.7k
               sizeof(ff_vp9_default_coef_probs));
892
30.7k
        memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
893
30.7k
               sizeof(ff_vp9_default_coef_probs));
894
282k
    } else if (s->s.h.intraonly && s->s.h.resetctx == 2) {
895
40
        s->prob_ctx[c].p = ff_vp9_default_probs;
896
40
        memcpy(s->prob_ctx[c].coef, ff_vp9_default_coef_probs,
897
40
               sizeof(ff_vp9_default_coef_probs));
898
40
    }
899
900
    // next 16 bits is size of the rest of the header (arith-coded)
901
313k
    s->s.h.compressed_header_size = size2 = get_bits(&s->gb, 16);
902
313k
    s->s.h.uncompressed_header_size = (get_bits_count(&s->gb) + 7) / 8;
903
904
313k
    data2 = align_get_bits(&s->gb);
905
313k
    if (size2 > size - (data2 - data)) {
906
8.93k
        av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
907
8.93k
        return AVERROR_INVALIDDATA;
908
8.93k
    }
909
304k
    ret = ff_vpx_init_range_decoder(&s->c, data2, size2);
910
304k
    if (ret < 0)
911
5.58k
        return ret;
912
913
298k
    if (vpx_rac_get_prob_branchy(&s->c, 128)) { // marker bit
914
1.64k
        av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
915
1.64k
        return AVERROR_INVALIDDATA;
916
1.64k
    }
917
918
593k
    for (i = 0; i < s->active_tile_cols; i++) {
919
296k
        if (s->s.h.keyframe || s->s.h.intraonly) {
920
27.1k
            memset(s->td[i].counts.coef, 0, sizeof(s->td[0].counts.coef));
921
27.1k
            memset(s->td[i].counts.eob,  0, sizeof(s->td[0].counts.eob));
922
269k
        } else {
923
269k
            memset(&s->td[i].counts, 0, sizeof(s->td[0].counts));
924
269k
        }
925
296k
        s->td[i].nb_block_structure = 0;
926
296k
    }
927
928
    /* FIXME is it faster to not copy here, but do it down in the fw updates
929
     * as explicit copies if the fw update is missing (and skip the copy upon
930
     * fw update)? */
931
296k
    s->prob.p = s->prob_ctx[c].p;
932
933
    // txfm updates
934
296k
    if (s->s.h.lossless) {
935
14.1k
        s->s.h.txfmmode = TX_4X4;
936
282k
    } else {
937
282k
        s->s.h.txfmmode = vp89_rac_get_uint(&s->c, 2);
938
282k
        if (s->s.h.txfmmode == 3)
939
71.9k
            s->s.h.txfmmode += vp89_rac_get(&s->c);
940
941
282k
        if (s->s.h.txfmmode == TX_SWITCHABLE) {
942
154k
            for (i = 0; i < 2; i++)
943
102k
                if (vpx_rac_get_prob_branchy(&s->c, 252))
944
255
                    s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
945
154k
            for (i = 0; i < 2; i++)
946
308k
                for (j = 0; j < 2; j++)
947
205k
                    if (vpx_rac_get_prob_branchy(&s->c, 252))
948
547
                        s->prob.p.tx16p[i][j] =
949
547
                            update_prob(&s->c, s->prob.p.tx16p[i][j]);
950
154k
            for (i = 0; i < 2; i++)
951
411k
                for (j = 0; j < 3; j++)
952
308k
                    if (vpx_rac_get_prob_branchy(&s->c, 252))
953
963
                        s->prob.p.tx32p[i][j] =
954
963
                            update_prob(&s->c, s->prob.p.tx32p[i][j]);
955
51.3k
        }
956
282k
    }
957
958
    // coef updates
959
583k
    for (i = 0; i < 4; i++) {
960
531k
        uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
961
531k
        if (vp89_rac_get(&s->c)) {
962
377k
            for (j = 0; j < 2; j++)
963
754k
                for (k = 0; k < 2; k++)
964
3.51M
                    for (l = 0; l < 6; l++)
965
19.6M
                        for (m = 0; m < 6; m++) {
966
17.0M
                            uint8_t *p = s->prob.coef[i][j][k][l][m];
967
17.0M
                            uint8_t *r = ref[j][k][l][m];
968
17.0M
                            if (m >= 3 && l == 0) // dc only has 3 pt
969
502k
                                break;
970
66.3M
                            for (n = 0; n < 3; n++) {
971
49.7M
                                if (vpx_rac_get_prob_branchy(&s->c, 252))
972
973k
                                    p[n] = update_prob(&s->c, r[n]);
973
48.8M
                                else
974
48.8M
                                    p[n] = r[n];
975
49.7M
                            }
976
16.5M
                            memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
977
16.5M
                        }
978
406k
        } else {
979
1.21M
            for (j = 0; j < 2; j++)
980
2.43M
                for (k = 0; k < 2; k++)
981
11.3M
                    for (l = 0; l < 6; l++)
982
64.9M
                        for (m = 0; m < 6; m++) {
983
56.8M
                            uint8_t *p = s->prob.coef[i][j][k][l][m];
984
56.8M
                            uint8_t *r = ref[j][k][l][m];
985
56.8M
                            if (m > 3 && l == 0) // dc only has 3 pt
986
1.62M
                                break;
987
55.2M
                            memcpy(p, r, 3);
988
55.2M
                            memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
989
55.2M
                        }
990
406k
        }
991
531k
        if (s->s.h.txfmmode == i)
992
245k
            break;
993
531k
    }
994
995
    // mode updates
996
1.18M
    for (i = 0; i < 3; i++)
997
890k
        if (vpx_rac_get_prob_branchy(&s->c, 252))
998
3.26k
            s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
999
296k
    if (!s->s.h.keyframe && !s->s.h.intraonly) {
1000
2.15M
        for (i = 0; i < 7; i++)
1001
7.55M
            for (j = 0; j < 3; j++)
1002
5.66M
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1003
168k
                    s->prob.p.mv_mode[i][j] =
1004
168k
                        update_prob(&s->c, s->prob.p.mv_mode[i][j]);
1005
1006
269k
        if (s->s.h.filtermode == FILTER_SWITCHABLE)
1007
425k
            for (i = 0; i < 4; i++)
1008
1.02M
                for (j = 0; j < 2; j++)
1009
680k
                    if (vpx_rac_get_prob_branchy(&s->c, 252))
1010
20.2k
                        s->prob.p.filter[i][j] =
1011
20.2k
                            update_prob(&s->c, s->prob.p.filter[i][j]);
1012
1013
1.34M
        for (i = 0; i < 4; i++)
1014
1.07M
            if (vpx_rac_get_prob_branchy(&s->c, 252))
1015
83.9k
                s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
1016
1017
269k
        if (s->s.h.allowcompinter) {
1018
269k
            s->s.h.comppredmode = vp89_rac_get(&s->c);
1019
269k
            if (s->s.h.comppredmode)
1020
207k
                s->s.h.comppredmode += vp89_rac_get(&s->c);
1021
269k
            if (s->s.h.comppredmode == PRED_SWITCHABLE)
1022
557k
                for (i = 0; i < 5; i++)
1023
464k
                    if (vpx_rac_get_prob_branchy(&s->c, 252))
1024
196
                        s->prob.p.comp[i] =
1025
196
                            update_prob(&s->c, s->prob.p.comp[i]);
1026
269k
        } else {
1027
637
            s->s.h.comppredmode = PRED_SINGLEREF;
1028
637
        }
1029
1030
269k
        if (s->s.h.comppredmode != PRED_COMPREF) {
1031
930k
            for (i = 0; i < 5; i++) {
1032
775k
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1033
2.25k
                    s->prob.p.single_ref[i][0] =
1034
2.25k
                        update_prob(&s->c, s->prob.p.single_ref[i][0]);
1035
775k
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1036
11.6k
                    s->prob.p.single_ref[i][1] =
1037
11.6k
                        update_prob(&s->c, s->prob.p.single_ref[i][1]);
1038
775k
            }
1039
155k
        }
1040
1041
269k
        if (s->s.h.comppredmode != PRED_SINGLEREF) {
1042
1.24M
            for (i = 0; i < 5; i++)
1043
1.03M
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1044
93.9k
                    s->prob.p.comp_ref[i] =
1045
93.9k
                        update_prob(&s->c, s->prob.p.comp_ref[i]);
1046
207k
        }
1047
1048
1.34M
        for (i = 0; i < 4; i++)
1049
10.7M
            for (j = 0; j < 9; j++)
1050
9.71M
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1051
37.6k
                    s->prob.p.y_mode[i][j] =
1052
37.6k
                        update_prob(&s->c, s->prob.p.y_mode[i][j]);
1053
1054
1.34M
        for (i = 0; i < 4; i++)
1055
5.39M
            for (j = 0; j < 4; j++)
1056
17.2M
                for (k = 0; k < 3; k++)
1057
12.9M
                    if (vpx_rac_get_prob_branchy(&s->c, 252))
1058
360k
                        s->prob.p.partition[3 - i][j][k] =
1059
360k
                            update_prob(&s->c,
1060
360k
                                        s->prob.p.partition[3 - i][j][k]);
1061
1062
        // mv fields don't use the update_prob subexp model for some reason
1063
1.07M
        for (i = 0; i < 3; i++)
1064
809k
            if (vpx_rac_get_prob_branchy(&s->c, 252))
1065
2.99k
                s->prob.p.mv_joint[i] = (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1066
1067
809k
        for (i = 0; i < 2; i++) {
1068
539k
            if (vpx_rac_get_prob_branchy(&s->c, 252))
1069
8.56k
                s->prob.p.mv_comp[i].sign =
1070
8.56k
                    (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1071
1072
5.93M
            for (j = 0; j < 10; j++)
1073
5.39M
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1074
133k
                    s->prob.p.mv_comp[i].classes[j] =
1075
133k
                        (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1076
1077
539k
            if (vpx_rac_get_prob_branchy(&s->c, 252))
1078
654
                s->prob.p.mv_comp[i].class0 =
1079
654
                    (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1080
1081
5.93M
            for (j = 0; j < 10; j++)
1082
5.39M
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1083
19.6k
                    s->prob.p.mv_comp[i].bits[j] =
1084
19.6k
                        (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1085
539k
        }
1086
1087
809k
        for (i = 0; i < 2; i++) {
1088
1.61M
            for (j = 0; j < 2; j++)
1089
4.31M
                for (k = 0; k < 3; k++)
1090
3.23M
                    if (vpx_rac_get_prob_branchy(&s->c, 252))
1091
66.2k
                        s->prob.p.mv_comp[i].class0_fp[j][k] =
1092
66.2k
                            (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1093
1094
2.15M
            for (j = 0; j < 3; j++)
1095
1.61M
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1096
3.93k
                    s->prob.p.mv_comp[i].fp[j] =
1097
3.93k
                        (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1098
539k
        }
1099
1100
269k
        if (s->s.h.highprecisionmvs) {
1101
586k
            for (i = 0; i < 2; i++) {
1102
390k
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1103
685
                    s->prob.p.mv_comp[i].class0_hp =
1104
685
                        (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1105
1106
390k
                if (vpx_rac_get_prob_branchy(&s->c, 252))
1107
772
                    s->prob.p.mv_comp[i].hp =
1108
772
                        (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1109
390k
            }
1110
195k
        }
1111
269k
    }
1112
1113
296k
    return (data2 - data) + size2;
1114
298k
}
1115
1116
static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1117
                      ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1118
39.3M
{
1119
39.3M
    const VP9Context *s = td->s;
1120
39.3M
    int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
1121
39.3M
            (((td->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
1122
39.3M
    const uint8_t *p = s->s.h.keyframe || s->s.h.intraonly ? ff_vp9_default_kf_partition_probs[bl][c] :
1123
39.3M
                                                     s->prob.p.partition[bl][c];
1124
39.3M
    enum BlockPartition bp;
1125
39.3M
    ptrdiff_t hbs = 4 >> bl;
1126
39.3M
    AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1127
39.3M
    ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1128
39.3M
    int bytesperpixel = s->bytesperpixel;
1129
1130
39.3M
    if (bl == BL_8X8) {
1131
19.1M
        bp = vp89_rac_get_tree(td->c, ff_vp9_partition_tree, p);
1132
19.1M
        ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1133
20.1M
    } else if (col + hbs < s->cols) { // FIXME why not <=?
1134
14.6M
        if (row + hbs < s->rows) { // FIXME why not <=?
1135
9.80M
            bp = vp89_rac_get_tree(td->c, ff_vp9_partition_tree, p);
1136
9.80M
            switch (bp) {
1137
3.13M
            case PARTITION_NONE:
1138
3.13M
                ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1139
3.13M
                break;
1140
760k
            case PARTITION_H:
1141
760k
                ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1142
760k
                yoff  += hbs * 8 * y_stride;
1143
760k
                uvoff += hbs * 8 * uv_stride >> s->ss_v;
1144
760k
                ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
1145
760k
                break;
1146
745k
            case PARTITION_V:
1147
745k
                ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1148
745k
                yoff  += hbs * 8 * bytesperpixel;
1149
745k
                uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1150
745k
                ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
1151
745k
                break;
1152
5.16M
            case PARTITION_SPLIT:
1153
5.16M
                decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1154
5.16M
                decode_sb(td, row, col + hbs, lflvl,
1155
5.16M
                          yoff + 8 * hbs * bytesperpixel,
1156
5.16M
                          uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1157
5.16M
                yoff  += hbs * 8 * y_stride;
1158
5.16M
                uvoff += hbs * 8 * uv_stride >> s->ss_v;
1159
5.16M
                decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1160
5.16M
                decode_sb(td, row + hbs, col + hbs, lflvl,
1161
5.16M
                          yoff + 8 * hbs * bytesperpixel,
1162
5.16M
                          uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1163
5.16M
                break;
1164
0
            default:
1165
0
                av_unreachable("ff_vp9_partition_tree only has "
1166
9.80M
                               "the four PARTITION_* terminal codes");
1167
9.80M
            }
1168
9.80M
        } else if (vpx_rac_get_prob_branchy(td->c, p[1])) {
1169
2.97M
            bp = PARTITION_SPLIT;
1170
2.97M
            decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1171
2.97M
            decode_sb(td, row, col + hbs, lflvl,
1172
2.97M
                      yoff + 8 * hbs * bytesperpixel,
1173
2.97M
                      uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1174
2.97M
        } else {
1175
1.83M
            bp = PARTITION_H;
1176
1.83M
            ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1177
1.83M
        }
1178
14.6M
    } else if (row + hbs < s->rows) { // FIXME why not <=?
1179
5.20M
        if (vpx_rac_get_prob_branchy(td->c, p[2])) {
1180
3.70M
            bp = PARTITION_SPLIT;
1181
3.70M
            decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1182
3.70M
            yoff  += hbs * 8 * y_stride;
1183
3.70M
            uvoff += hbs * 8 * uv_stride >> s->ss_v;
1184
3.70M
            decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1185
3.70M
        } else {
1186
1.49M
            bp = PARTITION_V;
1187
1.49M
            ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1188
1.49M
        }
1189
5.20M
    } else {
1190
361k
        bp = PARTITION_SPLIT;
1191
361k
        decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1192
361k
    }
1193
39.3M
    td->counts.partition[bl][c][bp]++;
1194
39.3M
}
1195
1196
static void decode_sb_mem(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1197
                          ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1198
0
{
1199
0
    const VP9Context *s = td->s;
1200
0
    VP9Block *b = td->b;
1201
0
    ptrdiff_t hbs = 4 >> bl;
1202
0
    AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1203
0
    ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1204
0
    int bytesperpixel = s->bytesperpixel;
1205
1206
0
    if (bl == BL_8X8) {
1207
0
        av_assert2(b->bl == BL_8X8);
1208
0
        ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1209
0
    } else if (td->b->bl == bl) {
1210
0
        ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1211
0
        if (b->bp == PARTITION_H && row + hbs < s->rows) {
1212
0
            yoff  += hbs * 8 * y_stride;
1213
0
            uvoff += hbs * 8 * uv_stride >> s->ss_v;
1214
0
            ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
1215
0
        } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
1216
0
            yoff  += hbs * 8 * bytesperpixel;
1217
0
            uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1218
0
            ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
1219
0
        }
1220
0
    } else {
1221
0
        decode_sb_mem(td, row, col, lflvl, yoff, uvoff, bl + 1);
1222
0
        if (col + hbs < s->cols) { // FIXME why not <=?
1223
0
            if (row + hbs < s->rows) {
1224
0
                decode_sb_mem(td, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
1225
0
                              uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1226
0
                yoff  += hbs * 8 * y_stride;
1227
0
                uvoff += hbs * 8 * uv_stride >> s->ss_v;
1228
0
                decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1229
0
                decode_sb_mem(td, row + hbs, col + hbs, lflvl,
1230
0
                              yoff + 8 * hbs * bytesperpixel,
1231
0
                              uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1232
0
            } else {
1233
0
                yoff  += hbs * 8 * bytesperpixel;
1234
0
                uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1235
0
                decode_sb_mem(td, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
1236
0
            }
1237
0
        } else if (row + hbs < s->rows) {
1238
0
            yoff  += hbs * 8 * y_stride;
1239
0
            uvoff += hbs * 8 * uv_stride >> s->ss_v;
1240
0
            decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1241
0
        }
1242
0
    }
1243
0
}
1244
1245
static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
1246
2.06M
{
1247
2.06M
    int sb_start = ( idx      * n) >> log2_n;
1248
2.06M
    int sb_end   = ((idx + 1) * n) >> log2_n;
1249
2.06M
    *start = FFMIN(sb_start, n) << 3;
1250
2.06M
    *end   = FFMIN(sb_end,   n) << 3;
1251
2.06M
}
1252
1253
static void free_buffers(VP9Context *s)
1254
11.9k
{
1255
11.9k
    int i;
1256
1257
11.9k
    av_freep(&s->intra_pred_data[0]);
1258
22.9k
    for (i = 0; i < s->active_tile_cols; i++)
1259
10.9k
        vp9_tile_data_free(&s->td[i]);
1260
11.9k
}
1261
1262
static av_cold int vp9_decode_free(AVCodecContext *avctx)
1263
11.9k
{
1264
11.9k
    VP9Context *s = avctx->priv_data;
1265
11.9k
    int i;
1266
1267
47.9k
    for (int i = 0; i < 3; i++)
1268
35.9k
        vp9_frame_unref(&s->s.frames[i]);
1269
11.9k
    av_refstruct_pool_uninit(&s->frame_extradata_pool);
1270
107k
    for (i = 0; i < 8; i++) {
1271
95.9k
        ff_progress_frame_unref(&s->s.refs[i]);
1272
95.9k
        ff_progress_frame_unref(&s->next_refs[i]);
1273
95.9k
        vp9_frame_unref(&s->s.ref_frames[i]);
1274
95.9k
    }
1275
1276
11.9k
    free_buffers(s);
1277
11.9k
#if HAVE_THREADS
1278
11.9k
    av_freep(&s->entries);
1279
11.9k
    ff_pthread_free(s, vp9_context_offsets);
1280
11.9k
#endif
1281
1282
11.9k
    av_refstruct_unref(&s->header_ref);
1283
11.9k
    ff_cbs_fragment_free(&s->current_frag);
1284
11.9k
    ff_cbs_close(&s->cbc);
1285
1286
11.9k
    av_freep(&s->td);
1287
11.9k
    return 0;
1288
11.9k
}
1289
1290
static int decode_tiles(AVCodecContext *avctx,
1291
                        const uint8_t *data, int size)
1292
295k
{
1293
295k
    VP9Context *s = avctx->priv_data;
1294
295k
    VP9TileData *td = &s->td[0];
1295
295k
    int row, col, tile_row, tile_col, ret;
1296
295k
    int bytesperpixel;
1297
295k
    int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1298
295k
    AVFrame *f;
1299
295k
    ptrdiff_t yoff, uvoff, ls_y, ls_uv;
1300
1301
295k
    f = s->s.frames[CUR_FRAME].tf.f;
1302
295k
    ls_y = f->linesize[0];
1303
295k
    ls_uv =f->linesize[1];
1304
295k
    bytesperpixel = s->bytesperpixel;
1305
1306
295k
    yoff = uvoff = 0;
1307
522k
    for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1308
296k
        set_tile_offset(&tile_row_start, &tile_row_end,
1309
296k
                        tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1310
1311
579k
        for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1312
296k
            int64_t tile_size;
1313
1314
296k
            if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1315
296k
                tile_row == s->s.h.tiling.tile_rows - 1) {
1316
293k
                tile_size = size;
1317
293k
            } else {
1318
2.55k
                tile_size = AV_RB32(data);
1319
2.55k
                data += 4;
1320
2.55k
                size -= 4;
1321
2.55k
            }
1322
296k
            if (tile_size > size)
1323
1.74k
                return AVERROR_INVALIDDATA;
1324
294k
            ret = ff_vpx_init_range_decoder(&td->c_b[tile_col], data, tile_size);
1325
294k
            if (ret < 0)
1326
5.12k
                return ret;
1327
289k
            if (vpx_rac_get_prob_branchy(&td->c_b[tile_col], 128)) // marker bit
1328
7.06k
                return AVERROR_INVALIDDATA;
1329
282k
            data += tile_size;
1330
282k
            size -= tile_size;
1331
282k
        }
1332
1333
1.99M
        for (row = tile_row_start; row < tile_row_end;
1334
1.77M
             row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1335
1.77M
            VP9Filter *lflvl_ptr = s->lflvl;
1336
1.77M
            ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1337
1338
3.49M
            for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1339
1.77M
                set_tile_offset(&tile_col_start, &tile_col_end,
1340
1.77M
                                tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1341
1.77M
                td->tile_col_start = tile_col_start;
1342
1.77M
                if (s->pass != 2) {
1343
1.77M
                    memset(td->left_partition_ctx, 0, 8);
1344
1.77M
                    memset(td->left_skip_ctx, 0, 8);
1345
1.77M
                    if (s->s.h.keyframe || s->s.h.intraonly) {
1346
330k
                        memset(td->left_mode_ctx, DC_PRED, 16);
1347
1.44M
                    } else {
1348
1.44M
                        memset(td->left_mode_ctx, NEARESTMV, 8);
1349
1.44M
                    }
1350
1.77M
                    memset(td->left_y_nnz_ctx, 0, 16);
1351
1.77M
                    memset(td->left_uv_nnz_ctx, 0, 32);
1352
1.77M
                    memset(td->left_segpred_ctx, 0, 8);
1353
1354
1.77M
                    td->c = &td->c_b[tile_col];
1355
1.77M
                }
1356
1357
1.77M
                for (col = tile_col_start;
1358
6.73M
                     col < tile_col_end;
1359
4.96M
                     col += 8, yoff2 += 64 * bytesperpixel,
1360
5.02M
                     uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1361
                    // FIXME integrate with lf code (i.e. zero after each
1362
                    // use, similar to invtxfm coefficients, or similar)
1363
5.02M
                    if (s->pass != 1) {
1364
5.02M
                        memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1365
5.02M
                    }
1366
1367
5.02M
                    if (s->pass == 2) {
1368
0
                        decode_sb_mem(td, row, col, lflvl_ptr,
1369
0
                                      yoff2, uvoff2, BL_64X64);
1370
5.02M
                    } else {
1371
5.02M
                        if (vpx_rac_is_end(td->c)) {
1372
55.8k
                            return AVERROR_INVALIDDATA;
1373
55.8k
                        }
1374
4.96M
                        decode_sb(td, row, col, lflvl_ptr,
1375
4.96M
                                  yoff2, uvoff2, BL_64X64);
1376
4.96M
                    }
1377
5.02M
                }
1378
1.77M
            }
1379
1380
1.71M
            if (s->pass == 1)
1381
0
                continue;
1382
1383
            // backup pre-loopfilter reconstruction data for intra
1384
            // prediction of next row of sb64s
1385
1.71M
            if (row + 8 < s->rows) {
1386
1.49M
                memcpy(s->intra_pred_data[0],
1387
1.49M
                       f->data[0] + yoff + 63 * ls_y,
1388
1.49M
                       8 * s->cols * bytesperpixel);
1389
1.49M
                memcpy(s->intra_pred_data[1],
1390
1.49M
                       f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1391
1.49M
                       8 * s->cols * bytesperpixel >> s->ss_h);
1392
1.49M
                memcpy(s->intra_pred_data[2],
1393
1.49M
                       f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1394
1.49M
                       8 * s->cols * bytesperpixel >> s->ss_h);
1395
1.49M
            }
1396
1397
            // loopfilter one row
1398
1.71M
            if (s->s.h.filter.level) {
1399
1.62M
                yoff2 = yoff;
1400
1.62M
                uvoff2 = uvoff;
1401
1.62M
                lflvl_ptr = s->lflvl;
1402
5.71M
                for (col = 0; col < s->cols;
1403
4.08M
                     col += 8, yoff2 += 64 * bytesperpixel,
1404
4.08M
                     uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1405
4.08M
                    ff_vp9_loopfilter_sb(avctx, lflvl_ptr, row, col,
1406
4.08M
                                         yoff2, uvoff2);
1407
4.08M
                }
1408
1.62M
            }
1409
1410
            // FIXME maybe we can make this more finegrained by running the
1411
            // loopfilter per-block instead of after each sbrow
1412
            // In fact that would also make intra pred left preparation easier?
1413
1.71M
            ff_progress_frame_report(&s->s.frames[CUR_FRAME].tf, row >> 3);
1414
1.71M
        }
1415
282k
    }
1416
225k
    return 0;
1417
295k
}
1418
1419
#if HAVE_THREADS
1420
static av_always_inline
1421
int decode_tiles_mt(AVCodecContext *avctx, void *tdata, int jobnr,
1422
                              int threadnr)
1423
0
{
1424
0
    VP9Context *s = avctx->priv_data;
1425
0
    VP9TileData *td = &s->td[jobnr];
1426
0
    ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1427
0
    int bytesperpixel = s->bytesperpixel, row, col, tile_row;
1428
0
    unsigned tile_cols_len;
1429
0
    int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1430
0
    VP9Filter *lflvl_ptr_base;
1431
0
    AVFrame *f;
1432
1433
0
    f = s->s.frames[CUR_FRAME].tf.f;
1434
0
    ls_y = f->linesize[0];
1435
0
    ls_uv =f->linesize[1];
1436
1437
0
    set_tile_offset(&tile_col_start, &tile_col_end,
1438
0
                    jobnr, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1439
0
    td->tile_col_start  = tile_col_start;
1440
0
    uvoff = (64 * bytesperpixel >> s->ss_h)*(tile_col_start >> 3);
1441
0
    yoff = (64 * bytesperpixel)*(tile_col_start >> 3);
1442
0
    lflvl_ptr_base = s->lflvl+(tile_col_start >> 3);
1443
1444
0
    for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1445
0
        set_tile_offset(&tile_row_start, &tile_row_end,
1446
0
                        tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1447
1448
0
        td->c = &td->c_b[tile_row];
1449
0
        for (row = tile_row_start; row < tile_row_end;
1450
0
             row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1451
0
            ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1452
0
            VP9Filter *lflvl_ptr = lflvl_ptr_base+s->sb_cols*(row >> 3);
1453
1454
0
            memset(td->left_partition_ctx, 0, 8);
1455
0
            memset(td->left_skip_ctx, 0, 8);
1456
0
            if (s->s.h.keyframe || s->s.h.intraonly) {
1457
0
                memset(td->left_mode_ctx, DC_PRED, 16);
1458
0
            } else {
1459
0
                memset(td->left_mode_ctx, NEARESTMV, 8);
1460
0
            }
1461
0
            memset(td->left_y_nnz_ctx, 0, 16);
1462
0
            memset(td->left_uv_nnz_ctx, 0, 32);
1463
0
            memset(td->left_segpred_ctx, 0, 8);
1464
1465
0
            for (col = tile_col_start;
1466
0
                 col < tile_col_end;
1467
0
                 col += 8, yoff2 += 64 * bytesperpixel,
1468
0
                 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1469
                // FIXME integrate with lf code (i.e. zero after each
1470
                // use, similar to invtxfm coefficients, or similar)
1471
0
                memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1472
0
                decode_sb(td, row, col, lflvl_ptr,
1473
0
                            yoff2, uvoff2, BL_64X64);
1474
0
            }
1475
1476
            // backup pre-loopfilter reconstruction data for intra
1477
            // prediction of next row of sb64s
1478
0
            tile_cols_len = tile_col_end - tile_col_start;
1479
0
            if (row + 8 < s->rows) {
1480
0
                memcpy(s->intra_pred_data[0] + (tile_col_start * 8 * bytesperpixel),
1481
0
                       f->data[0] + yoff + 63 * ls_y,
1482
0
                       8 * tile_cols_len * bytesperpixel);
1483
0
                memcpy(s->intra_pred_data[1] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1484
0
                       f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1485
0
                       8 * tile_cols_len * bytesperpixel >> s->ss_h);
1486
0
                memcpy(s->intra_pred_data[2] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1487
0
                       f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1488
0
                       8 * tile_cols_len * bytesperpixel >> s->ss_h);
1489
0
            }
1490
1491
0
            vp9_report_tile_progress(s, row >> 3, 1);
1492
0
        }
1493
0
    }
1494
0
    return 0;
1495
0
}
1496
1497
static av_always_inline
1498
int loopfilter_proc(AVCodecContext *avctx)
1499
0
{
1500
0
    VP9Context *s = avctx->priv_data;
1501
0
    ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1502
0
    VP9Filter *lflvl_ptr;
1503
0
    int bytesperpixel = s->bytesperpixel, col, i;
1504
0
    AVFrame *f;
1505
1506
0
    f = s->s.frames[CUR_FRAME].tf.f;
1507
0
    ls_y = f->linesize[0];
1508
0
    ls_uv =f->linesize[1];
1509
1510
0
    for (i = 0; i < s->sb_rows; i++) {
1511
0
        vp9_await_tile_progress(s, i, s->s.h.tiling.tile_cols);
1512
1513
0
        if (s->s.h.filter.level) {
1514
0
            yoff = (ls_y * 64)*i;
1515
0
            uvoff =  (ls_uv * 64 >> s->ss_v)*i;
1516
0
            lflvl_ptr = s->lflvl+s->sb_cols*i;
1517
0
            for (col = 0; col < s->cols;
1518
0
                 col += 8, yoff += 64 * bytesperpixel,
1519
0
                 uvoff += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1520
0
                ff_vp9_loopfilter_sb(avctx, lflvl_ptr, i << 3, col,
1521
0
                                     yoff, uvoff);
1522
0
            }
1523
0
        }
1524
0
    }
1525
0
    return 0;
1526
0
}
1527
#endif
1528
1529
static int vp9_export_enc_params(VP9Context *s, VP9Frame *frame)
1530
103k
{
1531
103k
    AVVideoEncParams *par;
1532
103k
    unsigned int tile, nb_blocks = 0;
1533
1534
103k
    if (s->s.h.segmentation.enabled) {
1535
143k
        for (tile = 0; tile < s->active_tile_cols; tile++)
1536
71.9k
            nb_blocks += s->td[tile].nb_block_structure;
1537
71.9k
    }
1538
1539
103k
    par = av_video_enc_params_create_side_data(frame->tf.f,
1540
103k
        AV_VIDEO_ENC_PARAMS_VP9, nb_blocks);
1541
103k
    if (!par)
1542
0
        return AVERROR(ENOMEM);
1543
1544
103k
    par->qp             = s->s.h.yac_qi;
1545
103k
    par->delta_qp[0][0] = s->s.h.ydc_qdelta;
1546
103k
    par->delta_qp[1][0] = s->s.h.uvdc_qdelta;
1547
103k
    par->delta_qp[2][0] = s->s.h.uvdc_qdelta;
1548
103k
    par->delta_qp[1][1] = s->s.h.uvac_qdelta;
1549
103k
    par->delta_qp[2][1] = s->s.h.uvac_qdelta;
1550
1551
103k
    if (nb_blocks) {
1552
71.9k
        unsigned int block = 0;
1553
71.9k
        unsigned int tile, block_tile;
1554
1555
143k
        for (tile = 0; tile < s->active_tile_cols; tile++) {
1556
71.9k
            VP9TileData *td = &s->td[tile];
1557
1558
6.12M
            for (block_tile = 0; block_tile < td->nb_block_structure; block_tile++) {
1559
6.05M
                AVVideoBlockParams *b = av_video_enc_params_block(par, block++);
1560
6.05M
                unsigned int      row = td->block_structure[block_tile].row;
1561
6.05M
                unsigned int      col = td->block_structure[block_tile].col;
1562
6.05M
                uint8_t        seg_id = frame->segmentation_map[row * 8 * s->sb_cols + col];
1563
1564
6.05M
                b->src_x = col * 8;
1565
6.05M
                b->src_y = row * 8;
1566
6.05M
                b->w     = 1 << (3 + td->block_structure[block_tile].block_size_idx_x);
1567
6.05M
                b->h     = 1 << (3 + td->block_structure[block_tile].block_size_idx_y);
1568
1569
6.05M
                if (s->s.h.segmentation.feat[seg_id].q_enabled) {
1570
4.10M
                    b->delta_qp = s->s.h.segmentation.feat[seg_id].q_val;
1571
4.10M
                    if (s->s.h.segmentation.absolute_vals)
1572
3.82M
                        b->delta_qp -= par->qp;
1573
4.10M
                }
1574
6.05M
            }
1575
71.9k
        }
1576
71.9k
    }
1577
1578
103k
    return 0;
1579
103k
}
1580
1581
static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1582
                            int *got_frame, AVPacket *pkt)
1583
738k
{
1584
738k
    const uint8_t *data = pkt->data;
1585
738k
    int size = pkt->size;
1586
738k
    VP9Context *s = avctx->priv_data;
1587
738k
    int ret, i, j, ref;
1588
738k
    CodedBitstreamUnit *unit;
1589
738k
    VP9RawFrame *rf;
1590
1591
738k
    int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
1592
319k
                            (!s->s.h.segmentation.enabled || !s->s.h.segmentation.update_map);
1593
738k
    const VP9Frame *src;
1594
738k
    AVFrame *f;
1595
1596
738k
    ret = ff_cbs_read_packet(s->cbc, &s->current_frag, pkt);
1597
738k
    if (ret < 0) {
1598
345k
        ff_cbs_fragment_reset(&s->current_frag);
1599
345k
        av_log(avctx, AV_LOG_ERROR, "Failed to read frame header.\n");
1600
345k
        return ret;
1601
345k
    }
1602
1603
392k
    unit = &s->current_frag.units[0];
1604
392k
    rf = unit->content;
1605
1606
392k
    av_refstruct_replace(&s->header_ref, unit->content_ref);
1607
392k
    s->frame_header = &rf->header;
1608
1609
392k
    if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
1610
87.9k
        return ret;
1611
305k
    } else if (ret == 0) {
1612
8.09k
        if (!s->s.refs[ref].f) {
1613
5.17k
            av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
1614
5.17k
            return AVERROR_INVALIDDATA;
1615
5.17k
        }
1616
26.2k
        for (int i = 0; i < 8; i++)
1617
23.3k
            ff_progress_frame_replace(&s->next_refs[i], &s->s.refs[i]);
1618
2.91k
        ff_thread_finish_setup(avctx);
1619
2.91k
        ff_progress_frame_await(&s->s.refs[ref], INT_MAX);
1620
2.91k
        ff_cbs_fragment_reset(&s->current_frag);
1621
1622
2.91k
        if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
1623
0
            return ret;
1624
2.91k
        frame->pts     = pkt->pts;
1625
2.91k
        frame->pkt_dts = pkt->dts;
1626
2.91k
        *got_frame = 1;
1627
2.91k
        return pkt->size;
1628
2.91k
    }
1629
296k
    data += ret;
1630
296k
    size -= ret;
1631
1632
296k
    src = !s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres ?
1633
269k
              &s->s.frames[CUR_FRAME] : &s->s.frames[BLANK_FRAME];
1634
296k
    if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly)
1635
242k
        vp9_frame_replace(&s->s.frames[REF_FRAME_SEGMAP], src);
1636
296k
    vp9_frame_replace(&s->s.frames[REF_FRAME_MVPAIR], src);
1637
296k
    vp9_frame_unref(&s->s.frames[CUR_FRAME]);
1638
296k
    if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0)
1639
1.24k
        return ret;
1640
1641
295k
    s->s.frames[CUR_FRAME].header_ref = av_refstruct_ref(s->header_ref);
1642
295k
    s->s.frames[CUR_FRAME].frame_header = s->frame_header;
1643
1644
295k
    f = s->s.frames[CUR_FRAME].tf.f;
1645
295k
    if (s->s.h.keyframe)
1646
20.5k
        f->flags |= AV_FRAME_FLAG_KEY;
1647
275k
    else
1648
275k
        f->flags &= ~AV_FRAME_FLAG_KEY;
1649
295k
    if (s->s.h.lossless)
1650
14.0k
        f->flags |= AV_FRAME_FLAG_LOSSLESS;
1651
281k
    else
1652
281k
        f->flags &= ~AV_FRAME_FLAG_LOSSLESS;
1653
295k
    f->pict_type = (s->s.h.keyframe || s->s.h.intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1654
1655
    // Non-existent frames have the implicit dimension 0x0 != CUR_FRAME
1656
295k
    if (!s->s.frames[REF_FRAME_MVPAIR].tf.f ||
1657
268k
        (s->s.frames[REF_FRAME_MVPAIR].tf.f->width  != s->s.frames[CUR_FRAME].tf.f->width ||
1658
262k
         s->s.frames[REF_FRAME_MVPAIR].tf.f->height != s->s.frames[CUR_FRAME].tf.f->height)) {
1659
34.3k
        vp9_frame_unref(&s->s.frames[REF_FRAME_SEGMAP]);
1660
34.3k
    }
1661
1662
    // ref frame setup
1663
2.66M
    for (i = 0; i < 8; i++) {
1664
2.36M
        ff_progress_frame_replace(&s->next_refs[i],
1665
2.36M
                                   s->s.h.refreshrefmask & (1 << i) ?
1666
1.20M
                                       &s->s.frames[CUR_FRAME].tf : &s->s.refs[i]);
1667
2.36M
    }
1668
1669
295k
    if (avctx->hwaccel) {
1670
0
        const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1671
0
        ret = hwaccel->start_frame(avctx, pkt->buf, pkt->data, pkt->size);
1672
0
        if (ret < 0)
1673
0
            return ret;
1674
0
        ret = hwaccel->decode_slice(avctx, pkt->data, pkt->size);
1675
0
        if (ret < 0)
1676
0
            return ret;
1677
0
        ret = hwaccel->end_frame(avctx);
1678
0
        if (ret < 0)
1679
0
            return ret;
1680
1681
0
        for (i = 0; i < 8; i++) {
1682
0
            vp9_frame_replace(&s->s.ref_frames[i],
1683
0
                              s->s.h.refreshrefmask & (1 << i) ?
1684
0
                                  &s->s.frames[CUR_FRAME] : &s->s.ref_frames[i]);
1685
0
        }
1686
1687
0
        goto finish;
1688
0
    }
1689
1690
    // main tile decode loop
1691
295k
    memset(s->above_partition_ctx, 0, s->cols);
1692
295k
    memset(s->above_skip_ctx, 0, s->cols);
1693
295k
    if (s->s.h.keyframe || s->s.h.intraonly) {
1694
26.9k
        memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
1695
268k
    } else {
1696
268k
        memset(s->above_mode_ctx, NEARESTMV, s->cols);
1697
268k
    }
1698
295k
    memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
1699
295k
    memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
1700
295k
    memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
1701
295k
    memset(s->above_segpred_ctx, 0, s->cols);
1702
295k
    s->pass = s->s.frames[CUR_FRAME].uses_2pass =
1703
295k
        avctx->active_thread_type == FF_THREAD_FRAME && s->s.h.refreshctx && !s->s.h.parallelmode;
1704
295k
    if ((ret = update_block_buffers(avctx)) < 0) {
1705
0
        av_log(avctx, AV_LOG_ERROR,
1706
0
               "Failed to allocate block buffers\n");
1707
0
        return ret;
1708
0
    }
1709
295k
    if (s->s.h.refreshctx && s->s.h.parallelmode) {
1710
18.4k
        int j, k, l, m;
1711
1712
69.5k
        for (i = 0; i < 4; i++) {
1713
206k
            for (j = 0; j < 2; j++)
1714
412k
                for (k = 0; k < 2; k++)
1715
1.92M
                    for (l = 0; l < 6; l++)
1716
11.5M
                        for (m = 0; m < 6; m++)
1717
9.89M
                            memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
1718
9.89M
                                   s->prob.coef[i][j][k][l][m], 3);
1719
68.7k
            if (s->s.h.txfmmode == i)
1720
17.6k
                break;
1721
68.7k
        }
1722
18.4k
        s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
1723
18.4k
        ff_thread_finish_setup(avctx);
1724
277k
    } else if (!s->s.h.refreshctx) {
1725
59.8k
        ff_thread_finish_setup(avctx);
1726
59.8k
    }
1727
1728
295k
#if HAVE_THREADS
1729
295k
    if (avctx->active_thread_type & FF_THREAD_SLICE) {
1730
0
        for (i = 0; i < s->sb_rows; i++)
1731
0
            atomic_init(&s->entries[i], 0);
1732
0
    }
1733
295k
#endif
1734
1735
295k
    do {
1736
591k
        for (i = 0; i < s->active_tile_cols; i++) {
1737
295k
            s->td[i].b = s->td[i].b_base;
1738
295k
            s->td[i].block = s->td[i].block_base;
1739
295k
            s->td[i].uvblock[0] = s->td[i].uvblock_base[0];
1740
295k
            s->td[i].uvblock[1] = s->td[i].uvblock_base[1];
1741
295k
            s->td[i].eob = s->td[i].eob_base;
1742
295k
            s->td[i].uveob[0] = s->td[i].uveob_base[0];
1743
295k
            s->td[i].uveob[1] = s->td[i].uveob_base[1];
1744
295k
            s->td[i].error_info = 0;
1745
295k
        }
1746
1747
295k
#if HAVE_THREADS
1748
295k
        if (avctx->active_thread_type == FF_THREAD_SLICE) {
1749
0
            int tile_row, tile_col;
1750
1751
0
            av_assert1(!s->pass);
1752
1753
0
            for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1754
0
                for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1755
0
                    int64_t tile_size;
1756
1757
0
                    if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1758
0
                        tile_row == s->s.h.tiling.tile_rows - 1) {
1759
0
                        tile_size = size;
1760
0
                    } else {
1761
0
                        tile_size = AV_RB32(data);
1762
0
                        data += 4;
1763
0
                        size -= 4;
1764
0
                    }
1765
0
                    if (tile_size > size)
1766
0
                        return AVERROR_INVALIDDATA;
1767
0
                    ret = ff_vpx_init_range_decoder(&s->td[tile_col].c_b[tile_row], data, tile_size);
1768
0
                    if (ret < 0)
1769
0
                        return ret;
1770
0
                    if (vpx_rac_get_prob_branchy(&s->td[tile_col].c_b[tile_row], 128)) // marker bit
1771
0
                        return AVERROR_INVALIDDATA;
1772
0
                    data += tile_size;
1773
0
                    size -= tile_size;
1774
0
                }
1775
0
            }
1776
1777
0
            ff_slice_thread_execute_with_mainfunc(avctx, decode_tiles_mt, loopfilter_proc, s->td, NULL, s->s.h.tiling.tile_cols);
1778
0
        } else
1779
295k
#endif
1780
295k
        {
1781
295k
            ret = decode_tiles(avctx, data, size);
1782
295k
            if (ret < 0)
1783
69.7k
                goto fail;
1784
295k
        }
1785
1786
        // Sum all counts fields into td[0].counts for tile threading
1787
225k
        if (avctx->active_thread_type == FF_THREAD_SLICE)
1788
0
            for (i = 1; i < s->s.h.tiling.tile_cols; i++)
1789
0
                for (j = 0; j < sizeof(s->td[i].counts) / sizeof(unsigned); j++)
1790
0
                    ((unsigned *)&s->td[0].counts)[j] += ((unsigned *)&s->td[i].counts)[j];
1791
1792
225k
        if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
1793
177k
            ff_vp9_adapt_probs(s);
1794
177k
            ff_thread_finish_setup(avctx);
1795
177k
        }
1796
225k
    } while (s->pass++ == 1);
1797
1798
225k
    if (s->td->error_info < 0) {
1799
1.75k
        av_log(avctx, AV_LOG_ERROR, "Failed to decode tile data\n");
1800
1.75k
        s->td->error_info = 0;
1801
1.75k
        ret = AVERROR_INVALIDDATA;
1802
1.75k
        goto fail;
1803
1.75k
    }
1804
224k
    if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
1805
103k
        ret = vp9_export_enc_params(s, &s->s.frames[CUR_FRAME]);
1806
103k
        if (ret < 0)
1807
0
            goto fail;
1808
103k
    }
1809
1810
224k
finish:
1811
224k
    ff_cbs_fragment_reset(&s->current_frag);
1812
1813
224k
    ff_progress_frame_report(&s->s.frames[CUR_FRAME].tf, INT_MAX);
1814
    // ref frame setup
1815
2.01M
    for (int i = 0; i < 8; i++)
1816
1.79M
        ff_progress_frame_replace(&s->s.refs[i], &s->next_refs[i]);
1817
1818
224k
    if (!s->s.h.invisible) {
1819
216k
        if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
1820
0
            return ret;
1821
216k
        *got_frame = 1;
1822
216k
    }
1823
1824
224k
    return pkt->size;
1825
71.5k
fail:
1826
71.5k
    ff_progress_frame_report(&s->s.frames[CUR_FRAME].tf, INT_MAX);
1827
71.5k
    return ret;
1828
224k
}
1829
1830
static av_cold void vp9_decode_flush(AVCodecContext *avctx)
1831
105k
{
1832
105k
    VP9Context *s = avctx->priv_data;
1833
105k
    int i;
1834
1835
423k
    for (i = 0; i < 3; i++)
1836
317k
        vp9_frame_unref(&s->s.frames[i]);
1837
1838
953k
    for (i = 0; i < 8; i++) {
1839
847k
        ff_progress_frame_unref(&s->s.refs[i]);
1840
847k
        vp9_frame_unref(&s->s.ref_frames[i]);
1841
847k
    }
1842
1843
105k
    ff_cbs_fragment_reset(&s->current_frag);
1844
105k
    ff_cbs_flush(s->cbc);
1845
1846
105k
    if (FF_HW_HAS_CB(avctx, flush))
1847
0
        FF_HW_SIMPLE_CALL(avctx, flush);
1848
105k
}
1849
1850
static av_cold int vp9_decode_init(AVCodecContext *avctx)
1851
11.9k
{
1852
11.9k
    VP9Context *s = avctx->priv_data;
1853
11.9k
    int ret;
1854
1855
11.9k
    s->last_bpp = 0;
1856
11.9k
    s->s.h.filter.sharpness = -1;
1857
1858
11.9k
    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VP9, avctx);
1859
11.9k
    if (ret < 0)
1860
0
        return ret;
1861
1862
11.9k
#if HAVE_THREADS
1863
11.9k
    if (avctx->active_thread_type & FF_THREAD_SLICE) {
1864
0
        ret = ff_pthread_init(s, vp9_context_offsets);
1865
0
        if (ret < 0)
1866
0
            return ret;
1867
0
    }
1868
11.9k
#endif
1869
1870
11.9k
    return 0;
1871
11.9k
}
1872
1873
#if HAVE_THREADS
1874
static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1875
0
{
1876
0
    VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
1877
1878
0
    for (int i = 0; i < 3; i++)
1879
0
        vp9_frame_replace(&s->s.frames[i], &ssrc->s.frames[i]);
1880
0
    for (int i = 0; i < 8; i++)
1881
0
        ff_progress_frame_replace(&s->s.refs[i], &ssrc->next_refs[i]);
1882
0
    av_refstruct_replace(&s->frame_extradata_pool, ssrc->frame_extradata_pool);
1883
0
    s->frame_extradata_pool_size = ssrc->frame_extradata_pool_size;
1884
1885
0
    av_refstruct_replace(&s->header_ref, ssrc->header_ref);
1886
0
    for (int i = 0; i < 8; i++)
1887
0
        vp9_frame_replace(&s->s.ref_frames[i], &ssrc->s.ref_frames[i]);
1888
1889
0
    s->frame_header = ssrc->frame_header;
1890
0
    memcpy(s->cbc->priv_data, ssrc->cbc->priv_data, sizeof(CodedBitstreamVP9Context));
1891
1892
0
    s->s.h.invisible = ssrc->s.h.invisible;
1893
0
    s->s.h.keyframe = ssrc->s.h.keyframe;
1894
0
    s->s.h.intraonly = ssrc->s.h.intraonly;
1895
0
    s->ss_v = ssrc->ss_v;
1896
0
    s->ss_h = ssrc->ss_h;
1897
0
    s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
1898
0
    s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
1899
0
    s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
1900
0
    s->bytesperpixel = ssrc->bytesperpixel;
1901
0
    s->gf_fmt = ssrc->gf_fmt;
1902
0
    s->w = ssrc->w;
1903
0
    s->h = ssrc->h;
1904
0
    s->s.h.bpp = ssrc->s.h.bpp;
1905
0
    s->bpp_index = ssrc->bpp_index;
1906
0
    s->pix_fmt = ssrc->pix_fmt;
1907
0
    memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
1908
0
    memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
1909
0
    memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
1910
0
           sizeof(s->s.h.segmentation.feat));
1911
1912
0
    return 0;
1913
0
}
1914
#endif
1915
1916
const FFCodec ff_vp9_decoder = {
1917
    .p.name                = "vp9",
1918
    CODEC_LONG_NAME("Google VP9"),
1919
    .p.type                = AVMEDIA_TYPE_VIDEO,
1920
    .p.id                  = AV_CODEC_ID_VP9,
1921
    .priv_data_size        = sizeof(VP9Context),
1922
    .init                  = vp9_decode_init,
1923
    .close                 = vp9_decode_free,
1924
    FF_CODEC_DECODE_CB(vp9_decode_frame),
1925
    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
1926
    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP |
1927
                             FF_CODEC_CAP_SLICE_THREAD_HAS_MF |
1928
                             FF_CODEC_CAP_USES_PROGRESSFRAMES,
1929
    .flush                 = vp9_decode_flush,
1930
    UPDATE_THREAD_CONTEXT(vp9_decode_update_thread_context),
1931
    .p.profiles            = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
1932
    .bsfs                  = "vp9_superframe_split",
1933
    .hw_configs            = (const AVCodecHWConfigInternal *const []) {
1934
#if CONFIG_VP9_DXVA2_HWACCEL
1935
                               HWACCEL_DXVA2(vp9),
1936
#endif
1937
#if CONFIG_VP9_D3D11VA_HWACCEL
1938
                               HWACCEL_D3D11VA(vp9),
1939
#endif
1940
#if CONFIG_VP9_D3D11VA2_HWACCEL
1941
                               HWACCEL_D3D11VA2(vp9),
1942
#endif
1943
#if CONFIG_VP9_D3D12VA_HWACCEL
1944
                               HWACCEL_D3D12VA(vp9),
1945
#endif
1946
#if CONFIG_VP9_NVDEC_HWACCEL
1947
                               HWACCEL_NVDEC(vp9),
1948
#endif
1949
#if CONFIG_VP9_VAAPI_HWACCEL
1950
                               HWACCEL_VAAPI(vp9),
1951
#endif
1952
#if CONFIG_VP9_VDPAU_HWACCEL
1953
                               HWACCEL_VDPAU(vp9),
1954
#endif
1955
#if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
1956
                               HWACCEL_VIDEOTOOLBOX(vp9),
1957
#endif
1958
#if CONFIG_VP9_VULKAN_HWACCEL
1959
                               HWACCEL_VULKAN(vp9),
1960
#endif
1961
                               NULL
1962
                           },
1963
};