FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 856 1189 72.0%
Functions: 20 27 74.1%
Branches: 611 848 72.1%

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