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.

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