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.

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