You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

711 lines
23KB

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