FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp8.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 1369 1600 85.6%
Functions: 77 84 91.7%
Branches: 815 1157 70.4%

Line Branch Exec Source
1 /*
2 * VP7/VP8 compatible video decoder
3 *
4 * Copyright (C) 2010 David Conrad
5 * Copyright (C) 2010 Ronald S. Bultje
6 * Copyright (C) 2010 Fiona Glaser
7 * Copyright (C) 2012 Daniel Kang
8 * Copyright (C) 2014 Peter Ross
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include "config_components.h"
28
29 #include "libavutil/attributes.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/mem_internal.h"
32
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "hwaccel_internal.h"
37 #include "hwconfig.h"
38 #include "mathops.h"
39 #include "progressframe.h"
40 #include "libavutil/refstruct.h"
41 #include "thread.h"
42 #include "vp8.h"
43 #include "vp89_rac.h"
44 #include "vp8data.h"
45 #include "vpx_rac.h"
46
47 #if ARCH_ARM
48 # include "arm/vp8.h"
49 #endif
50
51 // fixme: add 1 bit to all the calls to this?
52 6119 static int vp8_rac_get_sint(VPXRangeCoder *c, int bits)
53 {
54 int v;
55
56
2/2
✓ Branch 1 taken 5373 times.
✓ Branch 2 taken 746 times.
6119 if (!vp89_rac_get(c))
57 5373 return 0;
58
59 746 v = vp89_rac_get_uint(c, bits);
60
61
2/2
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 648 times.
746 if (vp89_rac_get(c))
62 98 v = -v;
63
64 746 return v;
65 }
66
67 306 static int vp8_rac_get_nn(VPXRangeCoder *c)
68 {
69 306 int v = vp89_rac_get_uint(c, 7) << 1;
70 306 return v + !v;
71 }
72
73 // DCTextra
74 46103 static int vp8_rac_get_coeff(VPXRangeCoder *c, const uint8_t *prob)
75 {
76 46103 int v = 0;
77
78 do {
79 174171 v = (v<<1) + vpx_rac_get_prob(c, *prob++);
80
2/2
✓ Branch 0 taken 128068 times.
✓ Branch 1 taken 46103 times.
174171 } while (*prob);
81
82 46103 return v;
83 }
84
85 77 static void free_buffers(VP8Context *s)
86 {
87 int i;
88
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 39 times.
77 if (s->thread_data)
89
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 38 times.
342 for (i = 0; i < MAX_THREADS; i++) {
90 #if HAVE_THREADS
91 304 pthread_cond_destroy(&s->thread_data[i].cond);
92 304 pthread_mutex_destroy(&s->thread_data[i].lock);
93 #endif
94 304 av_freep(&s->thread_data[i].filter_strength);
95 }
96 77 av_freep(&s->thread_data);
97 77 av_freep(&s->macroblocks_base);
98 77 av_freep(&s->intra4x4_pred_mode_top);
99 77 av_freep(&s->top_nnz);
100 77 av_freep(&s->top_border);
101
102 77 s->macroblocks = NULL;
103 77 }
104
105 1147 static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
106 {
107 1147 int ret = ff_progress_frame_get_buffer(s->avctx, &f->tf,
108 ref ? AV_GET_BUFFER_FLAG_REF : 0);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (ret < 0)
110 return ret;
111 1147 f->seg_map = av_refstruct_allocz(s->mb_width * s->mb_height);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (!f->seg_map) {
113 ret = AVERROR(ENOMEM);
114 goto fail;
115 }
116 1147 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &f->hwaccel_picture_private);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (ret < 0)
118 goto fail;
119
120 1147 return 0;
121
122 fail:
123 av_refstruct_unref(&f->seg_map);
124 ff_progress_frame_unref(&f->tf);
125 return ret;
126 }
127
128 1420 static void vp8_release_frame(VP8Frame *f)
129 {
130 1420 av_refstruct_unref(&f->seg_map);
131 1420 av_refstruct_unref(&f->hwaccel_picture_private);
132 1420 ff_progress_frame_unref(&f->tf);
133 1420 }
134
135 77 static av_cold void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
136 {
137 77 VP8Context *s = avctx->priv_data;
138 int i;
139
140
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 77 times.
462 for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
141 385 vp8_release_frame(&s->frames[i]);
142 77 memset(s->framep, 0, sizeof(s->framep));
143
144
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 if (free_mem)
145 77 free_buffers(s);
146
147
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
77 if (FF_HW_HAS_CB(avctx, flush))
148 FF_HW_SIMPLE_CALL(avctx, flush);
149 77 }
150
151 static av_cold void vp8_decode_flush(AVCodecContext *avctx)
152 {
153 vp8_decode_flush_impl(avctx, 0);
154 }
155
156 1147 static VP8Frame *vp8_find_free_buffer(VP8Context *s)
157 {
158 1147 VP8Frame *frame = NULL;
159 int i;
160
161 // find a free buffer
162
1/2
✓ Branch 0 taken 2802 times.
✗ Branch 1 not taken.
2802 for (i = 0; i < 5; i++)
163
2/2
✓ Branch 0 taken 2209 times.
✓ Branch 1 taken 593 times.
2802 if (&s->frames[i] != s->framep[VP8_FRAME_CURRENT] &&
164
2/2
✓ Branch 0 taken 2208 times.
✓ Branch 1 taken 1 times.
2209 &s->frames[i] != s->framep[VP8_FRAME_PREVIOUS] &&
165
2/2
✓ Branch 0 taken 1605 times.
✓ Branch 1 taken 603 times.
2208 &s->frames[i] != s->framep[VP8_FRAME_GOLDEN] &&
166
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 458 times.
1605 &s->frames[i] != s->framep[VP8_FRAME_ALTREF]) {
167 1147 frame = &s->frames[i];
168 1147 break;
169 }
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (i == 5) {
171 av_log(s->avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
172 abort();
173 }
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (frame->tf.f)
175 vp8_release_frame(frame);
176
177 1147 return frame;
178 }
179
180 21 static enum AVPixelFormat get_pixel_format(VP8Context *s)
181 {
182 21 enum AVPixelFormat pix_fmts[] = {
183 #if CONFIG_VP8_VAAPI_HWACCEL
184 AV_PIX_FMT_VAAPI,
185 #endif
186 #if CONFIG_VP8_NVDEC_HWACCEL
187 AV_PIX_FMT_CUDA,
188 #endif
189 AV_PIX_FMT_YUV420P,
190 AV_PIX_FMT_NONE,
191 };
192
193 21 return ff_get_format(s->avctx, pix_fmts);
194 }
195
196 static av_always_inline
197 38 int update_dimensions(VP8Context *s, int width, int height, int is_vp7)
198 {
199 38 AVCodecContext *avctx = s->avctx;
200 38 int i, ret, dim_reset = 0;
201
202
7/8
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✓ Branch 7 taken 5 times.
38 if (width != s->avctx->width || ((width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height) && s->macroblocks_base ||
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 height != s->avctx->height) {
204 14 vp8_decode_flush_impl(s->avctx, 1);
205
206 14 ret = ff_set_dimensions(s->avctx, width, height);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0)
208 return ret;
209
210 14 dim_reset = (s->macroblocks_base != NULL);
211 }
212
213
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
38 if ((s->pix_fmt == AV_PIX_FMT_NONE || dim_reset) &&
214
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1 times.
28 !s->actually_webp && !is_vp7) {
215 21 s->pix_fmt = get_pixel_format(s);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (s->pix_fmt < 0)
217 return AVERROR(EINVAL);
218 21 avctx->pix_fmt = s->pix_fmt;
219 }
220
221 38 s->mb_width = (s->avctx->coded_width + 15) / 16;
222 38 s->mb_height = (s->avctx->coded_height + 15) / 16;
223
224
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
38 s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&
225 avctx->thread_count > 1;
226
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
38 if (!s->mb_layout) { // Frame threading and one thread
227 37 s->macroblocks_base = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *
228 sizeof(*s->macroblocks));
229 37 s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);
230 } else // Sliced threading
231 1 s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *
232 sizeof(*s->macroblocks));
233 38 s->top_nnz = av_mallocz(s->mb_width * sizeof(*s->top_nnz));
234 38 s->top_border = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));
235 38 s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));
236
237
3/6
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
38 if (!s->macroblocks_base || !s->top_nnz || !s->top_border ||
238
4/6
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
38 !s->thread_data || (!s->intra4x4_pred_mode_top && !s->mb_layout)) {
239 free_buffers(s);
240 return AVERROR(ENOMEM);
241 }
242
243
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 38 times.
342 for (i = 0; i < MAX_THREADS; i++) {
244 608 s->thread_data[i].filter_strength =
245 304 av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (!s->thread_data[i].filter_strength) {
247 free_buffers(s);
248 return AVERROR(ENOMEM);
249 }
250 #if HAVE_THREADS
251 304 ret = pthread_mutex_init(&s->thread_data[i].lock, NULL);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (ret) {
253 free_buffers(s);
254 return AVERROR(ret);
255 }
256 304 ret = pthread_cond_init(&s->thread_data[i].cond, NULL);
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (ret) {
258 free_buffers(s);
259 return AVERROR(ret);
260 }
261 #endif
262 }
263
264 38 s->macroblocks = s->macroblocks_base + 1;
265
266 38 return 0;
267 }
268
269 1 static int vp7_update_dimensions(VP8Context *s, int width, int height)
270 {
271 1 return update_dimensions(s, width, height, IS_VP7);
272 }
273
274 37 static int vp8_update_dimensions(VP8Context *s, int width, int height)
275 {
276 37 return update_dimensions(s, width, height, IS_VP8);
277 }
278
279
280 433 static void parse_segment_info(VP8Context *s)
281 {
282 433 VPXRangeCoder *c = &s->c;
283 int i;
284
285 433 s->segmentation.update_map = vp89_rac_get(c);
286 433 s->segmentation.update_feature_data = vp89_rac_get(c);
287
288
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 365 times.
433 if (s->segmentation.update_feature_data) {
289 68 s->segmentation.absolute_vals = vp89_rac_get(c);
290
291
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 68 times.
340 for (i = 0; i < 4; i++)
292 272 s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
293
294
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 68 times.
340 for (i = 0; i < 4; i++)
295 272 s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
296 }
297
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 365 times.
433 if (s->segmentation.update_map)
298
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 68 times.
272 for (i = 0; i < 3; i++)
299
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 180 times.
204 s->prob->segmentid[i] = vp89_rac_get(c) ? vp89_rac_get_uint(c, 8) : 255;
300 433 }
301
302 43 static void update_lf_deltas(VP8Context *s)
303 {
304 43 VPXRangeCoder *c = &s->c;
305 int i;
306
307
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 43 times.
215 for (i = 0; i < 4; i++) {
308
2/2
✓ Branch 1 taken 129 times.
✓ Branch 2 taken 43 times.
172 if (vp89_rac_get(c)) {
309 129 s->lf_delta.ref[i] = vp89_rac_get_uint(c, 6);
310
311
2/2
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 43 times.
129 if (vp89_rac_get(c))
312 86 s->lf_delta.ref[i] = -s->lf_delta.ref[i];
313 }
314 }
315
316
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 43 times.
215 for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++) {
317
1/2
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
172 if (vp89_rac_get(c)) {
318 172 s->lf_delta.mode[i] = vp89_rac_get_uint(c, 6);
319
320
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 129 times.
172 if (vp89_rac_get(c))
321 43 s->lf_delta.mode[i] = -s->lf_delta.mode[i];
322 }
323 }
324 43 }
325
326 1115 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
327 {
328 1115 const uint8_t *sizes = buf;
329 int i;
330 int ret;
331
332 1115 s->num_coeff_partitions = 1 << vp89_rac_get_uint(&s->c, 2);
333
334 1115 buf += 3 * (s->num_coeff_partitions - 1);
335 1115 buf_size -= 3 * (s->num_coeff_partitions - 1);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1115 times.
1115 if (buf_size < 0)
337 return -1;
338
339
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 1115 times.
1454 for (i = 0; i < s->num_coeff_partitions - 1; i++) {
340 339 int size = AV_RL24(sizes + 3 * i);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339 times.
339 if (buf_size - size < 0)
342 return -1;
343 339 s->coeff_partition_size[i] = size;
344
345 339 ret = ff_vpx_init_range_decoder(&s->coeff_partition[i], buf, size);
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339 times.
339 if (ret < 0)
347 return ret;
348 339 buf += size;
349 339 buf_size -= size;
350 }
351
352 1115 s->coeff_partition_size[i] = buf_size;
353
354 1115 return ff_vpx_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
355 }
356
357 32 static void vp7_get_quants(VP8Context *s)
358 {
359 32 VPXRangeCoder *c = &s->c;
360
361 32 int yac_qi = vp89_rac_get_uint(c, 7);
362
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 int ydc_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 int y2dc_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
364
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 int y2ac_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
365
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 int uvdc_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 int uvac_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
367
368 32 s->qmat[0].luma_qmul[0] = vp7_ydc_qlookup[ydc_qi];
369 32 s->qmat[0].luma_qmul[1] = vp7_yac_qlookup[yac_qi];
370 32 s->qmat[0].luma_dc_qmul[0] = vp7_y2dc_qlookup[y2dc_qi];
371 32 s->qmat[0].luma_dc_qmul[1] = vp7_y2ac_qlookup[y2ac_qi];
372 32 s->qmat[0].chroma_qmul[0] = FFMIN(vp7_ydc_qlookup[uvdc_qi], 132);
373 32 s->qmat[0].chroma_qmul[1] = vp7_yac_qlookup[uvac_qi];
374 32 }
375
376 1115 static void vp8_get_quants(VP8Context *s)
377 {
378 1115 VPXRangeCoder *c = &s->c;
379 int i, base_qi;
380
381 1115 s->quant.yac_qi = vp89_rac_get_uint(c, 7);
382 1115 s->quant.ydc_delta = vp8_rac_get_sint(c, 4);
383 1115 s->quant.y2dc_delta = vp8_rac_get_sint(c, 4);
384 1115 s->quant.y2ac_delta = vp8_rac_get_sint(c, 4);
385 1115 s->quant.uvdc_delta = vp8_rac_get_sint(c, 4);
386 1115 s->quant.uvac_delta = vp8_rac_get_sint(c, 4);
387
388
2/2
✓ Branch 0 taken 4460 times.
✓ Branch 1 taken 1115 times.
5575 for (i = 0; i < 4; i++) {
389
2/2
✓ Branch 0 taken 1732 times.
✓ Branch 1 taken 2728 times.
4460 if (s->segmentation.enabled) {
390 1732 base_qi = s->segmentation.base_quant[i];
391
2/2
✓ Branch 0 taken 1608 times.
✓ Branch 1 taken 124 times.
1732 if (!s->segmentation.absolute_vals)
392 1608 base_qi += s->quant.yac_qi;
393 } else
394 2728 base_qi = s->quant.yac_qi;
395
396 4460 s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + s->quant.ydc_delta, 7)];
397 4460 s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi, 7)];
398 4460 s->qmat[i].luma_dc_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + s->quant.y2dc_delta, 7)] * 2;
399 /* 101581>>16 is equivalent to 155/100 */
400 4460 s->qmat[i].luma_dc_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + s->quant.y2ac_delta, 7)] * 101581 >> 16;
401 4460 s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + s->quant.uvdc_delta, 7)];
402 4460 s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + s->quant.uvac_delta, 7)];
403
404 4460 s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
405 4460 s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
406 }
407 1115 }
408
409 /**
410 * Determine which buffers golden and altref should be updated with after this frame.
411 * The spec isn't clear here, so I'm going by my understanding of what libvpx does
412 *
413 * Intra frames update all 3 references
414 * Inter frames update VP8_FRAME_PREVIOUS if the update_last flag is set
415 * If the update (golden|altref) flag is set, it's updated with the current frame
416 * if update_last is set, and VP8_FRAME_PREVIOUS otherwise.
417 * If the flag is not set, the number read means:
418 * 0: no update
419 * 1: VP8_FRAME_PREVIOUS
420 * 2: update golden with altref, or update altref with golden
421 */
422 2132 static VP8FrameType ref_to_update(VP8Context *s, int update, VP8FrameType ref)
423 {
424 2132 VPXRangeCoder *c = &s->c;
425
426
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 2042 times.
2132 if (update)
427 90 return VP8_FRAME_CURRENT;
428
429
3/3
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 1927 times.
2042 switch (vp89_rac_get_uint(c, 2)) {
430 31 case 1:
431 31 return VP8_FRAME_PREVIOUS;
432 84 case 2:
433
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 81 times.
84 return (ref == VP8_FRAME_GOLDEN) ? VP8_FRAME_ALTREF : VP8_FRAME_GOLDEN;
434 }
435 1927 return VP8_FRAME_NONE;
436 }
437
438 50 static void vp78_reset_probability_tables(VP8Context *s)
439 {
440 int i, j;
441
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 50 times.
250 for (i = 0; i < 4; i++)
442
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 200 times.
3400 for (j = 0; j < 16; j++)
443 3200 memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
444 sizeof(s->prob->token[i][j]));
445 50 }
446
447 1147 static void vp78_update_probability_tables(VP8Context *s)
448 {
449 1147 VPXRangeCoder *c = &s->c;
450 int i, j, k, l, m;
451
452
2/2
✓ Branch 0 taken 4588 times.
✓ Branch 1 taken 1147 times.
5735 for (i = 0; i < 4; i++)
453
2/2
✓ Branch 0 taken 36704 times.
✓ Branch 1 taken 4588 times.
41292 for (j = 0; j < 8; j++)
454
2/2
✓ Branch 0 taken 110112 times.
✓ Branch 1 taken 36704 times.
146816 for (k = 0; k < 3; k++)
455
2/2
✓ Branch 0 taken 1211232 times.
✓ Branch 1 taken 110112 times.
1321344 for (l = 0; l < NUM_DCT_TOKENS-1; l++)
456
2/2
✓ Branch 1 taken 7439 times.
✓ Branch 2 taken 1203793 times.
1211232 if (vpx_rac_get_prob_branchy(c, ff_vp8_token_update_probs[i][j][k][l])) {
457 7439 int prob = vp89_rac_get_uint(c, 8);
458
2/2
✓ Branch 0 taken 17215 times.
✓ Branch 1 taken 7439 times.
24654 for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
459 17215 s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
460 }
461 1147 }
462
463 #define VP7_MVC_SIZE 17
464 #define VP8_MVC_SIZE 19
465
466 1097 static void vp78_update_pred16x16_pred8x8_mvc_probabilities(VP8Context *s,
467 int mvc_size)
468 {
469 1097 VPXRangeCoder *c = &s->c;
470 int i, j;
471
472
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1095 times.
1097 if (vp89_rac_get(c))
473
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < 4; i++)
474 8 s->prob->pred16x16[i] = vp89_rac_get_uint(c, 8);
475
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1096 times.
1097 if (vp89_rac_get(c))
476
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < 3; i++)
477 3 s->prob->pred8x8c[i] = vp89_rac_get_uint(c, 8);
478
479 // 17.2 MV probability update
480
2/2
✓ Branch 0 taken 2194 times.
✓ Branch 1 taken 1097 times.
3291 for (i = 0; i < 2; i++)
481
2/2
✓ Branch 0 taken 41562 times.
✓ Branch 1 taken 2194 times.
43756 for (j = 0; j < mvc_size; j++)
482
2/2
✓ Branch 1 taken 306 times.
✓ Branch 2 taken 41256 times.
41562 if (vpx_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
483 306 s->prob->mvc[i][j] = vp8_rac_get_nn(c);
484 1097 }
485
486 1066 static void update_refs(VP8Context *s)
487 {
488 1066 VPXRangeCoder *c = &s->c;
489
490 1066 int update_golden = vp89_rac_get(c);
491 1066 int update_altref = vp89_rac_get(c);
492
493 1066 s->update_golden = ref_to_update(s, update_golden, VP8_FRAME_GOLDEN);
494 1066 s->update_altref = ref_to_update(s, update_altref, VP8_FRAME_ALTREF);
495 1066 }
496
497 static void copy_chroma(AVFrame *dst, const AVFrame *src, int width, int height)
498 {
499 int i, j;
500
501 for (j = 1; j < 3; j++) {
502 for (i = 0; i < height / 2; i++)
503 memcpy(dst->data[j] + i * dst->linesize[j],
504 src->data[j] + i * src->linesize[j], width / 2);
505 }
506 }
507
508 static void fade(uint8_t *dst, ptrdiff_t dst_linesize,
509 const uint8_t *src, ptrdiff_t src_linesize,
510 int width, int height,
511 int alpha, int beta)
512 {
513 int i, j;
514 for (j = 0; j < height; j++) {
515 const uint8_t *src2 = src + j * src_linesize;
516 uint8_t *dst2 = dst + j * dst_linesize;
517 for (i = 0; i < width; i++) {
518 uint8_t y = src2[i];
519 dst2[i] = av_clip_uint8(y + ((y * beta) >> 8) + alpha);
520 }
521 }
522 }
523
524 32 static int vp7_fade_frame(VP8Context *s, int alpha, int beta)
525 {
526 int ret;
527
528
4/6
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 31 times.
32 if (!s->keyframe && (alpha || beta)) {
529 int width = s->mb_width * 16;
530 int height = s->mb_height * 16;
531 const AVFrame *src;
532 AVFrame *dst;
533
534 if (!s->framep[VP8_FRAME_PREVIOUS] ||
535 !s->framep[VP8_FRAME_GOLDEN]) {
536 av_log(s->avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
537 return AVERROR_INVALIDDATA;
538 }
539
540 src =
541 dst = s->framep[VP8_FRAME_PREVIOUS]->tf.f;
542
543 /* preserve the golden frame, write a new previous frame */
544 if (s->framep[VP8_FRAME_GOLDEN] == s->framep[VP8_FRAME_PREVIOUS]) {
545 VP8Frame *prev_frame = vp8_find_free_buffer(s);
546
547 ret = vp8_alloc_frame(s, prev_frame, 1);
548 if (ret < 0)
549 return ret;
550 s->framep[VP8_FRAME_PREVIOUS] = prev_frame;
551
552 dst = s->framep[VP8_FRAME_PREVIOUS]->tf.f;
553
554 copy_chroma(dst, src, width, height);
555 }
556
557 fade(dst->data[0], dst->linesize[0],
558 src->data[0], src->linesize[0],
559 width, height, alpha, beta);
560 }
561
562 32 return 0;
563 }
564
565 32 static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
566 {
567 32 VPXRangeCoder *c = &s->c;
568 int part1_size, hscale, vscale, i, j, ret;
569 32 int width = s->avctx->width;
570 32 int height = s->avctx->height;
571 32 int alpha = 0;
572 32 int beta = 0;
573 32 int fade_present = 1;
574
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (buf_size < 4) {
576 return AVERROR_INVALIDDATA;
577 }
578
579 32 s->profile = (buf[0] >> 1) & 7;
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->profile > 1) {
581 avpriv_request_sample(s->avctx, "Unknown profile %d", s->profile);
582 return AVERROR_INVALIDDATA;
583 }
584
585 32 s->keyframe = !(buf[0] & 1);
586 32 s->invisible = 0;
587 32 part1_size = AV_RL24(buf) >> 4;
588
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (buf_size < 4 - s->profile + part1_size) {
590 av_log(s->avctx, AV_LOG_ERROR, "Buffer size %d is too small, needed : %d\n", buf_size, 4 - s->profile + part1_size);
591 return AVERROR_INVALIDDATA;
592 }
593
594 32 buf += 4 - s->profile;
595 32 buf_size -= 4 - s->profile;
596
597 32 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
598
599 32 ret = ff_vpx_init_range_decoder(c, buf, part1_size);
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
601 return ret;
602 32 buf += part1_size;
603 32 buf_size -= part1_size;
604
605 /* A. Dimension information (keyframes only) */
606
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
32 if (s->keyframe) {
607 1 width = vp89_rac_get_uint(c, 12);
608 1 height = vp89_rac_get_uint(c, 12);
609 1 hscale = vp89_rac_get_uint(c, 2);
610 1 vscale = vp89_rac_get_uint(c, 2);
611
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (hscale || vscale)
612 avpriv_request_sample(s->avctx, "Upscaling");
613
614 1 s->update_golden = s->update_altref = VP8_FRAME_CURRENT;
615 1 vp78_reset_probability_tables(s);
616 1 memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
617 sizeof(s->prob->pred16x16));
618 1 memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
619 sizeof(s->prob->pred8x8c));
620
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < 2; i++)
621 2 memcpy(s->prob->mvc[i], vp7_mv_default_prob[i],
622 sizeof(vp7_mv_default_prob[i]));
623 1 memset(&s->segmentation, 0, sizeof(s->segmentation));
624 1 memset(&s->lf_delta, 0, sizeof(s->lf_delta));
625 1 memcpy(s->prob[0].scan, ff_zigzag_scan, sizeof(s->prob[0].scan));
626 }
627
628
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
32 if (s->keyframe || s->profile > 0)
629 1 memset(s->inter_dc_pred, 0 , sizeof(s->inter_dc_pred));
630
631 /* B. Decoding information for all four macroblock-level features */
632
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (i = 0; i < 4; i++) {
633 128 s->feature_enabled[i] = vp89_rac_get(c);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (s->feature_enabled[i]) {
635 s->feature_present_prob[i] = vp89_rac_get_uint(c, 8);
636
637 for (j = 0; j < 3; j++)
638 s->feature_index_prob[i][j] =
639 vp89_rac_get(c) ? vp89_rac_get_uint(c, 8) : 255;
640
641 if (vp7_feature_value_size[s->profile][i])
642 for (j = 0; j < 4; j++)
643 s->feature_value[i][j] =
644 vp89_rac_get(c) ? vp89_rac_get_uint(c, vp7_feature_value_size[s->profile][i]) : 0;
645 }
646 }
647
648 32 s->segmentation.enabled = 0;
649 32 s->segmentation.update_map = 0;
650 32 s->lf_delta.enabled = 0;
651
652 32 s->num_coeff_partitions = 1;
653 32 ret = ff_vpx_init_range_decoder(&s->coeff_partition[0], buf, buf_size);
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
655 return ret;
656
657
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
32 if (!s->macroblocks_base || /* first frame */
658
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 width != s->avctx->width || height != s->avctx->height ||
659
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 (width + 15) / 16 != s->mb_width || (height + 15) / 16 != s->mb_height) {
660
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = vp7_update_dimensions(s, width, height)) < 0)
661 return ret;
662 }
663
664 /* C. Dequantization indices */
665 32 vp7_get_quants(s);
666
667 /* D. Golden frame update flag (a Flag) for interframes only */
668
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
32 if (!s->keyframe) {
669
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 28 times.
31 s->update_golden = vp89_rac_get(c) ? VP8_FRAME_CURRENT : VP8_FRAME_NONE;
670 31 s->sign_bias[VP8_FRAME_GOLDEN] = 0;
671 }
672
673 32 s->update_last = 1;
674 32 s->update_probabilities = 1;
675
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->profile > 0) {
677 s->update_probabilities = vp89_rac_get(c);
678 if (!s->update_probabilities)
679 s->prob[1] = s->prob[0];
680
681 if (!s->keyframe)
682 fade_present = vp89_rac_get(c);
683 }
684
685
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if (vpx_rac_is_end(c))
686 return AVERROR_INVALIDDATA;
687 /* E. Fading information for previous frame */
688
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
32 if (fade_present && vp89_rac_get(c)) {
689 alpha = (int8_t) vp89_rac_get_uint(c, 8);
690 beta = (int8_t) vp89_rac_get_uint(c, 8);
691 }
692
693 /* F. Loop filter type */
694
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (!s->profile)
695 32 s->filter.simple = vp89_rac_get(c);
696
697 /* G. DCT coefficient ordering specification */
698
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 19 times.
32 if (vp89_rac_get(c))
699
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 13 times.
208 for (i = 1; i < 16; i++)
700 195 s->prob[0].scan[i] = ff_zigzag_scan[vp89_rac_get_uint(c, 4)];
701
702 /* H. Loop filter levels */
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->profile > 0)
704 s->filter.simple = vp89_rac_get(c);
705 32 s->filter.level = vp89_rac_get_uint(c, 6);
706 32 s->filter.sharpness = vp89_rac_get_uint(c, 3);
707
708 /* I. DCT coefficient probability update; 13.3 Token Probability Updates */
709 32 vp78_update_probability_tables(s);
710
711 32 s->mbskip_enabled = 0;
712
713 /* J. The remaining frame header data occurs ONLY FOR INTERFRAMES */
714
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
32 if (!s->keyframe) {
715 31 s->prob->intra = vp89_rac_get_uint(c, 8);
716 31 s->prob->last = vp89_rac_get_uint(c, 8);
717 31 vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP7_MVC_SIZE);
718 }
719
720
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if (vpx_rac_is_end(c))
721 return AVERROR_INVALIDDATA;
722
723
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if ((ret = vp7_fade_frame(s, alpha, beta)) < 0)
724 return ret;
725
726 32 return 0;
727 }
728
729 1115 static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
730 {
731 1115 VPXRangeCoder *c = &s->c;
732 int header_size, hscale, vscale, ret;
733 1115 int width = s->avctx->width;
734 1115 int height = s->avctx->height;
735
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1115 times.
1115 if (buf_size < 3) {
737 av_log(s->avctx, AV_LOG_ERROR, "Insufficient data (%d) for header\n", buf_size);
738 return AVERROR_INVALIDDATA;
739 }
740
741 1115 s->keyframe = !(buf[0] & 1);
742 1115 s->profile = (buf[0]>>1) & 7;
743 1115 s->invisible = !(buf[0] & 0x10);
744 1115 header_size = AV_RL24(buf) >> 5;
745 1115 buf += 3;
746 1115 buf_size -= 3;
747
748 1115 s->header_partition_size = header_size;
749
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1115 times.
1115 if (s->profile > 3)
751 av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
752
753
2/2
✓ Branch 0 taken 959 times.
✓ Branch 1 taken 156 times.
1115 if (!s->profile)
754 959 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab,
755 sizeof(s->put_pixels_tab));
756 else // profile 1-3 use bilinear, 4+ aren't defined so whatever
757 156 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab,
758 sizeof(s->put_pixels_tab));
759
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1115 times.
1115 if (header_size > buf_size - 7 * s->keyframe) {
761 av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
762 return AVERROR_INVALIDDATA;
763 }
764
765
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1066 times.
1115 if (s->keyframe) {
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (AV_RL24(buf) != 0x2a019d) {
767 av_log(s->avctx, AV_LOG_ERROR,
768 "Invalid start code 0x%x\n", AV_RL24(buf));
769 return AVERROR_INVALIDDATA;
770 }
771 49 width = AV_RL16(buf + 3) & 0x3fff;
772 49 height = AV_RL16(buf + 5) & 0x3fff;
773 49 hscale = buf[4] >> 6;
774 49 vscale = buf[6] >> 6;
775 49 buf += 7;
776 49 buf_size -= 7;
777
778
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
49 if (hscale || vscale)
779 avpriv_request_sample(s->avctx, "Upscaling");
780
781 49 s->update_golden = s->update_altref = VP8_FRAME_CURRENT;
782 49 vp78_reset_probability_tables(s);
783 49 memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
784 sizeof(s->prob->pred16x16));
785 49 memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
786 sizeof(s->prob->pred8x8c));
787 49 memcpy(s->prob->mvc, vp8_mv_default_prob,
788 sizeof(s->prob->mvc));
789 49 memset(&s->segmentation, 0, sizeof(s->segmentation));
790 49 memset(&s->lf_delta, 0, sizeof(s->lf_delta));
791 }
792
793 1115 ret = ff_vpx_init_range_decoder(c, buf, header_size);
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1115 times.
1115 if (ret < 0)
795 return ret;
796 1115 buf += header_size;
797 1115 buf_size -= header_size;
798
799
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1066 times.
1115 if (s->keyframe) {
800 49 s->colorspace = vp89_rac_get(c);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (s->colorspace)
802 av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
803 49 s->fullrange = vp89_rac_get(c);
804 }
805
806
2/2
✓ Branch 1 taken 433 times.
✓ Branch 2 taken 682 times.
1115 if ((s->segmentation.enabled = vp89_rac_get(c)))
807 433 parse_segment_info(s);
808 else
809 682 s->segmentation.update_map = 0; // FIXME: move this to some init function?
810
811 1115 s->filter.simple = vp89_rac_get(c);
812 1115 s->filter.level = vp89_rac_get_uint(c, 6);
813 1115 s->filter.sharpness = vp89_rac_get_uint(c, 3);
814
815
2/2
✓ Branch 1 taken 1109 times.
✓ Branch 2 taken 6 times.
1115 if ((s->lf_delta.enabled = vp89_rac_get(c))) {
816 1109 s->lf_delta.update = vp89_rac_get(c);
817
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1066 times.
1109 if (s->lf_delta.update)
818 43 update_lf_deltas(s);
819 }
820
821
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1115 times.
1115 if (setup_partitions(s, buf, buf_size)) {
822 av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
823 return AVERROR_INVALIDDATA;
824 }
825
826
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 27 times.
1115 if (!s->macroblocks_base || /* first frame */
827
4/4
✓ Branch 0 taken 1083 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1078 times.
✓ Branch 3 taken 5 times.
1088 width != s->avctx->width || height != s->avctx->height ||
828
2/4
✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1078 times.
1078 (width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height)
829
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = vp8_update_dimensions(s, width, height)) < 0)
830 return ret;
831
832 1115 vp8_get_quants(s);
833
834
2/2
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 49 times.
1115 if (!s->keyframe) {
835 1066 update_refs(s);
836 1066 s->sign_bias[VP8_FRAME_GOLDEN] = vp89_rac_get(c);
837 1066 s->sign_bias[VP8_FRAME_ALTREF] = vp89_rac_get(c);
838 }
839
840 // if we aren't saving this frame's probabilities for future frames,
841 // make a copy of the current probabilities
842
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 1051 times.
1115 if (!(s->update_probabilities = vp89_rac_get(c)))
843 64 s->prob[1] = s->prob[0];
844
845
4/4
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 49 times.
✓ Branch 3 taken 1055 times.
✓ Branch 4 taken 11 times.
1115 s->update_last = s->keyframe || vp89_rac_get(c);
846
847 1115 vp78_update_probability_tables(s);
848
849
2/2
✓ Branch 1 taken 1109 times.
✓ Branch 2 taken 6 times.
1115 if ((s->mbskip_enabled = vp89_rac_get(c)))
850 1109 s->prob->mbskip = vp89_rac_get_uint(c, 8);
851
852
2/2
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 49 times.
1115 if (!s->keyframe) {
853 1066 s->prob->intra = vp89_rac_get_uint(c, 8);
854 1066 s->prob->last = vp89_rac_get_uint(c, 8);
855 1066 s->prob->golden = vp89_rac_get_uint(c, 8);
856 1066 vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP8_MVC_SIZE);
857 }
858
859 // Record the entropy coder state here so that hwaccels can use it.
860 1115 s->c.code_word = vpx_rac_renorm(&s->c);
861 1115 s->coder_state_at_header_end.input = s->c.buffer - (-s->c.bits / 8);
862 1115 s->coder_state_at_header_end.range = s->c.high;
863 1115 s->coder_state_at_header_end.value = s->c.code_word >> 16;
864 1115 s->coder_state_at_header_end.bit_count = -s->c.bits % 8;
865
866 1115 return 0;
867 }
868
869 static av_always_inline
870 57182 void clamp_mv(const VP8mvbounds *s, VP8mv *dst, const VP8mv *src)
871 {
872 57182 dst->x = av_clip(src->x, av_clip(s->mv_min.x, INT16_MIN, INT16_MAX),
873 57182 av_clip(s->mv_max.x, INT16_MIN, INT16_MAX));
874 57182 dst->y = av_clip(src->y, av_clip(s->mv_min.y, INT16_MIN, INT16_MAX),
875 57182 av_clip(s->mv_max.y, INT16_MIN, INT16_MAX));
876 57182 }
877
878 /**
879 * Motion vector coding, 17.1.
880 */
881 84910 static av_always_inline int read_mv_component(VPXRangeCoder *c, const uint8_t *p, int vp7)
882 {
883 84910 int bit, x = 0;
884
885
2/2
✓ Branch 1 taken 19966 times.
✓ Branch 2 taken 64944 times.
84910 if (vpx_rac_get_prob_branchy(c, p[0])) {
886 int i;
887
888
2/2
✓ Branch 0 taken 59898 times.
✓ Branch 1 taken 19966 times.
79864 for (i = 0; i < 3; i++)
889 59898 x += vpx_rac_get_prob(c, p[9 + i]) << i;
890
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19966 times.
✓ Branch 2 taken 119796 times.
✓ Branch 3 taken 19966 times.
139762 for (i = (vp7 ? 7 : 9); i > 3; i--)
891 119796 x += vpx_rac_get_prob(c, p[9 + i]) << i;
892
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 19966 times.
✓ Branch 2 taken 12222 times.
✓ Branch 3 taken 7744 times.
✓ Branch 5 taken 5564 times.
✓ Branch 6 taken 6658 times.
19966 if (!(x & (vp7 ? 0xF0 : 0xFFF0)) || vpx_rac_get_prob(c, p[12]))
893 13308 x += 8;
894 } else {
895 // small_mvtree
896 64944 const uint8_t *ps = p + 2;
897 64944 bit = vpx_rac_get_prob(c, *ps);
898 64944 ps += 1 + 3 * bit;
899 64944 x += 4 * bit;
900 64944 bit = vpx_rac_get_prob(c, *ps);
901 64944 ps += 1 + bit;
902 64944 x += 2 * bit;
903 64944 x += vpx_rac_get_prob(c, *ps);
904 }
905
906
4/4
✓ Branch 0 taken 66588 times.
✓ Branch 1 taken 18322 times.
✓ Branch 3 taken 34958 times.
✓ Branch 4 taken 31630 times.
84910 return (x && vpx_rac_get_prob(c, p[1])) ? -x : x;
907 }
908
909 static int vp7_read_mv_component(VPXRangeCoder *c, const uint8_t *p)
910 {
911 return read_mv_component(c, p, 1);
912 }
913
914 28394 static int vp8_read_mv_component(VPXRangeCoder *c, const uint8_t *p)
915 {
916 28394 return read_mv_component(c, p, 0);
917 }
918
919 static av_always_inline
920 136610 const uint8_t *get_submv_prob(uint32_t left, uint32_t top, int is_vp7)
921 {
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136610 times.
136610 if (is_vp7)
923 return vp7_submv_prob;
924
925
2/2
✓ Branch 0 taken 58719 times.
✓ Branch 1 taken 77891 times.
136610 if (left == top)
926
2/2
✓ Branch 0 taken 23422 times.
✓ Branch 1 taken 35297 times.
58719 return vp8_submv_prob[4 - !!left];
927
2/2
✓ Branch 0 taken 18934 times.
✓ Branch 1 taken 58957 times.
77891 if (!top)
928 18934 return vp8_submv_prob[2];
929 58957 return vp8_submv_prob[1 - !!left];
930 }
931
932 /**
933 * Split motion vector prediction, 16.4.
934 * @returns the number of motion vectors parsed (2, 4 or 16)
935 */
936 static av_always_inline
937 18330 int decode_splitmvs(const VP8Context *s, VPXRangeCoder *c, VP8Macroblock *mb,
938 int layout, int is_vp7)
939 {
940 int part_idx;
941 int n, num;
942 const VP8Macroblock *top_mb;
943 18330 const VP8Macroblock *left_mb = &mb[-1];
944 18330 const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning];
945 const uint8_t *mbsplits_top, *mbsplits_cur, *firstidx;
946 const VP8mv *top_mv;
947 18330 const VP8mv *left_mv = left_mb->bmv;
948 18330 const VP8mv *cur_mv = mb->bmv;
949
950
1/2
✓ Branch 0 taken 18330 times.
✗ Branch 1 not taken.
18330 if (!layout) // layout is inlined, s->mb_layout is not
951 18330 top_mb = &mb[2];
952 else
953 top_mb = &mb[-s->mb_width - 1];
954 18330 mbsplits_top = vp8_mbsplits[top_mb->partitioning];
955 18330 top_mv = top_mb->bmv;
956
957
2/2
✓ Branch 1 taken 11715 times.
✓ Branch 2 taken 6615 times.
18330 if (vpx_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
958
2/2
✓ Branch 1 taken 8045 times.
✓ Branch 2 taken 3670 times.
11715 if (vpx_rac_get_prob_branchy(c, vp8_mbsplit_prob[1]))
959 8045 part_idx = VP8_SPLITMVMODE_16x8 + vpx_rac_get_prob(c, vp8_mbsplit_prob[2]);
960 else
961 3670 part_idx = VP8_SPLITMVMODE_8x8;
962 } else {
963 6615 part_idx = VP8_SPLITMVMODE_4x4;
964 }
965
966 18330 num = vp8_mbsplit_count[part_idx];
967 18330 mbsplits_cur = vp8_mbsplits[part_idx],
968 18330 firstidx = vp8_mbfirstidx[part_idx];
969 18330 mb->partitioning = part_idx;
970
971
2/2
✓ Branch 0 taken 136610 times.
✓ Branch 1 taken 18330 times.
154940 for (n = 0; n < num; n++) {
972 136610 int k = firstidx[n];
973 uint32_t left, above;
974 const uint8_t *submv_prob;
975
976
2/2
✓ Branch 0 taken 46755 times.
✓ Branch 1 taken 89855 times.
136610 if (!(k & 3))
977 46755 left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
978 else
979 89855 left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
980
2/2
✓ Branch 0 taken 44980 times.
✓ Branch 1 taken 91630 times.
136610 if (k <= 3)
981 44980 above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
982 else
983 91630 above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
984
985 136610 submv_prob = get_submv_prob(left, above, is_vp7);
986
987
2/2
✓ Branch 1 taken 50617 times.
✓ Branch 2 taken 85993 times.
136610 if (vpx_rac_get_prob_branchy(c, submv_prob[0])) {
988
2/2
✓ Branch 1 taken 31746 times.
✓ Branch 2 taken 18871 times.
50617 if (vpx_rac_get_prob_branchy(c, submv_prob[1])) {
989
2/2
✓ Branch 1 taken 28258 times.
✓ Branch 2 taken 3488 times.
31746 if (vpx_rac_get_prob_branchy(c, submv_prob[2])) {
990 56516 mb->bmv[n].y = mb->mv.y +
991 28258 read_mv_component(c, s->prob->mvc[0], is_vp7);
992 28258 mb->bmv[n].x = mb->mv.x +
993 28258 read_mv_component(c, s->prob->mvc[1], is_vp7);
994 } else {
995 3488 AV_ZERO32(&mb->bmv[n]);
996 }
997 } else {
998 18871 AV_WN32A(&mb->bmv[n], above);
999 }
1000 } else {
1001 85993 AV_WN32A(&mb->bmv[n], left);
1002 }
1003 }
1004
1005 18330 return num;
1006 }
1007
1008 /**
1009 * The vp7 reference decoder uses a padding macroblock column (added to right
1010 * edge of the frame) to guard against illegal macroblock offsets. The
1011 * algorithm has bugs that permit offsets to straddle the padding column.
1012 * This function replicates those bugs.
1013 *
1014 * @param[out] edge_x macroblock x address
1015 * @param[out] edge_y macroblock y address
1016 *
1017 * @return macroblock offset legal (boolean)
1018 */
1019 81840 static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width,
1020 int xoffset, int yoffset, int boundary,
1021 int *edge_x, int *edge_y)
1022 {
1023 81840 int vwidth = mb_width + 1;
1024 81840 int new = (mb_y + yoffset) * vwidth + mb_x + xoffset;
1025
4/4
✓ Branch 0 taken 72013 times.
✓ Branch 1 taken 9827 times.
✓ Branch 2 taken 2852 times.
✓ Branch 3 taken 69161 times.
81840 if (new < boundary || new % vwidth == vwidth - 1)
1026 12679 return 0;
1027 69161 *edge_y = new / vwidth;
1028 69161 *edge_x = new % vwidth;
1029 69161 return 1;
1030 }
1031
1032 69161 static const VP8mv *get_bmv_ptr(const VP8Macroblock *mb, int subblock)
1033 {
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69161 times.
69161 return &mb->bmv[mb->mode == VP8_MVMODE_SPLIT ? vp8_mbsplits[mb->partitioning][subblock] : 0];
1035 }
1036
1037 static av_always_inline
1038 6820 void vp7_decode_mvs(VP8Context *s, VP8Macroblock *mb,
1039 int mb_x, int mb_y, int layout)
1040 {
1041 enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR };
1042 enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
1043 6820 int idx = CNT_ZERO;
1044 VP8mv near_mv[3];
1045 6820 uint8_t cnt[3] = { 0 };
1046 6820 VPXRangeCoder *c = &s->c;
1047 int i;
1048
1049 6820 AV_ZERO32(&near_mv[0]);
1050 6820 AV_ZERO32(&near_mv[1]);
1051 6820 AV_ZERO32(&near_mv[2]);
1052
1053
2/2
✓ Branch 0 taken 81840 times.
✓ Branch 1 taken 6820 times.
88660 for (i = 0; i < VP7_MV_PRED_COUNT; i++) {
1054 81840 const VP7MVPred * pred = &vp7_mv_pred[i];
1055 int edge_x, edge_y;
1056
1057
2/2
✓ Branch 0 taken 69161 times.
✓ Branch 1 taken 12679 times.
81840 if (vp7_calculate_mb_offset(mb_x, mb_y, s->mb_width, pred->xoffset,
1058 81840 pred->yoffset, !s->profile, &edge_x, &edge_y)) {
1059
1/2
✓ Branch 0 taken 69161 times.
✗ Branch 1 not taken.
69161 const VP8Macroblock *edge = (s->mb_layout == 1)
1060 69161 ? s->macroblocks_base + 1 + edge_x +
1061 69161 (s->mb_width + 1) * (edge_y + 1)
1062 : s->macroblocks + edge_x +
1063 (s->mb_height - edge_y - 1) * 2;
1064 69161 uint32_t mv = AV_RN32A(get_bmv_ptr(edge, vp7_mv_pred[i].subblock));
1065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69161 times.
69161 if (mv) {
1066 if (AV_RN32A(&near_mv[CNT_NEAREST])) {
1067 if (mv == AV_RN32A(&near_mv[CNT_NEAREST])) {
1068 idx = CNT_NEAREST;
1069 } else if (AV_RN32A(&near_mv[CNT_NEAR])) {
1070 if (mv != AV_RN32A(&near_mv[CNT_NEAR]))
1071 continue;
1072 idx = CNT_NEAR;
1073 } else {
1074 AV_WN32A(&near_mv[CNT_NEAR], mv);
1075 idx = CNT_NEAR;
1076 }
1077 } else {
1078 AV_WN32A(&near_mv[CNT_NEAREST], mv);
1079 idx = CNT_NEAREST;
1080 }
1081 } else {
1082 69161 idx = CNT_ZERO;
1083 }
1084 } else {
1085 12679 idx = CNT_ZERO;
1086 }
1087 81840 cnt[idx] += vp7_mv_pred[i].score;
1088 }
1089
1090 6820 mb->partitioning = VP8_SPLITMVMODE_NONE;
1091
1092
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6820 times.
6820 if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_ZERO]][0])) {
1093 mb->mode = VP8_MVMODE_MV;
1094
1095 if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAREST]][1])) {
1096
1097 if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][2])) {
1098
1099 if (cnt[CNT_NEAREST] > cnt[CNT_NEAR])
1100 AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAREST] ? 0 : AV_RN32A(&near_mv[CNT_NEAREST]));
1101 else
1102 AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAR] ? 0 : AV_RN32A(&near_mv[CNT_NEAR]));
1103
1104 if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][3])) {
1105 mb->mode = VP8_MVMODE_SPLIT;
1106 mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP7) - 1];
1107 } else {
1108 mb->mv.y += vp7_read_mv_component(c, s->prob->mvc[0]);
1109 mb->mv.x += vp7_read_mv_component(c, s->prob->mvc[1]);
1110 mb->bmv[0] = mb->mv;
1111 }
1112 } else {
1113 mb->mv = near_mv[CNT_NEAR];
1114 mb->bmv[0] = mb->mv;
1115 }
1116 } else {
1117 mb->mv = near_mv[CNT_NEAREST];
1118 mb->bmv[0] = mb->mv;
1119 }
1120 } else {
1121 6820 mb->mode = VP8_MVMODE_ZERO;
1122 6820 AV_ZERO32(&mb->mv);
1123 6820 mb->bmv[0] = mb->mv;
1124 }
1125 6820 }
1126
1127 static av_always_inline
1128 379559 void vp8_decode_mvs(VP8Context *s, const VP8mvbounds *mv_bounds, VP8Macroblock *mb,
1129 int mb_x, int mb_y, int layout)
1130 {
1131 379559 VP8Macroblock *mb_edge[3] = { 0 /* top */,
1132 379559 mb - 1 /* left */,
1133 0 /* top-left */ };
1134 enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
1135 enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
1136 379559 int idx = CNT_ZERO;
1137 379559 int cur_sign_bias = s->sign_bias[mb->ref_frame];
1138 379559 const int8_t *sign_bias = s->sign_bias;
1139 VP8mv near_mv[4];
1140 379559 uint8_t cnt[4] = { 0 };
1141 379559 VPXRangeCoder *c = &s->c;
1142
1143
1/2
✓ Branch 0 taken 379559 times.
✗ Branch 1 not taken.
379559 if (!layout) { // layout is inlined (s->mb_layout is not)
1144 379559 mb_edge[0] = mb + 2;
1145 379559 mb_edge[2] = mb + 1;
1146 } else {
1147 mb_edge[0] = mb - s->mb_width - 1;
1148 mb_edge[2] = mb - s->mb_width - 2;
1149 }
1150
1151 379559 AV_ZERO32(&near_mv[0]);
1152 379559 AV_ZERO32(&near_mv[1]);
1153 379559 AV_ZERO32(&near_mv[2]);
1154
1155 /* Process MB on top, left and top-left */
1156 #define MV_EDGE_CHECK(n) \
1157 { \
1158 const VP8Macroblock *edge = mb_edge[n]; \
1159 int edge_ref = edge->ref_frame; \
1160 if (edge_ref != VP8_FRAME_CURRENT) { \
1161 uint32_t mv = AV_RN32A(&edge->mv); \
1162 if (mv) { \
1163 if (cur_sign_bias != sign_bias[edge_ref]) { \
1164 /* SWAR negate of the values in mv. */ \
1165 mv = ~mv; \
1166 mv = ((mv & 0x7fff7fff) + \
1167 0x00010001) ^ (mv & 0x80008000); \
1168 } \
1169 if (!n || mv != AV_RN32A(&near_mv[idx])) \
1170 AV_WN32A(&near_mv[++idx], mv); \
1171 cnt[idx] += 1 + (n != 2); \
1172 } else \
1173 cnt[CNT_ZERO] += 1 + (n != 2); \
1174 } \
1175 }
1176
1177
6/6
✓ Branch 0 taken 352727 times.
✓ Branch 1 taken 26832 times.
✓ Branch 2 taken 44594 times.
✓ Branch 3 taken 308133 times.
✓ Branch 4 taken 109 times.
✓ Branch 5 taken 44485 times.
379559 MV_EDGE_CHECK(0)
1178
8/8
✓ Branch 0 taken 360687 times.
✓ Branch 1 taken 18872 times.
✓ Branch 2 taken 44761 times.
✓ Branch 3 taken 315926 times.
✓ Branch 4 taken 87 times.
✓ Branch 5 taken 44674 times.
✓ Branch 6 taken 36130 times.
✓ Branch 7 taken 8631 times.
379559 MV_EDGE_CHECK(1)
1179
8/8
✓ Branch 0 taken 339544 times.
✓ Branch 1 taken 40015 times.
✓ Branch 2 taken 41903 times.
✓ Branch 3 taken 297641 times.
✓ Branch 4 taken 123 times.
✓ Branch 5 taken 41780 times.
✓ Branch 6 taken 24718 times.
✓ Branch 7 taken 17185 times.
379559 MV_EDGE_CHECK(2)
1180
1181 379559 mb->partitioning = VP8_SPLITMVMODE_NONE;
1182
2/2
✓ Branch 1 taken 57182 times.
✓ Branch 2 taken 322377 times.
379559 if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
1183 57182 mb->mode = VP8_MVMODE_MV;
1184
1185 /* If we have three distinct MVs, merge first and last if they're the same */
1186
2/2
✓ Branch 0 taken 12759 times.
✓ Branch 1 taken 44423 times.
57182 if (cnt[CNT_SPLITMV] &&
1187
2/2
✓ Branch 0 taken 5465 times.
✓ Branch 1 taken 7294 times.
12759 AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
1188 5465 cnt[CNT_NEAREST] += 1;
1189
1190 /* Swap near and nearest if necessary */
1191
2/2
✓ Branch 0 taken 4482 times.
✓ Branch 1 taken 52700 times.
57182 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
1192 4482 FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
1193 4482 FFSWAP(VP8mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
1194 }
1195
1196
2/2
✓ Branch 1 taken 37615 times.
✓ Branch 2 taken 19567 times.
57182 if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
1197
2/2
✓ Branch 1 taken 32527 times.
✓ Branch 2 taken 5088 times.
37615 if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
1198 /* Choose the best mv out of 0,0 and the nearest mv */
1199 32527 clamp_mv(mv_bounds, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
1200 32527 cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
1201 32527 (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
1202 32527 (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
1203
1204
2/2
✓ Branch 1 taken 18330 times.
✓ Branch 2 taken 14197 times.
32527 if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
1205 18330 mb->mode = VP8_MVMODE_SPLIT;
1206 18330 mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP8) - 1];
1207 } else {
1208 14197 mb->mv.y += vp8_read_mv_component(c, s->prob->mvc[0]);
1209 14197 mb->mv.x += vp8_read_mv_component(c, s->prob->mvc[1]);
1210 14197 mb->bmv[0] = mb->mv;
1211 }
1212 } else {
1213 5088 clamp_mv(mv_bounds, &mb->mv, &near_mv[CNT_NEAR]);
1214 5088 mb->bmv[0] = mb->mv;
1215 }
1216 } else {
1217 19567 clamp_mv(mv_bounds, &mb->mv, &near_mv[CNT_NEAREST]);
1218 19567 mb->bmv[0] = mb->mv;
1219 }
1220 } else {
1221 322377 mb->mode = VP8_MVMODE_ZERO;
1222 322377 AV_ZERO32(&mb->mv);
1223 322377 mb->bmv[0] = mb->mv;
1224 }
1225 379559 }
1226
1227 static av_always_inline
1228 17729 void decode_intra4x4_modes(VP8Context *s, VPXRangeCoder *c, VP8Macroblock *mb,
1229 int mb_x, int keyframe, int layout)
1230 {
1231 17729 uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1232
1233
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17696 times.
17729 if (layout) {
1234 33 VP8Macroblock *mb_top = mb - s->mb_width - 1;
1235 33 memcpy(mb->intra4x4_pred_mode_top, mb_top->intra4x4_pred_mode_top, 4);
1236 }
1237
2/2
✓ Branch 0 taken 10859 times.
✓ Branch 1 taken 6870 times.
17729 if (keyframe) {
1238 int x, y;
1239 uint8_t *top;
1240 10859 uint8_t *const left = s->intra4x4_pred_mode_left;
1241
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 10826 times.
10859 if (layout)
1242 33 top = mb->intra4x4_pred_mode_top;
1243 else
1244 10826 top = s->intra4x4_pred_mode_top + 4 * mb_x;
1245
2/2
✓ Branch 0 taken 43436 times.
✓ Branch 1 taken 10859 times.
54295 for (y = 0; y < 4; y++) {
1246
2/2
✓ Branch 0 taken 173744 times.
✓ Branch 1 taken 43436 times.
217180 for (x = 0; x < 4; x++) {
1247 const uint8_t *ctx;
1248 173744 ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
1249 173744 *intra4x4 = vp89_rac_get_tree(c, vp8_pred4x4_tree, ctx);
1250 173744 left[y] = top[x] = *intra4x4;
1251 173744 intra4x4++;
1252 }
1253 }
1254 } else {
1255 int i;
1256
2/2
✓ Branch 0 taken 109920 times.
✓ Branch 1 taken 6870 times.
116790 for (i = 0; i < 16; i++)
1257 109920 intra4x4[i] = vp89_rac_get_tree(c, vp8_pred4x4_tree,
1258 vp8_pred4x4_prob_inter);
1259 }
1260 17729 }
1261
1262 static av_always_inline
1263 443889 void decode_mb_mode(VP8Context *s, const VP8mvbounds *mv_bounds,
1264 VP8Macroblock *mb, int mb_x, int mb_y,
1265 uint8_t *segment, const uint8_t *ref, int layout, int is_vp7)
1266 {
1267 443889 VPXRangeCoder *c = &s->c;
1268 static const char * const vp7_feature_name[] = { "q-index",
1269 "lf-delta",
1270 "partial-golden-update",
1271 "blit-pitch" };
1272
2/2
✓ Branch 0 taken 7040 times.
✓ Branch 1 taken 436849 times.
443889 if (is_vp7) {
1273 int i;
1274 7040 *segment = 0;
1275
2/2
✓ Branch 0 taken 28160 times.
✓ Branch 1 taken 7040 times.
35200 for (i = 0; i < 4; i++) {
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28160 times.
28160 if (s->feature_enabled[i]) {
1277 if (vpx_rac_get_prob_branchy(c, s->feature_present_prob[i])) {
1278 int index = vp89_rac_get_tree(c, vp7_feature_index_tree,
1279 s->feature_index_prob[i]);
1280 av_log(s->avctx, AV_LOG_WARNING,
1281 "Feature %s present in macroblock (value 0x%x)\n",
1282 vp7_feature_name[i], s->feature_value[i][index]);
1283 }
1284 }
1285 }
1286
2/2
✓ Branch 0 taken 8289 times.
✓ Branch 1 taken 428560 times.
436849 } else if (s->segmentation.update_map) {
1287 8289 int bit = vpx_rac_get_prob(c, s->prob->segmentid[0]);
1288 8289 *segment = vpx_rac_get_prob(c, s->prob->segmentid[1+bit]) + 2*bit;
1289
2/2
✓ Branch 0 taken 94425 times.
✓ Branch 1 taken 334135 times.
428560 } else if (s->segmentation.enabled)
1290
1/2
✓ Branch 0 taken 94425 times.
✗ Branch 1 not taken.
94425 *segment = ref ? *ref : *segment;
1291 443889 mb->segment = *segment;
1292
1293
2/2
✓ Branch 0 taken 436095 times.
✓ Branch 1 taken 7794 times.
443889 mb->skip = s->mbskip_enabled ? vpx_rac_get_prob(c, s->prob->mbskip) : 0;
1294
1295
2/2
✓ Branch 0 taken 32952 times.
✓ Branch 1 taken 410937 times.
443889 if (s->keyframe) {
1296 32952 mb->mode = vp89_rac_get_tree(c, vp8_pred16x16_tree_intra,
1297 vp8_pred16x16_prob_intra);
1298
1299
2/2
✓ Branch 0 taken 10859 times.
✓ Branch 1 taken 22093 times.
32952 if (mb->mode == MODE_I4x4) {
1300 10859 decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
1301 } else {
1302 22093 const uint32_t modes = (is_vp7 ? vp7_pred4x4_mode
1303
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 21906 times.
22093 : vp8_pred4x4_mode)[mb->mode] * 0x01010101u;
1304
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 21906 times.
22093 if (s->mb_layout)
1305 187 AV_WN32A(mb->intra4x4_pred_mode_top, modes);
1306 else
1307 21906 AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
1308 22093 AV_WN32A(s->intra4x4_pred_mode_left, modes);
1309 }
1310
1311 32952 mb->chroma_pred_mode = vp89_rac_get_tree(c, vp8_pred8x8c_tree,
1312 vp8_pred8x8c_prob_intra);
1313 32952 mb->ref_frame = VP8_FRAME_CURRENT;
1314
2/2
✓ Branch 1 taken 386379 times.
✓ Branch 2 taken 24558 times.
410937 } else if (vpx_rac_get_prob_branchy(c, s->prob->intra)) {
1315 // inter MB, 16.2
1316
2/2
✓ Branch 1 taken 17280 times.
✓ Branch 2 taken 369099 times.
386379 if (vpx_rac_get_prob_branchy(c, s->prob->last))
1317
2/2
✓ Branch 0 taken 17248 times.
✓ Branch 1 taken 32 times.
34528 mb->ref_frame =
1318
2/2
✓ Branch 1 taken 8904 times.
✓ Branch 2 taken 8344 times.
17248 (!is_vp7 && vpx_rac_get_prob(c, s->prob->golden)) ? VP8_FRAME_ALTREF
1319 : VP8_FRAME_GOLDEN;
1320 else
1321 369099 mb->ref_frame = VP8_FRAME_PREVIOUS;
1322 386379 s->ref_count[mb->ref_frame - 1]++;
1323
1324 // motion vectors, 16.3
1325
2/2
✓ Branch 0 taken 6820 times.
✓ Branch 1 taken 379559 times.
386379 if (is_vp7)
1326 6820 vp7_decode_mvs(s, mb, mb_x, mb_y, layout);
1327 else
1328 379559 vp8_decode_mvs(s, mv_bounds, mb, mb_x, mb_y, layout);
1329 } else {
1330 // intra MB, 16.1
1331 49116 mb->mode = vp89_rac_get_tree(c, vp8_pred16x16_tree_inter,
1332 24558 s->prob->pred16x16);
1333
1334
2/2
✓ Branch 0 taken 6870 times.
✓ Branch 1 taken 17688 times.
24558 if (mb->mode == MODE_I4x4)
1335 6870 decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
1336
1337 49116 mb->chroma_pred_mode = vp89_rac_get_tree(c, vp8_pred8x8c_tree,
1338 24558 s->prob->pred8x8c);
1339 24558 mb->ref_frame = VP8_FRAME_CURRENT;
1340 24558 mb->partitioning = VP8_SPLITMVMODE_NONE;
1341 24558 AV_ZERO32(&mb->bmv[0]);
1342 }
1343 443889 }
1344
1345 /**
1346 * @param r arithmetic bitstream reader context
1347 * @param block destination for block coefficients
1348 * @param probs probabilities to use when reading trees from the bitstream
1349 * @param i initial coeff index, 0 unless a separate DC block is coded
1350 * @param qmul array holding the dc/ac dequant factor at position 0/1
1351 *
1352 * @return 0 if no coeffs were decoded
1353 * otherwise, the index of the last coeff decoded plus one
1354 */
1355 static av_always_inline
1356 394232 int decode_block_coeffs_internal(VPXRangeCoder *r, int16_t block[16],
1357 uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1358 int i, const uint8_t *token_prob, const int16_t qmul[2],
1359 const uint8_t scan[16], int vp7)
1360 {
1361 394232 VPXRangeCoder c = *r;
1362 394232 goto skip_eob;
1363 do {
1364 int coeff;
1365 1108425 restart:
1366
2/2
✓ Branch 1 taken 374359 times.
✓ Branch 2 taken 736185 times.
1110544 if (!vpx_rac_get_prob_branchy(&c, token_prob[0])) // DCT_EOB
1367 374359 break;
1368
1369 1573403 skip_eob:
1370
2/2
✓ Branch 1 taken 839337 times.
✓ Branch 2 taken 1128298 times.
1967635 if (!vpx_rac_get_prob_branchy(&c, token_prob[1])) { // DCT_0
1371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 839337 times.
839337 if (++i == 16)
1372 break; // invalid input; blocks should end with EOB
1373 839337 token_prob = probs[i][0];
1374
2/2
✓ Branch 0 taken 2119 times.
✓ Branch 1 taken 837218 times.
839337 if (vp7)
1375 2119 goto restart;
1376 837218 goto skip_eob;
1377 }
1378
1379
2/2
✓ Branch 1 taken 721183 times.
✓ Branch 2 taken 407115 times.
1128298 if (!vpx_rac_get_prob_branchy(&c, token_prob[2])) { // DCT_1
1380 721183 coeff = 1;
1381 721183 token_prob = probs[i + 1][1];
1382 } else {
1383
2/2
✓ Branch 1 taken 276058 times.
✓ Branch 2 taken 131057 times.
407115 if (!vpx_rac_get_prob_branchy(&c, token_prob[3])) { // DCT 2,3,4
1384 276058 coeff = vpx_rac_get_prob_branchy(&c, token_prob[4]);
1385
2/2
✓ Branch 0 taken 114332 times.
✓ Branch 1 taken 161726 times.
276058 if (coeff)
1386 114332 coeff += vpx_rac_get_prob(&c, token_prob[5]);
1387 276058 coeff += 2;
1388 } else {
1389 // DCT_CAT*
1390
2/2
✓ Branch 1 taken 84954 times.
✓ Branch 2 taken 46103 times.
131057 if (!vpx_rac_get_prob_branchy(&c, token_prob[6])) {
1391
2/2
✓ Branch 1 taken 45865 times.
✓ Branch 2 taken 39089 times.
84954 if (!vpx_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
1392 45865 coeff = 5 + vpx_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
1393 } else { // DCT_CAT2
1394 39089 coeff = 7;
1395 39089 coeff += vpx_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
1396 39089 coeff += vpx_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
1397 }
1398 } else { // DCT_CAT3 and up
1399 46103 int a = vpx_rac_get_prob(&c, token_prob[8]);
1400 46103 int b = vpx_rac_get_prob(&c, token_prob[9 + a]);
1401 46103 int cat = (a << 1) + b;
1402 46103 coeff = 3 + (8 << cat);
1403 46103 coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
1404 }
1405 }
1406 407115 token_prob = probs[i + 1][2];
1407 }
1408
4/4
✓ Branch 1 taken 568483 times.
✓ Branch 2 taken 559815 times.
✓ Branch 3 taken 828829 times.
✓ Branch 4 taken 299469 times.
1128298 block[scan[i]] = (vp89_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
1409
2/2
✓ Branch 0 taken 1108425 times.
✓ Branch 1 taken 19873 times.
1128298 } while (++i < 16);
1410
1411 394232 *r = c;
1412 394232 return i;
1413 }
1414
1415 static av_always_inline
1416 6820 int inter_predict_dc(int16_t block[16], int16_t pred[2])
1417 {
1418 6820 int16_t dc = block[0];
1419 6820 int ret = 0;
1420
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6820 times.
6820 if (pred[1] > 3) {
1422 dc += pred[0];
1423 ret = 1;
1424 }
1425
1426
1/2
✓ Branch 0 taken 6820 times.
✗ Branch 1 not taken.
6820 if (!pred[0] | !dc | ((int32_t)pred[0] ^ (int32_t)dc) >> 31) {
1427 6820 block[0] = pred[0] = dc;
1428 6820 pred[1] = 0;
1429 } else {
1430 if (pred[0] == dc)
1431 pred[1]++;
1432 block[0] = pred[0] = dc;
1433 }
1434
1435 6820 return ret;
1436 }
1437
1438 736 static int vp7_decode_block_coeffs_internal(VPXRangeCoder *r,
1439 int16_t block[16],
1440 uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1441 int i, const uint8_t *token_prob,
1442 const int16_t qmul[2],
1443 const uint8_t scan[16])
1444 {
1445 736 return decode_block_coeffs_internal(r, block, probs, i,
1446 token_prob, qmul, scan, IS_VP7);
1447 }
1448
1449 #ifndef vp8_decode_block_coeffs_internal
1450 393496 static int vp8_decode_block_coeffs_internal(VPXRangeCoder *r,
1451 int16_t block[16],
1452 uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1453 int i, const uint8_t *token_prob,
1454 const int16_t qmul[2])
1455 {
1456 393496 return decode_block_coeffs_internal(r, block, probs, i,
1457 token_prob, qmul, ff_zigzag_scan, IS_VP8);
1458 }
1459 #endif
1460
1461 /**
1462 * @param c arithmetic bitstream reader context
1463 * @param block destination for block coefficients
1464 * @param probs probabilities to use when reading trees from the bitstream
1465 * @param i initial coeff index, 0 unless a separate DC block is coded
1466 * @param zero_nhood the initial prediction context for number of surrounding
1467 * all-zero blocks (only left/top, so 0-2)
1468 * @param qmul array holding the dc/ac dequant factor at position 0/1
1469 * @param scan scan pattern (VP7 only)
1470 *
1471 * @return 0 if no coeffs were decoded
1472 * otherwise, the index of the last coeff decoded plus one
1473 */
1474 static av_always_inline
1475 2064145 int decode_block_coeffs(VPXRangeCoder *c, int16_t block[16],
1476 uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1477 int i, int zero_nhood, const int16_t qmul[2],
1478 const uint8_t scan[16], int vp7)
1479 {
1480 2064145 const uint8_t *token_prob = probs[i][zero_nhood];
1481
2/2
✓ Branch 1 taken 1669913 times.
✓ Branch 2 taken 394232 times.
2064145 if (!vpx_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
1482 1669913 return 0;
1483 736 return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,
1484 token_prob, qmul, scan)
1485
2/2
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 393496 times.
394968 : vp8_decode_block_coeffs_internal(c, block, probs, i,
1486 token_prob, qmul);
1487 }
1488
1489 static av_always_inline
1490 83859 void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VPXRangeCoder *c,
1491 VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9],
1492 int is_vp7)
1493 {
1494 83859 int i, x, y, luma_start = 0, luma_ctx = 3;
1495 83859 int nnz_pred, nnz, nnz_total = 0;
1496 83859 int segment = mb->segment;
1497 83859 int block_dc = 0;
1498
1499
6/6
✓ Branch 0 taken 66592 times.
✓ Branch 1 taken 17267 times.
✓ Branch 2 taken 59585 times.
✓ Branch 3 taken 7007 times.
✓ Branch 4 taken 44522 times.
✓ Branch 5 taken 15063 times.
83859 if (mb->mode != MODE_I4x4 && (is_vp7 || mb->mode != VP8_MVMODE_SPLIT)) {
1500 51529 nnz_pred = t_nnz[8] + l_nnz[8];
1501
1502 // decode DC values and do hadamard
1503 51529 nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,
1504 51529 nnz_pred, s->qmat[segment].luma_dc_qmul,
1505 ff_zigzag_scan, is_vp7);
1506 51529 l_nnz[8] = t_nnz[8] = !!nnz;
1507
1508
4/4
✓ Branch 0 taken 7007 times.
✓ Branch 1 taken 44522 times.
✓ Branch 2 taken 6820 times.
✓ Branch 3 taken 187 times.
51529 if (is_vp7 && mb->mode > MODE_I4x4) {
1509 6820 nnz |= inter_predict_dc(td->block_dc,
1510 6820 s->inter_dc_pred[mb->ref_frame - 1]);
1511 }
1512
1513
2/2
✓ Branch 0 taken 34790 times.
✓ Branch 1 taken 16739 times.
51529 if (nnz) {
1514 34790 nnz_total += nnz;
1515 34790 block_dc = 1;
1516
2/2
✓ Branch 0 taken 10310 times.
✓ Branch 1 taken 24480 times.
34790 if (nnz == 1)
1517 10310 s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);
1518 else
1519 24480 s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
1520 }
1521 51529 luma_start = 1;
1522 51529 luma_ctx = 0;
1523 }
1524
1525 // luma blocks
1526
2/2
✓ Branch 0 taken 335436 times.
✓ Branch 1 taken 83859 times.
419295 for (y = 0; y < 4; y++)
1527
2/2
✓ Branch 0 taken 1341744 times.
✓ Branch 1 taken 335436 times.
1677180 for (x = 0; x < 4; x++) {
1528 1341744 nnz_pred = l_nnz[y] + t_nnz[x];
1529 1341744 nnz = decode_block_coeffs(c, td->block[y][x],
1530 1341744 s->prob->token[luma_ctx],
1531 luma_start, nnz_pred,
1532 1341744 s->qmat[segment].luma_qmul,
1533 1341744 s->prob[0].scan, is_vp7);
1534 /* nnz+block_dc may be one more than the actual last index,
1535 * but we don't care */
1536 1341744 td->non_zero_count_cache[y][x] = nnz + block_dc;
1537 1341744 t_nnz[x] = l_nnz[y] = !!nnz;
1538 1341744 nnz_total += nnz;
1539 }
1540
1541 // chroma blocks
1542 // TODO: what to do about dimensions? 2nd dim for luma is x,
1543 // but for chroma it's (y<<1)|x
1544
2/2
✓ Branch 0 taken 167718 times.
✓ Branch 1 taken 83859 times.
251577 for (i = 4; i < 6; i++)
1545
2/2
✓ Branch 0 taken 335436 times.
✓ Branch 1 taken 167718 times.
503154 for (y = 0; y < 2; y++)
1546
2/2
✓ Branch 0 taken 670872 times.
✓ Branch 1 taken 335436 times.
1006308 for (x = 0; x < 2; x++) {
1547 670872 nnz_pred = l_nnz[i + 2 * y] + t_nnz[i + 2 * x];
1548 670872 nnz = decode_block_coeffs(c, td->block[i][(y << 1) + x],
1549 670872 s->prob->token[2], 0, nnz_pred,
1550 670872 s->qmat[segment].chroma_qmul,
1551 670872 s->prob[0].scan, is_vp7);
1552 670872 td->non_zero_count_cache[i][(y << 1) + x] = nnz;
1553 670872 t_nnz[i + 2 * x] = l_nnz[i + 2 * y] = !!nnz;
1554 670872 nnz_total += nnz;
1555 }
1556
1557 // if there were no coded coeffs despite the macroblock not being marked skip,
1558 // we MUST not do the inner loop filter and should not do IDCT
1559 // Since skip isn't used for bitstream prediction, just manually set it.
1560
2/2
✓ Branch 0 taken 6895 times.
✓ Branch 1 taken 76964 times.
83859 if (!nnz_total)
1561 6895 mb->skip = 1;
1562 83859 }
1563
1564 static av_always_inline
1565 424829 void backup_mb_border(uint8_t *top_border, const uint8_t *src_y,
1566 const uint8_t *src_cb, const uint8_t *src_cr,
1567 ptrdiff_t linesize, ptrdiff_t uvlinesize, int simple)
1568 {
1569 424829 AV_COPY128(top_border, src_y + 15 * linesize);
1570
2/2
✓ Branch 0 taken 417107 times.
✓ Branch 1 taken 7722 times.
424829 if (!simple) {
1571 417107 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
1572 417107 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
1573 }
1574 424829 }
1575
1576 static av_always_inline
1577 84288 void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb,
1578 uint8_t *src_cr, ptrdiff_t linesize, ptrdiff_t uvlinesize, int mb_x,
1579 int mb_y, int mb_width, int simple, int xchg)
1580 {
1581 84288 uint8_t *top_border_m1 = top_border - 32; // for TL prediction
1582 84288 src_y -= linesize;
1583 84288 src_cb -= uvlinesize;
1584 84288 src_cr -= uvlinesize;
1585
1586 #define XCHG(a, b, xchg) \
1587 do { \
1588 if (xchg) \
1589 AV_SWAP64(b, a); \
1590 else \
1591 AV_COPY64(b, a); \
1592 } while (0)
1593
1594
2/2
✓ Branch 0 taken 42144 times.
✓ Branch 1 taken 42144 times.
84288 XCHG(top_border_m1 + 8, src_y - 8, xchg);
1595
2/2
✓ Branch 0 taken 42144 times.
✓ Branch 1 taken 42144 times.
84288 XCHG(top_border, src_y, xchg);
1596 84288 XCHG(top_border + 8, src_y + 8, 1);
1597
2/2
✓ Branch 0 taken 80840 times.
✓ Branch 1 taken 3448 times.
84288 if (mb_x < mb_width - 1)
1598 80840 XCHG(top_border + 32, src_y + 16, 1);
1599
1600 // only copy chroma for normal loop filter
1601 // or to initialize the top row to 127
1602
3/4
✓ Branch 0 taken 3082 times.
✓ Branch 1 taken 81206 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3082 times.
84288 if (!simple || !mb_y) {
1603
2/2
✓ Branch 0 taken 40603 times.
✓ Branch 1 taken 40603 times.
81206 XCHG(top_border_m1 + 16, src_cb - 8, xchg);
1604
2/2
✓ Branch 0 taken 40603 times.
✓ Branch 1 taken 40603 times.
81206 XCHG(top_border_m1 + 24, src_cr - 8, xchg);
1605 81206 XCHG(top_border + 16, src_cb, 1);
1606 81206 XCHG(top_border + 24, src_cr, 1);
1607 }
1608 84288 }
1609
1610 static av_always_inline
1611 73112 int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
1612 {
1613
2/2
✓ Branch 0 taken 3065 times.
✓ Branch 1 taken 70047 times.
73112 if (!mb_x)
1614
2/2
✓ Branch 0 taken 2865 times.
✓ Branch 1 taken 200 times.
3065 return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
1615 else
1616
2/2
✓ Branch 0 taken 64799 times.
✓ Branch 1 taken 5248 times.
70047 return mb_y ? mode : LEFT_DC_PRED8x8;
1617 }
1618
1619 static av_always_inline
1620 1815 int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
1621 {
1622
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1814 times.
1815 if (!mb_x)
1623
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 return mb_y ? VERT_PRED8x8 : (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8);
1624 else
1625
2/2
✓ Branch 0 taken 1812 times.
✓ Branch 1 taken 2 times.
1814 return mb_y ? mode : HOR_PRED8x8;
1626 }
1627
1628 static av_always_inline
1629 97291 int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
1630 {
1631
4/5
✓ Branch 0 taken 73112 times.
✓ Branch 1 taken 5774 times.
✓ Branch 2 taken 16590 times.
✓ Branch 3 taken 1815 times.
✗ Branch 4 not taken.
97291 switch (mode) {
1632 73112 case DC_PRED8x8:
1633 73112 return check_dc_pred8x8_mode(mode, mb_x, mb_y);
1634 5774 case VERT_PRED8x8:
1635
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 5708 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
5774 return !mb_y ? (vp7 ? DC_128_PRED8x8 : DC_127_PRED8x8) : mode;
1636 16590 case HOR_PRED8x8:
1637
3/4
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 16448 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 142 times.
16590 return !mb_x ? (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8) : mode;
1638 1815 case PLANE_PRED8x8: /* TM */
1639 1815 return check_tm_pred8x8_mode(mode, mb_x, mb_y, vp7);
1640 }
1641 return mode;
1642 }
1643
1644 static av_always_inline
1645 49295 int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
1646 {
1647
2/2
✓ Branch 0 taken 1362 times.
✓ Branch 1 taken 47933 times.
49295 if (!mb_x) {
1648
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1361 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1362 return mb_y ? VERT_VP8_PRED : (vp7 ? DC_128_PRED : DC_129_PRED);
1649 } else {
1650
2/2
✓ Branch 0 taken 46524 times.
✓ Branch 1 taken 1409 times.
47933 return mb_y ? mode : HOR_VP8_PRED;
1651 }
1652 }
1653
1654 static av_always_inline
1655 283664 int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y,
1656 int *copy_buf, int vp7)
1657 {
1658
6/7
✓ Branch 0 taken 13652 times.
✓ Branch 1 taken 21380 times.
✓ Branch 2 taken 19497 times.
✓ Branch 3 taken 12361 times.
✓ Branch 4 taken 49295 times.
✓ Branch 5 taken 167479 times.
✗ Branch 6 not taken.
283664 switch (mode) {
1659 13652 case VERT_PRED:
1660
3/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 13557 times.
✓ Branch 2 taken 95 times.
✗ Branch 3 not taken.
13652 if (!mb_x && mb_y) {
1661 95 *copy_buf = 1;
1662 95 return mode;
1663 }
1664 av_fallthrough;
1665 case DIAG_DOWN_LEFT_PRED:
1666 case VERT_LEFT_PRED:
1667
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 34913 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
34937 return !mb_y ? (vp7 ? DC_128_PRED : DC_127_PRED) : mode;
1668 19497 case HOR_PRED:
1669
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 19369 times.
19497 if (!mb_y) {
1670 128 *copy_buf = 1;
1671 128 return mode;
1672 }
1673 av_fallthrough;
1674 case HOR_UP_PRED:
1675
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 31686 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
31730 return !mb_x ? (vp7 ? DC_128_PRED : DC_129_PRED) : mode;
1676 49295 case TM_VP8_PRED:
1677 49295 return check_tm_pred4x4_mode(mode, mb_x, mb_y, vp7);
1678 167479 case DC_PRED: /* 4x4 DC doesn't use the same "H.264-style" exceptions
1679 * as 16x16/8x8 DC */
1680 case DIAG_DOWN_RIGHT_PRED:
1681 case VERT_RIGHT_PRED:
1682 case HOR_DOWN_PRED:
1683
4/4
✓ Branch 0 taken 166905 times.
✓ Branch 1 taken 574 times.
✓ Branch 2 taken 566 times.
✓ Branch 3 taken 166339 times.
167479 if (!mb_y || !mb_x)
1684 1140 *copy_buf = 1;
1685 167479 return mode;
1686 }
1687 return mode;
1688 }
1689
1690 static av_always_inline
1691 57510 void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
1692 VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
1693 {
1694 int x, y, mode, nnz;
1695 uint32_t tr;
1696
1697 /* for the first row, we need to run xchg_mb_border to init the top edge
1698 * to 127 otherwise, skip it if we aren't going to deblock */
1699
6/8
✓ Branch 0 taken 54157 times.
✓ Branch 1 taken 3353 times.
✓ Branch 2 taken 12013 times.
✓ Branch 3 taken 42144 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12013 times.
✓ Branch 6 taken 42144 times.
✗ Branch 7 not taken.
57510 if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1700 42144 xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1701 42144 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1702 42144 s->filter.simple, 1);
1703
1704
2/2
✓ Branch 0 taken 39781 times.
✓ Branch 1 taken 17729 times.
57510 if (mb->mode < MODE_I4x4) {
1705 39781 mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y, is_vp7);
1706 39781 s->hpc.pred16x16[mode](dst[0], s->linesize);
1707 } else {
1708 17729 uint8_t *ptr = dst[0];
1709 17729 const uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1710
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17696 times.
17729 const uint8_t lo = is_vp7 ? 128 : 127;
1711
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 17696 times.
17729 const uint8_t hi = is_vp7 ? 128 : 129;
1712 17729 const uint8_t tr_top[4] = { lo, lo, lo, lo };
1713
1714 // all blocks on the right edge of the macroblock use bottom edge
1715 // the top macroblock for their topright edge
1716 17729 const uint8_t *tr_right = ptr - s->linesize + 16;
1717
1718 // if we're on the right edge of the frame, said edge is extended
1719 // from the top macroblock
1720
4/4
✓ Branch 0 taken 17098 times.
✓ Branch 1 taken 631 times.
✓ Branch 2 taken 636 times.
✓ Branch 3 taken 16462 times.
17729 if (mb_y && mb_x == s->mb_width - 1) {
1721 636 tr = tr_right[-1] * 0x01010101u;
1722 636 tr_right = (uint8_t *) &tr;
1723 }
1724
1725
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 17266 times.
17729 if (mb->skip)
1726 463 AV_ZERO128(td->non_zero_count_cache);
1727
1728
2/2
✓ Branch 0 taken 70916 times.
✓ Branch 1 taken 17729 times.
88645 for (y = 0; y < 4; y++) {
1729 70916 const uint8_t *topright = ptr + 4 - s->linesize;
1730
2/2
✓ Branch 0 taken 283664 times.
✓ Branch 1 taken 70916 times.
354580 for (x = 0; x < 4; x++) {
1731 283664 int copy = 0;
1732 283664 ptrdiff_t linesize = s->linesize;
1733 283664 uint8_t *dst = ptr + 4 * x;
1734 283664 LOCAL_ALIGNED(4, uint8_t, copy_dst, [5 * 8]);
1735
1736
6/6
✓ Branch 0 taken 212748 times.
✓ Branch 1 taken 70916 times.
✓ Branch 2 taken 53187 times.
✓ Branch 3 taken 159561 times.
✓ Branch 4 taken 4417 times.
✓ Branch 5 taken 119686 times.
283664 if ((y == 0 || x == 3) && mb_y == 0) {
1737 4417 topright = tr_top;
1738
2/2
✓ Branch 0 taken 68392 times.
✓ Branch 1 taken 210855 times.
279247 } else if (x == 3)
1739 68392 topright = tr_right;
1740
1741 283664 mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x,
1742 mb_y + y, &copy, is_vp7);
1743
2/2
✓ Branch 0 taken 1363 times.
✓ Branch 1 taken 282301 times.
283664 if (copy) {
1744 1363 dst = copy_dst + 12;
1745 1363 linesize = 8;
1746
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 661 times.
1363 if (!(mb_y + y)) {
1747 702 copy_dst[3] = lo;
1748 702 AV_WN32A(copy_dst + 4, lo * 0x01010101U);
1749 } else {
1750 661 AV_COPY32(copy_dst + 4, ptr + 4 * x - s->linesize);
1751
1/2
✓ Branch 0 taken 661 times.
✗ Branch 1 not taken.
661 if (!(mb_x + x)) {
1752 661 copy_dst[3] = hi;
1753 } else {
1754 copy_dst[3] = ptr[4 * x - s->linesize - 1];
1755 }
1756 }
1757
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 661 times.
1363 if (!(mb_x + x)) {
1758 702 copy_dst[11] =
1759 702 copy_dst[19] =
1760 702 copy_dst[27] =
1761 702 copy_dst[35] = hi;
1762 } else {
1763 661 copy_dst[11] = ptr[4 * x - 1];
1764 661 copy_dst[19] = ptr[4 * x + s->linesize - 1];
1765 661 copy_dst[27] = ptr[4 * x + s->linesize * 2 - 1];
1766 661 copy_dst[35] = ptr[4 * x + s->linesize * 3 - 1];
1767 }
1768 }
1769 283664 s->hpc.pred4x4[mode](dst, topright, linesize);
1770
2/2
✓ Branch 0 taken 1363 times.
✓ Branch 1 taken 282301 times.
283664 if (copy) {
1771 1363 AV_COPY32(ptr + 4 * x, copy_dst + 12);
1772 1363 AV_COPY32(ptr + 4 * x + s->linesize, copy_dst + 20);
1773 1363 AV_COPY32(ptr + 4 * x + s->linesize * 2, copy_dst + 28);
1774 1363 AV_COPY32(ptr + 4 * x + s->linesize * 3, copy_dst + 36);
1775 }
1776
1777 283664 nnz = td->non_zero_count_cache[y][x];
1778
2/2
✓ Branch 0 taken 122848 times.
✓ Branch 1 taken 160816 times.
283664 if (nnz) {
1779
2/2
✓ Branch 0 taken 35168 times.
✓ Branch 1 taken 87680 times.
122848 if (nnz == 1)
1780 35168 s->vp8dsp.vp8_idct_dc_add(ptr + 4 * x,
1781 35168 td->block[y][x], s->linesize);
1782 else
1783 87680 s->vp8dsp.vp8_idct_add(ptr + 4 * x,
1784 87680 td->block[y][x], s->linesize);
1785 }
1786 283664 topright += 4;
1787 }
1788
1789 70916 ptr += 4 * s->linesize;
1790 70916 intra4x4 += 4;
1791 }
1792 }
1793
1794 57510 mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode,
1795 mb_x, mb_y, is_vp7);
1796 57510 s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
1797 57510 s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
1798
1799
6/8
✓ Branch 0 taken 54157 times.
✓ Branch 1 taken 3353 times.
✓ Branch 2 taken 12013 times.
✓ Branch 3 taken 42144 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12013 times.
✓ Branch 6 taken 42144 times.
✗ Branch 7 not taken.
57510 if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1800 42144 xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1801 42144 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1802 42144 s->filter.simple, 0);
1803 57510 }
1804
1805 static const uint8_t subpel_idx[3][8] = {
1806 { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
1807 // also function pointer index
1808 { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
1809 { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
1810 };
1811
1812 /**
1813 * luma MC function
1814 *
1815 * @param s VP8 decoding context
1816 * @param dst target buffer for block data at block position
1817 * @param ref reference picture buffer at origin (0, 0)
1818 * @param mv motion vector (relative to block position) to get pixel data from
1819 * @param x_off horizontal position of block from origin (0, 0)
1820 * @param y_off vertical position of block from origin (0, 0)
1821 * @param block_w width of block (16, 8 or 4)
1822 * @param block_h height of block (always same as block_w)
1823 * @param width width of src/dst plane data
1824 * @param height height of src/dst plane data
1825 * @param linesize size of a single line of plane data, including padding
1826 * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1827 */
1828 static av_always_inline
1829 504659 void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
1830 const ProgressFrame *ref, const VP8mv *mv,
1831 int x_off, int y_off, int block_w, int block_h,
1832 int width, int height, ptrdiff_t linesize,
1833 vp8_mc_func mc_func[3][3])
1834 {
1835 504659 const uint8_t *src = ref->f->data[0];
1836
1837
2/2
✓ Branch 0 taken 124899 times.
✓ Branch 1 taken 379760 times.
504659 if (AV_RN32A(mv)) {
1838 124899 ptrdiff_t src_linesize = linesize;
1839
1840 124899 int mx = (mv->x * 2) & 7, mx_idx = subpel_idx[0][mx];
1841 124899 int my = (mv->y * 2) & 7, my_idx = subpel_idx[0][my];
1842
1843 124899 x_off += mv->x >> 2;
1844 124899 y_off += mv->y >> 2;
1845
1846 // edge emulation
1847 124899 ff_progress_frame_await(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4);
1848 124899 src += y_off * linesize + x_off;
1849
6/6
✓ Branch 0 taken 123700 times.
✓ Branch 1 taken 1199 times.
✓ Branch 2 taken 120199 times.
✓ Branch 3 taken 3501 times.
✓ Branch 4 taken 118706 times.
✓ Branch 5 taken 1493 times.
124899 if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
1850
2/2
✓ Branch 0 taken 3043 times.
✓ Branch 1 taken 115663 times.
118706 y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
1851 9236 s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1852 9236 src - my_idx * linesize - mx_idx,
1853 EDGE_EMU_LINESIZE, linesize,
1854 9236 block_w + subpel_idx[1][mx],
1855 9236 block_h + subpel_idx[1][my],
1856 x_off - mx_idx, y_off - my_idx,
1857 width, height);
1858 9236 src = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1859 9236 src_linesize = EDGE_EMU_LINESIZE;
1860 }
1861 124899 mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
1862 } else {
1863 379760 ff_progress_frame_await(ref, (3 + y_off + block_h) >> 4);
1864 379760 mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
1865 linesize, block_h, 0, 0);
1866 }
1867 504659 }
1868
1869 /**
1870 * chroma MC function
1871 *
1872 * @param s VP8 decoding context
1873 * @param dst1 target buffer for block data at block position (U plane)
1874 * @param dst2 target buffer for block data at block position (V plane)
1875 * @param ref reference picture buffer at origin (0, 0)
1876 * @param mv motion vector (relative to block position) to get pixel data from
1877 * @param x_off horizontal position of block from origin (0, 0)
1878 * @param y_off vertical position of block from origin (0, 0)
1879 * @param block_w width of block (16, 8 or 4)
1880 * @param block_h height of block (always same as block_w)
1881 * @param width width of src/dst plane data
1882 * @param height height of src/dst plane data
1883 * @param linesize size of a single line of plane data, including padding
1884 * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1885 */
1886 static av_always_inline
1887 425279 void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
1888 uint8_t *dst2, const ProgressFrame *ref, const VP8mv *mv,
1889 int x_off, int y_off, int block_w, int block_h,
1890 int width, int height, ptrdiff_t linesize,
1891 vp8_mc_func mc_func[3][3])
1892 {
1893 425279 const uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
1894
1895
2/2
✓ Branch 0 taken 79190 times.
✓ Branch 1 taken 346089 times.
425279 if (AV_RN32A(mv)) {
1896 79190 int mx = mv->x & 7, mx_idx = subpel_idx[0][mx];
1897 79190 int my = mv->y & 7, my_idx = subpel_idx[0][my];
1898
1899 79190 x_off += mv->x >> 3;
1900 79190 y_off += mv->y >> 3;
1901
1902 // edge emulation
1903 79190 src1 += y_off * linesize + x_off;
1904 79190 src2 += y_off * linesize + x_off;
1905 79190 ff_progress_frame_await(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3);
1906
6/6
✓ Branch 0 taken 77874 times.
✓ Branch 1 taken 1316 times.
✓ Branch 2 taken 74583 times.
✓ Branch 3 taken 3291 times.
✓ Branch 4 taken 73050 times.
✓ Branch 5 taken 1533 times.
79190 if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
1907
2/2
✓ Branch 0 taken 3017 times.
✓ Branch 1 taken 70033 times.
73050 y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
1908 9157 s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1909 9157 src1 - my_idx * linesize - mx_idx,
1910 EDGE_EMU_LINESIZE, linesize,
1911 9157 block_w + subpel_idx[1][mx],
1912 9157 block_h + subpel_idx[1][my],
1913 x_off - mx_idx, y_off - my_idx, width, height);
1914 9157 src1 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1915 9157 mc_func[my_idx][mx_idx](dst1, linesize, src1, EDGE_EMU_LINESIZE, block_h, mx, my);
1916
1917 9157 s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1918 9157 src2 - my_idx * linesize - mx_idx,
1919 EDGE_EMU_LINESIZE, linesize,
1920 9157 block_w + subpel_idx[1][mx],
1921 9157 block_h + subpel_idx[1][my],
1922 x_off - mx_idx, y_off - my_idx, width, height);
1923 9157 src2 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1924 9157 mc_func[my_idx][mx_idx](dst2, linesize, src2, EDGE_EMU_LINESIZE, block_h, mx, my);
1925 } else {
1926 70033 mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
1927 70033 mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
1928 }
1929 } else {
1930 346089 ff_progress_frame_await(ref, (3 + y_off + block_h) >> 3);
1931 346089 mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
1932 346089 mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
1933 }
1934 425279 }
1935
1936 static av_always_inline
1937 398819 void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
1938 const ProgressFrame *ref_frame, int x_off, int y_off,
1939 int bx_off, int by_off, int block_w, int block_h,
1940 int width, int height, const VP8mv *mv)
1941 {
1942 398819 VP8mv uvmv = *mv;
1943
1944 /* Y */
1945 398819 vp8_mc_luma(s, td, dst[0] + by_off * s->linesize + bx_off,
1946 ref_frame, mv, x_off + bx_off, y_off + by_off,
1947 block_w, block_h, width, height, s->linesize,
1948 398819 s->put_pixels_tab[block_w == 8]);
1949
1950 /* U/V */
1951
2/2
✓ Branch 0 taken 4775 times.
✓ Branch 1 taken 394044 times.
398819 if (s->profile == 3) {
1952 /* this block only applies VP8; it is safe to check
1953 * only the profile, as VP7 profile <= 1 */
1954 4775 uvmv.x &= ~7;
1955 4775 uvmv.y &= ~7;
1956 }
1957 398819 x_off >>= 1;
1958 398819 y_off >>= 1;
1959 398819 bx_off >>= 1;
1960 398819 by_off >>= 1;
1961 398819 width >>= 1;
1962 398819 height >>= 1;
1963 398819 block_w >>= 1;
1964 398819 block_h >>= 1;
1965 398819 vp8_mc_chroma(s, td, dst[1] + by_off * s->uvlinesize + bx_off,
1966 398819 dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
1967 &uvmv, x_off + bx_off, y_off + by_off,
1968 block_w, block_h, width, height, s->uvlinesize,
1969
2/2
✓ Branch 0 taken 20950 times.
✓ Branch 1 taken 377869 times.
398819 s->put_pixels_tab[1 + (block_w == 4)]);
1970 398819 }
1971
1972 /* Fetch pixels for estimated mv 4 macroblocks ahead.
1973 * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1974 static av_always_inline
1975 1331667 void prefetch_motion(const VP8Context *s, const VP8Macroblock *mb,
1976 int mb_x, int mb_y, int mb_xy, int ref)
1977 {
1978 /* Don't prefetch refs that haven't been used very often this frame. */
1979
2/2
✓ Branch 0 taken 479331 times.
✓ Branch 1 taken 852336 times.
1331667 if (s->ref_count[ref - 1] > (mb_xy >> 5)) {
1980 479331 int x_off = mb_x << 4, y_off = mb_y << 4;
1981 479331 int mx = (mb->mv.x >> 2) + x_off + 8;
1982 479331 int my = (mb->mv.y >> 2) + y_off;
1983 479331 uint8_t **src = s->framep[ref]->tf.f->data;
1984 479331 int off = mx + (my + (mb_x & 3) * 4) * s->linesize + 64;
1985 /* For threading, a ff_thread_await_progress here might be useful, but
1986 * it actually slows down the decoder. Since a bad prefetch doesn't
1987 * generate bad decoder output, we don't run it here. */
1988 479331 s->vdsp.prefetch(src[0] + off, s->linesize, 4);
1989 479331 off = (mx >> 1) + ((my >> 1) + (mb_x & 7)) * s->uvlinesize + 64;
1990 479331 s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
1991 }
1992 1331667 }
1993
1994 /**
1995 * Apply motion vectors to prediction buffer, chapter 18.
1996 */
1997 static av_always_inline
1998 386379 void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
1999 VP8Macroblock *mb, int mb_x, int mb_y)
2000 {
2001 386379 int x_off = mb_x << 4, y_off = mb_y << 4;
2002 386379 int width = 16 * s->mb_width, height = 16 * s->mb_height;
2003 386379 const ProgressFrame *ref = &s->framep[mb->ref_frame]->tf;
2004 386379 const VP8mv *bmv = mb->bmv;
2005
2006
5/6
✓ Branch 0 taken 368049 times.
✓ Branch 1 taken 6615 times.
✓ Branch 2 taken 4910 times.
✓ Branch 3 taken 3135 times.
✓ Branch 4 taken 3670 times.
✗ Branch 5 not taken.
386379 switch (mb->partitioning) {
2007 368049 case VP8_SPLITMVMODE_NONE:
2008 368049 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2009 368049 0, 0, 16, 16, width, height, &mb->mv);
2010 368049 break;
2011 6615 case VP8_SPLITMVMODE_4x4: {
2012 int x, y;
2013 VP8mv uvmv;
2014
2015 /* Y */
2016
2/2
✓ Branch 0 taken 26460 times.
✓ Branch 1 taken 6615 times.
33075 for (y = 0; y < 4; y++) {
2017
2/2
✓ Branch 0 taken 105840 times.
✓ Branch 1 taken 26460 times.
132300 for (x = 0; x < 4; x++) {
2018 105840 vp8_mc_luma(s, td, dst[0] + 4 * y * s->linesize + x * 4,
2019 105840 ref, &bmv[4 * y + x],
2020 105840 4 * x + x_off, 4 * y + y_off, 4, 4,
2021 width, height, s->linesize,
2022 105840 s->put_pixels_tab[2]);
2023 }
2024 }
2025
2026 /* U/V */
2027 6615 x_off >>= 1;
2028 6615 y_off >>= 1;
2029 6615 width >>= 1;
2030 6615 height >>= 1;
2031
2/2
✓ Branch 0 taken 13230 times.
✓ Branch 1 taken 6615 times.
19845 for (y = 0; y < 2; y++) {
2032
2/2
✓ Branch 0 taken 26460 times.
✓ Branch 1 taken 13230 times.
39690 for (x = 0; x < 2; x++) {
2033 26460 uvmv.x = mb->bmv[2 * y * 4 + 2 * x ].x +
2034 26460 mb->bmv[2 * y * 4 + 2 * x + 1].x +
2035 26460 mb->bmv[(2 * y + 1) * 4 + 2 * x ].x +
2036 26460 mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].x;
2037 26460 uvmv.y = mb->bmv[2 * y * 4 + 2 * x ].y +
2038 26460 mb->bmv[2 * y * 4 + 2 * x + 1].y +
2039 26460 mb->bmv[(2 * y + 1) * 4 + 2 * x ].y +
2040 26460 mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].y;
2041 26460 uvmv.x = (uvmv.x + 2 + FF_SIGNBIT(uvmv.x)) >> 2;
2042 26460 uvmv.y = (uvmv.y + 2 + FF_SIGNBIT(uvmv.y)) >> 2;
2043
2/2
✓ Branch 0 taken 492 times.
✓ Branch 1 taken 25968 times.
26460 if (s->profile == 3) {
2044 492 uvmv.x &= ~7;
2045 492 uvmv.y &= ~7;
2046 }
2047 26460 vp8_mc_chroma(s, td, dst[1] + 4 * y * s->uvlinesize + x * 4,
2048 26460 dst[2] + 4 * y * s->uvlinesize + x * 4, ref,
2049 26460 &uvmv, 4 * x + x_off, 4 * y + y_off, 4, 4,
2050 width, height, s->uvlinesize,
2051 26460 s->put_pixels_tab[2]);
2052 }
2053 }
2054 6615 break;
2055 }
2056 4910 case VP8_SPLITMVMODE_16x8:
2057 4910 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2058 0, 0, 16, 8, width, height, &bmv[0]);
2059 4910 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2060 0, 8, 16, 8, width, height, &bmv[1]);
2061 4910 break;
2062 3135 case VP8_SPLITMVMODE_8x16:
2063 3135 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2064 0, 0, 8, 16, width, height, &bmv[0]);
2065 3135 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2066 8, 0, 8, 16, width, height, &bmv[1]);
2067 3135 break;
2068 3670 case VP8_SPLITMVMODE_8x8:
2069 3670 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2070 0, 0, 8, 8, width, height, &bmv[0]);
2071 3670 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2072 8, 0, 8, 8, width, height, &bmv[1]);
2073 3670 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2074 0, 8, 8, 8, width, height, &bmv[2]);
2075 3670 vp8_mc_part(s, td, dst, ref, x_off, y_off,
2076 8, 8, 8, 8, width, height, &bmv[3]);
2077 3670 break;
2078 }
2079 386379 }
2080
2081 static av_always_inline
2082 76964 void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
2083 const VP8Macroblock *mb)
2084 {
2085 int x, y, ch;
2086
2087
2/2
✓ Branch 0 taken 59698 times.
✓ Branch 1 taken 17266 times.
76964 if (mb->mode != MODE_I4x4) {
2088 59698 uint8_t *y_dst = dst[0];
2089
2/2
✓ Branch 0 taken 238792 times.
✓ Branch 1 taken 59698 times.
298490 for (y = 0; y < 4; y++) {
2090 238792 uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[y]);
2091
2/2
✓ Branch 0 taken 176071 times.
✓ Branch 1 taken 62721 times.
238792 if (nnz4) {
2092
2/2
✓ Branch 0 taken 45018 times.
✓ Branch 1 taken 131053 times.
176071 if (nnz4 & ~0x01010101) {
2093
1/2
✓ Branch 0 taken 156435 times.
✗ Branch 1 not taken.
156435 for (x = 0; x < 4; x++) {
2094
2/2
✓ Branch 0 taken 54199 times.
✓ Branch 1 taken 102236 times.
156435 if ((uint8_t) nnz4 == 1)
2095 54199 s->vp8dsp.vp8_idct_dc_add(y_dst + 4 * x,
2096 54199 td->block[y][x],
2097 s->linesize);
2098
2/2
✓ Branch 0 taken 74719 times.
✓ Branch 1 taken 27517 times.
102236 else if ((uint8_t) nnz4 > 1)
2099 74719 s->vp8dsp.vp8_idct_add(y_dst + 4 * x,
2100 74719 td->block[y][x],
2101 s->linesize);
2102 156435 nnz4 >>= 8;
2103
2/2
✓ Branch 0 taken 45018 times.
✓ Branch 1 taken 111417 times.
156435 if (!nnz4)
2104 45018 break;
2105 }
2106 } else {
2107 131053 s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
2108 }
2109 }
2110 238792 y_dst += 4 * s->linesize;
2111 }
2112 }
2113
2114
2/2
✓ Branch 0 taken 153928 times.
✓ Branch 1 taken 76964 times.
230892 for (ch = 0; ch < 2; ch++) {
2115 153928 uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4 + ch]);
2116
2/2
✓ Branch 0 taken 93764 times.
✓ Branch 1 taken 60164 times.
153928 if (nnz4) {
2117 60164 uint8_t *ch_dst = dst[1 + ch];
2118
2/2
✓ Branch 0 taken 30070 times.
✓ Branch 1 taken 30094 times.
60164 if (nnz4 & ~0x01010101) {
2119
1/2
✓ Branch 0 taken 55740 times.
✗ Branch 1 not taken.
55740 for (y = 0; y < 2; y++) {
2120
2/2
✓ Branch 0 taken 106743 times.
✓ Branch 1 taken 25670 times.
132413 for (x = 0; x < 2; x++) {
2121
2/2
✓ Branch 0 taken 16084 times.
✓ Branch 1 taken 90659 times.
106743 if ((uint8_t) nnz4 == 1)
2122 16084 s->vp8dsp.vp8_idct_dc_add(ch_dst + 4 * x,
2123 16084 td->block[4 + ch][(y << 1) + x],
2124 s->uvlinesize);
2125
2/2
✓ Branch 0 taken 67150 times.
✓ Branch 1 taken 23509 times.
90659 else if ((uint8_t) nnz4 > 1)
2126 67150 s->vp8dsp.vp8_idct_add(ch_dst + 4 * x,
2127 67150 td->block[4 + ch][(y << 1) + x],
2128 s->uvlinesize);
2129 106743 nnz4 >>= 8;
2130
2/2
✓ Branch 0 taken 30070 times.
✓ Branch 1 taken 76673 times.
106743 if (!nnz4)
2131 30070 goto chroma_idct_end;
2132 }
2133 25670 ch_dst += 4 * s->uvlinesize;
2134 }
2135 } else {
2136 30094 s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4 + ch], s->uvlinesize);
2137 }
2138 }
2139 153928 chroma_idct_end:
2140 ;
2141 }
2142 76964 }
2143
2144 static av_always_inline
2145 424829 void filter_level_for_mb(const VP8Context *s, const VP8Macroblock *mb,
2146 VP8FilterStrength *f, int is_vp7)
2147 {
2148 int interior_limit, filter_level;
2149
2150
2/2
✓ Branch 0 taken 102219 times.
✓ Branch 1 taken 322610 times.
424829 if (s->segmentation.enabled) {
2151 102219 filter_level = s->segmentation.filter_level[mb->segment];
2152
2/2
✓ Branch 0 taken 98895 times.
✓ Branch 1 taken 3324 times.
102219 if (!s->segmentation.absolute_vals)
2153 98895 filter_level += s->filter.level;
2154 } else
2155 322610 filter_level = s->filter.level;
2156
2157
2/2
✓ Branch 0 taken 417035 times.
✓ Branch 1 taken 7794 times.
424829 if (s->lf_delta.enabled) {
2158 417035 filter_level += s->lf_delta.ref[mb->ref_frame];
2159 417035 filter_level += s->lf_delta.mode[mb->mode];
2160 }
2161
2162 424829 filter_level = av_clip_uintp2(filter_level, 6);
2163
2164 424829 interior_limit = filter_level;
2165
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 422057 times.
424829 if (s->filter.sharpness) {
2166 2772 interior_limit >>= (s->filter.sharpness + 3) >> 2;
2167 2772 interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
2168 }
2169 424829 interior_limit = FFMAX(interior_limit, 1);
2170
2171 424829 f->filter_level = filter_level;
2172 424829 f->inner_limit = interior_limit;
2173
6/6
✓ Branch 0 taken 417789 times.
✓ Branch 1 taken 7040 times.
✓ Branch 2 taken 353400 times.
✓ Branch 3 taken 64389 times.
✓ Branch 4 taken 353112 times.
✓ Branch 5 taken 288 times.
777941 f->inner_filter = is_vp7 || !mb->skip || mb->mode == MODE_I4x4 ||
2174
2/2
✓ Branch 0 taken 3237 times.
✓ Branch 1 taken 349875 times.
353112 mb->mode == VP8_MVMODE_SPLIT;
2175 424829 }
2176
2177 static av_always_inline
2178 417107 void filter_mb(const VP8Context *s, uint8_t *const dst[3], const VP8FilterStrength *f,
2179 int mb_x, int mb_y, int is_vp7)
2180 {
2181 int mbedge_lim, bedge_lim_y, bedge_lim_uv, hev_thresh;
2182 417107 int filter_level = f->filter_level;
2183 417107 int inner_limit = f->inner_limit;
2184 417107 int inner_filter = f->inner_filter;
2185 417107 ptrdiff_t linesize = s->linesize;
2186 417107 ptrdiff_t uvlinesize = s->uvlinesize;
2187 static const uint8_t hev_thresh_lut[2][64] = {
2188 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
2189 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2190 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2191 3, 3, 3, 3 },
2192 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
2193 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2194 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2195 2, 2, 2, 2 }
2196 };
2197
2198
2/2
✓ Branch 0 taken 5876 times.
✓ Branch 1 taken 411231 times.
417107 if (!filter_level)
2199 5876 return;
2200
2201
2/2
✓ Branch 0 taken 7040 times.
✓ Branch 1 taken 404191 times.
411231 if (is_vp7) {
2202 7040 bedge_lim_y = filter_level;
2203 7040 bedge_lim_uv = filter_level * 2;
2204 7040 mbedge_lim = filter_level + 2;
2205 } else {
2206 404191 bedge_lim_y =
2207 404191 bedge_lim_uv = filter_level * 2 + inner_limit;
2208 404191 mbedge_lim = bedge_lim_y + 4;
2209 }
2210
2211 411231 hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
2212
2213
2/2
✓ Branch 0 taken 397088 times.
✓ Branch 1 taken 14143 times.
411231 if (mb_x) {
2214 397088 s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
2215 mbedge_lim, inner_limit, hev_thresh);
2216 397088 s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
2217 mbedge_lim, inner_limit, hev_thresh);
2218 }
2219
2220 #define H_LOOP_FILTER_16Y_INNER(cond) \
2221 if (cond && inner_filter) { \
2222 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 4, linesize, \
2223 bedge_lim_y, inner_limit, \
2224 hev_thresh); \
2225 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 8, linesize, \
2226 bedge_lim_y, inner_limit, \
2227 hev_thresh); \
2228 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 12, linesize, \
2229 bedge_lim_y, inner_limit, \
2230 hev_thresh); \
2231 s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4, \
2232 uvlinesize, bedge_lim_uv, \
2233 inner_limit, hev_thresh); \
2234 }
2235
2236
4/4
✓ Branch 0 taken 404191 times.
✓ Branch 1 taken 7040 times.
✓ Branch 2 taken 62665 times.
✓ Branch 3 taken 341526 times.
411231 H_LOOP_FILTER_16Y_INNER(!is_vp7)
2237
2238
2/2
✓ Branch 0 taken 390157 times.
✓ Branch 1 taken 21074 times.
411231 if (mb_y) {
2239 390157 s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
2240 mbedge_lim, inner_limit, hev_thresh);
2241 390157 s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
2242 mbedge_lim, inner_limit, hev_thresh);
2243 }
2244
2245
2/2
✓ Branch 0 taken 69705 times.
✓ Branch 1 taken 341526 times.
411231 if (inner_filter) {
2246 69705 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 4 * linesize,
2247 linesize, bedge_lim_y,
2248 inner_limit, hev_thresh);
2249 69705 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 8 * linesize,
2250 linesize, bedge_lim_y,
2251 inner_limit, hev_thresh);
2252 69705 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 12 * linesize,
2253 linesize, bedge_lim_y,
2254 inner_limit, hev_thresh);
2255 69705 s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
2256 69705 dst[2] + 4 * uvlinesize,
2257 uvlinesize, bedge_lim_uv,
2258 inner_limit, hev_thresh);
2259 }
2260
2261
3/4
✓ Branch 0 taken 7040 times.
✓ Branch 1 taken 404191 times.
✓ Branch 2 taken 7040 times.
✗ Branch 3 not taken.
411231 H_LOOP_FILTER_16Y_INNER(is_vp7)
2262 }
2263
2264 static av_always_inline
2265 7722 void filter_mb_simple(const VP8Context *s, uint8_t *dst, const VP8FilterStrength *f,
2266 int mb_x, int mb_y)
2267 {
2268 int mbedge_lim, bedge_lim;
2269 7722 int filter_level = f->filter_level;
2270 7722 int inner_limit = f->inner_limit;
2271 7722 int inner_filter = f->inner_filter;
2272 7722 ptrdiff_t linesize = s->linesize;
2273
2274
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7390 times.
7722 if (!filter_level)
2275 332 return;
2276
2277 7390 bedge_lim = 2 * filter_level + inner_limit;
2278 7390 mbedge_lim = bedge_lim + 4;
2279
2280
2/2
✓ Branch 0 taken 6713 times.
✓ Branch 1 taken 677 times.
7390 if (mb_x)
2281 6713 s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
2282
2/2
✓ Branch 0 taken 3523 times.
✓ Branch 1 taken 3867 times.
7390 if (inner_filter) {
2283 3523 s->vp8dsp.vp8_h_loop_filter_simple(dst + 4, linesize, bedge_lim);
2284 3523 s->vp8dsp.vp8_h_loop_filter_simple(dst + 8, linesize, bedge_lim);
2285 3523 s->vp8dsp.vp8_h_loop_filter_simple(dst + 12, linesize, bedge_lim);
2286 }
2287
2288
2/2
✓ Branch 0 taken 6609 times.
✓ Branch 1 taken 781 times.
7390 if (mb_y)
2289 6609 s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
2290
2/2
✓ Branch 0 taken 3523 times.
✓ Branch 1 taken 3867 times.
7390 if (inner_filter) {
2291 3523 s->vp8dsp.vp8_v_loop_filter_simple(dst + 4 * linesize, linesize, bedge_lim);
2292 3523 s->vp8dsp.vp8_v_loop_filter_simple(dst + 8 * linesize, linesize, bedge_lim);
2293 3523 s->vp8dsp.vp8_v_loop_filter_simple(dst + 12 * linesize, linesize, bedge_lim);
2294 }
2295 }
2296
2297 #define MARGIN (16 << 2)
2298 static av_always_inline
2299 32 int vp78_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
2300 const VP8Frame *prev_frame, int is_vp7)
2301 {
2302 32 VP8Context *s = avctx->priv_data;
2303 int mb_x, mb_y;
2304
2305 32 s->mv_bounds.mv_min.y = -MARGIN;
2306 32 s->mv_bounds.mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
2307
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 32 times.
384 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
2308 352 VP8Macroblock *mb = s->macroblocks_base +
2309 352 ((s->mb_width + 1) * (mb_y + 1) + 1);
2310 352 int mb_xy = mb_y * s->mb_width;
2311
2312 352 AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
2313
2314 352 s->mv_bounds.mv_min.x = -MARGIN;
2315 352 s->mv_bounds.mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
2316
2317
2/2
✓ Branch 0 taken 7040 times.
✓ Branch 1 taken 352 times.
7392 for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
2318
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7040 times.
7040 if (vpx_rac_is_end(&s->c)) {
2319 return AVERROR_INVALIDDATA;
2320 }
2321
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 6400 times.
7040 if (mb_y == 0)
2322 640 AV_WN32A((mb - s->mb_width - 1)->intra4x4_pred_mode_top,
2323 DC_PRED * 0x01010101);
2324
2/2
✓ Branch 0 taken 6820 times.
✓ Branch 1 taken 220 times.
13860 decode_mb_mode(s, &s->mv_bounds, mb, mb_x, mb_y, curframe->seg_map + mb_xy,
2325
1/2
✓ Branch 0 taken 6820 times.
✗ Branch 1 not taken.
6820 prev_frame && prev_frame->seg_map ?
2326 6820 prev_frame->seg_map + mb_xy : NULL, 1, is_vp7);
2327 7040 s->mv_bounds.mv_min.x -= 64;
2328 7040 s->mv_bounds.mv_max.x -= 64;
2329 }
2330 352 s->mv_bounds.mv_min.y -= 64;
2331 352 s->mv_bounds.mv_max.y -= 64;
2332 }
2333 32 return 0;
2334 }
2335
2336 32 static int vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
2337 const VP8Frame *prev_frame)
2338 {
2339 32 return vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP7);
2340 }
2341
2342 static int vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
2343 const VP8Frame *prev_frame)
2344 {
2345 return vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP8);
2346 }
2347
2348 #if HAVE_THREADS
2349 #define check_thread_pos(td, otd, mb_x_check, mb_y_check) \
2350 do { \
2351 int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF); \
2352 if (atomic_load(&otd->thread_mb_pos) < tmp) { \
2353 pthread_mutex_lock(&otd->lock); \
2354 atomic_store(&td->wait_mb_pos, tmp); \
2355 do { \
2356 if (atomic_load(&otd->thread_mb_pos) >= tmp) \
2357 break; \
2358 pthread_cond_wait(&otd->cond, &otd->lock); \
2359 } while (1); \
2360 atomic_store(&td->wait_mb_pos, INT_MAX); \
2361 pthread_mutex_unlock(&otd->lock); \
2362 } \
2363 } while (0)
2364
2365 #define update_pos(td, mb_y, mb_x) \
2366 do { \
2367 int pos = (mb_y << 16) | (mb_x & 0xFFFF); \
2368 int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \
2369 (num_jobs > 1); \
2370 int is_null = !next_td || !prev_td; \
2371 int pos_check = (is_null) ? 1 : \
2372 (next_td != td && pos >= atomic_load(&next_td->wait_mb_pos)) || \
2373 (prev_td != td && pos >= atomic_load(&prev_td->wait_mb_pos)); \
2374 atomic_store(&td->thread_mb_pos, pos); \
2375 if (sliced_threading && pos_check) { \
2376 pthread_mutex_lock(&td->lock); \
2377 pthread_cond_broadcast(&td->cond); \
2378 pthread_mutex_unlock(&td->lock); \
2379 } \
2380 } while (0)
2381 #else
2382 #define check_thread_pos(td, otd, mb_x_check, mb_y_check) while(0)
2383 #define update_pos(td, mb_y, mb_x) while(0)
2384 #endif
2385
2386 16227 static av_always_inline int decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
2387 int jobnr, int threadnr, int is_vp7)
2388 {
2389 16227 VP8Context *s = avctx->priv_data;
2390 16227 VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
2391 16227 int mb_y = atomic_load(&td->thread_mb_pos) >> 16;
2392 16227 int mb_x, mb_xy = mb_y * s->mb_width;
2393 16227 int num_jobs = s->num_jobs;
2394 16227 const VP8Frame *prev_frame = s->prev_frame;
2395 16227 VP8Frame *curframe = s->curframe;
2396 16227 VPXRangeCoder *coeff_c = &s->coeff_partition[mb_y & (s->num_coeff_partitions - 1)];
2397
2398 VP8Macroblock *mb;
2399 16227 uint8_t *dst[3] = {
2400 16227 curframe->tf.f->data[0] + 16 * mb_y * s->linesize,
2401 16227 curframe->tf.f->data[1] + 8 * mb_y * s->uvlinesize,
2402 16227 curframe->tf.f->data[2] + 8 * mb_y * s->uvlinesize
2403 };
2404
2405
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16227 times.
16227 if (vpx_rac_is_end(&s->c))
2406 return AVERROR_INVALIDDATA;
2407
2408
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 15080 times.
16227 if (mb_y == 0)
2409 1147 prev_td = td;
2410 else
2411 15080 prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
2412
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 15080 times.
16227 if (mb_y == s->mb_height - 1)
2413 1147 next_td = td;
2414 else
2415 15080 next_td = &s->thread_data[(jobnr + 1) % num_jobs];
2416
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 15875 times.
16227 if (s->mb_layout == 1)
2417 352 mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
2418 else {
2419 // Make sure the previous frame has read its segmentation map,
2420 // if we reuse the same map.
2421
4/4
✓ Branch 0 taken 15272 times.
✓ Branch 1 taken 603 times.
✓ Branch 2 taken 5598 times.
✓ Branch 3 taken 9674 times.
15875 if (prev_frame && s->segmentation.enabled &&
2422
2/2
✓ Branch 0 taken 5025 times.
✓ Branch 1 taken 573 times.
5598 !s->segmentation.update_map)
2423 5025 ff_progress_frame_await(&prev_frame->tf, mb_y);
2424 15875 mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
2425 15875 memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
2426 15875 AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
2427 }
2428
2429
4/4
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 15875 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 320 times.
16227 if (!is_vp7 || mb_y == 0)
2430 15907 memset(td->left_nnz, 0, sizeof(td->left_nnz));
2431
2432 16227 td->mv_bounds.mv_min.x = -MARGIN;
2433 16227 td->mv_bounds.mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
2434
2435
2/2
✓ Branch 0 taken 443889 times.
✓ Branch 1 taken 16227 times.
460116 for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
2436
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 443889 times.
443889 if (vpx_rac_is_end(&s->c))
2437 return AVERROR_INVALIDDATA;
2438 // Wait for previous thread to read mb_x+2, and reach mb_y-1.
2439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 443889 times.
443889 if (prev_td != td) {
2440 if (threadnr != 0) {
2441 check_thread_pos(td, prev_td,
2442 mb_x + (is_vp7 ? 2 : 1),
2443 mb_y - (is_vp7 ? 2 : 1));
2444 } else {
2445 check_thread_pos(td, prev_td,
2446 mb_x + (is_vp7 ? 2 : 1) + s->mb_width + 3,
2447 mb_y - (is_vp7 ? 2 : 1));
2448 }
2449 }
2450
2451 443889 s->vdsp.prefetch(dst[0] + (mb_x & 3) * 4 * s->linesize + 64,
2452 s->linesize, 4);
2453 443889 s->vdsp.prefetch(dst[1] + (mb_x & 7) * s->uvlinesize + 64,
2454 443889 dst[2] - dst[1], 2);
2455
2456
2/2
✓ Branch 0 taken 436849 times.
✓ Branch 1 taken 7040 times.
443889 if (!s->mb_layout)
2457
2/2
✓ Branch 0 taken 406109 times.
✓ Branch 1 taken 30740 times.
842958 decode_mb_mode(s, &td->mv_bounds, mb, mb_x, mb_y, curframe->seg_map + mb_xy,
2458
1/2
✓ Branch 0 taken 406109 times.
✗ Branch 1 not taken.
406109 prev_frame && prev_frame->seg_map ?
2459 406109 prev_frame->seg_map + mb_xy : NULL, 0, is_vp7);
2460
2461 443889 prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP8_FRAME_PREVIOUS);
2462
2463
2/2
✓ Branch 0 taken 83859 times.
✓ Branch 1 taken 360030 times.
443889 if (!mb->skip) {
2464
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 83859 times.
83859 if (vpx_rac_is_end(coeff_c))
2465 return AVERROR_INVALIDDATA;
2466 83859 decode_mb_coeffs(s, td, coeff_c, mb, s->top_nnz[mb_x], td->left_nnz, is_vp7);
2467 }
2468
2469
2/2
✓ Branch 0 taken 57510 times.
✓ Branch 1 taken 386379 times.
443889 if (mb->mode <= MODE_I4x4)
2470 57510 intra_predict(s, td, dst, mb, mb_x, mb_y, is_vp7);
2471 else
2472 386379 inter_predict(s, td, dst, mb, mb_x, mb_y);
2473
2474 443889 prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP8_FRAME_GOLDEN);
2475
2476
2/2
✓ Branch 0 taken 76964 times.
✓ Branch 1 taken 366925 times.
443889 if (!mb->skip) {
2477 76964 idct_mb(s, td, dst, mb);
2478 } else {
2479 366925 AV_ZERO64(td->left_nnz);
2480 366925 AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
2481
2482 /* Reset DC block predictors if they would exist
2483 * if the mb had coefficients */
2484
4/4
✓ Branch 0 taken 366462 times.
✓ Branch 1 taken 463 times.
✓ Branch 2 taken 363195 times.
✓ Branch 3 taken 3267 times.
366925 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
2485 363195 td->left_nnz[8] = 0;
2486 363195 s->top_nnz[mb_x][8] = 0;
2487 }
2488 }
2489
2490
2/2
✓ Branch 0 taken 424829 times.
✓ Branch 1 taken 19060 times.
443889 if (s->deblock_filter)
2491 424829 filter_level_for_mb(s, mb, &td->filter_strength[mb_x], is_vp7);
2492
2493
3/6
✓ Branch 0 taken 424829 times.
✓ Branch 1 taken 19060 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 424829 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
443889 if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs - 1) {
2494 if (s->filter.simple)
2495 backup_mb_border(s->top_border[mb_x + 1], dst[0],
2496 NULL, NULL, s->linesize, 0, 1);
2497 else
2498 backup_mb_border(s->top_border[mb_x + 1], dst[0],
2499 dst[1], dst[2], s->linesize, s->uvlinesize, 0);
2500 }
2501
2502 443889 prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP8_FRAME_ALTREF);
2503
2504 443889 dst[0] += 16;
2505 443889 dst[1] += 8;
2506 443889 dst[2] += 8;
2507 443889 td->mv_bounds.mv_min.x -= 64;
2508 443889 td->mv_bounds.mv_max.x -= 64;
2509
2510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 443889 times.
443889 if (mb_x == s->mb_width + 1) {
2511 update_pos(td, mb_y, s->mb_width + 3);
2512 } else {
2513
7/22
✗ Branch 0 not taken.
✓ Branch 1 taken 443889 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 443889 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 443889 times.
✓ Branch 8 taken 443889 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 443889 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 443889 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 443889 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
443889 update_pos(td, mb_y, mb_x);
2514 }
2515 }
2516 16227 return 0;
2517 }
2518
2519 15153 static av_always_inline void filter_mb_row(AVCodecContext *avctx, void *tdata,
2520 int jobnr, int threadnr, int is_vp7)
2521 {
2522 15153 VP8Context *s = avctx->priv_data;
2523 15153 VP8ThreadData *td = &s->thread_data[threadnr];
2524 15153 int mb_x, mb_y = atomic_load(&td->thread_mb_pos) >> 16, num_jobs = s->num_jobs;
2525 15153 AVFrame *curframe = s->curframe->tf.f;
2526 VP8ThreadData *prev_td, *next_td;
2527 15153 uint8_t *dst[3] = {
2528 15153 curframe->data[0] + 16 * mb_y * s->linesize,
2529 15153 curframe->data[1] + 8 * mb_y * s->uvlinesize,
2530 15153 curframe->data[2] + 8 * mb_y * s->uvlinesize
2531 };
2532
2533
2/2
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 14115 times.
15153 if (mb_y == 0)
2534 1038 prev_td = td;
2535 else
2536 14115 prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
2537
2/2
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 14115 times.
15153 if (mb_y == s->mb_height - 1)
2538 1038 next_td = td;
2539 else
2540 14115 next_td = &s->thread_data[(jobnr + 1) % num_jobs];
2541
2542
2/2
✓ Branch 0 taken 424829 times.
✓ Branch 1 taken 15153 times.
439982 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
2543 424829 const VP8FilterStrength *f = &td->filter_strength[mb_x];
2544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424829 times.
424829 if (prev_td != td)
2545 check_thread_pos(td, prev_td,
2546 (mb_x + 1) + (s->mb_width + 3), mb_y - 1);
2547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424829 times.
424829 if (next_td != td)
2548 if (next_td != &s->thread_data[0])
2549 check_thread_pos(td, next_td, mb_x + 1, mb_y + 1);
2550
2551
1/2
✓ Branch 0 taken 424829 times.
✗ Branch 1 not taken.
424829 if (num_jobs == 1) {
2552
2/2
✓ Branch 0 taken 7722 times.
✓ Branch 1 taken 417107 times.
424829 if (s->filter.simple)
2553 7722 backup_mb_border(s->top_border[mb_x + 1], dst[0],
2554 NULL, NULL, s->linesize, 0, 1);
2555 else
2556 417107 backup_mb_border(s->top_border[mb_x + 1], dst[0],
2557 417107 dst[1], dst[2], s->linesize, s->uvlinesize, 0);
2558 }
2559
2560
2/2
✓ Branch 0 taken 7722 times.
✓ Branch 1 taken 417107 times.
424829 if (s->filter.simple)
2561 7722 filter_mb_simple(s, dst[0], f, mb_x, mb_y);
2562 else
2563 417107 filter_mb(s, dst, f, mb_x, mb_y, is_vp7);
2564 424829 dst[0] += 16;
2565 424829 dst[1] += 8;
2566 424829 dst[2] += 8;
2567
2568
7/22
✗ Branch 0 not taken.
✓ Branch 1 taken 424829 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 424829 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 424829 times.
✓ Branch 8 taken 424829 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 424829 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 424829 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 424829 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
424829 update_pos(td, mb_y, (s->mb_width + 3) + mb_x);
2569 }
2570 15153 }
2571
2572 static av_always_inline
2573 1147 int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
2574 int threadnr, int is_vp7)
2575 {
2576 1147 const VP8Context *s = avctx->priv_data;
2577 1147 VP8ThreadData *td = &s->thread_data[jobnr];
2578 1147 VP8ThreadData *next_td = NULL, *prev_td = NULL;
2579 1147 VP8Frame *curframe = s->curframe;
2580 1147 int mb_y, num_jobs = s->num_jobs;
2581 int ret;
2582
2583 1147 td->thread_nr = threadnr;
2584 1147 td->mv_bounds.mv_min.y = -MARGIN - 64 * threadnr;
2585 1147 td->mv_bounds.mv_max.y = ((s->mb_height - 1) << 6) + MARGIN - 64 * threadnr;
2586
2/2
✓ Branch 0 taken 16227 times.
✓ Branch 1 taken 1147 times.
17374 for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
2587 16227 atomic_store(&td->thread_mb_pos, mb_y << 16);
2588 16227 ret = s->decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr);
2589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16227 times.
16227 if (ret < 0) {
2590 update_pos(td, s->mb_height, INT_MAX & 0xFFFF);
2591 return ret;
2592 }
2593
2/2
✓ Branch 0 taken 15153 times.
✓ Branch 1 taken 1074 times.
16227 if (s->deblock_filter)
2594 15153 s->filter_mb_row(avctx, tdata, jobnr, threadnr);
2595
4/22
✗ Branch 0 not taken.
✓ Branch 1 taken 16227 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 16227 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 16227 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 16227 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
16227 update_pos(td, mb_y, INT_MAX & 0xFFFF);
2596
2597 16227 td->mv_bounds.mv_min.y -= 64 * num_jobs;
2598 16227 td->mv_bounds.mv_max.y -= 64 * num_jobs;
2599
2600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16227 times.
16227 if (avctx->active_thread_type == FF_THREAD_FRAME)
2601 ff_progress_frame_report(&curframe->tf, mb_y);
2602 }
2603
2604 1147 return 0;
2605 }
2606
2607 32 static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
2608 int jobnr, int threadnr)
2609 {
2610 32 return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP7);
2611 }
2612
2613 1115 static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
2614 int jobnr, int threadnr)
2615 {
2616 1115 return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP8);
2617 }
2618
2619 static av_always_inline
2620 1147 int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame,
2621 const AVPacket *avpkt, int is_vp7)
2622 {
2623 1147 VP8Context *s = avctx->priv_data;
2624 int ret, i, referenced, num_jobs;
2625 enum AVDiscard skip_thresh;
2626 1147 VP8Frame *av_uninit(curframe), *prev_frame;
2627
2628
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1115 times.
1147 if (is_vp7)
2629 32 ret = vp7_decode_frame_header(s, avpkt->data, avpkt->size);
2630 else
2631 1115 ret = vp8_decode_frame_header(s, avpkt->data, avpkt->size);
2632
2633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (ret < 0)
2634 goto err;
2635
2636
4/4
✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1109 times.
1147 if (!is_vp7 && s->actually_webp) {
2637 // VP8 in WebP is supposed to be intra-only. Enforce this here
2638 // to ensure that output is reproducible with frame-threading.
2639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!s->keyframe)
2640 return AVERROR_INVALIDDATA;
2641 // avctx->pix_fmt already set in caller.
2642
3/4
✓ Branch 0 taken 1109 times.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1109 times.
1141 } else if (!is_vp7 && s->pix_fmt == AV_PIX_FMT_NONE) {
2643 s->pix_fmt = get_pixel_format(s);
2644 if (s->pix_fmt < 0) {
2645 ret = AVERROR(EINVAL);
2646 goto err;
2647 }
2648 avctx->pix_fmt = s->pix_fmt;
2649 }
2650
2651 1147 prev_frame = s->framep[VP8_FRAME_CURRENT];
2652
2653
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1136 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
1158 referenced = s->update_last || s->update_golden == VP8_FRAME_CURRENT ||
2654
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
11 s->update_altref == VP8_FRAME_CURRENT;
2655
2656 1147 skip_thresh = !referenced ? AVDISCARD_NONREF
2657
4/4
✓ Branch 0 taken 1145 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1095 times.
✓ Branch 3 taken 50 times.
1147 : !s->keyframe ? AVDISCARD_NONKEY
2658 : AVDISCARD_ALL;
2659
2660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (avctx->skip_frame >= skip_thresh) {
2661 s->invisible = 1;
2662 memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
2663 goto skip_decode;
2664 }
2665
3/4
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 1038 times.
✗ Branch 3 not taken.
1147 s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
2666
2667 // release no longer referenced frames
2668
2/2
✓ Branch 0 taken 5735 times.
✓ Branch 1 taken 1147 times.
6882 for (i = 0; i < 5; i++)
2669
2/2
✓ Branch 0 taken 3835 times.
✓ Branch 1 taken 1900 times.
5735 if (s->frames[i].tf.f &&
2670
2/2
✓ Branch 0 taken 2726 times.
✓ Branch 1 taken 1109 times.
3835 &s->frames[i] != prev_frame &&
2671
2/2
✓ Branch 0 taken 2715 times.
✓ Branch 1 taken 11 times.
2726 &s->frames[i] != s->framep[VP8_FRAME_PREVIOUS] &&
2672
2/2
✓ Branch 0 taken 1743 times.
✓ Branch 1 taken 972 times.
2715 &s->frames[i] != s->framep[VP8_FRAME_GOLDEN] &&
2673
2/2
✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 708 times.
1743 &s->frames[i] != s->framep[VP8_FRAME_ALTREF])
2674 1035 vp8_release_frame(&s->frames[i]);
2675
2676
1/2
✓ Branch 0 taken 1147 times.
✗ Branch 1 not taken.
1147 if (!s->colorspace)
2677 1147 avctx->colorspace = AVCOL_SPC_BT470BG;
2678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (s->fullrange)
2679 avctx->color_range = AVCOL_RANGE_JPEG;
2680 else
2681 1147 avctx->color_range = AVCOL_RANGE_MPEG;
2682
2683 /* Given that arithmetic probabilities are updated every frame, it's quite
2684 * likely that the values we have on a random interframe are complete
2685 * junk if we didn't start decode on a keyframe. So just don't display
2686 * anything rather than junk. */
2687
3/4
✓ Branch 0 taken 1097 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 1097 times.
✗ Branch 3 not taken.
1147 if (!s->keyframe && (!s->framep[VP8_FRAME_PREVIOUS] ||
2688
1/2
✓ Branch 0 taken 1097 times.
✗ Branch 1 not taken.
1097 !s->framep[VP8_FRAME_GOLDEN] ||
2689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1097 times.
1097 !s->framep[VP8_FRAME_ALTREF])) {
2690 av_log(avctx, AV_LOG_WARNING,
2691 "Discarding interframe without a prior keyframe!\n");
2692 ret = AVERROR_INVALIDDATA;
2693 goto err;
2694 }
2695
2696 1147 curframe = vp8_find_free_buffer(s);
2697
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1147 times.
1147 if ((ret = vp8_alloc_frame(s, curframe, referenced)) < 0)
2698 goto err;
2699 1147 s->framep[VP8_FRAME_CURRENT] = curframe;
2700
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1097 times.
1147 if (s->keyframe)
2701 50 curframe->tf.f->flags |= AV_FRAME_FLAG_KEY;
2702 else
2703 1097 curframe->tf.f->flags &= ~AV_FRAME_FLAG_KEY;
2704 2294 curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2705
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1097 times.
1147 : AV_PICTURE_TYPE_P;
2706
2707 // check if golden and altref are swapped
2708
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 948 times.
1147 if (s->update_altref != VP8_FRAME_NONE)
2709 199 s->next_framep[VP8_FRAME_ALTREF] = s->framep[s->update_altref];
2710 else
2711 948 s->next_framep[VP8_FRAME_ALTREF] = s->framep[VP8_FRAME_ALTREF];
2712
2713
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 1007 times.
1147 if (s->update_golden != VP8_FRAME_NONE)
2714 140 s->next_framep[VP8_FRAME_GOLDEN] = s->framep[s->update_golden];
2715 else
2716 1007 s->next_framep[VP8_FRAME_GOLDEN] = s->framep[VP8_FRAME_GOLDEN];
2717
2718
2/2
✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 11 times.
1147 if (s->update_last)
2719 1136 s->next_framep[VP8_FRAME_PREVIOUS] = curframe;
2720 else
2721 11 s->next_framep[VP8_FRAME_PREVIOUS] = s->framep[VP8_FRAME_PREVIOUS];
2722
2723 1147 s->next_framep[VP8_FRAME_CURRENT] = curframe;
2724
2725
4/4
✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 1109 times.
✓ Branch 3 taken 6 times.
1147 if (!is_vp7 && !s->actually_webp)
2726 1109 ff_thread_finish_setup(avctx);
2727
2728
3/4
✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1115 times.
1147 if (!is_vp7 && avctx->hwaccel) {
2729 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
2730 ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
2731 if (ret < 0)
2732 goto err;
2733
2734 ret = hwaccel->decode_slice(avctx, avpkt->data, avpkt->size);
2735 if (ret < 0)
2736 goto err;
2737
2738 ret = hwaccel->end_frame(avctx);
2739 if (ret < 0)
2740 goto err;
2741
2742 } else {
2743 1147 s->linesize = curframe->tf.f->linesize[0];
2744 1147 s->uvlinesize = curframe->tf.f->linesize[1];
2745
2746 1147 memset(s->top_nnz, 0, s->mb_width * sizeof(*s->top_nnz));
2747 /* Zero macroblock structures for top/top-left prediction
2748 * from outside the frame. */
2749
2/2
✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 32 times.
1147 if (!s->mb_layout)
2750 1115 memset(s->macroblocks + s->mb_height * 2 - 1, 0,
2751 1115 (s->mb_width + 1) * sizeof(*s->macroblocks));
2752
4/4
✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 1066 times.
1147 if (!s->mb_layout && s->keyframe)
2753 49 memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width * 4);
2754
2755 1147 memset(s->ref_count, 0, sizeof(s->ref_count));
2756
2757
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1115 times.
1147 if (s->mb_layout == 1) {
2758 // Make sure the previous frame has read its segmentation map,
2759 // if we reuse the same map.
2760
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
32 if (prev_frame && s->segmentation.enabled &&
2761 !s->segmentation.update_map)
2762 ff_progress_frame_await(&prev_frame->tf, 1);
2763
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (is_vp7)
2764 32 ret = vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
2765 else
2766 ret = vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
2767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
2768 goto err;
2769 }
2770
2771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (avctx->active_thread_type == FF_THREAD_FRAME)
2772 num_jobs = 1;
2773 else
2774 1147 num_jobs = FFMIN(s->num_coeff_partitions, avctx->thread_count);
2775 1147 s->num_jobs = num_jobs;
2776 1147 s->curframe = curframe;
2777 1147 s->prev_frame = prev_frame;
2778 1147 s->mv_bounds.mv_min.y = -MARGIN;
2779 1147 s->mv_bounds.mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
2780
2/2
✓ Branch 0 taken 9176 times.
✓ Branch 1 taken 1147 times.
10323 for (i = 0; i < MAX_THREADS; i++) {
2781 9176 VP8ThreadData *td = &s->thread_data[i];
2782 9176 atomic_init(&td->thread_mb_pos, 0);
2783 9176 atomic_init(&td->wait_mb_pos, INT_MAX);
2784 }
2785
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1115 times.
1147 if (is_vp7)
2786 32 avctx->execute2(avctx, vp7_decode_mb_row_sliced, s->thread_data, NULL,
2787 num_jobs);
2788 else
2789 1115 avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL,
2790 num_jobs);
2791 }
2792
2793 1147 ff_progress_frame_report(&curframe->tf, INT_MAX);
2794 1147 memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
2795
2796 1147 skip_decode:
2797 // if future frames don't use the updated probabilities,
2798 // reset them to the values we saved
2799
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1083 times.
1147 if (!s->update_probabilities)
2800 64 s->prob[0] = s->prob[1];
2801
2802
2/2
✓ Branch 0 taken 1138 times.
✓ Branch 1 taken 9 times.
1147 if (!s->invisible) {
2803
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1138 times.
1138 if ((ret = av_frame_ref(rframe, curframe->tf.f)) < 0)
2804 return ret;
2805 1138 *got_frame = 1;
2806 }
2807
2808 1147 return avpkt->size;
2809 err:
2810 memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
2811 return ret;
2812 }
2813
2814 63 av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
2815 {
2816 63 vp8_decode_flush_impl(avctx, 1);
2817
2818 63 return 0;
2819 }
2820
2821 63 static av_cold void vp78_decode_init(AVCodecContext *avctx)
2822 {
2823 63 VP8Context *s = avctx->priv_data;
2824
2825 63 s->avctx = avctx;
2826 63 s->pix_fmt = AV_PIX_FMT_NONE;
2827 63 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2828
2829 63 ff_videodsp_init(&s->vdsp, 8);
2830
2831 63 ff_vp78dsp_init(&s->vp8dsp);
2832
2833 /* does not change for VP8 */
2834 63 memcpy(s->prob[0].scan, ff_zigzag_scan, sizeof(s->prob[0].scan));
2835 63 }
2836
2837 #if CONFIG_VP8_DECODER
2838 15875 static int vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
2839 int jobnr, int threadnr)
2840 {
2841 15875 return decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, 0);
2842 }
2843
2844 14801 static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
2845 int jobnr, int threadnr)
2846 {
2847 14801 filter_mb_row(avctx, tdata, jobnr, threadnr, 0);
2848 14801 }
2849
2850 1115 int ff_vp8_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2851 int *got_frame, AVPacket *avpkt)
2852 {
2853 1115 return vp78_decode_frame(avctx, frame, got_frame, avpkt, IS_VP8);
2854 }
2855
2856 60 av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
2857 {
2858 60 VP8Context *s = avctx->priv_data;
2859
2860 60 vp78_decode_init(avctx);
2861 60 ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
2862 60 ff_vp8dsp_init(&s->vp8dsp);
2863 60 s->decode_mb_row_no_filter = vp8_decode_mb_row_no_filter;
2864 60 s->filter_mb_row = vp8_filter_mb_row;
2865
2866 60 return 0;
2867 }
2868
2869 #if HAVE_THREADS
2870 static void vp8_replace_frame(VP8Frame *dst, const VP8Frame *src)
2871 {
2872 ff_progress_frame_replace(&dst->tf, &src->tf);
2873 av_refstruct_replace(&dst->seg_map, src->seg_map);
2874 av_refstruct_replace(&dst->hwaccel_picture_private,
2875 src->hwaccel_picture_private);
2876 }
2877
2878 #define REBASE(pic) ((pic) ? (pic) - &s_src->frames[0] + &s->frames[0] : NULL)
2879
2880 static int vp8_decode_update_thread_context(AVCodecContext *dst,
2881 const AVCodecContext *src)
2882 {
2883 VP8Context *s = dst->priv_data, *s_src = src->priv_data;
2884
2885 if (s->macroblocks_base &&
2886 (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
2887 free_buffers(s);
2888 s->mb_width = s_src->mb_width;
2889 s->mb_height = s_src->mb_height;
2890 }
2891
2892 s->pix_fmt = s_src->pix_fmt;
2893 s->prob[0] = s_src->prob[!s_src->update_probabilities];
2894 s->segmentation = s_src->segmentation;
2895 s->lf_delta = s_src->lf_delta;
2896 memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
2897
2898 for (int i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++)
2899 vp8_replace_frame(&s->frames[i], &s_src->frames[i]);
2900
2901 s->framep[0] = REBASE(s_src->next_framep[0]);
2902 s->framep[1] = REBASE(s_src->next_framep[1]);
2903 s->framep[2] = REBASE(s_src->next_framep[2]);
2904 s->framep[3] = REBASE(s_src->next_framep[3]);
2905
2906 return 0;
2907 }
2908 #endif /* HAVE_THREADS */
2909 #endif /* CONFIG_VP8_DECODER */
2910
2911 #if CONFIG_VP7_DECODER
2912 352 static int vp7_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
2913 int jobnr, int threadnr)
2914 {
2915 352 return decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, 1);
2916 }
2917
2918 352 static void vp7_filter_mb_row(AVCodecContext *avctx, void *tdata,
2919 int jobnr, int threadnr)
2920 {
2921 352 filter_mb_row(avctx, tdata, jobnr, threadnr, 1);
2922 352 }
2923
2924 32 static int vp7_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2925 int *got_frame, AVPacket *avpkt)
2926 {
2927 32 return vp78_decode_frame(avctx, frame, got_frame, avpkt, IS_VP7);
2928 }
2929
2930 3 av_cold static int vp7_decode_init(AVCodecContext *avctx)
2931 {
2932 3 VP8Context *s = avctx->priv_data;
2933
2934 3 vp78_decode_init(avctx);
2935 3 ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP7, 8, 1);
2936 3 ff_vp7dsp_init(&s->vp8dsp);
2937 3 s->decode_mb_row_no_filter = vp7_decode_mb_row_no_filter;
2938 3 s->filter_mb_row = vp7_filter_mb_row;
2939
2940 3 return 0;
2941 }
2942
2943 const FFCodec ff_vp7_decoder = {
2944 .p.name = "vp7",
2945 CODEC_LONG_NAME("On2 VP7"),
2946 .p.type = AVMEDIA_TYPE_VIDEO,
2947 .p.id = AV_CODEC_ID_VP7,
2948 .priv_data_size = sizeof(VP8Context),
2949 .init = vp7_decode_init,
2950 .close = ff_vp8_decode_free,
2951 FF_CODEC_DECODE_CB(vp7_decode_frame),
2952 .p.capabilities = AV_CODEC_CAP_DR1,
2953 .flush = vp8_decode_flush,
2954 .caps_internal = FF_CODEC_CAP_USES_PROGRESSFRAMES,
2955 };
2956 #endif /* CONFIG_VP7_DECODER */
2957
2958 #if CONFIG_VP8_DECODER
2959 const FFCodec ff_vp8_decoder = {
2960 .p.name = "vp8",
2961 CODEC_LONG_NAME("On2 VP8"),
2962 .p.type = AVMEDIA_TYPE_VIDEO,
2963 .p.id = AV_CODEC_ID_VP8,
2964 .priv_data_size = sizeof(VP8Context),
2965 .init = ff_vp8_decode_init,
2966 .close = ff_vp8_decode_free,
2967 FF_CODEC_DECODE_CB(ff_vp8_decode_frame),
2968 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
2969 AV_CODEC_CAP_SLICE_THREADS,
2970 .caps_internal = FF_CODEC_CAP_USES_PROGRESSFRAMES,
2971 .flush = vp8_decode_flush,
2972 UPDATE_THREAD_CONTEXT(vp8_decode_update_thread_context),
2973 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2974 #if CONFIG_VP8_VAAPI_HWACCEL
2975 HWACCEL_VAAPI(vp8),
2976 #endif
2977 #if CONFIG_VP8_NVDEC_HWACCEL
2978 HWACCEL_NVDEC(vp8),
2979 #endif
2980 NULL
2981 },
2982 };
2983 #endif /* CONFIG_VP8_DECODER */
2984