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