GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/vp9.c Lines: 859 1202 71.5 %
Date: 2020-10-23 17:01:47 Branches: 610 850 71.8 %

Line Branch Exec 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 "avcodec.h"
25
#include "get_bits.h"
26
#include "hwconfig.h"
27
#include "internal.h"
28
#include "profiles.h"
29
#include "thread.h"
30
#include "videodsp.h"
31
#include "vp56.h"
32
#include "vp9.h"
33
#include "vp9data.h"
34
#include "vp9dec.h"
35
#include "libavutil/avassert.h"
36
#include "libavutil/pixdesc.h"
37
#include "libavutil/video_enc_params.h"
38
39
#define VP9_SYNCCODE 0x498342
40
41
#if HAVE_THREADS
42
934
static void vp9_free_entries(AVCodecContext *avctx) {
43
934
    VP9Context *s = avctx->priv_data;
44
45
934
    if (avctx->active_thread_type & FF_THREAD_SLICE)  {
46
        pthread_mutex_destroy(&s->progress_mutex);
47
        pthread_cond_destroy(&s->progress_cond);
48
        av_freep(&s->entries);
49
    }
50
934
}
51
52
466
static int vp9_alloc_entries(AVCodecContext *avctx, int n) {
53
466
    VP9Context *s = avctx->priv_data;
54
    int i;
55
56
466
    if (avctx->active_thread_type & FF_THREAD_SLICE)  {
57
        if (s->entries)
58
            av_freep(&s->entries);
59
60
        s->entries = av_malloc_array(n, sizeof(atomic_int));
61
62
        if (!s->entries) {
63
            av_freep(&s->entries);
64
            return AVERROR(ENOMEM);
65
        }
66
67
        for (i  = 0; i < n; i++)
68
            atomic_init(&s->entries[i], 0);
69
70
        pthread_mutex_init(&s->progress_mutex, NULL);
71
        pthread_cond_init(&s->progress_cond, NULL);
72
    }
73
466
    return 0;
74
}
75
76
static void vp9_report_tile_progress(VP9Context *s, int field, int n) {
77
    pthread_mutex_lock(&s->progress_mutex);
78
    atomic_fetch_add_explicit(&s->entries[field], n, memory_order_release);
79
    pthread_cond_signal(&s->progress_cond);
80
    pthread_mutex_unlock(&s->progress_mutex);
81
}
82
83
static void vp9_await_tile_progress(VP9Context *s, int field, int n) {
84
    if (atomic_load_explicit(&s->entries[field], memory_order_acquire) >= n)
85
        return;
86
87
    pthread_mutex_lock(&s->progress_mutex);
88
    while (atomic_load_explicit(&s->entries[field], memory_order_relaxed) != n)
89
        pthread_cond_wait(&s->progress_cond, &s->progress_mutex);
90
    pthread_mutex_unlock(&s->progress_mutex);
91
}
92
#else
93
static void vp9_free_entries(AVCodecContext *avctx) {}
94
static int vp9_alloc_entries(AVCodecContext *avctx, int n) { return 0; }
95
#endif
96
97
936
static void vp9_tile_data_free(VP9TileData *td)
98
{
99
936
    av_freep(&td->b_base);
100
936
    av_freep(&td->block_base);
101
936
    av_freep(&td->block_structure);
102
936
}
103
104
4836
static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
105
{
106
4836
    ff_thread_release_buffer(avctx, &f->tf);
107
4836
    av_buffer_unref(&f->extradata);
108
4836
    av_buffer_unref(&f->hwaccel_priv_buf);
109
4836
    f->segmentation_map = NULL;
110
4836
    f->hwaccel_picture_private = NULL;
111
4836
}
112
113
2277
static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
114
{
115
2277
    VP9Context *s = avctx->priv_data;
116
    int ret, sz;
117
118
2277
    ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF);
119
2277
    if (ret < 0)
120
        return ret;
121
122
2277
    sz = 64 * s->sb_cols * s->sb_rows;
123
2277
    if (sz != s->frame_extradata_pool_size) {
124
468
        av_buffer_pool_uninit(&s->frame_extradata_pool);
125
468
        s->frame_extradata_pool = av_buffer_pool_init(sz * (1 + sizeof(VP9mvrefPair)), NULL);
126
468
        if (!s->frame_extradata_pool) {
127
            s->frame_extradata_pool_size = 0;
128
            goto fail;
129
        }
130
468
        s->frame_extradata_pool_size = sz;
131
    }
132
2277
    f->extradata = av_buffer_pool_get(s->frame_extradata_pool);
133
2277
    if (!f->extradata) {
134
        goto fail;
135
    }
136
2277
    memset(f->extradata->data, 0, f->extradata->size);
137
138
2277
    f->segmentation_map = f->extradata->data;
139
2277
    f->mv = (VP9mvrefPair *) (f->extradata->data + sz);
140
141
2277
    if (avctx->hwaccel) {
142
        const AVHWAccel *hwaccel = avctx->hwaccel;
143
        av_assert0(!f->hwaccel_picture_private);
144
        if (hwaccel->frame_priv_data_size) {
145
            f->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
146
            if (!f->hwaccel_priv_buf)
147
                goto fail;
148
            f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
149
        }
150
    }
151
152
2277
    return 0;
153
154
fail:
155
    vp9_frame_unref(avctx, f);
156
    return AVERROR(ENOMEM);
157
}
158
159
2073
static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src)
160
{
161
    int ret;
162
163
2073
    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
164
2073
    if (ret < 0)
165
        return ret;
166
167
2073
    dst->extradata = av_buffer_ref(src->extradata);
168
2073
    if (!dst->extradata)
169
        goto fail;
170
171
2073
    dst->segmentation_map = src->segmentation_map;
172
2073
    dst->mv = src->mv;
173
2073
    dst->uses_2pass = src->uses_2pass;
174
175
2073
    if (src->hwaccel_picture_private) {
176
        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
177
        if (!dst->hwaccel_priv_buf)
178
            goto fail;
179
        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
180
    }
181
182
2073
    return 0;
183
184
fail:
185
    vp9_frame_unref(avctx, dst);
186
    return AVERROR(ENOMEM);
187
}
188
189
2277
static int update_size(AVCodecContext *avctx, int w, int h)
190
{
191
#define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + \
192
                     CONFIG_VP9_D3D11VA_HWACCEL * 2 + \
193
                     CONFIG_VP9_NVDEC_HWACCEL + \
194
                     CONFIG_VP9_VAAPI_HWACCEL + \
195
                     CONFIG_VP9_VDPAU_HWACCEL)
196
2277
    enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
197
2277
    VP9Context *s = avctx->priv_data;
198
    uint8_t *p;
199
2277
    int bytesperpixel = s->bytesperpixel, ret, cols, rows;
200
    int lflvl_len, i;
201
202

2277
    av_assert0(w > 0 && h > 0);
203
204

2277
    if (!(s->pix_fmt == s->gf_fmt && w == s->w && h == s->h)) {
205
468
        if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
206
            return ret;
207
208
468
        switch (s->pix_fmt) {
209
444
        case AV_PIX_FMT_YUV420P:
210
        case AV_PIX_FMT_YUV420P10:
211
#if CONFIG_VP9_DXVA2_HWACCEL
212
            *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
213
#endif
214
#if CONFIG_VP9_D3D11VA_HWACCEL
215
            *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
216
            *fmtp++ = AV_PIX_FMT_D3D11;
217
#endif
218
#if CONFIG_VP9_NVDEC_HWACCEL
219
            *fmtp++ = AV_PIX_FMT_CUDA;
220
#endif
221
#if CONFIG_VP9_VAAPI_HWACCEL
222
444
            *fmtp++ = AV_PIX_FMT_VAAPI;
223
#endif
224
#if CONFIG_VP9_VDPAU_HWACCEL
225
444
            *fmtp++ = AV_PIX_FMT_VDPAU;
226
#endif
227
444
            break;
228
2
        case AV_PIX_FMT_YUV420P12:
229
#if CONFIG_VP9_NVDEC_HWACCEL
230
            *fmtp++ = AV_PIX_FMT_CUDA;
231
#endif
232
#if CONFIG_VP9_VAAPI_HWACCEL
233
2
            *fmtp++ = AV_PIX_FMT_VAAPI;
234
#endif
235
#if CONFIG_VP9_VDPAU_HWACCEL
236
2
            *fmtp++ = AV_PIX_FMT_VDPAU;
237
#endif
238
2
            break;
239
        }
240
241
468
        *fmtp++ = s->pix_fmt;
242
468
        *fmtp = AV_PIX_FMT_NONE;
243
244
468
        ret = ff_thread_get_format(avctx, pix_fmts);
245
468
        if (ret < 0)
246
            return ret;
247
248
468
        avctx->pix_fmt = ret;
249
468
        s->gf_fmt  = s->pix_fmt;
250
468
        s->w = w;
251
468
        s->h = h;
252
    }
253
254
2277
    cols = (w + 7) >> 3;
255
2277
    rows = (h + 7) >> 3;
256
257


2277
    if (s->intra_pred_data[0] && cols == s->cols && rows == s->rows && s->pix_fmt == s->last_fmt)
258
1809
        return 0;
259
260
468
    s->last_fmt  = s->pix_fmt;
261
468
    s->sb_cols   = (w + 63) >> 6;
262
468
    s->sb_rows   = (h + 63) >> 6;
263
468
    s->cols      = (w + 7) >> 3;
264
468
    s->rows      = (h + 7) >> 3;
265
468
    lflvl_len    = avctx->active_thread_type == FF_THREAD_SLICE ? s->sb_rows : 1;
266
267
#define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
268
468
    av_freep(&s->intra_pred_data[0]);
269
    // FIXME we slightly over-allocate here for subsampled chroma, but a little
270
    // bit of padding shouldn't affect performance...
271
468
    p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
272
468
                                lflvl_len * sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
273
468
    if (!p)
274
        return AVERROR(ENOMEM);
275
468
    assign(s->intra_pred_data[0],  uint8_t *,             64 * bytesperpixel);
276
468
    assign(s->intra_pred_data[1],  uint8_t *,             64 * bytesperpixel);
277
468
    assign(s->intra_pred_data[2],  uint8_t *,             64 * bytesperpixel);
278
468
    assign(s->above_y_nnz_ctx,     uint8_t *,             16);
279
468
    assign(s->above_mode_ctx,      uint8_t *,             16);
280
468
    assign(s->above_mv_ctx,        VP56mv(*)[2],          16);
281
468
    assign(s->above_uv_nnz_ctx[0], uint8_t *,             16);
282
468
    assign(s->above_uv_nnz_ctx[1], uint8_t *,             16);
283
468
    assign(s->above_partition_ctx, uint8_t *,              8);
284
468
    assign(s->above_skip_ctx,      uint8_t *,              8);
285
468
    assign(s->above_txfm_ctx,      uint8_t *,              8);
286
468
    assign(s->above_segpred_ctx,   uint8_t *,              8);
287
468
    assign(s->above_intra_ctx,     uint8_t *,              8);
288
468
    assign(s->above_comp_ctx,      uint8_t *,              8);
289
468
    assign(s->above_ref_ctx,       uint8_t *,              8);
290
468
    assign(s->above_filter_ctx,    uint8_t *,              8);
291
468
    assign(s->lflvl,               VP9Filter *,            lflvl_len);
292
#undef assign
293
294
468
    if (s->td) {
295
4
        for (i = 0; i < s->active_tile_cols; i++)
296
2
            vp9_tile_data_free(&s->td[i]);
297
    }
298
299
468
    if (s->s.h.bpp != s->last_bpp) {
300
466
        ff_vp9dsp_init(&s->dsp, s->s.h.bpp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
301
466
        ff_videodsp_init(&s->vdsp, s->s.h.bpp);
302
466
        s->last_bpp = s->s.h.bpp;
303
    }
304
305
468
    return 0;
306
}
307
308
2277
static int update_block_buffers(AVCodecContext *avctx)
309
{
310
    int i;
311
2277
    VP9Context *s = avctx->priv_data;
312
2277
    int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
313
2277
    VP9TileData *td = &s->td[0];
314
315

2277
    if (td->b_base && td->block_base && s->block_alloc_using_2pass == s->s.frames[CUR_FRAME].uses_2pass)
316
1809
        return 0;
317
318
468
    vp9_tile_data_free(td);
319
468
    chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
320
468
    chroma_eobs   = 16 * 16 >> (s->ss_h + s->ss_v);
321
468
    if (s->s.frames[CUR_FRAME].uses_2pass) {
322
        int sbs = s->sb_cols * s->sb_rows;
323
324
        td->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
325
        td->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
326
                                    16 * 16 + 2 * chroma_eobs) * sbs);
327
        if (!td->b_base || !td->block_base)
328
            return AVERROR(ENOMEM);
329
        td->uvblock_base[0] = td->block_base + sbs * 64 * 64 * bytesperpixel;
330
        td->uvblock_base[1] = td->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
331
        td->eob_base = (uint8_t *) (td->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
332
        td->uveob_base[0] = td->eob_base + 16 * 16 * sbs;
333
        td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs;
334
335
        if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
336
            td->block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure));
337
            if (!td->block_structure)
338
                return AVERROR(ENOMEM);
339
        }
340
    } else {
341
468
        for (i = 1; i < s->active_tile_cols; i++)
342
            vp9_tile_data_free(&s->td[i]);
343
344
936
        for (i = 0; i < s->active_tile_cols; i++) {
345
468
            s->td[i].b_base = av_malloc(sizeof(VP9Block));
346
936
            s->td[i].block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
347
468
                                       16 * 16 + 2 * chroma_eobs);
348

468
            if (!s->td[i].b_base || !s->td[i].block_base)
349
                return AVERROR(ENOMEM);
350
468
            s->td[i].uvblock_base[0] = s->td[i].block_base + 64 * 64 * bytesperpixel;
351
468
            s->td[i].uvblock_base[1] = s->td[i].uvblock_base[0] + chroma_blocks * bytesperpixel;
352
468
            s->td[i].eob_base = (uint8_t *) (s->td[i].uvblock_base[1] + chroma_blocks * bytesperpixel);
353
468
            s->td[i].uveob_base[0] = s->td[i].eob_base + 16 * 16;
354
468
            s->td[i].uveob_base[1] = s->td[i].uveob_base[0] + chroma_eobs;
355
356
468
            if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
357
1
                s->td[i].block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure));
358
1
                if (!s->td[i].block_structure)
359
                    return AVERROR(ENOMEM);
360
            }
361
        }
362
    }
363
468
    s->block_alloc_using_2pass = s->s.frames[CUR_FRAME].uses_2pass;
364
365
468
    return 0;
366
}
367
368
// The sign bit is at the end, not the start, of a bit sequence
369
1709
static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
370
{
371
1709
    int v = get_bits(gb, n);
372
1709
    return get_bits1(gb) ? -v : v;
373
}
374
375
25284
static av_always_inline int inv_recenter_nonneg(int v, int m)
376
{
377
25284
    if (v > 2 * m)
378
6760
        return v;
379
18524
    if (v & 1)
380
11083
        return m - ((v + 1) >> 1);
381
7441
    return m + (v >> 1);
382
}
383
384
// differential forward probability updates
385
25284
static int update_prob(VP56RangeCoder *c, int p)
386
{
387
    static const uint8_t inv_map_table[255] = {
388
          7,  20,  33,  46,  59,  72,  85,  98, 111, 124, 137, 150, 163, 176,
389
        189, 202, 215, 228, 241, 254,   1,   2,   3,   4,   5,   6,   8,   9,
390
         10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  21,  22,  23,  24,
391
         25,  26,  27,  28,  29,  30,  31,  32,  34,  35,  36,  37,  38,  39,
392
         40,  41,  42,  43,  44,  45,  47,  48,  49,  50,  51,  52,  53,  54,
393
         55,  56,  57,  58,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,
394
         70,  71,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
395
         86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  99, 100,
396
        101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
397
        116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
398
        131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
399
        146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
400
        161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
401
        177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
402
        192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
403
        207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
404
        222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
405
        237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
406
        252, 253, 253,
407
    };
408
    int d;
409
410
    /* This code is trying to do a differential probability update. For a
411
     * current probability A in the range [1, 255], the difference to a new
412
     * probability of any value can be expressed differentially as 1-A, 255-A
413
     * where some part of this (absolute range) exists both in positive as
414
     * well as the negative part, whereas another part only exists in one
415
     * half. We're trying to code this shared part differentially, i.e.
416
     * times two where the value of the lowest bit specifies the sign, and
417
     * the single part is then coded on top of this. This absolute difference
418
     * then again has a value of [0, 254], but a bigger value in this range
419
     * indicates that we're further away from the original value A, so we
420
     * can code this as a VLC code, since higher values are increasingly
421
     * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
422
     * updates vs. the 'fine, exact' updates further down the range, which
423
     * adds one extra dimension to this differential update model. */
424
425
25284
    if (!vp8_rac_get(c)) {
426
18911
        d = vp8_rac_get_uint(c, 4) + 0;
427
6373
    } else if (!vp8_rac_get(c)) {
428
2366
        d = vp8_rac_get_uint(c, 4) + 16;
429
4007
    } else if (!vp8_rac_get(c)) {
430
2769
        d = vp8_rac_get_uint(c, 5) + 32;
431
    } else {
432
1238
        d = vp8_rac_get_uint(c, 7);
433
1238
        if (d >= 65)
434
193
            d = (d << 1) - 65 + vp8_rac_get(c);
435
1238
        d += 64;
436
        av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
437
    }
438
439
36389
    return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
440
11105
                    255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
441
}
442
443
466
static int read_colorspace_details(AVCodecContext *avctx)
444
{
445
    static const enum AVColorSpace colorspaces[8] = {
446
        AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
447
        AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
448
    };
449
466
    VP9Context *s = avctx->priv_data;
450
466
    int bits = avctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
451
452
466
    s->bpp_index = bits;
453
466
    s->s.h.bpp = 8 + bits * 2;
454
466
    s->bytesperpixel = (7 + s->s.h.bpp) >> 3;
455
466
    avctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
456
466
    if (avctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
457
        static const enum AVPixelFormat pix_fmt_rgb[3] = {
458
            AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12
459
        };
460
        s->ss_h = s->ss_v = 0;
461
        avctx->color_range = AVCOL_RANGE_JPEG;
462
        s->pix_fmt = pix_fmt_rgb[bits];
463
        if (avctx->profile & 1) {
464
            if (get_bits1(&s->gb)) {
465
                av_log(avctx, AV_LOG_ERROR, "Reserved bit set in RGB\n");
466
                return AVERROR_INVALIDDATA;
467
            }
468
        } else {
469
            av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
470
                   avctx->profile);
471
            return AVERROR_INVALIDDATA;
472
        }
473
    } else {
474
        static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
475
            { { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P },
476
              { AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV420P } },
477
            { { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10 },
478
              { AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV420P10 } },
479
            { { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12 },
480
              { AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV420P12 } }
481
        };
482
466
        avctx->color_range = get_bits1(&s->gb) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
483
466
        if (avctx->profile & 1) {
484
22
            s->ss_h = get_bits1(&s->gb);
485
22
            s->ss_v = get_bits1(&s->gb);
486
22
            s->pix_fmt = pix_fmt_for_ss[bits][s->ss_v][s->ss_h];
487
22
            if (s->pix_fmt == AV_PIX_FMT_YUV420P) {
488
                av_log(avctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
489
                       avctx->profile);
490
                return AVERROR_INVALIDDATA;
491
22
            } else if (get_bits1(&s->gb)) {
492
                av_log(avctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
493
                       avctx->profile);
494
                return AVERROR_INVALIDDATA;
495
            }
496
        } else {
497
444
            s->ss_h = s->ss_v = 1;
498
444
            s->pix_fmt = pix_fmt_for_ss[bits][1][1];
499
        }
500
    }
501
502
466
    return 0;
503
}
504
505
2296
static int decode_frame_header(AVCodecContext *avctx,
506
                               const uint8_t *data, int size, int *ref)
507
{
508
2296
    VP9Context *s = avctx->priv_data;
509
    int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
510
    int last_invisible;
511
    const uint8_t *data2;
512
513
    /* general header */
514
2296
    if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
515
        av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
516
        return ret;
517
    }
518
2296
    if (get_bits(&s->gb, 2) != 0x2) { // frame marker
519
        av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
520
        return AVERROR_INVALIDDATA;
521
    }
522
2296
    avctx->profile  = get_bits1(&s->gb);
523
2296
    avctx->profile |= get_bits1(&s->gb) << 1;
524
2296
    if (avctx->profile == 3) avctx->profile += get_bits1(&s->gb);
525
2296
    if (avctx->profile > 3) {
526
        av_log(avctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", avctx->profile);
527
        return AVERROR_INVALIDDATA;
528
    }
529
2296
    s->s.h.profile = avctx->profile;
530
2296
    if (get_bits1(&s->gb)) {
531
19
        *ref = get_bits(&s->gb, 3);
532
19
        return 0;
533
    }
534
535
2277
    s->last_keyframe  = s->s.h.keyframe;
536
2277
    s->s.h.keyframe   = !get_bits1(&s->gb);
537
538
2277
    last_invisible   = s->s.h.invisible;
539
2277
    s->s.h.invisible = !get_bits1(&s->gb);
540
2277
    s->s.h.errorres  = get_bits1(&s->gb);
541

2277
    s->s.h.use_last_frame_mvs = !s->s.h.errorres && !last_invisible;
542
543
2277
    if (s->s.h.keyframe) {
544
466
        if (get_bits(&s->gb, 24) != VP9_SYNCCODE) { // synccode
545
            av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
546
            return AVERROR_INVALIDDATA;
547
        }
548
466
        if ((ret = read_colorspace_details(avctx)) < 0)
549
            return ret;
550
        // for profile 1, here follows the subsampling bits
551
466
        s->s.h.refreshrefmask = 0xff;
552
466
        w = get_bits(&s->gb, 16) + 1;
553
466
        h = get_bits(&s->gb, 16) + 1;
554
466
        if (get_bits1(&s->gb)) // display size
555
            skip_bits(&s->gb, 32);
556
    } else {
557
1811
        s->s.h.intraonly = s->s.h.invisible ? get_bits1(&s->gb) : 0;
558
1811
        s->s.h.resetctx  = s->s.h.errorres ? 0 : get_bits(&s->gb, 2);
559
1811
        if (s->s.h.intraonly) {
560
6
            if (get_bits(&s->gb, 24) != VP9_SYNCCODE) { // synccode
561
                av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
562
                return AVERROR_INVALIDDATA;
563
            }
564
6
            if (avctx->profile >= 1) {
565
                if ((ret = read_colorspace_details(avctx)) < 0)
566
                    return ret;
567
            } else {
568
6
                s->ss_h = s->ss_v = 1;
569
6
                s->s.h.bpp = 8;
570
6
                s->bpp_index = 0;
571
6
                s->bytesperpixel = 1;
572
6
                s->pix_fmt = AV_PIX_FMT_YUV420P;
573
6
                avctx->colorspace = AVCOL_SPC_BT470BG;
574
6
                avctx->color_range = AVCOL_RANGE_MPEG;
575
            }
576
6
            s->s.h.refreshrefmask = get_bits(&s->gb, 8);
577
6
            w = get_bits(&s->gb, 16) + 1;
578
6
            h = get_bits(&s->gb, 16) + 1;
579
6
            if (get_bits1(&s->gb)) // display size
580
                skip_bits(&s->gb, 32);
581
        } else {
582
1805
            s->s.h.refreshrefmask = get_bits(&s->gb, 8);
583
1805
            s->s.h.refidx[0]      = get_bits(&s->gb, 3);
584

1805
            s->s.h.signbias[0]    = get_bits1(&s->gb) && !s->s.h.errorres;
585
1805
            s->s.h.refidx[1]      = get_bits(&s->gb, 3);
586

1805
            s->s.h.signbias[1]    = get_bits1(&s->gb) && !s->s.h.errorres;
587
1805
            s->s.h.refidx[2]      = get_bits(&s->gb, 3);
588

1805
            s->s.h.signbias[2]    = get_bits1(&s->gb) && !s->s.h.errorres;
589
1805
            if (!s->s.refs[s->s.h.refidx[0]].f->buf[0] ||
590
1805
                !s->s.refs[s->s.h.refidx[1]].f->buf[0] ||
591
1805
                !s->s.refs[s->s.h.refidx[2]].f->buf[0]) {
592
                av_log(avctx, AV_LOG_ERROR, "Not all references are available\n");
593
                return AVERROR_INVALIDDATA;
594
            }
595
1805
            if (get_bits1(&s->gb)) {
596
1803
                w = s->s.refs[s->s.h.refidx[0]].f->width;
597
1803
                h = s->s.refs[s->s.h.refidx[0]].f->height;
598
2
            } else if (get_bits1(&s->gb)) {
599
1
                w = s->s.refs[s->s.h.refidx[1]].f->width;
600
1
                h = s->s.refs[s->s.h.refidx[1]].f->height;
601
1
            } else if (get_bits1(&s->gb)) {
602
                w = s->s.refs[s->s.h.refidx[2]].f->width;
603
                h = s->s.refs[s->s.h.refidx[2]].f->height;
604
            } else {
605
1
                w = get_bits(&s->gb, 16) + 1;
606
1
                h = get_bits(&s->gb, 16) + 1;
607
            }
608
            // Note that in this code, "CUR_FRAME" is actually before we
609
            // have formally allocated a frame, and thus actually represents
610
            // the _last_ frame
611
3608
            s->s.h.use_last_frame_mvs &= s->s.frames[CUR_FRAME].tf.f->width == w &&
612
1803
                                       s->s.frames[CUR_FRAME].tf.f->height == h;
613
1805
            if (get_bits1(&s->gb)) // display size
614
3
                skip_bits(&s->gb, 32);
615
1805
            s->s.h.highprecisionmvs = get_bits1(&s->gb);
616
1805
            s->s.h.filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
617
493
                                                  get_bits(&s->gb, 2);
618
3610
            s->s.h.allowcompinter = s->s.h.signbias[0] != s->s.h.signbias[1] ||
619
1805
                                  s->s.h.signbias[0] != s->s.h.signbias[2];
620
1805
            if (s->s.h.allowcompinter) {
621
450
                if (s->s.h.signbias[0] == s->s.h.signbias[1]) {
622
450
                    s->s.h.fixcompref    = 2;
623
450
                    s->s.h.varcompref[0] = 0;
624
450
                    s->s.h.varcompref[1] = 1;
625
                } else if (s->s.h.signbias[0] == s->s.h.signbias[2]) {
626
                    s->s.h.fixcompref    = 1;
627
                    s->s.h.varcompref[0] = 0;
628
                    s->s.h.varcompref[1] = 2;
629
                } else {
630
                    s->s.h.fixcompref    = 0;
631
                    s->s.h.varcompref[0] = 1;
632
                    s->s.h.varcompref[1] = 2;
633
                }
634
            }
635
        }
636
    }
637
2277
    s->s.h.refreshctx   = s->s.h.errorres ? 0 : get_bits1(&s->gb);
638
2277
    s->s.h.parallelmode = s->s.h.errorres ? 1 : get_bits1(&s->gb);
639
2277
    s->s.h.framectxid   = c = get_bits(&s->gb, 2);
640

2277
    if (s->s.h.keyframe || s->s.h.intraonly)
641
472
        s->s.h.framectxid = 0; // BUG: libvpx ignores this field in keyframes
642
643
    /* loopfilter header data */
644

2277
    if (s->s.h.keyframe || s->s.h.errorres || s->s.h.intraonly) {
645
        // reset loopfilter defaults
646
481
        s->s.h.lf_delta.ref[0] = 1;
647
481
        s->s.h.lf_delta.ref[1] = 0;
648
481
        s->s.h.lf_delta.ref[2] = -1;
649
481
        s->s.h.lf_delta.ref[3] = -1;
650
481
        s->s.h.lf_delta.mode[0] = 0;
651
481
        s->s.h.lf_delta.mode[1] = 0;
652
481
        memset(s->s.h.segmentation.feat, 0, sizeof(s->s.h.segmentation.feat));
653
    }
654
2277
    s->s.h.filter.level = get_bits(&s->gb, 6);
655
2277
    sharp = get_bits(&s->gb, 3);
656
    // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
657
    // the old cache values since they are still valid
658
2277
    if (s->s.h.filter.sharpness != sharp) {
659
30272
        for (i = 1; i <= 63; i++) {
660
29799
            int limit = i;
661
662
29799
            if (sharp > 0) {
663
441
                limit >>= (sharp + 3) >> 2;
664
441
                limit = FFMIN(limit, 9 - sharp);
665
            }
666
29799
            limit = FFMAX(limit, 1);
667
668
29799
            s->filter_lut.lim_lut[i] = limit;
669
29799
            s->filter_lut.mblim_lut[i] = 2 * (i + 2) + limit;
670
        }
671
    }
672
2277
    s->s.h.filter.sharpness = sharp;
673
2277
    if ((s->s.h.lf_delta.enabled = get_bits1(&s->gb))) {
674
2262
        if ((s->s.h.lf_delta.updated = get_bits1(&s->gb))) {
675
2475
            for (i = 0; i < 4; i++)
676
1980
                if (get_bits1(&s->gb))
677
1446
                    s->s.h.lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
678
1485
            for (i = 0; i < 2; i++)
679
990
                if (get_bits1(&s->gb))
680
2
                    s->s.h.lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
681
        }
682
    }
683
684
    /* quantization header data */
685
2277
    s->s.h.yac_qi      = get_bits(&s->gb, 8);
686
2277
    s->s.h.ydc_qdelta  = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
687
2277
    s->s.h.uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
688
2277
    s->s.h.uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
689
17
    s->s.h.lossless    = s->s.h.yac_qi == 0 && s->s.h.ydc_qdelta == 0 &&
690

2294
                       s->s.h.uvdc_qdelta == 0 && s->s.h.uvac_qdelta == 0;
691
2277
    if (s->s.h.lossless)
692
17
        avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
693
694
    /* segmentation header info */
695
2277
    if ((s->s.h.segmentation.enabled = get_bits1(&s->gb))) {
696
236
        if ((s->s.h.segmentation.update_map = get_bits1(&s->gb))) {
697
544
            for (i = 0; i < 7; i++)
698
817
                s->s.h.segmentation.prob[i] = get_bits1(&s->gb) ?
699
341
                                 get_bits(&s->gb, 8) : 255;
700
68
            if ((s->s.h.segmentation.temporal = get_bits1(&s->gb)))
701
240
                for (i = 0; i < 3; i++)
702
360
                    s->s.h.segmentation.pred_prob[i] = get_bits1(&s->gb) ?
703
180
                                         get_bits(&s->gb, 8) : 255;
704
        }
705
706
236
        if (get_bits1(&s->gb)) {
707
70
            s->s.h.segmentation.absolute_vals = get_bits1(&s->gb);
708
630
            for (i = 0; i < 8; i++) {
709
560
                if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
710
249
                    s->s.h.segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
711
560
                if ((s->s.h.segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
712
3
                    s->s.h.segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
713
560
                if ((s->s.h.segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
714
3
                    s->s.h.segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
715
560
                s->s.h.segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
716
            }
717
        }
718
    }
719
720
    // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
721

6206
    for (i = 0; i < (s->s.h.segmentation.enabled ? 8 : 1); i++) {
722
        int qyac, qydc, quvac, quvdc, lflvl, sh;
723
724

3929
        if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].q_enabled) {
725
424
            if (s->s.h.segmentation.absolute_vals)
726
                qyac = av_clip_uintp2(s->s.h.segmentation.feat[i].q_val, 8);
727
            else
728
424
                qyac = av_clip_uintp2(s->s.h.yac_qi + s->s.h.segmentation.feat[i].q_val, 8);
729
        } else {
730
3505
            qyac  = s->s.h.yac_qi;
731
        }
732
3929
        qydc  = av_clip_uintp2(qyac + s->s.h.ydc_qdelta, 8);
733
3929
        quvdc = av_clip_uintp2(qyac + s->s.h.uvdc_qdelta, 8);
734
3929
        quvac = av_clip_uintp2(qyac + s->s.h.uvac_qdelta, 8);
735
3929
        qyac  = av_clip_uintp2(qyac, 8);
736
737
3929
        s->s.h.segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[s->bpp_index][qydc];
738
3929
        s->s.h.segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[s->bpp_index][qyac];
739
3929
        s->s.h.segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[s->bpp_index][quvdc];
740
3929
        s->s.h.segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[s->bpp_index][quvac];
741
742
3929
        sh = s->s.h.filter.level >= 32;
743

3929
        if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].lf_enabled) {
744
17
            if (s->s.h.segmentation.absolute_vals)
745
                lflvl = av_clip_uintp2(s->s.h.segmentation.feat[i].lf_val, 6);
746
            else
747
17
                lflvl = av_clip_uintp2(s->s.h.filter.level + s->s.h.segmentation.feat[i].lf_val, 6);
748
        } else {
749
3912
            lflvl  = s->s.h.filter.level;
750
        }
751
3929
        if (s->s.h.lf_delta.enabled) {
752
3914
            s->s.h.segmentation.feat[i].lflvl[0][0] =
753
3914
            s->s.h.segmentation.feat[i].lflvl[0][1] =
754
3914
                av_clip_uintp2(lflvl + (s->s.h.lf_delta.ref[0] * (1 << sh)), 6);
755
15656
            for (j = 1; j < 4; j++) {
756
11742
                s->s.h.segmentation.feat[i].lflvl[j][0] =
757
11742
                    av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
758
11742
                                             s->s.h.lf_delta.mode[0]) * (1 << sh)), 6);
759
11742
                s->s.h.segmentation.feat[i].lflvl[j][1] =
760
11742
                    av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
761
11742
                                             s->s.h.lf_delta.mode[1]) * (1 << sh)), 6);
762
            }
763
        } else {
764
15
            memset(s->s.h.segmentation.feat[i].lflvl, lflvl,
765
                   sizeof(s->s.h.segmentation.feat[i].lflvl));
766
        }
767
    }
768
769
    /* tiling info */
770
2277
    if ((ret = update_size(avctx, w, h)) < 0) {
771
        av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n",
772
               w, h, s->pix_fmt);
773
        return ret;
774
    }
775
2277
    for (s->s.h.tiling.log2_tile_cols = 0;
776
2277
         s->sb_cols > (64 << s->s.h.tiling.log2_tile_cols);
777
         s->s.h.tiling.log2_tile_cols++) ;
778
3894
    for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
779
2277
    max = FFMAX(0, max - 1);
780
2293
    while (max > s->s.h.tiling.log2_tile_cols) {
781
201
        if (get_bits1(&s->gb))
782
16
            s->s.h.tiling.log2_tile_cols++;
783
        else
784
185
            break;
785
    }
786
2277
    s->s.h.tiling.log2_tile_rows = decode012(&s->gb);
787
2277
    s->s.h.tiling.tile_rows = 1 << s->s.h.tiling.log2_tile_rows;
788
2277
    if (s->s.h.tiling.tile_cols != (1 << s->s.h.tiling.log2_tile_cols)) {
789
        int n_range_coders;
790
        VP56RangeCoder *rc;
791
792
466
        if (s->td) {
793
            for (i = 0; i < s->active_tile_cols; i++)
794
                vp9_tile_data_free(&s->td[i]);
795
            av_free(s->td);
796
        }
797
798
466
        s->s.h.tiling.tile_cols = 1 << s->s.h.tiling.log2_tile_cols;
799
466
        vp9_free_entries(avctx);
800
932
        s->active_tile_cols = avctx->active_thread_type == FF_THREAD_SLICE ?
801
466
                              s->s.h.tiling.tile_cols : 1;
802
466
        vp9_alloc_entries(avctx, s->sb_rows);
803
466
        if (avctx->active_thread_type == FF_THREAD_SLICE) {
804
            n_range_coders = 4; // max_tile_rows
805
        } else {
806
466
            n_range_coders = s->s.h.tiling.tile_cols;
807
        }
808
466
        s->td = av_mallocz_array(s->active_tile_cols, sizeof(VP9TileData) +
809
                                 n_range_coders * sizeof(VP56RangeCoder));
810
466
        if (!s->td)
811
            return AVERROR(ENOMEM);
812
466
        rc = (VP56RangeCoder *) &s->td[s->active_tile_cols];
813
932
        for (i = 0; i < s->active_tile_cols; i++) {
814
466
            s->td[i].s = s;
815
466
            s->td[i].c_b = rc;
816
466
            rc += n_range_coders;
817
        }
818
    }
819
820
    /* check reference frames */
821

2277
    if (!s->s.h.keyframe && !s->s.h.intraonly) {
822
1805
        int valid_ref_frame = 0;
823
7220
        for (i = 0; i < 3; i++) {
824
5415
            AVFrame *ref = s->s.refs[s->s.h.refidx[i]].f;
825
5415
            int refw = ref->width, refh = ref->height;
826
827
5415
            if (ref->format != avctx->pix_fmt) {
828
                av_log(avctx, AV_LOG_ERROR,
829
                       "Ref pixfmt (%s) did not match current frame (%s)",
830
                       av_get_pix_fmt_name(ref->format),
831
                       av_get_pix_fmt_name(avctx->pix_fmt));
832
                return AVERROR_INVALIDDATA;
833

5415
            } else if (refw == w && refh == h) {
834
5407
                s->mvscale[i][0] = s->mvscale[i][1] = 0;
835
            } else {
836
                /* Check to make sure at least one of frames that */
837
                /* this frame references has valid dimensions     */
838


8
                if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
839
                    av_log(avctx, AV_LOG_WARNING,
840
                           "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
841
                           refw, refh, w, h);
842
                    s->mvscale[i][0] = s->mvscale[i][1] = REF_INVALID_SCALE;
843
                    continue;
844
                }
845
8
                s->mvscale[i][0] = (refw << 14) / w;
846
8
                s->mvscale[i][1] = (refh << 14) / h;
847
8
                s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
848
8
                s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
849
            }
850
5415
            valid_ref_frame++;
851
        }
852
1805
        if (!valid_ref_frame) {
853
            av_log(avctx, AV_LOG_ERROR, "No valid reference frame is found, bitstream not supported\n");
854
            return AVERROR_INVALIDDATA;
855
        }
856
    }
857
858


2277
    if (s->s.h.keyframe || s->s.h.errorres || (s->s.h.intraonly && s->s.h.resetctx == 3)) {
859
475
        s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
860
475
                           s->prob_ctx[3].p = ff_vp9_default_probs;
861
475
        memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
862
               sizeof(ff_vp9_default_coef_probs));
863
475
        memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
864
               sizeof(ff_vp9_default_coef_probs));
865
475
        memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
866
               sizeof(ff_vp9_default_coef_probs));
867
475
        memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
868
               sizeof(ff_vp9_default_coef_probs));
869

1802
    } else if (s->s.h.intraonly && s->s.h.resetctx == 2) {
870
6
        s->prob_ctx[c].p = ff_vp9_default_probs;
871
6
        memcpy(s->prob_ctx[c].coef, ff_vp9_default_coef_probs,
872
               sizeof(ff_vp9_default_coef_probs));
873
    }
874
875
    // next 16 bits is size of the rest of the header (arith-coded)
876
2277
    s->s.h.compressed_header_size = size2 = get_bits(&s->gb, 16);
877
2277
    s->s.h.uncompressed_header_size = (get_bits_count(&s->gb) + 7) / 8;
878
879
2277
    data2 = align_get_bits(&s->gb);
880
2277
    if (size2 > size - (data2 - data)) {
881
        av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
882
        return AVERROR_INVALIDDATA;
883
    }
884
2277
    ret = ff_vp56_init_range_decoder(&s->c, data2, size2);
885
2277
    if (ret < 0)
886
        return ret;
887
888
2277
    if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
889
        av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
890
        return AVERROR_INVALIDDATA;
891
    }
892
893
4554
    for (i = 0; i < s->active_tile_cols; i++) {
894

2277
        if (s->s.h.keyframe || s->s.h.intraonly) {
895
472
            memset(s->td[i].counts.coef, 0, sizeof(s->td[0].counts.coef));
896
472
            memset(s->td[i].counts.eob,  0, sizeof(s->td[0].counts.eob));
897
        } else {
898
1805
            memset(&s->td[i].counts, 0, sizeof(s->td[0].counts));
899
        }
900
2277
        s->td[i].nb_block_structure = 0;
901
    }
902
903
    /* FIXME is it faster to not copy here, but do it down in the fw updates
904
     * as explicit copies if the fw update is missing (and skip the copy upon
905
     * fw update)? */
906
2277
    s->prob.p = s->prob_ctx[c].p;
907
908
    // txfm updates
909
2277
    if (s->s.h.lossless) {
910
17
        s->s.h.txfmmode = TX_4X4;
911
    } else {
912
2260
        s->s.h.txfmmode = vp8_rac_get_uint(&s->c, 2);
913
2260
        if (s->s.h.txfmmode == 3)
914
1907
            s->s.h.txfmmode += vp8_rac_get(&s->c);
915
916
2260
        if (s->s.h.txfmmode == TX_SWITCHABLE) {
917
5121
            for (i = 0; i < 2; i++)
918
3414
                if (vp56_rac_get_prob_branchy(&s->c, 252))
919
215
                    s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
920
5121
            for (i = 0; i < 2; i++)
921
10242
                for (j = 0; j < 2; j++)
922
6828
                    if (vp56_rac_get_prob_branchy(&s->c, 252))
923
168
                        s->prob.p.tx16p[i][j] =
924
168
                            update_prob(&s->c, s->prob.p.tx16p[i][j]);
925
5121
            for (i = 0; i < 2; i++)
926
13656
                for (j = 0; j < 3; j++)
927
10242
                    if (vp56_rac_get_prob_branchy(&s->c, 252))
928
41
                        s->prob.p.tx32p[i][j] =
929
41
                            update_prob(&s->c, s->prob.p.tx32p[i][j]);
930
        }
931
    }
932
933
    // coef updates
934
9941
    for (i = 0; i < 4; i++) {
935
8234
        uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
936
8234
        if (vp8_rac_get(&s->c)) {
937
4797
            for (j = 0; j < 2; j++)
938
9594
                for (k = 0; k < 2; k++)
939
44772
                    for (l = 0; l < 6; l++)
940
249444
                        for (m = 0; m < 6; m++) {
941
217464
                            uint8_t *p = s->prob.coef[i][j][k][l][m];
942
217464
                            uint8_t *r = ref[j][k][l][m];
943

217464
                            if (m >= 3 && l == 0) // dc only has 3 pt
944
6396
                                break;
945
844272
                            for (n = 0; n < 3; n++) {
946
633204
                                if (vp56_rac_get_prob_branchy(&s->c, 252))
947
22729
                                    p[n] = update_prob(&s->c, r[n]);
948
                                else
949
610475
                                    p[n] = r[n];
950
                            }
951
211068
                            memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
952
                        }
953
        } else {
954
19905
            for (j = 0; j < 2; j++)
955
39810
                for (k = 0; k < 2; k++)
956
185780
                    for (l = 0; l < 6; l++)
957
1061600
                        for (m = 0; m < 6; m++) {
958
928900
                            uint8_t *p = s->prob.coef[i][j][k][l][m];
959
928900
                            uint8_t *r = ref[j][k][l][m];
960

928900
                            if (m > 3 && l == 0) // dc only has 3 pt
961
26540
                                break;
962
902360
                            memcpy(p, r, 3);
963
902360
                            memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
964
                        }
965
        }
966
8234
        if (s->s.h.txfmmode == i)
967
570
            break;
968
    }
969
970
    // mode updates
971
9108
    for (i = 0; i < 3; i++)
972
6831
        if (vp56_rac_get_prob_branchy(&s->c, 252))
973
731
            s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
974

2277
    if (!s->s.h.keyframe && !s->s.h.intraonly) {
975
14440
        for (i = 0; i < 7; i++)
976
50540
            for (j = 0; j < 3; j++)
977
37905
                if (vp56_rac_get_prob_branchy(&s->c, 252))
978
311
                    s->prob.p.mv_mode[i][j] =
979
311
                        update_prob(&s->c, s->prob.p.mv_mode[i][j]);
980
981
1805
        if (s->s.h.filtermode == FILTER_SWITCHABLE)
982
6560
            for (i = 0; i < 4; i++)
983
15744
                for (j = 0; j < 2; j++)
984
10496
                    if (vp56_rac_get_prob_branchy(&s->c, 252))
985
184
                        s->prob.p.filter[i][j] =
986
184
                            update_prob(&s->c, s->prob.p.filter[i][j]);
987
988
9025
        for (i = 0; i < 4; i++)
989
7220
            if (vp56_rac_get_prob_branchy(&s->c, 252))
990
99
                s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
991
992
1805
        if (s->s.h.allowcompinter) {
993
450
            s->s.h.comppredmode = vp8_rac_get(&s->c);
994
450
            if (s->s.h.comppredmode)
995
305
                s->s.h.comppredmode += vp8_rac_get(&s->c);
996
450
            if (s->s.h.comppredmode == PRED_SWITCHABLE)
997
1818
                for (i = 0; i < 5; i++)
998
1515
                    if (vp56_rac_get_prob_branchy(&s->c, 252))
999
43
                        s->prob.p.comp[i] =
1000
43
                            update_prob(&s->c, s->prob.p.comp[i]);
1001
        } else {
1002
1355
            s->s.h.comppredmode = PRED_SINGLEREF;
1003
        }
1004
1005
1805
        if (s->s.h.comppredmode != PRED_COMPREF) {
1006
10818
            for (i = 0; i < 5; i++) {
1007
9015
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1008
319
                    s->prob.p.single_ref[i][0] =
1009
319
                        update_prob(&s->c, s->prob.p.single_ref[i][0]);
1010
9015
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1011
53
                    s->prob.p.single_ref[i][1] =
1012
53
                        update_prob(&s->c, s->prob.p.single_ref[i][1]);
1013
            }
1014
        }
1015
1016
1805
        if (s->s.h.comppredmode != PRED_SINGLEREF) {
1017
1830
            for (i = 0; i < 5; i++)
1018
1525
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1019
10
                    s->prob.p.comp_ref[i] =
1020
10
                        update_prob(&s->c, s->prob.p.comp_ref[i]);
1021
        }
1022
1023
9025
        for (i = 0; i < 4; i++)
1024
72200
            for (j = 0; j < 9; j++)
1025
64980
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1026
127
                    s->prob.p.y_mode[i][j] =
1027
127
                        update_prob(&s->c, s->prob.p.y_mode[i][j]);
1028
1029
9025
        for (i = 0; i < 4; i++)
1030
36100
            for (j = 0; j < 4; j++)
1031
115520
                for (k = 0; k < 3; k++)
1032
86640
                    if (vp56_rac_get_prob_branchy(&s->c, 252))
1033
254
                        s->prob.p.partition[3 - i][j][k] =
1034
254
                            update_prob(&s->c,
1035
254
                                        s->prob.p.partition[3 - i][j][k]);
1036
1037
        // mv fields don't use the update_prob subexp model for some reason
1038
7220
        for (i = 0; i < 3; i++)
1039
5415
            if (vp56_rac_get_prob_branchy(&s->c, 252))
1040
258
                s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1041
1042
5415
        for (i = 0; i < 2; i++) {
1043
3610
            if (vp56_rac_get_prob_branchy(&s->c, 252))
1044
153
                s->prob.p.mv_comp[i].sign =
1045
153
                    (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1046
1047
39710
            for (j = 0; j < 10; j++)
1048
36100
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1049
85
                    s->prob.p.mv_comp[i].classes[j] =
1050
85
                        (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1051
1052
3610
            if (vp56_rac_get_prob_branchy(&s->c, 252))
1053
86
                s->prob.p.mv_comp[i].class0 =
1054
86
                    (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1055
1056
39710
            for (j = 0; j < 10; j++)
1057
36100
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1058
15
                    s->prob.p.mv_comp[i].bits[j] =
1059
15
                        (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1060
        }
1061
1062
5415
        for (i = 0; i < 2; i++) {
1063
10830
            for (j = 0; j < 2; j++)
1064
28880
                for (k = 0; k < 3; k++)
1065
21660
                    if (vp56_rac_get_prob_branchy(&s->c, 252))
1066
209
                        s->prob.p.mv_comp[i].class0_fp[j][k] =
1067
209
                            (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1068
1069
14440
            for (j = 0; j < 3; j++)
1070
10830
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1071
4
                    s->prob.p.mv_comp[i].fp[j] =
1072
4
                        (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1073
        }
1074
1075
1805
        if (s->s.h.highprecisionmvs) {
1076
4221
            for (i = 0; i < 2; i++) {
1077
2814
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1078
86
                    s->prob.p.mv_comp[i].class0_hp =
1079
86
                        (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1080
1081
2814
                if (vp56_rac_get_prob_branchy(&s->c, 252))
1082
30
                    s->prob.p.mv_comp[i].hp =
1083
30
                        (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1084
            }
1085
        }
1086
    }
1087
1088
2277
    return (data2 - data) + size2;
1089
}
1090
1091
992137
static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1092
                      ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1093
{
1094
992137
    const VP9Context *s = td->s;
1095
992137
    int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
1096
992137
            (((td->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
1097

992137
    const uint8_t *p = s->s.h.keyframe || s->s.h.intraonly ? ff_vp9_default_kf_partition_probs[bl][c] :
1098
                                                     s->prob.p.partition[bl][c];
1099
    enum BlockPartition bp;
1100
992137
    ptrdiff_t hbs = 4 >> bl;
1101
992137
    AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1102
992137
    ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1103
992137
    int bytesperpixel = s->bytesperpixel;
1104
1105
992137
    if (bl == BL_8X8) {
1106
492222
        bp = vp8_rac_get_tree(td->c, ff_vp9_partition_tree, p);
1107
492222
        ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1108
499915
    } else if (col + hbs < s->cols) { // FIXME why not <=?
1109
477666
        if (row + hbs < s->rows) { // FIXME why not <=?
1110
463824
            bp = vp8_rac_get_tree(td->c, ff_vp9_partition_tree, p);
1111

463824
            switch (bp) {
1112
191581
            case PARTITION_NONE:
1113
191581
                ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1114
191581
                break;
1115
28349
            case PARTITION_H:
1116
28349
                ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1117
28349
                yoff  += hbs * 8 * y_stride;
1118
28349
                uvoff += hbs * 8 * uv_stride >> s->ss_v;
1119
28349
                ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
1120
28349
                break;
1121
22569
            case PARTITION_V:
1122
22569
                ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1123
22569
                yoff  += hbs * 8 * bytesperpixel;
1124
22569
                uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1125
22569
                ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
1126
22569
                break;
1127
221325
            case PARTITION_SPLIT:
1128
221325
                decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1129
221325
                decode_sb(td, row, col + hbs, lflvl,
1130
221325
                          yoff + 8 * hbs * bytesperpixel,
1131
221325
                          uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1132
221325
                yoff  += hbs * 8 * y_stride;
1133
221325
                uvoff += hbs * 8 * uv_stride >> s->ss_v;
1134
221325
                decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1135
221325
                decode_sb(td, row + hbs, col + hbs, lflvl,
1136
221325
                          yoff + 8 * hbs * bytesperpixel,
1137
221325
                          uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1138
221325
                break;
1139
            default:
1140
                av_assert0(0);
1141
            }
1142
13842
        } else if (vp56_rac_get_prob_branchy(td->c, p[1])) {
1143
9959
            bp = PARTITION_SPLIT;
1144
9959
            decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1145
9959
            decode_sb(td, row, col + hbs, lflvl,
1146
9959
                      yoff + 8 * hbs * bytesperpixel,
1147
9959
                      uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1148
        } else {
1149
3883
            bp = PARTITION_H;
1150
3883
            ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1151
        }
1152
22249
    } else if (row + hbs < s->rows) { // FIXME why not <=?
1153
19396
        if (vp56_rac_get_prob_branchy(td->c, p[2])) {
1154
16228
            bp = PARTITION_SPLIT;
1155
16228
            decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1156
16228
            yoff  += hbs * 8 * y_stride;
1157
16228
            uvoff += hbs * 8 * uv_stride >> s->ss_v;
1158
16228
            decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1159
        } else {
1160
3168
            bp = PARTITION_V;
1161
3168
            ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1162
        }
1163
    } else {
1164
2853
        bp = PARTITION_SPLIT;
1165
2853
        decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1166
    }
1167
992137
    td->counts.partition[bl][c][bp]++;
1168
992137
}
1169
1170
static void decode_sb_mem(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1171
                          ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1172
{
1173
    const VP9Context *s = td->s;
1174
    VP9Block *b = td->b;
1175
    ptrdiff_t hbs = 4 >> bl;
1176
    AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1177
    ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1178
    int bytesperpixel = s->bytesperpixel;
1179
1180
    if (bl == BL_8X8) {
1181
        av_assert2(b->bl == BL_8X8);
1182
        ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1183
    } else if (td->b->bl == bl) {
1184
        ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1185
        if (b->bp == PARTITION_H && row + hbs < s->rows) {
1186
            yoff  += hbs * 8 * y_stride;
1187
            uvoff += hbs * 8 * uv_stride >> s->ss_v;
1188
            ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
1189
        } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
1190
            yoff  += hbs * 8 * bytesperpixel;
1191
            uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1192
            ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
1193
        }
1194
    } else {
1195
        decode_sb_mem(td, row, col, lflvl, yoff, uvoff, bl + 1);
1196
        if (col + hbs < s->cols) { // FIXME why not <=?
1197
            if (row + hbs < s->rows) {
1198
                decode_sb_mem(td, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
1199
                              uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1200
                yoff  += hbs * 8 * y_stride;
1201
                uvoff += hbs * 8 * uv_stride >> s->ss_v;
1202
                decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1203
                decode_sb_mem(td, row + hbs, col + hbs, lflvl,
1204
                              yoff + 8 * hbs * bytesperpixel,
1205
                              uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1206
            } else {
1207
                yoff  += hbs * 8 * bytesperpixel;
1208
                uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1209
                decode_sb_mem(td, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
1210
            }
1211
        } else if (row + hbs < s->rows) {
1212
            yoff  += hbs * 8 * y_stride;
1213
            uvoff += hbs * 8 * uv_stride >> s->ss_v;
1214
            decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1215
        }
1216
    }
1217
}
1218
1219
10609
static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
1220
{
1221
10609
    int sb_start = ( idx      * n) >> log2_n;
1222
10609
    int sb_end   = ((idx + 1) * n) >> log2_n;
1223
10609
    *start = FFMIN(sb_start, n) << 3;
1224
10609
    *end   = FFMIN(sb_end,   n) << 3;
1225
10609
}
1226
1227
468
static void free_buffers(VP9Context *s)
1228
{
1229
    int i;
1230
1231
468
    av_freep(&s->intra_pred_data[0]);
1232
934
    for (i = 0; i < s->active_tile_cols; i++)
1233
466
        vp9_tile_data_free(&s->td[i]);
1234
468
}
1235
1236
468
static av_cold int vp9_decode_free(AVCodecContext *avctx)
1237
{
1238
468
    VP9Context *s = avctx->priv_data;
1239
    int i;
1240
1241
1872
    for (i = 0; i < 3; i++) {
1242
1404
        vp9_frame_unref(avctx, &s->s.frames[i]);
1243
1404
        av_frame_free(&s->s.frames[i].tf.f);
1244
    }
1245
468
    av_buffer_pool_uninit(&s->frame_extradata_pool);
1246
4212
    for (i = 0; i < 8; i++) {
1247
3744
        ff_thread_release_buffer(avctx, &s->s.refs[i]);
1248
3744
        av_frame_free(&s->s.refs[i].f);
1249
3744
        ff_thread_release_buffer(avctx, &s->next_refs[i]);
1250
3744
        av_frame_free(&s->next_refs[i].f);
1251
    }
1252
1253
468
    free_buffers(s);
1254
468
    vp9_free_entries(avctx);
1255
468
    av_freep(&s->td);
1256
468
    return 0;
1257
}
1258
1259
2277
static int decode_tiles(AVCodecContext *avctx,
1260
                        const uint8_t *data, int size)
1261
{
1262
2277
    VP9Context *s = avctx->priv_data;
1263
2277
    VP9TileData *td = &s->td[0];
1264
    int row, col, tile_row, tile_col, ret;
1265
    int bytesperpixel;
1266
    int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1267
    AVFrame *f;
1268
    ptrdiff_t yoff, uvoff, ls_y, ls_uv;
1269
1270
2277
    f = s->s.frames[CUR_FRAME].tf.f;
1271
2277
    ls_y = f->linesize[0];
1272
2277
    ls_uv =f->linesize[1];
1273
2277
    bytesperpixel = s->bytesperpixel;
1274
1275
2277
    yoff = uvoff = 0;
1276
4558
    for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1277
2281
        set_tile_offset(&tile_row_start, &tile_row_end,
1278
2281
                        tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1279
1280
4588
        for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1281
            int64_t tile_size;
1282
1283
2307
            if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1284
2281
                tile_row == s->s.h.tiling.tile_rows - 1) {
1285
2277
                tile_size = size;
1286
            } else {
1287
30
                tile_size = AV_RB32(data);
1288
30
                data += 4;
1289
30
                size -= 4;
1290
            }
1291
2307
            if (tile_size > size) {
1292
                ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1293
                return AVERROR_INVALIDDATA;
1294
            }
1295
2307
            ret = ff_vp56_init_range_decoder(&td->c_b[tile_col], data, tile_size);
1296
2307
            if (ret < 0)
1297
                return ret;
1298
2307
            if (vp56_rac_get_prob_branchy(&td->c_b[tile_col], 128)) { // marker bit
1299
                ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1300
                return AVERROR_INVALIDDATA;
1301
            }
1302
2307
            data += tile_size;
1303
2307
            size -= tile_size;
1304
        }
1305
1306
10325
        for (row = tile_row_start; row < tile_row_end;
1307
8044
             row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1308
8044
            VP9Filter *lflvl_ptr = s->lflvl;
1309
8044
            ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1310
1311
16372
            for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1312
8328
                set_tile_offset(&tile_col_start, &tile_col_end,
1313
8328
                                tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1314
8328
                td->tile_col_start = tile_col_start;
1315
8328
                if (s->pass != 2) {
1316
8328
                    memset(td->left_partition_ctx, 0, 8);
1317
8328
                    memset(td->left_skip_ctx, 0, 8);
1318

8328
                    if (s->s.h.keyframe || s->s.h.intraonly) {
1319
1832
                        memset(td->left_mode_ctx, DC_PRED, 16);
1320
                    } else {
1321
6496
                        memset(td->left_mode_ctx, NEARESTMV, 8);
1322
                    }
1323
8328
                    memset(td->left_y_nnz_ctx, 0, 16);
1324
8328
                    memset(td->left_uv_nnz_ctx, 0, 32);
1325
8328
                    memset(td->left_segpred_ctx, 0, 8);
1326
1327
8328
                    td->c = &td->c_b[tile_col];
1328
                }
1329
1330
8328
                for (col = tile_col_start;
1331
59938
                     col < tile_col_end;
1332
51610
                     col += 8, yoff2 += 64 * bytesperpixel,
1333
51610
                     uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1334
                    // FIXME integrate with lf code (i.e. zero after each
1335
                    // use, similar to invtxfm coefficients, or similar)
1336
51610
                    if (s->pass != 1) {
1337
51610
                        memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1338
                    }
1339
1340
51610
                    if (s->pass == 2) {
1341
                        decode_sb_mem(td, row, col, lflvl_ptr,
1342
                                      yoff2, uvoff2, BL_64X64);
1343
                    } else {
1344
51610
                        if (vpX_rac_is_end(td->c)) {
1345
                            return AVERROR_INVALIDDATA;
1346
                        }
1347
51610
                        decode_sb(td, row, col, lflvl_ptr,
1348
                                  yoff2, uvoff2, BL_64X64);
1349
                    }
1350
                }
1351
            }
1352
1353
8044
            if (s->pass == 1)
1354
                continue;
1355
1356
            // backup pre-loopfilter reconstruction data for intra
1357
            // prediction of next row of sb64s
1358
8044
            if (row + 8 < s->rows) {
1359
5767
                memcpy(s->intra_pred_data[0],
1360
5767
                       f->data[0] + yoff + 63 * ls_y,
1361
5767
                       8 * s->cols * bytesperpixel);
1362
5767
                memcpy(s->intra_pred_data[1],
1363
5767
                       f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1364
5767
                       8 * s->cols * bytesperpixel >> s->ss_h);
1365
5767
                memcpy(s->intra_pred_data[2],
1366
5767
                       f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1367
5767
                       8 * s->cols * bytesperpixel >> s->ss_h);
1368
            }
1369
1370
            // loopfilter one row
1371
8044
            if (s->s.h.filter.level) {
1372
7498
                yoff2 = yoff;
1373
7498
                uvoff2 = uvoff;
1374
7498
                lflvl_ptr = s->lflvl;
1375
55668
                for (col = 0; col < s->cols;
1376
48170
                     col += 8, yoff2 += 64 * bytesperpixel,
1377
48170
                     uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1378
48170
                    ff_vp9_loopfilter_sb(avctx, lflvl_ptr, row, col,
1379
                                         yoff2, uvoff2);
1380
                }
1381
            }
1382
1383
            // FIXME maybe we can make this more finegrained by running the
1384
            // loopfilter per-block instead of after each sbrow
1385
            // In fact that would also make intra pred left preparation easier?
1386
8044
            ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, row >> 3, 0);
1387
        }
1388
    }
1389
2277
    return 0;
1390
}
1391
1392
#if HAVE_THREADS
1393
static av_always_inline
1394
int decode_tiles_mt(AVCodecContext *avctx, void *tdata, int jobnr,
1395
                              int threadnr)
1396
{
1397
    VP9Context *s = avctx->priv_data;
1398
    VP9TileData *td = &s->td[jobnr];
1399
    ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1400
    int bytesperpixel = s->bytesperpixel, row, col, tile_row;
1401
    unsigned tile_cols_len;
1402
    int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1403
    VP9Filter *lflvl_ptr_base;
1404
    AVFrame *f;
1405
1406
    f = s->s.frames[CUR_FRAME].tf.f;
1407
    ls_y = f->linesize[0];
1408
    ls_uv =f->linesize[1];
1409
1410
    set_tile_offset(&tile_col_start, &tile_col_end,
1411
                    jobnr, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1412
    td->tile_col_start  = tile_col_start;
1413
    uvoff = (64 * bytesperpixel >> s->ss_h)*(tile_col_start >> 3);
1414
    yoff = (64 * bytesperpixel)*(tile_col_start >> 3);
1415
    lflvl_ptr_base = s->lflvl+(tile_col_start >> 3);
1416
1417
    for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1418
        set_tile_offset(&tile_row_start, &tile_row_end,
1419
                        tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1420
1421
        td->c = &td->c_b[tile_row];
1422
        for (row = tile_row_start; row < tile_row_end;
1423
             row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1424
            ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1425
            VP9Filter *lflvl_ptr = lflvl_ptr_base+s->sb_cols*(row >> 3);
1426
1427
            memset(td->left_partition_ctx, 0, 8);
1428
            memset(td->left_skip_ctx, 0, 8);
1429
            if (s->s.h.keyframe || s->s.h.intraonly) {
1430
                memset(td->left_mode_ctx, DC_PRED, 16);
1431
            } else {
1432
                memset(td->left_mode_ctx, NEARESTMV, 8);
1433
            }
1434
            memset(td->left_y_nnz_ctx, 0, 16);
1435
            memset(td->left_uv_nnz_ctx, 0, 32);
1436
            memset(td->left_segpred_ctx, 0, 8);
1437
1438
            for (col = tile_col_start;
1439
                 col < tile_col_end;
1440
                 col += 8, yoff2 += 64 * bytesperpixel,
1441
                 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1442
                // FIXME integrate with lf code (i.e. zero after each
1443
                // use, similar to invtxfm coefficients, or similar)
1444
                memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1445
                decode_sb(td, row, col, lflvl_ptr,
1446
                            yoff2, uvoff2, BL_64X64);
1447
            }
1448
1449
            // backup pre-loopfilter reconstruction data for intra
1450
            // prediction of next row of sb64s
1451
            tile_cols_len = tile_col_end - tile_col_start;
1452
            if (row + 8 < s->rows) {
1453
                memcpy(s->intra_pred_data[0] + (tile_col_start * 8 * bytesperpixel),
1454
                       f->data[0] + yoff + 63 * ls_y,
1455
                       8 * tile_cols_len * bytesperpixel);
1456
                memcpy(s->intra_pred_data[1] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1457
                       f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1458
                       8 * tile_cols_len * bytesperpixel >> s->ss_h);
1459
                memcpy(s->intra_pred_data[2] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1460
                       f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1461
                       8 * tile_cols_len * bytesperpixel >> s->ss_h);
1462
            }
1463
1464
            vp9_report_tile_progress(s, row >> 3, 1);
1465
        }
1466
    }
1467
    return 0;
1468
}
1469
1470
static av_always_inline
1471
int loopfilter_proc(AVCodecContext *avctx)
1472
{
1473
    VP9Context *s = avctx->priv_data;
1474
    ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1475
    VP9Filter *lflvl_ptr;
1476
    int bytesperpixel = s->bytesperpixel, col, i;
1477
    AVFrame *f;
1478
1479
    f = s->s.frames[CUR_FRAME].tf.f;
1480
    ls_y = f->linesize[0];
1481
    ls_uv =f->linesize[1];
1482
1483
    for (i = 0; i < s->sb_rows; i++) {
1484
        vp9_await_tile_progress(s, i, s->s.h.tiling.tile_cols);
1485
1486
        if (s->s.h.filter.level) {
1487
            yoff = (ls_y * 64)*i;
1488
            uvoff =  (ls_uv * 64 >> s->ss_v)*i;
1489
            lflvl_ptr = s->lflvl+s->sb_cols*i;
1490
            for (col = 0; col < s->cols;
1491
                 col += 8, yoff += 64 * bytesperpixel,
1492
                 uvoff += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1493
                ff_vp9_loopfilter_sb(avctx, lflvl_ptr, i << 3, col,
1494
                                     yoff, uvoff);
1495
            }
1496
        }
1497
    }
1498
    return 0;
1499
}
1500
#endif
1501
1502
6
static int vp9_export_enc_params(VP9Context *s, VP9Frame *frame)
1503
{
1504
    AVVideoEncParams *par;
1505
6
    unsigned int tile, nb_blocks = 0;
1506
1507
6
    if (s->s.h.segmentation.enabled) {
1508
12
        for (tile = 0; tile < s->active_tile_cols; tile++)
1509
6
            nb_blocks += s->td[tile].nb_block_structure;
1510
    }
1511
1512
6
    par = av_video_enc_params_create_side_data(frame->tf.f,
1513
        AV_VIDEO_ENC_PARAMS_VP9, nb_blocks);
1514
6
    if (!par)
1515
        return AVERROR(ENOMEM);
1516
1517
6
    par->qp             = s->s.h.yac_qi;
1518
6
    par->delta_qp[0][0] = s->s.h.ydc_qdelta;
1519
6
    par->delta_qp[1][0] = s->s.h.uvdc_qdelta;
1520
6
    par->delta_qp[2][0] = s->s.h.uvdc_qdelta;
1521
6
    par->delta_qp[1][1] = s->s.h.uvac_qdelta;
1522
6
    par->delta_qp[2][1] = s->s.h.uvac_qdelta;
1523
1524
6
    if (nb_blocks) {
1525
6
        unsigned int block = 0;
1526
        unsigned int tile, block_tile;
1527
1528
12
        for (tile = 0; tile < s->active_tile_cols; tile++) {
1529
6
            VP9TileData *td = &s->td[tile];
1530
1531
1210
            for (block_tile = 0; block_tile < td->nb_block_structure; block_tile++) {
1532
1204
                AVVideoBlockParams *b = av_video_enc_params_block(par, block++);
1533
1204
                unsigned int      row = td->block_structure[block_tile].row;
1534
1204
                unsigned int      col = td->block_structure[block_tile].col;
1535
1204
                uint8_t        seg_id = frame->segmentation_map[row * 8 * s->sb_cols + col];
1536
1537
1204
                b->src_x = col * 8;
1538
1204
                b->src_y = row * 8;
1539
1204
                b->w     = 1 << (3 + td->block_structure[block_tile].block_size_idx_x);
1540
1204
                b->h     = 1 << (3 + td->block_structure[block_tile].block_size_idx_y);
1541
1542
1204
                if (s->s.h.segmentation.feat[seg_id].q_enabled) {
1543
977
                    b->delta_qp = s->s.h.segmentation.feat[seg_id].q_val;
1544
977
                    if (s->s.h.segmentation.absolute_vals)
1545
                        b->delta_qp -= par->qp;
1546
                }
1547
            }
1548
        }
1549
    }
1550
1551
6
    return 0;
1552
}
1553
1554
2296
static int vp9_decode_frame(AVCodecContext *avctx, void *frame,
1555
                            int *got_frame, AVPacket *pkt)
1556
{
1557
2296
    const uint8_t *data = pkt->data;
1558
2296
    int size = pkt->size;
1559
2296
    VP9Context *s = avctx->priv_data;
1560
    int ret, i, j, ref;
1561
3883
    int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
1562

1587
                            (!s->s.h.segmentation.enabled || !s->s.h.segmentation.update_map);
1563
    AVFrame *f;
1564
1565
2296
    if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
1566
        return ret;
1567
2296
    } else if (ret == 0) {
1568
19
        if (!s->s.refs[ref].f->buf[0]) {
1569
            av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
1570
            return AVERROR_INVALIDDATA;
1571
        }
1572
19
        if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
1573
            return ret;
1574
19
        ((AVFrame *)frame)->pts = pkt->pts;
1575
#if FF_API_PKT_PTS
1576
FF_DISABLE_DEPRECATION_WARNINGS
1577
19
        ((AVFrame *)frame)->pkt_pts = pkt->pts;
1578
FF_ENABLE_DEPRECATION_WARNINGS
1579
#endif
1580
19
        ((AVFrame *)frame)->pkt_dts = pkt->dts;
1581
171
        for (i = 0; i < 8; i++) {
1582
152
            if (s->next_refs[i].f->buf[0])
1583
143
                ff_thread_release_buffer(avctx, &s->next_refs[i]);
1584

295
            if (s->s.refs[i].f->buf[0] &&
1585
143
                (ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i])) < 0)
1586
                return ret;
1587
        }
1588
19
        *got_frame = 1;
1589
19
        return pkt->size;
1590
    }
1591
2277
    data += ret;
1592
2277
    size -= ret;
1593
1594

2277
    if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly) {
1595
758
        if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0])
1596
49
            vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
1597


1035
        if (!s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
1598
277
            (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_SEGMAP], &s->s.frames[CUR_FRAME])) < 0)
1599
            return ret;
1600
    }
1601
2277
    if (s->s.frames[REF_FRAME_MVPAIR].tf.f->buf[0])
1602
1570
        vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_MVPAIR]);
1603


4073
    if (!s->s.h.intraonly && !s->s.h.keyframe && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
1604
1796
        (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_MVPAIR], &s->s.frames[CUR_FRAME])) < 0)
1605
        return ret;
1606
2277
    if (s->s.frames[CUR_FRAME].tf.f->buf[0])
1607
1811
        vp9_frame_unref(avctx, &s->s.frames[CUR_FRAME]);
1608
2277
    if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0)
1609
        return ret;
1610
2277
    f = s->s.frames[CUR_FRAME].tf.f;
1611
2277
    f->key_frame = s->s.h.keyframe;
1612

2277
    f->pict_type = (s->s.h.keyframe || s->s.h.intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1613
1614
2277
    if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0] &&
1615
1796
        (s->s.frames[REF_FRAME_MVPAIR].tf.f->width  != s->s.frames[CUR_FRAME].tf.f->width ||
1616
1794
         s->s.frames[REF_FRAME_MVPAIR].tf.f->height != s->s.frames[CUR_FRAME].tf.f->height)) {
1617
2
        vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
1618
    }
1619
1620
    // ref frame setup
1621
20493
    for (i = 0; i < 8; i++) {
1622
18216
        if (s->next_refs[i].f->buf[0])
1623
14442
            ff_thread_release_buffer(avctx, &s->next_refs[i]);
1624
18216
        if (s->s.h.refreshrefmask & (1 << i)) {
1625
5566
            ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.frames[CUR_FRAME].tf);
1626
12650
        } else if (s->s.refs[i].f->buf[0]) {
1627
12597
            ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i]);
1628
        }
1629
18216
        if (ret < 0)
1630
            return ret;
1631
    }
1632
1633
2277
    if (avctx->hwaccel) {
1634
        ret = avctx->hwaccel->start_frame(avctx, NULL, 0);
1635
        if (ret < 0)
1636
            return ret;
1637
        ret = avctx->hwaccel->decode_slice(avctx, pkt->data, pkt->size);
1638
        if (ret < 0)
1639
            return ret;
1640
        ret = avctx->hwaccel->end_frame(avctx);
1641
        if (ret < 0)
1642
            return ret;
1643
        goto finish;
1644
    }
1645
1646
    // main tile decode loop
1647
2277
    memset(s->above_partition_ctx, 0, s->cols);
1648
2277
    memset(s->above_skip_ctx, 0, s->cols);
1649

2277
    if (s->s.h.keyframe || s->s.h.intraonly) {
1650
472
        memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
1651
    } else {
1652
1805
        memset(s->above_mode_ctx, NEARESTMV, s->cols);
1653
    }
1654
2277
    memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
1655
2277
    memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
1656
2277
    memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
1657
2277
    memset(s->above_segpred_ctx, 0, s->cols);
1658
2277
    s->pass = s->s.frames[CUR_FRAME].uses_2pass =
1659

2277
        avctx->active_thread_type == FF_THREAD_FRAME && s->s.h.refreshctx && !s->s.h.parallelmode;
1660
2277
    if ((ret = update_block_buffers(avctx)) < 0) {
1661
        av_log(avctx, AV_LOG_ERROR,
1662
               "Failed to allocate block buffers\n");
1663
        return ret;
1664
    }
1665

2310
    if (s->s.h.refreshctx && s->s.h.parallelmode) {
1666
        int j, k, l, m;
1667
1668
97
        for (i = 0; i < 4; i++) {
1669
270
            for (j = 0; j < 2; j++)
1670
540
                for (k = 0; k < 2; k++)
1671
2520
                    for (l = 0; l < 6; l++)
1672
15120
                        for (m = 0; m < 6; m++)
1673
12960
                            memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
1674
12960
                                   s->prob.coef[i][j][k][l][m], 3);
1675
90
            if (s->s.h.txfmmode == i)
1676
26
                break;
1677
        }
1678
33
        s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
1679
33
        ff_thread_finish_setup(avctx);
1680
2244
    } else if (!s->s.h.refreshctx) {
1681
11
        ff_thread_finish_setup(avctx);
1682
    }
1683
1684
#if HAVE_THREADS
1685
2277
    if (avctx->active_thread_type & FF_THREAD_SLICE) {
1686
        for (i = 0; i < s->sb_rows; i++)
1687
            atomic_store(&s->entries[i], 0);
1688
    }
1689
#endif
1690
1691
    do {
1692
4554
        for (i = 0; i < s->active_tile_cols; i++) {
1693
2277
            s->td[i].b = s->td[i].b_base;
1694
2277
            s->td[i].block = s->td[i].block_base;
1695
2277
            s->td[i].uvblock[0] = s->td[i].uvblock_base[0];
1696
2277
            s->td[i].uvblock[1] = s->td[i].uvblock_base[1];
1697
2277
            s->td[i].eob = s->td[i].eob_base;
1698
2277
            s->td[i].uveob[0] = s->td[i].uveob_base[0];
1699
2277
            s->td[i].uveob[1] = s->td[i].uveob_base[1];
1700
2277
            s->td[i].error_info = 0;
1701
        }
1702
1703
#if HAVE_THREADS
1704
2277
        if (avctx->active_thread_type == FF_THREAD_SLICE) {
1705
            int tile_row, tile_col;
1706
1707
            av_assert1(!s->pass);
1708
1709
            for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1710
                for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1711
                    int64_t tile_size;
1712
1713
                    if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1714
                        tile_row == s->s.h.tiling.tile_rows - 1) {
1715
                        tile_size = size;
1716
                    } else {
1717
                        tile_size = AV_RB32(data);
1718
                        data += 4;
1719
                        size -= 4;
1720
                    }
1721
                    if (tile_size > size)
1722
                        return AVERROR_INVALIDDATA;
1723
                    ret = ff_vp56_init_range_decoder(&s->td[tile_col].c_b[tile_row], data, tile_size);
1724
                    if (ret < 0)
1725
                        return ret;
1726
                    if (vp56_rac_get_prob_branchy(&s->td[tile_col].c_b[tile_row], 128)) // marker bit
1727
                        return AVERROR_INVALIDDATA;
1728
                    data += tile_size;
1729
                    size -= tile_size;
1730
                }
1731
            }
1732
1733
            ff_slice_thread_execute_with_mainfunc(avctx, decode_tiles_mt, loopfilter_proc, s->td, NULL, s->s.h.tiling.tile_cols);
1734
        } else
1735
#endif
1736
        {
1737
2277
            ret = decode_tiles(avctx, data, size);
1738
2277
            if (ret < 0) {
1739
                ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1740
                return ret;
1741
            }
1742
        }
1743
1744
        // Sum all counts fields into td[0].counts for tile threading
1745
2277
        if (avctx->active_thread_type == FF_THREAD_SLICE)
1746
            for (i = 1; i < s->s.h.tiling.tile_cols; i++)
1747
                for (j = 0; j < sizeof(s->td[i].counts) / sizeof(unsigned); j++)
1748
                    ((unsigned *)&s->td[0].counts)[j] += ((unsigned *)&s->td[i].counts)[j];
1749
1750

2277
        if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
1751
2233
            ff_vp9_adapt_probs(s);
1752
2233
            ff_thread_finish_setup(avctx);
1753
        }
1754
2277
    } while (s->pass++ == 1);
1755
2277
    ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1756
1757
2277
    if (s->td->error_info < 0) {
1758
        av_log(avctx, AV_LOG_ERROR, "Failed to decode tile data\n");
1759
        s->td->error_info = 0;
1760
        return AVERROR_INVALIDDATA;
1761
    }
1762
2277
    if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
1763
6
        ret = vp9_export_enc_params(s, &s->s.frames[CUR_FRAME]);
1764
6
        if (ret < 0)
1765
            return ret;
1766
    }
1767
1768
2277
finish:
1769
    // ref frame setup
1770
20493
    for (i = 0; i < 8; i++) {
1771
18216
        if (s->s.refs[i].f->buf[0])
1772
14442
            ff_thread_release_buffer(avctx, &s->s.refs[i]);
1773

36379
        if (s->next_refs[i].f->buf[0] &&
1774
18163
            (ret = ff_thread_ref_frame(&s->s.refs[i], &s->next_refs[i])) < 0)
1775
            return ret;
1776
    }
1777
1778
2277
    if (!s->s.h.invisible) {
1779
2213
        if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
1780
            return ret;
1781
2213
        *got_frame = 1;
1782
    }
1783
1784
2277
    return pkt->size;
1785
}
1786
1787
static void vp9_decode_flush(AVCodecContext *avctx)
1788
{
1789
    VP9Context *s = avctx->priv_data;
1790
    int i;
1791
1792
    for (i = 0; i < 3; i++)
1793
        vp9_frame_unref(avctx, &s->s.frames[i]);
1794
    for (i = 0; i < 8; i++)
1795
        ff_thread_release_buffer(avctx, &s->s.refs[i]);
1796
}
1797
1798
468
static int init_frames(AVCodecContext *avctx)
1799
{
1800
468
    VP9Context *s = avctx->priv_data;
1801
    int i;
1802
1803
1872
    for (i = 0; i < 3; i++) {
1804
1404
        s->s.frames[i].tf.f = av_frame_alloc();
1805
1404
        if (!s->s.frames[i].tf.f) {
1806
            vp9_decode_free(avctx);
1807
            av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
1808
            return AVERROR(ENOMEM);
1809
        }
1810
    }
1811
4212
    for (i = 0; i < 8; i++) {
1812
3744
        s->s.refs[i].f = av_frame_alloc();
1813
3744
        s->next_refs[i].f = av_frame_alloc();
1814

3744
        if (!s->s.refs[i].f || !s->next_refs[i].f) {
1815
            vp9_decode_free(avctx);
1816
            av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
1817
            return AVERROR(ENOMEM);
1818
        }
1819
    }
1820
1821
468
    return 0;
1822
}
1823
1824
468
static av_cold int vp9_decode_init(AVCodecContext *avctx)
1825
{
1826
468
    VP9Context *s = avctx->priv_data;
1827
1828
468
    s->last_bpp = 0;
1829
468
    s->s.h.filter.sharpness = -1;
1830
1831
468
    return init_frames(avctx);
1832
}
1833
1834
#if HAVE_THREADS
1835
static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1836
{
1837
    int i, ret;
1838
    VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
1839
1840
    for (i = 0; i < 3; i++) {
1841
        if (s->s.frames[i].tf.f->buf[0])
1842
            vp9_frame_unref(dst, &s->s.frames[i]);
1843
        if (ssrc->s.frames[i].tf.f->buf[0]) {
1844
            if ((ret = vp9_frame_ref(dst, &s->s.frames[i], &ssrc->s.frames[i])) < 0)
1845
                return ret;
1846
        }
1847
    }
1848
    for (i = 0; i < 8; i++) {
1849
        if (s->s.refs[i].f->buf[0])
1850
            ff_thread_release_buffer(dst, &s->s.refs[i]);
1851
        if (ssrc->next_refs[i].f->buf[0]) {
1852
            if ((ret = ff_thread_ref_frame(&s->s.refs[i], &ssrc->next_refs[i])) < 0)
1853
                return ret;
1854
        }
1855
    }
1856
1857
    s->s.h.invisible = ssrc->s.h.invisible;
1858
    s->s.h.keyframe = ssrc->s.h.keyframe;
1859
    s->s.h.intraonly = ssrc->s.h.intraonly;
1860
    s->ss_v = ssrc->ss_v;
1861
    s->ss_h = ssrc->ss_h;
1862
    s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
1863
    s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
1864
    s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
1865
    s->bytesperpixel = ssrc->bytesperpixel;
1866
    s->gf_fmt = ssrc->gf_fmt;
1867
    s->w = ssrc->w;
1868
    s->h = ssrc->h;
1869
    s->s.h.bpp = ssrc->s.h.bpp;
1870
    s->bpp_index = ssrc->bpp_index;
1871
    s->pix_fmt = ssrc->pix_fmt;
1872
    memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
1873
    memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
1874
    memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
1875
           sizeof(s->s.h.segmentation.feat));
1876
1877
    return 0;
1878
}
1879
#endif
1880
1881
AVCodec ff_vp9_decoder = {
1882
    .name                  = "vp9",
1883
    .long_name             = NULL_IF_CONFIG_SMALL("Google VP9"),
1884
    .type                  = AVMEDIA_TYPE_VIDEO,
1885
    .id                    = AV_CODEC_ID_VP9,
1886
    .priv_data_size        = sizeof(VP9Context),
1887
    .init                  = vp9_decode_init,
1888
    .close                 = vp9_decode_free,
1889
    .decode                = vp9_decode_frame,
1890
    .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
1891
    .caps_internal         = FF_CODEC_CAP_SLICE_THREAD_HAS_MF |
1892
                             FF_CODEC_CAP_ALLOCATE_PROGRESS,
1893
    .flush                 = vp9_decode_flush,
1894
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp9_decode_update_thread_context),
1895
    .profiles              = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
1896
    .bsfs                  = "vp9_superframe_split",
1897
    .hw_configs            = (const AVCodecHWConfigInternal*[]) {
1898
#if CONFIG_VP9_DXVA2_HWACCEL
1899
                               HWACCEL_DXVA2(vp9),
1900
#endif
1901
#if CONFIG_VP9_D3D11VA_HWACCEL
1902
                               HWACCEL_D3D11VA(vp9),
1903
#endif
1904
#if CONFIG_VP9_D3D11VA2_HWACCEL
1905
                               HWACCEL_D3D11VA2(vp9),
1906
#endif
1907
#if CONFIG_VP9_NVDEC_HWACCEL
1908
                               HWACCEL_NVDEC(vp9),
1909
#endif
1910
#if CONFIG_VP9_VAAPI_HWACCEL
1911
                               HWACCEL_VAAPI(vp9),
1912
#endif
1913
#if CONFIG_VP9_VDPAU_HWACCEL
1914
                               HWACCEL_VDPAU(vp9),
1915
#endif
1916
                               NULL
1917
                           },
1918
};