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.

2184 lines
78KB

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