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.

714 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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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. avcodec_set_dimensions(avctx, 0, 0);
  400. av_log(avctx, AV_LOG_ERROR, "picture too big\n");
  401. return -1;
  402. }
  403. s->qscale_table = av_realloc(s->qscale_table, s->mb_width);
  404. s->above_blocks = av_realloc(s->above_blocks,
  405. (4*s->mb_width+6) * sizeof(*s->above_blocks));
  406. s->macroblocks = av_realloc(s->macroblocks,
  407. s->mb_width*s->mb_height*sizeof(*s->macroblocks));
  408. av_free(s->edge_emu_buffer_alloc);
  409. s->edge_emu_buffer_alloc = av_malloc(16*stride);
  410. s->edge_emu_buffer = s->edge_emu_buffer_alloc;
  411. if (s->flip < 0)
  412. s->edge_emu_buffer += 15 * stride;
  413. return 0;
  414. }
  415. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  416. AVPacket *avpkt)
  417. {
  418. const uint8_t *buf = avpkt->data;
  419. VP56Context *s = avctx->priv_data;
  420. AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
  421. int remaining_buf_size = avpkt->size;
  422. int is_alpha, av_uninit(alpha_offset);
  423. if (s->has_alpha) {
  424. if (remaining_buf_size < 3)
  425. return -1;
  426. alpha_offset = bytestream_get_be24(&buf);
  427. remaining_buf_size -= 3;
  428. if (remaining_buf_size < alpha_offset)
  429. return -1;
  430. }
  431. for (is_alpha=0; is_alpha < 1+s->has_alpha; is_alpha++) {
  432. int mb_row, mb_col, mb_row_flip, mb_offset = 0;
  433. int block, y, uv, stride_y, stride_uv;
  434. int golden_frame = 0;
  435. int res;
  436. s->modelp = &s->models[is_alpha];
  437. res = s->parse_header(s, buf, remaining_buf_size, &golden_frame);
  438. if (!res)
  439. return -1;
  440. if (res == 2) {
  441. int i;
  442. for (i = 0; i < 4; i++) {
  443. if (s->frames[i].data[0])
  444. avctx->release_buffer(avctx, &s->frames[i]);
  445. }
  446. if (is_alpha) {
  447. avcodec_set_dimensions(avctx, 0, 0);
  448. return -1;
  449. }
  450. }
  451. if (!is_alpha) {
  452. p->reference = 1;
  453. if (avctx->get_buffer(avctx, p) < 0) {
  454. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  455. return -1;
  456. }
  457. if (res == 2)
  458. if (vp56_size_changed(avctx)) {
  459. avctx->release_buffer(avctx, p);
  460. return -1;
  461. }
  462. }
  463. if (p->key_frame) {
  464. p->pict_type = AV_PICTURE_TYPE_I;
  465. s->default_models_init(s);
  466. for (block=0; block<s->mb_height*s->mb_width; block++)
  467. s->macroblocks[block].type = VP56_MB_INTRA;
  468. } else {
  469. p->pict_type = AV_PICTURE_TYPE_P;
  470. vp56_parse_mb_type_models(s);
  471. s->parse_vector_models(s);
  472. s->mb_type = VP56_MB_INTER_NOVEC_PF;
  473. }
  474. if (s->parse_coeff_models(s))
  475. goto next;
  476. memset(s->prev_dc, 0, sizeof(s->prev_dc));
  477. s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
  478. s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
  479. for (block=0; block < 4*s->mb_width+6; block++) {
  480. s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
  481. s->above_blocks[block].dc_coeff = 0;
  482. s->above_blocks[block].not_null_dc = 0;
  483. }
  484. s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
  485. s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
  486. stride_y = p->linesize[0];
  487. stride_uv = p->linesize[1];
  488. if (s->flip < 0)
  489. mb_offset = 7;
  490. /* main macroblocks loop */
  491. for (mb_row=0; mb_row<s->mb_height; mb_row++) {
  492. if (s->flip < 0)
  493. mb_row_flip = s->mb_height - mb_row - 1;
  494. else
  495. mb_row_flip = mb_row;
  496. for (block=0; block<4; block++) {
  497. s->left_block[block].ref_frame = VP56_FRAME_NONE;
  498. s->left_block[block].dc_coeff = 0;
  499. s->left_block[block].not_null_dc = 0;
  500. }
  501. memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
  502. memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
  503. s->above_block_idx[0] = 1;
  504. s->above_block_idx[1] = 2;
  505. s->above_block_idx[2] = 1;
  506. s->above_block_idx[3] = 2;
  507. s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
  508. s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
  509. s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
  510. s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
  511. s->block_offset[1] = s->block_offset[0] + 8;
  512. s->block_offset[3] = s->block_offset[2] + 8;
  513. s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
  514. s->block_offset[5] = s->block_offset[4];
  515. for (mb_col=0; mb_col<s->mb_width; mb_col++) {
  516. vp56_decode_mb(s, mb_row, mb_col, is_alpha);
  517. for (y=0; y<4; y++) {
  518. s->above_block_idx[y] += 2;
  519. s->block_offset[y] += 16;
  520. }
  521. for (uv=4; uv<6; uv++) {
  522. s->above_block_idx[uv] += 1;
  523. s->block_offset[uv] += 8;
  524. }
  525. }
  526. }
  527. next:
  528. if (p->key_frame || golden_frame) {
  529. if (s->framep[VP56_FRAME_GOLDEN]->data[0] &&
  530. s->framep[VP56_FRAME_GOLDEN] != s->framep[VP56_FRAME_GOLDEN2])
  531. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
  532. s->framep[VP56_FRAME_GOLDEN] = p;
  533. }
  534. if (s->has_alpha) {
  535. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN],
  536. s->framep[VP56_FRAME_GOLDEN2]);
  537. buf += alpha_offset;
  538. remaining_buf_size -= alpha_offset;
  539. }
  540. }
  541. if (s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN] ||
  542. s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN2]) {
  543. if (s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN] &&
  544. s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN2])
  545. FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
  546. s->framep[VP56_FRAME_UNUSED]);
  547. else
  548. FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
  549. s->framep[VP56_FRAME_UNUSED2]);
  550. } else if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
  551. avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
  552. FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
  553. s->framep[VP56_FRAME_PREVIOUS]);
  554. p->qstride = 0;
  555. p->qscale_table = s->qscale_table;
  556. p->qscale_type = FF_QSCALE_TYPE_VP56;
  557. *(AVFrame*)data = *p;
  558. *data_size = sizeof(AVFrame);
  559. return avpkt->size;
  560. }
  561. av_cold void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
  562. {
  563. VP56Context *s = avctx->priv_data;
  564. int i;
  565. s->avctx = avctx;
  566. avctx->pix_fmt = has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
  567. if (avctx->idct_algo == FF_IDCT_AUTO)
  568. avctx->idct_algo = FF_IDCT_VP3;
  569. dsputil_init(&s->dsp, avctx);
  570. ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
  571. ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
  572. for (i=0; i<4; i++)
  573. s->framep[i] = &s->frames[i];
  574. s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
  575. s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
  576. s->edge_emu_buffer_alloc = NULL;
  577. s->above_blocks = NULL;
  578. s->macroblocks = NULL;
  579. s->quantizer = -1;
  580. s->deblock_filtering = 1;
  581. s->filter = NULL;
  582. s->has_alpha = has_alpha;
  583. if (flip) {
  584. s->flip = -1;
  585. s->frbi = 2;
  586. s->srbi = 0;
  587. } else {
  588. s->flip = 1;
  589. s->frbi = 0;
  590. s->srbi = 2;
  591. }
  592. }
  593. av_cold int ff_vp56_free(AVCodecContext *avctx)
  594. {
  595. VP56Context *s = avctx->priv_data;
  596. av_freep(&s->qscale_table);
  597. av_freep(&s->above_blocks);
  598. av_freep(&s->macroblocks);
  599. av_freep(&s->edge_emu_buffer_alloc);
  600. if (s->framep[VP56_FRAME_GOLDEN]->data[0])
  601. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
  602. if (s->framep[VP56_FRAME_GOLDEN2]->data[0])
  603. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN2]);
  604. if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
  605. avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
  606. return 0;
  607. }