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.

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