FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 852 1188 71.7%
Branches: 610 852 71.6%

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