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.

2780 lines
100KB

  1. /*
  2. * VP7/VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  6. * Copyright (C) 2010 Fiona Glaser
  7. * Copyright (C) 2012 Daniel Kang
  8. * Copyright (C) 2014 Peter Ross
  9. *
  10. * This file is part of Libav.
  11. *
  12. * Libav is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Libav is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Libav; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "rectangle.h"
  30. #include "thread.h"
  31. #include "vp8.h"
  32. #include "vp8data.h"
  33. #if ARCH_ARM
  34. # include "arm/vp8.h"
  35. #endif
  36. static void free_buffers(VP8Context *s)
  37. {
  38. int i;
  39. if (s->thread_data)
  40. for (i = 0; i < MAX_THREADS; i++) {
  41. #if HAVE_THREADS
  42. pthread_cond_destroy(&s->thread_data[i].cond);
  43. pthread_mutex_destroy(&s->thread_data[i].lock);
  44. #endif
  45. av_freep(&s->thread_data[i].filter_strength);
  46. }
  47. av_freep(&s->thread_data);
  48. av_freep(&s->macroblocks_base);
  49. av_freep(&s->intra4x4_pred_mode_top);
  50. av_freep(&s->top_nnz);
  51. av_freep(&s->top_border);
  52. s->macroblocks = NULL;
  53. }
  54. static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
  55. {
  56. int ret;
  57. if ((ret = ff_thread_get_buffer(s->avctx, &f->tf,
  58. ref ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
  59. return ret;
  60. if (!(f->seg_map = av_buffer_allocz(s->mb_width * s->mb_height))) {
  61. ff_thread_release_buffer(s->avctx, &f->tf);
  62. return AVERROR(ENOMEM);
  63. }
  64. return 0;
  65. }
  66. static void vp8_release_frame(VP8Context *s, VP8Frame *f)
  67. {
  68. av_buffer_unref(&f->seg_map);
  69. ff_thread_release_buffer(s->avctx, &f->tf);
  70. }
  71. #if CONFIG_VP8_DECODER
  72. static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, VP8Frame *src)
  73. {
  74. int ret;
  75. vp8_release_frame(s, dst);
  76. if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
  77. return ret;
  78. if (src->seg_map &&
  79. !(dst->seg_map = av_buffer_ref(src->seg_map))) {
  80. vp8_release_frame(s, dst);
  81. return AVERROR(ENOMEM);
  82. }
  83. return 0;
  84. }
  85. #endif /* CONFIG_VP8_DECODER */
  86. static void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
  87. {
  88. VP8Context *s = avctx->priv_data;
  89. int i;
  90. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
  91. vp8_release_frame(s, &s->frames[i]);
  92. memset(s->framep, 0, sizeof(s->framep));
  93. if (free_mem)
  94. free_buffers(s);
  95. }
  96. static void vp8_decode_flush(AVCodecContext *avctx)
  97. {
  98. vp8_decode_flush_impl(avctx, 0);
  99. }
  100. static VP8Frame *vp8_find_free_buffer(VP8Context *s)
  101. {
  102. VP8Frame *frame = NULL;
  103. int i;
  104. // find a free buffer
  105. for (i = 0; i < 5; i++)
  106. if (&s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
  107. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  108. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  109. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  110. frame = &s->frames[i];
  111. break;
  112. }
  113. if (i == 5) {
  114. av_log(s->avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
  115. abort();
  116. }
  117. if (frame->tf.f->data[0])
  118. vp8_release_frame(s, frame);
  119. return frame;
  120. }
  121. static av_always_inline
  122. int update_dimensions(VP8Context *s, int width, int height, int is_vp7)
  123. {
  124. AVCodecContext *avctx = s->avctx;
  125. int i, ret;
  126. if (width != s->avctx->width ||
  127. height != s->avctx->height) {
  128. vp8_decode_flush_impl(s->avctx, 1);
  129. ret = ff_set_dimensions(s->avctx, width, height);
  130. if (ret < 0)
  131. return ret;
  132. }
  133. s->mb_width = (s->avctx->coded_width + 15) / 16;
  134. s->mb_height = (s->avctx->coded_height + 15) / 16;
  135. s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&
  136. FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1;
  137. if (!s->mb_layout) { // Frame threading and one thread
  138. s->macroblocks_base = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *
  139. sizeof(*s->macroblocks));
  140. s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);
  141. } else // Sliced threading
  142. s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *
  143. sizeof(*s->macroblocks));
  144. s->top_nnz = av_mallocz(s->mb_width * sizeof(*s->top_nnz));
  145. s->top_border = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));
  146. s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));
  147. if (!s->macroblocks_base || !s->top_nnz || !s->top_border ||
  148. !s->thread_data || (!s->intra4x4_pred_mode_top && !s->mb_layout)) {
  149. free_buffers(s);
  150. return AVERROR(ENOMEM);
  151. }
  152. for (i = 0; i < MAX_THREADS; i++) {
  153. s->thread_data[i].filter_strength =
  154. av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));
  155. if (!s->thread_data[i].filter_strength) {
  156. free_buffers(s);
  157. return AVERROR(ENOMEM);
  158. }
  159. #if HAVE_THREADS
  160. pthread_mutex_init(&s->thread_data[i].lock, NULL);
  161. pthread_cond_init(&s->thread_data[i].cond, NULL);
  162. #endif
  163. }
  164. s->macroblocks = s->macroblocks_base + 1;
  165. return 0;
  166. }
  167. static int vp7_update_dimensions(VP8Context *s, int width, int height)
  168. {
  169. return update_dimensions(s, width, height, IS_VP7);
  170. }
  171. static int vp8_update_dimensions(VP8Context *s, int width, int height)
  172. {
  173. return update_dimensions(s, width, height, IS_VP8);
  174. }
  175. static void parse_segment_info(VP8Context *s)
  176. {
  177. VP56RangeCoder *c = &s->c;
  178. int i;
  179. s->segmentation.update_map = vp8_rac_get(c);
  180. if (vp8_rac_get(c)) { // update segment feature data
  181. s->segmentation.absolute_vals = vp8_rac_get(c);
  182. for (i = 0; i < 4; i++)
  183. s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
  184. for (i = 0; i < 4; i++)
  185. s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
  186. }
  187. if (s->segmentation.update_map)
  188. for (i = 0; i < 3; i++)
  189. s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  190. }
  191. static void update_lf_deltas(VP8Context *s)
  192. {
  193. VP56RangeCoder *c = &s->c;
  194. int i;
  195. for (i = 0; i < 4; i++) {
  196. if (vp8_rac_get(c)) {
  197. s->lf_delta.ref[i] = vp8_rac_get_uint(c, 6);
  198. if (vp8_rac_get(c))
  199. s->lf_delta.ref[i] = -s->lf_delta.ref[i];
  200. }
  201. }
  202. for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++) {
  203. if (vp8_rac_get(c)) {
  204. s->lf_delta.mode[i] = vp8_rac_get_uint(c, 6);
  205. if (vp8_rac_get(c))
  206. s->lf_delta.mode[i] = -s->lf_delta.mode[i];
  207. }
  208. }
  209. }
  210. static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
  211. {
  212. const uint8_t *sizes = buf;
  213. int i;
  214. s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
  215. buf += 3 * (s->num_coeff_partitions - 1);
  216. buf_size -= 3 * (s->num_coeff_partitions - 1);
  217. if (buf_size < 0)
  218. return -1;
  219. for (i = 0; i < s->num_coeff_partitions - 1; i++) {
  220. int size = AV_RL24(sizes + 3 * i);
  221. if (buf_size - size < 0)
  222. return -1;
  223. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
  224. buf += size;
  225. buf_size -= size;
  226. }
  227. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
  228. return 0;
  229. }
  230. static void vp7_get_quants(VP8Context *s)
  231. {
  232. VP56RangeCoder *c = &s->c;
  233. int yac_qi = vp8_rac_get_uint(c, 7);
  234. int ydc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  235. int y2dc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  236. int y2ac_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  237. int uvdc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  238. int uvac_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  239. s->qmat[0].luma_qmul[0] = vp7_ydc_qlookup[ydc_qi];
  240. s->qmat[0].luma_qmul[1] = vp7_yac_qlookup[yac_qi];
  241. s->qmat[0].luma_dc_qmul[0] = vp7_y2dc_qlookup[y2dc_qi];
  242. s->qmat[0].luma_dc_qmul[1] = vp7_y2ac_qlookup[y2ac_qi];
  243. s->qmat[0].chroma_qmul[0] = FFMIN(vp7_ydc_qlookup[uvdc_qi], 132);
  244. s->qmat[0].chroma_qmul[1] = vp7_yac_qlookup[uvac_qi];
  245. }
  246. static void get_quants(VP8Context *s)
  247. {
  248. VP56RangeCoder *c = &s->c;
  249. int i, base_qi;
  250. int yac_qi = vp8_rac_get_uint(c, 7);
  251. int ydc_delta = vp8_rac_get_sint(c, 4);
  252. int y2dc_delta = vp8_rac_get_sint(c, 4);
  253. int y2ac_delta = vp8_rac_get_sint(c, 4);
  254. int uvdc_delta = vp8_rac_get_sint(c, 4);
  255. int uvac_delta = vp8_rac_get_sint(c, 4);
  256. for (i = 0; i < 4; i++) {
  257. if (s->segmentation.enabled) {
  258. base_qi = s->segmentation.base_quant[i];
  259. if (!s->segmentation.absolute_vals)
  260. base_qi += yac_qi;
  261. } else
  262. base_qi = yac_qi;
  263. s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + ydc_delta, 7)];
  264. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi, 7)];
  265. s->qmat[i].luma_dc_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)] * 2;
  266. /* 101581>>16 is equivalent to 155/100 */
  267. s->qmat[i].luma_dc_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)] * 101581 >> 16;
  268. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
  269. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
  270. s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
  271. s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
  272. }
  273. }
  274. /**
  275. * Determine which buffers golden and altref should be updated with after this frame.
  276. * The spec isn't clear here, so I'm going by my understanding of what libvpx does
  277. *
  278. * Intra frames update all 3 references
  279. * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
  280. * If the update (golden|altref) flag is set, it's updated with the current frame
  281. * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
  282. * If the flag is not set, the number read means:
  283. * 0: no update
  284. * 1: VP56_FRAME_PREVIOUS
  285. * 2: update golden with altref, or update altref with golden
  286. */
  287. static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
  288. {
  289. VP56RangeCoder *c = &s->c;
  290. if (update)
  291. return VP56_FRAME_CURRENT;
  292. switch (vp8_rac_get_uint(c, 2)) {
  293. case 1:
  294. return VP56_FRAME_PREVIOUS;
  295. case 2:
  296. return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
  297. }
  298. return VP56_FRAME_NONE;
  299. }
  300. static void vp78_reset_probability_tables(VP8Context *s)
  301. {
  302. int i, j;
  303. for (i = 0; i < 4; i++)
  304. for (j = 0; j < 16; j++)
  305. memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
  306. sizeof(s->prob->token[i][j]));
  307. }
  308. static void vp78_update_probability_tables(VP8Context *s)
  309. {
  310. VP56RangeCoder *c = &s->c;
  311. int i, j, k, l, m;
  312. for (i = 0; i < 4; i++)
  313. for (j = 0; j < 8; j++)
  314. for (k = 0; k < 3; k++)
  315. for (l = 0; l < NUM_DCT_TOKENS-1; l++)
  316. if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
  317. int prob = vp8_rac_get_uint(c, 8);
  318. for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
  319. s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
  320. }
  321. }
  322. #define VP7_MVC_SIZE 17
  323. #define VP8_MVC_SIZE 19
  324. static void vp78_update_pred16x16_pred8x8_mvc_probabilities(VP8Context *s,
  325. int mvc_size)
  326. {
  327. VP56RangeCoder *c = &s->c;
  328. int i, j;
  329. if (vp8_rac_get(c))
  330. for (i = 0; i < 4; i++)
  331. s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
  332. if (vp8_rac_get(c))
  333. for (i = 0; i < 3; i++)
  334. s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
  335. // 17.2 MV probability update
  336. for (i = 0; i < 2; i++)
  337. for (j = 0; j < mvc_size; j++)
  338. if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
  339. s->prob->mvc[i][j] = vp8_rac_get_nn(c);
  340. }
  341. static void update_refs(VP8Context *s)
  342. {
  343. VP56RangeCoder *c = &s->c;
  344. int update_golden = vp8_rac_get(c);
  345. int update_altref = vp8_rac_get(c);
  346. s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
  347. s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
  348. }
  349. static void copy_luma(AVFrame *dst, AVFrame *src, int width, int height)
  350. {
  351. int i, j;
  352. for (j = 1; j < 3; j++) {
  353. for (i = 0; i < height / 2; i++)
  354. memcpy(dst->data[j] + i * dst->linesize[j],
  355. src->data[j] + i * src->linesize[j], width / 2);
  356. }
  357. }
  358. static void fade(uint8_t *dst, uint8_t *src,
  359. int width, int height, int linesize,
  360. int alpha, int beta)
  361. {
  362. int i, j;
  363. for (j = 0; j < height; j++) {
  364. for (i = 0; i < width; i++) {
  365. uint8_t y = src[j * linesize + i];
  366. dst[j * linesize + i] = av_clip_uint8(y + ((y * beta) >> 8) + alpha);
  367. }
  368. }
  369. }
  370. static int vp7_fade_frame(VP8Context *s, VP56RangeCoder *c)
  371. {
  372. int alpha = (int8_t) vp8_rac_get_uint(c, 8);
  373. int beta = (int8_t) vp8_rac_get_uint(c, 8);
  374. int ret;
  375. if (!s->keyframe && (alpha || beta)) {
  376. int width = s->mb_width * 16;
  377. int height = s->mb_height * 16;
  378. AVFrame *src, *dst;
  379. if (!s->framep[VP56_FRAME_PREVIOUS])
  380. return AVERROR_INVALIDDATA;
  381. dst =
  382. src = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
  383. /* preserve the golden frame, write a new previous frame */
  384. if (s->framep[VP56_FRAME_GOLDEN] == s->framep[VP56_FRAME_PREVIOUS]) {
  385. s->framep[VP56_FRAME_PREVIOUS] = vp8_find_free_buffer(s);
  386. if ((ret = vp8_alloc_frame(s, s->framep[VP56_FRAME_PREVIOUS], 1)) < 0)
  387. return ret;
  388. dst = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
  389. copy_luma(dst, src, width, height);
  390. }
  391. fade(dst->data[0], src->data[0],
  392. width, height, dst->linesize[0], alpha, beta);
  393. }
  394. return 0;
  395. }
  396. static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  397. {
  398. VP56RangeCoder *c = &s->c;
  399. int part1_size, hscale, vscale, i, j, ret;
  400. int width = s->avctx->width;
  401. int height = s->avctx->height;
  402. s->profile = (buf[0] >> 1) & 7;
  403. if (s->profile > 1) {
  404. avpriv_request_sample(s->avctx, "Unknown profile %d", s->profile);
  405. return AVERROR_INVALIDDATA;
  406. }
  407. s->keyframe = !(buf[0] & 1);
  408. s->invisible = 0;
  409. part1_size = AV_RL24(buf) >> 4;
  410. buf += 4 - s->profile;
  411. buf_size -= 4 - s->profile;
  412. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
  413. ff_vp56_init_range_decoder(c, buf, part1_size);
  414. buf += part1_size;
  415. buf_size -= part1_size;
  416. /* A. Dimension information (keyframes only) */
  417. if (s->keyframe) {
  418. width = vp8_rac_get_uint(c, 12);
  419. height = vp8_rac_get_uint(c, 12);
  420. hscale = vp8_rac_get_uint(c, 2);
  421. vscale = vp8_rac_get_uint(c, 2);
  422. if (hscale || vscale)
  423. avpriv_request_sample(s->avctx, "Upscaling");
  424. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  425. vp78_reset_probability_tables(s);
  426. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
  427. sizeof(s->prob->pred16x16));
  428. memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
  429. sizeof(s->prob->pred8x8c));
  430. for (i = 0; i < 2; i++)
  431. memcpy(s->prob->mvc[i], vp7_mv_default_prob[i],
  432. sizeof(vp7_mv_default_prob[i]));
  433. memset(&s->segmentation, 0, sizeof(s->segmentation));
  434. memset(&s->lf_delta, 0, sizeof(s->lf_delta));
  435. memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
  436. }
  437. if (s->keyframe || s->profile > 0)
  438. memset(s->inter_dc_pred, 0 , sizeof(s->inter_dc_pred));
  439. /* B. Decoding information for all four macroblock-level features */
  440. for (i = 0; i < 4; i++) {
  441. s->feature_enabled[i] = vp8_rac_get(c);
  442. if (s->feature_enabled[i]) {
  443. s->feature_present_prob[i] = vp8_rac_get_uint(c, 8);
  444. for (j = 0; j < 3; j++)
  445. s->feature_index_prob[i][j] =
  446. vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  447. if (vp7_feature_value_size[s->profile][i])
  448. for (j = 0; j < 4; j++)
  449. s->feature_value[i][j] =
  450. vp8_rac_get(c) ? vp8_rac_get_uint(c, vp7_feature_value_size[s->profile][i]) : 0;
  451. }
  452. }
  453. s->segmentation.enabled = 0;
  454. s->segmentation.update_map = 0;
  455. s->lf_delta.enabled = 0;
  456. s->num_coeff_partitions = 1;
  457. ff_vp56_init_range_decoder(&s->coeff_partition[0], buf, buf_size);
  458. if (!s->macroblocks_base || /* first frame */
  459. width != s->avctx->width || height != s->avctx->height ||
  460. (width + 15) / 16 != s->mb_width || (height + 15) / 16 != s->mb_height) {
  461. if ((ret = vp7_update_dimensions(s, width, height)) < 0)
  462. return ret;
  463. }
  464. /* C. Dequantization indices */
  465. vp7_get_quants(s);
  466. /* D. Golden frame update flag (a Flag) for interframes only */
  467. if (!s->keyframe) {
  468. s->update_golden = vp8_rac_get(c) ? VP56_FRAME_CURRENT : VP56_FRAME_NONE;
  469. s->sign_bias[VP56_FRAME_GOLDEN] = 0;
  470. }
  471. s->update_last = 1;
  472. s->update_probabilities = 1;
  473. s->fade_present = 1;
  474. if (s->profile > 0) {
  475. s->update_probabilities = vp8_rac_get(c);
  476. if (!s->update_probabilities)
  477. s->prob[1] = s->prob[0];
  478. if (!s->keyframe)
  479. s->fade_present = vp8_rac_get(c);
  480. }
  481. /* E. Fading information for previous frame */
  482. if (s->fade_present && vp8_rac_get(c)) {
  483. if ((ret = vp7_fade_frame(s ,c)) < 0)
  484. return ret;
  485. }
  486. /* F. Loop filter type */
  487. if (!s->profile)
  488. s->filter.simple = vp8_rac_get(c);
  489. /* G. DCT coefficient ordering specification */
  490. if (vp8_rac_get(c))
  491. for (i = 1; i < 16; i++)
  492. s->prob[0].scan[i] = zigzag_scan[vp8_rac_get_uint(c, 4)];
  493. /* H. Loop filter levels */
  494. if (s->profile > 0)
  495. s->filter.simple = vp8_rac_get(c);
  496. s->filter.level = vp8_rac_get_uint(c, 6);
  497. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  498. /* I. DCT coefficient probability update; 13.3 Token Probability Updates */
  499. vp78_update_probability_tables(s);
  500. s->mbskip_enabled = 0;
  501. /* J. The remaining frame header data occurs ONLY FOR INTERFRAMES */
  502. if (!s->keyframe) {
  503. s->prob->intra = vp8_rac_get_uint(c, 8);
  504. s->prob->last = vp8_rac_get_uint(c, 8);
  505. vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP7_MVC_SIZE);
  506. }
  507. return 0;
  508. }
  509. static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  510. {
  511. VP56RangeCoder *c = &s->c;
  512. int header_size, hscale, vscale, ret;
  513. int width = s->avctx->width;
  514. int height = s->avctx->height;
  515. s->keyframe = !(buf[0] & 1);
  516. s->profile = (buf[0]>>1) & 7;
  517. s->invisible = !(buf[0] & 0x10);
  518. header_size = AV_RL24(buf) >> 5;
  519. buf += 3;
  520. buf_size -= 3;
  521. if (s->profile > 3)
  522. av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
  523. if (!s->profile)
  524. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab,
  525. sizeof(s->put_pixels_tab));
  526. else // profile 1-3 use bilinear, 4+ aren't defined so whatever
  527. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab,
  528. sizeof(s->put_pixels_tab));
  529. if (header_size > buf_size - 7 * s->keyframe) {
  530. av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
  531. return AVERROR_INVALIDDATA;
  532. }
  533. if (s->keyframe) {
  534. if (AV_RL24(buf) != 0x2a019d) {
  535. av_log(s->avctx, AV_LOG_ERROR,
  536. "Invalid start code 0x%x\n", AV_RL24(buf));
  537. return AVERROR_INVALIDDATA;
  538. }
  539. width = AV_RL16(buf + 3) & 0x3fff;
  540. height = AV_RL16(buf + 5) & 0x3fff;
  541. hscale = buf[4] >> 6;
  542. vscale = buf[6] >> 6;
  543. buf += 7;
  544. buf_size -= 7;
  545. if (hscale || vscale)
  546. avpriv_request_sample(s->avctx, "Upscaling");
  547. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  548. vp78_reset_probability_tables(s);
  549. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
  550. sizeof(s->prob->pred16x16));
  551. memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
  552. sizeof(s->prob->pred8x8c));
  553. memcpy(s->prob->mvc, vp8_mv_default_prob,
  554. sizeof(s->prob->mvc));
  555. memset(&s->segmentation, 0, sizeof(s->segmentation));
  556. memset(&s->lf_delta, 0, sizeof(s->lf_delta));
  557. }
  558. ff_vp56_init_range_decoder(c, buf, header_size);
  559. buf += header_size;
  560. buf_size -= header_size;
  561. if (s->keyframe) {
  562. s->colorspace = vp8_rac_get(c);
  563. if (s->colorspace)
  564. av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
  565. s->fullrange = vp8_rac_get(c);
  566. }
  567. if ((s->segmentation.enabled = vp8_rac_get(c)))
  568. parse_segment_info(s);
  569. else
  570. s->segmentation.update_map = 0; // FIXME: move this to some init function?
  571. s->filter.simple = vp8_rac_get(c);
  572. s->filter.level = vp8_rac_get_uint(c, 6);
  573. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  574. if ((s->lf_delta.enabled = vp8_rac_get(c)))
  575. if (vp8_rac_get(c))
  576. update_lf_deltas(s);
  577. if (setup_partitions(s, buf, buf_size)) {
  578. av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
  579. return AVERROR_INVALIDDATA;
  580. }
  581. if (!s->macroblocks_base || /* first frame */
  582. width != s->avctx->width || height != s->avctx->height)
  583. if ((ret = vp8_update_dimensions(s, width, height)) < 0)
  584. return ret;
  585. get_quants(s);
  586. if (!s->keyframe) {
  587. update_refs(s);
  588. s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
  589. s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
  590. }
  591. // if we aren't saving this frame's probabilities for future frames,
  592. // make a copy of the current probabilities
  593. if (!(s->update_probabilities = vp8_rac_get(c)))
  594. s->prob[1] = s->prob[0];
  595. s->update_last = s->keyframe || vp8_rac_get(c);
  596. vp78_update_probability_tables(s);
  597. if ((s->mbskip_enabled = vp8_rac_get(c)))
  598. s->prob->mbskip = vp8_rac_get_uint(c, 8);
  599. if (!s->keyframe) {
  600. s->prob->intra = vp8_rac_get_uint(c, 8);
  601. s->prob->last = vp8_rac_get_uint(c, 8);
  602. s->prob->golden = vp8_rac_get_uint(c, 8);
  603. vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP8_MVC_SIZE);
  604. }
  605. return 0;
  606. }
  607. static av_always_inline
  608. void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
  609. {
  610. dst->x = av_clip(src->x, s->mv_min.x, s->mv_max.x);
  611. dst->y = av_clip(src->y, s->mv_min.y, s->mv_max.y);
  612. }
  613. /**
  614. * Motion vector coding, 17.1.
  615. */
  616. static int read_mv_component(VP56RangeCoder *c, const uint8_t *p, int vp7)
  617. {
  618. int bit, x = 0;
  619. if (vp56_rac_get_prob_branchy(c, p[0])) {
  620. int i;
  621. for (i = 0; i < 3; i++)
  622. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  623. for (i = (vp7 ? 7 : 9); i > 3; i--)
  624. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  625. if (!(x & (vp7 ? 0xF0 : 0xFFF0)) || vp56_rac_get_prob(c, p[12]))
  626. x += 8;
  627. } else {
  628. // small_mvtree
  629. const uint8_t *ps = p + 2;
  630. bit = vp56_rac_get_prob(c, *ps);
  631. ps += 1 + 3 * bit;
  632. x += 4 * bit;
  633. bit = vp56_rac_get_prob(c, *ps);
  634. ps += 1 + bit;
  635. x += 2 * bit;
  636. x += vp56_rac_get_prob(c, *ps);
  637. }
  638. return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
  639. }
  640. static av_always_inline
  641. const uint8_t *get_submv_prob(uint32_t left, uint32_t top, int is_vp7)
  642. {
  643. if (is_vp7)
  644. return vp7_submv_prob;
  645. if (left == top)
  646. return vp8_submv_prob[4 - !!left];
  647. if (!top)
  648. return vp8_submv_prob[2];
  649. return vp8_submv_prob[1 - !!left];
  650. }
  651. /**
  652. * Split motion vector prediction, 16.4.
  653. * @returns the number of motion vectors parsed (2, 4 or 16)
  654. */
  655. static av_always_inline
  656. int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  657. int layout, int is_vp7)
  658. {
  659. int part_idx;
  660. int n, num;
  661. VP8Macroblock *top_mb;
  662. VP8Macroblock *left_mb = &mb[-1];
  663. const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning];
  664. const uint8_t *mbsplits_top, *mbsplits_cur, *firstidx;
  665. VP56mv *top_mv;
  666. VP56mv *left_mv = left_mb->bmv;
  667. VP56mv *cur_mv = mb->bmv;
  668. if (!layout) // layout is inlined, s->mb_layout is not
  669. top_mb = &mb[2];
  670. else
  671. top_mb = &mb[-s->mb_width - 1];
  672. mbsplits_top = vp8_mbsplits[top_mb->partitioning];
  673. top_mv = top_mb->bmv;
  674. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
  675. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1]))
  676. part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
  677. else
  678. part_idx = VP8_SPLITMVMODE_8x8;
  679. } else {
  680. part_idx = VP8_SPLITMVMODE_4x4;
  681. }
  682. num = vp8_mbsplit_count[part_idx];
  683. mbsplits_cur = vp8_mbsplits[part_idx],
  684. firstidx = vp8_mbfirstidx[part_idx];
  685. mb->partitioning = part_idx;
  686. for (n = 0; n < num; n++) {
  687. int k = firstidx[n];
  688. uint32_t left, above;
  689. const uint8_t *submv_prob;
  690. if (!(k & 3))
  691. left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
  692. else
  693. left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
  694. if (k <= 3)
  695. above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
  696. else
  697. above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
  698. submv_prob = get_submv_prob(left, above, is_vp7);
  699. if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
  700. if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
  701. if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
  702. mb->bmv[n].y = mb->mv.y +
  703. read_mv_component(c, s->prob->mvc[0], is_vp7);
  704. mb->bmv[n].x = mb->mv.x +
  705. read_mv_component(c, s->prob->mvc[1], is_vp7);
  706. } else {
  707. AV_ZERO32(&mb->bmv[n]);
  708. }
  709. } else {
  710. AV_WN32A(&mb->bmv[n], above);
  711. }
  712. } else {
  713. AV_WN32A(&mb->bmv[n], left);
  714. }
  715. }
  716. return num;
  717. }
  718. /**
  719. * The vp7 reference decoder uses a padding macroblock column (added to right
  720. * edge of the frame) to guard against illegal macroblock offsets. The
  721. * algorithm has bugs that permit offsets to straddle the padding column.
  722. * This function replicates those bugs.
  723. *
  724. * @param[out] edge_x macroblock x address
  725. * @param[out] edge_y macroblock y address
  726. *
  727. * @return macroblock offset legal (boolean)
  728. */
  729. static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width,
  730. int xoffset, int yoffset, int boundary,
  731. int *edge_x, int *edge_y)
  732. {
  733. int vwidth = mb_width + 1;
  734. int new = (mb_y + yoffset) * vwidth + mb_x + xoffset;
  735. if (new < boundary || new % vwidth == vwidth - 1)
  736. return 0;
  737. *edge_y = new / vwidth;
  738. *edge_x = new % vwidth;
  739. return 1;
  740. }
  741. static const VP56mv *get_bmv_ptr(const VP8Macroblock *mb, int subblock)
  742. {
  743. return &mb->bmv[mb->mode == VP8_MVMODE_SPLIT ? vp8_mbsplits[mb->partitioning][subblock] : 0];
  744. }
  745. static av_always_inline
  746. void vp7_decode_mvs(VP8Context *s, VP8Macroblock *mb,
  747. int mb_x, int mb_y, int layout)
  748. {
  749. VP8Macroblock *mb_edge[12];
  750. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR };
  751. enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
  752. int idx = CNT_ZERO;
  753. VP56mv near_mv[3];
  754. uint8_t cnt[3] = { 0 };
  755. VP56RangeCoder *c = &s->c;
  756. int i;
  757. AV_ZERO32(&near_mv[0]);
  758. AV_ZERO32(&near_mv[1]);
  759. AV_ZERO32(&near_mv[2]);
  760. for (i = 0; i < VP7_MV_PRED_COUNT; i++) {
  761. const VP7MVPred * pred = &vp7_mv_pred[i];
  762. int edge_x, edge_y;
  763. if (vp7_calculate_mb_offset(mb_x, mb_y, s->mb_width, pred->xoffset,
  764. pred->yoffset, !s->profile, &edge_x, &edge_y)) {
  765. VP8Macroblock *edge = mb_edge[i] = (s->mb_layout == 1)
  766. ? s->macroblocks_base + 1 + edge_x +
  767. (s->mb_width + 1) * (edge_y + 1)
  768. : s->macroblocks + edge_x +
  769. (s->mb_height - edge_y - 1) * 2;
  770. uint32_t mv = AV_RN32A(get_bmv_ptr(edge, vp7_mv_pred[i].subblock));
  771. if (mv) {
  772. if (AV_RN32A(&near_mv[CNT_NEAREST])) {
  773. if (mv == AV_RN32A(&near_mv[CNT_NEAREST])) {
  774. idx = CNT_NEAREST;
  775. } else if (AV_RN32A(&near_mv[CNT_NEAR])) {
  776. if (mv != AV_RN32A(&near_mv[CNT_NEAR]))
  777. continue;
  778. idx = CNT_NEAR;
  779. } else {
  780. AV_WN32A(&near_mv[CNT_NEAR], mv);
  781. idx = CNT_NEAR;
  782. }
  783. } else {
  784. AV_WN32A(&near_mv[CNT_NEAREST], mv);
  785. idx = CNT_NEAREST;
  786. }
  787. } else {
  788. idx = CNT_ZERO;
  789. }
  790. } else {
  791. idx = CNT_ZERO;
  792. }
  793. cnt[idx] += vp7_mv_pred[i].score;
  794. }
  795. mb->partitioning = VP8_SPLITMVMODE_NONE;
  796. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_ZERO]][0])) {
  797. mb->mode = VP8_MVMODE_MV;
  798. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAREST]][1])) {
  799. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][2])) {
  800. if (cnt[CNT_NEAREST] > cnt[CNT_NEAR])
  801. AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAREST] ? 0 : AV_RN32A(&near_mv[CNT_NEAREST]));
  802. else
  803. AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAR] ? 0 : AV_RN32A(&near_mv[CNT_NEAR]));
  804. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][3])) {
  805. mb->mode = VP8_MVMODE_SPLIT;
  806. mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP7) - 1];
  807. } else {
  808. mb->mv.y += read_mv_component(c, s->prob->mvc[0], IS_VP7);
  809. mb->mv.x += read_mv_component(c, s->prob->mvc[1], IS_VP7);
  810. mb->bmv[0] = mb->mv;
  811. }
  812. } else {
  813. mb->mv = near_mv[CNT_NEAR];
  814. mb->bmv[0] = mb->mv;
  815. }
  816. } else {
  817. mb->mv = near_mv[CNT_NEAREST];
  818. mb->bmv[0] = mb->mv;
  819. }
  820. } else {
  821. mb->mode = VP8_MVMODE_ZERO;
  822. AV_ZERO32(&mb->mv);
  823. mb->bmv[0] = mb->mv;
  824. }
  825. }
  826. static av_always_inline
  827. void vp8_decode_mvs(VP8Context *s, VP8Macroblock *mb,
  828. int mb_x, int mb_y, int layout)
  829. {
  830. VP8Macroblock *mb_edge[3] = { 0 /* top */,
  831. mb - 1 /* left */,
  832. 0 /* top-left */ };
  833. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  834. enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
  835. int idx = CNT_ZERO;
  836. int cur_sign_bias = s->sign_bias[mb->ref_frame];
  837. int8_t *sign_bias = s->sign_bias;
  838. VP56mv near_mv[4];
  839. uint8_t cnt[4] = { 0 };
  840. VP56RangeCoder *c = &s->c;
  841. if (!layout) { // layout is inlined (s->mb_layout is not)
  842. mb_edge[0] = mb + 2;
  843. mb_edge[2] = mb + 1;
  844. } else {
  845. mb_edge[0] = mb - s->mb_width - 1;
  846. mb_edge[2] = mb - s->mb_width - 2;
  847. }
  848. AV_ZERO32(&near_mv[0]);
  849. AV_ZERO32(&near_mv[1]);
  850. AV_ZERO32(&near_mv[2]);
  851. /* Process MB on top, left and top-left */
  852. #define MV_EDGE_CHECK(n) \
  853. { \
  854. VP8Macroblock *edge = mb_edge[n]; \
  855. int edge_ref = edge->ref_frame; \
  856. if (edge_ref != VP56_FRAME_CURRENT) { \
  857. uint32_t mv = AV_RN32A(&edge->mv); \
  858. if (mv) { \
  859. if (cur_sign_bias != sign_bias[edge_ref]) { \
  860. /* SWAR negate of the values in mv. */ \
  861. mv = ~mv; \
  862. mv = ((mv & 0x7fff7fff) + \
  863. 0x00010001) ^ (mv & 0x80008000); \
  864. } \
  865. if (!n || mv != AV_RN32A(&near_mv[idx])) \
  866. AV_WN32A(&near_mv[++idx], mv); \
  867. cnt[idx] += 1 + (n != 2); \
  868. } else \
  869. cnt[CNT_ZERO] += 1 + (n != 2); \
  870. } \
  871. }
  872. MV_EDGE_CHECK(0)
  873. MV_EDGE_CHECK(1)
  874. MV_EDGE_CHECK(2)
  875. mb->partitioning = VP8_SPLITMVMODE_NONE;
  876. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
  877. mb->mode = VP8_MVMODE_MV;
  878. /* If we have three distinct MVs, merge first and last if they're the same */
  879. if (cnt[CNT_SPLITMV] &&
  880. AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
  881. cnt[CNT_NEAREST] += 1;
  882. /* Swap near and nearest if necessary */
  883. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  884. FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
  885. FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
  886. }
  887. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
  888. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
  889. /* Choose the best mv out of 0,0 and the nearest mv */
  890. clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
  891. cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
  892. (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
  893. (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
  894. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
  895. mb->mode = VP8_MVMODE_SPLIT;
  896. mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP8) - 1];
  897. } else {
  898. mb->mv.y += read_mv_component(c, s->prob->mvc[0], IS_VP8);
  899. mb->mv.x += read_mv_component(c, s->prob->mvc[1], IS_VP8);
  900. mb->bmv[0] = mb->mv;
  901. }
  902. } else {
  903. clamp_mv(s, &mb->mv, &near_mv[CNT_NEAR]);
  904. mb->bmv[0] = mb->mv;
  905. }
  906. } else {
  907. clamp_mv(s, &mb->mv, &near_mv[CNT_NEAREST]);
  908. mb->bmv[0] = mb->mv;
  909. }
  910. } else {
  911. mb->mode = VP8_MVMODE_ZERO;
  912. AV_ZERO32(&mb->mv);
  913. mb->bmv[0] = mb->mv;
  914. }
  915. }
  916. static av_always_inline
  917. void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  918. int mb_x, int keyframe, int layout)
  919. {
  920. uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
  921. if (layout == 1) {
  922. VP8Macroblock *mb_top = mb - s->mb_width - 1;
  923. memcpy(mb->intra4x4_pred_mode_top, mb_top->intra4x4_pred_mode_top, 4);
  924. }
  925. if (keyframe) {
  926. int x, y;
  927. uint8_t *top;
  928. uint8_t *const left = s->intra4x4_pred_mode_left;
  929. if (layout == 1)
  930. top = mb->intra4x4_pred_mode_top;
  931. else
  932. top = s->intra4x4_pred_mode_top + 4 * mb_x;
  933. for (y = 0; y < 4; y++) {
  934. for (x = 0; x < 4; x++) {
  935. const uint8_t *ctx;
  936. ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
  937. *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
  938. left[y] = top[x] = *intra4x4;
  939. intra4x4++;
  940. }
  941. }
  942. } else {
  943. int i;
  944. for (i = 0; i < 16; i++)
  945. intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree,
  946. vp8_pred4x4_prob_inter);
  947. }
  948. }
  949. static av_always_inline
  950. void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
  951. uint8_t *segment, uint8_t *ref, int layout, int is_vp7)
  952. {
  953. VP56RangeCoder *c = &s->c;
  954. const char *vp7_feature_name[] = { "q-index",
  955. "lf-delta",
  956. "partial-golden-update",
  957. "blit-pitch" };
  958. if (is_vp7) {
  959. int i;
  960. *segment = 0;
  961. for (i = 0; i < 4; i++) {
  962. if (s->feature_enabled[i]) {
  963. if (vp56_rac_get_prob(c, s->feature_present_prob[i])) {
  964. int index = vp8_rac_get_tree(c, vp7_feature_index_tree,
  965. s->feature_index_prob[i]);
  966. av_log(s->avctx, AV_LOG_WARNING,
  967. "Feature %s present in macroblock (value 0x%x)\n",
  968. vp7_feature_name[i], s->feature_value[i][index]);
  969. }
  970. }
  971. }
  972. } else if (s->segmentation.update_map)
  973. *segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
  974. else if (s->segmentation.enabled)
  975. *segment = ref ? *ref : *segment;
  976. mb->segment = *segment;
  977. mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
  978. if (s->keyframe) {
  979. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra,
  980. vp8_pred16x16_prob_intra);
  981. if (mb->mode == MODE_I4x4) {
  982. decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
  983. } else {
  984. const uint32_t modes = (is_vp7 ? vp7_pred4x4_mode
  985. : vp8_pred4x4_mode)[mb->mode] * 0x01010101u;
  986. if (s->mb_layout == 1)
  987. AV_WN32A(mb->intra4x4_pred_mode_top, modes);
  988. else
  989. AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
  990. AV_WN32A(s->intra4x4_pred_mode_left, modes);
  991. }
  992. mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
  993. vp8_pred8x8c_prob_intra);
  994. mb->ref_frame = VP56_FRAME_CURRENT;
  995. } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
  996. // inter MB, 16.2
  997. if (vp56_rac_get_prob_branchy(c, s->prob->last))
  998. mb->ref_frame =
  999. (!is_vp7 && vp56_rac_get_prob(c, s->prob->golden)) ? VP56_FRAME_GOLDEN2 /* altref */
  1000. : VP56_FRAME_GOLDEN;
  1001. else
  1002. mb->ref_frame = VP56_FRAME_PREVIOUS;
  1003. s->ref_count[mb->ref_frame - 1]++;
  1004. // motion vectors, 16.3
  1005. if (is_vp7)
  1006. vp7_decode_mvs(s, mb, mb_x, mb_y, layout);
  1007. else
  1008. vp8_decode_mvs(s, mb, mb_x, mb_y, layout);
  1009. } else {
  1010. // intra MB, 16.1
  1011. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
  1012. if (mb->mode == MODE_I4x4)
  1013. decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
  1014. mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
  1015. s->prob->pred8x8c);
  1016. mb->ref_frame = VP56_FRAME_CURRENT;
  1017. mb->partitioning = VP8_SPLITMVMODE_NONE;
  1018. AV_ZERO32(&mb->bmv[0]);
  1019. }
  1020. }
  1021. /**
  1022. * @param r arithmetic bitstream reader context
  1023. * @param block destination for block coefficients
  1024. * @param probs probabilities to use when reading trees from the bitstream
  1025. * @param i initial coeff index, 0 unless a separate DC block is coded
  1026. * @param qmul array holding the dc/ac dequant factor at position 0/1
  1027. *
  1028. * @return 0 if no coeffs were decoded
  1029. * otherwise, the index of the last coeff decoded plus one
  1030. */
  1031. static av_always_inline
  1032. int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
  1033. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1034. int i, uint8_t *token_prob, int16_t qmul[2],
  1035. const uint8_t scan[16], int vp7)
  1036. {
  1037. VP56RangeCoder c = *r;
  1038. goto skip_eob;
  1039. do {
  1040. int coeff;
  1041. restart:
  1042. if (!vp56_rac_get_prob_branchy(&c, token_prob[0])) // DCT_EOB
  1043. break;
  1044. skip_eob:
  1045. if (!vp56_rac_get_prob_branchy(&c, token_prob[1])) { // DCT_0
  1046. if (++i == 16)
  1047. break; // invalid input; blocks should end with EOB
  1048. token_prob = probs[i][0];
  1049. if (vp7)
  1050. goto restart;
  1051. goto skip_eob;
  1052. }
  1053. if (!vp56_rac_get_prob_branchy(&c, token_prob[2])) { // DCT_1
  1054. coeff = 1;
  1055. token_prob = probs[i + 1][1];
  1056. } else {
  1057. if (!vp56_rac_get_prob_branchy(&c, token_prob[3])) { // DCT 2,3,4
  1058. coeff = vp56_rac_get_prob_branchy(&c, token_prob[4]);
  1059. if (coeff)
  1060. coeff += vp56_rac_get_prob(&c, token_prob[5]);
  1061. coeff += 2;
  1062. } else {
  1063. // DCT_CAT*
  1064. if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {
  1065. if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
  1066. coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
  1067. } else { // DCT_CAT2
  1068. coeff = 7;
  1069. coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
  1070. coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
  1071. }
  1072. } else { // DCT_CAT3 and up
  1073. int a = vp56_rac_get_prob(&c, token_prob[8]);
  1074. int b = vp56_rac_get_prob(&c, token_prob[9 + a]);
  1075. int cat = (a << 1) + b;
  1076. coeff = 3 + (8 << cat);
  1077. coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
  1078. }
  1079. }
  1080. token_prob = probs[i + 1][2];
  1081. }
  1082. block[scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
  1083. } while (++i < 16);
  1084. *r = c;
  1085. return i;
  1086. }
  1087. static av_always_inline
  1088. int inter_predict_dc(int16_t block[16], int16_t pred[2])
  1089. {
  1090. int16_t dc = block[0];
  1091. int ret = 0;
  1092. if (pred[1] > 3) {
  1093. dc += pred[0];
  1094. ret = 1;
  1095. }
  1096. if (!pred[0] | !dc | ((int32_t)pred[0] ^ (int32_t)dc) >> 31) {
  1097. block[0] = pred[0] = dc;
  1098. pred[1] = 0;
  1099. } else {
  1100. if (pred[0] == dc)
  1101. pred[1]++;
  1102. block[0] = pred[0] = dc;
  1103. }
  1104. return ret;
  1105. }
  1106. static int vp7_decode_block_coeffs_internal(VP56RangeCoder *r,
  1107. int16_t block[16],
  1108. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1109. int i, uint8_t *token_prob,
  1110. int16_t qmul[2],
  1111. const uint8_t scan[16])
  1112. {
  1113. return decode_block_coeffs_internal(r, block, probs, i,
  1114. token_prob, qmul, scan, IS_VP7);
  1115. }
  1116. #ifndef vp8_decode_block_coeffs_internal
  1117. static int vp8_decode_block_coeffs_internal(VP56RangeCoder *r,
  1118. int16_t block[16],
  1119. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1120. int i, uint8_t *token_prob,
  1121. int16_t qmul[2])
  1122. {
  1123. return decode_block_coeffs_internal(r, block, probs, i,
  1124. token_prob, qmul, zigzag_scan, IS_VP8);
  1125. }
  1126. #endif
  1127. /**
  1128. * @param c arithmetic bitstream reader context
  1129. * @param block destination for block coefficients
  1130. * @param probs probabilities to use when reading trees from the bitstream
  1131. * @param i initial coeff index, 0 unless a separate DC block is coded
  1132. * @param zero_nhood the initial prediction context for number of surrounding
  1133. * all-zero blocks (only left/top, so 0-2)
  1134. * @param qmul array holding the dc/ac dequant factor at position 0/1
  1135. *
  1136. * @return 0 if no coeffs were decoded
  1137. * otherwise, the index of the last coeff decoded plus one
  1138. */
  1139. static av_always_inline
  1140. int decode_block_coeffs(VP56RangeCoder *c, int16_t block[16],
  1141. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1142. int i, int zero_nhood, int16_t qmul[2],
  1143. const uint8_t scan[16], int vp7)
  1144. {
  1145. uint8_t *token_prob = probs[i][zero_nhood];
  1146. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  1147. return 0;
  1148. return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,
  1149. token_prob, qmul, scan)
  1150. : vp8_decode_block_coeffs_internal(c, block, probs, i,
  1151. token_prob, qmul);
  1152. }
  1153. static av_always_inline
  1154. void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c,
  1155. VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9],
  1156. int is_vp7)
  1157. {
  1158. int i, x, y, luma_start = 0, luma_ctx = 3;
  1159. int nnz_pred, nnz, nnz_total = 0;
  1160. int segment = mb->segment;
  1161. int block_dc = 0;
  1162. if (mb->mode != MODE_I4x4 && (is_vp7 || mb->mode != VP8_MVMODE_SPLIT)) {
  1163. nnz_pred = t_nnz[8] + l_nnz[8];
  1164. // decode DC values and do hadamard
  1165. nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,
  1166. nnz_pred, s->qmat[segment].luma_dc_qmul,
  1167. zigzag_scan, is_vp7);
  1168. l_nnz[8] = t_nnz[8] = !!nnz;
  1169. if (is_vp7 && mb->mode > MODE_I4x4) {
  1170. nnz |= inter_predict_dc(td->block_dc,
  1171. s->inter_dc_pred[mb->ref_frame - 1]);
  1172. }
  1173. if (nnz) {
  1174. nnz_total += nnz;
  1175. block_dc = 1;
  1176. if (nnz == 1)
  1177. s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);
  1178. else
  1179. s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
  1180. }
  1181. luma_start = 1;
  1182. luma_ctx = 0;
  1183. }
  1184. // luma blocks
  1185. for (y = 0; y < 4; y++)
  1186. for (x = 0; x < 4; x++) {
  1187. nnz_pred = l_nnz[y] + t_nnz[x];
  1188. nnz = decode_block_coeffs(c, td->block[y][x],
  1189. s->prob->token[luma_ctx],
  1190. luma_start, nnz_pred,
  1191. s->qmat[segment].luma_qmul,
  1192. s->prob[0].scan, is_vp7);
  1193. /* nnz+block_dc may be one more than the actual last index,
  1194. * but we don't care */
  1195. td->non_zero_count_cache[y][x] = nnz + block_dc;
  1196. t_nnz[x] = l_nnz[y] = !!nnz;
  1197. nnz_total += nnz;
  1198. }
  1199. // chroma blocks
  1200. // TODO: what to do about dimensions? 2nd dim for luma is x,
  1201. // but for chroma it's (y<<1)|x
  1202. for (i = 4; i < 6; i++)
  1203. for (y = 0; y < 2; y++)
  1204. for (x = 0; x < 2; x++) {
  1205. nnz_pred = l_nnz[i + 2 * y] + t_nnz[i + 2 * x];
  1206. nnz = decode_block_coeffs(c, td->block[i][(y << 1) + x],
  1207. s->prob->token[2], 0, nnz_pred,
  1208. s->qmat[segment].chroma_qmul,
  1209. s->prob[0].scan, is_vp7);
  1210. td->non_zero_count_cache[i][(y << 1) + x] = nnz;
  1211. t_nnz[i + 2 * x] = l_nnz[i + 2 * y] = !!nnz;
  1212. nnz_total += nnz;
  1213. }
  1214. // if there were no coded coeffs despite the macroblock not being marked skip,
  1215. // we MUST not do the inner loop filter and should not do IDCT
  1216. // Since skip isn't used for bitstream prediction, just manually set it.
  1217. if (!nnz_total)
  1218. mb->skip = 1;
  1219. }
  1220. static av_always_inline
  1221. void backup_mb_border(uint8_t *top_border, uint8_t *src_y,
  1222. uint8_t *src_cb, uint8_t *src_cr,
  1223. int linesize, int uvlinesize, int simple)
  1224. {
  1225. AV_COPY128(top_border, src_y + 15 * linesize);
  1226. if (!simple) {
  1227. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  1228. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  1229. }
  1230. }
  1231. static av_always_inline
  1232. void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb,
  1233. uint8_t *src_cr, int linesize, int uvlinesize, int mb_x,
  1234. int mb_y, int mb_width, int simple, int xchg)
  1235. {
  1236. uint8_t *top_border_m1 = top_border - 32; // for TL prediction
  1237. src_y -= linesize;
  1238. src_cb -= uvlinesize;
  1239. src_cr -= uvlinesize;
  1240. #define XCHG(a, b, xchg) \
  1241. do { \
  1242. if (xchg) \
  1243. AV_SWAP64(b, a); \
  1244. else \
  1245. AV_COPY64(b, a); \
  1246. } while (0)
  1247. XCHG(top_border_m1 + 8, src_y - 8, xchg);
  1248. XCHG(top_border, src_y, xchg);
  1249. XCHG(top_border + 8, src_y + 8, 1);
  1250. if (mb_x < mb_width - 1)
  1251. XCHG(top_border + 32, src_y + 16, 1);
  1252. // only copy chroma for normal loop filter
  1253. // or to initialize the top row to 127
  1254. if (!simple || !mb_y) {
  1255. XCHG(top_border_m1 + 16, src_cb - 8, xchg);
  1256. XCHG(top_border_m1 + 24, src_cr - 8, xchg);
  1257. XCHG(top_border + 16, src_cb, 1);
  1258. XCHG(top_border + 24, src_cr, 1);
  1259. }
  1260. }
  1261. static av_always_inline
  1262. int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
  1263. {
  1264. if (!mb_x)
  1265. return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
  1266. else
  1267. return mb_y ? mode : LEFT_DC_PRED8x8;
  1268. }
  1269. static av_always_inline
  1270. int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
  1271. {
  1272. if (!mb_x)
  1273. return mb_y ? VERT_PRED8x8 : (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8);
  1274. else
  1275. return mb_y ? mode : HOR_PRED8x8;
  1276. }
  1277. static av_always_inline
  1278. int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
  1279. {
  1280. switch (mode) {
  1281. case DC_PRED8x8:
  1282. return check_dc_pred8x8_mode(mode, mb_x, mb_y);
  1283. case VERT_PRED8x8:
  1284. return !mb_y ? (vp7 ? DC_128_PRED8x8 : DC_127_PRED8x8) : mode;
  1285. case HOR_PRED8x8:
  1286. return !mb_x ? (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8) : mode;
  1287. case PLANE_PRED8x8: /* TM */
  1288. return check_tm_pred8x8_mode(mode, mb_x, mb_y, vp7);
  1289. }
  1290. return mode;
  1291. }
  1292. static av_always_inline
  1293. int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
  1294. {
  1295. if (!mb_x) {
  1296. return mb_y ? VERT_VP8_PRED : (vp7 ? DC_128_PRED : DC_129_PRED);
  1297. } else {
  1298. return mb_y ? mode : HOR_VP8_PRED;
  1299. }
  1300. }
  1301. static av_always_inline
  1302. int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y,
  1303. int *copy_buf, int vp7)
  1304. {
  1305. switch (mode) {
  1306. case VERT_PRED:
  1307. if (!mb_x && mb_y) {
  1308. *copy_buf = 1;
  1309. return mode;
  1310. }
  1311. /* fall-through */
  1312. case DIAG_DOWN_LEFT_PRED:
  1313. case VERT_LEFT_PRED:
  1314. return !mb_y ? (vp7 ? DC_128_PRED : DC_127_PRED) : mode;
  1315. case HOR_PRED:
  1316. if (!mb_y) {
  1317. *copy_buf = 1;
  1318. return mode;
  1319. }
  1320. /* fall-through */
  1321. case HOR_UP_PRED:
  1322. return !mb_x ? (vp7 ? DC_128_PRED : DC_129_PRED) : mode;
  1323. case TM_VP8_PRED:
  1324. return check_tm_pred4x4_mode(mode, mb_x, mb_y, vp7);
  1325. case DC_PRED: /* 4x4 DC doesn't use the same "H.264-style" exceptions
  1326. * as 16x16/8x8 DC */
  1327. case DIAG_DOWN_RIGHT_PRED:
  1328. case VERT_RIGHT_PRED:
  1329. case HOR_DOWN_PRED:
  1330. if (!mb_y || !mb_x)
  1331. *copy_buf = 1;
  1332. return mode;
  1333. }
  1334. return mode;
  1335. }
  1336. static av_always_inline
  1337. void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
  1338. VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
  1339. {
  1340. int x, y, mode, nnz;
  1341. uint32_t tr;
  1342. /* for the first row, we need to run xchg_mb_border to init the top edge
  1343. * to 127 otherwise, skip it if we aren't going to deblock */
  1344. if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
  1345. xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
  1346. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  1347. s->filter.simple, 1);
  1348. if (mb->mode < MODE_I4x4) {
  1349. mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y, is_vp7);
  1350. s->hpc.pred16x16[mode](dst[0], s->linesize);
  1351. } else {
  1352. uint8_t *ptr = dst[0];
  1353. uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
  1354. const uint8_t lo = is_vp7 ? 128 : 127;
  1355. const uint8_t hi = is_vp7 ? 128 : 129;
  1356. uint8_t tr_top[4] = { lo, lo, lo, lo };
  1357. // all blocks on the right edge of the macroblock use bottom edge
  1358. // the top macroblock for their topright edge
  1359. uint8_t *tr_right = ptr - s->linesize + 16;
  1360. // if we're on the right edge of the frame, said edge is extended
  1361. // from the top macroblock
  1362. if (mb_y && mb_x == s->mb_width - 1) {
  1363. tr = tr_right[-1] * 0x01010101u;
  1364. tr_right = (uint8_t *) &tr;
  1365. }
  1366. if (mb->skip)
  1367. AV_ZERO128(td->non_zero_count_cache);
  1368. for (y = 0; y < 4; y++) {
  1369. uint8_t *topright = ptr + 4 - s->linesize;
  1370. for (x = 0; x < 4; x++) {
  1371. int copy = 0, linesize = s->linesize;
  1372. uint8_t *dst = ptr + 4 * x;
  1373. DECLARE_ALIGNED(4, uint8_t, copy_dst)[5 * 8];
  1374. if ((y == 0 || x == 3) && mb_y == 0) {
  1375. topright = tr_top;
  1376. } else if (x == 3)
  1377. topright = tr_right;
  1378. mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x,
  1379. mb_y + y, &copy, is_vp7);
  1380. if (copy) {
  1381. dst = copy_dst + 12;
  1382. linesize = 8;
  1383. if (!(mb_y + y)) {
  1384. copy_dst[3] = lo;
  1385. AV_WN32A(copy_dst + 4, lo * 0x01010101U);
  1386. } else {
  1387. AV_COPY32(copy_dst + 4, ptr + 4 * x - s->linesize);
  1388. if (!(mb_x + x)) {
  1389. copy_dst[3] = hi;
  1390. } else {
  1391. copy_dst[3] = ptr[4 * x - s->linesize - 1];
  1392. }
  1393. }
  1394. if (!(mb_x + x)) {
  1395. copy_dst[11] =
  1396. copy_dst[19] =
  1397. copy_dst[27] =
  1398. copy_dst[35] = hi;
  1399. } else {
  1400. copy_dst[11] = ptr[4 * x - 1];
  1401. copy_dst[19] = ptr[4 * x + s->linesize - 1];
  1402. copy_dst[27] = ptr[4 * x + s->linesize * 2 - 1];
  1403. copy_dst[35] = ptr[4 * x + s->linesize * 3 - 1];
  1404. }
  1405. }
  1406. s->hpc.pred4x4[mode](dst, topright, linesize);
  1407. if (copy) {
  1408. AV_COPY32(ptr + 4 * x, copy_dst + 12);
  1409. AV_COPY32(ptr + 4 * x + s->linesize, copy_dst + 20);
  1410. AV_COPY32(ptr + 4 * x + s->linesize * 2, copy_dst + 28);
  1411. AV_COPY32(ptr + 4 * x + s->linesize * 3, copy_dst + 36);
  1412. }
  1413. nnz = td->non_zero_count_cache[y][x];
  1414. if (nnz) {
  1415. if (nnz == 1)
  1416. s->vp8dsp.vp8_idct_dc_add(ptr + 4 * x,
  1417. td->block[y][x], s->linesize);
  1418. else
  1419. s->vp8dsp.vp8_idct_add(ptr + 4 * x,
  1420. td->block[y][x], s->linesize);
  1421. }
  1422. topright += 4;
  1423. }
  1424. ptr += 4 * s->linesize;
  1425. intra4x4 += 4;
  1426. }
  1427. }
  1428. mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode,
  1429. mb_x, mb_y, is_vp7);
  1430. s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
  1431. s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
  1432. if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
  1433. xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
  1434. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  1435. s->filter.simple, 0);
  1436. }
  1437. static const uint8_t subpel_idx[3][8] = {
  1438. { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
  1439. // also function pointer index
  1440. { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
  1441. { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
  1442. };
  1443. /**
  1444. * luma MC function
  1445. *
  1446. * @param s VP8 decoding context
  1447. * @param dst target buffer for block data at block position
  1448. * @param ref reference picture buffer at origin (0, 0)
  1449. * @param mv motion vector (relative to block position) to get pixel data from
  1450. * @param x_off horizontal position of block from origin (0, 0)
  1451. * @param y_off vertical position of block from origin (0, 0)
  1452. * @param block_w width of block (16, 8 or 4)
  1453. * @param block_h height of block (always same as block_w)
  1454. * @param width width of src/dst plane data
  1455. * @param height height of src/dst plane data
  1456. * @param linesize size of a single line of plane data, including padding
  1457. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  1458. */
  1459. static av_always_inline
  1460. void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
  1461. ThreadFrame *ref, const VP56mv *mv,
  1462. int x_off, int y_off, int block_w, int block_h,
  1463. int width, int height, ptrdiff_t linesize,
  1464. vp8_mc_func mc_func[3][3])
  1465. {
  1466. uint8_t *src = ref->f->data[0];
  1467. if (AV_RN32A(mv)) {
  1468. int src_linesize = linesize;
  1469. int mx = (mv->x << 1) & 7, mx_idx = subpel_idx[0][mx];
  1470. int my = (mv->y << 1) & 7, my_idx = subpel_idx[0][my];
  1471. x_off += mv->x >> 2;
  1472. y_off += mv->y >> 2;
  1473. // edge emulation
  1474. ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
  1475. src += y_off * linesize + x_off;
  1476. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  1477. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  1478. s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
  1479. src - my_idx * linesize - mx_idx,
  1480. EDGE_EMU_LINESIZE, linesize,
  1481. block_w + subpel_idx[1][mx],
  1482. block_h + subpel_idx[1][my],
  1483. x_off - mx_idx, y_off - my_idx,
  1484. width, height);
  1485. src = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
  1486. src_linesize = EDGE_EMU_LINESIZE;
  1487. }
  1488. mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
  1489. } else {
  1490. ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
  1491. mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
  1492. linesize, block_h, 0, 0);
  1493. }
  1494. }
  1495. /**
  1496. * chroma MC function
  1497. *
  1498. * @param s VP8 decoding context
  1499. * @param dst1 target buffer for block data at block position (U plane)
  1500. * @param dst2 target buffer for block data at block position (V plane)
  1501. * @param ref reference picture buffer at origin (0, 0)
  1502. * @param mv motion vector (relative to block position) to get pixel data from
  1503. * @param x_off horizontal position of block from origin (0, 0)
  1504. * @param y_off vertical position of block from origin (0, 0)
  1505. * @param block_w width of block (16, 8 or 4)
  1506. * @param block_h height of block (always same as block_w)
  1507. * @param width width of src/dst plane data
  1508. * @param height height of src/dst plane data
  1509. * @param linesize size of a single line of plane data, including padding
  1510. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  1511. */
  1512. static av_always_inline
  1513. void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
  1514. uint8_t *dst2, ThreadFrame *ref, const VP56mv *mv,
  1515. int x_off, int y_off, int block_w, int block_h,
  1516. int width, int height, ptrdiff_t linesize,
  1517. vp8_mc_func mc_func[3][3])
  1518. {
  1519. uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
  1520. if (AV_RN32A(mv)) {
  1521. int mx = mv->x & 7, mx_idx = subpel_idx[0][mx];
  1522. int my = mv->y & 7, my_idx = subpel_idx[0][my];
  1523. x_off += mv->x >> 3;
  1524. y_off += mv->y >> 3;
  1525. // edge emulation
  1526. src1 += y_off * linesize + x_off;
  1527. src2 += y_off * linesize + x_off;
  1528. ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
  1529. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  1530. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  1531. s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
  1532. src1 - my_idx * linesize - mx_idx,
  1533. EDGE_EMU_LINESIZE, linesize,
  1534. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  1535. x_off - mx_idx, y_off - my_idx, width, height);
  1536. src1 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
  1537. mc_func[my_idx][mx_idx](dst1, linesize, src1, EDGE_EMU_LINESIZE, block_h, mx, my);
  1538. s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
  1539. src2 - my_idx * linesize - mx_idx,
  1540. EDGE_EMU_LINESIZE, linesize,
  1541. block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
  1542. x_off - mx_idx, y_off - my_idx, width, height);
  1543. src2 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
  1544. mc_func[my_idx][mx_idx](dst2, linesize, src2, EDGE_EMU_LINESIZE, block_h, mx, my);
  1545. } else {
  1546. mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
  1547. mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
  1548. }
  1549. } else {
  1550. ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
  1551. mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1552. mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1553. }
  1554. }
  1555. static av_always_inline
  1556. void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
  1557. ThreadFrame *ref_frame, int x_off, int y_off,
  1558. int bx_off, int by_off, int block_w, int block_h,
  1559. int width, int height, VP56mv *mv)
  1560. {
  1561. VP56mv uvmv = *mv;
  1562. /* Y */
  1563. vp8_mc_luma(s, td, dst[0] + by_off * s->linesize + bx_off,
  1564. ref_frame, mv, x_off + bx_off, y_off + by_off,
  1565. block_w, block_h, width, height, s->linesize,
  1566. s->put_pixels_tab[block_w == 8]);
  1567. /* U/V */
  1568. if (s->profile == 3) {
  1569. /* this block only applies VP8; it is safe to check
  1570. * only the profile, as VP7 profile <= 1 */
  1571. uvmv.x &= ~7;
  1572. uvmv.y &= ~7;
  1573. }
  1574. x_off >>= 1;
  1575. y_off >>= 1;
  1576. bx_off >>= 1;
  1577. by_off >>= 1;
  1578. width >>= 1;
  1579. height >>= 1;
  1580. block_w >>= 1;
  1581. block_h >>= 1;
  1582. vp8_mc_chroma(s, td, dst[1] + by_off * s->uvlinesize + bx_off,
  1583. dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
  1584. &uvmv, x_off + bx_off, y_off + by_off,
  1585. block_w, block_h, width, height, s->uvlinesize,
  1586. s->put_pixels_tab[1 + (block_w == 4)]);
  1587. }
  1588. /* Fetch pixels for estimated mv 4 macroblocks ahead.
  1589. * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
  1590. static av_always_inline
  1591. void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
  1592. int mb_xy, int ref)
  1593. {
  1594. /* Don't prefetch refs that haven't been used very often this frame. */
  1595. if (s->ref_count[ref - 1] > (mb_xy >> 5)) {
  1596. int x_off = mb_x << 4, y_off = mb_y << 4;
  1597. int mx = (mb->mv.x >> 2) + x_off + 8;
  1598. int my = (mb->mv.y >> 2) + y_off;
  1599. uint8_t **src = s->framep[ref]->tf.f->data;
  1600. int off = mx + (my + (mb_x & 3) * 4) * s->linesize + 64;
  1601. /* For threading, a ff_thread_await_progress here might be useful, but
  1602. * it actually slows down the decoder. Since a bad prefetch doesn't
  1603. * generate bad decoder output, we don't run it here. */
  1604. s->vdsp.prefetch(src[0] + off, s->linesize, 4);
  1605. off = (mx >> 1) + ((my >> 1) + (mb_x & 7)) * s->uvlinesize + 64;
  1606. s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  1607. }
  1608. }
  1609. /**
  1610. * Apply motion vectors to prediction buffer, chapter 18.
  1611. */
  1612. static av_always_inline
  1613. void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
  1614. VP8Macroblock *mb, int mb_x, int mb_y)
  1615. {
  1616. int x_off = mb_x << 4, y_off = mb_y << 4;
  1617. int width = 16 * s->mb_width, height = 16 * s->mb_height;
  1618. ThreadFrame *ref = &s->framep[mb->ref_frame]->tf;
  1619. VP56mv *bmv = mb->bmv;
  1620. switch (mb->partitioning) {
  1621. case VP8_SPLITMVMODE_NONE:
  1622. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1623. 0, 0, 16, 16, width, height, &mb->mv);
  1624. break;
  1625. case VP8_SPLITMVMODE_4x4: {
  1626. int x, y;
  1627. VP56mv uvmv;
  1628. /* Y */
  1629. for (y = 0; y < 4; y++) {
  1630. for (x = 0; x < 4; x++) {
  1631. vp8_mc_luma(s, td, dst[0] + 4 * y * s->linesize + x * 4,
  1632. ref, &bmv[4 * y + x],
  1633. 4 * x + x_off, 4 * y + y_off, 4, 4,
  1634. width, height, s->linesize,
  1635. s->put_pixels_tab[2]);
  1636. }
  1637. }
  1638. /* U/V */
  1639. x_off >>= 1;
  1640. y_off >>= 1;
  1641. width >>= 1;
  1642. height >>= 1;
  1643. for (y = 0; y < 2; y++) {
  1644. for (x = 0; x < 2; x++) {
  1645. uvmv.x = mb->bmv[2 * y * 4 + 2 * x ].x +
  1646. mb->bmv[2 * y * 4 + 2 * x + 1].x +
  1647. mb->bmv[(2 * y + 1) * 4 + 2 * x ].x +
  1648. mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].x;
  1649. uvmv.y = mb->bmv[2 * y * 4 + 2 * x ].y +
  1650. mb->bmv[2 * y * 4 + 2 * x + 1].y +
  1651. mb->bmv[(2 * y + 1) * 4 + 2 * x ].y +
  1652. mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].y;
  1653. uvmv.x = (uvmv.x + 2 + FF_SIGNBIT(uvmv.x)) >> 2;
  1654. uvmv.y = (uvmv.y + 2 + FF_SIGNBIT(uvmv.y)) >> 2;
  1655. if (s->profile == 3) {
  1656. uvmv.x &= ~7;
  1657. uvmv.y &= ~7;
  1658. }
  1659. vp8_mc_chroma(s, td, dst[1] + 4 * y * s->uvlinesize + x * 4,
  1660. dst[2] + 4 * y * s->uvlinesize + x * 4, ref,
  1661. &uvmv, 4 * x + x_off, 4 * y + y_off, 4, 4,
  1662. width, height, s->uvlinesize,
  1663. s->put_pixels_tab[2]);
  1664. }
  1665. }
  1666. break;
  1667. }
  1668. case VP8_SPLITMVMODE_16x8:
  1669. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1670. 0, 0, 16, 8, width, height, &bmv[0]);
  1671. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1672. 0, 8, 16, 8, width, height, &bmv[1]);
  1673. break;
  1674. case VP8_SPLITMVMODE_8x16:
  1675. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1676. 0, 0, 8, 16, width, height, &bmv[0]);
  1677. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1678. 8, 0, 8, 16, width, height, &bmv[1]);
  1679. break;
  1680. case VP8_SPLITMVMODE_8x8:
  1681. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1682. 0, 0, 8, 8, width, height, &bmv[0]);
  1683. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1684. 8, 0, 8, 8, width, height, &bmv[1]);
  1685. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1686. 0, 8, 8, 8, width, height, &bmv[2]);
  1687. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1688. 8, 8, 8, 8, width, height, &bmv[3]);
  1689. break;
  1690. }
  1691. }
  1692. static av_always_inline
  1693. void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], VP8Macroblock *mb)
  1694. {
  1695. int x, y, ch;
  1696. if (mb->mode != MODE_I4x4) {
  1697. uint8_t *y_dst = dst[0];
  1698. for (y = 0; y < 4; y++) {
  1699. uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[y]);
  1700. if (nnz4) {
  1701. if (nnz4 & ~0x01010101) {
  1702. for (x = 0; x < 4; x++) {
  1703. if ((uint8_t) nnz4 == 1)
  1704. s->vp8dsp.vp8_idct_dc_add(y_dst + 4 * x,
  1705. td->block[y][x],
  1706. s->linesize);
  1707. else if ((uint8_t) nnz4 > 1)
  1708. s->vp8dsp.vp8_idct_add(y_dst + 4 * x,
  1709. td->block[y][x],
  1710. s->linesize);
  1711. nnz4 >>= 8;
  1712. if (!nnz4)
  1713. break;
  1714. }
  1715. } else {
  1716. s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
  1717. }
  1718. }
  1719. y_dst += 4 * s->linesize;
  1720. }
  1721. }
  1722. for (ch = 0; ch < 2; ch++) {
  1723. uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4 + ch]);
  1724. if (nnz4) {
  1725. uint8_t *ch_dst = dst[1 + ch];
  1726. if (nnz4 & ~0x01010101) {
  1727. for (y = 0; y < 2; y++) {
  1728. for (x = 0; x < 2; x++) {
  1729. if ((uint8_t) nnz4 == 1)
  1730. s->vp8dsp.vp8_idct_dc_add(ch_dst + 4 * x,
  1731. td->block[4 + ch][(y << 1) + x],
  1732. s->uvlinesize);
  1733. else if ((uint8_t) nnz4 > 1)
  1734. s->vp8dsp.vp8_idct_add(ch_dst + 4 * x,
  1735. td->block[4 + ch][(y << 1) + x],
  1736. s->uvlinesize);
  1737. nnz4 >>= 8;
  1738. if (!nnz4)
  1739. goto chroma_idct_end;
  1740. }
  1741. ch_dst += 4 * s->uvlinesize;
  1742. }
  1743. } else {
  1744. s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4 + ch], s->uvlinesize);
  1745. }
  1746. }
  1747. chroma_idct_end:
  1748. ;
  1749. }
  1750. }
  1751. static av_always_inline
  1752. void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb,
  1753. VP8FilterStrength *f, int is_vp7)
  1754. {
  1755. int interior_limit, filter_level;
  1756. if (s->segmentation.enabled) {
  1757. filter_level = s->segmentation.filter_level[mb->segment];
  1758. if (!s->segmentation.absolute_vals)
  1759. filter_level += s->filter.level;
  1760. } else
  1761. filter_level = s->filter.level;
  1762. if (s->lf_delta.enabled) {
  1763. filter_level += s->lf_delta.ref[mb->ref_frame];
  1764. filter_level += s->lf_delta.mode[mb->mode];
  1765. }
  1766. filter_level = av_clip_uintp2(filter_level, 6);
  1767. interior_limit = filter_level;
  1768. if (s->filter.sharpness) {
  1769. interior_limit >>= (s->filter.sharpness + 3) >> 2;
  1770. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  1771. }
  1772. interior_limit = FFMAX(interior_limit, 1);
  1773. f->filter_level = filter_level;
  1774. f->inner_limit = interior_limit;
  1775. f->inner_filter = is_vp7 || !mb->skip || mb->mode == MODE_I4x4 ||
  1776. mb->mode == VP8_MVMODE_SPLIT;
  1777. }
  1778. static av_always_inline
  1779. void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f,
  1780. int mb_x, int mb_y, int is_vp7)
  1781. {
  1782. int mbedge_lim, bedge_lim_y, bedge_lim_uv, hev_thresh;
  1783. int filter_level = f->filter_level;
  1784. int inner_limit = f->inner_limit;
  1785. int inner_filter = f->inner_filter;
  1786. int linesize = s->linesize;
  1787. int uvlinesize = s->uvlinesize;
  1788. static const uint8_t hev_thresh_lut[2][64] = {
  1789. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1790. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1791. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  1792. 3, 3, 3, 3 },
  1793. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1794. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1795. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1796. 2, 2, 2, 2 }
  1797. };
  1798. if (!filter_level)
  1799. return;
  1800. if (is_vp7) {
  1801. bedge_lim_y = filter_level;
  1802. bedge_lim_uv = filter_level * 2;
  1803. mbedge_lim = filter_level + 2;
  1804. } else {
  1805. bedge_lim_y =
  1806. bedge_lim_uv = filter_level * 2 + inner_limit;
  1807. mbedge_lim = bedge_lim_y + 4;
  1808. }
  1809. hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
  1810. if (mb_x) {
  1811. s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
  1812. mbedge_lim, inner_limit, hev_thresh);
  1813. s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1814. mbedge_lim, inner_limit, hev_thresh);
  1815. }
  1816. #define H_LOOP_FILTER_16Y_INNER(cond) \
  1817. if (cond && inner_filter) { \
  1818. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 4, linesize, \
  1819. bedge_lim_y, inner_limit, \
  1820. hev_thresh); \
  1821. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 8, linesize, \
  1822. bedge_lim_y, inner_limit, \
  1823. hev_thresh); \
  1824. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 12, linesize, \
  1825. bedge_lim_y, inner_limit, \
  1826. hev_thresh); \
  1827. s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4, \
  1828. uvlinesize, bedge_lim_uv, \
  1829. inner_limit, hev_thresh); \
  1830. }
  1831. H_LOOP_FILTER_16Y_INNER(!is_vp7)
  1832. if (mb_y) {
  1833. s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
  1834. mbedge_lim, inner_limit, hev_thresh);
  1835. s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1836. mbedge_lim, inner_limit, hev_thresh);
  1837. }
  1838. if (inner_filter) {
  1839. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 4 * linesize,
  1840. linesize, bedge_lim_y,
  1841. inner_limit, hev_thresh);
  1842. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 8 * linesize,
  1843. linesize, bedge_lim_y,
  1844. inner_limit, hev_thresh);
  1845. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 12 * linesize,
  1846. linesize, bedge_lim_y,
  1847. inner_limit, hev_thresh);
  1848. s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
  1849. dst[2] + 4 * uvlinesize,
  1850. uvlinesize, bedge_lim_uv,
  1851. inner_limit, hev_thresh);
  1852. }
  1853. H_LOOP_FILTER_16Y_INNER(is_vp7)
  1854. }
  1855. static av_always_inline
  1856. void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f,
  1857. int mb_x, int mb_y)
  1858. {
  1859. int mbedge_lim, bedge_lim;
  1860. int filter_level = f->filter_level;
  1861. int inner_limit = f->inner_limit;
  1862. int inner_filter = f->inner_filter;
  1863. int linesize = s->linesize;
  1864. if (!filter_level)
  1865. return;
  1866. bedge_lim = 2 * filter_level + inner_limit;
  1867. mbedge_lim = bedge_lim + 4;
  1868. if (mb_x)
  1869. s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
  1870. if (inner_filter) {
  1871. s->vp8dsp.vp8_h_loop_filter_simple(dst + 4, linesize, bedge_lim);
  1872. s->vp8dsp.vp8_h_loop_filter_simple(dst + 8, linesize, bedge_lim);
  1873. s->vp8dsp.vp8_h_loop_filter_simple(dst + 12, linesize, bedge_lim);
  1874. }
  1875. if (mb_y)
  1876. s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
  1877. if (inner_filter) {
  1878. s->vp8dsp.vp8_v_loop_filter_simple(dst + 4 * linesize, linesize, bedge_lim);
  1879. s->vp8dsp.vp8_v_loop_filter_simple(dst + 8 * linesize, linesize, bedge_lim);
  1880. s->vp8dsp.vp8_v_loop_filter_simple(dst + 12 * linesize, linesize, bedge_lim);
  1881. }
  1882. }
  1883. #define MARGIN (16 << 2)
  1884. static av_always_inline
  1885. void vp78_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
  1886. VP8Frame *prev_frame, int is_vp7)
  1887. {
  1888. VP8Context *s = avctx->priv_data;
  1889. int mb_x, mb_y;
  1890. s->mv_min.y = -MARGIN;
  1891. s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
  1892. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1893. VP8Macroblock *mb = s->macroblocks_base +
  1894. ((s->mb_width + 1) * (mb_y + 1) + 1);
  1895. int mb_xy = mb_y * s->mb_width;
  1896. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
  1897. s->mv_min.x = -MARGIN;
  1898. s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
  1899. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  1900. if (mb_y == 0)
  1901. AV_WN32A((mb - s->mb_width - 1)->intra4x4_pred_mode_top,
  1902. DC_PRED * 0x01010101);
  1903. decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
  1904. prev_frame && prev_frame->seg_map ?
  1905. prev_frame->seg_map->data + mb_xy : NULL, 1, is_vp7);
  1906. s->mv_min.x -= 64;
  1907. s->mv_max.x -= 64;
  1908. }
  1909. s->mv_min.y -= 64;
  1910. s->mv_max.y -= 64;
  1911. }
  1912. }
  1913. static void vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
  1914. VP8Frame *prev_frame)
  1915. {
  1916. vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP7);
  1917. }
  1918. static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
  1919. VP8Frame *prev_frame)
  1920. {
  1921. vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP8);
  1922. }
  1923. #if HAVE_THREADS
  1924. #define check_thread_pos(td, otd, mb_x_check, mb_y_check) \
  1925. do { \
  1926. int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF); \
  1927. if (otd->thread_mb_pos < tmp) { \
  1928. pthread_mutex_lock(&otd->lock); \
  1929. td->wait_mb_pos = tmp; \
  1930. do { \
  1931. if (otd->thread_mb_pos >= tmp) \
  1932. break; \
  1933. pthread_cond_wait(&otd->cond, &otd->lock); \
  1934. } while (1); \
  1935. td->wait_mb_pos = INT_MAX; \
  1936. pthread_mutex_unlock(&otd->lock); \
  1937. } \
  1938. } while (0);
  1939. #define update_pos(td, mb_y, mb_x) \
  1940. do { \
  1941. int pos = (mb_y << 16) | (mb_x & 0xFFFF); \
  1942. int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \
  1943. (num_jobs > 1); \
  1944. int is_null = !next_td || !prev_td; \
  1945. int pos_check = (is_null) ? 1 \
  1946. : (next_td != td && \
  1947. pos >= next_td->wait_mb_pos) || \
  1948. (prev_td != td && \
  1949. pos >= prev_td->wait_mb_pos); \
  1950. td->thread_mb_pos = pos; \
  1951. if (sliced_threading && pos_check) { \
  1952. pthread_mutex_lock(&td->lock); \
  1953. pthread_cond_broadcast(&td->cond); \
  1954. pthread_mutex_unlock(&td->lock); \
  1955. } \
  1956. } while (0);
  1957. #else
  1958. #define check_thread_pos(td, otd, mb_x_check, mb_y_check)
  1959. #define update_pos(td, mb_y, mb_x)
  1960. #endif
  1961. static void vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
  1962. int jobnr, int threadnr, int is_vp7)
  1963. {
  1964. VP8Context *s = avctx->priv_data;
  1965. VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
  1966. int mb_y = td->thread_mb_pos >> 16;
  1967. int mb_x, mb_xy = mb_y * s->mb_width;
  1968. int num_jobs = s->num_jobs;
  1969. VP8Frame *curframe = s->curframe, *prev_frame = s->prev_frame;
  1970. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions - 1)];
  1971. VP8Macroblock *mb;
  1972. uint8_t *dst[3] = {
  1973. curframe->tf.f->data[0] + 16 * mb_y * s->linesize,
  1974. curframe->tf.f->data[1] + 8 * mb_y * s->uvlinesize,
  1975. curframe->tf.f->data[2] + 8 * mb_y * s->uvlinesize
  1976. };
  1977. if (mb_y == 0)
  1978. prev_td = td;
  1979. else
  1980. prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
  1981. if (mb_y == s->mb_height - 1)
  1982. next_td = td;
  1983. else
  1984. next_td = &s->thread_data[(jobnr + 1) % num_jobs];
  1985. if (s->mb_layout == 1)
  1986. mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
  1987. else {
  1988. // Make sure the previous frame has read its segmentation map,
  1989. // if we re-use the same map.
  1990. if (prev_frame && s->segmentation.enabled &&
  1991. !s->segmentation.update_map)
  1992. ff_thread_await_progress(&prev_frame->tf, mb_y, 0);
  1993. mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
  1994. memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
  1995. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
  1996. }
  1997. if (!is_vp7 || mb_y == 0)
  1998. memset(td->left_nnz, 0, sizeof(td->left_nnz));
  1999. s->mv_min.x = -MARGIN;
  2000. s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
  2001. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  2002. // Wait for previous thread to read mb_x+2, and reach mb_y-1.
  2003. if (prev_td != td) {
  2004. if (threadnr != 0) {
  2005. check_thread_pos(td, prev_td,
  2006. mb_x + (is_vp7 ? 2 : 1),
  2007. mb_y - (is_vp7 ? 2 : 1));
  2008. } else {
  2009. check_thread_pos(td, prev_td,
  2010. mb_x + (is_vp7 ? 2 : 1) + s->mb_width + 3,
  2011. mb_y - (is_vp7 ? 2 : 1));
  2012. }
  2013. }
  2014. s->vdsp.prefetch(dst[0] + (mb_x & 3) * 4 * s->linesize + 64,
  2015. s->linesize, 4);
  2016. s->vdsp.prefetch(dst[1] + (mb_x & 7) * s->uvlinesize + 64,
  2017. dst[2] - dst[1], 2);
  2018. if (!s->mb_layout)
  2019. decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
  2020. prev_frame && prev_frame->seg_map ?
  2021. prev_frame->seg_map->data + mb_xy : NULL, 0, is_vp7);
  2022. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
  2023. if (!mb->skip)
  2024. decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz, is_vp7);
  2025. if (mb->mode <= MODE_I4x4)
  2026. intra_predict(s, td, dst, mb, mb_x, mb_y, is_vp7);
  2027. else
  2028. inter_predict(s, td, dst, mb, mb_x, mb_y);
  2029. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
  2030. if (!mb->skip) {
  2031. idct_mb(s, td, dst, mb);
  2032. } else {
  2033. AV_ZERO64(td->left_nnz);
  2034. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  2035. /* Reset DC block predictors if they would exist
  2036. * if the mb had coefficients */
  2037. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  2038. td->left_nnz[8] = 0;
  2039. s->top_nnz[mb_x][8] = 0;
  2040. }
  2041. }
  2042. if (s->deblock_filter)
  2043. filter_level_for_mb(s, mb, &td->filter_strength[mb_x], is_vp7);
  2044. if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs - 1) {
  2045. if (s->filter.simple)
  2046. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2047. NULL, NULL, s->linesize, 0, 1);
  2048. else
  2049. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2050. dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  2051. }
  2052. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
  2053. dst[0] += 16;
  2054. dst[1] += 8;
  2055. dst[2] += 8;
  2056. s->mv_min.x -= 64;
  2057. s->mv_max.x -= 64;
  2058. if (mb_x == s->mb_width + 1) {
  2059. update_pos(td, mb_y, s->mb_width + 3);
  2060. } else {
  2061. update_pos(td, mb_y, mb_x);
  2062. }
  2063. }
  2064. }
  2065. static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
  2066. int jobnr, int threadnr, int is_vp7)
  2067. {
  2068. VP8Context *s = avctx->priv_data;
  2069. VP8ThreadData *td = &s->thread_data[threadnr];
  2070. int mb_x, mb_y = td->thread_mb_pos >> 16, num_jobs = s->num_jobs;
  2071. AVFrame *curframe = s->curframe->tf.f;
  2072. VP8Macroblock *mb;
  2073. VP8ThreadData *prev_td, *next_td;
  2074. uint8_t *dst[3] = {
  2075. curframe->data[0] + 16 * mb_y * s->linesize,
  2076. curframe->data[1] + 8 * mb_y * s->uvlinesize,
  2077. curframe->data[2] + 8 * mb_y * s->uvlinesize
  2078. };
  2079. if (s->mb_layout == 1)
  2080. mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
  2081. else
  2082. mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
  2083. if (mb_y == 0)
  2084. prev_td = td;
  2085. else
  2086. prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
  2087. if (mb_y == s->mb_height - 1)
  2088. next_td = td;
  2089. else
  2090. next_td = &s->thread_data[(jobnr + 1) % num_jobs];
  2091. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb++) {
  2092. VP8FilterStrength *f = &td->filter_strength[mb_x];
  2093. if (prev_td != td)
  2094. check_thread_pos(td, prev_td,
  2095. (mb_x + 1) + (s->mb_width + 3), mb_y - 1);
  2096. if (next_td != td)
  2097. if (next_td != &s->thread_data[0])
  2098. check_thread_pos(td, next_td, mb_x + 1, mb_y + 1);
  2099. if (num_jobs == 1) {
  2100. if (s->filter.simple)
  2101. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2102. NULL, NULL, s->linesize, 0, 1);
  2103. else
  2104. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2105. dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  2106. }
  2107. if (s->filter.simple)
  2108. filter_mb_simple(s, dst[0], f, mb_x, mb_y);
  2109. else
  2110. filter_mb(s, dst, f, mb_x, mb_y, is_vp7);
  2111. dst[0] += 16;
  2112. dst[1] += 8;
  2113. dst[2] += 8;
  2114. update_pos(td, mb_y, (s->mb_width + 3) + mb_x);
  2115. }
  2116. }
  2117. static av_always_inline
  2118. int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
  2119. int threadnr, int is_vp7)
  2120. {
  2121. VP8Context *s = avctx->priv_data;
  2122. VP8ThreadData *td = &s->thread_data[jobnr];
  2123. VP8ThreadData *next_td = NULL, *prev_td = NULL;
  2124. VP8Frame *curframe = s->curframe;
  2125. int mb_y, num_jobs = s->num_jobs;
  2126. td->thread_nr = threadnr;
  2127. for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
  2128. if (mb_y >= s->mb_height)
  2129. break;
  2130. td->thread_mb_pos = mb_y << 16;
  2131. vp8_decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, is_vp7);
  2132. if (s->deblock_filter)
  2133. vp8_filter_mb_row(avctx, tdata, jobnr, threadnr, is_vp7);
  2134. update_pos(td, mb_y, INT_MAX & 0xFFFF);
  2135. s->mv_min.y -= 64;
  2136. s->mv_max.y -= 64;
  2137. if (avctx->active_thread_type == FF_THREAD_FRAME)
  2138. ff_thread_report_progress(&curframe->tf, mb_y, 0);
  2139. }
  2140. return 0;
  2141. }
  2142. static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
  2143. int jobnr, int threadnr)
  2144. {
  2145. return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP7);
  2146. }
  2147. static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
  2148. int jobnr, int threadnr)
  2149. {
  2150. return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP8);
  2151. }
  2152. static av_always_inline
  2153. int vp78_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  2154. AVPacket *avpkt, int is_vp7)
  2155. {
  2156. VP8Context *s = avctx->priv_data;
  2157. int ret, i, referenced, num_jobs;
  2158. enum AVDiscard skip_thresh;
  2159. VP8Frame *av_uninit(curframe), *prev_frame;
  2160. if (is_vp7)
  2161. ret = vp7_decode_frame_header(s, avpkt->data, avpkt->size);
  2162. else
  2163. ret = vp8_decode_frame_header(s, avpkt->data, avpkt->size);
  2164. if (ret < 0)
  2165. goto err;
  2166. prev_frame = s->framep[VP56_FRAME_CURRENT];
  2167. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT ||
  2168. s->update_altref == VP56_FRAME_CURRENT;
  2169. skip_thresh = !referenced ? AVDISCARD_NONREF
  2170. : !s->keyframe ? AVDISCARD_NONKEY
  2171. : AVDISCARD_ALL;
  2172. if (avctx->skip_frame >= skip_thresh) {
  2173. s->invisible = 1;
  2174. memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
  2175. goto skip_decode;
  2176. }
  2177. s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
  2178. // release no longer referenced frames
  2179. for (i = 0; i < 5; i++)
  2180. if (s->frames[i].tf.f->data[0] &&
  2181. &s->frames[i] != prev_frame &&
  2182. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  2183. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  2184. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  2185. vp8_release_frame(s, &s->frames[i]);
  2186. curframe = s->framep[VP56_FRAME_CURRENT] = vp8_find_free_buffer(s);
  2187. if (!s->colorspace)
  2188. avctx->colorspace = AVCOL_SPC_BT470BG;
  2189. if (s->fullrange)
  2190. avctx->color_range = AVCOL_RANGE_JPEG;
  2191. else
  2192. avctx->color_range = AVCOL_RANGE_MPEG;
  2193. /* Given that arithmetic probabilities are updated every frame, it's quite
  2194. * likely that the values we have on a random interframe are complete
  2195. * junk if we didn't start decode on a keyframe. So just don't display
  2196. * anything rather than junk. */
  2197. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  2198. !s->framep[VP56_FRAME_GOLDEN] ||
  2199. !s->framep[VP56_FRAME_GOLDEN2])) {
  2200. av_log(avctx, AV_LOG_WARNING,
  2201. "Discarding interframe without a prior keyframe!\n");
  2202. ret = AVERROR_INVALIDDATA;
  2203. goto err;
  2204. }
  2205. curframe->tf.f->key_frame = s->keyframe;
  2206. curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
  2207. : AV_PICTURE_TYPE_P;
  2208. if ((ret = vp8_alloc_frame(s, curframe, referenced))) {
  2209. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
  2210. goto err;
  2211. }
  2212. // check if golden and altref are swapped
  2213. if (s->update_altref != VP56_FRAME_NONE)
  2214. s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  2215. else
  2216. s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
  2217. if (s->update_golden != VP56_FRAME_NONE)
  2218. s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  2219. else
  2220. s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
  2221. if (s->update_last)
  2222. s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
  2223. else
  2224. s->next_framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_PREVIOUS];
  2225. s->next_framep[VP56_FRAME_CURRENT] = curframe;
  2226. ff_thread_finish_setup(avctx);
  2227. s->linesize = curframe->tf.f->linesize[0];
  2228. s->uvlinesize = curframe->tf.f->linesize[1];
  2229. memset(s->top_nnz, 0, s->mb_width * sizeof(*s->top_nnz));
  2230. /* Zero macroblock structures for top/top-left prediction
  2231. * from outside the frame. */
  2232. if (!s->mb_layout)
  2233. memset(s->macroblocks + s->mb_height * 2 - 1, 0,
  2234. (s->mb_width + 1) * sizeof(*s->macroblocks));
  2235. if (!s->mb_layout && s->keyframe)
  2236. memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width * 4);
  2237. memset(s->ref_count, 0, sizeof(s->ref_count));
  2238. if (s->mb_layout == 1) {
  2239. // Make sure the previous frame has read its segmentation map,
  2240. // if we re-use the same map.
  2241. if (prev_frame && s->segmentation.enabled &&
  2242. !s->segmentation.update_map)
  2243. ff_thread_await_progress(&prev_frame->tf, 1, 0);
  2244. if (is_vp7)
  2245. vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
  2246. else
  2247. vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
  2248. }
  2249. if (avctx->active_thread_type == FF_THREAD_FRAME)
  2250. num_jobs = 1;
  2251. else
  2252. num_jobs = FFMIN(s->num_coeff_partitions, avctx->thread_count);
  2253. s->num_jobs = num_jobs;
  2254. s->curframe = curframe;
  2255. s->prev_frame = prev_frame;
  2256. s->mv_min.y = -MARGIN;
  2257. s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
  2258. for (i = 0; i < MAX_THREADS; i++) {
  2259. s->thread_data[i].thread_mb_pos = 0;
  2260. s->thread_data[i].wait_mb_pos = INT_MAX;
  2261. }
  2262. if (is_vp7)
  2263. avctx->execute2(avctx, vp7_decode_mb_row_sliced, s->thread_data, NULL,
  2264. num_jobs);
  2265. else
  2266. avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL,
  2267. num_jobs);
  2268. ff_thread_report_progress(&curframe->tf, INT_MAX, 0);
  2269. memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
  2270. skip_decode:
  2271. // if future frames don't use the updated probabilities,
  2272. // reset them to the values we saved
  2273. if (!s->update_probabilities)
  2274. s->prob[0] = s->prob[1];
  2275. if (!s->invisible) {
  2276. if ((ret = av_frame_ref(data, curframe->tf.f)) < 0)
  2277. return ret;
  2278. *got_frame = 1;
  2279. }
  2280. return avpkt->size;
  2281. err:
  2282. memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
  2283. return ret;
  2284. }
  2285. int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  2286. AVPacket *avpkt)
  2287. {
  2288. return vp78_decode_frame(avctx, data, got_frame, avpkt, IS_VP8);
  2289. }
  2290. #if CONFIG_VP7_DECODER
  2291. static int vp7_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  2292. AVPacket *avpkt)
  2293. {
  2294. return vp78_decode_frame(avctx, data, got_frame, avpkt, IS_VP7);
  2295. }
  2296. #endif /* CONFIG_VP7_DECODER */
  2297. av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
  2298. {
  2299. VP8Context *s = avctx->priv_data;
  2300. int i;
  2301. vp8_decode_flush_impl(avctx, 1);
  2302. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
  2303. av_frame_free(&s->frames[i].tf.f);
  2304. return 0;
  2305. }
  2306. static av_cold int vp8_init_frames(VP8Context *s)
  2307. {
  2308. int i;
  2309. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
  2310. s->frames[i].tf.f = av_frame_alloc();
  2311. if (!s->frames[i].tf.f)
  2312. return AVERROR(ENOMEM);
  2313. }
  2314. return 0;
  2315. }
  2316. static av_always_inline
  2317. int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
  2318. {
  2319. VP8Context *s = avctx->priv_data;
  2320. int ret;
  2321. s->avctx = avctx;
  2322. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  2323. avctx->internal->allocate_progress = 1;
  2324. ff_videodsp_init(&s->vdsp, 8);
  2325. ff_vp78dsp_init(&s->vp8dsp);
  2326. if (CONFIG_VP7_DECODER && is_vp7) {
  2327. ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP7, 8, 1);
  2328. ff_vp7dsp_init(&s->vp8dsp);
  2329. } else if (CONFIG_VP8_DECODER && !is_vp7) {
  2330. ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
  2331. ff_vp8dsp_init(&s->vp8dsp);
  2332. }
  2333. /* does not change for VP8 */
  2334. memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
  2335. if ((ret = vp8_init_frames(s)) < 0) {
  2336. ff_vp8_decode_free(avctx);
  2337. return ret;
  2338. }
  2339. return 0;
  2340. }
  2341. #if CONFIG_VP7_DECODER
  2342. static int vp7_decode_init(AVCodecContext *avctx)
  2343. {
  2344. return vp78_decode_init(avctx, IS_VP7);
  2345. }
  2346. #endif /* CONFIG_VP7_DECODER */
  2347. av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
  2348. {
  2349. return vp78_decode_init(avctx, IS_VP8);
  2350. }
  2351. #if CONFIG_VP8_DECODER
  2352. static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
  2353. {
  2354. VP8Context *s = avctx->priv_data;
  2355. int ret;
  2356. s->avctx = avctx;
  2357. if ((ret = vp8_init_frames(s)) < 0) {
  2358. ff_vp8_decode_free(avctx);
  2359. return ret;
  2360. }
  2361. return 0;
  2362. }
  2363. #define REBASE(pic) pic ? pic - &s_src->frames[0] + &s->frames[0] : NULL
  2364. static int vp8_decode_update_thread_context(AVCodecContext *dst,
  2365. const AVCodecContext *src)
  2366. {
  2367. VP8Context *s = dst->priv_data, *s_src = src->priv_data;
  2368. int i;
  2369. if (s->macroblocks_base &&
  2370. (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
  2371. free_buffers(s);
  2372. s->mb_width = s_src->mb_width;
  2373. s->mb_height = s_src->mb_height;
  2374. }
  2375. s->prob[0] = s_src->prob[!s_src->update_probabilities];
  2376. s->segmentation = s_src->segmentation;
  2377. s->lf_delta = s_src->lf_delta;
  2378. memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
  2379. for (i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++) {
  2380. if (s_src->frames[i].tf.f->data[0]) {
  2381. int ret = vp8_ref_frame(s, &s->frames[i], &s_src->frames[i]);
  2382. if (ret < 0)
  2383. return ret;
  2384. }
  2385. }
  2386. s->framep[0] = REBASE(s_src->next_framep[0]);
  2387. s->framep[1] = REBASE(s_src->next_framep[1]);
  2388. s->framep[2] = REBASE(s_src->next_framep[2]);
  2389. s->framep[3] = REBASE(s_src->next_framep[3]);
  2390. return 0;
  2391. }
  2392. #endif /* CONFIG_VP8_DECODER */
  2393. #if CONFIG_VP7_DECODER
  2394. AVCodec ff_vp7_decoder = {
  2395. .name = "vp7",
  2396. .long_name = NULL_IF_CONFIG_SMALL("On2 VP7"),
  2397. .type = AVMEDIA_TYPE_VIDEO,
  2398. .id = AV_CODEC_ID_VP7,
  2399. .priv_data_size = sizeof(VP8Context),
  2400. .init = vp7_decode_init,
  2401. .close = ff_vp8_decode_free,
  2402. .decode = vp7_decode_frame,
  2403. .capabilities = CODEC_CAP_DR1,
  2404. .flush = vp8_decode_flush,
  2405. };
  2406. #endif /* CONFIG_VP7_DECODER */
  2407. #if CONFIG_VP8_DECODER
  2408. AVCodec ff_vp8_decoder = {
  2409. .name = "vp8",
  2410. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  2411. .type = AVMEDIA_TYPE_VIDEO,
  2412. .id = AV_CODEC_ID_VP8,
  2413. .priv_data_size = sizeof(VP8Context),
  2414. .init = ff_vp8_decode_init,
  2415. .close = ff_vp8_decode_free,
  2416. .decode = ff_vp8_decode_frame,
  2417. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  2418. .flush = vp8_decode_flush,
  2419. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
  2420. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
  2421. };
  2422. #endif /* CONFIG_VP7_DECODER */