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.

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