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.

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