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.

2859 lines
103KB

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