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.

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