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.

1853 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 "libavutil/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. #if ARCH_ARM
  32. # include "arm/vp8.h"
  33. #endif
  34. typedef struct {
  35. uint8_t filter_level;
  36. uint8_t inner_limit;
  37. uint8_t inner_filter;
  38. } VP8FilterStrength;
  39. typedef struct {
  40. uint8_t skip;
  41. // todo: make it possible to check for at least (i4x4 or split_mv)
  42. // in one op. are others needed?
  43. uint8_t mode;
  44. uint8_t ref_frame;
  45. uint8_t partitioning;
  46. VP56mv mv;
  47. VP56mv bmv[16];
  48. } VP8Macroblock;
  49. typedef struct {
  50. AVCodecContext *avctx;
  51. DSPContext dsp;
  52. VP8DSPContext vp8dsp;
  53. H264PredContext hpc;
  54. vp8_mc_func put_pixels_tab[3][3][3];
  55. AVFrame frames[4];
  56. AVFrame *framep[4];
  57. uint8_t *edge_emu_buffer;
  58. VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
  59. int profile;
  60. int mb_width; /* number of horizontal MB */
  61. int mb_height; /* number of vertical MB */
  62. int linesize;
  63. int uvlinesize;
  64. int keyframe;
  65. int invisible;
  66. int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
  67. int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
  68. int update_altref;
  69. int deblock_filter;
  70. /**
  71. * If this flag is not set, all the probability updates
  72. * are discarded after this frame is decoded.
  73. */
  74. int update_probabilities;
  75. /**
  76. * All coefficients are contained in separate arith coding contexts.
  77. * There can be 1, 2, 4, or 8 of these after the header context.
  78. */
  79. int num_coeff_partitions;
  80. VP56RangeCoder coeff_partition[8];
  81. VP8Macroblock *macroblocks;
  82. VP8Macroblock *macroblocks_base;
  83. VP8FilterStrength *filter_strength;
  84. uint8_t *intra4x4_pred_mode_top;
  85. uint8_t intra4x4_pred_mode_left[4];
  86. uint8_t *segmentation_map;
  87. /**
  88. * Cache of the top row needed for intra prediction
  89. * 16 for luma, 8 for each chroma plane
  90. */
  91. uint8_t (*top_border)[16+8+8];
  92. /**
  93. * For coeff decode, we need to know whether the above block had non-zero
  94. * coefficients. This means for each macroblock, we need data for 4 luma
  95. * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
  96. * per macroblock. We keep the last row in top_nnz.
  97. */
  98. uint8_t (*top_nnz)[9];
  99. DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
  100. /**
  101. * This is the index plus one of the last non-zero coeff
  102. * for each of the blocks in the current macroblock.
  103. * So, 0 -> no coeffs
  104. * 1 -> dc-only (special transform)
  105. * 2+-> full transform
  106. */
  107. DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
  108. DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
  109. DECLARE_ALIGNED(16, DCTELEM, block_dc)[16];
  110. uint8_t intra4x4_pred_mode_mb[16];
  111. int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
  112. int segment; ///< segment of the current macroblock
  113. int mbskip_enabled;
  114. int sign_bias[4]; ///< one state [0, 1] per ref frame type
  115. int ref_count[3];
  116. /**
  117. * Base parameters for segmentation, i.e. per-macroblock parameters.
  118. * These must be kept unchanged even if segmentation is not used for
  119. * a frame, since the values persist between interframes.
  120. */
  121. struct {
  122. int enabled;
  123. int absolute_vals;
  124. int update_map;
  125. int8_t base_quant[4];
  126. int8_t filter_level[4]; ///< base loop filter level
  127. } segmentation;
  128. /**
  129. * Macroblocks can have one of 4 different quants in a frame when
  130. * segmentation is enabled.
  131. * If segmentation is disabled, only the first segment's values are used.
  132. */
  133. struct {
  134. // [0] - DC qmul [1] - AC qmul
  135. int16_t luma_qmul[2];
  136. int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
  137. int16_t chroma_qmul[2];
  138. } qmat[4];
  139. struct {
  140. int simple;
  141. int level;
  142. int sharpness;
  143. } filter;
  144. struct {
  145. int enabled; ///< whether each mb can have a different strength based on mode/ref
  146. /**
  147. * filter strength adjustment for the following macroblock modes:
  148. * [0-3] - i16x16 (always zero)
  149. * [4] - i4x4
  150. * [5] - zero mv
  151. * [6] - inter modes except for zero or split mv
  152. * [7] - split mv
  153. * i16x16 modes never have any adjustment
  154. */
  155. int8_t mode[VP8_MVMODE_SPLIT+1];
  156. /**
  157. * filter strength adjustment for macroblocks that reference:
  158. * [0] - intra / VP56_FRAME_CURRENT
  159. * [1] - VP56_FRAME_PREVIOUS
  160. * [2] - VP56_FRAME_GOLDEN
  161. * [3] - altref / VP56_FRAME_GOLDEN2
  162. */
  163. int8_t ref[4];
  164. } lf_delta;
  165. /**
  166. * These are all of the updatable probabilities for binary decisions.
  167. * They are only implictly reset on keyframes, making it quite likely
  168. * for an interframe to desync if a prior frame's header was corrupt
  169. * or missing outright!
  170. */
  171. struct {
  172. uint8_t segmentid[3];
  173. uint8_t mbskip;
  174. uint8_t intra;
  175. uint8_t last;
  176. uint8_t golden;
  177. uint8_t pred16x16[4];
  178. uint8_t pred8x8c[3];
  179. /* Padded to allow overreads */
  180. uint8_t token[4][17][3][NUM_DCT_TOKENS-1];
  181. uint8_t mvc[2][19];
  182. } prob[2];
  183. } VP8Context;
  184. static void vp8_decode_flush(AVCodecContext *avctx)
  185. {
  186. VP8Context *s = avctx->priv_data;
  187. int i;
  188. for (i = 0; i < 4; i++)
  189. if (s->frames[i].data[0])
  190. avctx->release_buffer(avctx, &s->frames[i]);
  191. memset(s->framep, 0, sizeof(s->framep));
  192. av_freep(&s->macroblocks_base);
  193. av_freep(&s->filter_strength);
  194. av_freep(&s->intra4x4_pred_mode_top);
  195. av_freep(&s->top_nnz);
  196. av_freep(&s->edge_emu_buffer);
  197. av_freep(&s->top_border);
  198. av_freep(&s->segmentation_map);
  199. s->macroblocks = NULL;
  200. }
  201. static int update_dimensions(VP8Context *s, int width, int height)
  202. {
  203. if (av_image_check_size(width, height, 0, s->avctx))
  204. return AVERROR_INVALIDDATA;
  205. vp8_decode_flush(s->avctx);
  206. avcodec_set_dimensions(s->avctx, width, height);
  207. s->mb_width = (s->avctx->coded_width +15) / 16;
  208. s->mb_height = (s->avctx->coded_height+15) / 16;
  209. s->macroblocks_base = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
  210. s->filter_strength = av_mallocz(s->mb_width*sizeof(*s->filter_strength));
  211. s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
  212. s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
  213. s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
  214. s->segmentation_map = av_mallocz(s->mb_width*s->mb_height);
  215. if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_top ||
  216. !s->top_nnz || !s->top_border || !s->segmentation_map)
  217. return AVERROR(ENOMEM);
  218. s->macroblocks = s->macroblocks_base + 1;
  219. return 0;
  220. }
  221. static void parse_segment_info(VP8Context *s)
  222. {
  223. VP56RangeCoder *c = &s->c;
  224. int i;
  225. s->segmentation.update_map = vp8_rac_get(c);
  226. if (vp8_rac_get(c)) { // update segment feature data
  227. s->segmentation.absolute_vals = vp8_rac_get(c);
  228. for (i = 0; i < 4; i++)
  229. s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
  230. for (i = 0; i < 4; i++)
  231. s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
  232. }
  233. if (s->segmentation.update_map)
  234. for (i = 0; i < 3; i++)
  235. s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  236. }
  237. static void update_lf_deltas(VP8Context *s)
  238. {
  239. VP56RangeCoder *c = &s->c;
  240. int i;
  241. for (i = 0; i < 4; i++)
  242. s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
  243. for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++)
  244. s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
  245. }
  246. static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
  247. {
  248. const uint8_t *sizes = buf;
  249. int i;
  250. s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
  251. buf += 3*(s->num_coeff_partitions-1);
  252. buf_size -= 3*(s->num_coeff_partitions-1);
  253. if (buf_size < 0)
  254. return -1;
  255. for (i = 0; i < s->num_coeff_partitions-1; i++) {
  256. int size = AV_RL24(sizes + 3*i);
  257. if (buf_size - size < 0)
  258. return -1;
  259. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
  260. buf += size;
  261. buf_size -= size;
  262. }
  263. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
  264. return 0;
  265. }
  266. static void get_quants(VP8Context *s)
  267. {
  268. VP56RangeCoder *c = &s->c;
  269. int i, base_qi;
  270. int yac_qi = vp8_rac_get_uint(c, 7);
  271. int ydc_delta = vp8_rac_get_sint(c, 4);
  272. int y2dc_delta = vp8_rac_get_sint(c, 4);
  273. int y2ac_delta = vp8_rac_get_sint(c, 4);
  274. int uvdc_delta = vp8_rac_get_sint(c, 4);
  275. int uvac_delta = vp8_rac_get_sint(c, 4);
  276. for (i = 0; i < 4; i++) {
  277. if (s->segmentation.enabled) {
  278. base_qi = s->segmentation.base_quant[i];
  279. if (!s->segmentation.absolute_vals)
  280. base_qi += yac_qi;
  281. } else
  282. base_qi = yac_qi;
  283. s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
  284. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi , 0, 127)];
  285. s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
  286. s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
  287. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
  288. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
  289. s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
  290. s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
  291. }
  292. }
  293. /**
  294. * Determine which buffers golden and altref should be updated with after this frame.
  295. * The spec isn't clear here, so I'm going by my understanding of what libvpx does
  296. *
  297. * Intra frames update all 3 references
  298. * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
  299. * If the update (golden|altref) flag is set, it's updated with the current frame
  300. * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
  301. * If the flag is not set, the number read means:
  302. * 0: no update
  303. * 1: VP56_FRAME_PREVIOUS
  304. * 2: update golden with altref, or update altref with golden
  305. */
  306. static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
  307. {
  308. VP56RangeCoder *c = &s->c;
  309. if (update)
  310. return VP56_FRAME_CURRENT;
  311. switch (vp8_rac_get_uint(c, 2)) {
  312. case 1:
  313. return VP56_FRAME_PREVIOUS;
  314. case 2:
  315. return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
  316. }
  317. return VP56_FRAME_NONE;
  318. }
  319. static void update_refs(VP8Context *s)
  320. {
  321. VP56RangeCoder *c = &s->c;
  322. int update_golden = vp8_rac_get(c);
  323. int update_altref = vp8_rac_get(c);
  324. s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
  325. s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
  326. }
  327. static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  328. {
  329. VP56RangeCoder *c = &s->c;
  330. int header_size, hscale, vscale, i, j, k, l, m, ret;
  331. int width = s->avctx->width;
  332. int height = s->avctx->height;
  333. s->keyframe = !(buf[0] & 1);
  334. s->profile = (buf[0]>>1) & 7;
  335. s->invisible = !(buf[0] & 0x10);
  336. header_size = AV_RL24(buf) >> 5;
  337. buf += 3;
  338. buf_size -= 3;
  339. if (s->profile > 3)
  340. av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
  341. if (!s->profile)
  342. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
  343. else // profile 1-3 use bilinear, 4+ aren't defined so whatever
  344. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
  345. if (header_size > buf_size - 7*s->keyframe) {
  346. av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
  347. return AVERROR_INVALIDDATA;
  348. }
  349. if (s->keyframe) {
  350. if (AV_RL24(buf) != 0x2a019d) {
  351. av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
  352. return AVERROR_INVALIDDATA;
  353. }
  354. width = AV_RL16(buf+3) & 0x3fff;
  355. height = AV_RL16(buf+5) & 0x3fff;
  356. hscale = buf[4] >> 6;
  357. vscale = buf[6] >> 6;
  358. buf += 7;
  359. buf_size -= 7;
  360. if (hscale || vscale)
  361. av_log_missing_feature(s->avctx, "Upscaling", 1);
  362. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  363. for (i = 0; i < 4; i++)
  364. for (j = 0; j < 16; j++)
  365. memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
  366. sizeof(s->prob->token[i][j]));
  367. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
  368. memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
  369. memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
  370. memset(&s->segmentation, 0, sizeof(s->segmentation));
  371. }
  372. if (!s->macroblocks_base || /* first frame */
  373. width != s->avctx->width || height != s->avctx->height) {
  374. if ((ret = update_dimensions(s, width, height) < 0))
  375. return ret;
  376. }
  377. ff_vp56_init_range_decoder(c, buf, header_size);
  378. buf += header_size;
  379. buf_size -= header_size;
  380. if (s->keyframe) {
  381. if (vp8_rac_get(c))
  382. av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
  383. vp8_rac_get(c); // whether we can skip clamping in dsp functions
  384. }
  385. if ((s->segmentation.enabled = vp8_rac_get(c)))
  386. parse_segment_info(s);
  387. else
  388. s->segmentation.update_map = 0; // FIXME: move this to some init function?
  389. s->filter.simple = vp8_rac_get(c);
  390. s->filter.level = vp8_rac_get_uint(c, 6);
  391. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  392. if ((s->lf_delta.enabled = vp8_rac_get(c)))
  393. if (vp8_rac_get(c))
  394. update_lf_deltas(s);
  395. if (setup_partitions(s, buf, buf_size)) {
  396. av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
  397. return AVERROR_INVALIDDATA;
  398. }
  399. get_quants(s);
  400. if (!s->keyframe) {
  401. update_refs(s);
  402. s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
  403. s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
  404. }
  405. // if we aren't saving this frame's probabilities for future frames,
  406. // make a copy of the current probabilities
  407. if (!(s->update_probabilities = vp8_rac_get(c)))
  408. s->prob[1] = s->prob[0];
  409. s->update_last = s->keyframe || vp8_rac_get(c);
  410. for (i = 0; i < 4; i++)
  411. for (j = 0; j < 8; j++)
  412. for (k = 0; k < 3; k++)
  413. for (l = 0; l < NUM_DCT_TOKENS-1; l++)
  414. if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
  415. int prob = vp8_rac_get_uint(c, 8);
  416. for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
  417. s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
  418. }
  419. if ((s->mbskip_enabled = vp8_rac_get(c)))
  420. s->prob->mbskip = vp8_rac_get_uint(c, 8);
  421. if (!s->keyframe) {
  422. s->prob->intra = vp8_rac_get_uint(c, 8);
  423. s->prob->last = vp8_rac_get_uint(c, 8);
  424. s->prob->golden = vp8_rac_get_uint(c, 8);
  425. if (vp8_rac_get(c))
  426. for (i = 0; i < 4; i++)
  427. s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
  428. if (vp8_rac_get(c))
  429. for (i = 0; i < 3; i++)
  430. s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
  431. // 17.2 MV probability update
  432. for (i = 0; i < 2; i++)
  433. for (j = 0; j < 19; j++)
  434. if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
  435. s->prob->mvc[i][j] = vp8_rac_get_nn(c);
  436. }
  437. return 0;
  438. }
  439. static av_always_inline
  440. void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src, int mb_x, int mb_y)
  441. {
  442. #define MARGIN (16 << 2)
  443. dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
  444. ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
  445. dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
  446. ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
  447. }
  448. /**
  449. * Motion vector coding, 17.1.
  450. */
  451. static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
  452. {
  453. int bit, x = 0;
  454. if (vp56_rac_get_prob_branchy(c, p[0])) {
  455. int i;
  456. for (i = 0; i < 3; i++)
  457. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  458. for (i = 9; i > 3; i--)
  459. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  460. if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
  461. x += 8;
  462. } else {
  463. // small_mvtree
  464. const uint8_t *ps = p+2;
  465. bit = vp56_rac_get_prob(c, *ps);
  466. ps += 1 + 3*bit;
  467. x += 4*bit;
  468. bit = vp56_rac_get_prob(c, *ps);
  469. ps += 1 + bit;
  470. x += 2*bit;
  471. x += vp56_rac_get_prob(c, *ps);
  472. }
  473. return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
  474. }
  475. static av_always_inline
  476. const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
  477. {
  478. if (left == top)
  479. return vp8_submv_prob[4-!!left];
  480. if (!top)
  481. return vp8_submv_prob[2];
  482. return vp8_submv_prob[1-!!left];
  483. }
  484. /**
  485. * Split motion vector prediction, 16.4.
  486. * @returns the number of motion vectors parsed (2, 4 or 16)
  487. */
  488. static av_always_inline
  489. int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb)
  490. {
  491. int part_idx;
  492. int n, num;
  493. VP8Macroblock *top_mb = &mb[2];
  494. VP8Macroblock *left_mb = &mb[-1];
  495. const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
  496. *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
  497. *mbsplits_cur, *firstidx;
  498. VP56mv *top_mv = top_mb->bmv;
  499. VP56mv *left_mv = left_mb->bmv;
  500. VP56mv *cur_mv = mb->bmv;
  501. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
  502. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1])) {
  503. part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
  504. } else {
  505. part_idx = VP8_SPLITMVMODE_8x8;
  506. }
  507. } else {
  508. part_idx = VP8_SPLITMVMODE_4x4;
  509. }
  510. num = vp8_mbsplit_count[part_idx];
  511. mbsplits_cur = vp8_mbsplits[part_idx],
  512. firstidx = vp8_mbfirstidx[part_idx];
  513. mb->partitioning = part_idx;
  514. for (n = 0; n < num; n++) {
  515. int k = firstidx[n];
  516. uint32_t left, above;
  517. const uint8_t *submv_prob;
  518. if (!(k & 3))
  519. left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
  520. else
  521. left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
  522. if (k <= 3)
  523. above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
  524. else
  525. above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
  526. submv_prob = get_submv_prob(left, above);
  527. if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
  528. if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
  529. if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
  530. mb->bmv[n].y = mb->mv.y + read_mv_component(c, s->prob->mvc[0]);
  531. mb->bmv[n].x = mb->mv.x + read_mv_component(c, s->prob->mvc[1]);
  532. } else {
  533. AV_ZERO32(&mb->bmv[n]);
  534. }
  535. } else {
  536. AV_WN32A(&mb->bmv[n], above);
  537. }
  538. } else {
  539. AV_WN32A(&mb->bmv[n], left);
  540. }
  541. }
  542. return num;
  543. }
  544. static av_always_inline
  545. void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y)
  546. {
  547. VP8Macroblock *mb_edge[3] = { mb + 2 /* top */,
  548. mb - 1 /* left */,
  549. mb + 1 /* top-left */ };
  550. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  551. enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
  552. int idx = CNT_ZERO;
  553. int cur_sign_bias = s->sign_bias[mb->ref_frame];
  554. int *sign_bias = s->sign_bias;
  555. VP56mv near_mv[4];
  556. uint8_t cnt[4] = { 0 };
  557. VP56RangeCoder *c = &s->c;
  558. AV_ZERO32(&near_mv[0]);
  559. AV_ZERO32(&near_mv[1]);
  560. AV_ZERO32(&near_mv[2]);
  561. /* Process MB on top, left and top-left */
  562. #define MV_EDGE_CHECK(n)\
  563. {\
  564. VP8Macroblock *edge = mb_edge[n];\
  565. int edge_ref = edge->ref_frame;\
  566. if (edge_ref != VP56_FRAME_CURRENT) {\
  567. uint32_t mv = AV_RN32A(&edge->mv);\
  568. if (mv) {\
  569. if (cur_sign_bias != sign_bias[edge_ref]) {\
  570. /* SWAR negate of the values in mv. */\
  571. mv = ~mv;\
  572. mv = ((mv&0x7fff7fff) + 0x00010001) ^ (mv&0x80008000);\
  573. }\
  574. if (!n || mv != AV_RN32A(&near_mv[idx]))\
  575. AV_WN32A(&near_mv[++idx], mv);\
  576. cnt[idx] += 1 + (n != 2);\
  577. } else\
  578. cnt[CNT_ZERO] += 1 + (n != 2);\
  579. }\
  580. }
  581. MV_EDGE_CHECK(0)
  582. MV_EDGE_CHECK(1)
  583. MV_EDGE_CHECK(2)
  584. mb->partitioning = VP8_SPLITMVMODE_NONE;
  585. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
  586. mb->mode = VP8_MVMODE_MV;
  587. /* If we have three distinct MVs, merge first and last if they're the same */
  588. if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT]))
  589. cnt[CNT_NEAREST] += 1;
  590. /* Swap near and nearest if necessary */
  591. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  592. FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
  593. FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
  594. }
  595. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
  596. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
  597. /* Choose the best mv out of 0,0 and the nearest mv */
  598. clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])], mb_x, mb_y);
  599. cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
  600. (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
  601. (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
  602. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
  603. mb->mode = VP8_MVMODE_SPLIT;
  604. mb->mv = mb->bmv[decode_splitmvs(s, c, mb) - 1];
  605. } else {
  606. mb->mv.y += read_mv_component(c, s->prob->mvc[0]);
  607. mb->mv.x += read_mv_component(c, s->prob->mvc[1]);
  608. mb->bmv[0] = mb->mv;
  609. }
  610. } else {
  611. clamp_mv(s, &mb->mv, &near_mv[CNT_NEAR], mb_x, mb_y);
  612. mb->bmv[0] = mb->mv;
  613. }
  614. } else {
  615. clamp_mv(s, &mb->mv, &near_mv[CNT_NEAREST], mb_x, mb_y);
  616. mb->bmv[0] = mb->mv;
  617. }
  618. } else {
  619. mb->mode = VP8_MVMODE_ZERO;
  620. AV_ZERO32(&mb->mv);
  621. mb->bmv[0] = mb->mv;
  622. }
  623. }
  624. static av_always_inline
  625. void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c,
  626. int mb_x, int keyframe)
  627. {
  628. uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
  629. if (keyframe) {
  630. int x, y;
  631. uint8_t* const top = s->intra4x4_pred_mode_top + 4 * mb_x;
  632. uint8_t* const left = s->intra4x4_pred_mode_left;
  633. for (y = 0; y < 4; y++) {
  634. for (x = 0; x < 4; x++) {
  635. const uint8_t *ctx;
  636. ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
  637. *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
  638. left[y] = top[x] = *intra4x4;
  639. intra4x4++;
  640. }
  641. }
  642. } else {
  643. int i;
  644. for (i = 0; i < 16; i++)
  645. intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
  646. }
  647. }
  648. static av_always_inline
  649. void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, uint8_t *segment)
  650. {
  651. VP56RangeCoder *c = &s->c;
  652. if (s->segmentation.update_map)
  653. *segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
  654. s->segment = *segment;
  655. mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
  656. if (s->keyframe) {
  657. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
  658. if (mb->mode == MODE_I4x4) {
  659. decode_intra4x4_modes(s, c, mb_x, 1);
  660. } else {
  661. const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
  662. AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
  663. AV_WN32A(s->intra4x4_pred_mode_left, modes);
  664. }
  665. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
  666. mb->ref_frame = VP56_FRAME_CURRENT;
  667. } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
  668. // inter MB, 16.2
  669. if (vp56_rac_get_prob_branchy(c, s->prob->last))
  670. mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
  671. VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
  672. else
  673. mb->ref_frame = VP56_FRAME_PREVIOUS;
  674. s->ref_count[mb->ref_frame-1]++;
  675. // motion vectors, 16.3
  676. decode_mvs(s, mb, mb_x, mb_y);
  677. } else {
  678. // intra MB, 16.1
  679. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
  680. if (mb->mode == MODE_I4x4)
  681. decode_intra4x4_modes(s, c, mb_x, 0);
  682. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
  683. mb->ref_frame = VP56_FRAME_CURRENT;
  684. mb->partitioning = VP8_SPLITMVMODE_NONE;
  685. AV_ZERO32(&mb->bmv[0]);
  686. }
  687. }
  688. #ifndef decode_block_coeffs_internal
  689. /**
  690. * @param c arithmetic bitstream reader context
  691. * @param block destination for block coefficients
  692. * @param probs probabilities to use when reading trees from the bitstream
  693. * @param i initial coeff index, 0 unless a separate DC block is coded
  694. * @param zero_nhood the initial prediction context for number of surrounding
  695. * all-zero blocks (only left/top, so 0-2)
  696. * @param qmul array holding the dc/ac dequant factor at position 0/1
  697. * @return 0 if no coeffs were decoded
  698. * otherwise, the index of the last coeff decoded plus one
  699. */
  700. static int decode_block_coeffs_internal(VP56RangeCoder *c, DCTELEM block[16],
  701. uint8_t probs[8][3][NUM_DCT_TOKENS-1],
  702. int i, uint8_t *token_prob, int16_t qmul[2])
  703. {
  704. goto skip_eob;
  705. do {
  706. int coeff;
  707. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  708. return i;
  709. skip_eob:
  710. if (!vp56_rac_get_prob_branchy(c, token_prob[1])) { // DCT_0
  711. if (++i == 16)
  712. return i; // invalid input; blocks should end with EOB
  713. token_prob = probs[i][0];
  714. goto skip_eob;
  715. }
  716. if (!vp56_rac_get_prob_branchy(c, token_prob[2])) { // DCT_1
  717. coeff = 1;
  718. token_prob = probs[i+1][1];
  719. } else {
  720. if (!vp56_rac_get_prob_branchy(c, token_prob[3])) { // DCT 2,3,4
  721. coeff = vp56_rac_get_prob_branchy(c, token_prob[4]);
  722. if (coeff)
  723. coeff += vp56_rac_get_prob(c, token_prob[5]);
  724. coeff += 2;
  725. } else {
  726. // DCT_CAT*
  727. if (!vp56_rac_get_prob_branchy(c, token_prob[6])) {
  728. if (!vp56_rac_get_prob_branchy(c, token_prob[7])) { // DCT_CAT1
  729. coeff = 5 + vp56_rac_get_prob(c, vp8_dct_cat1_prob[0]);
  730. } else { // DCT_CAT2
  731. coeff = 7;
  732. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[0]) << 1;
  733. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[1]);
  734. }
  735. } else { // DCT_CAT3 and up
  736. int a = vp56_rac_get_prob(c, token_prob[8]);
  737. int b = vp56_rac_get_prob(c, token_prob[9+a]);
  738. int cat = (a<<1) + b;
  739. coeff = 3 + (8<<cat);
  740. coeff += vp8_rac_get_coeff(c, ff_vp8_dct_cat_prob[cat]);
  741. }
  742. }
  743. token_prob = probs[i+1][2];
  744. }
  745. block[zigzag_scan[i]] = (vp8_rac_get(c) ? -coeff : coeff) * qmul[!!i];
  746. } while (++i < 16);
  747. return i;
  748. }
  749. #endif
  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. AV_WN32A(copy_dst+4, 127U * 0x01010101U);
  989. } else {
  990. AV_COPY32(copy_dst+4, 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. AV_COPY32(ptr+4*x , copy_dst+12);
  1015. AV_COPY32(ptr+4*x+s->linesize , copy_dst+20);
  1016. AV_COPY32(ptr+4*x+s->linesize*2, copy_dst+28);
  1017. AV_COPY32(ptr+4*x+s->linesize*3, 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. static const uint8_t subpel_idx[3][8] = {
  1045. { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
  1046. // also function pointer index
  1047. { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
  1048. { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
  1049. };
  1050. /**
  1051. * Generic MC function.
  1052. *
  1053. * @param s VP8 decoding context
  1054. * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
  1055. * @param dst target buffer for block data at block position
  1056. * @param src reference picture buffer at origin (0, 0)
  1057. * @param mv motion vector (relative to block position) to get pixel data from
  1058. * @param x_off horizontal position of block from origin (0, 0)
  1059. * @param y_off vertical position of block from origin (0, 0)
  1060. * @param block_w width of block (16, 8 or 4)
  1061. * @param block_h height of block (always same as block_w)
  1062. * @param width width of src/dst plane data
  1063. * @param height height of src/dst plane data
  1064. * @param linesize size of a single line of plane data, including padding
  1065. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  1066. */
  1067. static av_always_inline
  1068. void vp8_mc_luma(VP8Context *s, uint8_t *dst, uint8_t *src, const VP56mv *mv,
  1069. int x_off, int y_off, int block_w, int block_h,
  1070. int width, int height, int linesize,
  1071. vp8_mc_func mc_func[3][3])
  1072. {
  1073. if (AV_RN32A(mv)) {
  1074. int mx = (mv->x << 1)&7, mx_idx = subpel_idx[0][mx];
  1075. int my = (mv->y << 1)&7, my_idx = subpel_idx[0][my];
  1076. x_off += mv->x >> 2;
  1077. y_off += mv->y >> 2;
  1078. // edge emulation
  1079. src += y_off * linesize + x_off;
  1080. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  1081. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  1082. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src - my_idx * linesize - mx_idx, linesize,
  1083. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  1084. x_off - mx_idx, y_off - my_idx, width, height);
  1085. src = s->edge_emu_buffer + mx_idx + linesize * my_idx;
  1086. }
  1087. mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
  1088. } else
  1089. mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1090. }
  1091. static av_always_inline
  1092. void vp8_mc_chroma(VP8Context *s, uint8_t *dst1, uint8_t *dst2, uint8_t *src1,
  1093. uint8_t *src2, const VP56mv *mv, int x_off, int y_off,
  1094. int block_w, int block_h, int width, int height, int linesize,
  1095. vp8_mc_func mc_func[3][3])
  1096. {
  1097. if (AV_RN32A(mv)) {
  1098. int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
  1099. int my = mv->y&7, my_idx = subpel_idx[0][my];
  1100. x_off += mv->x >> 3;
  1101. y_off += mv->y >> 3;
  1102. // edge emulation
  1103. src1 += y_off * linesize + x_off;
  1104. src2 += y_off * linesize + x_off;
  1105. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  1106. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  1107. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src1 - my_idx * linesize - mx_idx, linesize,
  1108. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  1109. x_off - mx_idx, y_off - my_idx, width, height);
  1110. src1 = s->edge_emu_buffer + mx_idx + linesize * my_idx;
  1111. mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
  1112. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src2 - my_idx * linesize - mx_idx, linesize,
  1113. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  1114. x_off - mx_idx, y_off - my_idx, width, height);
  1115. src2 = s->edge_emu_buffer + mx_idx + linesize * my_idx;
  1116. mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
  1117. } else {
  1118. mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
  1119. mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
  1120. }
  1121. } else {
  1122. mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1123. mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1124. }
  1125. }
  1126. static av_always_inline
  1127. void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
  1128. AVFrame *ref_frame, int x_off, int y_off,
  1129. int bx_off, int by_off,
  1130. int block_w, int block_h,
  1131. int width, int height, VP56mv *mv)
  1132. {
  1133. VP56mv uvmv = *mv;
  1134. /* Y */
  1135. vp8_mc_luma(s, dst[0] + by_off * s->linesize + bx_off,
  1136. ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
  1137. block_w, block_h, width, height, s->linesize,
  1138. s->put_pixels_tab[block_w == 8]);
  1139. /* U/V */
  1140. if (s->profile == 3) {
  1141. uvmv.x &= ~7;
  1142. uvmv.y &= ~7;
  1143. }
  1144. x_off >>= 1; y_off >>= 1;
  1145. bx_off >>= 1; by_off >>= 1;
  1146. width >>= 1; height >>= 1;
  1147. block_w >>= 1; block_h >>= 1;
  1148. vp8_mc_chroma(s, dst[1] + by_off * s->uvlinesize + bx_off,
  1149. dst[2] + by_off * s->uvlinesize + bx_off, ref_frame->data[1],
  1150. ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
  1151. block_w, block_h, width, height, s->uvlinesize,
  1152. s->put_pixels_tab[1 + (block_w == 4)]);
  1153. }
  1154. /* Fetch pixels for estimated mv 4 macroblocks ahead.
  1155. * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
  1156. static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
  1157. {
  1158. /* Don't prefetch refs that haven't been used very often this frame. */
  1159. if (s->ref_count[ref-1] > (mb_xy >> 5)) {
  1160. int x_off = mb_x << 4, y_off = mb_y << 4;
  1161. int mx = (mb->mv.x>>2) + x_off + 8;
  1162. int my = (mb->mv.y>>2) + y_off;
  1163. uint8_t **src= s->framep[ref]->data;
  1164. int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
  1165. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1166. off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
  1167. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1168. }
  1169. }
  1170. /**
  1171. * Apply motion vectors to prediction buffer, chapter 18.
  1172. */
  1173. static av_always_inline
  1174. void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  1175. int mb_x, int mb_y)
  1176. {
  1177. int x_off = mb_x << 4, y_off = mb_y << 4;
  1178. int width = 16*s->mb_width, height = 16*s->mb_height;
  1179. AVFrame *ref = s->framep[mb->ref_frame];
  1180. VP56mv *bmv = mb->bmv;
  1181. switch (mb->partitioning) {
  1182. case VP8_SPLITMVMODE_NONE:
  1183. vp8_mc_part(s, dst, ref, x_off, y_off,
  1184. 0, 0, 16, 16, width, height, &mb->mv);
  1185. break;
  1186. case VP8_SPLITMVMODE_4x4: {
  1187. int x, y;
  1188. VP56mv uvmv;
  1189. /* Y */
  1190. for (y = 0; y < 4; y++) {
  1191. for (x = 0; x < 4; x++) {
  1192. vp8_mc_luma(s, dst[0] + 4*y*s->linesize + x*4,
  1193. ref->data[0], &bmv[4*y + x],
  1194. 4*x + x_off, 4*y + y_off, 4, 4,
  1195. width, height, s->linesize,
  1196. s->put_pixels_tab[2]);
  1197. }
  1198. }
  1199. /* U/V */
  1200. x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
  1201. for (y = 0; y < 2; y++) {
  1202. for (x = 0; x < 2; x++) {
  1203. uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
  1204. mb->bmv[ 2*y * 4 + 2*x+1].x +
  1205. mb->bmv[(2*y+1) * 4 + 2*x ].x +
  1206. mb->bmv[(2*y+1) * 4 + 2*x+1].x;
  1207. uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
  1208. mb->bmv[ 2*y * 4 + 2*x+1].y +
  1209. mb->bmv[(2*y+1) * 4 + 2*x ].y +
  1210. mb->bmv[(2*y+1) * 4 + 2*x+1].y;
  1211. uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
  1212. uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
  1213. if (s->profile == 3) {
  1214. uvmv.x &= ~7;
  1215. uvmv.y &= ~7;
  1216. }
  1217. vp8_mc_chroma(s, dst[1] + 4*y*s->uvlinesize + x*4,
  1218. dst[2] + 4*y*s->uvlinesize + x*4,
  1219. ref->data[1], ref->data[2], &uvmv,
  1220. 4*x + x_off, 4*y + y_off, 4, 4,
  1221. width, height, s->uvlinesize,
  1222. s->put_pixels_tab[2]);
  1223. }
  1224. }
  1225. break;
  1226. }
  1227. case VP8_SPLITMVMODE_16x8:
  1228. vp8_mc_part(s, dst, ref, x_off, y_off,
  1229. 0, 0, 16, 8, width, height, &bmv[0]);
  1230. vp8_mc_part(s, dst, ref, x_off, y_off,
  1231. 0, 8, 16, 8, width, height, &bmv[1]);
  1232. break;
  1233. case VP8_SPLITMVMODE_8x16:
  1234. vp8_mc_part(s, dst, ref, x_off, y_off,
  1235. 0, 0, 8, 16, width, height, &bmv[0]);
  1236. vp8_mc_part(s, dst, ref, x_off, y_off,
  1237. 8, 0, 8, 16, width, height, &bmv[1]);
  1238. break;
  1239. case VP8_SPLITMVMODE_8x8:
  1240. vp8_mc_part(s, dst, ref, x_off, y_off,
  1241. 0, 0, 8, 8, width, height, &bmv[0]);
  1242. vp8_mc_part(s, dst, ref, x_off, y_off,
  1243. 8, 0, 8, 8, width, height, &bmv[1]);
  1244. vp8_mc_part(s, dst, ref, x_off, y_off,
  1245. 0, 8, 8, 8, width, height, &bmv[2]);
  1246. vp8_mc_part(s, dst, ref, x_off, y_off,
  1247. 8, 8, 8, 8, width, height, &bmv[3]);
  1248. break;
  1249. }
  1250. }
  1251. static av_always_inline void idct_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb)
  1252. {
  1253. int x, y, ch;
  1254. if (mb->mode != MODE_I4x4) {
  1255. uint8_t *y_dst = dst[0];
  1256. for (y = 0; y < 4; y++) {
  1257. uint32_t nnz4 = AV_RL32(s->non_zero_count_cache[y]);
  1258. if (nnz4) {
  1259. if (nnz4&~0x01010101) {
  1260. for (x = 0; x < 4; x++) {
  1261. if ((uint8_t)nnz4 == 1)
  1262. s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
  1263. else if((uint8_t)nnz4 > 1)
  1264. s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
  1265. nnz4 >>= 8;
  1266. if (!nnz4)
  1267. break;
  1268. }
  1269. } else {
  1270. s->vp8dsp.vp8_idct_dc_add4y(y_dst, s->block[y], s->linesize);
  1271. }
  1272. }
  1273. y_dst += 4*s->linesize;
  1274. }
  1275. }
  1276. for (ch = 0; ch < 2; ch++) {
  1277. uint32_t nnz4 = AV_RL32(s->non_zero_count_cache[4+ch]);
  1278. if (nnz4) {
  1279. uint8_t *ch_dst = dst[1+ch];
  1280. if (nnz4&~0x01010101) {
  1281. for (y = 0; y < 2; y++) {
  1282. for (x = 0; x < 2; x++) {
  1283. if ((uint8_t)nnz4 == 1)
  1284. s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1285. else if((uint8_t)nnz4 > 1)
  1286. s->vp8dsp.vp8_idct_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1287. nnz4 >>= 8;
  1288. if (!nnz4)
  1289. break;
  1290. }
  1291. ch_dst += 4*s->uvlinesize;
  1292. }
  1293. } else {
  1294. s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, s->block[4+ch], s->uvlinesize);
  1295. }
  1296. }
  1297. }
  1298. }
  1299. static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
  1300. {
  1301. int interior_limit, filter_level;
  1302. if (s->segmentation.enabled) {
  1303. filter_level = s->segmentation.filter_level[s->segment];
  1304. if (!s->segmentation.absolute_vals)
  1305. filter_level += s->filter.level;
  1306. } else
  1307. filter_level = s->filter.level;
  1308. if (s->lf_delta.enabled) {
  1309. filter_level += s->lf_delta.ref[mb->ref_frame];
  1310. filter_level += s->lf_delta.mode[mb->mode];
  1311. }
  1312. /* Like av_clip for inputs 0 and max, where max is equal to (2^n-1) */
  1313. #define POW2CLIP(x,max) (((x) & ~max) ? (-(x))>>31 & max : (x));
  1314. filter_level = POW2CLIP(filter_level, 63);
  1315. interior_limit = filter_level;
  1316. if (s->filter.sharpness) {
  1317. interior_limit >>= (s->filter.sharpness + 3) >> 2;
  1318. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  1319. }
  1320. interior_limit = FFMAX(interior_limit, 1);
  1321. f->filter_level = filter_level;
  1322. f->inner_limit = interior_limit;
  1323. f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
  1324. }
  1325. static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
  1326. {
  1327. int mbedge_lim, bedge_lim, hev_thresh;
  1328. int filter_level = f->filter_level;
  1329. int inner_limit = f->inner_limit;
  1330. int inner_filter = f->inner_filter;
  1331. int linesize = s->linesize;
  1332. int uvlinesize = s->uvlinesize;
  1333. static const uint8_t hev_thresh_lut[2][64] = {
  1334. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1335. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1336. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  1337. 3, 3, 3, 3 },
  1338. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1339. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1340. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1341. 2, 2, 2, 2 }
  1342. };
  1343. if (!filter_level)
  1344. return;
  1345. bedge_lim = 2*filter_level + inner_limit;
  1346. mbedge_lim = bedge_lim + 4;
  1347. hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
  1348. if (mb_x) {
  1349. s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
  1350. mbedge_lim, inner_limit, hev_thresh);
  1351. s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1352. mbedge_lim, inner_limit, hev_thresh);
  1353. }
  1354. if (inner_filter) {
  1355. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
  1356. inner_limit, hev_thresh);
  1357. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
  1358. inner_limit, hev_thresh);
  1359. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
  1360. inner_limit, hev_thresh);
  1361. s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
  1362. uvlinesize, bedge_lim,
  1363. inner_limit, hev_thresh);
  1364. }
  1365. if (mb_y) {
  1366. s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
  1367. mbedge_lim, inner_limit, hev_thresh);
  1368. s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1369. mbedge_lim, inner_limit, hev_thresh);
  1370. }
  1371. if (inner_filter) {
  1372. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
  1373. linesize, bedge_lim,
  1374. inner_limit, hev_thresh);
  1375. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
  1376. linesize, bedge_lim,
  1377. inner_limit, hev_thresh);
  1378. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
  1379. linesize, bedge_lim,
  1380. inner_limit, hev_thresh);
  1381. s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
  1382. dst[2] + 4 * uvlinesize,
  1383. uvlinesize, bedge_lim,
  1384. inner_limit, hev_thresh);
  1385. }
  1386. }
  1387. static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
  1388. {
  1389. int mbedge_lim, bedge_lim;
  1390. int filter_level = f->filter_level;
  1391. int inner_limit = f->inner_limit;
  1392. int inner_filter = f->inner_filter;
  1393. int linesize = s->linesize;
  1394. if (!filter_level)
  1395. return;
  1396. bedge_lim = 2*filter_level + inner_limit;
  1397. mbedge_lim = bedge_lim + 4;
  1398. if (mb_x)
  1399. s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
  1400. if (inner_filter) {
  1401. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
  1402. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
  1403. s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
  1404. }
  1405. if (mb_y)
  1406. s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
  1407. if (inner_filter) {
  1408. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
  1409. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
  1410. s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
  1411. }
  1412. }
  1413. static void filter_mb_row(VP8Context *s, int mb_y)
  1414. {
  1415. VP8FilterStrength *f = s->filter_strength;
  1416. uint8_t *dst[3] = {
  1417. s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
  1418. s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
  1419. s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
  1420. };
  1421. int mb_x;
  1422. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1423. backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  1424. filter_mb(s, dst, f++, mb_x, mb_y);
  1425. dst[0] += 16;
  1426. dst[1] += 8;
  1427. dst[2] += 8;
  1428. }
  1429. }
  1430. static void filter_mb_row_simple(VP8Context *s, int mb_y)
  1431. {
  1432. VP8FilterStrength *f = s->filter_strength;
  1433. uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
  1434. int mb_x;
  1435. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1436. backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
  1437. filter_mb_simple(s, dst, f++, mb_x, mb_y);
  1438. dst += 16;
  1439. }
  1440. }
  1441. static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  1442. AVPacket *avpkt)
  1443. {
  1444. VP8Context *s = avctx->priv_data;
  1445. int ret, mb_x, mb_y, i, y, referenced;
  1446. enum AVDiscard skip_thresh;
  1447. AVFrame *av_uninit(curframe);
  1448. if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
  1449. return ret;
  1450. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
  1451. || s->update_altref == VP56_FRAME_CURRENT;
  1452. skip_thresh = !referenced ? AVDISCARD_NONREF :
  1453. !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
  1454. if (avctx->skip_frame >= skip_thresh) {
  1455. s->invisible = 1;
  1456. goto skip_decode;
  1457. }
  1458. s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
  1459. for (i = 0; i < 4; i++)
  1460. if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1461. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1462. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  1463. curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
  1464. break;
  1465. }
  1466. if (curframe->data[0])
  1467. avctx->release_buffer(avctx, curframe);
  1468. curframe->key_frame = s->keyframe;
  1469. curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  1470. curframe->reference = referenced ? 3 : 0;
  1471. if ((ret = avctx->get_buffer(avctx, curframe))) {
  1472. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
  1473. return ret;
  1474. }
  1475. // Given that arithmetic probabilities are updated every frame, it's quite likely
  1476. // that the values we have on a random interframe are complete junk if we didn't
  1477. // start decode on a keyframe. So just don't display anything rather than junk.
  1478. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  1479. !s->framep[VP56_FRAME_GOLDEN] ||
  1480. !s->framep[VP56_FRAME_GOLDEN2])) {
  1481. av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
  1482. return AVERROR_INVALIDDATA;
  1483. }
  1484. s->linesize = curframe->linesize[0];
  1485. s->uvlinesize = curframe->linesize[1];
  1486. if (!s->edge_emu_buffer)
  1487. s->edge_emu_buffer = av_malloc(21*s->linesize);
  1488. memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
  1489. /* Zero macroblock structures for top/top-left prediction from outside the frame. */
  1490. memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
  1491. // top edge of 127 for intra prediction
  1492. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1493. s->top_border[0][15] = s->top_border[0][23] = 127;
  1494. memset(s->top_border[1]-1, 127, s->mb_width*sizeof(*s->top_border)+1);
  1495. }
  1496. memset(s->ref_count, 0, sizeof(s->ref_count));
  1497. if (s->keyframe)
  1498. memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
  1499. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1500. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
  1501. VP8Macroblock *mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
  1502. int mb_xy = mb_y*s->mb_width;
  1503. uint8_t *dst[3] = {
  1504. curframe->data[0] + 16*mb_y*s->linesize,
  1505. curframe->data[1] + 8*mb_y*s->uvlinesize,
  1506. curframe->data[2] + 8*mb_y*s->uvlinesize
  1507. };
  1508. memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
  1509. memset(s->left_nnz, 0, sizeof(s->left_nnz));
  1510. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
  1511. // left edge of 129 for intra prediction
  1512. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1513. for (i = 0; i < 3; i++)
  1514. for (y = 0; y < 16>>!!i; y++)
  1515. dst[i][y*curframe->linesize[i]-1] = 129;
  1516. if (mb_y == 1) // top left edge is also 129
  1517. s->top_border[0][15] = s->top_border[0][23] = s->top_border[0][31] = 129;
  1518. }
  1519. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  1520. /* Prefetch the current frame, 4 MBs ahead */
  1521. s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  1522. s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
  1523. decode_mb_mode(s, mb, mb_x, mb_y, s->segmentation_map + mb_xy);
  1524. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
  1525. if (!mb->skip)
  1526. decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
  1527. if (mb->mode <= MODE_I4x4)
  1528. intra_predict(s, dst, mb, mb_x, mb_y);
  1529. else
  1530. inter_predict(s, dst, mb, mb_x, mb_y);
  1531. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
  1532. if (!mb->skip) {
  1533. idct_mb(s, dst, mb);
  1534. } else {
  1535. AV_ZERO64(s->left_nnz);
  1536. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  1537. // Reset DC block predictors if they would exist if the mb had coefficients
  1538. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  1539. s->left_nnz[8] = 0;
  1540. s->top_nnz[mb_x][8] = 0;
  1541. }
  1542. }
  1543. if (s->deblock_filter)
  1544. filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
  1545. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
  1546. dst[0] += 16;
  1547. dst[1] += 8;
  1548. dst[2] += 8;
  1549. }
  1550. if (s->deblock_filter) {
  1551. if (s->filter.simple)
  1552. filter_mb_row_simple(s, mb_y);
  1553. else
  1554. filter_mb_row(s, mb_y);
  1555. }
  1556. }
  1557. skip_decode:
  1558. // if future frames don't use the updated probabilities,
  1559. // reset them to the values we saved
  1560. if (!s->update_probabilities)
  1561. s->prob[0] = s->prob[1];
  1562. // check if golden and altref are swapped
  1563. if (s->update_altref == VP56_FRAME_GOLDEN &&
  1564. s->update_golden == VP56_FRAME_GOLDEN2)
  1565. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
  1566. else {
  1567. if (s->update_altref != VP56_FRAME_NONE)
  1568. s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  1569. if (s->update_golden != VP56_FRAME_NONE)
  1570. s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  1571. }
  1572. if (s->update_last) // move cur->prev
  1573. s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
  1574. // release no longer referenced frames
  1575. for (i = 0; i < 4; i++)
  1576. if (s->frames[i].data[0] &&
  1577. &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
  1578. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1579. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1580. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  1581. avctx->release_buffer(avctx, &s->frames[i]);
  1582. if (!s->invisible) {
  1583. *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
  1584. *data_size = sizeof(AVFrame);
  1585. }
  1586. return avpkt->size;
  1587. }
  1588. static av_cold int vp8_decode_init(AVCodecContext *avctx)
  1589. {
  1590. VP8Context *s = avctx->priv_data;
  1591. s->avctx = avctx;
  1592. avctx->pix_fmt = PIX_FMT_YUV420P;
  1593. dsputil_init(&s->dsp, avctx);
  1594. ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
  1595. ff_vp8dsp_init(&s->vp8dsp);
  1596. return 0;
  1597. }
  1598. static av_cold int vp8_decode_free(AVCodecContext *avctx)
  1599. {
  1600. vp8_decode_flush(avctx);
  1601. return 0;
  1602. }
  1603. AVCodec ff_vp8_decoder = {
  1604. "vp8",
  1605. AVMEDIA_TYPE_VIDEO,
  1606. CODEC_ID_VP8,
  1607. sizeof(VP8Context),
  1608. vp8_decode_init,
  1609. NULL,
  1610. vp8_decode_free,
  1611. vp8_decode_frame,
  1612. CODEC_CAP_DR1,
  1613. .flush = vp8_decode_flush,
  1614. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  1615. };