Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vp56.c @ 5ce117c3

History | View | Annotate | Download (21 KB)

1
/**
2
 * @file vp56.c
3
 * VP5 and VP6 compatible video decoder (common features)
4
 *
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

    
22
#include "avcodec.h"
23

    
24
#include "vp56.h"
25
#include "vp56data.h"
26

    
27

    
28
void vp56_init_dequant(vp56_context_t *s, int quantizer)
29
{
30
    s->quantizer = quantizer;
31
    s->dequant_dc = vp56_dc_dequant[quantizer] << 2;
32
    s->dequant_ac = vp56_ac_dequant[quantizer] << 2;
33
}
34

    
35
static int vp56_get_vectors_predictors(vp56_context_t *s, int row, int col,
36
                                       vp56_frame_t ref_frame)
37
{
38
    int nb_pred = 0;
39
    vp56_mv_t vector[2] = {{0,0}, {0,0}};
40
    int pos, offset;
41
    vp56_mv_t mvp;
42

    
43
    for (pos=0; pos<12; pos++) {
44
        mvp.x = col + vp56_candidate_predictor_pos[pos][0];
45
        mvp.y = row + vp56_candidate_predictor_pos[pos][1];
46
        if (mvp.x < 0 || mvp.x >= s->mb_width ||
47
            mvp.y < 0 || mvp.y >= s->mb_height)
48
            continue;
49
        offset = mvp.x + s->mb_width*mvp.y;
50

    
51
        if (vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
52
            continue;
53
        if ((s->macroblocks[offset].mv.x == vector[0].x &&
54
             s->macroblocks[offset].mv.y == vector[0].y) ||
55
            (s->macroblocks[offset].mv.x == 0 &&
56
             s->macroblocks[offset].mv.y == 0))
57
            continue;
58

    
59
        vector[nb_pred++] = s->macroblocks[offset].mv;
60
        if (nb_pred > 1) {
61
            nb_pred = -1;
62
            break;
63
        }
64
        s->vector_candidate_pos = pos;
65
    }
66

    
67
    s->vector_candidate[0] = vector[0];
68
    s->vector_candidate[1] = vector[1];
69

    
70
    return nb_pred+1;
71
}
72

    
73
static void vp56_parse_mb_type_models(vp56_context_t *s)
74
{
75
    vp56_range_coder_t *c = &s->c;
76
    int i, ctx, type;
77

    
78
    for (ctx=0; ctx<3; ctx++) {
79
        if (vp56_rac_get_prob(c, 174)) {
80
            int idx = vp56_rac_gets(c, 4);
81
            memcpy(s->mb_types_stats[ctx],vp56_pre_def_mb_type_stats[idx][ctx],
82
                   sizeof(s->mb_types_stats[ctx]));
83
        }
84
        if (vp56_rac_get_prob(c, 254)) {
85
            for (type=0; type<10; type++) {
86
                for(i=0; i<2; i++) {
87
                    if (vp56_rac_get_prob(c, 205)) {
88
                        int delta, sign = vp56_rac_get(c);
89

    
90
                        delta = vp56_rac_get_tree(c, vp56_pmbtm_tree,
91
                                                  vp56_mb_type_model_model);
92
                        if (!delta)
93
                            delta = 4 * vp56_rac_gets(c, 7);
94
                        s->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
95
                    }
96
                }
97
            }
98
        }
99
    }
100

    
101
    /* compute MB type probability tables based on previous MB type */
102
    for (ctx=0; ctx<3; ctx++) {
103
        int p[10];
104

    
105
        for (type=0; type<10; type++)
106
            p[type] = 100 * s->mb_types_stats[ctx][type][1];
107

    
108
        for (type=0; type<10; type++) {
109
            int p02, p34, p0234, p17, p56, p89, p5689, p156789;
110

    
111
            /* conservative MB type probability */
112
            s->mb_type_model[ctx][type][0] = 255 - (255 * s->mb_types_stats[ctx][type][0]) / (1 + s->mb_types_stats[ctx][type][0] + s->mb_types_stats[ctx][type][1]);
113

    
114
            p[type] = 0;    /* same MB type => weight is null */
115

    
116
            /* binary tree parsing probabilities */
117
            p02 = p[0] + p[2];
118
            p34 = p[3] + p[4];
119
            p0234 = p02 + p34;
120
            p17 = p[1] + p[7];
121
            p56 = p[5] + p[6];
122
            p89 = p[8] + p[9];
123
            p5689 = p56 + p89;
124
            p156789 = p17 + p5689;
125

    
126
            s->mb_type_model[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
127
            s->mb_type_model[ctx][type][2] = 1 + 255 * p02  / (1+p0234);
128
            s->mb_type_model[ctx][type][3] = 1 + 255 * p17  / (1+p156789);
129
            s->mb_type_model[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
130
            s->mb_type_model[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
131
            s->mb_type_model[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
132
            s->mb_type_model[ctx][type][7] = 1 + 255 * p56  / (1+p5689);
133
            s->mb_type_model[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
134
            s->mb_type_model[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
135

    
136
            /* restore initial value */
137
            p[type] = 100 * s->mb_types_stats[ctx][type][1];
138
        }
139
    }
140
}
141

    
142
static vp56_mb_t vp56_parse_mb_type(vp56_context_t *s,
143
                                    vp56_mb_t prev_type, int ctx)
144
{
145
    uint8_t *mb_type_model = s->mb_type_model[ctx][prev_type];
146
    vp56_range_coder_t *c = &s->c;
147

    
148
    if (vp56_rac_get_prob(c, mb_type_model[0]))
149
        return prev_type;
150
    else
151
        return vp56_rac_get_tree(c, vp56_pmbt_tree, mb_type_model);
152
}
153

    
154
static void vp56_decode_4mv(vp56_context_t *s, int row, int col)
155
{
156
    vp56_mv_t mv = {0,0};
157
    int type[4];
158
    int b;
159

    
160
    /* parse each block type */
161
    for (b=0; b<4; b++) {
162
        type[b] = vp56_rac_gets(&s->c, 2);
163
        if (type[b])
164
            type[b]++;  /* only returns 0, 2, 3 or 4 (all INTER_PF) */
165
    }
166

    
167
    /* get vectors */
168
    for (b=0; b<4; b++) {
169
        switch (type[b]) {
170
            case VP56_MB_INTER_NOVEC_PF:
171
                s->mv[b] = (vp56_mv_t) {0,0};
172
                break;
173
            case VP56_MB_INTER_DELTA_PF:
174
                s->parse_vector_adjustment(s, &s->mv[b]);
175
                break;
176
            case VP56_MB_INTER_V1_PF:
177
                s->mv[b] = s->vector_candidate[0];
178
                break;
179
            case VP56_MB_INTER_V2_PF:
180
                s->mv[b] = s->vector_candidate[1];
181
                break;
182
        }
183
        mv.x += s->mv[b].x;
184
        mv.y += s->mv[b].y;
185
    }
186

    
187
    /* this is the one selected for the whole MB for prediction */
188
    s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
189

    
190
    /* chroma vectors are average luma vectors */
191
    if (s->avctx->codec->id == CODEC_ID_VP5) {
192
        s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
193
        s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
194
    } else {
195
        s->mv[4] = s->mv[5] = (vp56_mv_t) {mv.x/4, mv.y/4};
196
    }
197
}
198

    
199
static vp56_mb_t vp56_decode_mv(vp56_context_t *s, int row, int col)
200
{
201
    vp56_mv_t *mv, vector = {0,0};
202
    int ctx, b;
203

    
204
    ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
205
    s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
206
    s->macroblocks[row * s->mb_width + col].type = s->mb_type;
207

    
208
    switch (s->mb_type) {
209
        case VP56_MB_INTER_V1_PF:
210
            mv = &s->vector_candidate[0];
211
            break;
212

    
213
        case VP56_MB_INTER_V2_PF:
214
            mv = &s->vector_candidate[1];
215
            break;
216

    
217
        case VP56_MB_INTER_V1_GF:
218
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
219
            mv = &s->vector_candidate[0];
220
            break;
221

    
222
        case VP56_MB_INTER_V2_GF:
223
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
224
            mv = &s->vector_candidate[1];
225
            break;
226

    
227
        case VP56_MB_INTER_DELTA_PF:
228
            s->parse_vector_adjustment(s, &vector);
229
            mv = &vector;
230
            break;
231

    
232
        case VP56_MB_INTER_DELTA_GF:
233
            vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
234
            s->parse_vector_adjustment(s, &vector);
235
            mv = &vector;
236
            break;
237

    
238
        case VP56_MB_INTER_4V:
239
            vp56_decode_4mv(s, row, col);
240
            return s->mb_type;
241

    
242
        default:
243
            mv = &vector;
244
            break;
245
    }
246

    
247
    s->macroblocks[row*s->mb_width + col].mv = *mv;
248

    
249
    /* same vector for all blocks */
250
    for (b=0; b<6; b++)
251
        s->mv[b] = *mv;
252

    
253
    return s->mb_type;
254
}
255

    
256
static void vp56_add_predictors_dc(vp56_context_t *s, vp56_frame_t ref_frame)
257
{
258
    int idx = s->scantable.permutated[0];
259
    int i;
260

    
261
    for (i=0; i<6; i++) {
262
        vp56_ref_dc_t *ab = &s->above_blocks[s->above_block_idx[i]];
263
        vp56_ref_dc_t *lb = &s->left_block[vp56_b6to4[i]];
264
        int count = 0;
265
        int dc = 0;
266

    
267
        if (ref_frame == lb->ref_frame) {
268
            dc += lb->dc_coeff;
269
            count++;
270
        }
271
        if (ref_frame == ab->ref_frame) {
272
            dc += ab->dc_coeff;
273
            count++;
274
        }
275
        if (s->avctx->codec->id == CODEC_ID_VP5) {
276
            if (count < 2 && ref_frame == ab[-1].ref_frame) {
277
                dc += ab[-1].dc_coeff;
278
                count++;
279
            }
280
            if (count < 2 && ref_frame == ab[1].ref_frame) {
281
                dc += ab[1].dc_coeff;
282
                count++;
283
            }
284
        }
285
        if (count == 0)
286
            dc = s->prev_dc[vp56_b6to3[i]][ref_frame];
287
        else if (count == 2)
288
            dc /= 2;
289

    
290
        s->block_coeff[i][idx] += dc;
291
        s->prev_dc[vp56_b6to3[i]][ref_frame] = s->block_coeff[i][idx];
292
        ab->dc_coeff = s->block_coeff[i][idx];
293
        ab->ref_frame = ref_frame;
294
        lb->dc_coeff = s->block_coeff[i][idx];
295
        lb->ref_frame = ref_frame;
296
        s->block_coeff[i][idx] *= s->dequant_dc;
297
    }
298
}
299

    
300
static void vp56_edge_filter(vp56_context_t *s, uint8_t *yuv,
301
                             int pix_inc, int line_inc, int t)
302
{
303
    int pix2_inc = 2 * pix_inc;
304
    int i, v;
305

    
306
    for (i=0; i<12; i++) {
307
        v = (yuv[-pix2_inc] + 3*(yuv[0]-yuv[-pix_inc]) - yuv[pix_inc] + 4) >>3;
308
        v = s->adjust(v, t);
309
        yuv[-pix_inc] = clip_uint8(yuv[-pix_inc] + v);
310
        yuv[0] = clip_uint8(yuv[0] - v);
311
        yuv += line_inc;
312
    }
313
}
314

    
315
static void vp56_deblock_filter(vp56_context_t *s, uint8_t *yuv,
316
                                int stride, int dx, int dy)
317
{
318
    int t = vp56_filter_threshold[s->quantizer];
319
    if (dx)  vp56_edge_filter(s, yuv +         10-dx ,      1, stride, t);
320
    if (dy)  vp56_edge_filter(s, yuv + stride*(10-dy), stride,      1, t);
321
}
322

    
323
static void vp56_mc(vp56_context_t *s, int b, uint8_t *src,
324
                    int stride, int x, int y)
325
{
326
    int plane = vp56_b6to3[b];
327
    uint8_t *dst= s->frames[VP56_FRAME_CURRENT].data[plane]+s->block_offset[b];
328
    uint8_t *src_block;
329
    int src_offset;
330
    int overlap_offset = 0;
331
    int mask = s->vp56_coord_div[b] - 1;
332
    int deblock_filtering = s->deblock_filtering;
333
    int dx;
334
    int dy;
335

    
336
    if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
337
        (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
338
         && !s->frames[VP56_FRAME_CURRENT].key_frame))
339
        deblock_filtering = 0;
340

    
341
    dx = s->mv[b].x / s->vp56_coord_div[b];
342
    dy = s->mv[b].y / s->vp56_coord_div[b];
343

    
344
    if (b >= 4) {
345
        x /= 2;
346
        y /= 2;
347
    }
348
    x += dx - 2;
349
    y += dy - 2;
350

    
351
    if (x<0 || x+12>=s->plane_width[plane] ||
352
        y<0 || y+12>=s->plane_height[plane]) {
353
        ff_emulated_edge_mc(s->edge_emu_buffer,
354
                            src + s->block_offset[b] + (dy-2)*stride + (dx-2),
355
                            stride, 12, 12, x, y,
356
                            s->plane_width[plane],
357
                            s->plane_height[plane]);
358
        src_block = s->edge_emu_buffer;
359
        src_offset = 2 + 2*stride;
360
    } else if (deblock_filtering) {
361
        /* only need a 12x12 block, but there is no such dsp function, */
362
        /* so copy a 16x12 block */
363
        s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer,
364
                                    src + s->block_offset[b] + (dy-2)*stride + (dx-2),
365
                                    stride, 12);
366
        src_block = s->edge_emu_buffer;
367
        src_offset = 2 + 2*stride;
368
    } else {
369
        src_block = src;
370
        src_offset = s->block_offset[b] + dy*stride + dx;
371
    }
372

    
373
    if (deblock_filtering)
374
        vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
375

    
376
    if (s->mv[b].x & mask)
377
        overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
378
    if (s->mv[b].y & mask)
379
        overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
380

    
381
    if (overlap_offset) {
382
        if (s->filter)
383
            s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
384
                      stride, s->mv[b], mask, s->filter_selection, b<4);
385
        else
386
            s->dsp.put_no_rnd_pixels_l2[1](dst, src_block+src_offset,
387
                                           src_block+src_offset+overlap_offset,
388
                                           stride, 8);
389
    } else {
390
        s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
391
    }
392
}
393

    
394
static void vp56_decode_mb(vp56_context_t *s, int row, int col)
395
{
396
    AVFrame *frame_current, *frame_ref;
397
    vp56_mb_t mb_type;
398
    vp56_frame_t ref_frame;
399
    int b, plan, off;
400

    
401
    if (s->frames[VP56_FRAME_CURRENT].key_frame)
402
        mb_type = VP56_MB_INTRA;
403
    else
404
        mb_type = vp56_decode_mv(s, row, col);
405
    ref_frame = vp56_reference_frame[mb_type];
406

    
407
    memset(s->block_coeff, 0, sizeof(s->block_coeff));
408

    
409
    s->parse_coeff(s);
410

    
411
    vp56_add_predictors_dc(s, ref_frame);
412

    
413
    frame_current = &s->frames[VP56_FRAME_CURRENT];
414
    frame_ref = &s->frames[ref_frame];
415

    
416
    switch (mb_type) {
417
        case VP56_MB_INTRA:
418
            for (b=0; b<6; b++) {
419
                plan = vp56_b6to3[b];
420
                s->dsp.idct_put(frame_current->data[plan] + s->block_offset[b],
421
                                s->stride[plan], s->block_coeff[b]);
422
            }
423
            break;
424

    
425
        case VP56_MB_INTER_NOVEC_PF:
426
        case VP56_MB_INTER_NOVEC_GF:
427
            for (b=0; b<6; b++) {
428
                plan = vp56_b6to3[b];
429
                off = s->block_offset[b];
430
                s->dsp.put_pixels_tab[1][0](frame_current->data[plan] + off,
431
                                            frame_ref->data[plan] + off,
432
                                            s->stride[plan], 8);
433
                s->dsp.idct_add(frame_current->data[plan] + off,
434
                                s->stride[plan], s->block_coeff[b]);
435
            }
436
            break;
437

    
438
        case VP56_MB_INTER_DELTA_PF:
439
        case VP56_MB_INTER_V1_PF:
440
        case VP56_MB_INTER_V2_PF:
441
        case VP56_MB_INTER_DELTA_GF:
442
        case VP56_MB_INTER_4V:
443
        case VP56_MB_INTER_V1_GF:
444
        case VP56_MB_INTER_V2_GF:
445
            for (b=0; b<6; b++) {
446
                int x_off = b==1 || b==3 ? 8 : 0;
447
                int y_off = b==2 || b==3 ? 8 : 0;
448
                plan = vp56_b6to3[b];
449
                vp56_mc(s, b, frame_ref->data[plan], s->stride[plan],
450
                        16*col+x_off, 16*row+y_off);
451
                s->dsp.idct_add(frame_current->data[plan] + s->block_offset[b],
452
                                s->stride[plan], s->block_coeff[b]);
453
            }
454
            break;
455
    }
456
}
457

    
458
static int vp56_size_changed(AVCodecContext *avctx, vp56_context_t *s)
459
{
460
    int stride = s->frames[VP56_FRAME_CURRENT].linesize[0];
461
    int i;
462

    
463
    s->plane_width[0] = s->avctx->width;
464
    s->plane_width[1] = s->plane_width[2] = s->avctx->width/2;
465
    s->plane_height[0] = s->avctx->height;
466
    s->plane_height[1] = s->plane_height[2] = s->avctx->height/2;
467

    
468
    for (i=0; i<3; i++)
469
        s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT].linesize[i];
470

    
471
    s->mb_width = (s->avctx->width+15) / 16;
472
    s->mb_height = (s->avctx->height+15) / 16;
473

    
474
    if (s->mb_width > 1000 || s->mb_height > 1000) {
475
        av_log(avctx, AV_LOG_ERROR, "picture too big\n");
476
        return -1;
477
    }
478

    
479
    s->above_blocks = av_realloc(s->above_blocks,
480
                                 (4*s->mb_width+6) * sizeof(*s->above_blocks));
481
    s->macroblocks = av_realloc(s->macroblocks,
482
                                s->mb_width*s->mb_height*sizeof(*s->macroblocks));
483
    s->edge_emu_buffer_alloc = av_realloc(s->edge_emu_buffer_alloc, 16*stride);
484
    s->edge_emu_buffer = s->edge_emu_buffer_alloc;
485
    if (s->flip < 0)
486
        s->edge_emu_buffer += 15 * stride;
487

    
488
    return 0;
489
}
490

    
491
int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
492
                      uint8_t *buf, int buf_size)
493
{
494
    vp56_context_t *s = avctx->priv_data;
495
    AVFrame *const p = &s->frames[VP56_FRAME_CURRENT];
496
    AVFrame *picture = data;
497
    int mb_row, mb_col, mb_row_flip, mb_offset = 0;
498
    int block, y, uv, stride_y, stride_uv;
499
    int golden_frame = 0;
500
    int res;
501

    
502
    res = s->parse_header(s, buf, buf_size, &golden_frame);
503
    if (!res)
504
        return -1;
505

    
506
    p->reference = 1;
507
    if (avctx->get_buffer(avctx, p) < 0) {
508
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
509
        return -1;
510
    }
511

    
512
    if (res == 2)
513
        if (vp56_size_changed(avctx, s)) {
514
            avctx->release_buffer(avctx, p);
515
            return -1;
516
        }
517

    
518
    if (p->key_frame) {
519
        p->pict_type = FF_I_TYPE;
520
        s->default_models_init(s);
521
        for (block=0; block<s->mb_height*s->mb_width; block++)
522
            s->macroblocks[block].type = VP56_MB_INTRA;
523
    } else {
524
        p->pict_type = FF_P_TYPE;
525
        vp56_parse_mb_type_models(s);
526
        s->parse_vector_models(s);
527
        s->mb_type = VP56_MB_INTER_NOVEC_PF;
528
    }
529

    
530
    s->parse_coeff_models(s);
531

    
532
    memset(s->prev_dc, 0, sizeof(s->prev_dc));
533
    s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
534
    s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
535

    
536
    for (block=0; block < 4*s->mb_width+6; block++) {
537
        s->above_blocks[block].ref_frame = -1;
538
        s->above_blocks[block].dc_coeff = 0;
539
        s->above_blocks[block].not_null_dc = 0;
540
    }
541
    s->above_blocks[2*s->mb_width + 2].ref_frame = 0;
542
    s->above_blocks[3*s->mb_width + 4].ref_frame = 0;
543

    
544
    stride_y  = p->linesize[0];
545
    stride_uv = p->linesize[1];
546

    
547
    if (s->flip < 0)
548
        mb_offset = 7;
549

    
550
    /* main macroblocks loop */
551
    for (mb_row=0; mb_row<s->mb_height; mb_row++) {
552
        if (s->flip < 0)
553
            mb_row_flip = s->mb_height - mb_row - 1;
554
        else
555
            mb_row_flip = mb_row;
556

    
557
        for (block=0; block<4; block++) {
558
            s->left_block[block].ref_frame = -1;
559
            s->left_block[block].dc_coeff = 0;
560
            s->left_block[block].not_null_dc = 0;
561
            memset(s->coeff_ctx[block], 0, 64*sizeof(s->coeff_ctx[block][0]));
562
        }
563
        memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
564

    
565
        s->above_block_idx[0] = 1;
566
        s->above_block_idx[1] = 2;
567
        s->above_block_idx[2] = 1;
568
        s->above_block_idx[3] = 2;
569
        s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
570
        s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
571

    
572
        s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
573
        s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
574
        s->block_offset[1] = s->block_offset[0] + 8;
575
        s->block_offset[3] = s->block_offset[2] + 8;
576
        s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
577
        s->block_offset[5] = s->block_offset[4];
578

    
579
        for (mb_col=0; mb_col<s->mb_width; mb_col++) {
580
            vp56_decode_mb(s, mb_row, mb_col);
581

    
582
            for (y=0; y<4; y++) {
583
                s->above_block_idx[y] += 2;
584
                s->block_offset[y] += 16;
585
            }
586

    
587
            for (uv=4; uv<6; uv++) {
588
                s->above_block_idx[uv] += 1;
589
                s->block_offset[uv] += 8;
590
            }
591
        }
592
    }
593

    
594
    if (s->frames[VP56_FRAME_PREVIOUS].data[0]
595
        && (s->frames[VP56_FRAME_PREVIOUS].data[0]
596
            != s->frames[VP56_FRAME_GOLDEN].data[0])) {
597
        avctx->release_buffer(avctx, &s->frames[VP56_FRAME_PREVIOUS]);
598
    }
599
    if (p->key_frame || golden_frame) {
600
        if (s->frames[VP56_FRAME_GOLDEN].data[0])
601
            avctx->release_buffer(avctx, &s->frames[VP56_FRAME_GOLDEN]);
602
        s->frames[VP56_FRAME_GOLDEN] = *p;
603
    }
604
    s->frames[VP56_FRAME_PREVIOUS] = *p;
605

    
606
    *picture = *p;
607
    *data_size = sizeof(AVPicture);
608

    
609
    return buf_size;
610
}
611

    
612
void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip)
613
{
614
    int i;
615

    
616
    s->avctx = avctx;
617
    avctx->pix_fmt = PIX_FMT_YUV420P;
618

    
619
    if (s->avctx->idct_algo == FF_IDCT_AUTO)
620
        s->avctx->idct_algo = FF_IDCT_VP3;
621
    dsputil_init(&s->dsp, s->avctx);
622
    ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
623

    
624
    avcodec_set_dimensions(s->avctx, 0, 0);
625

    
626
    for (i=0; i<3; i++)
627
        s->frames[i].data[0] = NULL;
628
    s->edge_emu_buffer_alloc = NULL;
629

    
630
    s->above_blocks = NULL;
631
    s->macroblocks = NULL;
632
    s->quantizer = -1;
633
    s->deblock_filtering = 1;
634

    
635
    s->filter = NULL;
636

    
637
    if (flip) {
638
        s->flip = -1;
639
        s->frbi = 2;
640
        s->srbi = 0;
641
    } else {
642
        s->flip = 1;
643
        s->frbi = 0;
644
        s->srbi = 2;
645
    }
646
}
647

    
648
int vp56_free(AVCodecContext *avctx)
649
{
650
    vp56_context_t *s = avctx->priv_data;
651

    
652
    av_free(s->above_blocks);
653
    av_free(s->macroblocks);
654
    av_free(s->edge_emu_buffer_alloc);
655
    if (s->frames[VP56_FRAME_GOLDEN].data[0]
656
        && (s->frames[VP56_FRAME_PREVIOUS].data[0]
657
            != s->frames[VP56_FRAME_GOLDEN].data[0]))
658
        avctx->release_buffer(avctx, &s->frames[VP56_FRAME_GOLDEN]);
659
    if (s->frames[VP56_FRAME_PREVIOUS].data[0])
660
        avctx->release_buffer(avctx, &s->frames[VP56_FRAME_PREVIOUS]);
661
    return 0;
662
}