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.

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