FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 833 1151 72.4%
Functions: 20 27 74.1%
Branches: 573 786 72.9%

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