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.

2981 lines
107KB

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