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.

2837 lines
102KB

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