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.

2099 lines
75KB

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