FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp8.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 1372 1609 85.3%
Branches: 814 1171 69.5%

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