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.

725 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->dsp.clear_blocks(*s->block_coeff);
  341. s->parse_coeff(s);
  342. vp56_add_predictors_dc(s, ref_frame);
  343. frame_current = s->framep[VP56_FRAME_CURRENT];
  344. frame_ref = s->framep[ref_frame];
  345. if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
  346. return;
  347. ab = 6*is_alpha;
  348. b_max = 6 - 2*is_alpha;
  349. switch (mb_type) {
  350. case VP56_MB_INTRA:
  351. for (b=0; b<b_max; b++) {
  352. plane = ff_vp56_b2p[b+ab];
  353. s->vp3dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
  354. s->stride[plane], s->block_coeff[b]);
  355. }
  356. break;
  357. case VP56_MB_INTER_NOVEC_PF:
  358. case VP56_MB_INTER_NOVEC_GF:
  359. for (b=0; b<b_max; b++) {
  360. plane = ff_vp56_b2p[b+ab];
  361. off = s->block_offset[b];
  362. s->dsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
  363. frame_ref->data[plane] + off,
  364. s->stride[plane], 8);
  365. s->vp3dsp.idct_add(frame_current->data[plane] + off,
  366. s->stride[plane], s->block_coeff[b]);
  367. }
  368. break;
  369. case VP56_MB_INTER_DELTA_PF:
  370. case VP56_MB_INTER_V1_PF:
  371. case VP56_MB_INTER_V2_PF:
  372. case VP56_MB_INTER_DELTA_GF:
  373. case VP56_MB_INTER_4V:
  374. case VP56_MB_INTER_V1_GF:
  375. case VP56_MB_INTER_V2_GF:
  376. for (b=0; b<b_max; b++) {
  377. int x_off = b==1 || b==3 ? 8 : 0;
  378. int y_off = b==2 || b==3 ? 8 : 0;
  379. plane = ff_vp56_b2p[b+ab];
  380. vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
  381. 16*col+x_off, 16*row+y_off);
  382. s->vp3dsp.idct_add(frame_current->data[plane] + s->block_offset[b],
  383. s->stride[plane], s->block_coeff[b]);
  384. }
  385. break;
  386. }
  387. }
  388. static int vp56_size_changed(AVCodecContext *avctx)
  389. {
  390. VP56Context *s = avctx->priv_data;
  391. int stride = s->framep[VP56_FRAME_CURRENT]->linesize[0];
  392. int i;
  393. s->plane_width[0] = s->plane_width[3] = avctx->coded_width;
  394. s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
  395. s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
  396. s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
  397. for (i=0; i<4; i++)
  398. s->stride[i] = s->flip * s->framep[VP56_FRAME_CURRENT]->linesize[i];
  399. s->mb_width = (avctx->coded_width +15) / 16;
  400. s->mb_height = (avctx->coded_height+15) / 16;
  401. if (s->mb_width > 1000 || s->mb_height > 1000) {
  402. avcodec_set_dimensions(avctx, 0, 0);
  403. av_log(avctx, AV_LOG_ERROR, "picture too big\n");
  404. return -1;
  405. }
  406. s->qscale_table = av_realloc(s->qscale_table, s->mb_width);
  407. s->above_blocks = av_realloc(s->above_blocks,
  408. (4*s->mb_width+6) * sizeof(*s->above_blocks));
  409. s->macroblocks = av_realloc(s->macroblocks,
  410. s->mb_width*s->mb_height*sizeof(*s->macroblocks));
  411. av_free(s->edge_emu_buffer_alloc);
  412. s->edge_emu_buffer_alloc = av_malloc(16*stride);
  413. s->edge_emu_buffer = s->edge_emu_buffer_alloc;
  414. if (s->flip < 0)
  415. s->edge_emu_buffer += 15 * stride;
  416. return 0;
  417. }
  418. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  419. AVPacket *avpkt)
  420. {
  421. const uint8_t *buf = avpkt->data;
  422. VP56Context *s = avctx->priv_data;
  423. AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
  424. int remaining_buf_size = avpkt->size;
  425. int is_alpha, av_uninit(alpha_offset);
  426. if (s->has_alpha) {
  427. if (remaining_buf_size < 3)
  428. return -1;
  429. alpha_offset = bytestream_get_be24(&buf);
  430. remaining_buf_size -= 3;
  431. if (remaining_buf_size < alpha_offset)
  432. return -1;
  433. }
  434. for (is_alpha=0; is_alpha < 1+s->has_alpha; is_alpha++) {
  435. int mb_row, mb_col, mb_row_flip, mb_offset = 0;
  436. int block, y, uv, stride_y, stride_uv;
  437. int golden_frame = 0;
  438. int res;
  439. s->modelp = &s->models[is_alpha];
  440. res = s->parse_header(s, buf, remaining_buf_size, &golden_frame);
  441. if (res < 0) {
  442. int i;
  443. for (i = 0; i < 4; i++) {
  444. if (s->frames[i].data[0])
  445. avctx->release_buffer(avctx, &s->frames[i]);
  446. }
  447. return res;
  448. }
  449. if (res == VP56_SIZE_CHANGE) {
  450. int i;
  451. for (i = 0; i < 4; i++) {
  452. if (s->frames[i].data[0])
  453. avctx->release_buffer(avctx, &s->frames[i]);
  454. }
  455. if (is_alpha) {
  456. avcodec_set_dimensions(avctx, 0, 0);
  457. return -1;
  458. }
  459. }
  460. if (!is_alpha) {
  461. p->reference = 1;
  462. if (ff_get_buffer(avctx, p) < 0) {
  463. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  464. return -1;
  465. }
  466. if (res == VP56_SIZE_CHANGE)
  467. if (vp56_size_changed(avctx)) {
  468. avctx->release_buffer(avctx, p);
  469. return -1;
  470. }
  471. }
  472. if (p->key_frame) {
  473. p->pict_type = AV_PICTURE_TYPE_I;
  474. s->default_models_init(s);
  475. for (block=0; block<s->mb_height*s->mb_width; block++)
  476. s->macroblocks[block].type = VP56_MB_INTRA;
  477. } else {
  478. p->pict_type = AV_PICTURE_TYPE_P;
  479. vp56_parse_mb_type_models(s);
  480. s->parse_vector_models(s);
  481. s->mb_type = VP56_MB_INTER_NOVEC_PF;
  482. }
  483. if (s->parse_coeff_models(s))
  484. goto next;
  485. memset(s->prev_dc, 0, sizeof(s->prev_dc));
  486. s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
  487. s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
  488. for (block=0; block < 4*s->mb_width+6; block++) {
  489. s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
  490. s->above_blocks[block].dc_coeff = 0;
  491. s->above_blocks[block].not_null_dc = 0;
  492. }
  493. s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
  494. s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
  495. stride_y = p->linesize[0];
  496. stride_uv = p->linesize[1];
  497. if (s->flip < 0)
  498. mb_offset = 7;
  499. /* main macroblocks loop */
  500. for (mb_row=0; mb_row<s->mb_height; mb_row++) {
  501. if (s->flip < 0)
  502. mb_row_flip = s->mb_height - mb_row - 1;
  503. else
  504. mb_row_flip = mb_row;
  505. for (block=0; block<4; block++) {
  506. s->left_block[block].ref_frame = VP56_FRAME_NONE;
  507. s->left_block[block].dc_coeff = 0;
  508. s->left_block[block].not_null_dc = 0;
  509. }
  510. memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
  511. memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
  512. s->above_block_idx[0] = 1;
  513. s->above_block_idx[1] = 2;
  514. s->above_block_idx[2] = 1;
  515. s->above_block_idx[3] = 2;
  516. s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
  517. s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
  518. s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
  519. s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
  520. s->block_offset[1] = s->block_offset[0] + 8;
  521. s->block_offset[3] = s->block_offset[2] + 8;
  522. s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
  523. s->block_offset[5] = s->block_offset[4];
  524. for (mb_col=0; mb_col<s->mb_width; mb_col++) {
  525. vp56_decode_mb(s, mb_row, mb_col, is_alpha);
  526. for (y=0; y<4; y++) {
  527. s->above_block_idx[y] += 2;
  528. s->block_offset[y] += 16;
  529. }
  530. for (uv=4; uv<6; uv++) {
  531. s->above_block_idx[uv] += 1;
  532. s->block_offset[uv] += 8;
  533. }
  534. }
  535. }
  536. next:
  537. if (p->key_frame || golden_frame) {
  538. if (s->framep[VP56_FRAME_GOLDEN]->data[0] &&
  539. s->framep[VP56_FRAME_GOLDEN] != s->framep[VP56_FRAME_GOLDEN2])
  540. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
  541. s->framep[VP56_FRAME_GOLDEN] = p;
  542. }
  543. if (s->has_alpha) {
  544. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN],
  545. s->framep[VP56_FRAME_GOLDEN2]);
  546. buf += alpha_offset;
  547. remaining_buf_size -= alpha_offset;
  548. }
  549. }
  550. if (s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN] ||
  551. s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN2]) {
  552. if (s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN] &&
  553. s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN2])
  554. FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
  555. s->framep[VP56_FRAME_UNUSED]);
  556. else
  557. FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
  558. s->framep[VP56_FRAME_UNUSED2]);
  559. } else if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
  560. avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
  561. FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
  562. s->framep[VP56_FRAME_PREVIOUS]);
  563. p->qstride = 0;
  564. p->qscale_table = s->qscale_table;
  565. p->qscale_type = FF_QSCALE_TYPE_VP56;
  566. *(AVFrame*)data = *p;
  567. *got_frame = 1;
  568. return avpkt->size;
  569. }
  570. av_cold void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
  571. {
  572. VP56Context *s = avctx->priv_data;
  573. int i;
  574. s->avctx = avctx;
  575. avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  576. ff_dsputil_init(&s->dsp, avctx);
  577. ff_h264chroma_init(&s->h264chroma, 8);
  578. ff_videodsp_init(&s->vdsp, 8);
  579. ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
  580. ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
  581. ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
  582. ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
  583. for (i=0; i<4; i++)
  584. s->framep[i] = &s->frames[i];
  585. s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
  586. s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
  587. s->edge_emu_buffer_alloc = NULL;
  588. s->above_blocks = NULL;
  589. s->macroblocks = NULL;
  590. s->quantizer = -1;
  591. s->deblock_filtering = 1;
  592. s->filter = NULL;
  593. s->has_alpha = has_alpha;
  594. if (flip) {
  595. s->flip = -1;
  596. s->frbi = 2;
  597. s->srbi = 0;
  598. } else {
  599. s->flip = 1;
  600. s->frbi = 0;
  601. s->srbi = 2;
  602. }
  603. }
  604. av_cold int ff_vp56_free(AVCodecContext *avctx)
  605. {
  606. VP56Context *s = avctx->priv_data;
  607. av_freep(&s->qscale_table);
  608. av_freep(&s->above_blocks);
  609. av_freep(&s->macroblocks);
  610. av_freep(&s->edge_emu_buffer_alloc);
  611. if (s->framep[VP56_FRAME_GOLDEN]->data[0])
  612. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
  613. if (s->framep[VP56_FRAME_GOLDEN2]->data[0])
  614. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN2]);
  615. if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
  616. avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
  617. return 0;
  618. }