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.

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