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.

1781 lines
64KB

  1. /**
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  6. * Copyright (C) 2010 Jason Garrett-Glaser
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "vp8.h"
  27. #include "vp8data.h"
  28. #include "rectangle.h"
  29. #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_uintp2(base_qi + ydc_delta , 7)];
  138. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi , 7)];
  139. s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)];
  140. s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)] / 100;
  141. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
  142. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
  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 qmul array holding the dc/ac dequant factor at position 0/1
  546. * @return 0 if no coeffs were decoded
  547. * otherwise, the index of the last coeff decoded plus one
  548. */
  549. static int decode_block_coeffs_internal(VP56RangeCoder *c, DCTELEM block[16],
  550. uint8_t probs[16][3][NUM_DCT_TOKENS-1],
  551. int i, uint8_t *token_prob, int16_t qmul[2])
  552. {
  553. goto skip_eob;
  554. do {
  555. int coeff;
  556. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  557. return i;
  558. skip_eob:
  559. if (!vp56_rac_get_prob_branchy(c, token_prob[1])) { // DCT_0
  560. if (++i == 16)
  561. return i; // invalid input; blocks should end with EOB
  562. token_prob = probs[i][0];
  563. goto skip_eob;
  564. }
  565. if (!vp56_rac_get_prob_branchy(c, token_prob[2])) { // DCT_1
  566. coeff = 1;
  567. token_prob = probs[i+1][1];
  568. } else {
  569. if (!vp56_rac_get_prob_branchy(c, token_prob[3])) { // DCT 2,3,4
  570. coeff = vp56_rac_get_prob_branchy(c, token_prob[4]);
  571. if (coeff)
  572. coeff += vp56_rac_get_prob(c, token_prob[5]);
  573. coeff += 2;
  574. } else {
  575. // DCT_CAT*
  576. if (!vp56_rac_get_prob_branchy(c, token_prob[6])) {
  577. if (!vp56_rac_get_prob_branchy(c, token_prob[7])) { // DCT_CAT1
  578. coeff = 5 + vp56_rac_get_prob(c, vp8_dct_cat1_prob[0]);
  579. } else { // DCT_CAT2
  580. coeff = 7;
  581. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[0]) << 1;
  582. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[1]);
  583. }
  584. } else { // DCT_CAT3 and up
  585. int a = vp56_rac_get_prob(c, token_prob[8]);
  586. int b = vp56_rac_get_prob(c, token_prob[9+a]);
  587. int cat = (a<<1) + b;
  588. coeff = 3 + (8<<cat);
  589. coeff += vp8_rac_get_coeff(c, ff_vp8_dct_cat_prob[cat]);
  590. }
  591. }
  592. token_prob = probs[i+1][2];
  593. }
  594. block[zigzag_scan[i]] = (vp8_rac_get(c) ? -coeff : coeff) * qmul[!!i];
  595. } while (++i < 16);
  596. return i;
  597. }
  598. #endif
  599. /**
  600. * @param c arithmetic bitstream reader context
  601. * @param block destination for block coefficients
  602. * @param probs probabilities to use when reading trees from the bitstream
  603. * @param i initial coeff index, 0 unless a separate DC block is coded
  604. * @param zero_nhood the initial prediction context for number of surrounding
  605. * all-zero blocks (only left/top, so 0-2)
  606. * @param qmul array holding the dc/ac dequant factor at position 0/1
  607. * @return 0 if no coeffs were decoded
  608. * otherwise, the index of the last coeff decoded plus one
  609. */
  610. static av_always_inline
  611. int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
  612. uint8_t probs[16][3][NUM_DCT_TOKENS-1],
  613. int i, int zero_nhood, int16_t qmul[2])
  614. {
  615. uint8_t *token_prob = probs[i][zero_nhood];
  616. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  617. return 0;
  618. return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
  619. }
  620. static av_always_inline
  621. void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  622. uint8_t t_nnz[9], uint8_t l_nnz[9])
  623. {
  624. int i, x, y, luma_start = 0, luma_ctx = 3;
  625. int nnz_pred, nnz, nnz_total = 0;
  626. int segment = s->segment;
  627. int block_dc = 0;
  628. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  629. nnz_pred = t_nnz[8] + l_nnz[8];
  630. // decode DC values and do hadamard
  631. nnz = decode_block_coeffs(c, s->block_dc, s->prob->token[1], 0, nnz_pred,
  632. s->qmat[segment].luma_dc_qmul);
  633. l_nnz[8] = t_nnz[8] = !!nnz;
  634. if (nnz) {
  635. nnz_total += nnz;
  636. block_dc = 1;
  637. if (nnz == 1)
  638. s->vp8dsp.vp8_luma_dc_wht_dc(s->block, s->block_dc);
  639. else
  640. s->vp8dsp.vp8_luma_dc_wht(s->block, s->block_dc);
  641. }
  642. luma_start = 1;
  643. luma_ctx = 0;
  644. }
  645. // luma blocks
  646. for (y = 0; y < 4; y++)
  647. for (x = 0; x < 4; x++) {
  648. nnz_pred = l_nnz[y] + t_nnz[x];
  649. nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
  650. nnz_pred, s->qmat[segment].luma_qmul);
  651. // nnz+block_dc may be one more than the actual last index, but we don't care
  652. s->non_zero_count_cache[y][x] = nnz + block_dc;
  653. t_nnz[x] = l_nnz[y] = !!nnz;
  654. nnz_total += nnz;
  655. }
  656. // chroma blocks
  657. // TODO: what to do about dimensions? 2nd dim for luma is x,
  658. // but for chroma it's (y<<1)|x
  659. for (i = 4; i < 6; i++)
  660. for (y = 0; y < 2; y++)
  661. for (x = 0; x < 2; x++) {
  662. nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
  663. nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
  664. nnz_pred, s->qmat[segment].chroma_qmul);
  665. s->non_zero_count_cache[i][(y<<1)+x] = nnz;
  666. t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
  667. nnz_total += nnz;
  668. }
  669. // if there were no coded coeffs despite the macroblock not being marked skip,
  670. // we MUST not do the inner loop filter and should not do IDCT
  671. // Since skip isn't used for bitstream prediction, just manually set it.
  672. if (!nnz_total)
  673. mb->skip = 1;
  674. }
  675. static av_always_inline
  676. void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
  677. int linesize, int uvlinesize, int simple)
  678. {
  679. AV_COPY128(top_border, src_y + 15*linesize);
  680. if (!simple) {
  681. AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
  682. AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
  683. }
  684. }
  685. static av_always_inline
  686. void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
  687. int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
  688. int simple, int xchg)
  689. {
  690. uint8_t *top_border_m1 = top_border-32; // for TL prediction
  691. src_y -= linesize;
  692. src_cb -= uvlinesize;
  693. src_cr -= uvlinesize;
  694. #define XCHG(a,b,xchg) do { \
  695. if (xchg) AV_SWAP64(b,a); \
  696. else AV_COPY64(b,a); \
  697. } while (0)
  698. XCHG(top_border_m1+8, src_y-8, xchg);
  699. XCHG(top_border, src_y, xchg);
  700. XCHG(top_border+8, src_y+8, 1);
  701. if (mb_x < mb_width-1)
  702. XCHG(top_border+32, src_y+16, 1);
  703. // only copy chroma for normal loop filter
  704. // or to initialize the top row to 127
  705. if (!simple || !mb_y) {
  706. XCHG(top_border_m1+16, src_cb-8, xchg);
  707. XCHG(top_border_m1+24, src_cr-8, xchg);
  708. XCHG(top_border+16, src_cb, 1);
  709. XCHG(top_border+24, src_cr, 1);
  710. }
  711. }
  712. static av_always_inline
  713. int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
  714. {
  715. if (!mb_x) {
  716. return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
  717. } else {
  718. return mb_y ? mode : LEFT_DC_PRED8x8;
  719. }
  720. }
  721. static av_always_inline
  722. int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
  723. {
  724. if (!mb_x) {
  725. return mb_y ? VERT_PRED8x8 : DC_129_PRED8x8;
  726. } else {
  727. return mb_y ? mode : HOR_PRED8x8;
  728. }
  729. }
  730. static av_always_inline
  731. int check_intra_pred8x8_mode(int mode, int mb_x, int mb_y)
  732. {
  733. if (mode == DC_PRED8x8) {
  734. return check_dc_pred8x8_mode(mode, mb_x, mb_y);
  735. } else {
  736. return mode;
  737. }
  738. }
  739. static av_always_inline
  740. int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
  741. {
  742. switch (mode) {
  743. case DC_PRED8x8:
  744. return check_dc_pred8x8_mode(mode, mb_x, mb_y);
  745. case VERT_PRED8x8:
  746. return !mb_y ? DC_127_PRED8x8 : mode;
  747. case HOR_PRED8x8:
  748. return !mb_x ? DC_129_PRED8x8 : mode;
  749. case PLANE_PRED8x8 /*TM*/:
  750. return check_tm_pred8x8_mode(mode, mb_x, mb_y);
  751. }
  752. return mode;
  753. }
  754. static av_always_inline
  755. int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
  756. {
  757. if (!mb_x) {
  758. return mb_y ? VERT_VP8_PRED : DC_129_PRED;
  759. } else {
  760. return mb_y ? mode : HOR_VP8_PRED;
  761. }
  762. }
  763. static av_always_inline
  764. int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
  765. {
  766. switch (mode) {
  767. case VERT_PRED:
  768. if (!mb_x && mb_y) {
  769. *copy_buf = 1;
  770. return mode;
  771. }
  772. /* fall-through */
  773. case DIAG_DOWN_LEFT_PRED:
  774. case VERT_LEFT_PRED:
  775. return !mb_y ? DC_127_PRED : mode;
  776. case HOR_PRED:
  777. if (!mb_y) {
  778. *copy_buf = 1;
  779. return mode;
  780. }
  781. /* fall-through */
  782. case HOR_UP_PRED:
  783. return !mb_x ? DC_129_PRED : mode;
  784. case TM_VP8_PRED:
  785. return check_tm_pred4x4_mode(mode, mb_x, mb_y);
  786. case DC_PRED: // 4x4 DC doesn't use the same "H.264-style" exceptions as 16x16/8x8 DC
  787. case DIAG_DOWN_RIGHT_PRED:
  788. case VERT_RIGHT_PRED:
  789. case HOR_DOWN_PRED:
  790. if (!mb_y || !mb_x)
  791. *copy_buf = 1;
  792. return mode;
  793. }
  794. return mode;
  795. }
  796. static av_always_inline
  797. void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  798. int mb_x, int mb_y)
  799. {
  800. AVCodecContext *avctx = s->avctx;
  801. int x, y, mode, nnz;
  802. uint32_t tr;
  803. // for the first row, we need to run xchg_mb_border to init the top edge to 127
  804. // otherwise, skip it if we aren't going to deblock
  805. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y))
  806. xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
  807. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  808. s->filter.simple, 1);
  809. if (mb->mode < MODE_I4x4) {
  810. if (avctx->flags & CODEC_FLAG_EMU_EDGE) { // tested
  811. mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y);
  812. } else {
  813. mode = check_intra_pred8x8_mode(mb->mode, mb_x, mb_y);
  814. }
  815. s->hpc.pred16x16[mode](dst[0], s->linesize);
  816. } else {
  817. uint8_t *ptr = dst[0];
  818. uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
  819. uint8_t tr_top[4] = { 127, 127, 127, 127 };
  820. // all blocks on the right edge of the macroblock use bottom edge
  821. // the top macroblock for their topright edge
  822. uint8_t *tr_right = ptr - s->linesize + 16;
  823. // if we're on the right edge of the frame, said edge is extended
  824. // from the top macroblock
  825. if (!(!mb_y && avctx->flags & CODEC_FLAG_EMU_EDGE) &&
  826. mb_x == s->mb_width-1) {
  827. tr = tr_right[-1]*0x01010101u;
  828. tr_right = (uint8_t *)&tr;
  829. }
  830. if (mb->skip)
  831. AV_ZERO128(s->non_zero_count_cache);
  832. for (y = 0; y < 4; y++) {
  833. uint8_t *topright = ptr + 4 - s->linesize;
  834. for (x = 0; x < 4; x++) {
  835. int copy = 0, linesize = s->linesize;
  836. uint8_t *dst = ptr+4*x;
  837. DECLARE_ALIGNED(4, uint8_t, copy_dst)[5*8];
  838. if ((y == 0 || x == 3) && mb_y == 0 && avctx->flags & CODEC_FLAG_EMU_EDGE) {
  839. topright = tr_top;
  840. } else if (x == 3)
  841. topright = tr_right;
  842. if (avctx->flags & CODEC_FLAG_EMU_EDGE) { // mb_x+x or mb_y+y is a hack but works
  843. mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x, mb_y + y, &copy);
  844. if (copy) {
  845. dst = copy_dst + 12;
  846. linesize = 8;
  847. if (!(mb_y + y)) {
  848. copy_dst[3] = 127U;
  849. AV_WN32A(copy_dst+4, 127U * 0x01010101U);
  850. } else {
  851. AV_COPY32(copy_dst+4, ptr+4*x-s->linesize);
  852. if (!(mb_x + x)) {
  853. copy_dst[3] = 129U;
  854. } else {
  855. copy_dst[3] = ptr[4*x-s->linesize-1];
  856. }
  857. }
  858. if (!(mb_x + x)) {
  859. copy_dst[11] =
  860. copy_dst[19] =
  861. copy_dst[27] =
  862. copy_dst[35] = 129U;
  863. } else {
  864. copy_dst[11] = ptr[4*x -1];
  865. copy_dst[19] = ptr[4*x+s->linesize -1];
  866. copy_dst[27] = ptr[4*x+s->linesize*2-1];
  867. copy_dst[35] = ptr[4*x+s->linesize*3-1];
  868. }
  869. }
  870. } else {
  871. mode = intra4x4[x];
  872. }
  873. s->hpc.pred4x4[mode](dst, topright, linesize);
  874. if (copy) {
  875. AV_COPY32(ptr+4*x , copy_dst+12);
  876. AV_COPY32(ptr+4*x+s->linesize , copy_dst+20);
  877. AV_COPY32(ptr+4*x+s->linesize*2, copy_dst+28);
  878. AV_COPY32(ptr+4*x+s->linesize*3, copy_dst+36);
  879. }
  880. nnz = s->non_zero_count_cache[y][x];
  881. if (nnz) {
  882. if (nnz == 1)
  883. s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
  884. else
  885. s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
  886. }
  887. topright += 4;
  888. }
  889. ptr += 4*s->linesize;
  890. intra4x4 += 4;
  891. }
  892. }
  893. if (avctx->flags & CODEC_FLAG_EMU_EDGE) {
  894. mode = check_intra_pred8x8_mode_emuedge(s->chroma_pred_mode, mb_x, mb_y);
  895. } else {
  896. mode = check_intra_pred8x8_mode(s->chroma_pred_mode, mb_x, mb_y);
  897. }
  898. s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
  899. s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
  900. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y))
  901. xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
  902. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  903. s->filter.simple, 0);
  904. }
  905. static const uint8_t subpel_idx[3][8] = {
  906. { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
  907. // also function pointer index
  908. { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
  909. { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
  910. };
  911. /**
  912. * luma MC function
  913. *
  914. * @param s VP8 decoding context
  915. * @param dst target buffer for block data at block position
  916. * @param ref reference picture buffer at origin (0, 0)
  917. * @param mv motion vector (relative to block position) to get pixel data from
  918. * @param x_off horizontal position of block from origin (0, 0)
  919. * @param y_off vertical position of block from origin (0, 0)
  920. * @param block_w width of block (16, 8 or 4)
  921. * @param block_h height of block (always same as block_w)
  922. * @param width width of src/dst plane data
  923. * @param height height of src/dst plane data
  924. * @param linesize size of a single line of plane data, including padding
  925. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  926. */
  927. static av_always_inline
  928. void vp8_mc_luma(VP8Context *s, uint8_t *dst, AVFrame *ref, const VP56mv *mv,
  929. int x_off, int y_off, int block_w, int block_h,
  930. int width, int height, int linesize,
  931. vp8_mc_func mc_func[3][3])
  932. {
  933. uint8_t *src = ref->data[0];
  934. if (AV_RN32A(mv)) {
  935. int mx = (mv->x << 1)&7, mx_idx = subpel_idx[0][mx];
  936. int my = (mv->y << 1)&7, my_idx = subpel_idx[0][my];
  937. x_off += mv->x >> 2;
  938. y_off += mv->y >> 2;
  939. // edge emulation
  940. ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
  941. src += y_off * linesize + x_off;
  942. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  943. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  944. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src - my_idx * linesize - mx_idx, linesize,
  945. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  946. x_off - mx_idx, y_off - my_idx, width, height);
  947. src = s->edge_emu_buffer + mx_idx + linesize * my_idx;
  948. }
  949. mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
  950. } else {
  951. ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
  952. mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
  953. }
  954. }
  955. /**
  956. * chroma MC function
  957. *
  958. * @param s VP8 decoding context
  959. * @param dst1 target buffer for block data at block position (U plane)
  960. * @param dst2 target buffer for block data at block position (V plane)
  961. * @param ref reference picture buffer at origin (0, 0)
  962. * @param mv motion vector (relative to block position) to get pixel data from
  963. * @param x_off horizontal position of block from origin (0, 0)
  964. * @param y_off vertical position of block from origin (0, 0)
  965. * @param block_w width of block (16, 8 or 4)
  966. * @param block_h height of block (always same as block_w)
  967. * @param width width of src/dst plane data
  968. * @param height height of src/dst plane data
  969. * @param linesize size of a single line of plane data, including padding
  970. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  971. */
  972. static av_always_inline
  973. void vp8_mc_chroma(VP8Context *s, uint8_t *dst1, uint8_t *dst2, AVFrame *ref,
  974. const VP56mv *mv, int x_off, int y_off,
  975. int block_w, int block_h, int width, int height, int linesize,
  976. vp8_mc_func mc_func[3][3])
  977. {
  978. uint8_t *src1 = ref->data[1], *src2 = ref->data[2];
  979. if (AV_RN32A(mv)) {
  980. int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
  981. int my = mv->y&7, my_idx = subpel_idx[0][my];
  982. x_off += mv->x >> 3;
  983. y_off += mv->y >> 3;
  984. // edge emulation
  985. src1 += y_off * linesize + x_off;
  986. src2 += y_off * linesize + x_off;
  987. ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
  988. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  989. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  990. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src1 - my_idx * linesize - mx_idx, linesize,
  991. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  992. x_off - mx_idx, y_off - my_idx, width, height);
  993. src1 = s->edge_emu_buffer + mx_idx + linesize * my_idx;
  994. mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
  995. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src2 - 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. src2 = s->edge_emu_buffer + mx_idx + linesize * my_idx;
  999. mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
  1000. } else {
  1001. mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
  1002. mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
  1003. }
  1004. } else {
  1005. ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
  1006. mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1007. mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1008. }
  1009. }
  1010. static av_always_inline
  1011. void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
  1012. AVFrame *ref_frame, int x_off, int y_off,
  1013. int bx_off, int by_off,
  1014. int block_w, int block_h,
  1015. int width, int height, VP56mv *mv)
  1016. {
  1017. VP56mv uvmv = *mv;
  1018. /* Y */
  1019. vp8_mc_luma(s, dst[0] + by_off * s->linesize + bx_off,
  1020. ref_frame, mv, x_off + bx_off, y_off + by_off,
  1021. block_w, block_h, width, height, s->linesize,
  1022. s->put_pixels_tab[block_w == 8]);
  1023. /* U/V */
  1024. if (s->profile == 3) {
  1025. uvmv.x &= ~7;
  1026. uvmv.y &= ~7;
  1027. }
  1028. x_off >>= 1; y_off >>= 1;
  1029. bx_off >>= 1; by_off >>= 1;
  1030. width >>= 1; height >>= 1;
  1031. block_w >>= 1; block_h >>= 1;
  1032. vp8_mc_chroma(s, dst[1] + by_off * s->uvlinesize + bx_off,
  1033. dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
  1034. &uvmv, x_off + bx_off, y_off + by_off,
  1035. block_w, block_h, width, height, s->uvlinesize,
  1036. s->put_pixels_tab[1 + (block_w == 4)]);
  1037. }
  1038. /* Fetch pixels for estimated mv 4 macroblocks ahead.
  1039. * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
  1040. static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
  1041. {
  1042. /* Don't prefetch refs that haven't been used very often this frame. */
  1043. if (s->ref_count[ref-1] > (mb_xy >> 5)) {
  1044. int x_off = mb_x << 4, y_off = mb_y << 4;
  1045. int mx = (mb->mv.x>>2) + x_off + 8;
  1046. int my = (mb->mv.y>>2) + y_off;
  1047. uint8_t **src= s->framep[ref]->data;
  1048. int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
  1049. /* For threading, a ff_thread_await_progress here might be useful, but
  1050. * it actually slows down the decoder. Since a bad prefetch doesn't
  1051. * generate bad decoder output, we don't run it here. */
  1052. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1053. off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
  1054. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1055. }
  1056. }
  1057. /**
  1058. * Apply motion vectors to prediction buffer, chapter 18.
  1059. */
  1060. static av_always_inline
  1061. void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  1062. int mb_x, int mb_y)
  1063. {
  1064. int x_off = mb_x << 4, y_off = mb_y << 4;
  1065. int width = 16*s->mb_width, height = 16*s->mb_height;
  1066. AVFrame *ref = s->framep[mb->ref_frame];
  1067. VP56mv *bmv = mb->bmv;
  1068. switch (mb->partitioning) {
  1069. case VP8_SPLITMVMODE_NONE:
  1070. vp8_mc_part(s, dst, ref, x_off, y_off,
  1071. 0, 0, 16, 16, width, height, &mb->mv);
  1072. break;
  1073. case VP8_SPLITMVMODE_4x4: {
  1074. int x, y;
  1075. VP56mv uvmv;
  1076. /* Y */
  1077. for (y = 0; y < 4; y++) {
  1078. for (x = 0; x < 4; x++) {
  1079. vp8_mc_luma(s, dst[0] + 4*y*s->linesize + x*4,
  1080. ref, &bmv[4*y + x],
  1081. 4*x + x_off, 4*y + y_off, 4, 4,
  1082. width, height, s->linesize,
  1083. s->put_pixels_tab[2]);
  1084. }
  1085. }
  1086. /* U/V */
  1087. x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
  1088. for (y = 0; y < 2; y++) {
  1089. for (x = 0; x < 2; x++) {
  1090. uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
  1091. mb->bmv[ 2*y * 4 + 2*x+1].x +
  1092. mb->bmv[(2*y+1) * 4 + 2*x ].x +
  1093. mb->bmv[(2*y+1) * 4 + 2*x+1].x;
  1094. uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
  1095. mb->bmv[ 2*y * 4 + 2*x+1].y +
  1096. mb->bmv[(2*y+1) * 4 + 2*x ].y +
  1097. mb->bmv[(2*y+1) * 4 + 2*x+1].y;
  1098. uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
  1099. uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
  1100. if (s->profile == 3) {
  1101. uvmv.x &= ~7;
  1102. uvmv.y &= ~7;
  1103. }
  1104. vp8_mc_chroma(s, dst[1] + 4*y*s->uvlinesize + x*4,
  1105. dst[2] + 4*y*s->uvlinesize + x*4, ref, &uvmv,
  1106. 4*x + x_off, 4*y + y_off, 4, 4,
  1107. width, height, s->uvlinesize,
  1108. s->put_pixels_tab[2]);
  1109. }
  1110. }
  1111. break;
  1112. }
  1113. case VP8_SPLITMVMODE_16x8:
  1114. vp8_mc_part(s, dst, ref, x_off, y_off,
  1115. 0, 0, 16, 8, width, height, &bmv[0]);
  1116. vp8_mc_part(s, dst, ref, x_off, y_off,
  1117. 0, 8, 16, 8, width, height, &bmv[1]);
  1118. break;
  1119. case VP8_SPLITMVMODE_8x16:
  1120. vp8_mc_part(s, dst, ref, x_off, y_off,
  1121. 0, 0, 8, 16, width, height, &bmv[0]);
  1122. vp8_mc_part(s, dst, ref, x_off, y_off,
  1123. 8, 0, 8, 16, width, height, &bmv[1]);
  1124. break;
  1125. case VP8_SPLITMVMODE_8x8:
  1126. vp8_mc_part(s, dst, ref, x_off, y_off,
  1127. 0, 0, 8, 8, width, height, &bmv[0]);
  1128. vp8_mc_part(s, dst, ref, x_off, y_off,
  1129. 8, 0, 8, 8, width, height, &bmv[1]);
  1130. vp8_mc_part(s, dst, ref, x_off, y_off,
  1131. 0, 8, 8, 8, width, height, &bmv[2]);
  1132. vp8_mc_part(s, dst, ref, x_off, y_off,
  1133. 8, 8, 8, 8, width, height, &bmv[3]);
  1134. break;
  1135. }
  1136. }
  1137. static av_always_inline void idct_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb)
  1138. {
  1139. int x, y, ch;
  1140. if (mb->mode != MODE_I4x4) {
  1141. uint8_t *y_dst = dst[0];
  1142. for (y = 0; y < 4; y++) {
  1143. uint32_t nnz4 = AV_RL32(s->non_zero_count_cache[y]);
  1144. if (nnz4) {
  1145. if (nnz4&~0x01010101) {
  1146. for (x = 0; x < 4; x++) {
  1147. if ((uint8_t)nnz4 == 1)
  1148. s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
  1149. else if((uint8_t)nnz4 > 1)
  1150. s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
  1151. nnz4 >>= 8;
  1152. if (!nnz4)
  1153. break;
  1154. }
  1155. } else {
  1156. s->vp8dsp.vp8_idct_dc_add4y(y_dst, s->block[y], s->linesize);
  1157. }
  1158. }
  1159. y_dst += 4*s->linesize;
  1160. }
  1161. }
  1162. for (ch = 0; ch < 2; ch++) {
  1163. uint32_t nnz4 = AV_RL32(s->non_zero_count_cache[4+ch]);
  1164. if (nnz4) {
  1165. uint8_t *ch_dst = dst[1+ch];
  1166. if (nnz4&~0x01010101) {
  1167. for (y = 0; y < 2; y++) {
  1168. for (x = 0; x < 2; x++) {
  1169. if ((uint8_t)nnz4 == 1)
  1170. s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1171. else if((uint8_t)nnz4 > 1)
  1172. s->vp8dsp.vp8_idct_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1173. nnz4 >>= 8;
  1174. if (!nnz4)
  1175. goto chroma_idct_end;
  1176. }
  1177. ch_dst += 4*s->uvlinesize;
  1178. }
  1179. } else {
  1180. s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, s->block[4+ch], s->uvlinesize);
  1181. }
  1182. }
  1183. chroma_idct_end: ;
  1184. }
  1185. }
  1186. static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
  1187. {
  1188. int interior_limit, filter_level;
  1189. if (s->segmentation.enabled) {
  1190. filter_level = s->segmentation.filter_level[s->segment];
  1191. if (!s->segmentation.absolute_vals)
  1192. filter_level += s->filter.level;
  1193. } else
  1194. filter_level = s->filter.level;
  1195. if (s->lf_delta.enabled) {
  1196. filter_level += s->lf_delta.ref[mb->ref_frame];
  1197. filter_level += s->lf_delta.mode[mb->mode];
  1198. }
  1199. filter_level = av_clip_uintp2(filter_level, 6);
  1200. interior_limit = filter_level;
  1201. if (s->filter.sharpness) {
  1202. interior_limit >>= (s->filter.sharpness + 3) >> 2;
  1203. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  1204. }
  1205. interior_limit = FFMAX(interior_limit, 1);
  1206. f->filter_level = filter_level;
  1207. f->inner_limit = interior_limit;
  1208. f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
  1209. }
  1210. static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
  1211. {
  1212. int mbedge_lim, bedge_lim, hev_thresh;
  1213. int filter_level = f->filter_level;
  1214. int inner_limit = f->inner_limit;
  1215. int inner_filter = f->inner_filter;
  1216. int linesize = s->linesize;
  1217. int uvlinesize = s->uvlinesize;
  1218. static const uint8_t hev_thresh_lut[2][64] = {
  1219. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1220. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1221. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  1222. 3, 3, 3, 3 },
  1223. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1224. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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. 2, 2, 2, 2 }
  1227. };
  1228. if (!filter_level)
  1229. return;
  1230. bedge_lim = 2*filter_level + inner_limit;
  1231. mbedge_lim = bedge_lim + 4;
  1232. hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
  1233. if (mb_x) {
  1234. s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
  1235. mbedge_lim, inner_limit, hev_thresh);
  1236. s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1237. mbedge_lim, inner_limit, hev_thresh);
  1238. }
  1239. if (inner_filter) {
  1240. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
  1241. inner_limit, hev_thresh);
  1242. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
  1243. inner_limit, hev_thresh);
  1244. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
  1245. inner_limit, hev_thresh);
  1246. s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
  1247. uvlinesize, bedge_lim,
  1248. inner_limit, hev_thresh);
  1249. }
  1250. if (mb_y) {
  1251. s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
  1252. mbedge_lim, inner_limit, hev_thresh);
  1253. s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1254. mbedge_lim, inner_limit, hev_thresh);
  1255. }
  1256. if (inner_filter) {
  1257. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
  1258. linesize, bedge_lim,
  1259. inner_limit, hev_thresh);
  1260. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
  1261. linesize, bedge_lim,
  1262. inner_limit, hev_thresh);
  1263. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
  1264. linesize, bedge_lim,
  1265. inner_limit, hev_thresh);
  1266. s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
  1267. dst[2] + 4 * uvlinesize,
  1268. uvlinesize, bedge_lim,
  1269. inner_limit, hev_thresh);
  1270. }
  1271. }
  1272. static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
  1273. {
  1274. int mbedge_lim, bedge_lim;
  1275. int filter_level = f->filter_level;
  1276. int inner_limit = f->inner_limit;
  1277. int inner_filter = f->inner_filter;
  1278. int linesize = s->linesize;
  1279. if (!filter_level)
  1280. return;
  1281. bedge_lim = 2*filter_level + inner_limit;
  1282. mbedge_lim = bedge_lim + 4;
  1283. if (mb_x)
  1284. s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
  1285. if (inner_filter) {
  1286. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
  1287. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
  1288. s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
  1289. }
  1290. if (mb_y)
  1291. s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
  1292. if (inner_filter) {
  1293. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
  1294. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
  1295. s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
  1296. }
  1297. }
  1298. static void filter_mb_row(VP8Context *s, AVFrame *curframe, int mb_y)
  1299. {
  1300. VP8FilterStrength *f = s->filter_strength;
  1301. uint8_t *dst[3] = {
  1302. curframe->data[0] + 16*mb_y*s->linesize,
  1303. curframe->data[1] + 8*mb_y*s->uvlinesize,
  1304. curframe->data[2] + 8*mb_y*s->uvlinesize
  1305. };
  1306. int mb_x;
  1307. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1308. backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  1309. filter_mb(s, dst, f++, mb_x, mb_y);
  1310. dst[0] += 16;
  1311. dst[1] += 8;
  1312. dst[2] += 8;
  1313. }
  1314. }
  1315. static void filter_mb_row_simple(VP8Context *s, AVFrame *curframe, int mb_y)
  1316. {
  1317. VP8FilterStrength *f = s->filter_strength;
  1318. uint8_t *dst = curframe->data[0] + 16*mb_y*s->linesize;
  1319. int mb_x;
  1320. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1321. backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
  1322. filter_mb_simple(s, dst, f++, mb_x, mb_y);
  1323. dst += 16;
  1324. }
  1325. }
  1326. static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  1327. AVPacket *avpkt)
  1328. {
  1329. VP8Context *s = avctx->priv_data;
  1330. int ret, mb_x, mb_y, i, y, referenced;
  1331. enum AVDiscard skip_thresh;
  1332. AVFrame *av_uninit(curframe), *prev_frame = s->framep[VP56_FRAME_CURRENT];
  1333. if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
  1334. return ret;
  1335. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
  1336. || s->update_altref == VP56_FRAME_CURRENT;
  1337. skip_thresh = !referenced ? AVDISCARD_NONREF :
  1338. !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
  1339. if (avctx->skip_frame >= skip_thresh) {
  1340. s->invisible = 1;
  1341. goto skip_decode;
  1342. }
  1343. s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
  1344. // release no longer referenced frames
  1345. for (i = 0; i < 5; i++)
  1346. if (s->frames[i].data[0] &&
  1347. &s->frames[i] != prev_frame &&
  1348. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1349. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1350. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  1351. ff_thread_release_buffer(avctx, &s->frames[i]);
  1352. // find a free buffer
  1353. for (i = 0; i < 5; i++)
  1354. if (&s->frames[i] != prev_frame &&
  1355. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1356. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1357. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  1358. curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
  1359. break;
  1360. }
  1361. if (i == 5) {
  1362. av_log(avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
  1363. abort();
  1364. }
  1365. if (curframe->data[0])
  1366. ff_thread_release_buffer(avctx, curframe);
  1367. curframe->key_frame = s->keyframe;
  1368. curframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1369. curframe->reference = referenced ? 3 : 0;
  1370. curframe->ref_index[0] = s->segmentation_map;
  1371. if ((ret = ff_thread_get_buffer(avctx, curframe))) {
  1372. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
  1373. return ret;
  1374. }
  1375. // check if golden and altref are swapped
  1376. if (s->update_altref != VP56_FRAME_NONE) {
  1377. s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  1378. } else {
  1379. s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
  1380. }
  1381. if (s->update_golden != VP56_FRAME_NONE) {
  1382. s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  1383. } else {
  1384. s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
  1385. }
  1386. if (s->update_last) {
  1387. s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
  1388. } else {
  1389. s->next_framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_PREVIOUS];
  1390. }
  1391. s->next_framep[VP56_FRAME_CURRENT] = curframe;
  1392. ff_thread_finish_setup(avctx);
  1393. // Given that arithmetic probabilities are updated every frame, it's quite likely
  1394. // that the values we have on a random interframe are complete junk if we didn't
  1395. // start decode on a keyframe. So just don't display anything rather than junk.
  1396. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  1397. !s->framep[VP56_FRAME_GOLDEN] ||
  1398. !s->framep[VP56_FRAME_GOLDEN2])) {
  1399. av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
  1400. return AVERROR_INVALIDDATA;
  1401. }
  1402. s->linesize = curframe->linesize[0];
  1403. s->uvlinesize = curframe->linesize[1];
  1404. if (!s->edge_emu_buffer)
  1405. s->edge_emu_buffer = av_malloc(21*s->linesize);
  1406. memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
  1407. /* Zero macroblock structures for top/top-left prediction from outside the frame. */
  1408. memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
  1409. // top edge of 127 for intra prediction
  1410. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1411. s->top_border[0][15] = s->top_border[0][23] = 127;
  1412. memset(s->top_border[1]-1, 127, s->mb_width*sizeof(*s->top_border)+1);
  1413. }
  1414. memset(s->ref_count, 0, sizeof(s->ref_count));
  1415. if (s->keyframe)
  1416. memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
  1417. #define MARGIN (16 << 2)
  1418. s->mv_min.y = -MARGIN;
  1419. s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
  1420. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1421. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
  1422. VP8Macroblock *mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
  1423. int mb_xy = mb_y*s->mb_width;
  1424. uint8_t *dst[3] = {
  1425. curframe->data[0] + 16*mb_y*s->linesize,
  1426. curframe->data[1] + 8*mb_y*s->uvlinesize,
  1427. curframe->data[2] + 8*mb_y*s->uvlinesize
  1428. };
  1429. memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
  1430. memset(s->left_nnz, 0, sizeof(s->left_nnz));
  1431. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
  1432. // left edge of 129 for intra prediction
  1433. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1434. for (i = 0; i < 3; i++)
  1435. for (y = 0; y < 16>>!!i; y++)
  1436. dst[i][y*curframe->linesize[i]-1] = 129;
  1437. if (mb_y == 1) // top left edge is also 129
  1438. s->top_border[0][15] = s->top_border[0][23] = s->top_border[0][31] = 129;
  1439. }
  1440. s->mv_min.x = -MARGIN;
  1441. s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
  1442. if (prev_frame && s->segmentation.enabled && !s->segmentation.update_map)
  1443. ff_thread_await_progress(prev_frame, mb_y, 0);
  1444. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  1445. /* Prefetch the current frame, 4 MBs ahead */
  1446. s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  1447. s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
  1448. decode_mb_mode(s, mb, mb_x, mb_y, s->segmentation_map + mb_xy,
  1449. prev_frame ? prev_frame->ref_index[0] + mb_xy : NULL);
  1450. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
  1451. if (!mb->skip)
  1452. decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
  1453. if (mb->mode <= MODE_I4x4)
  1454. intra_predict(s, dst, mb, mb_x, mb_y);
  1455. else
  1456. inter_predict(s, dst, mb, mb_x, mb_y);
  1457. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
  1458. if (!mb->skip) {
  1459. idct_mb(s, dst, mb);
  1460. } else {
  1461. AV_ZERO64(s->left_nnz);
  1462. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  1463. // Reset DC block predictors if they would exist if the mb had coefficients
  1464. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  1465. s->left_nnz[8] = 0;
  1466. s->top_nnz[mb_x][8] = 0;
  1467. }
  1468. }
  1469. if (s->deblock_filter)
  1470. filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
  1471. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
  1472. dst[0] += 16;
  1473. dst[1] += 8;
  1474. dst[2] += 8;
  1475. s->mv_min.x -= 64;
  1476. s->mv_max.x -= 64;
  1477. }
  1478. if (s->deblock_filter) {
  1479. if (s->filter.simple)
  1480. filter_mb_row_simple(s, curframe, mb_y);
  1481. else
  1482. filter_mb_row(s, curframe, mb_y);
  1483. }
  1484. s->mv_min.y -= 64;
  1485. s->mv_max.y -= 64;
  1486. ff_thread_report_progress(curframe, mb_y, 0);
  1487. }
  1488. ff_thread_report_progress(curframe, INT_MAX, 0);
  1489. skip_decode:
  1490. // if future frames don't use the updated probabilities,
  1491. // reset them to the values we saved
  1492. if (!s->update_probabilities)
  1493. s->prob[0] = s->prob[1];
  1494. memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
  1495. if (!s->invisible) {
  1496. *(AVFrame*)data = *curframe;
  1497. *data_size = sizeof(AVFrame);
  1498. }
  1499. return avpkt->size;
  1500. }
  1501. static av_cold int vp8_decode_init(AVCodecContext *avctx)
  1502. {
  1503. VP8Context *s = avctx->priv_data;
  1504. s->avctx = avctx;
  1505. avctx->pix_fmt = PIX_FMT_YUV420P;
  1506. dsputil_init(&s->dsp, avctx);
  1507. ff_h264_pred_init(&s->hpc, CODEC_ID_VP8, 8, 1);
  1508. ff_vp8dsp_init(&s->vp8dsp);
  1509. return 0;
  1510. }
  1511. static av_cold int vp8_decode_free(AVCodecContext *avctx)
  1512. {
  1513. vp8_decode_flush(avctx);
  1514. return 0;
  1515. }
  1516. static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
  1517. {
  1518. VP8Context *s = avctx->priv_data;
  1519. s->avctx = avctx;
  1520. return 0;
  1521. }
  1522. #define REBASE(pic) \
  1523. pic ? pic - &s_src->frames[0] + &s->frames[0] : NULL
  1524. static int vp8_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1525. {
  1526. VP8Context *s = dst->priv_data, *s_src = src->priv_data;
  1527. s->prob[0] = s_src->prob[!s_src->update_probabilities];
  1528. s->segmentation = s_src->segmentation;
  1529. s->lf_delta = s_src->lf_delta;
  1530. memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
  1531. memcpy(&s->frames, &s_src->frames, sizeof(s->frames));
  1532. s->framep[0] = REBASE(s_src->next_framep[0]);
  1533. s->framep[1] = REBASE(s_src->next_framep[1]);
  1534. s->framep[2] = REBASE(s_src->next_framep[2]);
  1535. s->framep[3] = REBASE(s_src->next_framep[3]);
  1536. return 0;
  1537. }
  1538. AVCodec ff_vp8_decoder = {
  1539. .name = "vp8",
  1540. .type = AVMEDIA_TYPE_VIDEO,
  1541. .id = CODEC_ID_VP8,
  1542. .priv_data_size = sizeof(VP8Context),
  1543. .init = vp8_decode_init,
  1544. .close = vp8_decode_free,
  1545. .decode = vp8_decode_frame,
  1546. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  1547. .flush = vp8_decode_flush,
  1548. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  1549. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
  1550. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
  1551. };