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.

701 lines
23KB

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