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.

728 lines
24KB

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