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.

1552 lines
54KB

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