FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp8.c
Date: 2026-08-26 01:27:24
Exec Total Coverage
Lines: 1377 1610 85.5%
Functions: 79 86 91.9%
Branches: 816 1163 70.2%

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