00001
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026
00027 #include "vp56.h"
00028 #include "vp56data.h"
00029
00030
00031 void vp56_init_dequant(VP56Context *s, int quantizer)
00032 {
00033 s->quantizer = quantizer;
00034 s->dequant_dc = vp56_dc_dequant[quantizer] << 2;
00035 s->dequant_ac = vp56_ac_dequant[quantizer] << 2;
00036 memset(s->qscale_table, quantizer, s->mb_width);
00037 }
00038
00039 static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
00040 VP56Frame ref_frame)
00041 {
00042 int nb_pred = 0;
00043 VP56mv vect[2] = {{0,0}, {0,0}};
00044 int pos, offset;
00045 VP56mv mvp;
00046
00047 for (pos=0; pos<12; pos++) {
00048 mvp.x = col + vp56_candidate_predictor_pos[pos][0];
00049 mvp.y = row + vp56_candidate_predictor_pos[pos][1];
00050 if (mvp.x < 0 || mvp.x >= s->mb_width ||
00051 mvp.y < 0 || mvp.y >= s->mb_height)
00052 continue;
00053 offset = mvp.x + s->mb_width*mvp.y;
00054
00055 if (vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
00056 continue;
00057 if ((s->macroblocks[offset].mv.x == vect[0].x &&
00058 s->macroblocks[offset].mv.y == vect[0].y) ||
00059 (s->macroblocks[offset].mv.x == 0 &&
00060 s->macroblocks[offset].mv.y == 0))
00061 continue;
00062
00063 vect[nb_pred++] = s->macroblocks[offset].mv;
00064 if (nb_pred > 1) {
00065 nb_pred = -1;
00066 break;
00067 }
00068 s->vector_candidate_pos = pos;
00069 }
00070
00071 s->vector_candidate[0] = vect[0];
00072 s->vector_candidate[1] = vect[1];
00073
00074 return nb_pred+1;
00075 }
00076
00077 static void vp56_parse_mb_type_models(VP56Context *s)
00078 {
00079 VP56RangeCoder *c = &s->c;
00080 VP56Model *model = s->modelp;
00081 int i, ctx, type;
00082
00083 for (ctx=0; ctx<3; ctx++) {
00084 if (vp56_rac_get_prob(c, 174)) {
00085 int idx = vp56_rac_gets(c, 4);
00086 memcpy(model->mb_types_stats[ctx],
00087 vp56_pre_def_mb_type_stats[idx][ctx],
00088 sizeof(model->mb_types_stats[ctx]));
00089 }
00090 if (vp56_rac_get_prob(c, 254)) {
00091 for (type=0; type<10; type++) {
00092 for(i=0; i<2; i++) {
00093 if (vp56_rac_get_prob(c, 205)) {
00094 int delta, sign = vp56_rac_get(c);
00095
00096 delta = vp56_rac_get_tree(c, vp56_pmbtm_tree,
00097 vp56_mb_type_model_model);
00098 if (!delta)
00099 delta = 4 * vp56_rac_gets(c, 7);
00100 model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
00101 }
00102 }
00103 }
00104 }
00105 }
00106
00107
00108 for (ctx=0; ctx<3; ctx++) {
00109 int p[10];
00110
00111 for (type=0; type<10; type++)
00112 p[type] = 100 * model->mb_types_stats[ctx][type][1];
00113
00114 for (type=0; type<10; type++) {
00115 int p02, p34, p0234, p17, p56, p89, p5689, p156789;
00116
00117
00118 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]);
00119
00120 p[type] = 0;
00121
00122
00123 p02 = p[0] + p[2];
00124 p34 = p[3] + p[4];
00125 p0234 = p02 + p34;
00126 p17 = p[1] + p[7];
00127 p56 = p[5] + p[6];
00128 p89 = p[8] + p[9];
00129 p5689 = p56 + p89;
00130 p156789 = p17 + p5689;
00131
00132 model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
00133 model->mb_type[ctx][type][2] = 1 + 255 * p02 / (1+p0234);
00134 model->mb_type[ctx][type][3] = 1 + 255 * p17 / (1+p156789);
00135 model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
00136 model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
00137 model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
00138 model->mb_type[ctx][type][7] = 1 + 255 * p56 / (1+p5689);
00139 model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
00140 model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
00141
00142
00143 p[type] = 100 * model->mb_types_stats[ctx][type][1];
00144 }
00145 }
00146 }
00147
00148 static VP56mb vp56_parse_mb_type(VP56Context *s,
00149 VP56mb prev_type, int ctx)
00150 {
00151 uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
00152 VP56RangeCoder *c = &s->c;
00153
00154 if (vp56_rac_get_prob(c, mb_type_model[0]))
00155 return prev_type;
00156 else
00157 return vp56_rac_get_tree(c, vp56_pmbt_tree, mb_type_model);
00158 }
00159
00160 static void vp56_decode_4mv(VP56Context *s, int row, int col)
00161 {
00162 VP56mv mv = {0,0};
00163 int type[4];
00164 int b;
00165
00166
00167 for (b=0; b<4; b++) {
00168 type[b] = vp56_rac_gets(&s->c, 2);
00169 if (type[b])
00170 type[b]++;
00171 }
00172
00173
00174 for (b=0; b<4; b++) {
00175 switch (type[b]) {
00176 case VP56_MB_INTER_NOVEC_PF:
00177 s->mv[b] = (VP56mv) {0,0};
00178 break;
00179 case VP56_MB_INTER_DELTA_PF:
00180 s->parse_vector_adjustment(s, &s->mv[b]);
00181 break;
00182 case VP56_MB_INTER_V1_PF:
00183 s->mv[b] = s->vector_candidate[0];
00184 break;
00185 case VP56_MB_INTER_V2_PF:
00186 s->mv[b] = s->vector_candidate[1];
00187 break;
00188 }
00189 mv.x += s->mv[b].x;
00190 mv.y += s->mv[b].y;
00191 }
00192
00193
00194 s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
00195
00196
00197 if (s->avctx->codec->id == CODEC_ID_VP5) {
00198 s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
00199 s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
00200 } else {
00201 s->mv[4] = s->mv[5] = (VP56mv) {mv.x/4, mv.y/4};
00202 }
00203 }
00204
00205 static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
00206 {
00207 VP56mv *mv, vect = {0,0};
00208 int ctx, b;
00209
00210 ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
00211 s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
00212 s->macroblocks[row * s->mb_width + col].type = s->mb_type;
00213
00214 switch (s->mb_type) {
00215 case VP56_MB_INTER_V1_PF:
00216 mv = &s->vector_candidate[0];
00217 break;
00218
00219 case VP56_MB_INTER_V2_PF:
00220 mv = &s->vector_candidate[1];
00221 break;
00222
00223 case VP56_MB_INTER_V1_GF:
00224 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
00225 mv = &s->vector_candidate[0];
00226 break;
00227
00228 case VP56_MB_INTER_V2_GF:
00229 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
00230 mv = &s->vector_candidate[1];
00231 break;
00232
00233 case VP56_MB_INTER_DELTA_PF:
00234 s->parse_vector_adjustment(s, &vect);
00235 mv = &vect;
00236 break;
00237
00238 case VP56_MB_INTER_DELTA_GF:
00239 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
00240 s->parse_vector_adjustment(s, &vect);
00241 mv = &vect;
00242 break;
00243
00244 case VP56_MB_INTER_4V:
00245 vp56_decode_4mv(s, row, col);
00246 return s->mb_type;
00247
00248 default:
00249 mv = &vect;
00250 break;
00251 }
00252
00253 s->macroblocks[row*s->mb_width + col].mv = *mv;
00254
00255
00256 for (b=0; b<6; b++)
00257 s->mv[b] = *mv;
00258
00259 return s->mb_type;
00260 }
00261
00262 static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
00263 {
00264 int idx = s->scantable.permutated[0];
00265 int b;
00266
00267 for (b=0; b<6; b++) {
00268 VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
00269 VP56RefDc *lb = &s->left_block[vp56_b6to4[b]];
00270 int count = 0;
00271 int dc = 0;
00272 int i;
00273
00274 if (ref_frame == lb->ref_frame) {
00275 dc += lb->dc_coeff;
00276 count++;
00277 }
00278 if (ref_frame == ab->ref_frame) {
00279 dc += ab->dc_coeff;
00280 count++;
00281 }
00282 if (s->avctx->codec->id == CODEC_ID_VP5)
00283 for (i=0; i<2; i++)
00284 if (count < 2 && ref_frame == ab[-1+2*i].ref_frame) {
00285 dc += ab[-1+2*i].dc_coeff;
00286 count++;
00287 }
00288 if (count == 0)
00289 dc = s->prev_dc[vp56_b2p[b]][ref_frame];
00290 else if (count == 2)
00291 dc /= 2;
00292
00293 s->block_coeff[b][idx] += dc;
00294 s->prev_dc[vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
00295 ab->dc_coeff = s->block_coeff[b][idx];
00296 ab->ref_frame = ref_frame;
00297 lb->dc_coeff = s->block_coeff[b][idx];
00298 lb->ref_frame = ref_frame;
00299 s->block_coeff[b][idx] *= s->dequant_dc;
00300 }
00301 }
00302
00303 static void vp56_deblock_filter(VP56Context *s, uint8_t *yuv,
00304 int stride, int dx, int dy)
00305 {
00306 int t = vp56_filter_threshold[s->quantizer];
00307 if (dx) s->vp56dsp.edge_filter_hor(yuv + 10-dx , stride, t);
00308 if (dy) s->vp56dsp.edge_filter_ver(yuv + stride*(10-dy), stride, t);
00309 }
00310
00311 static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
00312 int stride, int x, int y)
00313 {
00314 uint8_t *dst=s->framep[VP56_FRAME_CURRENT]->data[plane]+s->block_offset[b];
00315 uint8_t *src_block;
00316 int src_offset;
00317 int overlap_offset = 0;
00318 int mask = s->vp56_coord_div[b] - 1;
00319 int deblock_filtering = s->deblock_filtering;
00320 int dx;
00321 int dy;
00322
00323 if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
00324 (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
00325 && !s->framep[VP56_FRAME_CURRENT]->key_frame))
00326 deblock_filtering = 0;
00327
00328 dx = s->mv[b].x / s->vp56_coord_div[b];
00329 dy = s->mv[b].y / s->vp56_coord_div[b];
00330
00331 if (b >= 4) {
00332 x /= 2;
00333 y /= 2;
00334 }
00335 x += dx - 2;
00336 y += dy - 2;
00337
00338 if (x<0 || x+12>=s->plane_width[plane] ||
00339 y<0 || y+12>=s->plane_height[plane]) {
00340 ff_emulated_edge_mc(s->edge_emu_buffer,
00341 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
00342 stride, 12, 12, x, y,
00343 s->plane_width[plane],
00344 s->plane_height[plane]);
00345 src_block = s->edge_emu_buffer;
00346 src_offset = 2 + 2*stride;
00347 } else if (deblock_filtering) {
00348
00349
00350 s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer,
00351 src + s->block_offset[b] + (dy-2)*stride + (dx-2),
00352 stride, 12);
00353 src_block = s->edge_emu_buffer;
00354 src_offset = 2 + 2*stride;
00355 } else {
00356 src_block = src;
00357 src_offset = s->block_offset[b] + dy*stride + dx;
00358 }
00359
00360 if (deblock_filtering)
00361 vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
00362
00363 if (s->mv[b].x & mask)
00364 overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
00365 if (s->mv[b].y & mask)
00366 overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
00367
00368 if (overlap_offset) {
00369 if (s->filter)
00370 s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
00371 stride, s->mv[b], mask, s->filter_selection, b<4);
00372 else
00373 s->dsp.put_no_rnd_pixels_l2[1](dst, src_block+src_offset,
00374 src_block+src_offset+overlap_offset,
00375 stride, 8);
00376 } else {
00377 s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
00378 }
00379 }
00380
00381 static void vp56_decode_mb(VP56Context *s, int row, int col, int is_alpha)
00382 {
00383 AVFrame *frame_current, *frame_ref;
00384 VP56mb mb_type;
00385 VP56Frame ref_frame;
00386 int b, ab, b_max, plane, off;
00387
00388 if (s->framep[VP56_FRAME_CURRENT]->key_frame)
00389 mb_type = VP56_MB_INTRA;
00390 else
00391 mb_type = vp56_decode_mv(s, row, col);
00392 ref_frame = vp56_reference_frame[mb_type];
00393
00394 s->dsp.clear_blocks(*s->block_coeff);
00395
00396 s->parse_coeff(s);
00397
00398 vp56_add_predictors_dc(s, ref_frame);
00399
00400 frame_current = s->framep[VP56_FRAME_CURRENT];
00401 frame_ref = s->framep[ref_frame];
00402
00403 ab = 6*is_alpha;
00404 b_max = 6 - 2*is_alpha;
00405
00406 switch (mb_type) {
00407 case VP56_MB_INTRA:
00408 for (b=0; b<b_max; b++) {
00409 plane = vp56_b2p[b+ab];
00410 s->dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
00411 s->stride[plane], s->block_coeff[b]);
00412 }
00413 break;
00414
00415 case VP56_MB_INTER_NOVEC_PF:
00416 case VP56_MB_INTER_NOVEC_GF:
00417 for (b=0; b<b_max; b++) {
00418 plane = vp56_b2p[b+ab];
00419 off = s->block_offset[b];
00420 s->dsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
00421 frame_ref->data[plane] + off,
00422 s->stride[plane], 8);
00423 s->dsp.idct_add(frame_current->data[plane] + off,
00424 s->stride[plane], s->block_coeff[b]);
00425 }
00426 break;
00427
00428 case VP56_MB_INTER_DELTA_PF:
00429 case VP56_MB_INTER_V1_PF:
00430 case VP56_MB_INTER_V2_PF:
00431 case VP56_MB_INTER_DELTA_GF:
00432 case VP56_MB_INTER_4V:
00433 case VP56_MB_INTER_V1_GF:
00434 case VP56_MB_INTER_V2_GF:
00435 for (b=0; b<b_max; b++) {
00436 int x_off = b==1 || b==3 ? 8 : 0;
00437 int y_off = b==2 || b==3 ? 8 : 0;
00438 plane = vp56_b2p[b+ab];
00439 vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
00440 16*col+x_off, 16*row+y_off);
00441 s->dsp.idct_add(frame_current->data[plane] + s->block_offset[b],
00442 s->stride[plane], s->block_coeff[b]);
00443 }
00444 break;
00445 }
00446 }
00447
00448 static int vp56_size_changed(AVCodecContext *avctx)
00449 {
00450 VP56Context *s = avctx->priv_data;
00451 int stride = s->framep[VP56_FRAME_CURRENT]->linesize[0];
00452 int i;
00453
00454 s->plane_width[0] = s->plane_width[3] = avctx->coded_width;
00455 s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
00456 s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
00457 s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
00458
00459 for (i=0; i<4; i++)
00460 s->stride[i] = s->flip * s->framep[VP56_FRAME_CURRENT]->linesize[i];
00461
00462 s->mb_width = (avctx->coded_width +15) / 16;
00463 s->mb_height = (avctx->coded_height+15) / 16;
00464
00465 if (s->mb_width > 1000 || s->mb_height > 1000) {
00466 av_log(avctx, AV_LOG_ERROR, "picture too big\n");
00467 return -1;
00468 }
00469
00470 s->qscale_table = av_realloc(s->qscale_table, s->mb_width);
00471 s->above_blocks = av_realloc(s->above_blocks,
00472 (4*s->mb_width+6) * sizeof(*s->above_blocks));
00473 s->macroblocks = av_realloc(s->macroblocks,
00474 s->mb_width*s->mb_height*sizeof(*s->macroblocks));
00475 av_free(s->edge_emu_buffer_alloc);
00476 s->edge_emu_buffer_alloc = av_malloc(16*stride);
00477 s->edge_emu_buffer = s->edge_emu_buffer_alloc;
00478 if (s->flip < 0)
00479 s->edge_emu_buffer += 15 * stride;
00480
00481 return 0;
00482 }
00483
00484 int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00485 AVPacket *avpkt)
00486 {
00487 const uint8_t *buf = avpkt->data;
00488 VP56Context *s = avctx->priv_data;
00489 AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
00490 int remaining_buf_size = avpkt->size;
00491 int is_alpha, av_uninit(alpha_offset);
00492
00493 if (s->has_alpha) {
00494 if (remaining_buf_size < 3)
00495 return -1;
00496 alpha_offset = bytestream_get_be24(&buf);
00497 remaining_buf_size -= 3;
00498 if (remaining_buf_size < alpha_offset)
00499 return -1;
00500 }
00501
00502 for (is_alpha=0; is_alpha < 1+s->has_alpha; is_alpha++) {
00503 int mb_row, mb_col, mb_row_flip, mb_offset = 0;
00504 int block, y, uv, stride_y, stride_uv;
00505 int golden_frame = 0;
00506 int res;
00507
00508 s->modelp = &s->models[is_alpha];
00509
00510 res = s->parse_header(s, buf, remaining_buf_size, &golden_frame);
00511 if (!res)
00512 return -1;
00513
00514 if (!is_alpha) {
00515 p->reference = 1;
00516 if (avctx->get_buffer(avctx, p) < 0) {
00517 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00518 return -1;
00519 }
00520
00521 if (res == 2)
00522 if (vp56_size_changed(avctx)) {
00523 avctx->release_buffer(avctx, p);
00524 return -1;
00525 }
00526 }
00527
00528 if (p->key_frame) {
00529 p->pict_type = FF_I_TYPE;
00530 s->default_models_init(s);
00531 for (block=0; block<s->mb_height*s->mb_width; block++)
00532 s->macroblocks[block].type = VP56_MB_INTRA;
00533 } else {
00534 p->pict_type = FF_P_TYPE;
00535 vp56_parse_mb_type_models(s);
00536 s->parse_vector_models(s);
00537 s->mb_type = VP56_MB_INTER_NOVEC_PF;
00538 }
00539
00540 if (s->parse_coeff_models(s))
00541 goto next;
00542
00543 memset(s->prev_dc, 0, sizeof(s->prev_dc));
00544 s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
00545 s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
00546
00547 for (block=0; block < 4*s->mb_width+6; block++) {
00548 s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
00549 s->above_blocks[block].dc_coeff = 0;
00550 s->above_blocks[block].not_null_dc = 0;
00551 }
00552 s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
00553 s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
00554
00555 stride_y = p->linesize[0];
00556 stride_uv = p->linesize[1];
00557
00558 if (s->flip < 0)
00559 mb_offset = 7;
00560
00561
00562 for (mb_row=0; mb_row<s->mb_height; mb_row++) {
00563 if (s->flip < 0)
00564 mb_row_flip = s->mb_height - mb_row - 1;
00565 else
00566 mb_row_flip = mb_row;
00567
00568 for (block=0; block<4; block++) {
00569 s->left_block[block].ref_frame = VP56_FRAME_NONE;
00570 s->left_block[block].dc_coeff = 0;
00571 s->left_block[block].not_null_dc = 0;
00572 }
00573 memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
00574 memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
00575
00576 s->above_block_idx[0] = 1;
00577 s->above_block_idx[1] = 2;
00578 s->above_block_idx[2] = 1;
00579 s->above_block_idx[3] = 2;
00580 s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
00581 s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
00582
00583 s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
00584 s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
00585 s->block_offset[1] = s->block_offset[0] + 8;
00586 s->block_offset[3] = s->block_offset[2] + 8;
00587 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
00588 s->block_offset[5] = s->block_offset[4];
00589
00590 for (mb_col=0; mb_col<s->mb_width; mb_col++) {
00591 vp56_decode_mb(s, mb_row, mb_col, is_alpha);
00592
00593 for (y=0; y<4; y++) {
00594 s->above_block_idx[y] += 2;
00595 s->block_offset[y] += 16;
00596 }
00597
00598 for (uv=4; uv<6; uv++) {
00599 s->above_block_idx[uv] += 1;
00600 s->block_offset[uv] += 8;
00601 }
00602 }
00603 }
00604
00605 next:
00606 if (p->key_frame || golden_frame) {
00607 if (s->framep[VP56_FRAME_GOLDEN]->data[0] &&
00608 s->framep[VP56_FRAME_GOLDEN] != s->framep[VP56_FRAME_GOLDEN2])
00609 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
00610 s->framep[VP56_FRAME_GOLDEN] = p;
00611 }
00612
00613 if (s->has_alpha) {
00614 FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN],
00615 s->framep[VP56_FRAME_GOLDEN2]);
00616 buf += alpha_offset;
00617 remaining_buf_size -= alpha_offset;
00618 }
00619 }
00620
00621 if (s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN] ||
00622 s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN2]) {
00623 if (s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN] &&
00624 s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN2])
00625 FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
00626 s->framep[VP56_FRAME_UNUSED]);
00627 else
00628 FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
00629 s->framep[VP56_FRAME_UNUSED2]);
00630 } else if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
00631 avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
00632 FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
00633 s->framep[VP56_FRAME_PREVIOUS]);
00634
00635 p->qstride = 0;
00636 p->qscale_table = s->qscale_table;
00637 p->qscale_type = FF_QSCALE_TYPE_VP56;
00638 *(AVFrame*)data = *p;
00639 *data_size = sizeof(AVFrame);
00640
00641 return avpkt->size;
00642 }
00643
00644 av_cold void vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
00645 {
00646 VP56Context *s = avctx->priv_data;
00647 int i;
00648
00649 s->avctx = avctx;
00650 avctx->pix_fmt = has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
00651
00652 if (avctx->idct_algo == FF_IDCT_AUTO)
00653 avctx->idct_algo = FF_IDCT_VP3;
00654 dsputil_init(&s->dsp, avctx);
00655 ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
00656 ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
00657
00658 for (i=0; i<4; i++)
00659 s->framep[i] = &s->frames[i];
00660 s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
00661 s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
00662 s->edge_emu_buffer_alloc = NULL;
00663
00664 s->above_blocks = NULL;
00665 s->macroblocks = NULL;
00666 s->quantizer = -1;
00667 s->deblock_filtering = 1;
00668
00669 s->filter = NULL;
00670
00671 s->has_alpha = has_alpha;
00672 if (flip) {
00673 s->flip = -1;
00674 s->frbi = 2;
00675 s->srbi = 0;
00676 } else {
00677 s->flip = 1;
00678 s->frbi = 0;
00679 s->srbi = 2;
00680 }
00681 }
00682
00683 av_cold int vp56_free(AVCodecContext *avctx)
00684 {
00685 VP56Context *s = avctx->priv_data;
00686
00687 av_freep(&s->qscale_table);
00688 av_freep(&s->above_blocks);
00689 av_freep(&s->macroblocks);
00690 av_freep(&s->edge_emu_buffer_alloc);
00691 if (s->framep[VP56_FRAME_GOLDEN]->data[0])
00692 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
00693 if (s->framep[VP56_FRAME_GOLDEN2]->data[0])
00694 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN2]);
00695 if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
00696 avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
00697 return 0;
00698 }