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.

1867 lines
67KB

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