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.

1847 lines
66KB

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