FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp56.c
Date: 2024-04-19 07:31:02
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 963 void ff_vp56_init_dequant(VP56Context *s, int quantizer)
37 {
38
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 555 times.
963 if (s->quantizer != quantizer)
39 408 ff_vp3dsp_set_bounding_values(s->bounding_values_array, ff_vp56_filter_threshold[quantizer]);
40 963 s->quantizer = quantizer;
41 963 s->dequant_dc = ff_vp56_dc_dequant[quantizer] << 2;
42 963 s->dequant_ac = ff_vp56_ac_dequant[quantizer] << 2;
43 963 }
44
45 238360 static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
46 VP56Frame ref_frame)
47 {
48 238360 int nb_pred = 0;
49 238360 VP56mv vect[2] = {{0,0}, {0,0}};
50 int pos, offset;
51 VP56mv mvp;
52
53
2/2
✓ Branch 0 taken 2279672 times.
✓ Branch 1 taken 161351 times.
2441023 for (pos=0; pos<12; pos++) {
54 2279672 mvp.x = col + ff_vp56_candidate_predictor_pos[pos][0];
55 2279672 mvp.y = row + ff_vp56_candidate_predictor_pos[pos][1];
56
4/4
✓ Branch 0 taken 2186427 times.
✓ Branch 1 taken 93245 times.
✓ Branch 2 taken 2132045 times.
✓ Branch 3 taken 54382 times.
2279672 if (mvp.x < 0 || mvp.x >= s->mb_width ||
57
3/4
✓ Branch 0 taken 1916389 times.
✓ Branch 1 taken 215656 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1916389 times.
2132045 mvp.y < 0 || mvp.y >= s->mb_height)
58 363283 continue;
59 1916389 offset = mvp.x + s->mb_width*mvp.y;
60
61
2/2
✓ Branch 0 taken 331945 times.
✓ Branch 1 taken 1584444 times.
1916389 if (ff_vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
62 331945 continue;
63
2/2
✓ Branch 0 taken 1294021 times.
✓ Branch 1 taken 290423 times.
1584444 if ((s->macroblocks[offset].mv.x == vect[0].x &&
64
2/2
✓ Branch 0 taken 76226 times.
✓ Branch 1 taken 1217795 times.
1294021 s->macroblocks[offset].mv.y == vect[0].y) ||
65
2/2
✓ Branch 0 taken 208016 times.
✓ Branch 1 taken 158633 times.
366649 (s->macroblocks[offset].mv.x == 0 &&
66
2/2
✓ Branch 0 taken 177181 times.
✓ Branch 1 taken 30835 times.
208016 s->macroblocks[offset].mv.y == 0))
67 1394976 continue;
68
69 189468 vect[nb_pred++] = s->macroblocks[offset].mv;
70
2/2
✓ Branch 0 taken 77009 times.
✓ Branch 1 taken 112459 times.
189468 if (nb_pred > 1) {
71 77009 nb_pred = -1;
72 77009 break;
73 }
74 112459 s->vector_candidate_pos = pos;
75 }
76
77 238360 s->vector_candidate[0] = vect[0];
78 238360 s->vector_candidate[1] = vect[1];
79
80 238360 return nb_pred+1;
81 }
82
83 919 static void vp56_parse_mb_type_models(VP56Context *s)
84 {
85 919 VPXRangeCoder *c = &s->c;
86 919 VP56Model *model = s->modelp;
87 int i, ctx, type;
88
89
2/2
✓ Branch 0 taken 2757 times.
✓ Branch 1 taken 919 times.
3676 for (ctx=0; ctx<3; ctx++) {
90
2/2
✓ Branch 1 taken 730 times.
✓ Branch 2 taken 2027 times.
2757 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 2640 times.
2757 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 2757 times.
✓ Branch 1 taken 919 times.
3676 for (ctx=0; ctx<3; ctx++) {
115 int p[10];
116
117
2/2
✓ Branch 0 taken 27570 times.
✓ Branch 1 taken 2757 times.
30327 for (type=0; type<10; type++)
118 27570 p[type] = 100 * model->mb_types_stats[ctx][type][1];
119
120
2/2
✓ Branch 0 taken 27570 times.
✓ Branch 1 taken 2757 times.
30327 for (type=0; type<10; type++) {
121 int p02, p34, p0234, p17, p56, p89, p5689, p156789;
122
123 /* conservative MB type probability */
124 27570 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 27570 p[type] = 0; /* same MB type => weight is null */
127
128 /* binary tree parsing probabilities */
129 27570 p02 = p[0] + p[2];
130 27570 p34 = p[3] + p[4];
131 27570 p0234 = p02 + p34;
132 27570 p17 = p[1] + p[7];
133 27570 p56 = p[5] + p[6];
134 27570 p89 = p[8] + p[9];
135 27570 p5689 = p56 + p89;
136 27570 p156789 = p17 + p5689;
137
138 27570 model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
139 27570 model->mb_type[ctx][type][2] = 1 + 255 * p02 / (1+p0234);
140 27570 model->mb_type[ctx][type][3] = 1 + 255 * p17 / (1+p156789);
141 27570 model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
142 27570 model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
143 27570 model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
144 27570 model->mb_type[ctx][type][7] = 1 + 255 * p56 / (1+p5689);
145 27570 model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
146 27570 model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
147
148 /* restore initial value */
149 27570 p[type] = 100 * model->mb_types_stats[ctx][type][1];
150 }
151 }
152 919 }
153
154 235705 static VP56mb vp56_parse_mb_type(VP56Context *s,
155 VP56mb prev_type, int ctx)
156 {
157 235705 uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
158 235705 VPXRangeCoder *c = &s->c;
159
160
2/2
✓ Branch 1 taken 178979 times.
✓ Branch 2 taken 56726 times.
235705 if (vpx_rac_get_prob_branchy(c, mb_type_model[0]))
161 178979 return prev_type;
162 else
163 56726 return vp56_rac_get_tree(c, ff_vp56_pmbt_tree, mb_type_model);
164 }
165
166 10525 static void vp56_decode_4mv(VP56Context *s, int row, int col)
167 {
168 10525 VP56mv mv = {0,0};
169 int type[4];
170 int b;
171
172 /* parse each block type */
173
2/2
✓ Branch 0 taken 42100 times.
✓ Branch 1 taken 10525 times.
52625 for (b=0; b<4; b++) {
174 42100 type[b] = vp56_rac_gets(&s->c, 2);
175
2/2
✓ Branch 0 taken 33512 times.
✓ Branch 1 taken 8588 times.
42100 if (type[b])
176 33512 type[b]++; /* only returns 0, 2, 3 or 4 (all INTER_PF) */
177 }
178
179 /* get vectors */
180
2/2
✓ Branch 0 taken 42100 times.
✓ Branch 1 taken 10525 times.
52625 for (b=0; b<4; b++) {
181
4/5
✓ Branch 0 taken 8588 times.
✓ Branch 1 taken 12581 times.
✓ Branch 2 taken 12491 times.
✓ Branch 3 taken 8440 times.
✗ Branch 4 not taken.
42100 switch (type[b]) {
182 8588 case VP56_MB_INTER_NOVEC_PF:
183 8588 s->mv[b] = (VP56mv) {0,0};
184 8588 break;
185 12581 case VP56_MB_INTER_DELTA_PF:
186 12581 s->parse_vector_adjustment(s, &s->mv[b]);
187 12581 break;
188 12491 case VP56_MB_INTER_V1_PF:
189 12491 s->mv[b] = s->vector_candidate[0];
190 12491 break;
191 8440 case VP56_MB_INTER_V2_PF:
192 8440 s->mv[b] = s->vector_candidate[1];
193 8440 break;
194 }
195 42100 mv.x += s->mv[b].x;
196 42100 mv.y += s->mv[b].y;
197 }
198
199 /* this is the one selected for the whole MB for prediction */
200 10525 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 5548 times.
10525 s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
204
2/2
✓ Branch 0 taken 4666 times.
✓ Branch 1 taken 5859 times.
10525 s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
205 10525 }
206
207 235705 static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
208 {
209 235705 VP56mv *mv, vect = {0,0};
210 int ctx, b;
211
212 235705 ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
213 235705 s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
214 235705 s->macroblocks[row * s->mb_width + col].type = s->mb_type;
215
216
8/8
✓ Branch 0 taken 36931 times.
✓ Branch 1 taken 11522 times.
✓ Branch 2 taken 1054 times.
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 14429 times.
✓ Branch 5 taken 1441 times.
✓ Branch 6 taken 10525 times.
✓ Branch 7 taken 159643 times.
235705 switch (s->mb_type) {
217 36931 case VP56_MB_INTER_V1_PF:
218 36931 mv = &s->vector_candidate[0];
219 36931 break;
220
221 11522 case VP56_MB_INTER_V2_PF:
222 11522 mv = &s->vector_candidate[1];
223 11522 break;
224
225 1054 case VP56_MB_INTER_V1_GF:
226 1054 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
227 1054 mv = &s->vector_candidate[0];
228 1054 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 14429 case VP56_MB_INTER_DELTA_PF:
236 14429 s->parse_vector_adjustment(s, &vect);
237 14429 mv = &vect;
238 14429 break;
239
240 1441 case VP56_MB_INTER_DELTA_GF:
241 1441 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
242 1441 s->parse_vector_adjustment(s, &vect);
243 1441 mv = &vect;
244 1441 break;
245
246 10525 case VP56_MB_INTER_4V:
247 10525 vp56_decode_4mv(s, row, col);
248 10525 return s->mb_type;
249
250 159643 default:
251 159643 mv = &vect;
252 159643 break;
253 }
254
255 225180 s->macroblocks[row*s->mb_width + col].mv = *mv;
256
257 /* same vector for all blocks */
258
2/2
✓ Branch 0 taken 1351080 times.
✓ Branch 1 taken 225180 times.
1576260 for (b=0; b<6; b++)
259 1351080 s->mv[b] = *mv;
260
261 225180 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 245878 static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
284 {
285 245878 int idx = s->idct_scantable[0];
286 int b;
287
288
2/2
✓ Branch 0 taken 1475268 times.
✓ Branch 1 taken 245878 times.
1721146 for (b=0; b<6; b++) {
289 1475268 VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
290 1475268 VP56RefDc *lb = &s->left_block[ff_vp56_b6to4[b]];
291 1475268 int count = 0;
292 1475268 int dc = 0;
293 int i;
294
295
2/2
✓ Branch 0 taken 1389496 times.
✓ Branch 1 taken 85772 times.
1475268 if (ref_frame == lb->ref_frame) {
296 1389496 dc += lb->dc_coeff;
297 1389496 count++;
298 }
299
2/2
✓ Branch 0 taken 1357872 times.
✓ Branch 1 taken 117396 times.
1475268 if (ref_frame == ab->ref_frame) {
300 1357872 dc += ab->dc_coeff;
301 1357872 count++;
302 }
303
2/2
✓ Branch 0 taken 901056 times.
✓ Branch 1 taken 574212 times.
1475268 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 18343 times.
✓ Branch 1 taken 1456925 times.
1475268 if (count == 0)
310 18343 dc = s->prev_dc[ff_vp56_b2p[b]][ref_frame];
311
2/2
✓ Branch 0 taken 1362555 times.
✓ Branch 1 taken 94370 times.
1456925 else if (count == 2)
312 1362555 dc /= 2;
313
314 1475268 s->block_coeff[b][idx] += dc;
315 1475268 s->prev_dc[ff_vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
316 1475268 ab->dc_coeff = s->block_coeff[b][idx];
317 1475268 ab->ref_frame = ref_frame;
318 1475268 lb->dc_coeff = s->block_coeff[b][idx];
319 1475268 lb->ref_frame = ref_frame;
320 1475268 s->block_coeff[b][idx] *= s->dequant_dc;
321 }
322 245878 }
323
324 423784 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 174166 times.
423784 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 174166 int * bounding_values = s->bounding_values_array + 127;
333
2/2
✓ Branch 0 taken 66268 times.
✓ Branch 1 taken 107898 times.
174166 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 110884 times.
174166 if (dy)
336 63282 ff_vp3dsp_v_loop_filter_12(yuv + stride*(10-dy), stride, bounding_values);
337 }
338 423784 }
339
340 444058 static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
341 ptrdiff_t stride, int x, int y)
342 {
343 444058 uint8_t *dst = s->frames[VP56_FRAME_CURRENT]->data[plane] + s->block_offset[b];
344 uint8_t *src_block;
345 int src_offset;
346 444058 int overlap_offset = 0;
347 444058 int mask = s->vp56_coord_div[b] - 1;
348 444058 int deblock_filtering = s->deblock_filtering;
349 int dx;
350 int dy;
351
352
1/2
✓ Branch 0 taken 444058 times.
✗ Branch 1 not taken.
444058 if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 444058 times.
444058 (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
354 && !(s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)))
355 deblock_filtering = 0;
356
357 444058 dx = s->mv[b].x / s->vp56_coord_div[b];
358 444058 dy = s->mv[b].y / s->vp56_coord_div[b];
359
360
2/2
✓ Branch 0 taken 139814 times.
✓ Branch 1 taken 304244 times.
444058 if (b >= 4) {
361 139814 x /= 2;
362 139814 y /= 2;
363 }
364 444058 x += dx - 2;
365 444058 y += dy - 2;
366
367
6/6
✓ Branch 0 taken 436548 times.
✓ Branch 1 taken 7510 times.
✓ Branch 2 taken 423093 times.
✓ Branch 3 taken 13455 times.
✓ Branch 4 taken 413517 times.
✓ Branch 5 taken 9576 times.
444058 if (x<0 || x+12>=s->plane_width[plane] ||
368
2/2
✓ Branch 0 taken 19429 times.
✓ Branch 1 taken 394088 times.
413517 y<0 || y+12>=s->plane_height[plane]) {
369 49970 s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
370 49970 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 49970 src_block = s->edge_emu_buffer;
376 49970 src_offset = 2 + 2*stride;
377
2/2
✓ Branch 0 taken 375632 times.
✓ Branch 1 taken 18456 times.
394088 } else if (deblock_filtering) {
378 /* only need a 12x12 block, but there is no such dsp function, */
379 /* so copy a 16x12 block */
380 375632 s->hdsp.put_pixels_tab[0][0](s->edge_emu_buffer,
381 375632 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
382 stride, 12);
383 375632 src_block = s->edge_emu_buffer;
384 375632 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 423784 times.
✓ Branch 1 taken 20274 times.
444058 if (deblock_filtering)
391 423784 vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
392
393
2/2
✓ Branch 0 taken 282743 times.
✓ Branch 1 taken 161315 times.
444058 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 278281 times.
✓ Branch 1 taken 165777 times.
444058 if (s->mv[b].y & mask)
396
2/2
✓ Branch 0 taken 126163 times.
✓ Branch 1 taken 152118 times.
278281 overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
397
398
2/2
✓ Branch 0 taken 387878 times.
✓ Branch 1 taken 56180 times.
444058 if (overlap_offset) {
399
2/2
✓ Branch 0 taken 171759 times.
✓ Branch 1 taken 216119 times.
387878 if (s->filter)
400 171759 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 56180 s->hdsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
408 }
409 444058 }
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 1235196 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 865570 times.
✓ Branch 1 taken 369626 times.
1235196 if (selector > 10)
422 865570 s->vp3dsp.idct_add(dest, stride, block);
423
1/2
✓ Branch 0 taken 369626 times.
✗ Branch 1 not taken.
369626 else if (selector > 1)
424 369626 ff_vp3dsp_idct10_add(dest, stride, block);
425 else
426 s->vp3dsp.idct_dc_add(dest, stride, block);
427 1235196 }
428
429 245878 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 245878 VP56Frame ref_frame = ff_vp56_reference_frame[mb_type];
434
435 245878 vp56_add_predictors_dc(s, ref_frame);
436
437 245878 frame_current = s->frames[VP56_FRAME_CURRENT];
438 245878 frame_ref = s->frames[ref_frame];
439
3/4
✓ Branch 0 taken 212366 times.
✓ Branch 1 taken 33512 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 212366 times.
245878 if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
440 return;
441
442 245878 ab = 6*is_alpha;
443 245878 b_max = 6 - 2*is_alpha;
444
445
3/4
✓ Branch 0 taken 33512 times.
✓ Branch 1 taken 136305 times.
✓ Branch 2 taken 76061 times.
✗ Branch 3 not taken.
245878 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 136305 case VP56_MB_INTER_NOVEC_PF:
455 case VP56_MB_INTER_NOVEC_GF:
456
2/2
✓ Branch 0 taken 791138 times.
✓ Branch 1 taken 136305 times.
927443 for (b=0; b<b_max; b++) {
457 791138 plane = ff_vp56_b2p[b+ab];
458 791138 off = s->block_offset[b];
459 791138 s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
460 791138 frame_ref->data[plane] + off,
461 s->stride[plane], 8);
462 791138 vp56_idct_add(s, frame_current->data[plane] + off,
463 791138 s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
464 }
465 136305 break;
466
467 76061 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 444058 times.
✓ Branch 1 taken 76061 times.
520119 for (b=0; b<b_max; b++) {
475
4/4
✓ Branch 0 taken 367997 times.
✓ Branch 1 taken 76061 times.
✓ Branch 2 taken 76061 times.
✓ Branch 3 taken 291936 times.
444058 int x_off = b==1 || b==3 ? 8 : 0;
476
4/4
✓ Branch 0 taken 367997 times.
✓ Branch 1 taken 76061 times.
✓ Branch 2 taken 76061 times.
✓ Branch 3 taken 291936 times.
444058 int y_off = b==2 || b==3 ? 8 : 0;
477 444058 plane = ff_vp56_b2p[b+ab];
478 444058 vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
479 444058 16*col+x_off, 16*row+y_off);
480 444058 vp56_idct_add(s, frame_current->data[plane] + s->block_offset[b],
481 444058 s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
482 }
483 76061 break;
484 }
485
486
2/2
✓ Branch 0 taken 21204 times.
✓ Branch 1 taken 224674 times.
245878 if (is_alpha) {
487 21204 s->block_coeff[4][0] = 0;
488 21204 s->block_coeff[5][0] = 0;
489 }
490 }
491
492 245516 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 235705 times.
245516 if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)
498 9811 mb_type = VP56_MB_INTRA;
499 else
500 235705 mb_type = vp56_decode_mv(s, row, col);
501
502 245516 ret = s->parse_coeff(s);
503
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 245515 times.
245516 if (ret < 0)
504 1 return ret;
505
506 245515 vp56_render_mb(s, row, col, is_alpha, mb_type);
507
508 245515 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 870 int ff_vp56_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
571 int *got_frame, AVPacket *avpkt)
572 {
573 870 const uint8_t *buf = avpkt->data;
574 870 VP56Context *s = avctx->priv_data;
575 870 AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
576 870 int remaining_buf_size = avpkt->size;
577 870 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 684 times.
870 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 870 res = s->parse_header(s, buf, alpha_offset);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 870 times.
870 if (res < 0)
592 return res;
593
594
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 858 times.
870 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 870 ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF);
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 870 times.
870 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 777 times.
870 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 858 times.
870 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 777 times.
870 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 870 s->discard_frame = 0;
649
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 777 times.
870 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 870 times.
870 if (s->discard_frame)
652 return AVERROR_INVALIDDATA;
653
654
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 870 times.
870 if ((res = av_frame_ref(rframe, p)) < 0)
655 return res;
656 870 *got_frame = 1;
657
658 870 return avpkt->size;
659 }
660
661 963 static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *data,
662 int jobnr, int threadnr)
663 {
664 963 VP56Context *s0 = avctx->priv_data;
665 963 int is_alpha = (jobnr == 1);
666
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 870 times.
963 VP56Context *s = is_alpha ? s0->alpha_context : s0;
667 963 AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
668 963 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 963 int damaged = 0;
673
674
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 919 times.
963 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 919 p->pict_type = AV_PICTURE_TYPE_P;
681 919 vp56_parse_mb_type_models(s);
682 919 s->parse_vector_models(s);
683 919 s->mb_type = VP56_MB_INTER_NOVEC_PF;
684 }
685
686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 963 times.
963 if (s->parse_coeff_models(s))
687 goto next;
688
689 963 memset(s->prev_dc, 0, sizeof(s->prev_dc));
690 963 s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
691 963 s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
692
693
2/2
✓ Branch 0 taken 73590 times.
✓ Branch 1 taken 963 times.
74553 for (block=0; block < 4*s->mb_width+6; block++) {
694 73590 s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
695 73590 s->above_blocks[block].dc_coeff = 0;
696 73590 s->above_blocks[block].not_null_dc = 0;
697 }
698 963 s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
699 963 s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
700
701 963 stride_y = p->linesize[0];
702 963 stride_uv = p->linesize[1];
703
704
2/2
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 454 times.
963 if (s->flip < 0)
705 509 mb_offset = 7;
706
707 /* main macroblocks loop */
708
2/2
✓ Branch 0 taken 11423 times.
✓ Branch 1 taken 963 times.
12386 for (mb_row=0; mb_row<s->mb_height; mb_row++) {
709
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 4223 times.
11423 if (s->flip < 0)
710 7200 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 45692 times.
✓ Branch 1 taken 11423 times.
57115 for (block=0; block<4; block++) {
715 45692 s->left_block[block].ref_frame = VP56_FRAME_NONE;
716 45692 s->left_block[block].dc_coeff = 0;
717 45692 s->left_block[block].not_null_dc = 0;
718 }
719 11423 memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
720 11423 memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
721
722 11423 s->above_block_idx[0] = 1;
723 11423 s->above_block_idx[1] = 2;
724 11423 s->above_block_idx[2] = 1;
725 11423 s->above_block_idx[3] = 2;
726 11423 s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
727 11423 s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
728
729 11423 s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
730 11423 s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
731 11423 s->block_offset[1] = s->block_offset[0] + 8;
732 11423 s->block_offset[3] = s->block_offset[2] + 8;
733 11423 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
734 11423 s->block_offset[5] = s->block_offset[4];
735
736
2/2
✓ Branch 0 taken 245878 times.
✓ Branch 1 taken 11423 times.
257301 for (mb_col=0; mb_col<s->mb_width; mb_col++) {
737
2/2
✓ Branch 0 taken 245516 times.
✓ Branch 1 taken 362 times.
245878 if (!damaged) {
738 245516 int ret = vp56_decode_mb(s, mb_row, mb_col, is_alpha);
739
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 245515 times.
245516 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 245515 times.
245878 if (damaged)
748 363 vp56_conceal_mb(s, mb_row, mb_col, is_alpha);
749
750
2/2
✓ Branch 0 taken 983512 times.
✓ Branch 1 taken 245878 times.
1229390 for (y=0; y<4; y++) {
751 983512 s->above_block_idx[y] += 2;
752 983512 s->block_offset[y] += 16;
753 }
754
755
2/2
✓ Branch 0 taken 491756 times.
✓ Branch 1 taken 245878 times.
737634 for (uv=4; uv<6; uv++) {
756 491756 s->above_block_idx[uv] += 1;
757 491756 s->block_offset[uv] += 8;
758 }
759 }
760 }
761
762
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 962 times.
963 if (!damaged)
763 962 s->have_undamaged_frame = 1;
764
765 1 next:
766
4/4
✓ Branch 0 taken 919 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 886 times.
963 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 963 av_frame_unref(s->frames[VP56_FRAME_PREVIOUS]);
772 963 FFSWAP(AVFrame *, s->frames[VP56_FRAME_CURRENT],
773 s->frames[VP56_FRAME_PREVIOUS]);
774 963 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