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.

1473 lines
51KB

  1. /**
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  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 "vp56.h"
  25. #include "vp8data.h"
  26. #include "vp8dsp.h"
  27. #include "h264pred.h"
  28. #include "rectangle.h"
  29. typedef struct {
  30. uint8_t segment;
  31. uint8_t skip;
  32. // todo: make it possible to check for at least (i4x4 or split_mv)
  33. // in one op. are others needed?
  34. uint8_t mode;
  35. uint8_t ref_frame;
  36. uint8_t partitioning;
  37. VP56mv mv;
  38. VP56mv bmv[16];
  39. } VP8Macroblock;
  40. typedef struct {
  41. AVCodecContext *avctx;
  42. DSPContext dsp;
  43. VP8DSPContext vp8dsp;
  44. H264PredContext hpc;
  45. vp8_mc_func put_pixels_tab[3][3][3];
  46. AVFrame frames[4];
  47. AVFrame *framep[4];
  48. uint8_t *edge_emu_buffer;
  49. VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
  50. int profile;
  51. int mb_width; /* number of horizontal MB */
  52. int mb_height; /* number of vertical MB */
  53. int linesize;
  54. int uvlinesize;
  55. int keyframe;
  56. int invisible;
  57. int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
  58. int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
  59. int update_altref;
  60. /**
  61. * If this flag is not set, all the probability updates
  62. * are discarded after this frame is decoded.
  63. */
  64. int update_probabilities;
  65. /**
  66. * All coefficients are contained in separate arith coding contexts.
  67. * There can be 1, 2, 4, or 8 of these after the header context.
  68. */
  69. int num_coeff_partitions;
  70. VP56RangeCoder coeff_partition[8];
  71. VP8Macroblock *macroblocks;
  72. VP8Macroblock *macroblocks_base;
  73. int mb_stride;
  74. uint8_t *intra4x4_pred_mode;
  75. uint8_t *intra4x4_pred_mode_base;
  76. int b4_stride;
  77. /**
  78. * For coeff decode, we need to know whether the above block had non-zero
  79. * coefficients. This means for each macroblock, we need data for 4 luma
  80. * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
  81. * per macroblock. We keep the last row in top_nnz.
  82. */
  83. uint8_t (*top_nnz)[9];
  84. DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
  85. /**
  86. * This is the index plus one of the last non-zero coeff
  87. * for each of the blocks in the current macroblock.
  88. * So, 0 -> no coeffs
  89. * 1 -> dc-only (special transform)
  90. * 2+-> full transform
  91. */
  92. DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
  93. DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
  94. int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
  95. int mbskip_enabled;
  96. int sign_bias[4]; ///< one state [0, 1] per ref frame type
  97. /**
  98. * Base parameters for segmentation, i.e. per-macroblock parameters.
  99. * These must be kept unchanged even if segmentation is not used for
  100. * a frame, since the values persist between interframes.
  101. */
  102. struct {
  103. int enabled;
  104. int absolute_vals;
  105. int update_map;
  106. int8_t base_quant[4];
  107. int8_t filter_level[4]; ///< base loop filter level
  108. } segmentation;
  109. /**
  110. * Macroblocks can have one of 4 different quants in a frame when
  111. * segmentation is enabled.
  112. * If segmentation is disabled, only the first segment's values are used.
  113. */
  114. struct {
  115. // [0] - DC qmul [1] - AC qmul
  116. int16_t luma_qmul[2];
  117. int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
  118. int16_t chroma_qmul[2];
  119. } qmat[4];
  120. struct {
  121. int simple;
  122. int level;
  123. int sharpness;
  124. } filter;
  125. struct {
  126. int enabled; ///< whether each mb can have a different strength based on mode/ref
  127. /**
  128. * filter strength adjustment for the following macroblock modes:
  129. * [0] - i4x4
  130. * [1] - zero mv
  131. * [2] - inter modes except for zero or split mv
  132. * [3] - split mv
  133. * i16x16 modes never have any adjustment
  134. */
  135. int8_t mode[4];
  136. /**
  137. * filter strength adjustment for macroblocks that reference:
  138. * [0] - intra / VP56_FRAME_CURRENT
  139. * [1] - VP56_FRAME_PREVIOUS
  140. * [2] - VP56_FRAME_GOLDEN
  141. * [3] - altref / VP56_FRAME_GOLDEN2
  142. */
  143. int8_t ref[4];
  144. } lf_delta;
  145. /**
  146. * These are all of the updatable probabilities for binary decisions.
  147. * They are only implictly reset on keyframes, making it quite likely
  148. * for an interframe to desync if a prior frame's header was corrupt
  149. * or missing outright!
  150. */
  151. struct {
  152. uint8_t segmentid[3];
  153. uint8_t mbskip;
  154. uint8_t intra;
  155. uint8_t last;
  156. uint8_t golden;
  157. uint8_t pred16x16[4];
  158. uint8_t pred8x8c[3];
  159. uint8_t token[4][8][3][NUM_DCT_TOKENS-1];
  160. uint8_t mvc[2][19];
  161. } prob[2];
  162. } VP8Context;
  163. #define RL24(p) (AV_RL16(p) + ((p)[2] << 16))
  164. static void vp8_decode_flush(AVCodecContext *avctx)
  165. {
  166. VP8Context *s = avctx->priv_data;
  167. int i;
  168. for (i = 0; i < 4; i++)
  169. if (s->frames[i].data[0])
  170. avctx->release_buffer(avctx, &s->frames[i]);
  171. memset(s->framep, 0, sizeof(s->framep));
  172. av_freep(&s->macroblocks_base);
  173. av_freep(&s->intra4x4_pred_mode_base);
  174. av_freep(&s->top_nnz);
  175. av_freep(&s->edge_emu_buffer);
  176. s->macroblocks = NULL;
  177. s->intra4x4_pred_mode = NULL;
  178. }
  179. static int update_dimensions(VP8Context *s, int width, int height)
  180. {
  181. int i;
  182. if (avcodec_check_dimensions(s->avctx, width, height))
  183. return AVERROR_INVALIDDATA;
  184. vp8_decode_flush(s->avctx);
  185. avcodec_set_dimensions(s->avctx, width, height);
  186. s->mb_width = (s->avctx->coded_width +15) / 16;
  187. s->mb_height = (s->avctx->coded_height+15) / 16;
  188. // we allocate a border around the top/left of intra4x4 modes
  189. // this is 4 blocks for intra4x4 to keep 4-byte alignment for fill_rectangle
  190. s->mb_stride = s->mb_width+1;
  191. s->b4_stride = 4*s->mb_stride;
  192. s->macroblocks_base = av_mallocz(s->mb_stride*(s->mb_height+1)*sizeof(*s->macroblocks));
  193. s->intra4x4_pred_mode_base = av_mallocz(s->b4_stride*(4*s->mb_height+1));
  194. s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
  195. s->macroblocks = s->macroblocks_base + 1 + s->mb_stride;
  196. s->intra4x4_pred_mode = s->intra4x4_pred_mode_base + 4 + s->b4_stride;
  197. memset(s->intra4x4_pred_mode_base, DC_PRED, s->b4_stride);
  198. for (i = 0; i < 4*s->mb_height; i++)
  199. s->intra4x4_pred_mode[i*s->b4_stride-1] = DC_PRED;
  200. return 0;
  201. }
  202. static void parse_segment_info(VP8Context *s)
  203. {
  204. VP56RangeCoder *c = &s->c;
  205. int i;
  206. s->segmentation.update_map = vp8_rac_get(c);
  207. if (vp8_rac_get(c)) { // update segment feature data
  208. s->segmentation.absolute_vals = vp8_rac_get(c);
  209. for (i = 0; i < 4; i++)
  210. s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
  211. for (i = 0; i < 4; i++)
  212. s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
  213. }
  214. if (s->segmentation.update_map)
  215. for (i = 0; i < 3; i++)
  216. s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  217. }
  218. static void update_lf_deltas(VP8Context *s)
  219. {
  220. VP56RangeCoder *c = &s->c;
  221. int i;
  222. for (i = 0; i < 4; i++)
  223. s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
  224. for (i = 0; i < 4; i++)
  225. s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
  226. }
  227. static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
  228. {
  229. const uint8_t *sizes = buf;
  230. int i;
  231. s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
  232. buf += 3*(s->num_coeff_partitions-1);
  233. buf_size -= 3*(s->num_coeff_partitions-1);
  234. if (buf_size < 0)
  235. return -1;
  236. for (i = 0; i < s->num_coeff_partitions-1; i++) {
  237. int size = RL24(sizes + 3*i);
  238. if (buf_size - size < 0)
  239. return -1;
  240. vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
  241. buf += size;
  242. buf_size -= size;
  243. }
  244. vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
  245. return 0;
  246. }
  247. static void get_quants(VP8Context *s)
  248. {
  249. VP56RangeCoder *c = &s->c;
  250. int i, base_qi;
  251. int yac_qi = vp8_rac_get_uint(c, 7);
  252. int ydc_delta = vp8_rac_get_sint(c, 4);
  253. int y2dc_delta = vp8_rac_get_sint(c, 4);
  254. int y2ac_delta = vp8_rac_get_sint(c, 4);
  255. int uvdc_delta = vp8_rac_get_sint(c, 4);
  256. int uvac_delta = vp8_rac_get_sint(c, 4);
  257. for (i = 0; i < 4; i++) {
  258. if (s->segmentation.enabled) {
  259. base_qi = s->segmentation.base_quant[i];
  260. if (!s->segmentation.absolute_vals)
  261. base_qi += yac_qi;
  262. } else
  263. base_qi = yac_qi;
  264. s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
  265. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi , 0, 127)];
  266. s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
  267. s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
  268. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
  269. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
  270. s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
  271. s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
  272. }
  273. }
  274. /**
  275. * Determine which buffers golden and altref should be updated with after this frame.
  276. * The spec isn't clear here, so I'm going by my understanding of what libvpx does
  277. *
  278. * Intra frames update all 3 references
  279. * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
  280. * If the update (golden|altref) flag is set, it's updated with the current frame
  281. * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
  282. * If the flag is not set, the number read means:
  283. * 0: no update
  284. * 1: VP56_FRAME_PREVIOUS
  285. * 2: update golden with altref, or update altref with golden
  286. */
  287. static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
  288. {
  289. VP56RangeCoder *c = &s->c;
  290. if (update)
  291. return VP56_FRAME_CURRENT;
  292. switch (vp8_rac_get_uint(c, 2)) {
  293. case 1:
  294. return VP56_FRAME_PREVIOUS;
  295. case 2:
  296. return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
  297. }
  298. return VP56_FRAME_NONE;
  299. }
  300. static void update_refs(VP8Context *s)
  301. {
  302. VP56RangeCoder *c = &s->c;
  303. int update_golden = vp8_rac_get(c);
  304. int update_altref = vp8_rac_get(c);
  305. s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
  306. s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
  307. }
  308. static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  309. {
  310. VP56RangeCoder *c = &s->c;
  311. int header_size, hscale, vscale, i, j, k, l, ret;
  312. int width = s->avctx->width;
  313. int height = s->avctx->height;
  314. s->keyframe = !(buf[0] & 1);
  315. s->profile = (buf[0]>>1) & 7;
  316. s->invisible = !(buf[0] & 0x10);
  317. header_size = RL24(buf) >> 5;
  318. buf += 3;
  319. buf_size -= 3;
  320. if (s->profile > 3)
  321. av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
  322. if (!s->profile)
  323. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
  324. else // profile 1-3 use bilinear, 4+ aren't defined so whatever
  325. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
  326. if (header_size > buf_size - 7*s->keyframe) {
  327. av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
  328. return AVERROR_INVALIDDATA;
  329. }
  330. if (s->keyframe) {
  331. if (RL24(buf) != 0x2a019d) {
  332. av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", RL24(buf));
  333. return AVERROR_INVALIDDATA;
  334. }
  335. width = AV_RL16(buf+3) & 0x3fff;
  336. height = AV_RL16(buf+5) & 0x3fff;
  337. hscale = buf[4] >> 6;
  338. vscale = buf[6] >> 6;
  339. buf += 7;
  340. buf_size -= 7;
  341. if (hscale || vscale)
  342. av_log_missing_feature(s->avctx, "Upscaling", 1);
  343. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  344. memcpy(s->prob->token , vp8_token_default_probs , sizeof(s->prob->token));
  345. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
  346. memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
  347. memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
  348. memset(&s->segmentation, 0, sizeof(s->segmentation));
  349. }
  350. if (!s->macroblocks_base || /* first frame */
  351. width != s->avctx->width || height != s->avctx->height) {
  352. if ((ret = update_dimensions(s, width, height) < 0))
  353. return ret;
  354. }
  355. vp56_init_range_decoder(c, buf, header_size);
  356. buf += header_size;
  357. buf_size -= header_size;
  358. if (s->keyframe) {
  359. if (vp8_rac_get(c))
  360. av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
  361. vp8_rac_get(c); // whether we can skip clamping in dsp functions
  362. }
  363. if ((s->segmentation.enabled = vp8_rac_get(c)))
  364. parse_segment_info(s);
  365. else
  366. s->segmentation.update_map = 0; // FIXME: move this to some init function?
  367. s->filter.simple = vp8_rac_get(c);
  368. s->filter.level = vp8_rac_get_uint(c, 6);
  369. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  370. if ((s->lf_delta.enabled = vp8_rac_get(c)))
  371. if (vp8_rac_get(c))
  372. update_lf_deltas(s);
  373. if (setup_partitions(s, buf, buf_size)) {
  374. av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
  375. return AVERROR_INVALIDDATA;
  376. }
  377. get_quants(s);
  378. if (!s->keyframe) {
  379. update_refs(s);
  380. s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
  381. s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
  382. }
  383. // if we aren't saving this frame's probabilities for future frames,
  384. // make a copy of the current probabilities
  385. if (!(s->update_probabilities = vp8_rac_get(c)))
  386. s->prob[1] = s->prob[0];
  387. s->update_last = s->keyframe || vp8_rac_get(c);
  388. for (i = 0; i < 4; i++)
  389. for (j = 0; j < 8; j++)
  390. for (k = 0; k < 3; k++)
  391. for (l = 0; l < NUM_DCT_TOKENS-1; l++)
  392. if (vp56_rac_get_prob(c, vp8_token_update_probs[i][j][k][l]))
  393. s->prob->token[i][j][k][l] = vp8_rac_get_uint(c, 8);
  394. if ((s->mbskip_enabled = vp8_rac_get(c)))
  395. s->prob->mbskip = vp8_rac_get_uint(c, 8);
  396. if (!s->keyframe) {
  397. s->prob->intra = vp8_rac_get_uint(c, 8);
  398. s->prob->last = vp8_rac_get_uint(c, 8);
  399. s->prob->golden = vp8_rac_get_uint(c, 8);
  400. if (vp8_rac_get(c))
  401. for (i = 0; i < 4; i++)
  402. s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
  403. if (vp8_rac_get(c))
  404. for (i = 0; i < 3; i++)
  405. s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
  406. // 17.2 MV probability update
  407. for (i = 0; i < 2; i++)
  408. for (j = 0; j < 19; j++)
  409. if (vp56_rac_get_prob(c, vp8_mv_update_prob[i][j]))
  410. s->prob->mvc[i][j] = vp8_rac_get_nn(c);
  411. }
  412. return 0;
  413. }
  414. static inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src,
  415. int mb_x, int mb_y)
  416. {
  417. #define MARGIN (16 << 2)
  418. dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
  419. ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
  420. dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
  421. ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
  422. }
  423. static void find_near_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
  424. VP56mv near[2], VP56mv *best, int cnt[4])
  425. {
  426. VP8Macroblock *mb_edge[3] = { mb - s->mb_stride /* top */,
  427. mb - 1 /* left */,
  428. mb - s->mb_stride - 1 /* top-left */ };
  429. enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
  430. VP56mv near_mv[4] = {{ 0 }};
  431. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  432. int idx = CNT_ZERO, n;
  433. int best_idx = CNT_ZERO;
  434. /* Process MB on top, left and top-left */
  435. for (n = 0; n < 3; n++) {
  436. VP8Macroblock *edge = mb_edge[n];
  437. if (edge->ref_frame != VP56_FRAME_CURRENT) {
  438. if (edge->mv.x | edge->mv.y) {
  439. VP56mv tmp = edge->mv;
  440. if (s->sign_bias[mb->ref_frame] != s->sign_bias[edge->ref_frame]) {
  441. tmp.x *= -1;
  442. tmp.y *= -1;
  443. }
  444. if ((tmp.x ^ near_mv[idx].x) | (tmp.y ^ near_mv[idx].y))
  445. near_mv[++idx] = tmp;
  446. cnt[idx] += 1 + (n != 2);
  447. } else
  448. cnt[CNT_ZERO] += 1 + (n != 2);
  449. }
  450. }
  451. /* If we have three distinct MV's, merge first and last if they're the same */
  452. if (cnt[CNT_SPLITMV] &&
  453. !((near_mv[1+EDGE_TOP].x ^ near_mv[1+EDGE_TOPLEFT].x) |
  454. (near_mv[1+EDGE_TOP].y ^ near_mv[1+EDGE_TOPLEFT].y)))
  455. cnt[CNT_NEAREST] += 1;
  456. cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
  457. (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
  458. (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
  459. /* Swap near and nearest if necessary */
  460. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  461. FFSWAP(int, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
  462. FFSWAP(VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
  463. }
  464. /* Choose the best mv out of 0,0 and the nearest mv */
  465. if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
  466. best_idx = CNT_NEAREST;
  467. clamp_mv(s, best, &near_mv[best_idx], mb_x, mb_y);
  468. near[0] = near_mv[CNT_NEAREST];
  469. near[1] = near_mv[CNT_NEAR];
  470. }
  471. /**
  472. * Motion vector coding, 17.1.
  473. */
  474. static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
  475. {
  476. int x = 0;
  477. if (vp56_rac_get_prob(c, p[0])) {
  478. int i;
  479. for (i = 0; i < 3; i++)
  480. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  481. for (i = 9; i > 3; i--)
  482. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  483. if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
  484. x += 8;
  485. } else
  486. x = vp8_rac_get_tree(c, vp8_small_mvtree, &p[2]);
  487. return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
  488. }
  489. static const uint8_t *get_submv_prob(const VP56mv *left, const VP56mv *top)
  490. {
  491. int l_is_zero = !(left->x | left->y);
  492. int t_is_zero = !(top->x | top->y);
  493. int equal = !((left->x ^ top->x) | (left->y ^ top->y));
  494. if (equal)
  495. return l_is_zero ? vp8_submv_prob[4] : vp8_submv_prob[3];
  496. if (t_is_zero)
  497. return vp8_submv_prob[2];
  498. return l_is_zero ? vp8_submv_prob[1] : vp8_submv_prob[0];
  499. }
  500. /**
  501. * Split motion vector prediction, 16.4.
  502. * @returns the number of motion vectors parsed (2, 4 or 16)
  503. */
  504. static int decode_splitmvs(VP8Context *s, VP56RangeCoder *c,
  505. VP8Macroblock *mb, VP56mv *base_mv)
  506. {
  507. int part_idx = mb->partitioning =
  508. vp8_rac_get_tree(c, vp8_mbsplit_tree, vp8_mbsplit_prob);
  509. int n, num = vp8_mbsplit_count[part_idx];
  510. const uint8_t *mbsplits = vp8_mbsplits[part_idx],
  511. *firstidx = vp8_mbfirstidx[part_idx];
  512. for (n = 0; n < num; n++) {
  513. int k = firstidx[n];
  514. const VP56mv *left, *above;
  515. const uint8_t *submv_prob;
  516. if (!(k & 3)) {
  517. VP8Macroblock *left_mb = &mb[-1];
  518. left = &left_mb->bmv[vp8_mbsplits[left_mb->partitioning][k + 3]];
  519. } else
  520. left = &mb->bmv[mbsplits[k - 1]];
  521. if (k <= 3) {
  522. VP8Macroblock *above_mb = &mb[-s->mb_stride];
  523. above = &above_mb->bmv[vp8_mbsplits[above_mb->partitioning][k + 12]];
  524. } else
  525. above = &mb->bmv[mbsplits[k - 4]];
  526. submv_prob = get_submv_prob(left, above);
  527. switch (vp8_rac_get_tree(c, vp8_submv_ref_tree, submv_prob)) {
  528. case VP8_SUBMVMODE_NEW4X4:
  529. mb->bmv[n].y = base_mv->y + read_mv_component(c, s->prob->mvc[0]);
  530. mb->bmv[n].x = base_mv->x + read_mv_component(c, s->prob->mvc[1]);
  531. break;
  532. case VP8_SUBMVMODE_ZERO4X4:
  533. mb->bmv[n].x = 0;
  534. mb->bmv[n].y = 0;
  535. break;
  536. case VP8_SUBMVMODE_LEFT4X4:
  537. mb->bmv[n] = *left;
  538. break;
  539. case VP8_SUBMVMODE_TOP4X4:
  540. mb->bmv[n] = *above;
  541. break;
  542. }
  543. }
  544. return num;
  545. }
  546. static inline void decode_intra4x4_modes(VP56RangeCoder *c, uint8_t *intra4x4,
  547. int stride, int keyframe)
  548. {
  549. int x, y, t, l;
  550. const uint8_t *ctx = vp8_pred4x4_prob_inter;
  551. for (y = 0; y < 4; y++) {
  552. for (x = 0; x < 4; x++) {
  553. if (keyframe) {
  554. t = intra4x4[x - stride];
  555. l = intra4x4[x - 1];
  556. ctx = vp8_pred4x4_prob_intra[t][l];
  557. }
  558. intra4x4[x] = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
  559. }
  560. intra4x4 += stride;
  561. }
  562. }
  563. static void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
  564. uint8_t *intra4x4)
  565. {
  566. VP56RangeCoder *c = &s->c;
  567. int n;
  568. if (s->segmentation.update_map)
  569. mb->segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
  570. mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
  571. if (s->keyframe) {
  572. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
  573. if (mb->mode == MODE_I4x4) {
  574. decode_intra4x4_modes(c, intra4x4, s->b4_stride, 1);
  575. } else
  576. fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
  577. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
  578. mb->ref_frame = VP56_FRAME_CURRENT;
  579. } else if (vp56_rac_get_prob(c, s->prob->intra)) {
  580. VP56mv near[2], best;
  581. int cnt[4] = { 0 };
  582. uint8_t p[4];
  583. // inter MB, 16.2
  584. if (vp56_rac_get_prob(c, s->prob->last))
  585. mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
  586. VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
  587. else
  588. mb->ref_frame = VP56_FRAME_PREVIOUS;
  589. // motion vectors, 16.3
  590. find_near_mvs(s, mb, mb_x, mb_y, near, &best, cnt);
  591. for (n = 0; n < 4; n++)
  592. p[n] = vp8_mode_contexts[cnt[n]][n];
  593. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_mvinter, p);
  594. switch (mb->mode) {
  595. case VP8_MVMODE_SPLIT:
  596. mb->mv = mb->bmv[decode_splitmvs(s, c, mb, &best) - 1];
  597. break;
  598. case VP8_MVMODE_ZERO:
  599. mb->mv.x = 0;
  600. mb->mv.y = 0;
  601. break;
  602. case VP8_MVMODE_NEAREST:
  603. clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
  604. break;
  605. case VP8_MVMODE_NEAR:
  606. clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
  607. break;
  608. case VP8_MVMODE_NEW:
  609. mb->mv.y = best.y + read_mv_component(c, s->prob->mvc[0]);
  610. mb->mv.x = best.x + read_mv_component(c, s->prob->mvc[1]);
  611. break;
  612. }
  613. if (mb->mode != VP8_MVMODE_SPLIT) {
  614. mb->partitioning = VP8_SPLITMVMODE_NONE;
  615. mb->bmv[0] = mb->mv;
  616. }
  617. } else {
  618. // intra MB, 16.1
  619. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
  620. if (mb->mode == MODE_I4x4) {
  621. decode_intra4x4_modes(c, intra4x4, s->b4_stride, 0);
  622. } else
  623. fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
  624. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
  625. mb->ref_frame = VP56_FRAME_CURRENT;
  626. }
  627. }
  628. /**
  629. * @param i initial coeff index, 0 unless a separate DC block is coded
  630. * @param zero_nhood the initial prediction context for number of surrounding
  631. * all-zero blocks (only left/top, so 0-2)
  632. * @param qmul[0] dc dequant factor
  633. * @param qmul[1] ac dequant factor
  634. * @return 0 if no coeffs were decoded
  635. * otherwise, the index of the last coeff decoded plus one
  636. */
  637. static int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
  638. uint8_t probs[8][3][NUM_DCT_TOKENS-1],
  639. int i, int zero_nhood, int16_t qmul[2])
  640. {
  641. int token, nonzero = 0;
  642. int offset = 0;
  643. for (; i < 16; i++) {
  644. token = vp8_rac_get_tree_with_offset(c, vp8_coeff_tree, probs[vp8_coeff_band[i]][zero_nhood], offset);
  645. if (token == DCT_EOB)
  646. break;
  647. else if (token >= DCT_CAT1) {
  648. int cat = token-DCT_CAT1;
  649. token = vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
  650. token += vp8_dct_cat_offset[cat];
  651. }
  652. // after the first token, the non-zero prediction context becomes
  653. // based on the last decoded coeff
  654. if (!token) {
  655. zero_nhood = 0;
  656. offset = 1;
  657. continue;
  658. } else if (token == 1)
  659. zero_nhood = 1;
  660. else
  661. zero_nhood = 2;
  662. // todo: full [16] qmat? load into register?
  663. block[zigzag_scan[i]] = (vp8_rac_get(c) ? -token : token) * qmul[!!i];
  664. nonzero = i+1;
  665. offset = 0;
  666. }
  667. return nonzero;
  668. }
  669. static void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  670. uint8_t t_nnz[9], uint8_t l_nnz[9])
  671. {
  672. LOCAL_ALIGNED_16(DCTELEM, dc,[16]);
  673. int i, x, y, luma_start = 0, luma_ctx = 3;
  674. int nnz_pred, nnz, nnz_total = 0;
  675. int segment = s->segmentation.enabled ? mb->segment : 0;
  676. s->dsp.clear_blocks((DCTELEM *)s->block);
  677. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  678. AV_ZERO128(dc);
  679. AV_ZERO128(dc+8);
  680. nnz_pred = t_nnz[8] + l_nnz[8];
  681. // decode DC values and do hadamard
  682. nnz = decode_block_coeffs(c, dc, s->prob->token[1], 0, nnz_pred,
  683. s->qmat[segment].luma_dc_qmul);
  684. l_nnz[8] = t_nnz[8] = !!nnz;
  685. nnz_total += nnz;
  686. s->vp8dsp.vp8_luma_dc_wht(s->block, dc);
  687. luma_start = 1;
  688. luma_ctx = 0;
  689. }
  690. // luma blocks
  691. for (y = 0; y < 4; y++)
  692. for (x = 0; x < 4; x++) {
  693. nnz_pred = l_nnz[y] + t_nnz[x];
  694. nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
  695. nnz_pred, s->qmat[segment].luma_qmul);
  696. // nnz+luma_start may be one more than the actual last index, but we don't care
  697. s->non_zero_count_cache[y][x] = nnz + luma_start;
  698. t_nnz[x] = l_nnz[y] = !!nnz;
  699. nnz_total += nnz;
  700. }
  701. // chroma blocks
  702. // TODO: what to do about dimensions? 2nd dim for luma is x,
  703. // but for chroma it's (y<<1)|x
  704. for (i = 4; i < 6; i++)
  705. for (y = 0; y < 2; y++)
  706. for (x = 0; x < 2; x++) {
  707. nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
  708. nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
  709. nnz_pred, s->qmat[segment].chroma_qmul);
  710. s->non_zero_count_cache[i][(y<<1)+x] = nnz;
  711. t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
  712. nnz_total += nnz;
  713. }
  714. // if there were no coded coeffs despite the macroblock not being marked skip,
  715. // we MUST not do the inner loop filter and should not do IDCT
  716. // Since skip isn't used for bitstream prediction, just manually set it.
  717. if (!nnz_total)
  718. mb->skip = 1;
  719. }
  720. static int check_intra_pred_mode(int mode, int mb_x, int mb_y)
  721. {
  722. if (mode == DC_PRED8x8) {
  723. if (!(mb_x|mb_y))
  724. mode = DC_128_PRED8x8;
  725. else if (!mb_y)
  726. mode = LEFT_DC_PRED8x8;
  727. else if (!mb_x)
  728. mode = TOP_DC_PRED8x8;
  729. }
  730. return mode;
  731. }
  732. static void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  733. uint8_t *bmode, int mb_x, int mb_y)
  734. {
  735. int x, y, mode, nnz, tr;
  736. if (mb->mode < MODE_I4x4) {
  737. mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
  738. s->hpc.pred16x16[mode](dst[0], s->linesize);
  739. } else {
  740. uint8_t *ptr = dst[0];
  741. // all blocks on the right edge of the macroblock use bottom edge
  742. // the top macroblock for their topright edge
  743. uint8_t *tr_right = ptr - s->linesize + 16;
  744. // if we're on the right edge of the frame, said edge is extended
  745. // from the top macroblock
  746. if (mb_x == s->mb_width-1) {
  747. tr = tr_right[-1]*0x01010101;
  748. tr_right = (uint8_t *)&tr;
  749. }
  750. for (y = 0; y < 4; y++) {
  751. uint8_t *topright = ptr + 4 - s->linesize;
  752. for (x = 0; x < 4; x++) {
  753. if (x == 3)
  754. topright = tr_right;
  755. s->hpc.pred4x4[bmode[x]](ptr+4*x, topright, s->linesize);
  756. nnz = s->non_zero_count_cache[y][x];
  757. if (nnz) {
  758. if (nnz == 1)
  759. s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
  760. else
  761. s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
  762. }
  763. topright += 4;
  764. }
  765. ptr += 4*s->linesize;
  766. bmode += s->b4_stride;
  767. }
  768. }
  769. mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
  770. s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
  771. s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
  772. }
  773. /**
  774. * Generic MC function.
  775. *
  776. * @param s VP8 decoding context
  777. * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
  778. * @param dst target buffer for block data at block position
  779. * @param src reference picture buffer at origin (0, 0)
  780. * @param mv motion vector (relative to block position) to get pixel data from
  781. * @param x_off horizontal position of block from origin (0, 0)
  782. * @param y_off vertical position of block from origin (0, 0)
  783. * @param block_w width of block (16, 8 or 4)
  784. * @param block_h height of block (always same as block_w)
  785. * @param width width of src/dst plane data
  786. * @param height height of src/dst plane data
  787. * @param linesize size of a single line of plane data, including padding
  788. */
  789. static inline void vp8_mc(VP8Context *s, int luma,
  790. uint8_t *dst, uint8_t *src, const VP56mv *mv,
  791. int x_off, int y_off, int block_w, int block_h,
  792. int width, int height, int linesize,
  793. vp8_mc_func mc_func[3][3])
  794. {
  795. static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
  796. int mx = (mv->x << luma)&7, mx_idx = idx[mx];
  797. int my = (mv->y << luma)&7, my_idx = idx[my];
  798. x_off += mv->x >> (3 - luma);
  799. y_off += mv->y >> (3 - luma);
  800. // edge emulation
  801. src += y_off * linesize + x_off;
  802. if (x_off < 2 || x_off >= width - block_w - 3 ||
  803. y_off < 2 || y_off >= height - block_h - 3) {
  804. ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
  805. block_w + 5, block_h + 5,
  806. x_off - 2, y_off - 2, width, height);
  807. src = s->edge_emu_buffer + 2 + linesize * 2;
  808. }
  809. mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
  810. }
  811. static inline void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
  812. AVFrame *ref_frame, int x_off, int y_off,
  813. int bx_off, int by_off,
  814. int block_w, int block_h,
  815. int width, int height, VP56mv *mv)
  816. {
  817. VP56mv uvmv = *mv;
  818. /* Y */
  819. vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,
  820. ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
  821. block_w, block_h, width, height, s->linesize,
  822. s->put_pixels_tab[block_w == 8]);
  823. /* U/V */
  824. if (s->profile == 3) {
  825. uvmv.x &= ~7;
  826. uvmv.y &= ~7;
  827. }
  828. x_off >>= 1; y_off >>= 1;
  829. bx_off >>= 1; by_off >>= 1;
  830. width >>= 1; height >>= 1;
  831. block_w >>= 1; block_h >>= 1;
  832. vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,
  833. ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,
  834. block_w, block_h, width, height, s->uvlinesize,
  835. s->put_pixels_tab[1 + (block_w == 4)]);
  836. vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,
  837. ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
  838. block_w, block_h, width, height, s->uvlinesize,
  839. s->put_pixels_tab[1 + (block_w == 4)]);
  840. }
  841. /**
  842. * Apply motion vectors to prediction buffer, chapter 18.
  843. */
  844. static void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  845. int mb_x, int mb_y)
  846. {
  847. int x_off = mb_x << 4, y_off = mb_y << 4;
  848. int width = 16*s->mb_width, height = 16*s->mb_height;
  849. if (mb->mode < VP8_MVMODE_SPLIT) {
  850. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  851. 0, 0, 16, 16, width, height, &mb->mv);
  852. } else switch (mb->partitioning) {
  853. case VP8_SPLITMVMODE_4x4: {
  854. int x, y;
  855. VP56mv uvmv;
  856. /* Y */
  857. for (y = 0; y < 4; y++) {
  858. for (x = 0; x < 4; x++) {
  859. vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
  860. s->framep[mb->ref_frame]->data[0], &mb->bmv[4*y + x],
  861. 4*x + x_off, 4*y + y_off, 4, 4,
  862. width, height, s->linesize,
  863. s->put_pixels_tab[2]);
  864. }
  865. }
  866. /* U/V */
  867. x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
  868. for (y = 0; y < 2; y++) {
  869. for (x = 0; x < 2; x++) {
  870. uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
  871. mb->bmv[ 2*y * 4 + 2*x+1].x +
  872. mb->bmv[(2*y+1) * 4 + 2*x ].x +
  873. mb->bmv[(2*y+1) * 4 + 2*x+1].x;
  874. uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
  875. mb->bmv[ 2*y * 4 + 2*x+1].y +
  876. mb->bmv[(2*y+1) * 4 + 2*x ].y +
  877. mb->bmv[(2*y+1) * 4 + 2*x+1].y;
  878. uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
  879. uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
  880. if (s->profile == 3) {
  881. uvmv.x &= ~7;
  882. uvmv.y &= ~7;
  883. }
  884. vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
  885. s->framep[mb->ref_frame]->data[1], &uvmv,
  886. 4*x + x_off, 4*y + y_off, 4, 4,
  887. width, height, s->uvlinesize,
  888. s->put_pixels_tab[2]);
  889. vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
  890. s->framep[mb->ref_frame]->data[2], &uvmv,
  891. 4*x + x_off, 4*y + y_off, 4, 4,
  892. width, height, s->uvlinesize,
  893. s->put_pixels_tab[2]);
  894. }
  895. }
  896. break;
  897. }
  898. case VP8_SPLITMVMODE_16x8:
  899. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  900. 0, 0, 16, 8, width, height, &mb->bmv[0]);
  901. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  902. 0, 8, 16, 8, width, height, &mb->bmv[1]);
  903. break;
  904. case VP8_SPLITMVMODE_8x16:
  905. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  906. 0, 0, 8, 16, width, height, &mb->bmv[0]);
  907. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  908. 8, 0, 8, 16, width, height, &mb->bmv[1]);
  909. break;
  910. case VP8_SPLITMVMODE_8x8:
  911. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  912. 0, 0, 8, 8, width, height, &mb->bmv[0]);
  913. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  914. 8, 0, 8, 8, width, height, &mb->bmv[1]);
  915. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  916. 0, 8, 8, 8, width, height, &mb->bmv[2]);
  917. vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
  918. 8, 8, 8, 8, width, height, &mb->bmv[3]);
  919. break;
  920. }
  921. }
  922. static void idct_mb(VP8Context *s, uint8_t *y_dst, uint8_t *u_dst, uint8_t *v_dst,
  923. VP8Macroblock *mb)
  924. {
  925. int x, y, nnz;
  926. if (mb->mode != MODE_I4x4)
  927. for (y = 0; y < 4; y++) {
  928. for (x = 0; x < 4; x++) {
  929. nnz = s->non_zero_count_cache[y][x];
  930. if (nnz) {
  931. if (nnz == 1)
  932. s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
  933. else
  934. s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
  935. }
  936. }
  937. y_dst += 4*s->linesize;
  938. }
  939. for (y = 0; y < 2; y++) {
  940. for (x = 0; x < 2; x++) {
  941. nnz = s->non_zero_count_cache[4][(y<<1)+x];
  942. if (nnz) {
  943. if (nnz == 1)
  944. s->vp8dsp.vp8_idct_dc_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
  945. else
  946. s->vp8dsp.vp8_idct_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
  947. }
  948. nnz = s->non_zero_count_cache[5][(y<<1)+x];
  949. if (nnz) {
  950. if (nnz == 1)
  951. s->vp8dsp.vp8_idct_dc_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
  952. else
  953. s->vp8dsp.vp8_idct_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
  954. }
  955. }
  956. u_dst += 4*s->uvlinesize;
  957. v_dst += 4*s->uvlinesize;
  958. }
  959. }
  960. static void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, int *level, int *inner, int *hev_thresh)
  961. {
  962. int interior_limit, filter_level;
  963. if (s->segmentation.enabled) {
  964. filter_level = s->segmentation.filter_level[mb->segment];
  965. if (!s->segmentation.absolute_vals)
  966. filter_level += s->filter.level;
  967. } else
  968. filter_level = s->filter.level;
  969. if (s->lf_delta.enabled) {
  970. filter_level += s->lf_delta.ref[mb->ref_frame];
  971. if (mb->ref_frame == VP56_FRAME_CURRENT) {
  972. if (mb->mode == MODE_I4x4)
  973. filter_level += s->lf_delta.mode[0];
  974. } else {
  975. if (mb->mode == VP8_MVMODE_ZERO)
  976. filter_level += s->lf_delta.mode[1];
  977. else if (mb->mode == VP8_MVMODE_SPLIT)
  978. filter_level += s->lf_delta.mode[3];
  979. else
  980. filter_level += s->lf_delta.mode[2];
  981. }
  982. }
  983. filter_level = av_clip(filter_level, 0, 63);
  984. interior_limit = filter_level;
  985. if (s->filter.sharpness) {
  986. interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
  987. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  988. }
  989. interior_limit = FFMAX(interior_limit, 1);
  990. *level = filter_level;
  991. *inner = interior_limit;
  992. if (hev_thresh) {
  993. *hev_thresh = filter_level >= 15;
  994. if (s->keyframe) {
  995. if (filter_level >= 40)
  996. *hev_thresh = 2;
  997. } else {
  998. if (filter_level >= 40)
  999. *hev_thresh = 3;
  1000. else if (filter_level >= 20)
  1001. *hev_thresh = 2;
  1002. }
  1003. }
  1004. }
  1005. // TODO: look at backup_mb_border / xchg_mb_border in h264.c
  1006. static void filter_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
  1007. {
  1008. int filter_level, inner_limit, hev_thresh;
  1009. filter_level_for_mb(s, mb, &filter_level, &inner_limit, &hev_thresh);
  1010. if (!filter_level)
  1011. return;
  1012. if (mb_x) {
  1013. s->vp8dsp.vp8_h_loop_filter16(dst[0], s->linesize, filter_level+2, inner_limit, hev_thresh);
  1014. s->vp8dsp.vp8_h_loop_filter8 (dst[1], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
  1015. s->vp8dsp.vp8_h_loop_filter8 (dst[2], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
  1016. }
  1017. if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
  1018. s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+ 4, s->linesize, filter_level, inner_limit, hev_thresh);
  1019. s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+ 8, s->linesize, filter_level, inner_limit, hev_thresh);
  1020. s->vp8dsp.vp8_h_loop_filter16_inner(dst[0]+12, s->linesize, filter_level, inner_limit, hev_thresh);
  1021. s->vp8dsp.vp8_h_loop_filter8_inner (dst[1]+ 4, s->uvlinesize, filter_level, inner_limit, hev_thresh);
  1022. s->vp8dsp.vp8_h_loop_filter8_inner (dst[2]+ 4, s->uvlinesize, filter_level, inner_limit, hev_thresh);
  1023. }
  1024. if (mb_y) {
  1025. s->vp8dsp.vp8_v_loop_filter16(dst[0], s->linesize, filter_level+2, inner_limit, hev_thresh);
  1026. s->vp8dsp.vp8_v_loop_filter8 (dst[1], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
  1027. s->vp8dsp.vp8_v_loop_filter8 (dst[2], s->uvlinesize, filter_level+2, inner_limit, hev_thresh);
  1028. }
  1029. if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
  1030. s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+ 4*s->linesize, s->linesize, filter_level, inner_limit, hev_thresh);
  1031. s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+ 8*s->linesize, s->linesize, filter_level, inner_limit, hev_thresh);
  1032. s->vp8dsp.vp8_v_loop_filter16_inner(dst[0]+12*s->linesize, s->linesize, filter_level, inner_limit, hev_thresh);
  1033. s->vp8dsp.vp8_v_loop_filter8_inner (dst[1]+ 4*s->uvlinesize, s->uvlinesize, filter_level, inner_limit, hev_thresh);
  1034. s->vp8dsp.vp8_v_loop_filter8_inner (dst[2]+ 4*s->uvlinesize, s->uvlinesize, filter_level, inner_limit, hev_thresh);
  1035. }
  1036. }
  1037. static void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8Macroblock *mb, int mb_x, int mb_y)
  1038. {
  1039. int filter_level, inner_limit, mbedge_lim, bedge_lim;
  1040. filter_level_for_mb(s, mb, &filter_level, &inner_limit, NULL);
  1041. if (!filter_level)
  1042. return;
  1043. mbedge_lim = 2*(filter_level+2) + inner_limit;
  1044. bedge_lim = 2* filter_level + inner_limit;
  1045. if (mb_x)
  1046. s->vp8dsp.vp8_h_loop_filter_simple(dst, s->linesize, mbedge_lim);
  1047. if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
  1048. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, s->linesize, bedge_lim);
  1049. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, s->linesize, bedge_lim);
  1050. s->vp8dsp.vp8_h_loop_filter_simple(dst+12, s->linesize, bedge_lim);
  1051. }
  1052. if (mb_y)
  1053. s->vp8dsp.vp8_v_loop_filter_simple(dst, s->linesize, mbedge_lim);
  1054. if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
  1055. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*s->linesize, s->linesize, bedge_lim);
  1056. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*s->linesize, s->linesize, bedge_lim);
  1057. s->vp8dsp.vp8_v_loop_filter_simple(dst+12*s->linesize, s->linesize, bedge_lim);
  1058. }
  1059. }
  1060. static void filter_mb_row(VP8Context *s, int mb_y)
  1061. {
  1062. VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
  1063. uint8_t *dst[3] = {
  1064. s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
  1065. s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
  1066. s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
  1067. };
  1068. int mb_x;
  1069. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1070. filter_mb(s, dst, mb++, mb_x, mb_y);
  1071. dst[0] += 16;
  1072. dst[1] += 8;
  1073. dst[2] += 8;
  1074. }
  1075. }
  1076. static void filter_mb_row_simple(VP8Context *s, int mb_y)
  1077. {
  1078. uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
  1079. VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
  1080. int mb_x;
  1081. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1082. filter_mb_simple(s, dst, mb++, mb_x, mb_y);
  1083. dst += 16;
  1084. }
  1085. }
  1086. static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  1087. AVPacket *avpkt)
  1088. {
  1089. VP8Context *s = avctx->priv_data;
  1090. int ret, mb_x, mb_y, i, y, referenced;
  1091. enum AVDiscard skip_thresh;
  1092. AVFrame *curframe;
  1093. if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
  1094. return ret;
  1095. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
  1096. || s->update_altref == VP56_FRAME_CURRENT;
  1097. skip_thresh = !referenced ? AVDISCARD_NONREF :
  1098. !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
  1099. if (avctx->skip_frame >= skip_thresh) {
  1100. s->invisible = 1;
  1101. goto skip_decode;
  1102. }
  1103. for (i = 0; i < 4; i++)
  1104. if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1105. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1106. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  1107. curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
  1108. break;
  1109. }
  1110. if (curframe->data[0])
  1111. avctx->release_buffer(avctx, curframe);
  1112. curframe->key_frame = s->keyframe;
  1113. curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  1114. curframe->reference = referenced ? 3 : 0;
  1115. if ((ret = avctx->get_buffer(avctx, curframe))) {
  1116. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
  1117. return ret;
  1118. }
  1119. // Given that arithmetic probabilities are updated every frame, it's quite likely
  1120. // that the values we have on a random interframe are complete junk if we didn't
  1121. // start decode on a keyframe. So just don't display anything rather than junk.
  1122. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  1123. !s->framep[VP56_FRAME_GOLDEN] ||
  1124. !s->framep[VP56_FRAME_GOLDEN2])) {
  1125. av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
  1126. return AVERROR_INVALIDDATA;
  1127. }
  1128. s->linesize = curframe->linesize[0];
  1129. s->uvlinesize = curframe->linesize[1];
  1130. if (!s->edge_emu_buffer)
  1131. s->edge_emu_buffer = av_malloc(21*s->linesize);
  1132. memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
  1133. // top edge of 127 for intra prediction
  1134. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1135. memset(curframe->data[0] - s->linesize -1, 127, s->linesize +1);
  1136. memset(curframe->data[1] - s->uvlinesize-1, 127, s->uvlinesize+1);
  1137. memset(curframe->data[2] - s->uvlinesize-1, 127, s->uvlinesize+1);
  1138. }
  1139. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1140. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
  1141. VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
  1142. uint8_t *intra4x4 = s->intra4x4_pred_mode + 4*mb_y*s->b4_stride;
  1143. uint8_t *dst[3] = {
  1144. curframe->data[0] + 16*mb_y*s->linesize,
  1145. curframe->data[1] + 8*mb_y*s->uvlinesize,
  1146. curframe->data[2] + 8*mb_y*s->uvlinesize
  1147. };
  1148. memset(s->left_nnz, 0, sizeof(s->left_nnz));
  1149. // left edge of 129 for intra prediction
  1150. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
  1151. for (i = 0; i < 3; i++)
  1152. for (y = 0; y < 16>>!!i; y++)
  1153. dst[i][y*curframe->linesize[i]-1] = 129;
  1154. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1155. decode_mb_mode(s, mb, mb_x, mb_y, intra4x4 + 4*mb_x);
  1156. if (!mb->skip)
  1157. decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
  1158. else {
  1159. AV_ZERO128(s->non_zero_count_cache); // luma
  1160. AV_ZERO64(s->non_zero_count_cache[4]); // chroma
  1161. }
  1162. if (mb->mode <= MODE_I4x4) {
  1163. intra_predict(s, dst, mb, intra4x4 + 4*mb_x, mb_x, mb_y);
  1164. memset(mb->bmv, 0, sizeof(mb->bmv));
  1165. } else {
  1166. inter_predict(s, dst, mb, mb_x, mb_y);
  1167. }
  1168. if (!mb->skip) {
  1169. idct_mb(s, dst[0], dst[1], dst[2], mb);
  1170. } else {
  1171. AV_ZERO64(s->left_nnz);
  1172. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  1173. // Reset DC block predictors if they would exist if the mb had coefficients
  1174. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  1175. s->left_nnz[8] = 0;
  1176. s->top_nnz[mb_x][8] = 0;
  1177. }
  1178. }
  1179. dst[0] += 16;
  1180. dst[1] += 8;
  1181. dst[2] += 8;
  1182. mb++;
  1183. }
  1184. if (mb_y && s->filter.level && avctx->skip_loop_filter < skip_thresh) {
  1185. if (s->filter.simple)
  1186. filter_mb_row_simple(s, mb_y-1);
  1187. else
  1188. filter_mb_row(s, mb_y-1);
  1189. }
  1190. }
  1191. if (s->filter.level && avctx->skip_loop_filter < skip_thresh) {
  1192. if (s->filter.simple)
  1193. filter_mb_row_simple(s, mb_y-1);
  1194. else
  1195. filter_mb_row(s, mb_y-1);
  1196. }
  1197. skip_decode:
  1198. // if future frames don't use the updated probabilities,
  1199. // reset them to the values we saved
  1200. if (!s->update_probabilities)
  1201. s->prob[0] = s->prob[1];
  1202. // check if golden and altref are swapped
  1203. if (s->update_altref == VP56_FRAME_GOLDEN &&
  1204. s->update_golden == VP56_FRAME_GOLDEN2)
  1205. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
  1206. else {
  1207. if (s->update_altref != VP56_FRAME_NONE)
  1208. s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  1209. if (s->update_golden != VP56_FRAME_NONE)
  1210. s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  1211. }
  1212. if (s->update_last) // move cur->prev
  1213. s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
  1214. // release no longer referenced frames
  1215. for (i = 0; i < 4; i++)
  1216. if (s->frames[i].data[0] &&
  1217. &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
  1218. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1219. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1220. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  1221. avctx->release_buffer(avctx, &s->frames[i]);
  1222. if (!s->invisible) {
  1223. *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
  1224. *data_size = sizeof(AVFrame);
  1225. }
  1226. return avpkt->size;
  1227. }
  1228. static av_cold int vp8_decode_init(AVCodecContext *avctx)
  1229. {
  1230. VP8Context *s = avctx->priv_data;
  1231. s->avctx = avctx;
  1232. avctx->pix_fmt = PIX_FMT_YUV420P;
  1233. dsputil_init(&s->dsp, avctx);
  1234. ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
  1235. ff_vp8dsp_init(&s->vp8dsp);
  1236. // intra pred needs edge emulation among other things
  1237. if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
  1238. av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported\n");
  1239. return AVERROR_PATCHWELCOME;
  1240. }
  1241. return 0;
  1242. }
  1243. static av_cold int vp8_decode_free(AVCodecContext *avctx)
  1244. {
  1245. vp8_decode_flush(avctx);
  1246. return 0;
  1247. }
  1248. AVCodec vp8_decoder = {
  1249. "vp8",
  1250. AVMEDIA_TYPE_VIDEO,
  1251. CODEC_ID_VP8,
  1252. sizeof(VP8Context),
  1253. vp8_decode_init,
  1254. NULL,
  1255. vp8_decode_free,
  1256. vp8_decode_frame,
  1257. CODEC_CAP_DR1,
  1258. .flush = vp8_decode_flush,
  1259. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  1260. };