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.

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