FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp56.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 472 508 92.9%
Functions: 20 20 100.0%
Branches: 247 283 87.3%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * VP5 and VP6 compatible video decoder (common features)
24 */
25
26 #include "libavutil/mem.h"
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "decode.h"
30 #include "h264chroma.h"
31 #include "vp56.h"
32 #include "vp56data.h"
33 #include "vpx_rac.h"
34
35
36 961 void ff_vp56_init_dequant(VP56Context *s, int quantizer)
37 {
38
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 553 times.
961 if (s->quantizer != quantizer)
39 408 ff_vp3dsp_set_bounding_values(s->bounding_values_array, ff_vp56_filter_threshold[quantizer]);
40 961 s->quantizer = quantizer;
41 961 s->dequant_dc = ff_vp56_dc_dequant[quantizer] << 2;
42 961 s->dequant_ac = ff_vp56_ac_dequant[quantizer] << 2;
43 961 }
44
45 238244 static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
46 VP56Frame ref_frame)
47 {
48 238244 int nb_pred = 0;
49 238244 VP56mv vect[2] = {{0,0}, {0,0}};
50 int pos, offset;
51 VP56mv mvp;
52
53
2/2
✓ Branch 0 taken 2278280 times.
✓ Branch 1 taken 161235 times.
2439515 for (pos=0; pos<12; pos++) {
54 2278280 mvp.x = col + ff_vp56_candidate_predictor_pos[pos][0];
55 2278280 mvp.y = row + ff_vp56_candidate_predictor_pos[pos][1];
56
4/4
✓ Branch 0 taken 2185191 times.
✓ Branch 1 taken 93089 times.
✓ Branch 2 taken 2130905 times.
✓ Branch 3 taken 54286 times.
2278280 if (mvp.x < 0 || mvp.x >= s->mb_width ||
57
3/4
✓ Branch 0 taken 1915428 times.
✓ Branch 1 taken 215477 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1915428 times.
2130905 mvp.y < 0 || mvp.y >= s->mb_height)
58 362852 continue;
59 1915428 offset = mvp.x + s->mb_width*mvp.y;
60
61
2/2
✓ Branch 0 taken 331678 times.
✓ Branch 1 taken 1583750 times.
1915428 if (ff_vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
62 331678 continue;
63
2/2
✓ Branch 0 taken 1293327 times.
✓ Branch 1 taken 290423 times.
1583750 if ((s->macroblocks[offset].mv.x == vect[0].x &&
64
2/2
✓ Branch 0 taken 76010 times.
✓ Branch 1 taken 1217317 times.
1293327 s->macroblocks[offset].mv.y == vect[0].y) ||
65
2/2
✓ Branch 0 taken 207800 times.
✓ Branch 1 taken 158633 times.
366433 (s->macroblocks[offset].mv.x == 0 &&
66
2/2
✓ Branch 0 taken 177064 times.
✓ Branch 1 taken 30736 times.
207800 s->macroblocks[offset].mv.y == 0))
67 1394381 continue;
68
69 189369 vect[nb_pred++] = s->macroblocks[offset].mv;
70
2/2
✓ Branch 0 taken 77009 times.
✓ Branch 1 taken 112360 times.
189369 if (nb_pred > 1) {
71 77009 nb_pred = -1;
72 77009 break;
73 }
74 112360 s->vector_candidate_pos = pos;
75 }
76
77 238244 s->vector_candidate[0] = vect[0];
78 238244 s->vector_candidate[1] = vect[1];
79
80 238244 return nb_pred+1;
81 }
82
83 917 static void vp56_parse_mb_type_models(VP56Context *s)
84 {
85 917 VPXRangeCoder *c = &s->c;
86 917 VP56Model *model = s->modelp;
87 int i, ctx, type;
88
89
2/2
✓ Branch 0 taken 2751 times.
✓ Branch 1 taken 917 times.
3668 for (ctx=0; ctx<3; ctx++) {
90
2/2
✓ Branch 1 taken 730 times.
✓ Branch 2 taken 2021 times.
2751 if (vpx_rac_get_prob_branchy(c, 174)) {
91 730 int idx = vp56_rac_gets(c, 4);
92 730 memcpy(model->mb_types_stats[ctx],
93 730 ff_vp56_pre_def_mb_type_stats[idx][ctx],
94 sizeof(model->mb_types_stats[ctx]));
95 }
96
2/2
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 2634 times.
2751 if (vpx_rac_get_prob_branchy(c, 254)) {
97
2/2
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 117 times.
1287 for (type=0; type<10; type++) {
98
2/2
✓ Branch 0 taken 2340 times.
✓ Branch 1 taken 1170 times.
3510 for(i=0; i<2; i++) {
99
2/2
✓ Branch 1 taken 915 times.
✓ Branch 2 taken 1425 times.
2340 if (vpx_rac_get_prob_branchy(c, 205)) {
100 915 int delta, sign = vpx_rac_get(c);
101
102 915 delta = vp56_rac_get_tree(c, ff_vp56_pmbtm_tree,
103 ff_vp56_mb_type_model_model);
104
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 776 times.
915 if (!delta)
105 139 delta = 4 * vp56_rac_gets(c, 7);
106 915 model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
107 }
108 }
109 }
110 }
111 }
112
113 /* compute MB type probability tables based on previous MB type */
114
2/2
✓ Branch 0 taken 2751 times.
✓ Branch 1 taken 917 times.
3668 for (ctx=0; ctx<3; ctx++) {
115 int p[10];
116
117
2/2
✓ Branch 0 taken 27510 times.
✓ Branch 1 taken 2751 times.
30261 for (type=0; type<10; type++)
118 27510 p[type] = 100 * model->mb_types_stats[ctx][type][1];
119
120
2/2
✓ Branch 0 taken 27510 times.
✓ Branch 1 taken 2751 times.
30261 for (type=0; type<10; type++) {
121 int p02, p34, p0234, p17, p56, p89, p5689, p156789;
122
123 /* conservative MB type probability */
124 27510 model->mb_type[ctx][type][0] = 255 - (255 * model->mb_types_stats[ctx][type][0]) / (1 + model->mb_types_stats[ctx][type][0] + model->mb_types_stats[ctx][type][1]);
125
126 27510 p[type] = 0; /* same MB type => weight is null */
127
128 /* binary tree parsing probabilities */
129 27510 p02 = p[0] + p[2];
130 27510 p34 = p[3] + p[4];
131 27510 p0234 = p02 + p34;
132 27510 p17 = p[1] + p[7];
133 27510 p56 = p[5] + p[6];
134 27510 p89 = p[8] + p[9];
135 27510 p5689 = p56 + p89;
136 27510 p156789 = p17 + p5689;
137
138 27510 model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
139 27510 model->mb_type[ctx][type][2] = 1 + 255 * p02 / (1+p0234);
140 27510 model->mb_type[ctx][type][3] = 1 + 255 * p17 / (1+p156789);
141 27510 model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
142 27510 model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
143 27510 model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
144 27510 model->mb_type[ctx][type][7] = 1 + 255 * p56 / (1+p5689);
145 27510 model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
146 27510 model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
147
148 /* restore initial value */
149 27510 p[type] = 100 * model->mb_types_stats[ctx][type][1];
150 }
151 }
152 917 }
153
154 235607 static VP56mb vp56_parse_mb_type(VP56Context *s,
155 VP56mb prev_type, int ctx)
156 {
157 235607 uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
158 235607 VPXRangeCoder *c = &s->c;
159
160
2/2
✓ Branch 1 taken 178921 times.
✓ Branch 2 taken 56686 times.
235607 if (vpx_rac_get_prob_branchy(c, mb_type_model[0]))
161 178921 return prev_type;
162 else
163 56686 return vp56_rac_get_tree(c, ff_vp56_pmbt_tree, mb_type_model);
164 }
165
166 10514 static void vp56_decode_4mv(VP56Context *s, int row, int col)
167 {
168 10514 VP56mv mv = {0,0};
169 int type[4];
170 int b;
171
172 /* parse each block type */
173
2/2
✓ Branch 0 taken 42056 times.
✓ Branch 1 taken 10514 times.
52570 for (b=0; b<4; b++) {
174 42056 type[b] = vp56_rac_gets(&s->c, 2);
175
2/2
✓ Branch 0 taken 33494 times.
✓ Branch 1 taken 8562 times.
42056 if (type[b])
176 33494 type[b]++; /* only returns 0, 2, 3 or 4 (all INTER_PF) */
177 }
178
179 /* get vectors */
180
2/2
✓ Branch 0 taken 42056 times.
✓ Branch 1 taken 10514 times.
52570 for (b=0; b<4; b++) {
181
4/5
✓ Branch 0 taken 8562 times.
✓ Branch 1 taken 12581 times.
✓ Branch 2 taken 12473 times.
✓ Branch 3 taken 8440 times.
✗ Branch 4 not taken.
42056 switch (type[b]) {
182 8562 case VP56_MB_INTER_NOVEC_PF:
183 8562 s->mv[b] = (VP56mv) {0,0};
184 8562 break;
185 12581 case VP56_MB_INTER_DELTA_PF:
186 12581 s->parse_vector_adjustment(s, &s->mv[b]);
187 12581 break;
188 12473 case VP56_MB_INTER_V1_PF:
189 12473 s->mv[b] = s->vector_candidate[0];
190 12473 break;
191 8440 case VP56_MB_INTER_V2_PF:
192 8440 s->mv[b] = s->vector_candidate[1];
193 8440 break;
194 }
195 42056 mv.x += s->mv[b].x;
196 42056 mv.y += s->mv[b].y;
197 }
198
199 /* this is the one selected for the whole MB for prediction */
200 10514 s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
201
202 /* chroma vectors are average luma vectors */
203
2/2
✓ Branch 0 taken 4977 times.
✓ Branch 1 taken 5537 times.
10514 s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
204
2/2
✓ Branch 0 taken 4666 times.
✓ Branch 1 taken 5848 times.
10514 s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
205 10514 }
206
207 235607 static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
208 {
209 235607 VP56mv *mv, vect = {0,0};
210 int ctx, b;
211
212 235607 ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
213 235607 s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
214 235607 s->macroblocks[row * s->mb_width + col].type = s->mb_type;
215
216
8/8
✓ Branch 0 taken 36890 times.
✓ Branch 1 taken 11522 times.
✓ Branch 2 taken 1037 times.
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 14427 times.
✓ Branch 5 taken 1440 times.
✓ Branch 6 taken 10514 times.
✓ Branch 7 taken 159617 times.
235607 switch (s->mb_type) {
217 36890 case VP56_MB_INTER_V1_PF:
218 36890 mv = &s->vector_candidate[0];
219 36890 break;
220
221 11522 case VP56_MB_INTER_V2_PF:
222 11522 mv = &s->vector_candidate[1];
223 11522 break;
224
225 1037 case VP56_MB_INTER_V1_GF:
226 1037 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
227 1037 mv = &s->vector_candidate[0];
228 1037 break;
229
230 160 case VP56_MB_INTER_V2_GF:
231 160 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
232 160 mv = &s->vector_candidate[1];
233 160 break;
234
235 14427 case VP56_MB_INTER_DELTA_PF:
236 14427 s->parse_vector_adjustment(s, &vect);
237 14427 mv = &vect;
238 14427 break;
239
240 1440 case VP56_MB_INTER_DELTA_GF:
241 1440 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
242 1440 s->parse_vector_adjustment(s, &vect);
243 1440 mv = &vect;
244 1440 break;
245
246 10514 case VP56_MB_INTER_4V:
247 10514 vp56_decode_4mv(s, row, col);
248 10514 return s->mb_type;
249
250 159617 default:
251 159617 mv = &vect;
252 159617 break;
253 }
254
255 225093 s->macroblocks[row*s->mb_width + col].mv = *mv;
256
257 /* same vector for all blocks */
258
2/2
✓ Branch 0 taken 1350558 times.
✓ Branch 1 taken 225093 times.
1575651 for (b=0; b<6; b++)
259 1350558 s->mv[b] = *mv;
260
261 225093 return s->mb_type;
262 }
263
264 363 static VP56mb vp56_conceal_mv(VP56Context *s, int row, int col)
265 {
266 363 VP56mv *mv, vect = {0,0};
267 int b;
268
269 363 s->mb_type = VP56_MB_INTER_NOVEC_PF;
270 363 s->macroblocks[row * s->mb_width + col].type = s->mb_type;
271
272 363 mv = &vect;
273
274 363 s->macroblocks[row*s->mb_width + col].mv = *mv;
275
276 /* same vector for all blocks */
277
2/2
✓ Branch 0 taken 2178 times.
✓ Branch 1 taken 363 times.
2541 for (b=0; b<6; b++)
278 2178 s->mv[b] = *mv;
279
280 363 return s->mb_type;
281 }
282
283 245780 static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
284 {
285 245780 int idx = s->idct_scantable[0];
286 int b;
287
288
2/2
✓ Branch 0 taken 1474680 times.
✓ Branch 1 taken 245780 times.
1720460 for (b=0; b<6; b++) {
289 1474680 VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
290 1474680 VP56RefDc *lb = &s->left_block[ff_vp56_b6to4[b]];
291 1474680 int count = 0;
292 1474680 int dc = 0;
293 int i;
294
295
2/2
✓ Branch 0 taken 1389024 times.
✓ Branch 1 taken 85656 times.
1474680 if (ref_frame == lb->ref_frame) {
296 1389024 dc += lb->dc_coeff;
297 1389024 count++;
298 }
299
2/2
✓ Branch 0 taken 1357452 times.
✓ Branch 1 taken 117228 times.
1474680 if (ref_frame == ab->ref_frame) {
300 1357452 dc += ab->dc_coeff;
301 1357452 count++;
302 }
303
2/2
✓ Branch 0 taken 901056 times.
✓ Branch 1 taken 573624 times.
1474680 if (s->avctx->codec->id == AV_CODEC_ID_VP5)
304
2/2
✓ Branch 0 taken 1802112 times.
✓ Branch 1 taken 901056 times.
2703168 for (i=0; i<2; i++)
305
4/4
✓ Branch 0 taken 107317 times.
✓ Branch 1 taken 1694795 times.
✓ Branch 2 taken 72112 times.
✓ Branch 3 taken 35205 times.
1802112 if (count < 2 && ref_frame == ab[-1+2*i].ref_frame) {
306 72112 dc += ab[-1+2*i].dc_coeff;
307 72112 count++;
308 }
309
2/2
✓ Branch 0 taken 18295 times.
✓ Branch 1 taken 1456385 times.
1474680 if (count == 0)
310 18295 dc = s->prev_dc[ff_vp56_b2p[b]][ref_frame];
311
2/2
✓ Branch 0 taken 1362203 times.
✓ Branch 1 taken 94182 times.
1456385 else if (count == 2)
312 1362203 dc /= 2;
313
314 1474680 s->block_coeff[b][idx] += dc;
315 1474680 s->prev_dc[ff_vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
316 1474680 ab->dc_coeff = s->block_coeff[b][idx];
317 1474680 ab->ref_frame = ref_frame;
318 1474680 lb->dc_coeff = s->block_coeff[b][idx];
319 1474680 lb->ref_frame = ref_frame;
320 1474680 s->block_coeff[b][idx] *= s->dequant_dc;
321 }
322 245780 }
323
324 423352 static void vp56_deblock_filter(VP56Context *s, uint8_t *yuv,
325 ptrdiff_t stride, int dx, int dy)
326 {
327
2/2
✓ Branch 0 taken 249618 times.
✓ Branch 1 taken 173734 times.
423352 if (s->avctx->codec->id == AV_CODEC_ID_VP5) {
328 249618 int t = ff_vp56_filter_threshold[s->quantizer];
329
2/2
✓ Branch 0 taken 84139 times.
✓ Branch 1 taken 165479 times.
249618 if (dx) s->vp56dsp.edge_filter_hor(yuv + 10-dx , stride, t);
330
2/2
✓ Branch 0 taken 25724 times.
✓ Branch 1 taken 223894 times.
249618 if (dy) s->vp56dsp.edge_filter_ver(yuv + stride*(10-dy), stride, t);
331 } else {
332 173734 int * bounding_values = s->bounding_values_array + 127;
333
2/2
✓ Branch 0 taken 66268 times.
✓ Branch 1 taken 107466 times.
173734 if (dx)
334 66268 ff_vp3dsp_h_loop_filter_12(yuv + 10-dx, stride, bounding_values);
335
2/2
✓ Branch 0 taken 63282 times.
✓ Branch 1 taken 110452 times.
173734 if (dy)
336 63282 ff_vp3dsp_v_loop_filter_12(yuv + stride*(10-dy), stride, bounding_values);
337 }
338 423352 }
339
340 443626 static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
341 ptrdiff_t stride, int x, int y)
342 {
343 443626 uint8_t *dst = s->frames[VP56_FRAME_CURRENT]->data[plane] + s->block_offset[b];
344 uint8_t *src_block;
345 int src_offset;
346 443626 int overlap_offset = 0;
347 443626 int mask = s->vp56_coord_div[b] - 1;
348 443626 int deblock_filtering = s->deblock_filtering;
349 int dx;
350 int dy;
351
352
1/2
✓ Branch 0 taken 443626 times.
✗ Branch 1 not taken.
443626 if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 443626 times.
443626 (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
354 && !(s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)))
355 deblock_filtering = 0;
356
357 443626 dx = s->mv[b].x / s->vp56_coord_div[b];
358 443626 dy = s->mv[b].y / s->vp56_coord_div[b];
359
360
2/2
✓ Branch 0 taken 139670 times.
✓ Branch 1 taken 303956 times.
443626 if (b >= 4) {
361 139670 x /= 2;
362 139670 y /= 2;
363 }
364 443626 x += dx - 2;
365 443626 y += dy - 2;
366
367
6/6
✓ Branch 0 taken 436152 times.
✓ Branch 1 taken 7474 times.
✓ Branch 2 taken 422737 times.
✓ Branch 3 taken 13415 times.
✓ Branch 4 taken 413182 times.
✓ Branch 5 taken 9555 times.
443626 if (x<0 || x+12>=s->plane_width[plane] ||
368
2/2
✓ Branch 0 taken 19416 times.
✓ Branch 1 taken 393766 times.
413182 y<0 || y+12>=s->plane_height[plane]) {
369 49860 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
370 49860 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
371 stride, stride,
372 12, 12, x, y,
373 s->plane_width[plane],
374 s->plane_height[plane]);
375 49860 src_block = s->edge_emu_buffer;
376 49860 src_offset = 2 + 2*stride;
377
2/2
✓ Branch 0 taken 375310 times.
✓ Branch 1 taken 18456 times.
393766 } else if (deblock_filtering) {
378 /* only need a 12x12 block, but there is no such dsp function, */
379 /* so copy a 16x12 block */
380 375310 s->hdsp.put_pixels_tab[0][0](s->edge_emu_buffer,
381 375310 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
382 stride, 12);
383 375310 src_block = s->edge_emu_buffer;
384 375310 src_offset = 2 + 2*stride;
385 } else {
386 18456 src_block = src;
387 18456 src_offset = s->block_offset[b] + dy*stride + dx;
388 }
389
390
2/2
✓ Branch 0 taken 423352 times.
✓ Branch 1 taken 20274 times.
443626 if (deblock_filtering)
391 423352 vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
392
393
2/2
✓ Branch 0 taken 282743 times.
✓ Branch 1 taken 160883 times.
443626 if (s->mv[b].x & mask)
394
2/2
✓ Branch 0 taken 158355 times.
✓ Branch 1 taken 124388 times.
282743 overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
395
2/2
✓ Branch 0 taken 277885 times.
✓ Branch 1 taken 165741 times.
443626 if (s->mv[b].y & mask)
396
2/2
✓ Branch 0 taken 125767 times.
✓ Branch 1 taken 152118 times.
277885 overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
397
398
2/2
✓ Branch 0 taken 387482 times.
✓ Branch 1 taken 56144 times.
443626 if (overlap_offset) {
399
2/2
✓ Branch 0 taken 171363 times.
✓ Branch 1 taken 216119 times.
387482 if (s->filter)
400 171363 s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
401 stride, s->mv[b], mask, s->filter_selection, b<4);
402 else
403 216119 s->vp3dsp.put_no_rnd_pixels_l2(dst, src_block+src_offset,
404 216119 src_block+src_offset+overlap_offset,
405 stride, 8);
406 } else {
407 56144 s->hdsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
408 }
409 443626 }
410
411 197664 static void vp56_idct_put(VP56Context *s, uint8_t * dest, ptrdiff_t stride, int16_t *block, int selector)
412 {
413
3/4
✓ Branch 0 taken 34944 times.
✓ Branch 1 taken 162720 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34944 times.
197664 if (selector > 10 || selector == 1)
414 162720 s->vp3dsp.idct_put(dest, stride, block);
415 else
416 34944 ff_vp3dsp_idct10_put(dest, stride, block);
417 197664 }
418
419 1234608 static void vp56_idct_add(VP56Context *s, uint8_t * dest, ptrdiff_t stride, int16_t *block, int selector)
420 {
421
2/2
✓ Branch 0 taken 865309 times.
✓ Branch 1 taken 369299 times.
1234608 if (selector > 10)
422 865309 s->vp3dsp.idct_add(dest, stride, block);
423
1/2
✓ Branch 0 taken 369299 times.
✗ Branch 1 not taken.
369299 else if (selector > 1)
424 369299 ff_vp3dsp_idct10_add(dest, stride, block);
425 else
426 s->vp3dsp.idct_dc_add(dest, stride, block);
427 1234608 }
428
429 245780 static av_always_inline void vp56_render_mb(VP56Context *s, int row, int col, int is_alpha, VP56mb mb_type)
430 {
431 int b, ab, b_max, plane, off;
432 AVFrame *frame_current, *frame_ref;
433 245780 VP56Frame ref_frame = ff_vp56_reference_frame[mb_type];
434
435 245780 vp56_add_predictors_dc(s, ref_frame);
436
437 245780 frame_current = s->frames[VP56_FRAME_CURRENT];
438 245780 frame_ref = s->frames[ref_frame];
439
3/4
✓ Branch 0 taken 212268 times.
✓ Branch 1 taken 33512 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 212268 times.
245780 if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
440 return;
441
442 245780 ab = 6*is_alpha;
443 245780 b_max = 6 - 2*is_alpha;
444
445
3/4
✓ Branch 0 taken 33512 times.
✓ Branch 1 taken 136279 times.
✓ Branch 2 taken 75989 times.
✗ Branch 3 not taken.
245780 switch (mb_type) {
446 33512 case VP56_MB_INTRA:
447
2/2
✓ Branch 0 taken 197664 times.
✓ Branch 1 taken 33512 times.
231176 for (b=0; b<b_max; b++) {
448 197664 plane = ff_vp56_b2p[b+ab];
449 197664 vp56_idct_put(s, frame_current->data[plane] + s->block_offset[b],
450 197664 s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
451 }
452 33512 break;
453
454 136279 case VP56_MB_INTER_NOVEC_PF:
455 case VP56_MB_INTER_NOVEC_GF:
456
2/2
✓ Branch 0 taken 790982 times.
✓ Branch 1 taken 136279 times.
927261 for (b=0; b<b_max; b++) {
457 790982 plane = ff_vp56_b2p[b+ab];
458 790982 off = s->block_offset[b];
459 790982 s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
460 790982 frame_ref->data[plane] + off,
461 s->stride[plane], 8);
462 790982 vp56_idct_add(s, frame_current->data[plane] + off,
463 790982 s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
464 }
465 136279 break;
466
467 75989 case VP56_MB_INTER_DELTA_PF:
468 case VP56_MB_INTER_V1_PF:
469 case VP56_MB_INTER_V2_PF:
470 case VP56_MB_INTER_DELTA_GF:
471 case VP56_MB_INTER_4V:
472 case VP56_MB_INTER_V1_GF:
473 case VP56_MB_INTER_V2_GF:
474
2/2
✓ Branch 0 taken 443626 times.
✓ Branch 1 taken 75989 times.
519615 for (b=0; b<b_max; b++) {
475
4/4
✓ Branch 0 taken 367637 times.
✓ Branch 1 taken 75989 times.
✓ Branch 2 taken 75989 times.
✓ Branch 3 taken 291648 times.
443626 int x_off = b==1 || b==3 ? 8 : 0;
476
4/4
✓ Branch 0 taken 367637 times.
✓ Branch 1 taken 75989 times.
✓ Branch 2 taken 75989 times.
✓ Branch 3 taken 291648 times.
443626 int y_off = b==2 || b==3 ? 8 : 0;
477 443626 plane = ff_vp56_b2p[b+ab];
478 443626 vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
479 443626 16*col+x_off, 16*row+y_off);
480 443626 vp56_idct_add(s, frame_current->data[plane] + s->block_offset[b],
481 443626 s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
482 }
483 75989 break;
484 }
485
486
2/2
✓ Branch 0 taken 21204 times.
✓ Branch 1 taken 224576 times.
245780 if (is_alpha) {
487 21204 s->block_coeff[4][0] = 0;
488 21204 s->block_coeff[5][0] = 0;
489 }
490 }
491
492 245418 static int vp56_decode_mb(VP56Context *s, int row, int col, int is_alpha)
493 {
494 VP56mb mb_type;
495 int ret;
496
497
2/2
✓ Branch 0 taken 9811 times.
✓ Branch 1 taken 235607 times.
245418 if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)
498 9811 mb_type = VP56_MB_INTRA;
499 else
500 235607 mb_type = vp56_decode_mv(s, row, col);
501
502 245418 ret = s->parse_coeff(s);
503
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 245417 times.
245418 if (ret < 0)
504 1 return ret;
505
506 245417 vp56_render_mb(s, row, col, is_alpha, mb_type);
507
508 245417 return 0;
509 }
510
511 363 static int vp56_conceal_mb(VP56Context *s, int row, int col, int is_alpha)
512 {
513 VP56mb mb_type;
514
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)
516 mb_type = VP56_MB_INTRA;
517 else
518 363 mb_type = vp56_conceal_mv(s, row, col);
519
520 363 vp56_render_mb(s, row, col, is_alpha, mb_type);
521
522 363 return 0;
523 }
524
525 14 static int vp56_size_changed(VP56Context *s)
526 {
527 14 AVCodecContext *avctx = s->avctx;
528 14 int stride = s->frames[VP56_FRAME_CURRENT]->linesize[0];
529 int i;
530
531 14 s->plane_width[0] = s->plane_width[3] = avctx->coded_width;
532 14 s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
533 14 s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
534 14 s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
535
536 14 s->have_undamaged_frame = 0;
537
538
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 14 times.
70 for (i=0; i<4; i++)
539 56 s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT]->linesize[i];
540
541 14 s->mb_width = (avctx->coded_width +15) / 16;
542 14 s->mb_height = (avctx->coded_height+15) / 16;
543
544
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (s->mb_width > 1000 || s->mb_height > 1000) {
545 ff_set_dimensions(avctx, 0, 0);
546 av_log(avctx, AV_LOG_ERROR, "picture too big\n");
547 return AVERROR_INVALIDDATA;
548 }
549
550 14 av_reallocp_array(&s->above_blocks, 4*s->mb_width+6,
551 sizeof(*s->above_blocks));
552 14 av_reallocp_array(&s->macroblocks, s->mb_width*s->mb_height,
553 sizeof(*s->macroblocks));
554 14 av_free(s->edge_emu_buffer_alloc);
555 14 s->edge_emu_buffer_alloc = av_malloc(16*stride);
556 14 s->edge_emu_buffer = s->edge_emu_buffer_alloc;
557
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
14 if (!s->above_blocks || !s->macroblocks || !s->edge_emu_buffer_alloc)
558 return AVERROR(ENOMEM);
559
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 if (s->flip < 0)
560 8 s->edge_emu_buffer += 15 * stride;
561
562
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (s->alpha_context)
563 2 return vp56_size_changed(s->alpha_context);
564
565 12 return 0;
566 }
567
568 static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *, int, int);
569
570 868 int ff_vp56_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
571 int *got_frame, AVPacket *avpkt)
572 {
573 868 const uint8_t *buf = avpkt->data;
574 868 VP56Context *s = avctx->priv_data;
575 868 AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
576 868 int remaining_buf_size = avpkt->size;
577 868 int alpha_offset = remaining_buf_size;
578 int i, res;
579 int ret;
580
581
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 682 times.
868 if (s->has_alpha) {
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (remaining_buf_size < 3)
583 return AVERROR_INVALIDDATA;
584 186 alpha_offset = bytestream_get_be24(&buf);
585 186 remaining_buf_size -= 3;
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (remaining_buf_size < alpha_offset)
587 return AVERROR_INVALIDDATA;
588 }
589
590 868 res = s->parse_header(s, buf, alpha_offset);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 868 times.
868 if (res < 0)
592 return res;
593
594
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 856 times.
868 if (res == VP56_SIZE_CHANGE) {
595
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (i = 0; i < 4; i++) {
596 48 av_frame_unref(s->frames[i]);
597
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 40 times.
48 if (s->alpha_context)
598 8 av_frame_unref(s->alpha_context->frames[i]);
599 }
600 12 s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY; //FIXME
601 }
602
603 868 ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF);
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 868 times.
868 if (ret < 0) {
605 if (res == VP56_SIZE_CHANGE)
606 ff_set_dimensions(avctx, 0, 0);
607 return ret;
608 }
609
610
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 775 times.
868 if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
611
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
93 if ((ret = av_frame_replace(s->alpha_context->frames[VP56_FRAME_CURRENT], p)) < 0) {
612 av_frame_unref(p);
613 if (res == VP56_SIZE_CHANGE)
614 ff_set_dimensions(avctx, 0, 0);
615 return ret;
616 }
617 }
618
619
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 856 times.
868 if (res == VP56_SIZE_CHANGE) {
620
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (vp56_size_changed(s)) {
621 av_frame_unref(p);
622 return AVERROR_INVALIDDATA;
623 }
624 }
625
626
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 775 times.
868 if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
627 93 int bak_w = avctx->width;
628 93 int bak_h = avctx->height;
629 93 int bak_cw = avctx->coded_width;
630 93 int bak_ch = avctx->coded_height;
631 93 buf += alpha_offset;
632 93 remaining_buf_size -= alpha_offset;
633
634 93 res = s->alpha_context->parse_header(s->alpha_context, buf, remaining_buf_size);
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (res != 0) {
636 if(res==VP56_SIZE_CHANGE) {
637 av_log(avctx, AV_LOG_ERROR, "Alpha reconfiguration\n");
638 avctx->width = bak_w;
639 avctx->height = bak_h;
640 avctx->coded_width = bak_cw;
641 avctx->coded_height = bak_ch;
642 }
643 av_frame_unref(p);
644 return AVERROR_INVALIDDATA;
645 }
646 }
647
648 868 s->discard_frame = 0;
649
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 775 times.
868 avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) + 1);
650
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 868 times.
868 if (s->discard_frame)
652 return AVERROR_INVALIDDATA;
653
654
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 868 times.
868 if ((res = av_frame_ref(rframe, p)) < 0)
655 return res;
656 868 *got_frame = 1;
657
658 868 return avpkt->size;
659 }
660
661 961 static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *data,
662 int jobnr, int threadnr)
663 {
664 961 VP56Context *s0 = avctx->priv_data;
665 961 int is_alpha = (jobnr == 1);
666
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 868 times.
961 VP56Context *s = is_alpha ? s0->alpha_context : s0;
667 961 AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
668 961 int mb_row, mb_col, mb_row_flip, mb_offset = 0;
669 int block, y, uv;
670 ptrdiff_t stride_y, stride_uv;
671 int res;
672 961 int damaged = 0;
673
674
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 917 times.
961 if (p->flags & AV_FRAME_FLAG_KEY) {
675 44 p->pict_type = AV_PICTURE_TYPE_I;
676 44 s->default_models_init(s);
677
2/2
✓ Branch 0 taken 9811 times.
✓ Branch 1 taken 44 times.
9855 for (block=0; block<s->mb_height*s->mb_width; block++)
678 9811 s->macroblocks[block].type = VP56_MB_INTRA;
679 } else {
680 917 p->pict_type = AV_PICTURE_TYPE_P;
681 917 vp56_parse_mb_type_models(s);
682 917 s->parse_vector_models(s);
683 917 s->mb_type = VP56_MB_INTER_NOVEC_PF;
684 }
685
686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 961 times.
961 if (s->parse_coeff_models(s))
687 goto next;
688
689 961 memset(s->prev_dc, 0, sizeof(s->prev_dc));
690 961 s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
691 961 s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
692
693
2/2
✓ Branch 0 taken 73522 times.
✓ Branch 1 taken 961 times.
74483 for (block=0; block < 4*s->mb_width+6; block++) {
694 73522 s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
695 73522 s->above_blocks[block].dc_coeff = 0;
696 73522 s->above_blocks[block].not_null_dc = 0;
697 }
698 961 s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
699 961 s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
700
701 961 stride_y = p->linesize[0];
702 961 stride_uv = p->linesize[1];
703
704
2/2
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 454 times.
961 if (s->flip < 0)
705 507 mb_offset = 7;
706
707 /* main macroblocks loop */
708
2/2
✓ Branch 0 taken 11409 times.
✓ Branch 1 taken 961 times.
12370 for (mb_row=0; mb_row<s->mb_height; mb_row++) {
709
2/2
✓ Branch 0 taken 7186 times.
✓ Branch 1 taken 4223 times.
11409 if (s->flip < 0)
710 7186 mb_row_flip = s->mb_height - mb_row - 1;
711 else
712 4223 mb_row_flip = mb_row;
713
714
2/2
✓ Branch 0 taken 45636 times.
✓ Branch 1 taken 11409 times.
57045 for (block=0; block<4; block++) {
715 45636 s->left_block[block].ref_frame = VP56_FRAME_NONE;
716 45636 s->left_block[block].dc_coeff = 0;
717 45636 s->left_block[block].not_null_dc = 0;
718 }
719 11409 memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
720 11409 memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
721
722 11409 s->above_block_idx[0] = 1;
723 11409 s->above_block_idx[1] = 2;
724 11409 s->above_block_idx[2] = 1;
725 11409 s->above_block_idx[3] = 2;
726 11409 s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
727 11409 s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
728
729 11409 s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
730 11409 s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
731 11409 s->block_offset[1] = s->block_offset[0] + 8;
732 11409 s->block_offset[3] = s->block_offset[2] + 8;
733 11409 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
734 11409 s->block_offset[5] = s->block_offset[4];
735
736
2/2
✓ Branch 0 taken 245780 times.
✓ Branch 1 taken 11409 times.
257189 for (mb_col=0; mb_col<s->mb_width; mb_col++) {
737
2/2
✓ Branch 0 taken 245418 times.
✓ Branch 1 taken 362 times.
245780 if (!damaged) {
738 245418 int ret = vp56_decode_mb(s, mb_row, mb_col, is_alpha);
739
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 245417 times.
245418 if (ret < 0) {
740 1 damaged = 1;
741
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!s->have_undamaged_frame || !avctx->error_concealment) {
742 s->discard_frame = 1;
743 return AVERROR_INVALIDDATA;
744 }
745 }
746 }
747
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 245417 times.
245780 if (damaged)
748 363 vp56_conceal_mb(s, mb_row, mb_col, is_alpha);
749
750
2/2
✓ Branch 0 taken 983120 times.
✓ Branch 1 taken 245780 times.
1228900 for (y=0; y<4; y++) {
751 983120 s->above_block_idx[y] += 2;
752 983120 s->block_offset[y] += 16;
753 }
754
755
2/2
✓ Branch 0 taken 491560 times.
✓ Branch 1 taken 245780 times.
737340 for (uv=4; uv<6; uv++) {
756 491560 s->above_block_idx[uv] += 1;
757 491560 s->block_offset[uv] += 8;
758 }
759 }
760 }
761
762
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 960 times.
961 if (!damaged)
763 960 s->have_undamaged_frame = 1;
764
765 1 next:
766
4/4
✓ Branch 0 taken 917 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 884 times.
961 if ((p->flags & AV_FRAME_FLAG_KEY) || s->golden_frame) {
767
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if ((res = av_frame_replace(s->frames[VP56_FRAME_GOLDEN], p)) < 0)
768 return res;
769 }
770
771 961 av_frame_unref(s->frames[VP56_FRAME_PREVIOUS]);
772 961 FFSWAP(AVFrame *, s->frames[VP56_FRAME_CURRENT],
773 s->frames[VP56_FRAME_PREVIOUS]);
774 961 return 0;
775 }
776
777 21 av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
778 int flip, int has_alpha)
779 {
780 int i;
781
782 21 s->avctx = avctx;
783
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 13 times.
21 avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
784
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
21 if (avctx->skip_alpha) avctx->pix_fmt = AV_PIX_FMT_YUV420P;
785
786 21 ff_h264chroma_init(&s->h264chroma, 8);
787 21 ff_hpeldsp_init(&s->hdsp, avctx->flags);
788 21 ff_videodsp_init(&s->vdsp, 8);
789 21 ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
790
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 21 times.
1365 for (i = 0; i < 64; i++) {
791 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
792 1344 s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
793 #undef TRANSPOSE
794 }
795
796
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 21 times.
105 for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
797 84 s->frames[i] = av_frame_alloc();
798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (!s->frames[i])
799 return AVERROR(ENOMEM);
800 }
801 21 s->edge_emu_buffer_alloc = NULL;
802
803 21 s->above_blocks = NULL;
804 21 s->macroblocks = NULL;
805 21 s->quantizer = -1;
806 21 s->deblock_filtering = 1;
807 21 s->golden_frame = 0;
808
809 21 s->filter = NULL;
810
811 21 s->has_alpha = has_alpha;
812
813 21 s->modelp = &s->model;
814
815
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (flip) {
816 11 s->flip = -1;
817 11 s->frbi = 2;
818 11 s->srbi = 0;
819 } else {
820 10 s->flip = 1;
821 10 s->frbi = 0;
822 10 s->srbi = 2;
823 }
824
825 21 return 0;
826 }
827
828 21 av_cold int ff_vp56_free_context(VP56Context *s)
829 {
830 int i;
831
832 21 av_freep(&s->above_blocks);
833 21 av_freep(&s->macroblocks);
834 21 av_freep(&s->edge_emu_buffer_alloc);
835
836
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 21 times.
105 for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
837 84 av_frame_free(&s->frames[i]);
838
839 21 return 0;
840 }
841