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.

1870 lines
67KB

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