FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2026-08-19 01:31:13
Exec Total Coverage
Lines: 865 1202 72.0%
Functions: 21 28 75.0%
Branches: 581 804 72.3%

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