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.

1834 lines
66KB

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