Coverage Report

Created: 2026-10-28 08:13

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