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.

2086 lines
75KB

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