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.

1846 lines
67KB

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