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.

1818 lines
64KB

  1. /**
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  6. * Copyright (C) 2010 Jason Garrett-Glaser
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavcore/imgutils.h"
  25. #include "avcodec.h"
  26. #include "vp56.h"
  27. #include "vp8data.h"
  28. #include "vp8dsp.h"
  29. #include "h264pred.h"
  30. #include "rectangle.h"
  31. typedef struct {
  32. uint8_t filter_level;
  33. uint8_t inner_limit;
  34. uint8_t inner_filter;
  35. } VP8FilterStrength;
  36. typedef struct {
  37. uint8_t skip;
  38. // todo: make it possible to check for at least (i4x4 or split_mv)
  39. // in one op. are others needed?
  40. uint8_t mode;
  41. uint8_t ref_frame;
  42. uint8_t partitioning;
  43. VP56mv mv;
  44. VP56mv bmv[16];
  45. } VP8Macroblock;
  46. typedef struct {
  47. AVCodecContext *avctx;
  48. DSPContext dsp;
  49. VP8DSPContext vp8dsp;
  50. H264PredContext hpc;
  51. vp8_mc_func put_pixels_tab[3][3][3];
  52. AVFrame frames[4];
  53. AVFrame *framep[4];
  54. uint8_t *edge_emu_buffer;
  55. VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
  56. int profile;
  57. int mb_width; /* number of horizontal MB */
  58. int mb_height; /* number of vertical MB */
  59. int linesize;
  60. int uvlinesize;
  61. int keyframe;
  62. int invisible;
  63. int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
  64. int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
  65. int update_altref;
  66. int deblock_filter;
  67. /**
  68. * If this flag is not set, all the probability updates
  69. * are discarded after this frame is decoded.
  70. */
  71. int update_probabilities;
  72. /**
  73. * All coefficients are contained in separate arith coding contexts.
  74. * There can be 1, 2, 4, or 8 of these after the header context.
  75. */
  76. int num_coeff_partitions;
  77. VP56RangeCoder coeff_partition[8];
  78. VP8Macroblock *macroblocks;
  79. VP8Macroblock *macroblocks_base;
  80. VP8FilterStrength *filter_strength;
  81. uint8_t *intra4x4_pred_mode_top;
  82. uint8_t intra4x4_pred_mode_left[4];
  83. uint8_t *segmentation_map;
  84. /**
  85. * Cache of the top row needed for intra prediction
  86. * 16 for luma, 8 for each chroma plane
  87. */
  88. uint8_t (*top_border)[16+8+8];
  89. /**
  90. * For coeff decode, we need to know whether the above block had non-zero
  91. * coefficients. This means for each macroblock, we need data for 4 luma
  92. * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
  93. * per macroblock. We keep the last row in top_nnz.
  94. */
  95. uint8_t (*top_nnz)[9];
  96. DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
  97. /**
  98. * This is the index plus one of the last non-zero coeff
  99. * for each of the blocks in the current macroblock.
  100. * So, 0 -> no coeffs
  101. * 1 -> dc-only (special transform)
  102. * 2+-> full transform
  103. */
  104. DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
  105. DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
  106. DECLARE_ALIGNED(16, DCTELEM, block_dc)[16];
  107. uint8_t intra4x4_pred_mode_mb[16];
  108. int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
  109. int segment; ///< segment of the current macroblock
  110. int mbskip_enabled;
  111. int sign_bias[4]; ///< one state [0, 1] per ref frame type
  112. int ref_count[3];
  113. /**
  114. * Base parameters for segmentation, i.e. per-macroblock parameters.
  115. * These must be kept unchanged even if segmentation is not used for
  116. * a frame, since the values persist between interframes.
  117. */
  118. struct {
  119. int enabled;
  120. int absolute_vals;
  121. int update_map;
  122. int8_t base_quant[4];
  123. int8_t filter_level[4]; ///< base loop filter level
  124. } segmentation;
  125. /**
  126. * Macroblocks can have one of 4 different quants in a frame when
  127. * segmentation is enabled.
  128. * If segmentation is disabled, only the first segment's values are used.
  129. */
  130. struct {
  131. // [0] - DC qmul [1] - AC qmul
  132. int16_t luma_qmul[2];
  133. int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
  134. int16_t chroma_qmul[2];
  135. } qmat[4];
  136. struct {
  137. int simple;
  138. int level;
  139. int sharpness;
  140. } filter;
  141. struct {
  142. int enabled; ///< whether each mb can have a different strength based on mode/ref
  143. /**
  144. * filter strength adjustment for the following macroblock modes:
  145. * [0] - i4x4
  146. * [1] - zero mv
  147. * [2] - inter modes except for zero or split mv
  148. * [3] - split mv
  149. * i16x16 modes never have any adjustment
  150. */
  151. int8_t mode[4];
  152. /**
  153. * filter strength adjustment for macroblocks that reference:
  154. * [0] - intra / VP56_FRAME_CURRENT
  155. * [1] - VP56_FRAME_PREVIOUS
  156. * [2] - VP56_FRAME_GOLDEN
  157. * [3] - altref / VP56_FRAME_GOLDEN2
  158. */
  159. int8_t ref[4];
  160. } lf_delta;
  161. /**
  162. * These are all of the updatable probabilities for binary decisions.
  163. * They are only implictly reset on keyframes, making it quite likely
  164. * for an interframe to desync if a prior frame's header was corrupt
  165. * or missing outright!
  166. */
  167. struct {
  168. uint8_t segmentid[3];
  169. uint8_t mbskip;
  170. uint8_t intra;
  171. uint8_t last;
  172. uint8_t golden;
  173. uint8_t pred16x16[4];
  174. uint8_t pred8x8c[3];
  175. /* Padded to allow overreads */
  176. uint8_t token[4][17][3][NUM_DCT_TOKENS-1];
  177. uint8_t mvc[2][19];
  178. } prob[2];
  179. } VP8Context;
  180. static void vp8_decode_flush(AVCodecContext *avctx)
  181. {
  182. VP8Context *s = avctx->priv_data;
  183. int i;
  184. for (i = 0; i < 4; i++)
  185. if (s->frames[i].data[0])
  186. avctx->release_buffer(avctx, &s->frames[i]);
  187. memset(s->framep, 0, sizeof(s->framep));
  188. av_freep(&s->macroblocks_base);
  189. av_freep(&s->filter_strength);
  190. av_freep(&s->intra4x4_pred_mode_top);
  191. av_freep(&s->top_nnz);
  192. av_freep(&s->edge_emu_buffer);
  193. av_freep(&s->top_border);
  194. av_freep(&s->segmentation_map);
  195. s->macroblocks = NULL;
  196. }
  197. static int update_dimensions(VP8Context *s, int width, int height)
  198. {
  199. if (av_image_check_size(width, height, 0, s->avctx))
  200. return AVERROR_INVALIDDATA;
  201. vp8_decode_flush(s->avctx);
  202. avcodec_set_dimensions(s->avctx, width, height);
  203. s->mb_width = (s->avctx->coded_width +15) / 16;
  204. s->mb_height = (s->avctx->coded_height+15) / 16;
  205. s->macroblocks_base = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
  206. s->filter_strength = av_mallocz(s->mb_width*sizeof(*s->filter_strength));
  207. s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
  208. s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
  209. s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
  210. s->segmentation_map = av_mallocz(s->mb_width*s->mb_height);
  211. if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_top ||
  212. !s->top_nnz || !s->top_border || !s->segmentation_map)
  213. return AVERROR(ENOMEM);
  214. s->macroblocks = s->macroblocks_base + 1;
  215. return 0;
  216. }
  217. static void parse_segment_info(VP8Context *s)
  218. {
  219. VP56RangeCoder *c = &s->c;
  220. int i;
  221. s->segmentation.update_map = vp8_rac_get(c);
  222. if (vp8_rac_get(c)) { // update segment feature data
  223. s->segmentation.absolute_vals = vp8_rac_get(c);
  224. for (i = 0; i < 4; i++)
  225. s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
  226. for (i = 0; i < 4; i++)
  227. s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
  228. }
  229. if (s->segmentation.update_map)
  230. for (i = 0; i < 3; i++)
  231. s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  232. }
  233. static void update_lf_deltas(VP8Context *s)
  234. {
  235. VP56RangeCoder *c = &s->c;
  236. int i;
  237. for (i = 0; i < 4; i++)
  238. s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
  239. for (i = 0; i < 4; i++)
  240. s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
  241. }
  242. static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
  243. {
  244. const uint8_t *sizes = buf;
  245. int i;
  246. s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
  247. buf += 3*(s->num_coeff_partitions-1);
  248. buf_size -= 3*(s->num_coeff_partitions-1);
  249. if (buf_size < 0)
  250. return -1;
  251. for (i = 0; i < s->num_coeff_partitions-1; i++) {
  252. int size = AV_RL24(sizes + 3*i);
  253. if (buf_size - size < 0)
  254. return -1;
  255. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
  256. buf += size;
  257. buf_size -= size;
  258. }
  259. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
  260. return 0;
  261. }
  262. static void get_quants(VP8Context *s)
  263. {
  264. VP56RangeCoder *c = &s->c;
  265. int i, base_qi;
  266. int yac_qi = vp8_rac_get_uint(c, 7);
  267. int ydc_delta = vp8_rac_get_sint(c, 4);
  268. int y2dc_delta = vp8_rac_get_sint(c, 4);
  269. int y2ac_delta = vp8_rac_get_sint(c, 4);
  270. int uvdc_delta = vp8_rac_get_sint(c, 4);
  271. int uvac_delta = vp8_rac_get_sint(c, 4);
  272. for (i = 0; i < 4; i++) {
  273. if (s->segmentation.enabled) {
  274. base_qi = s->segmentation.base_quant[i];
  275. if (!s->segmentation.absolute_vals)
  276. base_qi += yac_qi;
  277. } else
  278. base_qi = yac_qi;
  279. s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
  280. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi , 0, 127)];
  281. s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
  282. s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
  283. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
  284. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
  285. s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
  286. s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
  287. }
  288. }
  289. /**
  290. * Determine which buffers golden and altref should be updated with after this frame.
  291. * The spec isn't clear here, so I'm going by my understanding of what libvpx does
  292. *
  293. * Intra frames update all 3 references
  294. * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
  295. * If the update (golden|altref) flag is set, it's updated with the current frame
  296. * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
  297. * If the flag is not set, the number read means:
  298. * 0: no update
  299. * 1: VP56_FRAME_PREVIOUS
  300. * 2: update golden with altref, or update altref with golden
  301. */
  302. static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
  303. {
  304. VP56RangeCoder *c = &s->c;
  305. if (update)
  306. return VP56_FRAME_CURRENT;
  307. switch (vp8_rac_get_uint(c, 2)) {
  308. case 1:
  309. return VP56_FRAME_PREVIOUS;
  310. case 2:
  311. return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
  312. }
  313. return VP56_FRAME_NONE;
  314. }
  315. static void update_refs(VP8Context *s)
  316. {
  317. VP56RangeCoder *c = &s->c;
  318. int update_golden = vp8_rac_get(c);
  319. int update_altref = vp8_rac_get(c);
  320. s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
  321. s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
  322. }
  323. static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  324. {
  325. VP56RangeCoder *c = &s->c;
  326. int header_size, hscale, vscale, i, j, k, l, m, ret;
  327. int width = s->avctx->width;
  328. int height = s->avctx->height;
  329. s->keyframe = !(buf[0] & 1);
  330. s->profile = (buf[0]>>1) & 7;
  331. s->invisible = !(buf[0] & 0x10);
  332. header_size = AV_RL24(buf) >> 5;
  333. buf += 3;
  334. buf_size -= 3;
  335. if (s->profile > 3)
  336. av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
  337. if (!s->profile)
  338. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
  339. else // profile 1-3 use bilinear, 4+ aren't defined so whatever
  340. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
  341. if (header_size > buf_size - 7*s->keyframe) {
  342. av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
  343. return AVERROR_INVALIDDATA;
  344. }
  345. if (s->keyframe) {
  346. if (AV_RL24(buf) != 0x2a019d) {
  347. av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
  348. return AVERROR_INVALIDDATA;
  349. }
  350. width = AV_RL16(buf+3) & 0x3fff;
  351. height = AV_RL16(buf+5) & 0x3fff;
  352. hscale = buf[4] >> 6;
  353. vscale = buf[6] >> 6;
  354. buf += 7;
  355. buf_size -= 7;
  356. if (hscale || vscale)
  357. av_log_missing_feature(s->avctx, "Upscaling", 1);
  358. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  359. for (i = 0; i < 4; i++)
  360. for (j = 0; j < 16; j++)
  361. memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
  362. sizeof(s->prob->token[i][j]));
  363. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
  364. memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
  365. memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
  366. memset(&s->segmentation, 0, sizeof(s->segmentation));
  367. }
  368. if (!s->macroblocks_base || /* first frame */
  369. width != s->avctx->width || height != s->avctx->height) {
  370. if ((ret = update_dimensions(s, width, height) < 0))
  371. return ret;
  372. }
  373. ff_vp56_init_range_decoder(c, buf, header_size);
  374. buf += header_size;
  375. buf_size -= header_size;
  376. if (s->keyframe) {
  377. if (vp8_rac_get(c))
  378. av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
  379. vp8_rac_get(c); // whether we can skip clamping in dsp functions
  380. }
  381. if ((s->segmentation.enabled = vp8_rac_get(c)))
  382. parse_segment_info(s);
  383. else
  384. s->segmentation.update_map = 0; // FIXME: move this to some init function?
  385. s->filter.simple = vp8_rac_get(c);
  386. s->filter.level = vp8_rac_get_uint(c, 6);
  387. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  388. if ((s->lf_delta.enabled = vp8_rac_get(c)))
  389. if (vp8_rac_get(c))
  390. update_lf_deltas(s);
  391. if (setup_partitions(s, buf, buf_size)) {
  392. av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
  393. return AVERROR_INVALIDDATA;
  394. }
  395. get_quants(s);
  396. if (!s->keyframe) {
  397. update_refs(s);
  398. s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
  399. s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
  400. }
  401. // if we aren't saving this frame's probabilities for future frames,
  402. // make a copy of the current probabilities
  403. if (!(s->update_probabilities = vp8_rac_get(c)))
  404. s->prob[1] = s->prob[0];
  405. s->update_last = s->keyframe || vp8_rac_get(c);
  406. for (i = 0; i < 4; i++)
  407. for (j = 0; j < 8; j++)
  408. for (k = 0; k < 3; k++)
  409. for (l = 0; l < NUM_DCT_TOKENS-1; l++)
  410. if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
  411. int prob = vp8_rac_get_uint(c, 8);
  412. for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
  413. s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
  414. }
  415. if ((s->mbskip_enabled = vp8_rac_get(c)))
  416. s->prob->mbskip = vp8_rac_get_uint(c, 8);
  417. if (!s->keyframe) {
  418. s->prob->intra = vp8_rac_get_uint(c, 8);
  419. s->prob->last = vp8_rac_get_uint(c, 8);
  420. s->prob->golden = vp8_rac_get_uint(c, 8);
  421. if (vp8_rac_get(c))
  422. for (i = 0; i < 4; i++)
  423. s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
  424. if (vp8_rac_get(c))
  425. for (i = 0; i < 3; i++)
  426. s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
  427. // 17.2 MV probability update
  428. for (i = 0; i < 2; i++)
  429. for (j = 0; j < 19; j++)
  430. if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
  431. s->prob->mvc[i][j] = vp8_rac_get_nn(c);
  432. }
  433. return 0;
  434. }
  435. static av_always_inline
  436. void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src, int mb_x, int mb_y)
  437. {
  438. #define MARGIN (16 << 2)
  439. dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
  440. ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
  441. dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
  442. ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
  443. }
  444. static av_always_inline
  445. void find_near_mvs(VP8Context *s, VP8Macroblock *mb,
  446. VP56mv near[2], VP56mv *best, uint8_t cnt[4])
  447. {
  448. VP8Macroblock *mb_edge[3] = { mb + 2 /* top */,
  449. mb - 1 /* left */,
  450. mb + 1 /* top-left */ };
  451. enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
  452. VP56mv near_mv[4] = {{ 0 }};
  453. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  454. int idx = CNT_ZERO;
  455. int best_idx = CNT_ZERO;
  456. int cur_sign_bias = s->sign_bias[mb->ref_frame];
  457. int *sign_bias = s->sign_bias;
  458. /* Process MB on top, left and top-left */
  459. #define MV_EDGE_CHECK(n)\
  460. {\
  461. VP8Macroblock *edge = mb_edge[n];\
  462. int edge_ref = edge->ref_frame;\
  463. if (edge_ref != VP56_FRAME_CURRENT) {\
  464. uint32_t mv = AV_RN32A(&edge->mv);\
  465. if (mv) {\
  466. if (cur_sign_bias != sign_bias[edge_ref]) {\
  467. /* SWAR negate of the values in mv. */\
  468. mv = ~mv;\
  469. mv = ((mv&0x7fff7fff) + 0x00010001) ^ (mv&0x80008000);\
  470. }\
  471. if (!n || mv != AV_RN32A(&near_mv[idx]))\
  472. AV_WN32A(&near_mv[++idx], mv);\
  473. cnt[idx] += 1 + (n != 2);\
  474. } else\
  475. cnt[CNT_ZERO] += 1 + (n != 2);\
  476. }\
  477. }
  478. MV_EDGE_CHECK(0)
  479. MV_EDGE_CHECK(1)
  480. MV_EDGE_CHECK(2)
  481. /* If we have three distinct MVs, merge first and last if they're the same */
  482. if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT]))
  483. cnt[CNT_NEAREST] += 1;
  484. cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
  485. (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
  486. (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
  487. /* Swap near and nearest if necessary */
  488. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  489. FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
  490. FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
  491. }
  492. /* Choose the best mv out of 0,0 and the nearest mv */
  493. if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
  494. best_idx = CNT_NEAREST;
  495. mb->mv = near_mv[best_idx];
  496. near[0] = near_mv[CNT_NEAREST];
  497. near[1] = near_mv[CNT_NEAR];
  498. }
  499. /**
  500. * Motion vector coding, 17.1.
  501. */
  502. static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
  503. {
  504. int bit, x = 0;
  505. if (vp56_rac_get_prob_branchy(c, p[0])) {
  506. int i;
  507. for (i = 0; i < 3; i++)
  508. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  509. for (i = 9; i > 3; i--)
  510. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  511. if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
  512. x += 8;
  513. } else {
  514. // small_mvtree
  515. const uint8_t *ps = p+2;
  516. bit = vp56_rac_get_prob(c, *ps);
  517. ps += 1 + 3*bit;
  518. x += 4*bit;
  519. bit = vp56_rac_get_prob(c, *ps);
  520. ps += 1 + bit;
  521. x += 2*bit;
  522. x += vp56_rac_get_prob(c, *ps);
  523. }
  524. return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
  525. }
  526. static av_always_inline
  527. const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
  528. {
  529. if (left == top)
  530. return vp8_submv_prob[4-!!left];
  531. if (!top)
  532. return vp8_submv_prob[2];
  533. return vp8_submv_prob[1-!!left];
  534. }
  535. /**
  536. * Split motion vector prediction, 16.4.
  537. * @returns the number of motion vectors parsed (2, 4 or 16)
  538. */
  539. static av_always_inline
  540. int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb)
  541. {
  542. int part_idx;
  543. int n, num;
  544. VP8Macroblock *top_mb = &mb[2];
  545. VP8Macroblock *left_mb = &mb[-1];
  546. const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
  547. *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
  548. *mbsplits_cur, *firstidx;
  549. VP56mv *top_mv = top_mb->bmv;
  550. VP56mv *left_mv = left_mb->bmv;
  551. VP56mv *cur_mv = mb->bmv;
  552. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
  553. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1])) {
  554. part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
  555. } else {
  556. part_idx = VP8_SPLITMVMODE_8x8;
  557. }
  558. } else {
  559. part_idx = VP8_SPLITMVMODE_4x4;
  560. }
  561. num = vp8_mbsplit_count[part_idx];
  562. mbsplits_cur = vp8_mbsplits[part_idx],
  563. firstidx = vp8_mbfirstidx[part_idx];
  564. mb->partitioning = part_idx;
  565. for (n = 0; n < num; n++) {
  566. int k = firstidx[n];
  567. uint32_t left, above;
  568. const uint8_t *submv_prob;
  569. if (!(k & 3))
  570. left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
  571. else
  572. left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
  573. if (k <= 3)
  574. above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
  575. else
  576. above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
  577. submv_prob = get_submv_prob(left, above);
  578. if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
  579. if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
  580. if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
  581. mb->bmv[n].y = mb->mv.y + read_mv_component(c, s->prob->mvc[0]);
  582. mb->bmv[n].x = mb->mv.x + read_mv_component(c, s->prob->mvc[1]);
  583. } else {
  584. AV_ZERO32(&mb->bmv[n]);
  585. }
  586. } else {
  587. AV_WN32A(&mb->bmv[n], above);
  588. }
  589. } else {
  590. AV_WN32A(&mb->bmv[n], left);
  591. }
  592. }
  593. return num;
  594. }
  595. static av_always_inline
  596. void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c,
  597. int mb_x, int keyframe)
  598. {
  599. uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
  600. if (keyframe) {
  601. int x, y;
  602. uint8_t* const top = s->intra4x4_pred_mode_top + 4 * mb_x;
  603. uint8_t* const left = s->intra4x4_pred_mode_left;
  604. for (y = 0; y < 4; y++) {
  605. for (x = 0; x < 4; x++) {
  606. const uint8_t *ctx;
  607. ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
  608. *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
  609. left[y] = top[x] = *intra4x4;
  610. intra4x4++;
  611. }
  612. }
  613. } else {
  614. int i;
  615. for (i = 0; i < 16; i++)
  616. intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
  617. }
  618. }
  619. static av_always_inline
  620. void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, uint8_t *segment)
  621. {
  622. VP56RangeCoder *c = &s->c;
  623. if (s->segmentation.update_map)
  624. *segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
  625. s->segment = *segment;
  626. mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
  627. if (s->keyframe) {
  628. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
  629. if (mb->mode == MODE_I4x4) {
  630. decode_intra4x4_modes(s, c, mb_x, 1);
  631. } else {
  632. const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
  633. AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
  634. AV_WN32A(s->intra4x4_pred_mode_left, modes);
  635. }
  636. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
  637. mb->ref_frame = VP56_FRAME_CURRENT;
  638. } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
  639. VP56mv near[2], best;
  640. uint8_t cnt[4] = { 0 };
  641. // inter MB, 16.2
  642. if (vp56_rac_get_prob_branchy(c, s->prob->last))
  643. mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
  644. VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
  645. else
  646. mb->ref_frame = VP56_FRAME_PREVIOUS;
  647. s->ref_count[mb->ref_frame-1]++;
  648. // motion vectors, 16.3
  649. find_near_mvs(s, mb, near, &best, cnt);
  650. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[0]][0])) {
  651. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[1]][1])) {
  652. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[2]][2])) {
  653. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[3]][3])) {
  654. mb->mode = VP8_MVMODE_SPLIT;
  655. clamp_mv(s, &mb->mv, &mb->mv, mb_x, mb_y);
  656. mb->mv = mb->bmv[decode_splitmvs(s, c, mb) - 1];
  657. } else {
  658. mb->mode = VP8_MVMODE_NEW;
  659. clamp_mv(s, &mb->mv, &mb->mv, mb_x, mb_y);
  660. mb->mv.y += read_mv_component(c, s->prob->mvc[0]);
  661. mb->mv.x += read_mv_component(c, s->prob->mvc[1]);
  662. }
  663. } else {
  664. mb->mode = VP8_MVMODE_NEAR;
  665. clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
  666. }
  667. } else {
  668. mb->mode = VP8_MVMODE_NEAREST;
  669. clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
  670. }
  671. } else {
  672. mb->mode = VP8_MVMODE_ZERO;
  673. AV_ZERO32(&mb->mv);
  674. }
  675. if (mb->mode != VP8_MVMODE_SPLIT) {
  676. mb->partitioning = VP8_SPLITMVMODE_NONE;
  677. mb->bmv[0] = mb->mv;
  678. }
  679. } else {
  680. // intra MB, 16.1
  681. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
  682. if (mb->mode == MODE_I4x4)
  683. decode_intra4x4_modes(s, c, mb_x, 0);
  684. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
  685. mb->ref_frame = VP56_FRAME_CURRENT;
  686. mb->partitioning = VP8_SPLITMVMODE_NONE;
  687. AV_ZERO32(&mb->bmv[0]);
  688. }
  689. }
  690. /**
  691. * @param c arithmetic bitstream reader context
  692. * @param block destination for block coefficients
  693. * @param probs probabilities to use when reading trees from the bitstream
  694. * @param i initial coeff index, 0 unless a separate DC block is coded
  695. * @param zero_nhood the initial prediction context for number of surrounding
  696. * all-zero blocks (only left/top, so 0-2)
  697. * @param qmul array holding the dc/ac dequant factor at position 0/1
  698. * @return 0 if no coeffs were decoded
  699. * otherwise, the index of the last coeff decoded plus one
  700. */
  701. static int decode_block_coeffs_internal(VP56RangeCoder *c, DCTELEM block[16],
  702. uint8_t probs[8][3][NUM_DCT_TOKENS-1],
  703. int i, uint8_t *token_prob, int16_t qmul[2])
  704. {
  705. goto skip_eob;
  706. do {
  707. int coeff;
  708. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  709. return i;
  710. skip_eob:
  711. if (!vp56_rac_get_prob_branchy(c, token_prob[1])) { // DCT_0
  712. if (++i == 16)
  713. return i; // invalid input; blocks should end with EOB
  714. token_prob = probs[i][0];
  715. goto skip_eob;
  716. }
  717. if (!vp56_rac_get_prob_branchy(c, token_prob[2])) { // DCT_1
  718. coeff = 1;
  719. token_prob = probs[i+1][1];
  720. } else {
  721. if (!vp56_rac_get_prob_branchy(c, token_prob[3])) { // DCT 2,3,4
  722. coeff = vp56_rac_get_prob_branchy(c, token_prob[4]);
  723. if (coeff)
  724. coeff += vp56_rac_get_prob(c, token_prob[5]);
  725. coeff += 2;
  726. } else {
  727. // DCT_CAT*
  728. if (!vp56_rac_get_prob_branchy(c, token_prob[6])) {
  729. if (!vp56_rac_get_prob_branchy(c, token_prob[7])) { // DCT_CAT1
  730. coeff = 5 + vp56_rac_get_prob(c, vp8_dct_cat1_prob[0]);
  731. } else { // DCT_CAT2
  732. coeff = 7;
  733. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[0]) << 1;
  734. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[1]);
  735. }
  736. } else { // DCT_CAT3 and up
  737. int a = vp56_rac_get_prob(c, token_prob[8]);
  738. int b = vp56_rac_get_prob(c, token_prob[9+a]);
  739. int cat = (a<<1) + b;
  740. coeff = 3 + (8<<cat);
  741. coeff += vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
  742. }
  743. }
  744. token_prob = probs[i+1][2];
  745. }
  746. block[zigzag_scan[i]] = (vp8_rac_get(c) ? -coeff : coeff) * qmul[!!i];
  747. } while (++i < 16);
  748. return i;
  749. }
  750. static av_always_inline
  751. int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
  752. uint8_t probs[8][3][NUM_DCT_TOKENS-1],
  753. int i, int zero_nhood, int16_t qmul[2])
  754. {
  755. uint8_t *token_prob = probs[i][zero_nhood];
  756. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  757. return 0;
  758. return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
  759. }
  760. static av_always_inline
  761. void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  762. uint8_t t_nnz[9], uint8_t l_nnz[9])
  763. {
  764. int i, x, y, luma_start = 0, luma_ctx = 3;
  765. int nnz_pred, nnz, nnz_total = 0;
  766. int segment = s->segment;
  767. int block_dc = 0;
  768. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  769. nnz_pred = t_nnz[8] + l_nnz[8];
  770. // decode DC values and do hadamard
  771. nnz = decode_block_coeffs(c, s->block_dc, s->prob->token[1], 0, nnz_pred,
  772. s->qmat[segment].luma_dc_qmul);
  773. l_nnz[8] = t_nnz[8] = !!nnz;
  774. if (nnz) {
  775. nnz_total += nnz;
  776. block_dc = 1;
  777. if (nnz == 1)
  778. s->vp8dsp.vp8_luma_dc_wht_dc(s->block, s->block_dc);
  779. else
  780. s->vp8dsp.vp8_luma_dc_wht(s->block, s->block_dc);
  781. }
  782. luma_start = 1;
  783. luma_ctx = 0;
  784. }
  785. // luma blocks
  786. for (y = 0; y < 4; y++)
  787. for (x = 0; x < 4; x++) {
  788. nnz_pred = l_nnz[y] + t_nnz[x];
  789. nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
  790. nnz_pred, s->qmat[segment].luma_qmul);
  791. // nnz+block_dc may be one more than the actual last index, but we don't care
  792. s->non_zero_count_cache[y][x] = nnz + block_dc;
  793. t_nnz[x] = l_nnz[y] = !!nnz;
  794. nnz_total += nnz;
  795. }
  796. // chroma blocks
  797. // TODO: what to do about dimensions? 2nd dim for luma is x,
  798. // but for chroma it's (y<<1)|x
  799. for (i = 4; i < 6; i++)
  800. for (y = 0; y < 2; y++)
  801. for (x = 0; x < 2; x++) {
  802. nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
  803. nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
  804. nnz_pred, s->qmat[segment].chroma_qmul);
  805. s->non_zero_count_cache[i][(y<<1)+x] = nnz;
  806. t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
  807. nnz_total += nnz;
  808. }
  809. // if there were no coded coeffs despite the macroblock not being marked skip,
  810. // we MUST not do the inner loop filter and should not do IDCT
  811. // Since skip isn't used for bitstream prediction, just manually set it.
  812. if (!nnz_total)
  813. mb->skip = 1;
  814. }
  815. static av_always_inline
  816. void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
  817. int linesize, int uvlinesize, int simple)
  818. {
  819. AV_COPY128(top_border, src_y + 15*linesize);
  820. if (!simple) {
  821. AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
  822. AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
  823. }
  824. }
  825. static av_always_inline
  826. void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
  827. int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
  828. int simple, int xchg)
  829. {
  830. uint8_t *top_border_m1 = top_border-32; // for TL prediction
  831. src_y -= linesize;
  832. src_cb -= uvlinesize;
  833. src_cr -= uvlinesize;
  834. #define XCHG(a,b,xchg) do { \
  835. if (xchg) AV_SWAP64(b,a); \
  836. else AV_COPY64(b,a); \
  837. } while (0)
  838. XCHG(top_border_m1+8, src_y-8, xchg);
  839. XCHG(top_border, src_y, xchg);
  840. XCHG(top_border+8, src_y+8, 1);
  841. if (mb_x < mb_width-1)
  842. XCHG(top_border+32, src_y+16, 1);
  843. // only copy chroma for normal loop filter
  844. // or to initialize the top row to 127
  845. if (!simple || !mb_y) {
  846. XCHG(top_border_m1+16, src_cb-8, xchg);
  847. XCHG(top_border_m1+24, src_cr-8, xchg);
  848. XCHG(top_border+16, src_cb, 1);
  849. XCHG(top_border+24, src_cr, 1);
  850. }
  851. }
  852. static av_always_inline
  853. int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
  854. {
  855. if (!mb_x) {
  856. return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
  857. } else {
  858. return mb_y ? mode : LEFT_DC_PRED8x8;
  859. }
  860. }
  861. static av_always_inline
  862. int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
  863. {
  864. if (!mb_x) {
  865. return mb_y ? VERT_PRED8x8 : DC_129_PRED8x8;
  866. } else {
  867. return mb_y ? mode : HOR_PRED8x8;
  868. }
  869. }
  870. static av_always_inline
  871. int check_intra_pred8x8_mode(int mode, int mb_x, int mb_y)
  872. {
  873. if (mode == DC_PRED8x8) {
  874. return check_dc_pred8x8_mode(mode, mb_x, mb_y);
  875. } else {
  876. return mode;
  877. }
  878. }
  879. static av_always_inline
  880. int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
  881. {
  882. switch (mode) {
  883. case DC_PRED8x8:
  884. return check_dc_pred8x8_mode(mode, mb_x, mb_y);
  885. case VERT_PRED8x8:
  886. return !mb_y ? DC_127_PRED8x8 : mode;
  887. case HOR_PRED8x8:
  888. return !mb_x ? DC_129_PRED8x8 : mode;
  889. case PLANE_PRED8x8 /*TM*/:
  890. return check_tm_pred8x8_mode(mode, mb_x, mb_y);
  891. }
  892. return mode;
  893. }
  894. static av_always_inline
  895. int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
  896. {
  897. if (!mb_x) {
  898. return mb_y ? VERT_VP8_PRED : DC_129_PRED;
  899. } else {
  900. return mb_y ? mode : HOR_VP8_PRED;
  901. }
  902. }
  903. static av_always_inline
  904. int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
  905. {
  906. switch (mode) {
  907. case VERT_PRED:
  908. if (!mb_x && mb_y) {
  909. *copy_buf = 1;
  910. return mode;
  911. }
  912. /* fall-through */
  913. case DIAG_DOWN_LEFT_PRED:
  914. case VERT_LEFT_PRED:
  915. return !mb_y ? DC_127_PRED : mode;
  916. case HOR_PRED:
  917. if (!mb_y) {
  918. *copy_buf = 1;
  919. return mode;
  920. }
  921. /* fall-through */
  922. case HOR_UP_PRED:
  923. return !mb_x ? DC_129_PRED : mode;
  924. case TM_VP8_PRED:
  925. return check_tm_pred4x4_mode(mode, mb_x, mb_y);
  926. case DC_PRED: // 4x4 DC doesn't use the same "H.264-style" exceptions as 16x16/8x8 DC
  927. case DIAG_DOWN_RIGHT_PRED:
  928. case VERT_RIGHT_PRED:
  929. case HOR_DOWN_PRED:
  930. if (!mb_y || !mb_x)
  931. *copy_buf = 1;
  932. return mode;
  933. }
  934. return mode;
  935. }
  936. static av_always_inline
  937. void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  938. int mb_x, int mb_y)
  939. {
  940. AVCodecContext *avctx = s->avctx;
  941. int x, y, mode, nnz, tr;
  942. // for the first row, we need to run xchg_mb_border to init the top edge to 127
  943. // otherwise, skip it if we aren't going to deblock
  944. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y))
  945. xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
  946. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  947. s->filter.simple, 1);
  948. if (mb->mode < MODE_I4x4) {
  949. if (avctx->flags & CODEC_FLAG_EMU_EDGE) { // tested
  950. mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y);
  951. } else {
  952. mode = check_intra_pred8x8_mode(mb->mode, mb_x, mb_y);
  953. }
  954. s->hpc.pred16x16[mode](dst[0], s->linesize);
  955. } else {
  956. uint8_t *ptr = dst[0];
  957. uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
  958. uint8_t tr_top[4] = { 127, 127, 127, 127 };
  959. // all blocks on the right edge of the macroblock use bottom edge
  960. // the top macroblock for their topright edge
  961. uint8_t *tr_right = ptr - s->linesize + 16;
  962. // if we're on the right edge of the frame, said edge is extended
  963. // from the top macroblock
  964. if (!(!mb_y && avctx->flags & CODEC_FLAG_EMU_EDGE) &&
  965. mb_x == s->mb_width-1) {
  966. tr = tr_right[-1]*0x01010101;
  967. tr_right = (uint8_t *)&tr;
  968. }
  969. if (mb->skip)
  970. AV_ZERO128(s->non_zero_count_cache);
  971. for (y = 0; y < 4; y++) {
  972. uint8_t *topright = ptr + 4 - s->linesize;
  973. for (x = 0; x < 4; x++) {
  974. int copy = 0, linesize = s->linesize;
  975. uint8_t *dst = ptr+4*x;
  976. DECLARE_ALIGNED(4, uint8_t, copy_dst)[5*8];
  977. if ((y == 0 || x == 3) && mb_y == 0 && avctx->flags & CODEC_FLAG_EMU_EDGE) {
  978. topright = tr_top;
  979. } else if (x == 3)
  980. topright = tr_right;
  981. if (avctx->flags & CODEC_FLAG_EMU_EDGE) { // mb_x+x or mb_y+y is a hack but works
  982. mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x, mb_y + y, &copy);
  983. if (copy) {
  984. dst = copy_dst + 12;
  985. linesize = 8;
  986. if (!(mb_y + y)) {
  987. copy_dst[3] = 127U;
  988. * (uint32_t *) (copy_dst + 4) = 127U * 0x01010101U;
  989. } else {
  990. * (uint32_t *) (copy_dst + 4) = * (uint32_t *) (ptr+4*x-s->linesize);
  991. if (!(mb_x + x)) {
  992. copy_dst[3] = 129U;
  993. } else {
  994. copy_dst[3] = ptr[4*x-s->linesize-1];
  995. }
  996. }
  997. if (!(mb_x + x)) {
  998. copy_dst[11] =
  999. copy_dst[19] =
  1000. copy_dst[27] =
  1001. copy_dst[35] = 129U;
  1002. } else {
  1003. copy_dst[11] = ptr[4*x -1];
  1004. copy_dst[19] = ptr[4*x+s->linesize -1];
  1005. copy_dst[27] = ptr[4*x+s->linesize*2-1];
  1006. copy_dst[35] = ptr[4*x+s->linesize*3-1];
  1007. }
  1008. }
  1009. } else {
  1010. mode = intra4x4[x];
  1011. }
  1012. s->hpc.pred4x4[mode](dst, topright, linesize);
  1013. if (copy) {
  1014. * (uint32_t *) (ptr+4*x) = * (uint32_t *) (copy_dst + 12);
  1015. * (uint32_t *) (ptr+4*x+s->linesize) = * (uint32_t *) (copy_dst + 20);
  1016. * (uint32_t *) (ptr+4*x+s->linesize*2) = * (uint32_t *) (copy_dst + 28);
  1017. * (uint32_t *) (ptr+4*x+s->linesize*3) = * (uint32_t *) (copy_dst + 36);
  1018. }
  1019. nnz = s->non_zero_count_cache[y][x];
  1020. if (nnz) {
  1021. if (nnz == 1)
  1022. s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
  1023. else
  1024. s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
  1025. }
  1026. topright += 4;
  1027. }
  1028. ptr += 4*s->linesize;
  1029. intra4x4 += 4;
  1030. }
  1031. }
  1032. if (avctx->flags & CODEC_FLAG_EMU_EDGE) {
  1033. mode = check_intra_pred8x8_mode_emuedge(s->chroma_pred_mode, mb_x, mb_y);
  1034. } else {
  1035. mode = check_intra_pred8x8_mode(s->chroma_pred_mode, mb_x, mb_y);
  1036. }
  1037. s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
  1038. s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
  1039. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y))
  1040. xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
  1041. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  1042. s->filter.simple, 0);
  1043. }
  1044. /**
  1045. * Generic MC function.
  1046. *
  1047. * @param s VP8 decoding context
  1048. * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
  1049. * @param dst target buffer for block data at block position
  1050. * @param src reference picture buffer at origin (0, 0)
  1051. * @param mv motion vector (relative to block position) to get pixel data from
  1052. * @param x_off horizontal position of block from origin (0, 0)
  1053. * @param y_off vertical position of block from origin (0, 0)
  1054. * @param block_w width of block (16, 8 or 4)
  1055. * @param block_h height of block (always same as block_w)
  1056. * @param width width of src/dst plane data
  1057. * @param height height of src/dst plane data
  1058. * @param linesize size of a single line of plane data, including padding
  1059. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  1060. */
  1061. static av_always_inline
  1062. void vp8_mc(VP8Context *s, int luma,
  1063. uint8_t *dst, uint8_t *src, const VP56mv *mv,
  1064. int x_off, int y_off, int block_w, int block_h,
  1065. int width, int height, int linesize,
  1066. vp8_mc_func mc_func[3][3])
  1067. {
  1068. if (AV_RN32A(mv)) {
  1069. static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
  1070. int mx = (mv->x << luma)&7, mx_idx = idx[mx];
  1071. int my = (mv->y << luma)&7, my_idx = idx[my];
  1072. x_off += mv->x >> (3 - luma);
  1073. y_off += mv->y >> (3 - luma);
  1074. // edge emulation
  1075. src += y_off * linesize + x_off;
  1076. if (x_off < 2 || x_off >= width - block_w - 3 ||
  1077. y_off < 2 || y_off >= height - block_h - 3) {
  1078. ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
  1079. block_w + 5, block_h + 5,
  1080. x_off - 2, y_off - 2, width, height);
  1081. src = s->edge_emu_buffer + 2 + linesize * 2;
  1082. }
  1083. mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
  1084. } else
  1085. mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1086. }
  1087. static av_always_inline
  1088. void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
  1089. AVFrame *ref_frame, int x_off, int y_off,
  1090. int bx_off, int by_off,
  1091. int block_w, int block_h,
  1092. int width, int height, VP56mv *mv)
  1093. {
  1094. VP56mv uvmv = *mv;
  1095. /* Y */
  1096. vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,
  1097. ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
  1098. block_w, block_h, width, height, s->linesize,
  1099. s->put_pixels_tab[block_w == 8]);
  1100. /* U/V */
  1101. if (s->profile == 3) {
  1102. uvmv.x &= ~7;
  1103. uvmv.y &= ~7;
  1104. }
  1105. x_off >>= 1; y_off >>= 1;
  1106. bx_off >>= 1; by_off >>= 1;
  1107. width >>= 1; height >>= 1;
  1108. block_w >>= 1; block_h >>= 1;
  1109. vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,
  1110. ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,
  1111. block_w, block_h, width, height, s->uvlinesize,
  1112. s->put_pixels_tab[1 + (block_w == 4)]);
  1113. vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,
  1114. ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
  1115. block_w, block_h, width, height, s->uvlinesize,
  1116. s->put_pixels_tab[1 + (block_w == 4)]);
  1117. }
  1118. /* Fetch pixels for estimated mv 4 macroblocks ahead.
  1119. * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
  1120. static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
  1121. {
  1122. /* Don't prefetch refs that haven't been used very often this frame. */
  1123. if (s->ref_count[ref-1] > (mb_xy >> 5)) {
  1124. int x_off = mb_x << 4, y_off = mb_y << 4;
  1125. int mx = (mb->mv.x>>2) + x_off + 8;
  1126. int my = (mb->mv.y>>2) + y_off;
  1127. uint8_t **src= s->framep[ref]->data;
  1128. int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
  1129. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1130. off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
  1131. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1132. }
  1133. }
  1134. /**
  1135. * Apply motion vectors to prediction buffer, chapter 18.
  1136. */
  1137. static av_always_inline
  1138. void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  1139. int mb_x, int mb_y)
  1140. {
  1141. int x_off = mb_x << 4, y_off = mb_y << 4;
  1142. int width = 16*s->mb_width, height = 16*s->mb_height;
  1143. AVFrame *ref = s->framep[mb->ref_frame];
  1144. VP56mv *bmv = mb->bmv;
  1145. if (mb->mode < VP8_MVMODE_SPLIT) {
  1146. vp8_mc_part(s, dst, ref, x_off, y_off,
  1147. 0, 0, 16, 16, width, height, &mb->mv);
  1148. } else switch (mb->partitioning) {
  1149. case VP8_SPLITMVMODE_4x4: {
  1150. int x, y;
  1151. VP56mv uvmv;
  1152. /* Y */
  1153. for (y = 0; y < 4; y++) {
  1154. for (x = 0; x < 4; x++) {
  1155. vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
  1156. ref->data[0], &bmv[4*y + x],
  1157. 4*x + x_off, 4*y + y_off, 4, 4,
  1158. width, height, s->linesize,
  1159. s->put_pixels_tab[2]);
  1160. }
  1161. }
  1162. /* U/V */
  1163. x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
  1164. for (y = 0; y < 2; y++) {
  1165. for (x = 0; x < 2; x++) {
  1166. uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
  1167. mb->bmv[ 2*y * 4 + 2*x+1].x +
  1168. mb->bmv[(2*y+1) * 4 + 2*x ].x +
  1169. mb->bmv[(2*y+1) * 4 + 2*x+1].x;
  1170. uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
  1171. mb->bmv[ 2*y * 4 + 2*x+1].y +
  1172. mb->bmv[(2*y+1) * 4 + 2*x ].y +
  1173. mb->bmv[(2*y+1) * 4 + 2*x+1].y;
  1174. uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
  1175. uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
  1176. if (s->profile == 3) {
  1177. uvmv.x &= ~7;
  1178. uvmv.y &= ~7;
  1179. }
  1180. vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
  1181. ref->data[1], &uvmv,
  1182. 4*x + x_off, 4*y + y_off, 4, 4,
  1183. width, height, s->uvlinesize,
  1184. s->put_pixels_tab[2]);
  1185. vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
  1186. ref->data[2], &uvmv,
  1187. 4*x + x_off, 4*y + y_off, 4, 4,
  1188. width, height, s->uvlinesize,
  1189. s->put_pixels_tab[2]);
  1190. }
  1191. }
  1192. break;
  1193. }
  1194. case VP8_SPLITMVMODE_16x8:
  1195. vp8_mc_part(s, dst, ref, x_off, y_off,
  1196. 0, 0, 16, 8, width, height, &bmv[0]);
  1197. vp8_mc_part(s, dst, ref, x_off, y_off,
  1198. 0, 8, 16, 8, width, height, &bmv[1]);
  1199. break;
  1200. case VP8_SPLITMVMODE_8x16:
  1201. vp8_mc_part(s, dst, ref, x_off, y_off,
  1202. 0, 0, 8, 16, width, height, &bmv[0]);
  1203. vp8_mc_part(s, dst, ref, x_off, y_off,
  1204. 8, 0, 8, 16, width, height, &bmv[1]);
  1205. break;
  1206. case VP8_SPLITMVMODE_8x8:
  1207. vp8_mc_part(s, dst, ref, x_off, y_off,
  1208. 0, 0, 8, 8, width, height, &bmv[0]);
  1209. vp8_mc_part(s, dst, ref, x_off, y_off,
  1210. 8, 0, 8, 8, width, height, &bmv[1]);
  1211. vp8_mc_part(s, dst, ref, x_off, y_off,
  1212. 0, 8, 8, 8, width, height, &bmv[2]);
  1213. vp8_mc_part(s, dst, ref, x_off, y_off,
  1214. 8, 8, 8, 8, width, height, &bmv[3]);
  1215. break;
  1216. }
  1217. }
  1218. static av_always_inline void idct_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb)
  1219. {
  1220. int x, y, ch;
  1221. if (mb->mode != MODE_I4x4) {
  1222. uint8_t *y_dst = dst[0];
  1223. for (y = 0; y < 4; y++) {
  1224. uint32_t nnz4 = AV_RN32A(s->non_zero_count_cache[y]);
  1225. if (nnz4) {
  1226. if (nnz4&~0x01010101) {
  1227. for (x = 0; x < 4; x++) {
  1228. int nnz = s->non_zero_count_cache[y][x];
  1229. if (nnz) {
  1230. if (nnz == 1)
  1231. s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
  1232. else
  1233. s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
  1234. }
  1235. }
  1236. } else {
  1237. s->vp8dsp.vp8_idct_dc_add4y(y_dst, s->block[y], s->linesize);
  1238. }
  1239. }
  1240. y_dst += 4*s->linesize;
  1241. }
  1242. }
  1243. for (ch = 0; ch < 2; ch++) {
  1244. uint32_t nnz4 = AV_RN32A(s->non_zero_count_cache[4+ch]);
  1245. if (nnz4) {
  1246. uint8_t *ch_dst = dst[1+ch];
  1247. if (nnz4&~0x01010101) {
  1248. for (y = 0; y < 2; y++) {
  1249. for (x = 0; x < 2; x++) {
  1250. int nnz = s->non_zero_count_cache[4+ch][(y<<1)+x];
  1251. if (nnz) {
  1252. if (nnz == 1)
  1253. s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1254. else
  1255. s->vp8dsp.vp8_idct_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1256. }
  1257. }
  1258. ch_dst += 4*s->uvlinesize;
  1259. }
  1260. } else {
  1261. s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, s->block[4+ch], s->uvlinesize);
  1262. }
  1263. }
  1264. }
  1265. }
  1266. static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
  1267. {
  1268. int interior_limit, filter_level;
  1269. if (s->segmentation.enabled) {
  1270. filter_level = s->segmentation.filter_level[s->segment];
  1271. if (!s->segmentation.absolute_vals)
  1272. filter_level += s->filter.level;
  1273. } else
  1274. filter_level = s->filter.level;
  1275. if (s->lf_delta.enabled) {
  1276. filter_level += s->lf_delta.ref[mb->ref_frame];
  1277. if (mb->ref_frame == VP56_FRAME_CURRENT) {
  1278. if (mb->mode == MODE_I4x4)
  1279. filter_level += s->lf_delta.mode[0];
  1280. } else {
  1281. if (mb->mode == VP8_MVMODE_ZERO)
  1282. filter_level += s->lf_delta.mode[1];
  1283. else if (mb->mode == VP8_MVMODE_SPLIT)
  1284. filter_level += s->lf_delta.mode[3];
  1285. else
  1286. filter_level += s->lf_delta.mode[2];
  1287. }
  1288. }
  1289. filter_level = av_clip(filter_level, 0, 63);
  1290. interior_limit = filter_level;
  1291. if (s->filter.sharpness) {
  1292. interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
  1293. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  1294. }
  1295. interior_limit = FFMAX(interior_limit, 1);
  1296. f->filter_level = filter_level;
  1297. f->inner_limit = interior_limit;
  1298. f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
  1299. }
  1300. static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
  1301. {
  1302. int mbedge_lim, bedge_lim, hev_thresh;
  1303. int filter_level = f->filter_level;
  1304. int inner_limit = f->inner_limit;
  1305. int inner_filter = f->inner_filter;
  1306. int linesize = s->linesize;
  1307. int uvlinesize = s->uvlinesize;
  1308. if (!filter_level)
  1309. return;
  1310. mbedge_lim = 2*(filter_level+2) + inner_limit;
  1311. bedge_lim = 2* filter_level + inner_limit;
  1312. hev_thresh = filter_level >= 15;
  1313. if (s->keyframe) {
  1314. if (filter_level >= 40)
  1315. hev_thresh = 2;
  1316. } else {
  1317. if (filter_level >= 40)
  1318. hev_thresh = 3;
  1319. else if (filter_level >= 20)
  1320. hev_thresh = 2;
  1321. }
  1322. if (mb_x) {
  1323. s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
  1324. mbedge_lim, inner_limit, hev_thresh);
  1325. s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1326. mbedge_lim, inner_limit, hev_thresh);
  1327. }
  1328. if (inner_filter) {
  1329. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
  1330. inner_limit, hev_thresh);
  1331. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
  1332. inner_limit, hev_thresh);
  1333. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
  1334. inner_limit, hev_thresh);
  1335. s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
  1336. uvlinesize, bedge_lim,
  1337. inner_limit, hev_thresh);
  1338. }
  1339. if (mb_y) {
  1340. s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
  1341. mbedge_lim, inner_limit, hev_thresh);
  1342. s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1343. mbedge_lim, inner_limit, hev_thresh);
  1344. }
  1345. if (inner_filter) {
  1346. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
  1347. linesize, bedge_lim,
  1348. inner_limit, hev_thresh);
  1349. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
  1350. linesize, bedge_lim,
  1351. inner_limit, hev_thresh);
  1352. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
  1353. linesize, bedge_lim,
  1354. inner_limit, hev_thresh);
  1355. s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
  1356. dst[2] + 4 * uvlinesize,
  1357. uvlinesize, bedge_lim,
  1358. inner_limit, hev_thresh);
  1359. }
  1360. }
  1361. static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
  1362. {
  1363. int mbedge_lim, bedge_lim;
  1364. int filter_level = f->filter_level;
  1365. int inner_limit = f->inner_limit;
  1366. int inner_filter = f->inner_filter;
  1367. int linesize = s->linesize;
  1368. if (!filter_level)
  1369. return;
  1370. mbedge_lim = 2*(filter_level+2) + inner_limit;
  1371. bedge_lim = 2* filter_level + inner_limit;
  1372. if (mb_x)
  1373. s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
  1374. if (inner_filter) {
  1375. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
  1376. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
  1377. s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
  1378. }
  1379. if (mb_y)
  1380. s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
  1381. if (inner_filter) {
  1382. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
  1383. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
  1384. s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
  1385. }
  1386. }
  1387. static void filter_mb_row(VP8Context *s, int mb_y)
  1388. {
  1389. VP8FilterStrength *f = s->filter_strength;
  1390. uint8_t *dst[3] = {
  1391. s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
  1392. s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
  1393. s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
  1394. };
  1395. int mb_x;
  1396. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1397. backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  1398. filter_mb(s, dst, f++, mb_x, mb_y);
  1399. dst[0] += 16;
  1400. dst[1] += 8;
  1401. dst[2] += 8;
  1402. }
  1403. }
  1404. static void filter_mb_row_simple(VP8Context *s, int mb_y)
  1405. {
  1406. VP8FilterStrength *f = s->filter_strength;
  1407. uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
  1408. int mb_x;
  1409. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1410. backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
  1411. filter_mb_simple(s, dst, f++, mb_x, mb_y);
  1412. dst += 16;
  1413. }
  1414. }
  1415. static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  1416. AVPacket *avpkt)
  1417. {
  1418. VP8Context *s = avctx->priv_data;
  1419. int ret, mb_x, mb_y, i, y, referenced;
  1420. enum AVDiscard skip_thresh;
  1421. AVFrame *av_uninit(curframe);
  1422. if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
  1423. return ret;
  1424. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
  1425. || s->update_altref == VP56_FRAME_CURRENT;
  1426. skip_thresh = !referenced ? AVDISCARD_NONREF :
  1427. !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
  1428. if (avctx->skip_frame >= skip_thresh) {
  1429. s->invisible = 1;
  1430. goto skip_decode;
  1431. }
  1432. s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
  1433. for (i = 0; i < 4; i++)
  1434. if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1435. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1436. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  1437. curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
  1438. break;
  1439. }
  1440. if (curframe->data[0])
  1441. avctx->release_buffer(avctx, curframe);
  1442. curframe->key_frame = s->keyframe;
  1443. curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  1444. curframe->reference = referenced ? 3 : 0;
  1445. if ((ret = avctx->get_buffer(avctx, curframe))) {
  1446. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
  1447. return ret;
  1448. }
  1449. // Given that arithmetic probabilities are updated every frame, it's quite likely
  1450. // that the values we have on a random interframe are complete junk if we didn't
  1451. // start decode on a keyframe. So just don't display anything rather than junk.
  1452. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  1453. !s->framep[VP56_FRAME_GOLDEN] ||
  1454. !s->framep[VP56_FRAME_GOLDEN2])) {
  1455. av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
  1456. return AVERROR_INVALIDDATA;
  1457. }
  1458. s->linesize = curframe->linesize[0];
  1459. s->uvlinesize = curframe->linesize[1];
  1460. if (!s->edge_emu_buffer)
  1461. s->edge_emu_buffer = av_malloc(21*s->linesize);
  1462. memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
  1463. /* Zero macroblock structures for top/top-left prediction from outside the frame. */
  1464. memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
  1465. // top edge of 127 for intra prediction
  1466. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1467. s->top_border[0][15] = s->top_border[0][23] = 127;
  1468. memset(s->top_border[1]-1, 127, s->mb_width*sizeof(*s->top_border)+1);
  1469. }
  1470. memset(s->ref_count, 0, sizeof(s->ref_count));
  1471. if (s->keyframe)
  1472. memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
  1473. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1474. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
  1475. VP8Macroblock *mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
  1476. int mb_xy = mb_y*s->mb_width;
  1477. uint8_t *dst[3] = {
  1478. curframe->data[0] + 16*mb_y*s->linesize,
  1479. curframe->data[1] + 8*mb_y*s->uvlinesize,
  1480. curframe->data[2] + 8*mb_y*s->uvlinesize
  1481. };
  1482. memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
  1483. memset(s->left_nnz, 0, sizeof(s->left_nnz));
  1484. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
  1485. // left edge of 129 for intra prediction
  1486. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1487. for (i = 0; i < 3; i++)
  1488. for (y = 0; y < 16>>!!i; y++)
  1489. dst[i][y*curframe->linesize[i]-1] = 129;
  1490. if (mb_y == 1) // top left edge is also 129
  1491. s->top_border[0][15] = s->top_border[0][23] = s->top_border[0][31] = 129;
  1492. }
  1493. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  1494. /* Prefetch the current frame, 4 MBs ahead */
  1495. s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  1496. s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
  1497. decode_mb_mode(s, mb, mb_x, mb_y, s->segmentation_map + mb_xy);
  1498. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
  1499. if (!mb->skip)
  1500. decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
  1501. if (mb->mode <= MODE_I4x4)
  1502. intra_predict(s, dst, mb, mb_x, mb_y);
  1503. else
  1504. inter_predict(s, dst, mb, mb_x, mb_y);
  1505. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
  1506. if (!mb->skip) {
  1507. idct_mb(s, dst, mb);
  1508. } else {
  1509. AV_ZERO64(s->left_nnz);
  1510. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  1511. // Reset DC block predictors if they would exist if the mb had coefficients
  1512. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  1513. s->left_nnz[8] = 0;
  1514. s->top_nnz[mb_x][8] = 0;
  1515. }
  1516. }
  1517. if (s->deblock_filter)
  1518. filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
  1519. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
  1520. dst[0] += 16;
  1521. dst[1] += 8;
  1522. dst[2] += 8;
  1523. }
  1524. if (s->deblock_filter) {
  1525. if (s->filter.simple)
  1526. filter_mb_row_simple(s, mb_y);
  1527. else
  1528. filter_mb_row(s, mb_y);
  1529. }
  1530. }
  1531. skip_decode:
  1532. // if future frames don't use the updated probabilities,
  1533. // reset them to the values we saved
  1534. if (!s->update_probabilities)
  1535. s->prob[0] = s->prob[1];
  1536. // check if golden and altref are swapped
  1537. if (s->update_altref == VP56_FRAME_GOLDEN &&
  1538. s->update_golden == VP56_FRAME_GOLDEN2)
  1539. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
  1540. else {
  1541. if (s->update_altref != VP56_FRAME_NONE)
  1542. s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  1543. if (s->update_golden != VP56_FRAME_NONE)
  1544. s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  1545. }
  1546. if (s->update_last) // move cur->prev
  1547. s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
  1548. // release no longer referenced frames
  1549. for (i = 0; i < 4; i++)
  1550. if (s->frames[i].data[0] &&
  1551. &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
  1552. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1553. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1554. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  1555. avctx->release_buffer(avctx, &s->frames[i]);
  1556. if (!s->invisible) {
  1557. *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
  1558. *data_size = sizeof(AVFrame);
  1559. }
  1560. return avpkt->size;
  1561. }
  1562. static av_cold int vp8_decode_init(AVCodecContext *avctx)
  1563. {
  1564. VP8Context *s = avctx->priv_data;
  1565. s->avctx = avctx;
  1566. avctx->pix_fmt = PIX_FMT_YUV420P;
  1567. dsputil_init(&s->dsp, avctx);
  1568. ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
  1569. ff_vp8dsp_init(&s->vp8dsp);
  1570. return 0;
  1571. }
  1572. static av_cold int vp8_decode_free(AVCodecContext *avctx)
  1573. {
  1574. vp8_decode_flush(avctx);
  1575. return 0;
  1576. }
  1577. AVCodec vp8_decoder = {
  1578. "vp8",
  1579. AVMEDIA_TYPE_VIDEO,
  1580. CODEC_ID_VP8,
  1581. sizeof(VP8Context),
  1582. vp8_decode_init,
  1583. NULL,
  1584. vp8_decode_free,
  1585. vp8_decode_frame,
  1586. CODEC_CAP_DR1,
  1587. .flush = vp8_decode_flush,
  1588. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  1589. };