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