FFmpeg coverage


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