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.

2122 lines
76KB

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