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.

2169 lines
79KB

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