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.

2852 lines
103KB

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