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.

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