FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2026-11-01 05:14:56
Exec Total Coverage
Lines: 868 1208 71.9%
Functions: 21 28 75.0%
Branches: 583 806 72.3%

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