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.

2881 lines
104KB

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