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.

843 lines
26KB

  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 = ff_vp56_dc_dequant[quantizer] << 2;
  34. s->dequant_ac = ff_vp56_ac_dequant[quantizer] << 2;
  35. }
  36. static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
  37. VP56Frame ref_frame)
  38. {
  39. int nb_pred = 0;
  40. VP56mv vect[2] = {{0,0}, {0,0}};
  41. int pos, offset;
  42. VP56mv mvp;
  43. for (pos=0; pos<12; pos++) {
  44. mvp.x = col + ff_vp56_candidate_predictor_pos[pos][0];
  45. mvp.y = row + ff_vp56_candidate_predictor_pos[pos][1];
  46. if (mvp.x < 0 || mvp.x >= s->mb_width ||
  47. mvp.y < 0 || mvp.y >= s->mb_height)
  48. continue;
  49. offset = mvp.x + s->mb_width*mvp.y;
  50. if (ff_vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
  51. continue;
  52. if ((s->macroblocks[offset].mv.x == vect[0].x &&
  53. s->macroblocks[offset].mv.y == vect[0].y) ||
  54. (s->macroblocks[offset].mv.x == 0 &&
  55. s->macroblocks[offset].mv.y == 0))
  56. continue;
  57. vect[nb_pred++] = s->macroblocks[offset].mv;
  58. if (nb_pred > 1) {
  59. nb_pred = -1;
  60. break;
  61. }
  62. s->vector_candidate_pos = pos;
  63. }
  64. s->vector_candidate[0] = vect[0];
  65. s->vector_candidate[1] = vect[1];
  66. return nb_pred+1;
  67. }
  68. static void vp56_parse_mb_type_models(VP56Context *s)
  69. {
  70. VP56RangeCoder *c = &s->c;
  71. VP56Model *model = s->modelp;
  72. int i, ctx, type;
  73. for (ctx=0; ctx<3; ctx++) {
  74. if (vp56_rac_get_prob_branchy(c, 174)) {
  75. int idx = vp56_rac_gets(c, 4);
  76. memcpy(model->mb_types_stats[ctx],
  77. ff_vp56_pre_def_mb_type_stats[idx][ctx],
  78. sizeof(model->mb_types_stats[ctx]));
  79. }
  80. if (vp56_rac_get_prob_branchy(c, 254)) {
  81. for (type=0; type<10; type++) {
  82. for(i=0; i<2; i++) {
  83. if (vp56_rac_get_prob_branchy(c, 205)) {
  84. int delta, sign = vp56_rac_get(c);
  85. delta = vp56_rac_get_tree(c, ff_vp56_pmbtm_tree,
  86. ff_vp56_mb_type_model_model);
  87. if (!delta)
  88. delta = 4 * vp56_rac_gets(c, 7);
  89. model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. /* compute MB type probability tables based on previous MB type */
  96. for (ctx=0; ctx<3; ctx++) {
  97. int p[10];
  98. for (type=0; type<10; type++)
  99. p[type] = 100 * model->mb_types_stats[ctx][type][1];
  100. for (type=0; type<10; type++) {
  101. int p02, p34, p0234, p17, p56, p89, p5689, p156789;
  102. /* conservative MB type probability */
  103. 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]);
  104. p[type] = 0; /* same MB type => weight is null */
  105. /* binary tree parsing probabilities */
  106. p02 = p[0] + p[2];
  107. p34 = p[3] + p[4];
  108. p0234 = p02 + p34;
  109. p17 = p[1] + p[7];
  110. p56 = p[5] + p[6];
  111. p89 = p[8] + p[9];
  112. p5689 = p56 + p89;
  113. p156789 = p17 + p5689;
  114. model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
  115. model->mb_type[ctx][type][2] = 1 + 255 * p02 / (1+p0234);
  116. model->mb_type[ctx][type][3] = 1 + 255 * p17 / (1+p156789);
  117. model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
  118. model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
  119. model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
  120. model->mb_type[ctx][type][7] = 1 + 255 * p56 / (1+p5689);
  121. model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
  122. model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
  123. /* restore initial value */
  124. p[type] = 100 * model->mb_types_stats[ctx][type][1];
  125. }
  126. }
  127. }
  128. static VP56mb vp56_parse_mb_type(VP56Context *s,
  129. VP56mb prev_type, int ctx)
  130. {
  131. uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
  132. VP56RangeCoder *c = &s->c;
  133. if (vp56_rac_get_prob_branchy(c, mb_type_model[0]))
  134. return prev_type;
  135. else
  136. return vp56_rac_get_tree(c, ff_vp56_pmbt_tree, mb_type_model);
  137. }
  138. static void vp56_decode_4mv(VP56Context *s, int row, int col)
  139. {
  140. VP56mv mv = {0,0};
  141. int type[4];
  142. int b;
  143. /* parse each block type */
  144. for (b=0; b<4; b++) {
  145. type[b] = vp56_rac_gets(&s->c, 2);
  146. if (type[b])
  147. type[b]++; /* only returns 0, 2, 3 or 4 (all INTER_PF) */
  148. }
  149. /* get vectors */
  150. for (b=0; b<4; b++) {
  151. switch (type[b]) {
  152. case VP56_MB_INTER_NOVEC_PF:
  153. s->mv[b] = (VP56mv) {0,0};
  154. break;
  155. case VP56_MB_INTER_DELTA_PF:
  156. s->parse_vector_adjustment(s, &s->mv[b]);
  157. break;
  158. case VP56_MB_INTER_V1_PF:
  159. s->mv[b] = s->vector_candidate[0];
  160. break;
  161. case VP56_MB_INTER_V2_PF:
  162. s->mv[b] = s->vector_candidate[1];
  163. break;
  164. }
  165. mv.x += s->mv[b].x;
  166. mv.y += s->mv[b].y;
  167. }
  168. /* this is the one selected for the whole MB for prediction */
  169. s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
  170. /* chroma vectors are average luma vectors */
  171. if (s->avctx->codec->id == AV_CODEC_ID_VP5) {
  172. s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
  173. s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
  174. } else {
  175. s->mv[4] = s->mv[5] = (VP56mv) {mv.x/4, mv.y/4};
  176. }
  177. }
  178. static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
  179. {
  180. VP56mv *mv, vect = {0,0};
  181. int ctx, b;
  182. ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
  183. s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
  184. s->macroblocks[row * s->mb_width + col].type = s->mb_type;
  185. switch (s->mb_type) {
  186. case VP56_MB_INTER_V1_PF:
  187. mv = &s->vector_candidate[0];
  188. break;
  189. case VP56_MB_INTER_V2_PF:
  190. mv = &s->vector_candidate[1];
  191. break;
  192. case VP56_MB_INTER_V1_GF:
  193. vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
  194. mv = &s->vector_candidate[0];
  195. break;
  196. case VP56_MB_INTER_V2_GF:
  197. vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
  198. mv = &s->vector_candidate[1];
  199. break;
  200. case VP56_MB_INTER_DELTA_PF:
  201. s->parse_vector_adjustment(s, &vect);
  202. mv = &vect;
  203. break;
  204. case VP56_MB_INTER_DELTA_GF:
  205. vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
  206. s->parse_vector_adjustment(s, &vect);
  207. mv = &vect;
  208. break;
  209. case VP56_MB_INTER_4V:
  210. vp56_decode_4mv(s, row, col);
  211. return s->mb_type;
  212. default:
  213. mv = &vect;
  214. break;
  215. }
  216. s->macroblocks[row*s->mb_width + col].mv = *mv;
  217. /* same vector for all blocks */
  218. for (b=0; b<6; b++)
  219. s->mv[b] = *mv;
  220. return s->mb_type;
  221. }
  222. static VP56mb vp56_conceal_mv(VP56Context *s, int row, int col)
  223. {
  224. VP56mv *mv, vect = {0,0};
  225. int b;
  226. s->mb_type = VP56_MB_INTER_NOVEC_PF;
  227. s->macroblocks[row * s->mb_width + col].type = s->mb_type;
  228. mv = &vect;
  229. s->macroblocks[row*s->mb_width + col].mv = *mv;
  230. /* same vector for all blocks */
  231. for (b=0; b<6; b++)
  232. s->mv[b] = *mv;
  233. return s->mb_type;
  234. }
  235. static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
  236. {
  237. int idx = s->idct_scantable[0];
  238. int b;
  239. for (b=0; b<6; b++) {
  240. VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
  241. VP56RefDc *lb = &s->left_block[ff_vp56_b6to4[b]];
  242. int count = 0;
  243. int dc = 0;
  244. int i;
  245. if (ref_frame == lb->ref_frame) {
  246. dc += lb->dc_coeff;
  247. count++;
  248. }
  249. if (ref_frame == ab->ref_frame) {
  250. dc += ab->dc_coeff;
  251. count++;
  252. }
  253. if (s->avctx->codec->id == AV_CODEC_ID_VP5)
  254. for (i=0; i<2; i++)
  255. if (count < 2 && ref_frame == ab[-1+2*i].ref_frame) {
  256. dc += ab[-1+2*i].dc_coeff;
  257. count++;
  258. }
  259. if (count == 0)
  260. dc = s->prev_dc[ff_vp56_b2p[b]][ref_frame];
  261. else if (count == 2)
  262. dc /= 2;
  263. s->block_coeff[b][idx] += dc;
  264. s->prev_dc[ff_vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
  265. ab->dc_coeff = s->block_coeff[b][idx];
  266. ab->ref_frame = ref_frame;
  267. lb->dc_coeff = s->block_coeff[b][idx];
  268. lb->ref_frame = ref_frame;
  269. s->block_coeff[b][idx] *= s->dequant_dc;
  270. }
  271. }
  272. static void vp56_deblock_filter(VP56Context *s, uint8_t *yuv,
  273. ptrdiff_t stride, int dx, int dy)
  274. {
  275. int t = ff_vp56_filter_threshold[s->quantizer];
  276. if (dx) s->vp56dsp.edge_filter_hor(yuv + 10-dx , stride, t);
  277. if (dy) s->vp56dsp.edge_filter_ver(yuv + stride*(10-dy), stride, t);
  278. }
  279. static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
  280. ptrdiff_t stride, int x, int y)
  281. {
  282. uint8_t *dst = s->frames[VP56_FRAME_CURRENT]->data[plane] + s->block_offset[b];
  283. uint8_t *src_block;
  284. int src_offset;
  285. int overlap_offset = 0;
  286. int mask = s->vp56_coord_div[b] - 1;
  287. int deblock_filtering = s->deblock_filtering;
  288. int dx;
  289. int dy;
  290. if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  291. (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
  292. && !s->frames[VP56_FRAME_CURRENT]->key_frame))
  293. deblock_filtering = 0;
  294. dx = s->mv[b].x / s->vp56_coord_div[b];
  295. dy = s->mv[b].y / s->vp56_coord_div[b];
  296. if (b >= 4) {
  297. x /= 2;
  298. y /= 2;
  299. }
  300. x += dx - 2;
  301. y += dy - 2;
  302. if (x<0 || x+12>=s->plane_width[plane] ||
  303. y<0 || y+12>=s->plane_height[plane]) {
  304. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  305. src + s->block_offset[b] + (dy-2)*stride + (dx-2),
  306. stride, stride,
  307. 12, 12, x, y,
  308. s->plane_width[plane],
  309. s->plane_height[plane]);
  310. src_block = s->edge_emu_buffer;
  311. src_offset = 2 + 2*stride;
  312. } else if (deblock_filtering) {
  313. /* only need a 12x12 block, but there is no such dsp function, */
  314. /* so copy a 16x12 block */
  315. s->hdsp.put_pixels_tab[0][0](s->edge_emu_buffer,
  316. src + s->block_offset[b] + (dy-2)*stride + (dx-2),
  317. stride, 12);
  318. src_block = s->edge_emu_buffer;
  319. src_offset = 2 + 2*stride;
  320. } else {
  321. src_block = src;
  322. src_offset = s->block_offset[b] + dy*stride + dx;
  323. }
  324. if (deblock_filtering)
  325. vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
  326. if (s->mv[b].x & mask)
  327. overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
  328. if (s->mv[b].y & mask)
  329. overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
  330. if (overlap_offset) {
  331. if (s->filter)
  332. s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
  333. stride, s->mv[b], mask, s->filter_selection, b<4);
  334. else
  335. s->vp3dsp.put_no_rnd_pixels_l2(dst, src_block+src_offset,
  336. src_block+src_offset+overlap_offset,
  337. stride, 8);
  338. } else {
  339. s->hdsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
  340. }
  341. }
  342. static int vp56_decode_mb(VP56Context *s, int row, int col, int is_alpha)
  343. {
  344. AVFrame *frame_current, *frame_ref;
  345. VP56mb mb_type;
  346. VP56Frame ref_frame;
  347. int b, ab, b_max, plane, off;
  348. int ret;
  349. if (s->frames[VP56_FRAME_CURRENT]->key_frame)
  350. mb_type = VP56_MB_INTRA;
  351. else
  352. mb_type = vp56_decode_mv(s, row, col);
  353. ref_frame = ff_vp56_reference_frame[mb_type];
  354. ret = s->parse_coeff(s);
  355. if (ret < 0)
  356. return ret;
  357. vp56_add_predictors_dc(s, ref_frame);
  358. frame_current = s->frames[VP56_FRAME_CURRENT];
  359. frame_ref = s->frames[ref_frame];
  360. if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
  361. return 0;
  362. ab = 6*is_alpha;
  363. b_max = 6 - 2*is_alpha;
  364. switch (mb_type) {
  365. case VP56_MB_INTRA:
  366. for (b=0; b<b_max; b++) {
  367. plane = ff_vp56_b2p[b+ab];
  368. s->vp3dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
  369. s->stride[plane], s->block_coeff[b]);
  370. }
  371. break;
  372. case VP56_MB_INTER_NOVEC_PF:
  373. case VP56_MB_INTER_NOVEC_GF:
  374. for (b=0; b<b_max; b++) {
  375. plane = ff_vp56_b2p[b+ab];
  376. off = s->block_offset[b];
  377. s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
  378. frame_ref->data[plane] + off,
  379. s->stride[plane], 8);
  380. s->vp3dsp.idct_add(frame_current->data[plane] + off,
  381. s->stride[plane], s->block_coeff[b]);
  382. }
  383. break;
  384. case VP56_MB_INTER_DELTA_PF:
  385. case VP56_MB_INTER_V1_PF:
  386. case VP56_MB_INTER_V2_PF:
  387. case VP56_MB_INTER_DELTA_GF:
  388. case VP56_MB_INTER_4V:
  389. case VP56_MB_INTER_V1_GF:
  390. case VP56_MB_INTER_V2_GF:
  391. for (b=0; b<b_max; b++) {
  392. int x_off = b==1 || b==3 ? 8 : 0;
  393. int y_off = b==2 || b==3 ? 8 : 0;
  394. plane = ff_vp56_b2p[b+ab];
  395. vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
  396. 16*col+x_off, 16*row+y_off);
  397. s->vp3dsp.idct_add(frame_current->data[plane] + s->block_offset[b],
  398. s->stride[plane], s->block_coeff[b]);
  399. }
  400. break;
  401. }
  402. if (is_alpha) {
  403. s->block_coeff[4][0] = 0;
  404. s->block_coeff[5][0] = 0;
  405. }
  406. return 0;
  407. }
  408. static int vp56_conceal_mb(VP56Context *s, int row, int col, int is_alpha)
  409. {
  410. AVFrame *frame_current, *frame_ref;
  411. VP56mb mb_type;
  412. VP56Frame ref_frame;
  413. int b, ab, b_max, plane, off;
  414. if (s->frames[VP56_FRAME_CURRENT]->key_frame)
  415. mb_type = VP56_MB_INTRA;
  416. else
  417. mb_type = vp56_conceal_mv(s, row, col);
  418. ref_frame = ff_vp56_reference_frame[mb_type];
  419. frame_current = s->frames[VP56_FRAME_CURRENT];
  420. frame_ref = s->frames[ref_frame];
  421. if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
  422. return 0;
  423. ab = 6*is_alpha;
  424. b_max = 6 - 2*is_alpha;
  425. switch (mb_type) {
  426. case VP56_MB_INTRA:
  427. for (b=0; b<b_max; b++) {
  428. plane = ff_vp56_b2p[b+ab];
  429. s->vp3dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
  430. s->stride[plane], s->block_coeff[b]);
  431. }
  432. break;
  433. case VP56_MB_INTER_NOVEC_PF:
  434. case VP56_MB_INTER_NOVEC_GF:
  435. for (b=0; b<b_max; b++) {
  436. plane = ff_vp56_b2p[b+ab];
  437. off = s->block_offset[b];
  438. s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
  439. frame_ref->data[plane] + off,
  440. s->stride[plane], 8);
  441. s->vp3dsp.idct_add(frame_current->data[plane] + off,
  442. s->stride[plane], s->block_coeff[b]);
  443. }
  444. break;
  445. }
  446. if (is_alpha) {
  447. s->block_coeff[4][0] = 0;
  448. s->block_coeff[5][0] = 0;
  449. }
  450. return 0;
  451. }
  452. static int vp56_size_changed(VP56Context *s)
  453. {
  454. AVCodecContext *avctx = s->avctx;
  455. int stride = s->frames[VP56_FRAME_CURRENT]->linesize[0];
  456. int i;
  457. s->plane_width[0] = s->plane_width[3] = avctx->coded_width;
  458. s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
  459. s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
  460. s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
  461. for (i=0; i<4; i++)
  462. s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT]->linesize[i];
  463. s->mb_width = (avctx->coded_width +15) / 16;
  464. s->mb_height = (avctx->coded_height+15) / 16;
  465. if (s->mb_width > 1000 || s->mb_height > 1000) {
  466. ff_set_dimensions(avctx, 0, 0);
  467. av_log(avctx, AV_LOG_ERROR, "picture too big\n");
  468. return AVERROR_INVALIDDATA;
  469. }
  470. av_reallocp_array(&s->above_blocks, 4*s->mb_width+6,
  471. sizeof(*s->above_blocks));
  472. av_reallocp_array(&s->macroblocks, s->mb_width*s->mb_height,
  473. sizeof(*s->macroblocks));
  474. av_free(s->edge_emu_buffer_alloc);
  475. s->edge_emu_buffer_alloc = av_malloc(16*stride);
  476. s->edge_emu_buffer = s->edge_emu_buffer_alloc;
  477. if (!s->above_blocks || !s->macroblocks || !s->edge_emu_buffer_alloc)
  478. return AVERROR(ENOMEM);
  479. if (s->flip < 0)
  480. s->edge_emu_buffer += 15 * stride;
  481. if (s->alpha_context)
  482. return vp56_size_changed(s->alpha_context);
  483. return 0;
  484. }
  485. static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *, int, int);
  486. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  487. AVPacket *avpkt)
  488. {
  489. const uint8_t *buf = avpkt->data;
  490. VP56Context *s = avctx->priv_data;
  491. AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
  492. int remaining_buf_size = avpkt->size;
  493. int av_uninit(alpha_offset);
  494. int i, res;
  495. int ret;
  496. if (s->has_alpha) {
  497. if (remaining_buf_size < 3)
  498. return AVERROR_INVALIDDATA;
  499. alpha_offset = bytestream_get_be24(&buf);
  500. remaining_buf_size -= 3;
  501. if (remaining_buf_size < alpha_offset)
  502. return AVERROR_INVALIDDATA;
  503. }
  504. res = s->parse_header(s, buf, remaining_buf_size);
  505. if (res < 0)
  506. return res;
  507. if (res == VP56_SIZE_CHANGE) {
  508. for (i = 0; i < 4; i++) {
  509. av_frame_unref(s->frames[i]);
  510. if (s->alpha_context)
  511. av_frame_unref(s->alpha_context->frames[i]);
  512. }
  513. }
  514. ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF);
  515. if (ret < 0)
  516. return ret;
  517. if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
  518. av_frame_unref(s->alpha_context->frames[VP56_FRAME_CURRENT]);
  519. if ((ret = av_frame_ref(s->alpha_context->frames[VP56_FRAME_CURRENT], p)) < 0) {
  520. av_frame_unref(p);
  521. return ret;
  522. }
  523. }
  524. if (res == VP56_SIZE_CHANGE) {
  525. if (vp56_size_changed(s)) {
  526. av_frame_unref(p);
  527. return AVERROR_INVALIDDATA;
  528. }
  529. }
  530. if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
  531. int bak_w = avctx->width;
  532. int bak_h = avctx->height;
  533. int bak_cw = avctx->coded_width;
  534. int bak_ch = avctx->coded_height;
  535. buf += alpha_offset;
  536. remaining_buf_size -= alpha_offset;
  537. res = s->alpha_context->parse_header(s->alpha_context, buf, remaining_buf_size);
  538. if (res != 0) {
  539. if(res==VP56_SIZE_CHANGE) {
  540. av_log(avctx, AV_LOG_ERROR, "Alpha reconfiguration\n");
  541. avctx->width = bak_w;
  542. avctx->height = bak_h;
  543. avctx->coded_width = bak_cw;
  544. avctx->coded_height = bak_ch;
  545. }
  546. av_frame_unref(p);
  547. return AVERROR_INVALIDDATA;
  548. }
  549. }
  550. avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) + 1);
  551. if ((res = av_frame_ref(data, p)) < 0)
  552. return res;
  553. *got_frame = 1;
  554. return avpkt->size;
  555. }
  556. static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *data,
  557. int jobnr, int threadnr)
  558. {
  559. VP56Context *s0 = avctx->priv_data;
  560. int is_alpha = (jobnr == 1);
  561. VP56Context *s = is_alpha ? s0->alpha_context : s0;
  562. AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
  563. int mb_row, mb_col, mb_row_flip, mb_offset = 0;
  564. int block, y, uv;
  565. ptrdiff_t stride_y, stride_uv;
  566. int res;
  567. int damaged = 0;
  568. if (p->key_frame) {
  569. p->pict_type = AV_PICTURE_TYPE_I;
  570. s->default_models_init(s);
  571. for (block=0; block<s->mb_height*s->mb_width; block++)
  572. s->macroblocks[block].type = VP56_MB_INTRA;
  573. } else {
  574. p->pict_type = AV_PICTURE_TYPE_P;
  575. vp56_parse_mb_type_models(s);
  576. s->parse_vector_models(s);
  577. s->mb_type = VP56_MB_INTER_NOVEC_PF;
  578. }
  579. if (s->parse_coeff_models(s))
  580. goto next;
  581. memset(s->prev_dc, 0, sizeof(s->prev_dc));
  582. s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
  583. s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
  584. for (block=0; block < 4*s->mb_width+6; block++) {
  585. s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
  586. s->above_blocks[block].dc_coeff = 0;
  587. s->above_blocks[block].not_null_dc = 0;
  588. }
  589. s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
  590. s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
  591. stride_y = p->linesize[0];
  592. stride_uv = p->linesize[1];
  593. if (s->flip < 0)
  594. mb_offset = 7;
  595. /* main macroblocks loop */
  596. for (mb_row=0; mb_row<s->mb_height; mb_row++) {
  597. if (s->flip < 0)
  598. mb_row_flip = s->mb_height - mb_row - 1;
  599. else
  600. mb_row_flip = mb_row;
  601. for (block=0; block<4; block++) {
  602. s->left_block[block].ref_frame = VP56_FRAME_NONE;
  603. s->left_block[block].dc_coeff = 0;
  604. s->left_block[block].not_null_dc = 0;
  605. }
  606. memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
  607. memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
  608. s->above_block_idx[0] = 1;
  609. s->above_block_idx[1] = 2;
  610. s->above_block_idx[2] = 1;
  611. s->above_block_idx[3] = 2;
  612. s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
  613. s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
  614. s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
  615. s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
  616. s->block_offset[1] = s->block_offset[0] + 8;
  617. s->block_offset[3] = s->block_offset[2] + 8;
  618. s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
  619. s->block_offset[5] = s->block_offset[4];
  620. for (mb_col=0; mb_col<s->mb_width; mb_col++) {
  621. if (!damaged) {
  622. int ret = vp56_decode_mb(s, mb_row, mb_col, is_alpha);
  623. if (ret < 0)
  624. damaged = 1;
  625. }
  626. if (damaged)
  627. vp56_conceal_mb(s, mb_row, mb_col, is_alpha);
  628. for (y=0; y<4; y++) {
  629. s->above_block_idx[y] += 2;
  630. s->block_offset[y] += 16;
  631. }
  632. for (uv=4; uv<6; uv++) {
  633. s->above_block_idx[uv] += 1;
  634. s->block_offset[uv] += 8;
  635. }
  636. }
  637. }
  638. next:
  639. if (p->key_frame || s->golden_frame) {
  640. av_frame_unref(s->frames[VP56_FRAME_GOLDEN]);
  641. if ((res = av_frame_ref(s->frames[VP56_FRAME_GOLDEN], p)) < 0)
  642. return res;
  643. }
  644. av_frame_unref(s->frames[VP56_FRAME_PREVIOUS]);
  645. FFSWAP(AVFrame *, s->frames[VP56_FRAME_CURRENT],
  646. s->frames[VP56_FRAME_PREVIOUS]);
  647. return 0;
  648. }
  649. av_cold int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
  650. {
  651. VP56Context *s = avctx->priv_data;
  652. return ff_vp56_init_context(avctx, s, flip, has_alpha);
  653. }
  654. av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
  655. int flip, int has_alpha)
  656. {
  657. int i;
  658. s->avctx = avctx;
  659. avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
  660. if (avctx->skip_alpha) avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  661. ff_h264chroma_init(&s->h264chroma, 8);
  662. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  663. ff_videodsp_init(&s->vdsp, 8);
  664. ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
  665. ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
  666. for (i = 0; i < 64; i++) {
  667. #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
  668. s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
  669. #undef TRANSPOSE
  670. }
  671. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
  672. s->frames[i] = av_frame_alloc();
  673. if (!s->frames[i]) {
  674. ff_vp56_free(avctx);
  675. return AVERROR(ENOMEM);
  676. }
  677. }
  678. s->edge_emu_buffer_alloc = NULL;
  679. s->above_blocks = NULL;
  680. s->macroblocks = NULL;
  681. s->quantizer = -1;
  682. s->deblock_filtering = 1;
  683. s->golden_frame = 0;
  684. s->filter = NULL;
  685. s->has_alpha = has_alpha;
  686. s->modelp = &s->model;
  687. if (flip) {
  688. s->flip = -1;
  689. s->frbi = 2;
  690. s->srbi = 0;
  691. } else {
  692. s->flip = 1;
  693. s->frbi = 0;
  694. s->srbi = 2;
  695. }
  696. return 0;
  697. }
  698. av_cold int ff_vp56_free(AVCodecContext *avctx)
  699. {
  700. VP56Context *s = avctx->priv_data;
  701. return ff_vp56_free_context(s);
  702. }
  703. av_cold int ff_vp56_free_context(VP56Context *s)
  704. {
  705. int i;
  706. av_freep(&s->above_blocks);
  707. av_freep(&s->macroblocks);
  708. av_freep(&s->edge_emu_buffer_alloc);
  709. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
  710. av_frame_free(&s->frames[i]);
  711. return 0;
  712. }