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.

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