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.

768 lines
24KB

  1. /*
  2. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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(VP56Context *s)
  392. {
  393. AVCodecContext *avctx = s->avctx;
  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. if (s->alpha_context)
  420. return vp56_size_changed(s->alpha_context);
  421. return 0;
  422. }
  423. static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *, int, int);
  424. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  425. AVPacket *avpkt)
  426. {
  427. const uint8_t *buf = avpkt->data;
  428. VP56Context *s = avctx->priv_data;
  429. AVFrame *p = 0;
  430. int remaining_buf_size = avpkt->size;
  431. int av_uninit(alpha_offset);
  432. int i, res;
  433. /* select a current frame from the unused frames */
  434. for (i = 0; i < 4; ++i) {
  435. if (!s->frames[i].data[0]) {
  436. p = &s->frames[i];
  437. break;
  438. }
  439. }
  440. av_assert0(p != 0);
  441. s->framep[VP56_FRAME_CURRENT] = p;
  442. if (s->alpha_context)
  443. s->alpha_context->framep[VP56_FRAME_CURRENT] = p;
  444. if (s->has_alpha) {
  445. if (remaining_buf_size < 3)
  446. return -1;
  447. alpha_offset = bytestream_get_be24(&buf);
  448. remaining_buf_size -= 3;
  449. if (remaining_buf_size < alpha_offset)
  450. return -1;
  451. }
  452. res = s->parse_header(s, buf, remaining_buf_size);
  453. if (res < 0)
  454. return res;
  455. if (res == VP56_SIZE_CHANGE) {
  456. for (i = 0; i < 4; i++) {
  457. if (s->frames[i].data[0])
  458. avctx->release_buffer(avctx, &s->frames[i]);
  459. }
  460. }
  461. p->reference = 3;
  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(s)) {
  468. avctx->release_buffer(avctx, p);
  469. return -1;
  470. }
  471. }
  472. if (s->has_alpha) {
  473. int bak_w = avctx->width;
  474. int bak_h = avctx->height;
  475. int bak_cw = avctx->coded_width;
  476. int bak_ch = avctx->coded_height;
  477. buf += alpha_offset;
  478. remaining_buf_size -= alpha_offset;
  479. res = s->alpha_context->parse_header(s->alpha_context, buf, remaining_buf_size);
  480. if (res != 0) {
  481. if(res==VP56_SIZE_CHANGE) {
  482. av_log(avctx, AV_LOG_ERROR, "Alpha reconfiguration\n");
  483. avctx->width = bak_w;
  484. avctx->height = bak_h;
  485. avctx->coded_width = bak_cw;
  486. avctx->coded_height = bak_ch;
  487. }
  488. avctx->release_buffer(avctx, p);
  489. return -1;
  490. }
  491. }
  492. avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, s->has_alpha + 1);
  493. /* release frames that aren't in use */
  494. for (i = 0; i < 4; ++i) {
  495. AVFrame *victim = &s->frames[i];
  496. if (!victim->data[0])
  497. continue;
  498. if (victim != s->framep[VP56_FRAME_PREVIOUS] &&
  499. victim != s->framep[VP56_FRAME_GOLDEN] &&
  500. (!s->has_alpha || victim != s->alpha_context->framep[VP56_FRAME_GOLDEN]))
  501. avctx->release_buffer(avctx, victim);
  502. }
  503. p->qstride = 0;
  504. p->qscale_table = s->qscale_table;
  505. p->qscale_type = FF_QSCALE_TYPE_VP56;
  506. *(AVFrame*)data = *p;
  507. *got_frame = 1;
  508. return avpkt->size;
  509. }
  510. static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *data,
  511. int jobnr, int threadnr)
  512. {
  513. VP56Context *s0 = avctx->priv_data;
  514. int is_alpha = (jobnr == 1);
  515. VP56Context *s = is_alpha ? s0->alpha_context : s0;
  516. AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
  517. int mb_row, mb_col, mb_row_flip, mb_offset = 0;
  518. int block, y, uv, stride_y, stride_uv;
  519. if (p->key_frame) {
  520. p->pict_type = AV_PICTURE_TYPE_I;
  521. s->default_models_init(s);
  522. for (block=0; block<s->mb_height*s->mb_width; block++)
  523. s->macroblocks[block].type = VP56_MB_INTRA;
  524. } else {
  525. p->pict_type = AV_PICTURE_TYPE_P;
  526. vp56_parse_mb_type_models(s);
  527. s->parse_vector_models(s);
  528. s->mb_type = VP56_MB_INTER_NOVEC_PF;
  529. }
  530. if (s->parse_coeff_models(s))
  531. goto next;
  532. memset(s->prev_dc, 0, sizeof(s->prev_dc));
  533. s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
  534. s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
  535. for (block=0; block < 4*s->mb_width+6; block++) {
  536. s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
  537. s->above_blocks[block].dc_coeff = 0;
  538. s->above_blocks[block].not_null_dc = 0;
  539. }
  540. s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
  541. s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
  542. stride_y = p->linesize[0];
  543. stride_uv = p->linesize[1];
  544. if (s->flip < 0)
  545. mb_offset = 7;
  546. /* main macroblocks loop */
  547. for (mb_row=0; mb_row<s->mb_height; mb_row++) {
  548. if (s->flip < 0)
  549. mb_row_flip = s->mb_height - mb_row - 1;
  550. else
  551. mb_row_flip = mb_row;
  552. for (block=0; block<4; block++) {
  553. s->left_block[block].ref_frame = VP56_FRAME_NONE;
  554. s->left_block[block].dc_coeff = 0;
  555. s->left_block[block].not_null_dc = 0;
  556. }
  557. memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
  558. memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
  559. s->above_block_idx[0] = 1;
  560. s->above_block_idx[1] = 2;
  561. s->above_block_idx[2] = 1;
  562. s->above_block_idx[3] = 2;
  563. s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
  564. s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
  565. s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
  566. s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
  567. s->block_offset[1] = s->block_offset[0] + 8;
  568. s->block_offset[3] = s->block_offset[2] + 8;
  569. s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
  570. s->block_offset[5] = s->block_offset[4];
  571. for (mb_col=0; mb_col<s->mb_width; mb_col++) {
  572. vp56_decode_mb(s, mb_row, mb_col, is_alpha);
  573. for (y=0; y<4; y++) {
  574. s->above_block_idx[y] += 2;
  575. s->block_offset[y] += 16;
  576. }
  577. for (uv=4; uv<6; uv++) {
  578. s->above_block_idx[uv] += 1;
  579. s->block_offset[uv] += 8;
  580. }
  581. }
  582. }
  583. next:
  584. if (p->key_frame || s->golden_frame) {
  585. s->framep[VP56_FRAME_GOLDEN] = p;
  586. }
  587. FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
  588. s->framep[VP56_FRAME_PREVIOUS]);
  589. return 0;
  590. }
  591. av_cold void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
  592. {
  593. VP56Context *s = avctx->priv_data;
  594. ff_vp56_init_context(avctx, s, flip, has_alpha);
  595. }
  596. av_cold void ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
  597. int flip, int has_alpha)
  598. {
  599. int i;
  600. s->avctx = avctx;
  601. avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  602. ff_dsputil_init(&s->dsp, avctx);
  603. ff_h264chroma_init(&s->h264chroma, 8);
  604. ff_videodsp_init(&s->vdsp, 8);
  605. ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
  606. ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
  607. ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
  608. ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
  609. for (i=0; i<4; i++) {
  610. s->framep[i] = &s->frames[i];
  611. avcodec_get_frame_defaults(&s->frames[i]);
  612. }
  613. s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
  614. s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
  615. s->edge_emu_buffer_alloc = NULL;
  616. s->above_blocks = NULL;
  617. s->macroblocks = NULL;
  618. s->quantizer = -1;
  619. s->deblock_filtering = 1;
  620. s->golden_frame = 0;
  621. s->filter = NULL;
  622. s->has_alpha = has_alpha;
  623. s->modelp = &s->model;
  624. if (flip) {
  625. s->flip = -1;
  626. s->frbi = 2;
  627. s->srbi = 0;
  628. } else {
  629. s->flip = 1;
  630. s->frbi = 0;
  631. s->srbi = 2;
  632. }
  633. }
  634. av_cold int ff_vp56_free(AVCodecContext *avctx)
  635. {
  636. VP56Context *s = avctx->priv_data;
  637. return ff_vp56_free_context(s);
  638. }
  639. av_cold int ff_vp56_free_context(VP56Context *s)
  640. {
  641. AVCodecContext *avctx = s->avctx;
  642. int i;
  643. av_freep(&s->qscale_table);
  644. av_freep(&s->above_blocks);
  645. av_freep(&s->macroblocks);
  646. av_freep(&s->edge_emu_buffer_alloc);
  647. for (i = 0; i < 4; ++i) {
  648. if (s->frames[i].data[0])
  649. avctx->release_buffer(avctx, &s->frames[i]);
  650. }
  651. return 0;
  652. }