vp56.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /**
  2. * @file libavcodec/vp56.c
  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. }
  33. static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
  34. VP56Frame ref_frame)
  35. {
  36. int nb_pred = 0;
  37. VP56mv vect[2] = {{0,0}, {0,0}};
  38. int pos, offset;
  39. VP56mv mvp;
  40. for (pos=0; pos<12; pos++) {
  41. mvp.x = col + vp56_candidate_predictor_pos[pos][0];
  42. mvp.y = row + vp56_candidate_predictor_pos[pos][1];
  43. if (mvp.x < 0 || mvp.x >= s->mb_width ||
  44. mvp.y < 0 || mvp.y >= s->mb_height)
  45. continue;
  46. offset = mvp.x + s->mb_width*mvp.y;
  47. if (vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
  48. continue;
  49. if ((s->macroblocks[offset].mv.x == vect[0].x &&
  50. s->macroblocks[offset].mv.y == vect[0].y) ||
  51. (s->macroblocks[offset].mv.x == 0 &&
  52. s->macroblocks[offset].mv.y == 0))
  53. continue;
  54. vect[nb_pred++] = s->macroblocks[offset].mv;
  55. if (nb_pred > 1) {
  56. nb_pred = -1;
  57. break;
  58. }
  59. s->vector_candidate_pos = pos;
  60. }
  61. s->vector_candidate[0] = vect[0];
  62. s->vector_candidate[1] = vect[1];
  63. return nb_pred+1;
  64. }
  65. static void vp56_parse_mb_type_models(VP56Context *s)
  66. {
  67. VP56RangeCoder *c = &s->c;
  68. VP56Model *model = s->modelp;
  69. int i, ctx, type;
  70. for (ctx=0; ctx<3; ctx++) {
  71. if (vp56_rac_get_prob(c, 174)) {
  72. int idx = vp56_rac_gets(c, 4);
  73. memcpy(model->mb_types_stats[ctx],
  74. vp56_pre_def_mb_type_stats[idx][ctx],
  75. sizeof(model->mb_types_stats[ctx]));
  76. }
  77. if (vp56_rac_get_prob(c, 254)) {
  78. for (type=0; type<10; type++) {
  79. for(i=0; i<2; i++) {
  80. if (vp56_rac_get_prob(c, 205)) {
  81. int delta, sign = vp56_rac_get(c);
  82. delta = vp56_rac_get_tree(c, vp56_pmbtm_tree,
  83. vp56_mb_type_model_model);
  84. if (!delta)
  85. delta = 4 * vp56_rac_gets(c, 7);
  86. model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. /* compute MB type probability tables based on previous MB type */
  93. for (ctx=0; ctx<3; ctx++) {
  94. int p[10];
  95. for (type=0; type<10; type++)
  96. p[type] = 100 * model->mb_types_stats[ctx][type][1];
  97. for (type=0; type<10; type++) {
  98. int p02, p34, p0234, p17, p56, p89, p5689, p156789;
  99. /* conservative MB type probability */
  100. 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]);
  101. p[type] = 0; /* same MB type => weight is null */
  102. /* binary tree parsing probabilities */
  103. p02 = p[0] + p[2];
  104. p34 = p[3] + p[4];
  105. p0234 = p02 + p34;
  106. p17 = p[1] + p[7];
  107. p56 = p[5] + p[6];
  108. p89 = p[8] + p[9];
  109. p5689 = p56 + p89;
  110. p156789 = p17 + p5689;
  111. model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
  112. model->mb_type[ctx][type][2] = 1 + 255 * p02 / (1+p0234);
  113. model->mb_type[ctx][type][3] = 1 + 255 * p17 / (1+p156789);
  114. model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
  115. model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
  116. model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
  117. model->mb_type[ctx][type][7] = 1 + 255 * p56 / (1+p5689);
  118. model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
  119. model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
  120. /* restore initial value */
  121. p[type] = 100 * model->mb_types_stats[ctx][type][1];
  122. }
  123. }
  124. }
  125. static VP56mb vp56_parse_mb_type(VP56Context *s,
  126. VP56mb prev_type, int ctx)
  127. {
  128. uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
  129. VP56RangeCoder *c = &s->c;
  130. if (vp56_rac_get_prob(c, mb_type_model[0]))
  131. return prev_type;
  132. else
  133. return vp56_rac_get_tree(c, vp56_pmbt_tree, mb_type_model);
  134. }
  135. static void vp56_decode_4mv(VP56Context *s, int row, int col)
  136. {
  137. VP56mv mv = {0,0};
  138. int type[4];
  139. int b;
  140. /* parse each block type */
  141. for (b=0; b<4; b++) {
  142. type[b] = vp56_rac_gets(&s->c, 2);
  143. if (type[b])
  144. type[b]++; /* only returns 0, 2, 3 or 4 (all INTER_PF) */
  145. }
  146. /* get vectors */
  147. for (b=0; b<4; b++) {
  148. switch (type[b]) {
  149. case VP56_MB_INTER_NOVEC_PF:
  150. s->mv[b] = (VP56mv) {0,0};
  151. break;
  152. case VP56_MB_INTER_DELTA_PF:
  153. s->parse_vector_adjustment(s, &s->mv[b]);
  154. break;
  155. case VP56_MB_INTER_V1_PF:
  156. s->mv[b] = s->vector_candidate[0];
  157. break;
  158. case VP56_MB_INTER_V2_PF:
  159. s->mv[b] = s->vector_candidate[1];
  160. break;
  161. }
  162. mv.x += s->mv[b].x;
  163. mv.y += s->mv[b].y;
  164. }
  165. /* this is the one selected for the whole MB for prediction */
  166. s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
  167. /* chroma vectors are average luma vectors */
  168. if (s->avctx->codec->id == CODEC_ID_VP5) {
  169. s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
  170. s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
  171. } else {
  172. s->mv[4] = s->mv[5] = (VP56mv) {mv.x/4, mv.y/4};
  173. }
  174. }
  175. static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
  176. {
  177. VP56mv *mv, vect = {0,0};
  178. int ctx, b;
  179. ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
  180. s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
  181. s->macroblocks[row * s->mb_width + col].type = s->mb_type;
  182. switch (s->mb_type) {
  183. case VP56_MB_INTER_V1_PF:
  184. mv = &s->vector_candidate[0];
  185. break;
  186. case VP56_MB_INTER_V2_PF:
  187. mv = &s->vector_candidate[1];
  188. break;
  189. case VP56_MB_INTER_V1_GF:
  190. vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
  191. mv = &s->vector_candidate[0];
  192. break;
  193. case VP56_MB_INTER_V2_GF:
  194. vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
  195. mv = &s->vector_candidate[1];
  196. break;
  197. case VP56_MB_INTER_DELTA_PF:
  198. s->parse_vector_adjustment(s, &vect);
  199. mv = &vect;
  200. break;
  201. case VP56_MB_INTER_DELTA_GF:
  202. vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
  203. s->parse_vector_adjustment(s, &vect);
  204. mv = &vect;
  205. break;
  206. case VP56_MB_INTER_4V:
  207. vp56_decode_4mv(s, row, col);
  208. return s->mb_type;
  209. default:
  210. mv = &vect;
  211. break;
  212. }
  213. s->macroblocks[row*s->mb_width + col].mv = *mv;
  214. /* same vector for all blocks */
  215. for (b=0; b<6; b++)
  216. s->mv[b] = *mv;
  217. return s->mb_type;
  218. }
  219. static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
  220. {
  221. int idx = s->scantable.permutated[0];
  222. int b;
  223. for (b=0; b<6; b++) {
  224. VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
  225. VP56RefDc *lb = &s->left_block[vp56_b6to4[b]];
  226. int count = 0;
  227. int dc = 0;
  228. int i;
  229. if (ref_frame == lb->ref_frame) {
  230. dc += lb->dc_coeff;
  231. count++;
  232. }
  233. if (ref_frame == ab->ref_frame) {
  234. dc += ab->dc_coeff;
  235. count++;
  236. }
  237. if (s->avctx->codec->id == CODEC_ID_VP5)
  238. for (i=0; i<2; i++)
  239. if (count < 2 && ref_frame == ab[-1+2*i].ref_frame) {
  240. dc += ab[-1+2*i].dc_coeff;
  241. count++;
  242. }
  243. if (count == 0)
  244. dc = s->prev_dc[vp56_b2p[b]][ref_frame];
  245. else if (count == 2)
  246. dc /= 2;
  247. s->block_coeff[b][idx] += dc;
  248. s->prev_dc[vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
  249. ab->dc_coeff = s->block_coeff[b][idx];
  250. ab->ref_frame = ref_frame;
  251. lb->dc_coeff = s->block_coeff[b][idx];
  252. lb->ref_frame = ref_frame;
  253. s->block_coeff[b][idx] *= s->dequant_dc;
  254. }
  255. }
  256. static void vp56_edge_filter(VP56Context *s, uint8_t *yuv,
  257. int pix_inc, int line_inc, int t)
  258. {
  259. int pix2_inc = 2 * pix_inc;
  260. int i, v;
  261. for (i=0; i<12; i++) {
  262. v = (yuv[-pix2_inc] + 3*(yuv[0]-yuv[-pix_inc]) - yuv[pix_inc] + 4) >>3;
  263. v = s->adjust(v, t);
  264. yuv[-pix_inc] = av_clip_uint8(yuv[-pix_inc] + v);
  265. yuv[0] = av_clip_uint8(yuv[0] - v);
  266. yuv += line_inc;
  267. }
  268. }
  269. static void vp56_deblock_filter(VP56Context *s, uint8_t *yuv,
  270. int stride, int dx, int dy)
  271. {
  272. int t = vp56_filter_threshold[s->quantizer];
  273. if (dx) vp56_edge_filter(s, yuv + 10-dx , 1, stride, t);
  274. if (dy) vp56_edge_filter(s, yuv + stride*(10-dy), stride, 1, t);
  275. }
  276. static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
  277. int stride, int x, int y)
  278. {
  279. uint8_t *dst=s->framep[VP56_FRAME_CURRENT]->data[plane]+s->block_offset[b];
  280. uint8_t *src_block;
  281. int src_offset;
  282. int overlap_offset = 0;
  283. int mask = s->vp56_coord_div[b] - 1;
  284. int deblock_filtering = s->deblock_filtering;
  285. int dx;
  286. int dy;
  287. if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  288. (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
  289. && !s->framep[VP56_FRAME_CURRENT]->key_frame))
  290. deblock_filtering = 0;
  291. dx = s->mv[b].x / s->vp56_coord_div[b];
  292. dy = s->mv[b].y / s->vp56_coord_div[b];
  293. if (b >= 4) {
  294. x /= 2;
  295. y /= 2;
  296. }
  297. x += dx - 2;
  298. y += dy - 2;
  299. if (x<0 || x+12>=s->plane_width[plane] ||
  300. y<0 || y+12>=s->plane_height[plane]) {
  301. ff_emulated_edge_mc(s->edge_emu_buffer,
  302. src + s->block_offset[b] + (dy-2)*stride + (dx-2),
  303. stride, 12, 12, x, y,
  304. s->plane_width[plane],
  305. s->plane_height[plane]);
  306. src_block = s->edge_emu_buffer;
  307. src_offset = 2 + 2*stride;
  308. } else if (deblock_filtering) {
  309. /* only need a 12x12 block, but there is no such dsp function, */
  310. /* so copy a 16x12 block */
  311. s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer,
  312. src + s->block_offset[b] + (dy-2)*stride + (dx-2),
  313. stride, 12);
  314. src_block = s->edge_emu_buffer;
  315. src_offset = 2 + 2*stride;
  316. } else {
  317. src_block = src;
  318. src_offset = s->block_offset[b] + dy*stride + dx;
  319. }
  320. if (deblock_filtering)
  321. vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
  322. if (s->mv[b].x & mask)
  323. overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
  324. if (s->mv[b].y & mask)
  325. overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
  326. if (overlap_offset) {
  327. if (s->filter)
  328. s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
  329. stride, s->mv[b], mask, s->filter_selection, b<4);
  330. else
  331. s->dsp.put_no_rnd_pixels_l2[1](dst, src_block+src_offset,
  332. src_block+src_offset+overlap_offset,
  333. stride, 8);
  334. } else {
  335. s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
  336. }
  337. }
  338. static void vp56_decode_mb(VP56Context *s, int row, int col, int is_alpha)
  339. {
  340. AVFrame *frame_current, *frame_ref;
  341. VP56mb mb_type;
  342. VP56Frame ref_frame;
  343. int b, ab, b_max, plane, off;
  344. if (s->framep[VP56_FRAME_CURRENT]->key_frame)
  345. mb_type = VP56_MB_INTRA;
  346. else
  347. mb_type = vp56_decode_mv(s, row, col);
  348. ref_frame = vp56_reference_frame[mb_type];
  349. s->dsp.clear_blocks(*s->block_coeff);
  350. s->parse_coeff(s);
  351. vp56_add_predictors_dc(s, ref_frame);
  352. frame_current = s->framep[VP56_FRAME_CURRENT];
  353. frame_ref = s->framep[ref_frame];
  354. ab = 6*is_alpha;
  355. b_max = 6 - 2*is_alpha;
  356. switch (mb_type) {
  357. case VP56_MB_INTRA:
  358. for (b=0; b<b_max; b++) {
  359. plane = vp56_b2p[b+ab];
  360. s->dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
  361. s->stride[plane], s->block_coeff[b]);
  362. }
  363. break;
  364. case VP56_MB_INTER_NOVEC_PF:
  365. case VP56_MB_INTER_NOVEC_GF:
  366. for (b=0; b<b_max; b++) {
  367. plane = vp56_b2p[b+ab];
  368. off = s->block_offset[b];
  369. s->dsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
  370. frame_ref->data[plane] + off,
  371. s->stride[plane], 8);
  372. s->dsp.idct_add(frame_current->data[plane] + off,
  373. s->stride[plane], s->block_coeff[b]);
  374. }
  375. break;
  376. case VP56_MB_INTER_DELTA_PF:
  377. case VP56_MB_INTER_V1_PF:
  378. case VP56_MB_INTER_V2_PF:
  379. case VP56_MB_INTER_DELTA_GF:
  380. case VP56_MB_INTER_4V:
  381. case VP56_MB_INTER_V1_GF:
  382. case VP56_MB_INTER_V2_GF:
  383. for (b=0; b<b_max; b++) {
  384. int x_off = b==1 || b==3 ? 8 : 0;
  385. int y_off = b==2 || b==3 ? 8 : 0;
  386. plane = vp56_b2p[b+ab];
  387. vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
  388. 16*col+x_off, 16*row+y_off);
  389. s->dsp.idct_add(frame_current->data[plane] + s->block_offset[b],
  390. s->stride[plane], s->block_coeff[b]);
  391. }
  392. break;
  393. }
  394. }
  395. static int vp56_size_changed(AVCodecContext *avctx)
  396. {
  397. VP56Context *s = avctx->priv_data;
  398. int stride = s->framep[VP56_FRAME_CURRENT]->linesize[0];
  399. int i;
  400. s->plane_width[0] = s->plane_width[3] = avctx->coded_width;
  401. s->plane_width[1] = s->plane_width[2] = avctx->coded_width/2;
  402. s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
  403. s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
  404. for (i=0; i<4; i++)
  405. s->stride[i] = s->flip * s->framep[VP56_FRAME_CURRENT]->linesize[i];
  406. s->mb_width = (avctx->coded_width +15) / 16;
  407. s->mb_height = (avctx->coded_height+15) / 16;
  408. if (s->mb_width > 1000 || s->mb_height > 1000) {
  409. av_log(avctx, AV_LOG_ERROR, "picture too big\n");
  410. return -1;
  411. }
  412. s->above_blocks = av_realloc(s->above_blocks,
  413. (4*s->mb_width+6) * sizeof(*s->above_blocks));
  414. s->macroblocks = av_realloc(s->macroblocks,
  415. s->mb_width*s->mb_height*sizeof(*s->macroblocks));
  416. av_free(s->edge_emu_buffer_alloc);
  417. s->edge_emu_buffer_alloc = av_malloc(16*stride);
  418. s->edge_emu_buffer = s->edge_emu_buffer_alloc;
  419. if (s->flip < 0)
  420. s->edge_emu_buffer += 15 * stride;
  421. return 0;
  422. }
  423. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  424. const uint8_t *buf, int buf_size)
  425. {
  426. VP56Context *s = avctx->priv_data;
  427. AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
  428. int remaining_buf_size = buf_size;
  429. int is_alpha, av_uninit(alpha_offset);
  430. if (s->has_alpha) {
  431. alpha_offset = bytestream_get_be24(&buf);
  432. remaining_buf_size -= 3;
  433. }
  434. for (is_alpha=0; is_alpha < 1+s->has_alpha; is_alpha++) {
  435. int mb_row, mb_col, mb_row_flip, mb_offset = 0;
  436. int block, y, uv, stride_y, stride_uv;
  437. int golden_frame = 0;
  438. int res;
  439. s->modelp = &s->models[is_alpha];
  440. res = s->parse_header(s, buf, remaining_buf_size, &golden_frame);
  441. if (!res) {
  442. int i;
  443. for (i = 0; i < 4; i++) {
  444. if (s->frames[i].data[0])
  445. avctx->release_buffer(avctx, &s->frames[i]);
  446. }
  447. return res;
  448. }
  449. if (res == 2) {
  450. int i;
  451. for (i = 0; i < 4; i++) {
  452. if (s->frames[i].data[0])
  453. avctx->release_buffer(avctx, &s->frames[i]);
  454. }
  455. if (is_alpha)
  456. return -1;
  457. }
  458. if (!is_alpha) {
  459. p->reference = 1;
  460. if (avctx->get_buffer(avctx, p) < 0) {
  461. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  462. return -1;
  463. }
  464. if (res == 2)
  465. if (vp56_size_changed(avctx)) {
  466. avctx->release_buffer(avctx, p);
  467. return -1;
  468. }
  469. }
  470. if (p->key_frame) {
  471. p->pict_type = FF_I_TYPE;
  472. s->default_models_init(s);
  473. for (block=0; block<s->mb_height*s->mb_width; block++)
  474. s->macroblocks[block].type = VP56_MB_INTRA;
  475. } else {
  476. p->pict_type = FF_P_TYPE;
  477. vp56_parse_mb_type_models(s);
  478. s->parse_vector_models(s);
  479. s->mb_type = VP56_MB_INTER_NOVEC_PF;
  480. }
  481. s->parse_coeff_models(s);
  482. memset(s->prev_dc, 0, sizeof(s->prev_dc));
  483. s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
  484. s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
  485. for (block=0; block < 4*s->mb_width+6; block++) {
  486. s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
  487. s->above_blocks[block].dc_coeff = 0;
  488. s->above_blocks[block].not_null_dc = 0;
  489. }
  490. s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
  491. s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
  492. stride_y = p->linesize[0];
  493. stride_uv = p->linesize[1];
  494. if (s->flip < 0)
  495. mb_offset = 7;
  496. /* main macroblocks loop */
  497. for (mb_row=0; mb_row<s->mb_height; mb_row++) {
  498. if (s->flip < 0)
  499. mb_row_flip = s->mb_height - mb_row - 1;
  500. else
  501. mb_row_flip = mb_row;
  502. for (block=0; block<4; block++) {
  503. s->left_block[block].ref_frame = VP56_FRAME_NONE;
  504. s->left_block[block].dc_coeff = 0;
  505. s->left_block[block].not_null_dc = 0;
  506. }
  507. memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
  508. memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
  509. s->above_block_idx[0] = 1;
  510. s->above_block_idx[1] = 2;
  511. s->above_block_idx[2] = 1;
  512. s->above_block_idx[3] = 2;
  513. s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
  514. s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
  515. s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
  516. s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
  517. s->block_offset[1] = s->block_offset[0] + 8;
  518. s->block_offset[3] = s->block_offset[2] + 8;
  519. s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
  520. s->block_offset[5] = s->block_offset[4];
  521. for (mb_col=0; mb_col<s->mb_width; mb_col++) {
  522. vp56_decode_mb(s, mb_row, mb_col, is_alpha);
  523. for (y=0; y<4; y++) {
  524. s->above_block_idx[y] += 2;
  525. s->block_offset[y] += 16;
  526. }
  527. for (uv=4; uv<6; uv++) {
  528. s->above_block_idx[uv] += 1;
  529. s->block_offset[uv] += 8;
  530. }
  531. }
  532. }
  533. if (p->key_frame || golden_frame) {
  534. if (s->framep[VP56_FRAME_GOLDEN]->data[0] &&
  535. s->framep[VP56_FRAME_GOLDEN] != s->framep[VP56_FRAME_GOLDEN2])
  536. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
  537. s->framep[VP56_FRAME_GOLDEN] = p;
  538. }
  539. if (s->has_alpha) {
  540. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN],
  541. s->framep[VP56_FRAME_GOLDEN2]);
  542. buf += alpha_offset;
  543. remaining_buf_size -= alpha_offset;
  544. }
  545. }
  546. if (s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN] ||
  547. s->framep[VP56_FRAME_PREVIOUS] == s->framep[VP56_FRAME_GOLDEN2]) {
  548. if (s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN] &&
  549. s->framep[VP56_FRAME_UNUSED] != s->framep[VP56_FRAME_GOLDEN2])
  550. FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
  551. s->framep[VP56_FRAME_UNUSED]);
  552. else
  553. FFSWAP(AVFrame *, s->framep[VP56_FRAME_PREVIOUS],
  554. s->framep[VP56_FRAME_UNUSED2]);
  555. } else if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
  556. avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
  557. FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
  558. s->framep[VP56_FRAME_PREVIOUS]);
  559. *(AVFrame*)data = *p;
  560. *data_size = sizeof(AVFrame);
  561. return buf_size;
  562. }
  563. av_cold void vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
  564. {
  565. VP56Context *s = avctx->priv_data;
  566. int i;
  567. s->avctx = avctx;
  568. avctx->pix_fmt = has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
  569. if (avctx->idct_algo == FF_IDCT_AUTO)
  570. avctx->idct_algo = FF_IDCT_VP3;
  571. dsputil_init(&s->dsp, avctx);
  572. ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
  573. for (i=0; i<4; i++)
  574. s->framep[i] = &s->frames[i];
  575. s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
  576. s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
  577. s->edge_emu_buffer_alloc = NULL;
  578. s->above_blocks = NULL;
  579. s->macroblocks = NULL;
  580. s->quantizer = -1;
  581. s->deblock_filtering = 1;
  582. s->filter = NULL;
  583. s->has_alpha = has_alpha;
  584. if (flip) {
  585. s->flip = -1;
  586. s->frbi = 2;
  587. s->srbi = 0;
  588. } else {
  589. s->flip = 1;
  590. s->frbi = 0;
  591. s->srbi = 2;
  592. }
  593. }
  594. av_cold int vp56_free(AVCodecContext *avctx)
  595. {
  596. VP56Context *s = avctx->priv_data;
  597. int pt;
  598. av_free(s->above_blocks);
  599. av_free(s->macroblocks);
  600. av_free(s->edge_emu_buffer_alloc);
  601. if (s->framep[VP56_FRAME_GOLDEN]->data[0])
  602. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN]);
  603. if (s->framep[VP56_FRAME_GOLDEN2]->data[0])
  604. avctx->release_buffer(avctx, s->framep[VP56_FRAME_GOLDEN2]);
  605. if (s->framep[VP56_FRAME_PREVIOUS]->data[0])
  606. avctx->release_buffer(avctx, s->framep[VP56_FRAME_PREVIOUS]);
  607. for (pt=0; pt < 2; pt++) {
  608. int ct, cg;
  609. free_vlc(&s->dccv_vlc[pt]);
  610. free_vlc(&s->runv_vlc[pt]);
  611. for (ct=0; ct<3; ct++)
  612. for (cg = 0; cg < 6; cg++)
  613. free_vlc(&s->ract_vlc[pt][ct][cg]);
  614. }
  615. return 0;
  616. }