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.

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