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.

2790 lines
100KB

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