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.

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