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.

1758 lines
63KB

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