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.

2993 lines
107KB

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