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.

2086 lines
74KB

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