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.

1871 lines
68KB

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