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.

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