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.

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