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.

2112 lines
75KB

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