FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 852 1188 71.7%
Branches: 610 852 71.6%

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