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.

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