reindent
[libav.git] / libavcodec / vp56.c
CommitLineData
5ce117c3
AJ
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 *
b78e7197
DB
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
5ce117c3
AJ
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
b78e7197 14 * FFmpeg is distributed in the hope that it will be useful,
5ce117c3
AJ
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
b78e7197 20 * License along with FFmpeg; if not, write to the Free Software
e5a389a1 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5ce117c3
AJ
22 */
23
24#include "avcodec.h"
25
26#include "vp56.h"
27#include "vp56data.h"
28
29
30void vp56_init_dequant(vp56_context_t *s, int quantizer)
31{
32 s->quantizer = quantizer;
33 s->dequant_dc = vp56_dc_dequant[quantizer] << 2;
34 s->dequant_ac = vp56_ac_dequant[quantizer] << 2;
35}
36
37static int vp56_get_vectors_predictors(vp56_context_t *s, int row, int col,
38 vp56_frame_t ref_frame)
39{
40 int nb_pred = 0;
d120e402 41 vp56_mv_t vect[2] = {{0,0}, {0,0}};
5ce117c3
AJ
42 int pos, offset;
43 vp56_mv_t mvp;
44
45 for (pos=0; pos<12; pos++) {
46 mvp.x = col + vp56_candidate_predictor_pos[pos][0];
47 mvp.y = row + vp56_candidate_predictor_pos[pos][1];
48 if (mvp.x < 0 || mvp.x >= s->mb_width ||
49 mvp.y < 0 || mvp.y >= s->mb_height)
50 continue;
51 offset = mvp.x + s->mb_width*mvp.y;
52
53 if (vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
54 continue;
d120e402
AJ
55 if ((s->macroblocks[offset].mv.x == vect[0].x &&
56 s->macroblocks[offset].mv.y == vect[0].y) ||
5ce117c3
AJ
57 (s->macroblocks[offset].mv.x == 0 &&
58 s->macroblocks[offset].mv.y == 0))
59 continue;
60
d120e402 61 vect[nb_pred++] = s->macroblocks[offset].mv;
5ce117c3
AJ
62 if (nb_pred > 1) {
63 nb_pred = -1;
64 break;
65 }
66 s->vector_candidate_pos = pos;
67 }
68
d120e402
AJ
69 s->vector_candidate[0] = vect[0];
70 s->vector_candidate[1] = vect[1];
5ce117c3
AJ
71
72 return nb_pred+1;
73}
74
75static void vp56_parse_mb_type_models(vp56_context_t *s)
76{
77 vp56_range_coder_t *c = &s->c;
247df384 78 vp56_model_t *model = s->modelp;
5ce117c3
AJ
79 int i, ctx, type;
80
81 for (ctx=0; ctx<3; ctx++) {
82 if (vp56_rac_get_prob(c, 174)) {
83 int idx = vp56_rac_gets(c, 4);
247df384
AJ
84 memcpy(model->mb_types_stats[ctx],
85 vp56_pre_def_mb_type_stats[idx][ctx],
86 sizeof(model->mb_types_stats[ctx]));
5ce117c3
AJ
87 }
88 if (vp56_rac_get_prob(c, 254)) {
89 for (type=0; type<10; type++) {
90 for(i=0; i<2; i++) {
91 if (vp56_rac_get_prob(c, 205)) {
92 int delta, sign = vp56_rac_get(c);
93
94 delta = vp56_rac_get_tree(c, vp56_pmbtm_tree,
95 vp56_mb_type_model_model);
96 if (!delta)
97 delta = 4 * vp56_rac_gets(c, 7);
247df384 98 model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
5ce117c3
AJ
99 }
100 }
101 }
102 }
103 }
104
105 /* compute MB type probability tables based on previous MB type */
106 for (ctx=0; ctx<3; ctx++) {
107 int p[10];
108
109 for (type=0; type<10; type++)
247df384 110 p[type] = 100 * model->mb_types_stats[ctx][type][1];
5ce117c3
AJ
111
112 for (type=0; type<10; type++) {
113 int p02, p34, p0234, p17, p56, p89, p5689, p156789;
114
115 /* conservative MB type probability */
247df384 116 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]);
5ce117c3
AJ
117
118 p[type] = 0; /* same MB type => weight is null */
119
120 /* binary tree parsing probabilities */
121 p02 = p[0] + p[2];
122 p34 = p[3] + p[4];
123 p0234 = p02 + p34;
124 p17 = p[1] + p[7];
125 p56 = p[5] + p[6];
126 p89 = p[8] + p[9];
127 p5689 = p56 + p89;
128 p156789 = p17 + p5689;
129
247df384
AJ
130 model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
131 model->mb_type[ctx][type][2] = 1 + 255 * p02 / (1+p0234);
132 model->mb_type[ctx][type][3] = 1 + 255 * p17 / (1+p156789);
133 model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
134 model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
135 model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
136 model->mb_type[ctx][type][7] = 1 + 255 * p56 / (1+p5689);
137 model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
138 model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
5ce117c3
AJ
139
140 /* restore initial value */
247df384 141 p[type] = 100 * model->mb_types_stats[ctx][type][1];
5ce117c3
AJ
142 }
143 }
144}
145
146static vp56_mb_t vp56_parse_mb_type(vp56_context_t *s,
147 vp56_mb_t prev_type, int ctx)
148{
247df384 149 uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
5ce117c3
AJ
150 vp56_range_coder_t *c = &s->c;
151
152 if (vp56_rac_get_prob(c, mb_type_model[0]))
153 return prev_type;
154 else
155 return vp56_rac_get_tree(c, vp56_pmbt_tree, mb_type_model);
156}
157
158static void vp56_decode_4mv(vp56_context_t *s, int row, int col)
159{
160 vp56_mv_t mv = {0,0};
161 int type[4];
162 int b;
163
164 /* parse each block type */
165 for (b=0; b<4; b++) {
166 type[b] = vp56_rac_gets(&s->c, 2);
167 if (type[b])
168 type[b]++; /* only returns 0, 2, 3 or 4 (all INTER_PF) */
169 }
170
171 /* get vectors */
172 for (b=0; b<4; b++) {
173 switch (type[b]) {
174 case VP56_MB_INTER_NOVEC_PF:
175 s->mv[b] = (vp56_mv_t) {0,0};
176 break;
177 case VP56_MB_INTER_DELTA_PF:
178 s->parse_vector_adjustment(s, &s->mv[b]);
179 break;
180 case VP56_MB_INTER_V1_PF:
181 s->mv[b] = s->vector_candidate[0];
182 break;
183 case VP56_MB_INTER_V2_PF:
184 s->mv[b] = s->vector_candidate[1];
185 break;
186 }
187 mv.x += s->mv[b].x;
188 mv.y += s->mv[b].y;
189 }
190
191 /* this is the one selected for the whole MB for prediction */
192 s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
193
194 /* chroma vectors are average luma vectors */
195 if (s->avctx->codec->id == CODEC_ID_VP5) {
196 s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
197 s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
198 } else {
199 s->mv[4] = s->mv[5] = (vp56_mv_t) {mv.x/4, mv.y/4};
200 }
201}
202
203static vp56_mb_t vp56_decode_mv(vp56_context_t *s, int row, int col)
204{
d120e402 205 vp56_mv_t *mv, vect = {0,0};
5ce117c3
AJ
206 int ctx, b;
207
208 ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
209 s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
210 s->macroblocks[row * s->mb_width + col].type = s->mb_type;
211
212 switch (s->mb_type) {
213 case VP56_MB_INTER_V1_PF:
214 mv = &s->vector_candidate[0];
215 break;
216
217 case VP56_MB_INTER_V2_PF:
218 mv = &s->vector_candidate[1];
219 break;
220
221 case VP56_MB_INTER_V1_GF:
222 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
223 mv = &s->vector_candidate[0];
224 break;
225
226 case VP56_MB_INTER_V2_GF:
227 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
228 mv = &s->vector_candidate[1];
229 break;
230
231 case VP56_MB_INTER_DELTA_PF:
d120e402
AJ
232 s->parse_vector_adjustment(s, &vect);
233 mv = &vect;
5ce117c3
AJ
234 break;
235
236 case VP56_MB_INTER_DELTA_GF:
237 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
d120e402
AJ
238 s->parse_vector_adjustment(s, &vect);
239 mv = &vect;
5ce117c3
AJ
240 break;
241
242 case VP56_MB_INTER_4V:
243 vp56_decode_4mv(s, row, col);
244 return s->mb_type;
245
246 default:
d120e402 247 mv = &vect;
5ce117c3
AJ
248 break;
249 }
250
251 s->macroblocks[row*s->mb_width + col].mv = *mv;
252
253 /* same vector for all blocks */
254 for (b=0; b<6; b++)
255 s->mv[b] = *mv;
256
257 return s->mb_type;
258}
259
260static void vp56_add_predictors_dc(vp56_context_t *s, vp56_frame_t ref_frame)
261{
262 int idx = s->scantable.permutated[0];
263 int i;
264
265 for (i=0; i<6; i++) {
266 vp56_ref_dc_t *ab = &s->above_blocks[s->above_block_idx[i]];
267 vp56_ref_dc_t *lb = &s->left_block[vp56_b6to4[i]];
268 int count = 0;
269 int dc = 0;
270
271 if (ref_frame == lb->ref_frame) {
272 dc += lb->dc_coeff;
273 count++;
274 }
275 if (ref_frame == ab->ref_frame) {
276 dc += ab->dc_coeff;
277 count++;
278 }
279 if (s->avctx->codec->id == CODEC_ID_VP5) {
280 if (count < 2 && ref_frame == ab[-1].ref_frame) {
281 dc += ab[-1].dc_coeff;
282 count++;
283 }
284 if (count < 2 && ref_frame == ab[1].ref_frame) {
285 dc += ab[1].dc_coeff;
286 count++;
287 }
288 }
289 if (count == 0)
a7d70d88 290 dc = s->prev_dc[vp56_b2p[i]][ref_frame];
5ce117c3
AJ
291 else if (count == 2)
292 dc /= 2;
293
294 s->block_coeff[i][idx] += dc;
a7d70d88 295 s->prev_dc[vp56_b2p[i]][ref_frame] = s->block_coeff[i][idx];
5ce117c3
AJ
296 ab->dc_coeff = s->block_coeff[i][idx];
297 ab->ref_frame = ref_frame;
298 lb->dc_coeff = s->block_coeff[i][idx];
299 lb->ref_frame = ref_frame;
300 s->block_coeff[i][idx] *= s->dequant_dc;
301 }
302}
303
304static void vp56_edge_filter(vp56_context_t *s, uint8_t *yuv,
305 int pix_inc, int line_inc, int t)
306{
307 int pix2_inc = 2 * pix_inc;
308 int i, v;
309
310 for (i=0; i<12; i++) {
311 v = (yuv[-pix2_inc] + 3*(yuv[0]-yuv[-pix_inc]) - yuv[pix_inc] + 4) >>3;
312 v = s->adjust(v, t);
f66e4f5f
RD
313 yuv[-pix_inc] = av_clip_uint8(yuv[-pix_inc] + v);
314 yuv[0] = av_clip_uint8(yuv[0] - v);
5ce117c3
AJ
315 yuv += line_inc;
316 }
317}
318
319static void vp56_deblock_filter(vp56_context_t *s, uint8_t *yuv,
320 int stride, int dx, int dy)
321{
322 int t = vp56_filter_threshold[s->quantizer];
323 if (dx) vp56_edge_filter(s, yuv + 10-dx , 1, stride, t);
324 if (dy) vp56_edge_filter(s, yuv + stride*(10-dy), stride, 1, t);
325}
326
1457516f 327static void vp56_mc(vp56_context_t *s, int b, int plane, uint8_t *src,
5ce117c3
AJ
328 int stride, int x, int y)
329{
704a2881 330 uint8_t *dst=s->framep[VP56_FRAME_CURRENT]->data[plane]+s->block_offset[b];
5ce117c3
AJ
331 uint8_t *src_block;
332 int src_offset;
333 int overlap_offset = 0;
334 int mask = s->vp56_coord_div[b] - 1;
335 int deblock_filtering = s->deblock_filtering;
336 int dx;
337 int dy;
338
339 if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
340 (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
704a2881 341 && !s->framep[VP56_FRAME_CURRENT]->key_frame))
5ce117c3
AJ
342 deblock_filtering = 0;
343
344 dx = s->mv[b].x / s->vp56_coord_div[b];
345 dy = s->mv[b].y / s->vp56_coord_div[b];
346
347 if (b >= 4) {
348 x /= 2;
349 y /= 2;
350 }
351 x += dx - 2;
352 y += dy - 2;
353
354 if (x<0 || x+12>=s->plane_width[plane] ||
355 y<0 || y+12>=s->plane_height[plane]) {
356 ff_emulated_edge_mc(s->edge_emu_buffer,
357 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
358 stride, 12, 12, x, y,
359 s->plane_width[plane],
360 s->plane_height[plane]);
361 src_block = s->edge_emu_buffer;
362 src_offset = 2 + 2*stride;
363 } else if (deblock_filtering) {
a8678a3a
AJ
364 /* only need a 12x12 block, but there is no such dsp function, */
365 /* so copy a 16x12 block */
366 s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer,
367 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
368 stride, 12);
5ce117c3
AJ
369 src_block = s->edge_emu_buffer;
370 src_offset = 2 + 2*stride;
371 } else {
372 src_block = src;
373 src_offset = s->block_offset[b] + dy*stride + dx;
374 }
375
376 if (deblock_filtering)
377 vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
378
379 if (s->mv[b].x & mask)
380 overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
381 if (s->mv[b].y & mask)
382 overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
383
384 if (overlap_offset) {
385 if (s->filter)
386 s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
387 stride, s->mv[b], mask, s->filter_selection, b<4);
388 else
389 s->dsp.put_no_rnd_pixels_l2[1](dst, src_block+src_offset,
390 src_block+src_offset+overlap_offset,
391 stride, 8);
392 } else {
393 s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
394 }
395}
396
397static void vp56_decode_mb(vp56_context_t *s, int row, int col)
398{
399 AVFrame *frame_current, *frame_ref;
400 vp56_mb_t mb_type;
401 vp56_frame_t ref_frame;
402 int b, plan, off;
403
704a2881 404 if (s->framep[VP56_FRAME_CURRENT]->key_frame)
5ce117c3
AJ
405 mb_type = VP56_MB_INTRA;
406 else
407 mb_type = vp56_decode_mv(s, row, col);
408 ref_frame = vp56_reference_frame[mb_type];
409
410 memset(s->block_coeff, 0, sizeof(s->block_coeff));
411
412 s->parse_coeff(s);
413
414 vp56_add_predictors_dc(s, ref_frame);
415
704a2881
AJ
416 frame_current = s->framep[VP56_FRAME_CURRENT];
417 frame_ref = s->framep[ref_frame];
5ce117c3
AJ
418
419 switch (mb_type) {
420 case VP56_MB_INTRA:
421 for (b=0; b<6; b++) {
a7d70d88 422 plan = vp56_b2p[b];
5ce117c3
AJ
423 s->dsp.idct_put(frame_current->data[plan] + s->block_offset[b],
424 s->stride[plan], s->block_coeff[b]);
425 }
426 break;
427
428 case VP56_MB_INTER_NOVEC_PF:
429 case VP56_MB_INTER_NOVEC_GF:
430 for (b=0; b<6; b++) {
a7d70d88 431 plan = vp56_b2p[b];
5ce117c3
AJ
432 off = s->block_offset[b];
433 s->dsp.put_pixels_tab[1][0](frame_current->data[plan] + off,
434 frame_ref->data[plan] + off,
435 s->stride[plan], 8);
436 s->dsp.idct_add(frame_current->data[plan] + off,
437 s->stride[plan], s->block_coeff[b]);
438 }
439 break;
440
441 case VP56_MB_INTER_DELTA_PF:
442 case VP56_MB_INTER_V1_PF:
443 case VP56_MB_INTER_V2_PF:
444 case VP56_MB_INTER_DELTA_GF:
445 case VP56_MB_INTER_4V:
446 case VP56_MB_INTER_V1_GF:
447 case VP56_MB_INTER_V2_GF:
448 for (b=0; b<6; b++) {
449 int x_off = b==1 || b==3 ? 8 : 0;
450 int y_off = b==2 || b==3 ? 8 : 0;
a7d70d88 451 plan = vp56_b2p[b];
1457516f 452 vp56_mc(s, b, plan, frame_ref->data[plan], s->stride[plan],
5ce117c3
AJ
453 16*col+x_off, 16*row+y_off);
454 s->dsp.idct_add(frame_current->data[plan] + s->block_offset[b],
455 s->stride[plan], s->block_coeff[b]);
456 }
457 break;
458 }
459}
460
1457516f 461static int vp56_size_changed(AVCodecContext *avctx)
5ce117c3 462{
1457516f 463 vp56_context_t *s = avctx->priv_data;
704a2881 464 int stride = s->framep[VP56_FRAME_CURRENT]->linesize[0];
5ce117c3
AJ
465 int i;
466
1457516f
AJ
467 s->plane_width[0] = avctx->coded_width;
468 s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
469 s->plane_height[0] = avctx->coded_height;
470 s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
5ce117c3
AJ
471
472 for (i=0; i<3; i++)
704a2881 473 s->stride[i] = s->flip * s->framep[VP56_FRAME_CURRENT]->linesize[i];
5ce117c3 474
1457516f
AJ
475 s->mb_width = (avctx->coded_width +15) / 16;
476 s->mb_height = (avctx->coded_height+15) / 16;
5ce117c3
AJ
477
478 if (s->mb_width > 1000 || s->mb_height > 1000) {
479 av_log(avctx, AV_LOG_ERROR, "picture too big\n");
480 return -1;
481 }
482
483 s->above_blocks = av_realloc(s->above_blocks,
484 (4*s->mb_width+6) * sizeof(*s->above_blocks));
485 s->macroblocks = av_realloc(s->macroblocks,
486 s->mb_width*s->mb_height*sizeof(*s->macroblocks));
a8678a3a
AJ
487 av_free(s->edge_emu_buffer_alloc);
488 s->edge_emu_buffer_alloc = av_malloc(16*stride);
5ce117c3
AJ
489 s->edge_emu_buffer = s->edge_emu_buffer_alloc;
490 if (s->flip < 0)
491 s->edge_emu_buffer += 15 * stride;
492
493 return 0;
494}
495
496int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
497 uint8_t *buf, int buf_size)
498{
499 vp56_context_t *s = avctx->priv_data;
704a2881 500 AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
5ce117c3
AJ
501 int mb_row, mb_col, mb_row_flip, mb_offset = 0;
502 int block, y, uv, stride_y, stride_uv;
503 int golden_frame = 0;
504 int res;
505
247df384
AJ
506 s->modelp = &s->models;
507
5ce117c3
AJ
508 res = s->parse_header(s, buf, buf_size, &golden_frame);
509 if (!res)
510 return -1;
511
512 p->reference = 1;
513 if (avctx->get_buffer(avctx, p) < 0) {
514 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
515 return -1;
516 }
517
518 if (res == 2)
1457516f 519 if (vp56_size_changed(avctx)) {
5ce117c3
AJ
520 avctx->release_buffer(avctx, p);
521 return -1;
522 }
523
524 if (p->key_frame) {
525 p->pict_type = FF_I_TYPE;
526 s->default_models_init(s);
527 for (block=0; block<s->mb_height*s->mb_width; block++)
528 s->macroblocks[block].type = VP56_MB_INTRA;
529 } else {
530 p->pict_type = FF_P_TYPE;
531 vp56_parse_mb_type_models(s);
532 s->parse_vector_models(s);
533 s->mb_type = VP56_MB_INTER_NOVEC_PF;
534 }
535
536 s->parse_coeff_models(s);
537
538 memset(s->prev_dc, 0, sizeof(s->prev_dc));
539 s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
540 s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
541
542 for (block=0; block < 4*s->mb_width+6; block++) {
543 s->above_blocks[block].ref_frame = -1;
544 s->above_blocks[block].dc_coeff = 0;
545 s->above_blocks[block].not_null_dc = 0;
546 }
547 s->above_blocks[2*s->mb_width + 2].ref_frame = 0;
548 s->above_blocks[3*s->mb_width + 4].ref_frame = 0;
549
550 stride_y = p->linesize[0];
551 stride_uv = p->linesize[1];
552
553 if (s->flip < 0)
554 mb_offset = 7;
555
556 /* main macroblocks loop */
557 for (mb_row=0; mb_row<s->mb_height; mb_row++) {
558 if (s->flip < 0)
559 mb_row_flip = s->mb_height - mb_row - 1;
560 else
561 mb_row_flip = mb_row;
562
563 for (block=0; block<4; block++) {
564 s->left_block[block].ref_frame = -1;
565 s->left_block[block].dc_coeff = 0;
566 s->left_block[block].not_null_dc = 0;
567 memset(s->coeff_ctx[block], 0, 64*sizeof(s->coeff_ctx[block][0]));
568 }
569 memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
570
571 s->above_block_idx[0] = 1;
572 s->above_block_idx[1] = 2;
573 s->above_block_idx[2] = 1;
574 s->above_block_idx[3] = 2;
575 s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
576 s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
577
578 s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
579 s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
580 s->block_offset[1] = s->block_offset[0] + 8;
581 s->block_offset[3] = s->block_offset[2] + 8;
582 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
583 s->block_offset[5] = s->block_offset[4];
584
585 for (mb_col=0; mb_col<s->mb_width; mb_col++) {
586 vp56_decode_mb(s, mb_row, mb_col);
587
588 for (y=0; y<4; y++) {
589 s->above_block_idx[y] += 2;
590 s->block_offset[y] += 16;
591 }
592
593 for (uv=4; uv<6; uv++) {
594 s->above_block_idx[uv] += 1;
595 s->block_offset[uv] += 8;
596 }
597 }
598 }
599
704a2881
AJ
600 if (s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN])
601 FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
602 s->framep[VP56_FRAME_UNUSED]);
603 else if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
604 avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
5ce117c3 605 if (p->key_frame || golden_frame) {
704a2881
AJ
606 if (s->framep[VP56_FRAME_GOLDEN]->data[0])
607 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
608 s->framep[VP56_FRAME_GOLDEN] = p;
5ce117c3 609 }
704a2881
AJ
610 FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
611 s->framep[VP56_FRAME_PREVIOUS]);
5ce117c3 612
eba0fcad
AJ
613 *(AVFrame*)data = *p;
614 *data_size = sizeof(AVFrame);
5ce117c3
AJ
615
616 return buf_size;
617}
618
1457516f 619void vp56_init(AVCodecContext *avctx, int flip)
5ce117c3 620{
1457516f 621 vp56_context_t *s = avctx->priv_data;
5ce117c3
AJ
622 int i;
623
624 s->avctx = avctx;
625 avctx->pix_fmt = PIX_FMT_YUV420P;
626
1457516f
AJ
627 if (avctx->idct_algo == FF_IDCT_AUTO)
628 avctx->idct_algo = FF_IDCT_VP3;
629 dsputil_init(&s->dsp, avctx);
5ce117c3
AJ
630 ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
631
1457516f 632 avcodec_set_dimensions(avctx, 0, 0);
5ce117c3
AJ
633
634 for (i=0; i<3; i++)
704a2881
AJ
635 s->framep[i] = &s->frames[i];
636 s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
5ce117c3
AJ
637 s->edge_emu_buffer_alloc = NULL;
638
639 s->above_blocks = NULL;
640 s->macroblocks = NULL;
641 s->quantizer = -1;
642 s->deblock_filtering = 1;
643
644 s->filter = NULL;
645
646 if (flip) {
647 s->flip = -1;
648 s->frbi = 2;
649 s->srbi = 0;
650 } else {
651 s->flip = 1;
652 s->frbi = 0;
653 s->srbi = 2;
654 }
655}
656
657int vp56_free(AVCodecContext *avctx)
658{
659 vp56_context_t *s = avctx->priv_data;
660
661 av_free(s->above_blocks);
662 av_free(s->macroblocks);
663 av_free(s->edge_emu_buffer_alloc);
1457516f 664 if (s->framep[VP56_FRAME_GOLDEN]->data[0])
704a2881
AJ
665 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
666 if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
667 avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
5ce117c3
AJ
668 return 0;
669}