FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 861 1196 72.0%
Functions: 20 27 74.1%
Branches: 582 800 72.8%

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