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.

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