00001
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026
00027 #include "vp56.h"
00028 #include "vp56data.h"
00029
00030
00031 void ff_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 s->dsp.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 if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
00403 return;
00404
00405 ab = 6*is_alpha;
00406 b_max = 6 - 2*is_alpha;
00407
00408 switch (mb_type) {
00409 case VP56_MB_INTRA:
00410 for (b=0; b<b_max; b++) {
00411 plane = vp56_b2p[b+ab];
00412 s->dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
00413 s->stride[plane], s->block_coeff[b]);
00414 }
00415 break;
00416
00417 case VP56_MB_INTER_NOVEC_PF:
00418 case VP56_MB_INTER_NOVEC_GF:
00419 for (b=0; b<b_max; b++) {
00420 plane = vp56_b2p[b+ab];
00421 off = s->block_offset[b];
00422 s->dsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
00423 frame_ref->data[plane] + off,
00424 s->stride[plane], 8);
00425 s->dsp.idct_add(frame_current->data[plane] + off,
00426 s->stride[plane], s->block_coeff[b]);
00427 }
00428 break;
00429
00430 case VP56_MB_INTER_DELTA_PF:
00431 case VP56_MB_INTER_V1_PF:
00432 case VP56_MB_INTER_V2_PF:
00433 case VP56_MB_INTER_DELTA_GF:
00434 case VP56_MB_INTER_4V:
00435 case VP56_MB_INTER_V1_GF:
00436 case VP56_MB_INTER_V2_GF:
00437 for (b=0; b<b_max; b++) {
00438 int x_off = b==1 || b==3 ? 8 : 0;
00439 int y_off = b==2 || b==3 ? 8 : 0;
00440 plane = vp56_b2p[b+ab];
00441 vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
00442 16*col+x_off, 16*row+y_off);
00443 s->dsp.idct_add(frame_current->data[plane] + s->block_offset[b],
00444 s->stride[plane], s->block_coeff[b]);
00445 }
00446 break;
00447 }
00448 }
00449
00450 static int vp56_size_changed(AVCodecContext *avctx)
00451 {
00452 VP56Context *s = avctx->priv_data;
00453 int stride = s->framep[VP56_FRAME_CURRENT]->linesize[0];
00454 int i;
00455
00456 s->plane_width[0] = s->plane_width[3] = avctx->coded_width;
00457 s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
00458 s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
00459 s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
00460
00461 for (i=0; i<4; i++)
00462 s->stride[i] = s->flip * s->framep[VP56_FRAME_CURRENT]->linesize[i];
00463
00464 s->mb_width = (avctx->coded_width +15) / 16;
00465 s->mb_height = (avctx->coded_height+15) / 16;
00466
00467 if (s->mb_width > 1000 || s->mb_height > 1000) {
00468 avcodec_set_dimensions(avctx, 0, 0);
00469 av_log(avctx, AV_LOG_ERROR, "picture too big\n");
00470 return -1;
00471 }
00472
00473 s->qscale_table = av_realloc(s->qscale_table, s->mb_width);
00474 s->above_blocks = av_realloc(s->above_blocks,
00475 (4*s->mb_width+6) * sizeof(*s->above_blocks));
00476 s->macroblocks = av_realloc(s->macroblocks,
00477 s->mb_width*s->mb_height*sizeof(*s->macroblocks));
00478 av_free(s->edge_emu_buffer_alloc);
00479 s->edge_emu_buffer_alloc = av_malloc(16*stride);
00480 s->edge_emu_buffer = s->edge_emu_buffer_alloc;
00481 if (s->flip < 0)
00482 s->edge_emu_buffer += 15 * stride;
00483
00484 return 0;
00485 }
00486
00487 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00488 AVPacket *avpkt)
00489 {
00490 const uint8_t *buf = avpkt->data;
00491 VP56Context *s = avctx->priv_data;
00492 AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
00493 int remaining_buf_size = avpkt->size;
00494 int is_alpha, av_uninit(alpha_offset);
00495
00496 if (s->has_alpha) {
00497 if (remaining_buf_size < 3)
00498 return -1;
00499 alpha_offset = bytestream_get_be24(&buf);
00500 remaining_buf_size -= 3;
00501 if (remaining_buf_size < alpha_offset)
00502 return -1;
00503 }
00504
00505 for (is_alpha=0; is_alpha < 1+s->has_alpha; is_alpha++) {
00506 int mb_row, mb_col, mb_row_flip, mb_offset = 0;
00507 int block, y, uv, stride_y, stride_uv;
00508 int golden_frame = 0;
00509 int res;
00510
00511 s->modelp = &s->models[is_alpha];
00512
00513 res = s->parse_header(s, buf, remaining_buf_size, &golden_frame);
00514 if (!res)
00515 return -1;
00516
00517 if (res == 2) {
00518 int i;
00519 for (i = 0; i < 4; i++) {
00520 if (s->frames[i].data[0])
00521 avctx->release_buffer(avctx, &s->frames[i]);
00522 }
00523 if (is_alpha) {
00524 avcodec_set_dimensions(avctx, 0, 0);
00525 return -1;
00526 }
00527 }
00528
00529 if (!is_alpha) {
00530 p->reference = 1;
00531 if (avctx->get_buffer(avctx, p) < 0) {
00532 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00533 return -1;
00534 }
00535
00536 if (res == 2)
00537 if (vp56_size_changed(avctx)) {
00538 avctx->release_buffer(avctx, p);
00539 return -1;
00540 }
00541 }
00542
00543 if (p->key_frame) {
00544 p->pict_type = AV_PICTURE_TYPE_I;
00545 s->default_models_init(s);
00546 for (block=0; block<s->mb_height*s->mb_width; block++)
00547 s->macroblocks[block].type = VP56_MB_INTRA;
00548 } else {
00549 p->pict_type = AV_PICTURE_TYPE_P;
00550 vp56_parse_mb_type_models(s);
00551 s->parse_vector_models(s);
00552 s->mb_type = VP56_MB_INTER_NOVEC_PF;
00553 }
00554
00555 if (s->parse_coeff_models(s))
00556 goto next;
00557
00558 memset(s->prev_dc, 0, sizeof(s->prev_dc));
00559 s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
00560 s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
00561
00562 for (block=0; block < 4*s->mb_width+6; block++) {
00563 s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
00564 s->above_blocks[block].dc_coeff = 0;
00565 s->above_blocks[block].not_null_dc = 0;
00566 }
00567 s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
00568 s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
00569
00570 stride_y = p->linesize[0];
00571 stride_uv = p->linesize[1];
00572
00573 if (s->flip < 0)
00574 mb_offset = 7;
00575
00576
00577 for (mb_row=0; mb_row<s->mb_height; mb_row++) {
00578 if (s->flip < 0)
00579 mb_row_flip = s->mb_height - mb_row - 1;
00580 else
00581 mb_row_flip = mb_row;
00582
00583 for (block=0; block<4; block++) {
00584 s->left_block[block].ref_frame = VP56_FRAME_NONE;
00585 s->left_block[block].dc_coeff = 0;
00586 s->left_block[block].not_null_dc = 0;
00587 }
00588 memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
00589 memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
00590
00591 s->above_block_idx[0] = 1;
00592 s->above_block_idx[1] = 2;
00593 s->above_block_idx[2] = 1;
00594 s->above_block_idx[3] = 2;
00595 s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
00596 s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
00597
00598 s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
00599 s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
00600 s->block_offset[1] = s->block_offset[0] + 8;
00601 s->block_offset[3] = s->block_offset[2] + 8;
00602 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
00603 s->block_offset[5] = s->block_offset[4];
00604
00605 for (mb_col=0; mb_col<s->mb_width; mb_col++) {
00606 vp56_decode_mb(s, mb_row, mb_col, is_alpha);
00607
00608 for (y=0; y<4; y++) {
00609 s->above_block_idx[y] += 2;
00610 s->block_offset[y] += 16;
00611 }
00612
00613 for (uv=4; uv<6; uv++) {
00614 s->above_block_idx[uv] += 1;
00615 s->block_offset[uv] += 8;
00616 }
00617 }
00618 }
00619
00620 next:
00621 if (p->key_frame || golden_frame) {
00622 if (s->framep[VP56_FRAME_GOLDEN]->data[0] &&
00623 s->framep[VP56_FRAME_GOLDEN] != s->framep[VP56_FRAME_GOLDEN2])
00624 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
00625 s->framep[VP56_FRAME_GOLDEN] = p;
00626 }
00627
00628 if (s->has_alpha) {
00629 FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN],
00630 s->framep[VP56_FRAME_GOLDEN2]);
00631 buf += alpha_offset;
00632 remaining_buf_size -= alpha_offset;
00633 }
00634 }
00635
00636 if (s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN] ||
00637 s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN2]) {
00638 if (s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN] &&
00639 s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN2])
00640 FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
00641 s->framep[VP56_FRAME_UNUSED]);
00642 else
00643 FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
00644 s->framep[VP56_FRAME_UNUSED2]);
00645 } else if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
00646 avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
00647 FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
00648 s->framep[VP56_FRAME_PREVIOUS]);
00649
00650 p->qstride = 0;
00651 p->qscale_table = s->qscale_table;
00652 p->qscale_type = FF_QSCALE_TYPE_VP56;
00653 *(AVFrame*)data = *p;
00654 *data_size = sizeof(AVFrame);
00655
00656 return avpkt->size;
00657 }
00658
00659 av_cold void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
00660 {
00661 VP56Context *s = avctx->priv_data;
00662 int i;
00663
00664 s->avctx = avctx;
00665 avctx->pix_fmt = has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
00666
00667 if (avctx->idct_algo == FF_IDCT_AUTO)
00668 avctx->idct_algo = FF_IDCT_VP3;
00669 dsputil_init(&s->dsp, avctx);
00670 ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
00671 ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
00672
00673 for (i=0; i<4; i++) {
00674 s->framep[i] = &s->frames[i];
00675 avcodec_get_frame_defaults(&s->frames[i]);
00676 }
00677 s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
00678 s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
00679 s->edge_emu_buffer_alloc = NULL;
00680
00681 s->above_blocks = NULL;
00682 s->macroblocks = NULL;
00683 s->quantizer = -1;
00684 s->deblock_filtering = 1;
00685
00686 s->filter = NULL;
00687
00688 s->has_alpha = has_alpha;
00689 if (flip) {
00690 s->flip = -1;
00691 s->frbi = 2;
00692 s->srbi = 0;
00693 } else {
00694 s->flip = 1;
00695 s->frbi = 0;
00696 s->srbi = 2;
00697 }
00698 }
00699
00700 av_cold int ff_vp56_free(AVCodecContext *avctx)
00701 {
00702 VP56Context *s = avctx->priv_data;
00703
00704 av_freep(&s->qscale_table);
00705 av_freep(&s->above_blocks);
00706 av_freep(&s->macroblocks);
00707 av_freep(&s->edge_emu_buffer_alloc);
00708 if (s->framep[VP56_FRAME_GOLDEN]->data[0])
00709 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
00710 if (s->framep[VP56_FRAME_GOLDEN2]->data[0])
00711 avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN2]);
00712 if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
00713 avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
00714 return 0;
00715 }