Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/vp56.c
Line
Count
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
void ff_vp56_init_dequant(VP56Context *s, int quantizer)
37
438k
{
38
438k
    if (s->quantizer != quantizer)
39
61.3k
        ff_vp3dsp_set_bounding_values(s->bounding_values_array, ff_vp56_filter_threshold[quantizer]);
40
438k
    s->quantizer = quantizer;
41
438k
    s->dequant_dc = ff_vp56_dc_dequant[quantizer] << 2;
42
438k
    s->dequant_ac = ff_vp56_ac_dequant[quantizer] << 2;
43
438k
}
44
45
static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
46
                                       VP56Frame ref_frame)
47
4.16M
{
48
4.16M
    int nb_pred = 0;
49
4.16M
    VP56mv vect[2] = {{0,0}, {0,0}};
50
4.16M
    int pos, offset;
51
4.16M
    VP56mv mvp;
52
53
37.4M
    for (pos=0; pos<12; pos++) {
54
34.9M
        mvp.x = col + ff_vp56_candidate_predictor_pos[pos][0];
55
34.9M
        mvp.y = row + ff_vp56_candidate_predictor_pos[pos][1];
56
34.9M
        if (mvp.x < 0 || mvp.x >= s->mb_width ||
57
26.4M
            mvp.y < 0 || mvp.y >= s->mb_height)
58
14.2M
            continue;
59
20.6M
        offset = mvp.x + s->mb_width*mvp.y;
60
61
20.6M
        if (ff_vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
62
1.73M
            continue;
63
18.9M
        if ((s->macroblocks[offset].mv.x == vect[0].x &&
64
14.6M
             s->macroblocks[offset].mv.y == vect[0].y) ||
65
4.98M
            (s->macroblocks[offset].mv.x == 0 &&
66
1.55M
             s->macroblocks[offset].mv.y == 0))
67
15.2M
            continue;
68
69
3.62M
        vect[nb_pred++] = s->macroblocks[offset].mv;
70
3.62M
        if (nb_pred > 1) {
71
1.70M
            nb_pred = -1;
72
1.70M
            break;
73
1.70M
        }
74
1.92M
        s->vector_candidate_pos = pos;
75
1.92M
    }
76
77
4.16M
    s->vector_candidate[0] = vect[0];
78
4.16M
    s->vector_candidate[1] = vect[1];
79
80
4.16M
    return nb_pred+1;
81
4.16M
}
82
83
static void vp56_parse_mb_type_models(VP56Context *s)
84
327k
{
85
327k
    VPXRangeCoder *c = &s->c;
86
327k
    VP56Model *model = s->modelp;
87
327k
    int i, ctx, type;
88
89
1.31M
    for (ctx=0; ctx<3; ctx++) {
90
982k
        if (vpx_rac_get_prob_branchy(c, 174)) {
91
406k
            int idx = vp56_rac_gets(c, 4);
92
406k
            memcpy(model->mb_types_stats[ctx],
93
406k
                   ff_vp56_pre_def_mb_type_stats[idx][ctx],
94
406k
                   sizeof(model->mb_types_stats[ctx]));
95
406k
        }
96
982k
        if (vpx_rac_get_prob_branchy(c, 254)) {
97
2.99M
            for (type=0; type<10; type++) {
98
8.15M
                for(i=0; i<2; i++) {
99
5.43M
                    if (vpx_rac_get_prob_branchy(c, 205)) {
100
5.33M
                        int delta, sign = vpx_rac_get(c);
101
102
5.33M
                        delta = vp56_rac_get_tree(c, ff_vp56_pmbtm_tree,
103
5.33M
                                                  ff_vp56_mb_type_model_model);
104
5.33M
                        if (!delta)
105
5.30M
                            delta = 4 * vp56_rac_gets(c, 7);
106
5.33M
                        model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
107
5.33M
                    }
108
5.43M
                }
109
2.71M
            }
110
271k
        }
111
982k
    }
112
113
    /* compute MB type probability tables based on previous MB type */
114
1.31M
    for (ctx=0; ctx<3; ctx++) {
115
982k
        int p[10];
116
117
10.8M
        for (type=0; type<10; type++)
118
9.82M
            p[type] = 100 * model->mb_types_stats[ctx][type][1];
119
120
10.8M
        for (type=0; type<10; type++) {
121
9.82M
            int p02, p34, p0234, p17, p56, p89, p5689, p156789;
122
123
            /* conservative MB type probability */
124
9.82M
            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
9.82M
            p[type] = 0;    /* same MB type => weight is null */
127
128
            /* binary tree parsing probabilities */
129
9.82M
            p02 = p[0] + p[2];
130
9.82M
            p34 = p[3] + p[4];
131
9.82M
            p0234 = p02 + p34;
132
9.82M
            p17 = p[1] + p[7];
133
9.82M
            p56 = p[5] + p[6];
134
9.82M
            p89 = p[8] + p[9];
135
9.82M
            p5689 = p56 + p89;
136
9.82M
            p156789 = p17 + p5689;
137
138
9.82M
            model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
139
9.82M
            model->mb_type[ctx][type][2] = 1 + 255 * p02  / (1+p0234);
140
9.82M
            model->mb_type[ctx][type][3] = 1 + 255 * p17  / (1+p156789);
141
9.82M
            model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
142
9.82M
            model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
143
9.82M
            model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
144
9.82M
            model->mb_type[ctx][type][7] = 1 + 255 * p56  / (1+p5689);
145
9.82M
            model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
146
9.82M
            model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
147
148
            /* restore initial value */
149
9.82M
            p[type] = 100 * model->mb_types_stats[ctx][type][1];
150
9.82M
        }
151
982k
    }
152
327k
}
153
154
static VP56mb vp56_parse_mb_type(VP56Context *s,
155
                                 VP56mb prev_type, int ctx)
156
4.05M
{
157
4.05M
    uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
158
4.05M
    VPXRangeCoder *c = &s->c;
159
160
4.05M
    if (vpx_rac_get_prob_branchy(c, mb_type_model[0]))
161
1.91M
        return prev_type;
162
2.13M
    else
163
2.13M
        return vp56_rac_get_tree(c, ff_vp56_pmbt_tree, mb_type_model);
164
4.05M
}
165
166
static void vp56_decode_4mv(VP56Context *s, int row, int col)
167
105k
{
168
105k
    VP56mv mv = {0,0};
169
105k
    int type[4];
170
105k
    int b;
171
172
    /* parse each block type */
173
529k
    for (b=0; b<4; b++) {
174
423k
        type[b] = vp56_rac_gets(&s->c, 2);
175
423k
        if (type[b])
176
328k
            type[b]++;  /* only returns 0, 2, 3 or 4 (all INTER_PF) */
177
423k
    }
178
179
    /* get vectors */
180
529k
    for (b=0; b<4; b++) {
181
423k
        switch (type[b]) {
182
95.0k
            case VP56_MB_INTER_NOVEC_PF:
183
95.0k
                s->mv[b] = (VP56mv) {0,0};
184
95.0k
                break;
185
234k
            case VP56_MB_INTER_DELTA_PF:
186
234k
                s->parse_vector_adjustment(s, &s->mv[b]);
187
234k
                break;
188
54.6k
            case VP56_MB_INTER_V1_PF:
189
54.6k
                s->mv[b] = s->vector_candidate[0];
190
54.6k
                break;
191
39.4k
            case VP56_MB_INTER_V2_PF:
192
39.4k
                s->mv[b] = s->vector_candidate[1];
193
39.4k
                break;
194
423k
        }
195
423k
        mv.x += s->mv[b].x;
196
423k
        mv.y += s->mv[b].y;
197
423k
    }
198
199
    /* this is the one selected for the whole MB for prediction */
200
105k
    s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
201
202
    /* chroma vectors are average luma vectors */
203
105k
    s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
204
105k
    s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
205
105k
}
206
207
static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
208
4.05M
{
209
4.05M
    VP56mv *mv, vect = {0,0};
210
4.05M
    int ctx, b;
211
212
4.05M
    ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
213
4.05M
    s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
214
4.05M
    s->macroblocks[row * s->mb_width + col].type = s->mb_type;
215
216
4.05M
    switch (s->mb_type) {
217
98.1k
        case VP56_MB_INTER_V1_PF:
218
98.1k
            mv = &s->vector_candidate[0];
219
98.1k
            break;
220
221
62.6k
        case VP56_MB_INTER_V2_PF:
222
62.6k
            mv = &s->vector_candidate[1];
223
62.6k
            break;
224
225
31.1k
        case VP56_MB_INTER_V1_GF:
226
31.1k
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
227
31.1k
            mv = &s->vector_candidate[0];
228
31.1k
            break;
229
230
39.9k
        case VP56_MB_INTER_V2_GF:
231
39.9k
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
232
39.9k
            mv = &s->vector_candidate[1];
233
39.9k
            break;
234
235
1.22M
        case VP56_MB_INTER_DELTA_PF:
236
1.22M
            s->parse_vector_adjustment(s, &vect);
237
1.22M
            mv = &vect;
238
1.22M
            break;
239
240
40.8k
        case VP56_MB_INTER_DELTA_GF:
241
40.8k
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
242
40.8k
            s->parse_vector_adjustment(s, &vect);
243
40.8k
            mv = &vect;
244
40.8k
            break;
245
246
105k
        case VP56_MB_INTER_4V:
247
105k
            vp56_decode_4mv(s, row, col);
248
105k
            return s->mb_type;
249
250
2.45M
        default:
251
2.45M
            mv = &vect;
252
2.45M
            break;
253
4.05M
    }
254
255
3.94M
    s->macroblocks[row*s->mb_width + col].mv = *mv;
256
257
    /* same vector for all blocks */
258
27.6M
    for (b=0; b<6; b++)
259
23.6M
        s->mv[b] = *mv;
260
261
3.94M
    return s->mb_type;
262
4.05M
}
263
264
static VP56mb vp56_conceal_mv(VP56Context *s, int row, int col)
265
2.13M
{
266
2.13M
    VP56mv *mv, vect = {0,0};
267
2.13M
    int b;
268
269
2.13M
    s->mb_type = VP56_MB_INTER_NOVEC_PF;
270
2.13M
    s->macroblocks[row * s->mb_width + col].type = s->mb_type;
271
272
2.13M
    mv = &vect;
273
274
2.13M
    s->macroblocks[row*s->mb_width + col].mv = *mv;
275
276
    /* same vector for all blocks */
277
14.9M
    for (b=0; b<6; b++)
278
12.8M
        s->mv[b] = *mv;
279
280
2.13M
    return s->mb_type;
281
2.13M
}
282
283
static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
284
10.3M
{
285
10.3M
    int idx = s->idct_scantable[0];
286
10.3M
    int b;
287
288
72.1M
    for (b=0; b<6; b++) {
289
61.8M
        VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
290
61.8M
        VP56RefDc *lb = &s->left_block[ff_vp56_b6to4[b]];
291
61.8M
        int count = 0;
292
61.8M
        int dc = 0;
293
61.8M
        int i;
294
295
61.8M
        if (ref_frame == lb->ref_frame) {
296
57.2M
            dc += lb->dc_coeff;
297
57.2M
            count++;
298
57.2M
        }
299
61.8M
        if (ref_frame == ab->ref_frame) {
300
55.9M
            dc += ab->dc_coeff;
301
55.9M
            count++;
302
55.9M
        }
303
61.8M
        if (s->avctx->codec->id == AV_CODEC_ID_VP5)
304
17.0M
            for (i=0; i<2; i++)
305
11.3M
                if (count < 2 && ref_frame == ab[-1+2*i].ref_frame) {
306
1.21M
                    dc += ab[-1+2*i].dc_coeff;
307
1.21M
                    count++;
308
1.21M
                }
309
61.8M
        if (count == 0)
310
1.32M
            dc = s->prev_dc[ff_vp56_b2p[b]][ref_frame];
311
60.5M
        else if (count == 2)
312
53.8M
            dc /= 2;
313
314
61.8M
        s->block_coeff[b][idx] += dc;
315
61.8M
        s->prev_dc[ff_vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
316
61.8M
        ab->dc_coeff = s->block_coeff[b][idx];
317
61.8M
        ab->ref_frame = ref_frame;
318
61.8M
        lb->dc_coeff = s->block_coeff[b][idx];
319
61.8M
        lb->ref_frame = ref_frame;
320
61.8M
        s->block_coeff[b][idx] *= s->dequant_dc;
321
61.8M
    }
322
10.3M
}
323
324
static void vp56_deblock_filter(VP56Context *s, uint8_t *yuv,
325
                                ptrdiff_t stride, int dx, int dy)
326
2.64M
{
327
2.64M
    if (s->avctx->codec->id == AV_CODEC_ID_VP5) {
328
110k
        int t = ff_vp56_filter_threshold[s->quantizer];
329
110k
        if (dx)  s->vp5dsp.edge_filter_hor(yuv +         10-dx , stride, t);
330
110k
        if (dy)  s->vp5dsp.edge_filter_ver(yuv + stride*(10-dy), stride, t);
331
2.53M
    } else {
332
2.53M
        int * bounding_values = s->bounding_values_array + 127;
333
2.53M
        if (dx)
334
1.93M
            ff_vp3dsp_h_loop_filter_12(yuv +         10-dx, stride, bounding_values);
335
2.53M
        if (dy)
336
2.01M
            ff_vp3dsp_v_loop_filter_12(yuv + stride*(10-dy), stride, bounding_values);
337
2.53M
    }
338
2.64M
}
339
340
static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
341
                    ptrdiff_t stride, int x, int y, ptrdiff_t ref_stride)
342
5.28M
{
343
5.28M
    uint8_t *dst = s->frames[VP56_FRAME_CURRENT]->data[plane] + s->block_offset[b];
344
5.28M
    uint8_t *src_block;
345
5.28M
    int src_offset;
346
5.28M
    int overlap_offset = 0;
347
5.28M
    int mask = s->vp56_coord_div[b] - 1;
348
5.28M
    int deblock_filtering = s->deblock_filtering;
349
5.28M
    int dx;
350
5.28M
    int dy;
351
352
5.28M
    if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
353
5.28M
        (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
354
0
         && !(s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)))
355
0
        deblock_filtering = 0;
356
357
5.28M
    dx = s->mv[b].x / s->vp56_coord_div[b];
358
5.28M
    dy = s->mv[b].y / s->vp56_coord_div[b];
359
360
5.28M
    if (b >= 4) {
361
1.54M
        x /= 2;
362
1.54M
        y /= 2;
363
1.54M
    }
364
5.28M
    x += dx - 2;
365
5.28M
    y += dy - 2;
366
367
5.28M
    if (s->interlaced && s->il_block) {
368
        /* extract 12*(4+16+4) block from frame (containing both fields), then treat src_block as specific field */
369
31.1k
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
370
31.1k
                                 src + s->block_offset[b] + (dy-4)*ref_stride + (dx-2),
371
31.1k
                                 ref_stride, ref_stride,
372
31.1k
                                 12, 24, x, y - 2,
373
31.1k
                                 s->plane_width[plane],
374
31.1k
                                 s->plane_height[plane]);
375
31.1k
        src_block = s->edge_emu_buffer;
376
31.1k
        src_offset = 2 + 4*ref_stride;
377
5.24M
    } else if (x<0 || x+12>=s->plane_width[plane] ||
378
4.05M
        y<0 || y+12>=s->plane_height[plane]) {
379
1.69M
        s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
380
1.69M
                                 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
381
1.69M
                                 stride, stride,
382
1.69M
                                 12, 12, x, y,
383
1.69M
                                 s->plane_width[plane],
384
1.69M
                                 s->plane_height[plane]);
385
1.69M
        src_block = s->edge_emu_buffer;
386
1.69M
        src_offset = 2 + 2*stride;
387
3.55M
    } else if (deblock_filtering) {
388
        /* only need a 12x12 block, but there is no such dsp function, */
389
        /* so copy a 16x12 block */
390
1.29M
        s->hdsp.put_pixels_tab[0][0](s->edge_emu_buffer,
391
1.29M
                                     src + s->block_offset[b] + (dy-2)*stride + (dx-2),
392
1.29M
                                     stride, 12);
393
1.29M
        src_block = s->edge_emu_buffer;
394
1.29M
        src_offset = 2 + 2*stride;
395
2.25M
    } else {
396
2.25M
        src_block = src;
397
2.25M
        src_offset = s->block_offset[b] + dy*stride + dx;
398
2.25M
    }
399
400
5.28M
    if (deblock_filtering)
401
2.64M
        vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
402
403
5.28M
    if (s->mv[b].x & mask)
404
3.62M
        overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
405
5.28M
    if (s->mv[b].y & mask)
406
3.49M
        overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
407
408
5.28M
    if (overlap_offset) {
409
4.53M
        if (s->filter)
410
4.47M
            s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
411
4.47M
                      stride, s->mv[b], mask, s->filter_selection, b<4);
412
57.2k
        else
413
57.2k
            s->vp3dsp.put_no_rnd_pixels_l2(dst, src_block+src_offset,
414
57.2k
                                           src_block+src_offset+overlap_offset,
415
57.2k
                                           stride, 8);
416
4.53M
    } else {
417
744k
        s->hdsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
418
744k
    }
419
5.28M
}
420
421
static void vp56_idct_put(VP56Context *s, uint8_t * dest, ptrdiff_t stride, int16_t *block, int selector)
422
24.5M
{
423
24.5M
    if (selector > 10 || selector == 1)
424
16.0M
        s->vp3dsp.idct_put(dest, stride, block);
425
8.50M
    else
426
8.50M
        ff_vp3dsp_idct10_put(dest, stride, block);
427
24.5M
}
428
429
static void vp56_idct_add(VP56Context *s, uint8_t * dest, ptrdiff_t stride, int16_t *block, int selector)
430
27.6M
{
431
27.6M
    if (selector > 10)
432
5.85M
        s->vp3dsp.idct_add(dest, stride, block);
433
21.7M
    else if (selector > 1)
434
11.2M
        ff_vp3dsp_idct10_add(dest, stride, block);
435
10.5M
    else
436
10.5M
        s->vp3dsp.idct_dc_add(dest, stride, block);
437
27.6M
}
438
439
static av_always_inline void vp56_render_mb(VP56Context *s, int row, int col, int is_alpha, VP56mb mb_type)
440
10.3M
{
441
10.3M
    int b, ab, b_max, plane, off;
442
10.3M
    AVFrame *frame_current, *frame_ref;
443
10.3M
    VP56Frame ref_frame = ff_vp56_reference_frame[mb_type];
444
10.3M
    ptrdiff_t ref_stride[4];
445
446
10.3M
    vp56_add_predictors_dc(s, ref_frame);
447
448
10.3M
    frame_current = s->frames[VP56_FRAME_CURRENT];
449
10.3M
    frame_ref = s->frames[ref_frame];
450
10.3M
    if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
451
1.26M
        return;
452
453
9.05M
    memcpy(ref_stride, s->stride, sizeof(s->stride));
454
9.05M
    if (s->interlaced && s->il_block) {
455
180k
        s->block_offset[2] -= s->stride[0] * 7;
456
180k
        s->block_offset[3] -= s->stride[0] * 7;
457
180k
        s->stride[0] *= 2;
458
180k
    }
459
460
9.05M
    ab = 6*is_alpha;
461
9.05M
    b_max = 6 - 2*is_alpha;
462
463
9.05M
    switch (mb_type) {
464
4.18M
        case VP56_MB_INTRA:
465
28.7M
            for (b=0; b<b_max; b++) {
466
24.5M
                plane = ff_vp56_b2p[b+ab];
467
24.5M
                vp56_idct_put(s, frame_current->data[plane] + s->block_offset[b],
468
24.5M
                                s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
469
24.5M
            }
470
4.18M
            break;
471
472
3.92M
        case VP56_MB_INTER_NOVEC_PF:
473
3.93M
        case VP56_MB_INTER_NOVEC_GF:
474
26.3M
            for (b=0; b<b_max; b++) {
475
22.3M
                plane = ff_vp56_b2p[b+ab];
476
22.3M
                off = s->block_offset[b];
477
22.3M
                s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
478
22.3M
                                             frame_ref->data[plane] + off,
479
22.3M
                                             s->stride[plane], 8);
480
22.3M
                vp56_idct_add(s, frame_current->data[plane] + off,
481
22.3M
                              s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
482
22.3M
            }
483
3.93M
            break;
484
485
708k
        case VP56_MB_INTER_DELTA_PF:
486
761k
        case VP56_MB_INTER_V1_PF:
487
808k
        case VP56_MB_INTER_V2_PF:
488
821k
        case VP56_MB_INTER_DELTA_GF:
489
901k
        case VP56_MB_INTER_4V:
490
910k
        case VP56_MB_INTER_V1_GF:
491
934k
        case VP56_MB_INTER_V2_GF:
492
6.21M
            for (b=0; b<b_max; b++) {
493
5.28M
                int x_off = b==1 || b==3 ? 8 : 0;
494
5.28M
                int y_off = b==2 || b==3 ? (s->interlaced && s->il_block ? 1 : 8) : 0;
495
5.28M
                plane = ff_vp56_b2p[b+ab];
496
5.28M
                vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
497
5.28M
                        16*col+x_off, 16*row+y_off, ref_stride[plane]);
498
5.28M
                vp56_idct_add(s, frame_current->data[plane] + s->block_offset[b],
499
5.28M
                              s->stride[plane], s->block_coeff[b], s->idct_selector[b]);
500
5.28M
            }
501
934k
            break;
502
9.05M
    }
503
504
9.05M
    if (is_alpha) {
505
1.04M
        s->block_coeff[4][0] = 0;
506
1.04M
        s->block_coeff[5][0] = 0;
507
1.04M
    }
508
509
9.05M
    if (s->interlaced && s->il_block) {
510
180k
        s->stride[0] /= 2;
511
180k
        s->block_offset[2] += s->stride[0] * 7;
512
180k
        s->block_offset[3] += s->stride[0] * 7;
513
180k
    }
514
9.05M
}
515
516
static int vp56_decode_mb(VP56Context *s, int row, int col, int is_alpha)
517
8.19M
{
518
8.19M
    VP56mb mb_type;
519
8.19M
    int ret;
520
521
8.19M
    if (s->interlaced) {
522
370k
        int prob = s->il_prob;
523
524
370k
        if (col > 0) {
525
324k
            if (s->il_block)
526
193k
                prob -= prob >> 1;
527
131k
            else
528
131k
                prob += (256 - prob) >> 1;  /* can be simplified/combined */
529
324k
        }
530
531
370k
        s->il_block = vpx_rac_get_prob(&s->c, prob);
532
370k
    }
533
534
8.19M
    if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)
535
4.14M
        mb_type = VP56_MB_INTRA;
536
4.05M
    else
537
4.05M
        mb_type = vp56_decode_mv(s, row, col);
538
539
8.19M
    ret = s->parse_coeff(s);
540
8.19M
    if (ret < 0)
541
30.0k
        return ret;
542
543
8.16M
    vp56_render_mb(s, row, col, is_alpha, mb_type);
544
545
8.16M
    return 0;
546
8.19M
}
547
548
static int vp56_conceal_mb(VP56Context *s, int row, int col, int is_alpha)
549
2.14M
{
550
2.14M
    VP56mb mb_type;
551
552
2.14M
    if (s->frames[VP56_FRAME_CURRENT]->flags & AV_FRAME_FLAG_KEY)
553
12.8k
        mb_type = VP56_MB_INTRA;
554
2.13M
    else
555
2.13M
        mb_type = vp56_conceal_mv(s, row, col);
556
557
2.14M
    vp56_render_mb(s, row, col, is_alpha, mb_type);
558
559
2.14M
    return 0;
560
2.14M
}
561
562
static int vp56_size_changed(VP56Context *s)
563
37.3k
{
564
37.3k
    AVCodecContext *avctx = s->avctx;
565
37.3k
    int stride = s->frames[VP56_FRAME_CURRENT]->linesize[0];
566
37.3k
    int i;
567
568
37.3k
    s->plane_width[0]  = s->plane_width[3]  = avctx->coded_width;
569
37.3k
    s->plane_width[1]  = s->plane_width[2]  = avctx->coded_width/2;
570
37.3k
    s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
571
37.3k
    s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
572
573
37.3k
    s->have_undamaged_frame = 0;
574
575
186k
    for (i=0; i<4; i++)
576
149k
        s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT]->linesize[i];
577
578
37.3k
    s->mb_width  = (avctx->coded_width +15) / 16;
579
37.3k
    s->mb_height = (avctx->coded_height+15) / 16;
580
581
37.3k
    if (s->mb_width > 1000 || s->mb_height > 1000) {
582
0
        ff_set_dimensions(avctx, 0, 0);
583
0
        av_log(avctx, AV_LOG_ERROR, "picture too big\n");
584
0
        return AVERROR_INVALIDDATA;
585
0
    }
586
587
37.3k
    av_reallocp_array(&s->above_blocks, 4*s->mb_width+6,
588
37.3k
                      sizeof(*s->above_blocks));
589
37.3k
    av_reallocp_array(&s->macroblocks, s->mb_width*s->mb_height,
590
37.3k
                      sizeof(*s->macroblocks));
591
37.3k
    av_free(s->edge_emu_buffer_alloc);
592
37.3k
    s->edge_emu_buffer_alloc = av_malloc(16*stride*2);
593
37.3k
    s->edge_emu_buffer = s->edge_emu_buffer_alloc;
594
37.3k
    if (!s->above_blocks || !s->macroblocks || !s->edge_emu_buffer_alloc)
595
0
        return AVERROR(ENOMEM);
596
37.3k
    if (s->flip < 0)
597
16.2k
        s->edge_emu_buffer += 15 * stride * 2;
598
599
37.3k
    if (s->alpha_context)
600
6.76k
        return vp56_size_changed(s->alpha_context);
601
602
30.5k
    return 0;
603
37.3k
}
604
605
static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *, int, int);
606
607
int ff_vp56_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
608
                         int *got_frame, AVPacket *avpkt)
609
361k
{
610
361k
    const uint8_t *buf = avpkt->data;
611
361k
    VP56Context *s = avctx->priv_data;
612
361k
    AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
613
361k
    int remaining_buf_size = avpkt->size;
614
361k
    int alpha_offset = remaining_buf_size;
615
361k
    int i, res;
616
361k
    int ret;
617
618
361k
    if (s->has_alpha) {
619
103k
        if (remaining_buf_size < 3)
620
7.39k
            return AVERROR_INVALIDDATA;
621
96.3k
        alpha_offset = bytestream_get_be24(&buf);
622
96.3k
        remaining_buf_size -= 3;
623
96.3k
        if (remaining_buf_size < alpha_offset)
624
3.35k
            return AVERROR_INVALIDDATA;
625
96.3k
    }
626
627
351k
    res = s->parse_header(s, buf, alpha_offset);
628
351k
    if (res < 0)
629
42.8k
        return res;
630
631
308k
    if (res == VP56_SIZE_CHANGE) {
632
152k
        for (i = 0; i < 4; i++) {
633
122k
            av_frame_unref(s->frames[i]);
634
122k
            if (s->alpha_context)
635
27.0k
                av_frame_unref(s->alpha_context->frames[i]);
636
122k
        }
637
30.5k
        s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_KEY; //FIXME
638
30.5k
    }
639
640
308k
    ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF);
641
308k
    if (ret < 0) {
642
8.93k
        if (res == VP56_SIZE_CHANGE)
643
0
            ff_set_dimensions(avctx, 0, 0);
644
8.93k
        return ret;
645
8.93k
    }
646
647
299k
    if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
648
87.4k
        if ((ret = av_frame_replace(s->alpha_context->frames[VP56_FRAME_CURRENT], p)) < 0) {
649
0
            av_frame_unref(p);
650
0
            if (res == VP56_SIZE_CHANGE)
651
0
                ff_set_dimensions(avctx, 0, 0);
652
0
            return ret;
653
0
        }
654
87.4k
    }
655
656
299k
    if (res == VP56_SIZE_CHANGE) {
657
30.5k
        if (vp56_size_changed(s)) {
658
0
            av_frame_unref(p);
659
0
            return AVERROR_INVALIDDATA;
660
0
        }
661
30.5k
    }
662
663
299k
    if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
664
87.4k
        int bak_w = avctx->width;
665
87.4k
        int bak_h = avctx->height;
666
87.4k
        int bak_cw = avctx->coded_width;
667
87.4k
        int bak_ch = avctx->coded_height;
668
87.4k
        buf += alpha_offset;
669
87.4k
        remaining_buf_size -= alpha_offset;
670
671
87.4k
        res = s->alpha_context->parse_header(s->alpha_context, buf, remaining_buf_size);
672
87.4k
        if (res != 0) {
673
10.2k
            if(res==VP56_SIZE_CHANGE) {
674
1.53k
                av_log(avctx, AV_LOG_ERROR, "Alpha reconfiguration\n");
675
1.53k
                avctx->width  = bak_w;
676
1.53k
                avctx->height = bak_h;
677
1.53k
                avctx->coded_width  = bak_cw;
678
1.53k
                avctx->coded_height = bak_ch;
679
1.53k
            }
680
10.2k
            av_frame_unref(p);
681
10.2k
            return AVERROR_INVALIDDATA;
682
10.2k
        }
683
87.4k
    }
684
685
289k
    s->discard_frame = 0;
686
289k
    avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) + 1);
687
688
289k
    if (s->discard_frame)
689
19.6k
        return AVERROR_INVALIDDATA;
690
691
269k
    if ((res = av_frame_ref(rframe, p)) < 0)
692
0
        return res;
693
269k
    *got_frame = 1;
694
695
269k
    return avpkt->size;
696
269k
}
697
698
static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *data,
699
                              int jobnr, int threadnr)
700
366k
{
701
366k
    VP56Context *s0 = avctx->priv_data;
702
366k
    int is_alpha = (jobnr == 1);
703
366k
    VP56Context *s = is_alpha ? s0->alpha_context : s0;
704
366k
    AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
705
366k
    int mb_row, mb_col, mb_row_flip, mb_offset = 0;
706
366k
    int block, y, uv;
707
366k
    ptrdiff_t stride_y, stride_uv;
708
366k
    int res;
709
366k
    int damaged = 0;
710
711
366k
    if (p->flags & AV_FRAME_FLAG_KEY) {
712
38.7k
        p->pict_type = AV_PICTURE_TYPE_I;
713
38.7k
        s->default_models_init(s);
714
77.5M
        for (block=0; block<s->mb_height*s->mb_width; block++)
715
77.5M
            s->macroblocks[block].type = VP56_MB_INTRA;
716
327k
    } else {
717
327k
        p->pict_type = AV_PICTURE_TYPE_P;
718
327k
        vp56_parse_mb_type_models(s);
719
327k
        s->parse_vector_models(s);
720
327k
        s->mb_type = VP56_MB_INTER_NOVEC_PF;
721
327k
    }
722
723
366k
    if (s->parse_coeff_models(s))
724
0
        goto next;
725
726
366k
    if (s->interlaced) {
727
18.2k
       s->frames[VP56_FRAME_CURRENT]->flags |= AV_FRAME_FLAG_INTERLACED;
728
18.2k
       s->il_prob = vp56_rac_gets(&s->c, 8);
729
18.2k
    }
730
731
366k
    memset(s->prev_dc, 0, sizeof(s->prev_dc));
732
366k
    s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
733
366k
    s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
734
735
11.6M
    for (block=0; block < 4*s->mb_width+6; block++) {
736
11.2M
        s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
737
11.2M
        s->above_blocks[block].dc_coeff = 0;
738
11.2M
        s->above_blocks[block].not_null_dc = 0;
739
11.2M
    }
740
366k
    s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
741
366k
    s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
742
743
366k
    stride_y  = p->linesize[0];
744
366k
    stride_uv = p->linesize[1];
745
746
366k
    if (s->flip < 0)
747
146k
        mb_offset = 7;
748
749
    /* main macroblocks loop */
750
1.39M
    for (mb_row=0; mb_row<s->mb_height; mb_row++) {
751
1.04M
        if (s->flip < 0)
752
424k
            mb_row_flip = s->mb_height - mb_row - 1;
753
624k
        else
754
624k
            mb_row_flip = mb_row;
755
756
5.24M
        for (block=0; block<4; block++) {
757
4.19M
            s->left_block[block].ref_frame = VP56_FRAME_NONE;
758
4.19M
            s->left_block[block].dc_coeff = 0;
759
4.19M
            s->left_block[block].not_null_dc = 0;
760
4.19M
        }
761
1.04M
        memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
762
1.04M
        memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
763
764
1.04M
        s->above_block_idx[0] = 1;
765
1.04M
        s->above_block_idx[1] = 2;
766
1.04M
        s->above_block_idx[2] = 1;
767
1.04M
        s->above_block_idx[3] = 2;
768
1.04M
        s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
769
1.04M
        s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
770
771
1.04M
        s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
772
1.04M
        s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
773
1.04M
        s->block_offset[1] = s->block_offset[0] + 8;
774
1.04M
        s->block_offset[3] = s->block_offset[2] + 8;
775
1.04M
        s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
776
1.04M
        s->block_offset[5] = s->block_offset[4];
777
778
11.3M
        for (mb_col=0; mb_col<s->mb_width; mb_col++) {
779
10.3M
            if (!damaged) {
780
8.19M
                int ret = vp56_decode_mb(s, mb_row, mb_col, is_alpha);
781
8.19M
                if (ret < 0) {
782
30.0k
                    damaged = 1;
783
30.0k
                    if (!s->have_undamaged_frame || !avctx->error_concealment) {
784
23.9k
                        s->discard_frame = 1;
785
23.9k
                        return AVERROR_INVALIDDATA;
786
23.9k
                    }
787
30.0k
                }
788
8.19M
            }
789
10.3M
            if (damaged)
790
2.14M
                vp56_conceal_mb(s, mb_row, mb_col, is_alpha);
791
792
51.5M
            for (y=0; y<4; y++) {
793
41.2M
                s->above_block_idx[y] += 2;
794
41.2M
                s->block_offset[y] += 16;
795
41.2M
            }
796
797
30.9M
            for (uv=4; uv<6; uv++) {
798
20.6M
                s->above_block_idx[uv] += 1;
799
20.6M
                s->block_offset[uv] += 8;
800
20.6M
            }
801
10.3M
        }
802
1.04M
    }
803
804
342k
    if (!damaged)
805
336k
        s->have_undamaged_frame = 1;
806
807
342k
next:
808
342k
    if ((p->flags & AV_FRAME_FLAG_KEY) || s->golden_frame) {
809
160k
        if ((res = av_frame_replace(s->frames[VP56_FRAME_GOLDEN], p)) < 0)
810
0
            return res;
811
160k
    }
812
813
342k
    av_frame_unref(s->frames[VP56_FRAME_PREVIOUS]);
814
342k
    FFSWAP(AVFrame *, s->frames[VP56_FRAME_CURRENT],
815
342k
                      s->frames[VP56_FRAME_PREVIOUS]);
816
342k
    return 0;
817
342k
}
818
819
av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
820
                                  int flip, int has_alpha)
821
20.4k
{
822
20.4k
    int i;
823
824
20.4k
    s->avctx = avctx;
825
20.4k
    avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
826
20.4k
    if (avctx->skip_alpha) avctx->pix_fmt = AV_PIX_FMT_YUV420P;
827
828
20.4k
    ff_h264chroma_init(&s->h264chroma, 8);
829
20.4k
    ff_hpeldsp_init(&s->hdsp, avctx->flags);
830
20.4k
    ff_videodsp_init(&s->vdsp, 8);
831
20.4k
    ff_vp3dsp_init(&s->vp3dsp);
832
1.33M
    for (i = 0; i < 64; i++) {
833
1.31M
#define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
834
1.31M
        s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
835
1.31M
#undef TRANSPOSE
836
1.31M
    }
837
838
102k
    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
839
81.9k
        s->frames[i] = av_frame_alloc();
840
81.9k
        if (!s->frames[i])
841
0
            return AVERROR(ENOMEM);
842
81.9k
    }
843
20.4k
    s->edge_emu_buffer_alloc = NULL;
844
845
20.4k
    s->above_blocks = NULL;
846
20.4k
    s->macroblocks = NULL;
847
20.4k
    s->quantizer = -1;
848
20.4k
    s->deblock_filtering = 1;
849
20.4k
    s->golden_frame = 0;
850
851
20.4k
    s->filter = NULL;
852
853
20.4k
    s->has_alpha = has_alpha;
854
855
20.4k
    s->modelp = &s->model;
856
857
20.4k
    if (flip) {
858
7.61k
        s->flip = -1;
859
7.61k
        s->frbi = 2;
860
7.61k
        s->srbi = 0;
861
12.8k
    } else {
862
12.8k
        s->flip = 1;
863
12.8k
        s->frbi = 0;
864
12.8k
        s->srbi = 2;
865
12.8k
    }
866
867
20.4k
    return 0;
868
20.4k
}
869
870
av_cold int ff_vp56_free_context(VP56Context *s)
871
20.4k
{
872
20.4k
    int i;
873
874
20.4k
    av_freep(&s->above_blocks);
875
20.4k
    av_freep(&s->macroblocks);
876
20.4k
    av_freep(&s->edge_emu_buffer_alloc);
877
878
102k
    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
879
81.9k
        av_frame_free(&s->frames[i]);
880
881
20.4k
    return 0;
882
20.4k
}