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.

1792 lines
65KB

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