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.

1686 lines
61KB

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