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.

1689 lines
59KB

  1. /**
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  6. * Copyright (C) 2010 Jason Garrett-Glaser
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavcore/imgutils.h"
  25. #include "avcodec.h"
  26. #include "vp56.h"
  27. #include "vp8data.h"
  28. #include "vp8dsp.h"
  29. #include "h264pred.h"
  30. #include "rectangle.h"
  31. typedef struct {
  32. uint8_t filter_level;
  33. uint8_t inner_limit;
  34. uint8_t inner_filter;
  35. } VP8FilterStrength;
  36. typedef struct {
  37. uint8_t skip;
  38. // todo: make it possible to check for at least (i4x4 or split_mv)
  39. // in one op. are others needed?
  40. uint8_t mode;
  41. uint8_t ref_frame;
  42. uint8_t partitioning;
  43. VP56mv mv;
  44. VP56mv bmv[16];
  45. } VP8Macroblock;
  46. typedef struct {
  47. AVCodecContext *avctx;
  48. DSPContext dsp;
  49. VP8DSPContext vp8dsp;
  50. H264PredContext hpc;
  51. vp8_mc_func put_pixels_tab[3][3][3];
  52. AVFrame frames[4];
  53. AVFrame *framep[4];
  54. uint8_t *edge_emu_buffer;
  55. VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
  56. int profile;
  57. int mb_width; /* number of horizontal MB */
  58. int mb_height; /* number of vertical MB */
  59. int linesize;
  60. int uvlinesize;
  61. int keyframe;
  62. int invisible;
  63. int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
  64. int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
  65. int update_altref;
  66. int deblock_filter;
  67. /**
  68. * If this flag is not set, all the probability updates
  69. * are discarded after this frame is decoded.
  70. */
  71. int update_probabilities;
  72. /**
  73. * All coefficients are contained in separate arith coding contexts.
  74. * There can be 1, 2, 4, or 8 of these after the header context.
  75. */
  76. int num_coeff_partitions;
  77. VP56RangeCoder coeff_partition[8];
  78. VP8Macroblock *macroblocks;
  79. VP8Macroblock *macroblocks_base;
  80. VP8FilterStrength *filter_strength;
  81. uint8_t *intra4x4_pred_mode_top;
  82. uint8_t intra4x4_pred_mode_left[4];
  83. uint8_t *segmentation_map;
  84. /**
  85. * Cache of the top row needed for intra prediction
  86. * 16 for luma, 8 for each chroma plane
  87. */
  88. uint8_t (*top_border)[16+8+8];
  89. /**
  90. * For coeff decode, we need to know whether the above block had non-zero
  91. * coefficients. This means for each macroblock, we need data for 4 luma
  92. * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
  93. * per macroblock. We keep the last row in top_nnz.
  94. */
  95. uint8_t (*top_nnz)[9];
  96. DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
  97. /**
  98. * This is the index plus one of the last non-zero coeff
  99. * for each of the blocks in the current macroblock.
  100. * So, 0 -> no coeffs
  101. * 1 -> dc-only (special transform)
  102. * 2+-> full transform
  103. */
  104. DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
  105. DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
  106. DECLARE_ALIGNED(16, DCTELEM, block_dc)[16];
  107. uint8_t intra4x4_pred_mode_mb[16];
  108. int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
  109. int segment; ///< segment of the current macroblock
  110. int mbskip_enabled;
  111. int sign_bias[4]; ///< one state [0, 1] per ref frame type
  112. int ref_count[3];
  113. /**
  114. * Base parameters for segmentation, i.e. per-macroblock parameters.
  115. * These must be kept unchanged even if segmentation is not used for
  116. * a frame, since the values persist between interframes.
  117. */
  118. struct {
  119. int enabled;
  120. int absolute_vals;
  121. int update_map;
  122. int8_t base_quant[4];
  123. int8_t filter_level[4]; ///< base loop filter level
  124. } segmentation;
  125. /**
  126. * Macroblocks can have one of 4 different quants in a frame when
  127. * segmentation is enabled.
  128. * If segmentation is disabled, only the first segment's values are used.
  129. */
  130. struct {
  131. // [0] - DC qmul [1] - AC qmul
  132. int16_t luma_qmul[2];
  133. int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
  134. int16_t chroma_qmul[2];
  135. } qmat[4];
  136. struct {
  137. int simple;
  138. int level;
  139. int sharpness;
  140. } filter;
  141. struct {
  142. int enabled; ///< whether each mb can have a different strength based on mode/ref
  143. /**
  144. * filter strength adjustment for the following macroblock modes:
  145. * [0] - i4x4
  146. * [1] - zero mv
  147. * [2] - inter modes except for zero or split mv
  148. * [3] - split mv
  149. * i16x16 modes never have any adjustment
  150. */
  151. int8_t mode[4];
  152. /**
  153. * filter strength adjustment for macroblocks that reference:
  154. * [0] - intra / VP56_FRAME_CURRENT
  155. * [1] - VP56_FRAME_PREVIOUS
  156. * [2] - VP56_FRAME_GOLDEN
  157. * [3] - altref / VP56_FRAME_GOLDEN2
  158. */
  159. int8_t ref[4];
  160. } lf_delta;
  161. /**
  162. * These are all of the updatable probabilities for binary decisions.
  163. * They are only implictly reset on keyframes, making it quite likely
  164. * for an interframe to desync if a prior frame's header was corrupt
  165. * or missing outright!
  166. */
  167. struct {
  168. uint8_t segmentid[3];
  169. uint8_t mbskip;
  170. uint8_t intra;
  171. uint8_t last;
  172. uint8_t golden;
  173. uint8_t pred16x16[4];
  174. uint8_t pred8x8c[3];
  175. /* Padded to allow overreads */
  176. uint8_t token[4][17][3][NUM_DCT_TOKENS-1];
  177. uint8_t mvc[2][19];
  178. } prob[2];
  179. } VP8Context;
  180. static void vp8_decode_flush(AVCodecContext *avctx)
  181. {
  182. VP8Context *s = avctx->priv_data;
  183. int i;
  184. for (i = 0; i < 4; i++)
  185. if (s->frames[i].data[0])
  186. avctx->release_buffer(avctx, &s->frames[i]);
  187. memset(s->framep, 0, sizeof(s->framep));
  188. av_freep(&s->macroblocks_base);
  189. av_freep(&s->filter_strength);
  190. av_freep(&s->intra4x4_pred_mode_top);
  191. av_freep(&s->top_nnz);
  192. av_freep(&s->edge_emu_buffer);
  193. av_freep(&s->top_border);
  194. av_freep(&s->segmentation_map);
  195. s->macroblocks = NULL;
  196. }
  197. static int update_dimensions(VP8Context *s, int width, int height)
  198. {
  199. if (av_check_image_size(width, height, 0, s->avctx))
  200. return AVERROR_INVALIDDATA;
  201. vp8_decode_flush(s->avctx);
  202. avcodec_set_dimensions(s->avctx, width, height);
  203. s->mb_width = (s->avctx->coded_width +15) / 16;
  204. s->mb_height = (s->avctx->coded_height+15) / 16;
  205. s->macroblocks_base = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
  206. s->filter_strength = av_mallocz(s->mb_width*sizeof(*s->filter_strength));
  207. s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
  208. s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
  209. s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
  210. s->segmentation_map = av_mallocz(s->mb_width*s->mb_height);
  211. if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_top ||
  212. !s->top_nnz || !s->top_border || !s->segmentation_map)
  213. return AVERROR(ENOMEM);
  214. s->macroblocks = s->macroblocks_base + 1;
  215. return 0;
  216. }
  217. static void parse_segment_info(VP8Context *s)
  218. {
  219. VP56RangeCoder *c = &s->c;
  220. int i;
  221. s->segmentation.update_map = vp8_rac_get(c);
  222. if (vp8_rac_get(c)) { // update segment feature data
  223. s->segmentation.absolute_vals = vp8_rac_get(c);
  224. for (i = 0; i < 4; i++)
  225. s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
  226. for (i = 0; i < 4; i++)
  227. s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
  228. }
  229. if (s->segmentation.update_map)
  230. for (i = 0; i < 3; i++)
  231. s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  232. }
  233. static void update_lf_deltas(VP8Context *s)
  234. {
  235. VP56RangeCoder *c = &s->c;
  236. int i;
  237. for (i = 0; i < 4; i++)
  238. s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
  239. for (i = 0; i < 4; i++)
  240. s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
  241. }
  242. static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
  243. {
  244. const uint8_t *sizes = buf;
  245. int i;
  246. s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
  247. buf += 3*(s->num_coeff_partitions-1);
  248. buf_size -= 3*(s->num_coeff_partitions-1);
  249. if (buf_size < 0)
  250. return -1;
  251. for (i = 0; i < s->num_coeff_partitions-1; i++) {
  252. int size = AV_RL24(sizes + 3*i);
  253. if (buf_size - size < 0)
  254. return -1;
  255. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
  256. buf += size;
  257. buf_size -= size;
  258. }
  259. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
  260. return 0;
  261. }
  262. static void get_quants(VP8Context *s)
  263. {
  264. VP56RangeCoder *c = &s->c;
  265. int i, base_qi;
  266. int yac_qi = vp8_rac_get_uint(c, 7);
  267. int ydc_delta = vp8_rac_get_sint(c, 4);
  268. int y2dc_delta = vp8_rac_get_sint(c, 4);
  269. int y2ac_delta = vp8_rac_get_sint(c, 4);
  270. int uvdc_delta = vp8_rac_get_sint(c, 4);
  271. int uvac_delta = vp8_rac_get_sint(c, 4);
  272. for (i = 0; i < 4; i++) {
  273. if (s->segmentation.enabled) {
  274. base_qi = s->segmentation.base_quant[i];
  275. if (!s->segmentation.absolute_vals)
  276. base_qi += yac_qi;
  277. } else
  278. base_qi = yac_qi;
  279. s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
  280. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi , 0, 127)];
  281. s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
  282. s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
  283. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
  284. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
  285. s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
  286. s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
  287. }
  288. }
  289. /**
  290. * Determine which buffers golden and altref should be updated with after this frame.
  291. * The spec isn't clear here, so I'm going by my understanding of what libvpx does
  292. *
  293. * Intra frames update all 3 references
  294. * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
  295. * If the update (golden|altref) flag is set, it's updated with the current frame
  296. * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
  297. * If the flag is not set, the number read means:
  298. * 0: no update
  299. * 1: VP56_FRAME_PREVIOUS
  300. * 2: update golden with altref, or update altref with golden
  301. */
  302. static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
  303. {
  304. VP56RangeCoder *c = &s->c;
  305. if (update)
  306. return VP56_FRAME_CURRENT;
  307. switch (vp8_rac_get_uint(c, 2)) {
  308. case 1:
  309. return VP56_FRAME_PREVIOUS;
  310. case 2:
  311. return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
  312. }
  313. return VP56_FRAME_NONE;
  314. }
  315. static void update_refs(VP8Context *s)
  316. {
  317. VP56RangeCoder *c = &s->c;
  318. int update_golden = vp8_rac_get(c);
  319. int update_altref = vp8_rac_get(c);
  320. s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
  321. s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
  322. }
  323. static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  324. {
  325. VP56RangeCoder *c = &s->c;
  326. int header_size, hscale, vscale, i, j, k, l, m, ret;
  327. int width = s->avctx->width;
  328. int height = s->avctx->height;
  329. s->keyframe = !(buf[0] & 1);
  330. s->profile = (buf[0]>>1) & 7;
  331. s->invisible = !(buf[0] & 0x10);
  332. header_size = AV_RL24(buf) >> 5;
  333. buf += 3;
  334. buf_size -= 3;
  335. if (s->profile > 3)
  336. av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
  337. if (!s->profile)
  338. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
  339. else // profile 1-3 use bilinear, 4+ aren't defined so whatever
  340. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
  341. if (header_size > buf_size - 7*s->keyframe) {
  342. av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
  343. return AVERROR_INVALIDDATA;
  344. }
  345. if (s->keyframe) {
  346. if (AV_RL24(buf) != 0x2a019d) {
  347. av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
  348. return AVERROR_INVALIDDATA;
  349. }
  350. width = AV_RL16(buf+3) & 0x3fff;
  351. height = AV_RL16(buf+5) & 0x3fff;
  352. hscale = buf[4] >> 6;
  353. vscale = buf[6] >> 6;
  354. buf += 7;
  355. buf_size -= 7;
  356. if (hscale || vscale)
  357. av_log_missing_feature(s->avctx, "Upscaling", 1);
  358. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  359. for (i = 0; i < 4; i++)
  360. for (j = 0; j < 16; j++)
  361. memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
  362. sizeof(s->prob->token[i][j]));
  363. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
  364. memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
  365. memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
  366. memset(&s->segmentation, 0, sizeof(s->segmentation));
  367. }
  368. if (!s->macroblocks_base || /* first frame */
  369. width != s->avctx->width || height != s->avctx->height) {
  370. if ((ret = update_dimensions(s, width, height) < 0))
  371. return ret;
  372. }
  373. ff_vp56_init_range_decoder(c, buf, header_size);
  374. buf += header_size;
  375. buf_size -= header_size;
  376. if (s->keyframe) {
  377. if (vp8_rac_get(c))
  378. av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
  379. vp8_rac_get(c); // whether we can skip clamping in dsp functions
  380. }
  381. if ((s->segmentation.enabled = vp8_rac_get(c)))
  382. parse_segment_info(s);
  383. else
  384. s->segmentation.update_map = 0; // FIXME: move this to some init function?
  385. s->filter.simple = vp8_rac_get(c);
  386. s->filter.level = vp8_rac_get_uint(c, 6);
  387. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  388. if ((s->lf_delta.enabled = vp8_rac_get(c)))
  389. if (vp8_rac_get(c))
  390. update_lf_deltas(s);
  391. if (setup_partitions(s, buf, buf_size)) {
  392. av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
  393. return AVERROR_INVALIDDATA;
  394. }
  395. get_quants(s);
  396. if (!s->keyframe) {
  397. update_refs(s);
  398. s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
  399. s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
  400. }
  401. // if we aren't saving this frame's probabilities for future frames,
  402. // make a copy of the current probabilities
  403. if (!(s->update_probabilities = vp8_rac_get(c)))
  404. s->prob[1] = s->prob[0];
  405. s->update_last = s->keyframe || vp8_rac_get(c);
  406. for (i = 0; i < 4; i++)
  407. for (j = 0; j < 8; j++)
  408. for (k = 0; k < 3; k++)
  409. for (l = 0; l < NUM_DCT_TOKENS-1; l++)
  410. if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
  411. int prob = vp8_rac_get_uint(c, 8);
  412. for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
  413. s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
  414. }
  415. if ((s->mbskip_enabled = vp8_rac_get(c)))
  416. s->prob->mbskip = vp8_rac_get_uint(c, 8);
  417. if (!s->keyframe) {
  418. s->prob->intra = vp8_rac_get_uint(c, 8);
  419. s->prob->last = vp8_rac_get_uint(c, 8);
  420. s->prob->golden = vp8_rac_get_uint(c, 8);
  421. if (vp8_rac_get(c))
  422. for (i = 0; i < 4; i++)
  423. s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
  424. if (vp8_rac_get(c))
  425. for (i = 0; i < 3; i++)
  426. s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
  427. // 17.2 MV probability update
  428. for (i = 0; i < 2; i++)
  429. for (j = 0; j < 19; j++)
  430. if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
  431. s->prob->mvc[i][j] = vp8_rac_get_nn(c);
  432. }
  433. return 0;
  434. }
  435. static av_always_inline
  436. void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src, int mb_x, int mb_y)
  437. {
  438. #define MARGIN (16 << 2)
  439. dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
  440. ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
  441. dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
  442. ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
  443. }
  444. static av_always_inline
  445. void find_near_mvs(VP8Context *s, VP8Macroblock *mb,
  446. VP56mv near[2], VP56mv *best, uint8_t cnt[4])
  447. {
  448. VP8Macroblock *mb_edge[3] = { mb + 2 /* top */,
  449. mb - 1 /* left */,
  450. mb + 1 /* top-left */ };
  451. enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
  452. VP56mv near_mv[4] = {{ 0 }};
  453. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  454. int idx = CNT_ZERO;
  455. int best_idx = CNT_ZERO;
  456. int cur_sign_bias = s->sign_bias[mb->ref_frame];
  457. int *sign_bias = s->sign_bias;
  458. /* Process MB on top, left and top-left */
  459. #define MV_EDGE_CHECK(n)\
  460. {\
  461. VP8Macroblock *edge = mb_edge[n];\
  462. int edge_ref = edge->ref_frame;\
  463. if (edge_ref != VP56_FRAME_CURRENT) {\
  464. uint32_t mv = AV_RN32A(&edge->mv);\
  465. if (mv) {\
  466. if (cur_sign_bias != sign_bias[edge_ref]) {\
  467. /* SWAR negate of the values in mv. */\
  468. mv = ~mv;\
  469. mv = ((mv&0x7fff7fff) + 0x00010001) ^ (mv&0x80008000);\
  470. }\
  471. if (!n || mv != AV_RN32A(&near_mv[idx]))\
  472. AV_WN32A(&near_mv[++idx], mv);\
  473. cnt[idx] += 1 + (n != 2);\
  474. } else\
  475. cnt[CNT_ZERO] += 1 + (n != 2);\
  476. }\
  477. }
  478. MV_EDGE_CHECK(0)
  479. MV_EDGE_CHECK(1)
  480. MV_EDGE_CHECK(2)
  481. /* If we have three distinct MVs, merge first and last if they're the same */
  482. if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT]))
  483. cnt[CNT_NEAREST] += 1;
  484. cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
  485. (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
  486. (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
  487. /* Swap near and nearest if necessary */
  488. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  489. FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
  490. FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
  491. }
  492. /* Choose the best mv out of 0,0 and the nearest mv */
  493. if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
  494. best_idx = CNT_NEAREST;
  495. mb->mv = near_mv[best_idx];
  496. near[0] = near_mv[CNT_NEAREST];
  497. near[1] = near_mv[CNT_NEAR];
  498. }
  499. /**
  500. * Motion vector coding, 17.1.
  501. */
  502. static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
  503. {
  504. int bit, x = 0;
  505. if (vp56_rac_get_prob_branchy(c, p[0])) {
  506. int i;
  507. for (i = 0; i < 3; i++)
  508. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  509. for (i = 9; i > 3; i--)
  510. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  511. if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
  512. x += 8;
  513. } else {
  514. // small_mvtree
  515. const uint8_t *ps = p+2;
  516. bit = vp56_rac_get_prob(c, *ps);
  517. ps += 1 + 3*bit;
  518. x += 4*bit;
  519. bit = vp56_rac_get_prob(c, *ps);
  520. ps += 1 + bit;
  521. x += 2*bit;
  522. x += vp56_rac_get_prob(c, *ps);
  523. }
  524. return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
  525. }
  526. static av_always_inline
  527. const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
  528. {
  529. if (left == top)
  530. return vp8_submv_prob[4-!!left];
  531. if (!top)
  532. return vp8_submv_prob[2];
  533. return vp8_submv_prob[1-!!left];
  534. }
  535. /**
  536. * Split motion vector prediction, 16.4.
  537. * @returns the number of motion vectors parsed (2, 4 or 16)
  538. */
  539. static av_always_inline
  540. int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb)
  541. {
  542. int part_idx;
  543. int n, num;
  544. VP8Macroblock *top_mb = &mb[2];
  545. VP8Macroblock *left_mb = &mb[-1];
  546. const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
  547. *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
  548. *mbsplits_cur, *firstidx;
  549. VP56mv *top_mv = top_mb->bmv;
  550. VP56mv *left_mv = left_mb->bmv;
  551. VP56mv *cur_mv = mb->bmv;
  552. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
  553. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1])) {
  554. part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
  555. } else {
  556. part_idx = VP8_SPLITMVMODE_8x8;
  557. }
  558. } else {
  559. part_idx = VP8_SPLITMVMODE_4x4;
  560. }
  561. num = vp8_mbsplit_count[part_idx];
  562. mbsplits_cur = vp8_mbsplits[part_idx],
  563. firstidx = vp8_mbfirstidx[part_idx];
  564. mb->partitioning = part_idx;
  565. for (n = 0; n < num; n++) {
  566. int k = firstidx[n];
  567. uint32_t left, above;
  568. const uint8_t *submv_prob;
  569. if (!(k & 3))
  570. left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
  571. else
  572. left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
  573. if (k <= 3)
  574. above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
  575. else
  576. above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
  577. submv_prob = get_submv_prob(left, above);
  578. if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
  579. if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
  580. if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
  581. mb->bmv[n].y = mb->mv.y + read_mv_component(c, s->prob->mvc[0]);
  582. mb->bmv[n].x = mb->mv.x + read_mv_component(c, s->prob->mvc[1]);
  583. } else {
  584. AV_ZERO32(&mb->bmv[n]);
  585. }
  586. } else {
  587. AV_WN32A(&mb->bmv[n], above);
  588. }
  589. } else {
  590. AV_WN32A(&mb->bmv[n], left);
  591. }
  592. }
  593. return num;
  594. }
  595. static av_always_inline
  596. void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c,
  597. int mb_x, int keyframe)
  598. {
  599. uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
  600. if (keyframe) {
  601. int x, y;
  602. uint8_t* const top = s->intra4x4_pred_mode_top + 4 * mb_x;
  603. uint8_t* const left = s->intra4x4_pred_mode_left;
  604. for (y = 0; y < 4; y++) {
  605. for (x = 0; x < 4; x++) {
  606. const uint8_t *ctx;
  607. ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
  608. *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
  609. left[y] = top[x] = *intra4x4;
  610. intra4x4++;
  611. }
  612. }
  613. } else {
  614. int i;
  615. for (i = 0; i < 16; i++)
  616. intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
  617. }
  618. }
  619. static av_always_inline
  620. void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, uint8_t *segment)
  621. {
  622. VP56RangeCoder *c = &s->c;
  623. if (s->segmentation.update_map)
  624. *segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
  625. s->segment = *segment;
  626. mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
  627. if (s->keyframe) {
  628. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
  629. if (mb->mode == MODE_I4x4) {
  630. decode_intra4x4_modes(s, c, mb_x, 1);
  631. } else {
  632. const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
  633. AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
  634. AV_WN32A(s->intra4x4_pred_mode_left, modes);
  635. }
  636. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
  637. mb->ref_frame = VP56_FRAME_CURRENT;
  638. } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
  639. VP56mv near[2], best;
  640. uint8_t cnt[4] = { 0 };
  641. // inter MB, 16.2
  642. if (vp56_rac_get_prob_branchy(c, s->prob->last))
  643. mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
  644. VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
  645. else
  646. mb->ref_frame = VP56_FRAME_PREVIOUS;
  647. s->ref_count[mb->ref_frame-1]++;
  648. // motion vectors, 16.3
  649. find_near_mvs(s, mb, near, &best, cnt);
  650. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[0]][0])) {
  651. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[1]][1])) {
  652. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[2]][2])) {
  653. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[3]][3])) {
  654. mb->mode = VP8_MVMODE_SPLIT;
  655. clamp_mv(s, &mb->mv, &mb->mv, mb_x, mb_y);
  656. mb->mv = mb->bmv[decode_splitmvs(s, c, mb) - 1];
  657. } else {
  658. mb->mode = VP8_MVMODE_NEW;
  659. clamp_mv(s, &mb->mv, &mb->mv, mb_x, mb_y);
  660. mb->mv.y += read_mv_component(c, s->prob->mvc[0]);
  661. mb->mv.x += read_mv_component(c, s->prob->mvc[1]);
  662. }
  663. } else {
  664. mb->mode = VP8_MVMODE_NEAR;
  665. clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
  666. }
  667. } else {
  668. mb->mode = VP8_MVMODE_NEAREST;
  669. clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
  670. }
  671. } else {
  672. mb->mode = VP8_MVMODE_ZERO;
  673. AV_ZERO32(&mb->mv);
  674. }
  675. if (mb->mode != VP8_MVMODE_SPLIT) {
  676. mb->partitioning = VP8_SPLITMVMODE_NONE;
  677. mb->bmv[0] = mb->mv;
  678. }
  679. } else {
  680. // intra MB, 16.1
  681. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
  682. if (mb->mode == MODE_I4x4)
  683. decode_intra4x4_modes(s, c, mb_x, 0);
  684. s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
  685. mb->ref_frame = VP56_FRAME_CURRENT;
  686. mb->partitioning = VP8_SPLITMVMODE_NONE;
  687. AV_ZERO32(&mb->bmv[0]);
  688. }
  689. }
  690. /**
  691. * @param c arithmetic bitstream reader context
  692. * @param block destination for block coefficients
  693. * @param probs probabilities to use when reading trees from the bitstream
  694. * @param i initial coeff index, 0 unless a separate DC block is coded
  695. * @param zero_nhood the initial prediction context for number of surrounding
  696. * all-zero blocks (only left/top, so 0-2)
  697. * @param qmul array holding the dc/ac dequant factor at position 0/1
  698. * @return 0 if no coeffs were decoded
  699. * otherwise, the index of the last coeff decoded plus one
  700. */
  701. static int decode_block_coeffs_internal(VP56RangeCoder *c, DCTELEM block[16],
  702. uint8_t probs[8][3][NUM_DCT_TOKENS-1],
  703. int i, uint8_t *token_prob, int16_t qmul[2])
  704. {
  705. goto skip_eob;
  706. do {
  707. int coeff;
  708. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  709. return i;
  710. skip_eob:
  711. if (!vp56_rac_get_prob_branchy(c, token_prob[1])) { // DCT_0
  712. if (++i == 16)
  713. return i; // invalid input; blocks should end with EOB
  714. token_prob = probs[i][0];
  715. goto skip_eob;
  716. }
  717. if (!vp56_rac_get_prob_branchy(c, token_prob[2])) { // DCT_1
  718. coeff = 1;
  719. token_prob = probs[i+1][1];
  720. } else {
  721. if (!vp56_rac_get_prob_branchy(c, token_prob[3])) { // DCT 2,3,4
  722. coeff = vp56_rac_get_prob_branchy(c, token_prob[4]);
  723. if (coeff)
  724. coeff += vp56_rac_get_prob(c, token_prob[5]);
  725. coeff += 2;
  726. } else {
  727. // DCT_CAT*
  728. if (!vp56_rac_get_prob_branchy(c, token_prob[6])) {
  729. if (!vp56_rac_get_prob_branchy(c, token_prob[7])) { // DCT_CAT1
  730. coeff = 5 + vp56_rac_get_prob(c, vp8_dct_cat1_prob[0]);
  731. } else { // DCT_CAT2
  732. coeff = 7;
  733. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[0]) << 1;
  734. coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[1]);
  735. }
  736. } else { // DCT_CAT3 and up
  737. int a = vp56_rac_get_prob(c, token_prob[8]);
  738. int b = vp56_rac_get_prob(c, token_prob[9+a]);
  739. int cat = (a<<1) + b;
  740. coeff = 3 + (8<<cat);
  741. coeff += vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
  742. }
  743. }
  744. token_prob = probs[i+1][2];
  745. }
  746. block[zigzag_scan[i]] = (vp8_rac_get(c) ? -coeff : coeff) * qmul[!!i];
  747. } while (++i < 16);
  748. return i;
  749. }
  750. static av_always_inline
  751. int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
  752. uint8_t probs[8][3][NUM_DCT_TOKENS-1],
  753. int i, int zero_nhood, int16_t qmul[2])
  754. {
  755. uint8_t *token_prob = probs[i][zero_nhood];
  756. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  757. return 0;
  758. return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
  759. }
  760. static av_always_inline
  761. void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  762. uint8_t t_nnz[9], uint8_t l_nnz[9])
  763. {
  764. int i, x, y, luma_start = 0, luma_ctx = 3;
  765. int nnz_pred, nnz, nnz_total = 0;
  766. int segment = s->segment;
  767. int block_dc = 0;
  768. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  769. nnz_pred = t_nnz[8] + l_nnz[8];
  770. // decode DC values and do hadamard
  771. nnz = decode_block_coeffs(c, s->block_dc, s->prob->token[1], 0, nnz_pred,
  772. s->qmat[segment].luma_dc_qmul);
  773. l_nnz[8] = t_nnz[8] = !!nnz;
  774. if (nnz) {
  775. nnz_total += nnz;
  776. block_dc = 1;
  777. if (nnz == 1)
  778. s->vp8dsp.vp8_luma_dc_wht_dc(s->block, s->block_dc);
  779. else
  780. s->vp8dsp.vp8_luma_dc_wht(s->block, s->block_dc);
  781. }
  782. luma_start = 1;
  783. luma_ctx = 0;
  784. }
  785. // luma blocks
  786. for (y = 0; y < 4; y++)
  787. for (x = 0; x < 4; x++) {
  788. nnz_pred = l_nnz[y] + t_nnz[x];
  789. nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
  790. nnz_pred, s->qmat[segment].luma_qmul);
  791. // nnz+block_dc may be one more than the actual last index, but we don't care
  792. s->non_zero_count_cache[y][x] = nnz + block_dc;
  793. t_nnz[x] = l_nnz[y] = !!nnz;
  794. nnz_total += nnz;
  795. }
  796. // chroma blocks
  797. // TODO: what to do about dimensions? 2nd dim for luma is x,
  798. // but for chroma it's (y<<1)|x
  799. for (i = 4; i < 6; i++)
  800. for (y = 0; y < 2; y++)
  801. for (x = 0; x < 2; x++) {
  802. nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
  803. nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
  804. nnz_pred, s->qmat[segment].chroma_qmul);
  805. s->non_zero_count_cache[i][(y<<1)+x] = nnz;
  806. t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
  807. nnz_total += nnz;
  808. }
  809. // if there were no coded coeffs despite the macroblock not being marked skip,
  810. // we MUST not do the inner loop filter and should not do IDCT
  811. // Since skip isn't used for bitstream prediction, just manually set it.
  812. if (!nnz_total)
  813. mb->skip = 1;
  814. }
  815. static av_always_inline
  816. void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
  817. int linesize, int uvlinesize, int simple)
  818. {
  819. AV_COPY128(top_border, src_y + 15*linesize);
  820. if (!simple) {
  821. AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
  822. AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
  823. }
  824. }
  825. static av_always_inline
  826. void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
  827. int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
  828. int simple, int xchg)
  829. {
  830. uint8_t *top_border_m1 = top_border-32; // for TL prediction
  831. src_y -= linesize;
  832. src_cb -= uvlinesize;
  833. src_cr -= uvlinesize;
  834. #define XCHG(a,b,xchg) do { \
  835. if (xchg) AV_SWAP64(b,a); \
  836. else AV_COPY64(b,a); \
  837. } while (0)
  838. XCHG(top_border_m1+8, src_y-8, xchg);
  839. XCHG(top_border, src_y, xchg);
  840. XCHG(top_border+8, src_y+8, 1);
  841. if (mb_x < mb_width-1)
  842. XCHG(top_border+32, src_y+16, 1);
  843. // only copy chroma for normal loop filter
  844. // or to initialize the top row to 127
  845. if (!simple || !mb_y) {
  846. XCHG(top_border_m1+16, src_cb-8, xchg);
  847. XCHG(top_border_m1+24, src_cr-8, xchg);
  848. XCHG(top_border+16, src_cb, 1);
  849. XCHG(top_border+24, src_cr, 1);
  850. }
  851. }
  852. static av_always_inline
  853. int check_intra_pred_mode(int mode, int mb_x, int mb_y)
  854. {
  855. if (mode == DC_PRED8x8) {
  856. if (!mb_x) {
  857. mode = mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
  858. } else if (!mb_y) {
  859. mode = LEFT_DC_PRED8x8;
  860. }
  861. }
  862. return mode;
  863. }
  864. static av_always_inline
  865. void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  866. int mb_x, int mb_y)
  867. {
  868. int x, y, mode, nnz, tr;
  869. // for the first row, we need to run xchg_mb_border to init the top edge to 127
  870. // otherwise, skip it if we aren't going to deblock
  871. if (s->deblock_filter || !mb_y)
  872. xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
  873. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  874. s->filter.simple, 1);
  875. if (mb->mode < MODE_I4x4) {
  876. mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
  877. s->hpc.pred16x16[mode](dst[0], s->linesize);
  878. } else {
  879. uint8_t *ptr = dst[0];
  880. uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
  881. // all blocks on the right edge of the macroblock use bottom edge
  882. // the top macroblock for their topright edge
  883. uint8_t *tr_right = ptr - s->linesize + 16;
  884. // if we're on the right edge of the frame, said edge is extended
  885. // from the top macroblock
  886. if (mb_x == s->mb_width-1) {
  887. tr = tr_right[-1]*0x01010101;
  888. tr_right = (uint8_t *)&tr;
  889. }
  890. if (mb->skip)
  891. AV_ZERO128(s->non_zero_count_cache);
  892. for (y = 0; y < 4; y++) {
  893. uint8_t *topright = ptr + 4 - s->linesize;
  894. for (x = 0; x < 4; x++) {
  895. if (x == 3)
  896. topright = tr_right;
  897. s->hpc.pred4x4[intra4x4[x]](ptr+4*x, topright, s->linesize);
  898. nnz = s->non_zero_count_cache[y][x];
  899. if (nnz) {
  900. if (nnz == 1)
  901. s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
  902. else
  903. s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
  904. }
  905. topright += 4;
  906. }
  907. ptr += 4*s->linesize;
  908. intra4x4 += 4;
  909. }
  910. }
  911. mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
  912. s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
  913. s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
  914. if (s->deblock_filter || !mb_y)
  915. xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
  916. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  917. s->filter.simple, 0);
  918. }
  919. /**
  920. * Generic MC function.
  921. *
  922. * @param s VP8 decoding context
  923. * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
  924. * @param dst target buffer for block data at block position
  925. * @param src reference picture buffer at origin (0, 0)
  926. * @param mv motion vector (relative to block position) to get pixel data from
  927. * @param x_off horizontal position of block from origin (0, 0)
  928. * @param y_off vertical position of block from origin (0, 0)
  929. * @param block_w width of block (16, 8 or 4)
  930. * @param block_h height of block (always same as block_w)
  931. * @param width width of src/dst plane data
  932. * @param height height of src/dst plane data
  933. * @param linesize size of a single line of plane data, including padding
  934. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  935. */
  936. static av_always_inline
  937. void vp8_mc(VP8Context *s, int luma,
  938. uint8_t *dst, uint8_t *src, const VP56mv *mv,
  939. int x_off, int y_off, int block_w, int block_h,
  940. int width, int height, int linesize,
  941. vp8_mc_func mc_func[3][3])
  942. {
  943. if (AV_RN32A(mv)) {
  944. static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
  945. int mx = (mv->x << luma)&7, mx_idx = idx[mx];
  946. int my = (mv->y << luma)&7, my_idx = idx[my];
  947. x_off += mv->x >> (3 - luma);
  948. y_off += mv->y >> (3 - luma);
  949. // edge emulation
  950. src += y_off * linesize + x_off;
  951. if (x_off < 2 || x_off >= width - block_w - 3 ||
  952. y_off < 2 || y_off >= height - block_h - 3) {
  953. ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
  954. block_w + 5, block_h + 5,
  955. x_off - 2, y_off - 2, width, height);
  956. src = s->edge_emu_buffer + 2 + linesize * 2;
  957. }
  958. mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
  959. } else
  960. mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
  961. }
  962. static av_always_inline
  963. void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
  964. AVFrame *ref_frame, int x_off, int y_off,
  965. int bx_off, int by_off,
  966. int block_w, int block_h,
  967. int width, int height, VP56mv *mv)
  968. {
  969. VP56mv uvmv = *mv;
  970. /* Y */
  971. vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,
  972. ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
  973. block_w, block_h, width, height, s->linesize,
  974. s->put_pixels_tab[block_w == 8]);
  975. /* U/V */
  976. if (s->profile == 3) {
  977. uvmv.x &= ~7;
  978. uvmv.y &= ~7;
  979. }
  980. x_off >>= 1; y_off >>= 1;
  981. bx_off >>= 1; by_off >>= 1;
  982. width >>= 1; height >>= 1;
  983. block_w >>= 1; block_h >>= 1;
  984. vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,
  985. ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,
  986. block_w, block_h, width, height, s->uvlinesize,
  987. s->put_pixels_tab[1 + (block_w == 4)]);
  988. vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,
  989. ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
  990. block_w, block_h, width, height, s->uvlinesize,
  991. s->put_pixels_tab[1 + (block_w == 4)]);
  992. }
  993. /* Fetch pixels for estimated mv 4 macroblocks ahead.
  994. * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
  995. static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
  996. {
  997. /* Don't prefetch refs that haven't been used very often this frame. */
  998. if (s->ref_count[ref-1] > (mb_xy >> 5)) {
  999. int x_off = mb_x << 4, y_off = mb_y << 4;
  1000. int mx = (mb->mv.x>>2) + x_off + 8;
  1001. int my = (mb->mv.y>>2) + y_off;
  1002. uint8_t **src= s->framep[ref]->data;
  1003. int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
  1004. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1005. off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
  1006. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1007. }
  1008. }
  1009. /**
  1010. * Apply motion vectors to prediction buffer, chapter 18.
  1011. */
  1012. static av_always_inline
  1013. void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
  1014. int mb_x, int mb_y)
  1015. {
  1016. int x_off = mb_x << 4, y_off = mb_y << 4;
  1017. int width = 16*s->mb_width, height = 16*s->mb_height;
  1018. AVFrame *ref = s->framep[mb->ref_frame];
  1019. VP56mv *bmv = mb->bmv;
  1020. if (mb->mode < VP8_MVMODE_SPLIT) {
  1021. vp8_mc_part(s, dst, ref, x_off, y_off,
  1022. 0, 0, 16, 16, width, height, &mb->mv);
  1023. } else switch (mb->partitioning) {
  1024. case VP8_SPLITMVMODE_4x4: {
  1025. int x, y;
  1026. VP56mv uvmv;
  1027. /* Y */
  1028. for (y = 0; y < 4; y++) {
  1029. for (x = 0; x < 4; x++) {
  1030. vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
  1031. ref->data[0], &bmv[4*y + x],
  1032. 4*x + x_off, 4*y + y_off, 4, 4,
  1033. width, height, s->linesize,
  1034. s->put_pixels_tab[2]);
  1035. }
  1036. }
  1037. /* U/V */
  1038. x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
  1039. for (y = 0; y < 2; y++) {
  1040. for (x = 0; x < 2; x++) {
  1041. uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
  1042. mb->bmv[ 2*y * 4 + 2*x+1].x +
  1043. mb->bmv[(2*y+1) * 4 + 2*x ].x +
  1044. mb->bmv[(2*y+1) * 4 + 2*x+1].x;
  1045. uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
  1046. mb->bmv[ 2*y * 4 + 2*x+1].y +
  1047. mb->bmv[(2*y+1) * 4 + 2*x ].y +
  1048. mb->bmv[(2*y+1) * 4 + 2*x+1].y;
  1049. uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
  1050. uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
  1051. if (s->profile == 3) {
  1052. uvmv.x &= ~7;
  1053. uvmv.y &= ~7;
  1054. }
  1055. vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
  1056. ref->data[1], &uvmv,
  1057. 4*x + x_off, 4*y + y_off, 4, 4,
  1058. width, height, s->uvlinesize,
  1059. s->put_pixels_tab[2]);
  1060. vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
  1061. ref->data[2], &uvmv,
  1062. 4*x + x_off, 4*y + y_off, 4, 4,
  1063. width, height, s->uvlinesize,
  1064. s->put_pixels_tab[2]);
  1065. }
  1066. }
  1067. break;
  1068. }
  1069. case VP8_SPLITMVMODE_16x8:
  1070. vp8_mc_part(s, dst, ref, x_off, y_off,
  1071. 0, 0, 16, 8, width, height, &bmv[0]);
  1072. vp8_mc_part(s, dst, ref, x_off, y_off,
  1073. 0, 8, 16, 8, width, height, &bmv[1]);
  1074. break;
  1075. case VP8_SPLITMVMODE_8x16:
  1076. vp8_mc_part(s, dst, ref, x_off, y_off,
  1077. 0, 0, 8, 16, width, height, &bmv[0]);
  1078. vp8_mc_part(s, dst, ref, x_off, y_off,
  1079. 8, 0, 8, 16, width, height, &bmv[1]);
  1080. break;
  1081. case VP8_SPLITMVMODE_8x8:
  1082. vp8_mc_part(s, dst, ref, x_off, y_off,
  1083. 0, 0, 8, 8, width, height, &bmv[0]);
  1084. vp8_mc_part(s, dst, ref, x_off, y_off,
  1085. 8, 0, 8, 8, width, height, &bmv[1]);
  1086. vp8_mc_part(s, dst, ref, x_off, y_off,
  1087. 0, 8, 8, 8, width, height, &bmv[2]);
  1088. vp8_mc_part(s, dst, ref, x_off, y_off,
  1089. 8, 8, 8, 8, width, height, &bmv[3]);
  1090. break;
  1091. }
  1092. }
  1093. static av_always_inline void idct_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb)
  1094. {
  1095. int x, y, ch;
  1096. if (mb->mode != MODE_I4x4) {
  1097. uint8_t *y_dst = dst[0];
  1098. for (y = 0; y < 4; y++) {
  1099. uint32_t nnz4 = AV_RN32A(s->non_zero_count_cache[y]);
  1100. if (nnz4) {
  1101. if (nnz4&~0x01010101) {
  1102. for (x = 0; x < 4; x++) {
  1103. int nnz = s->non_zero_count_cache[y][x];
  1104. if (nnz) {
  1105. if (nnz == 1)
  1106. s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
  1107. else
  1108. s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
  1109. }
  1110. }
  1111. } else {
  1112. s->vp8dsp.vp8_idct_dc_add4y(y_dst, s->block[y], s->linesize);
  1113. }
  1114. }
  1115. y_dst += 4*s->linesize;
  1116. }
  1117. }
  1118. for (ch = 0; ch < 2; ch++) {
  1119. uint32_t nnz4 = AV_RN32A(s->non_zero_count_cache[4+ch]);
  1120. if (nnz4) {
  1121. uint8_t *ch_dst = dst[1+ch];
  1122. if (nnz4&~0x01010101) {
  1123. for (y = 0; y < 2; y++) {
  1124. for (x = 0; x < 2; x++) {
  1125. int nnz = s->non_zero_count_cache[4+ch][(y<<1)+x];
  1126. if (nnz) {
  1127. if (nnz == 1)
  1128. s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1129. else
  1130. s->vp8dsp.vp8_idct_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
  1131. }
  1132. }
  1133. ch_dst += 4*s->uvlinesize;
  1134. }
  1135. } else {
  1136. s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, s->block[4+ch], s->uvlinesize);
  1137. }
  1138. }
  1139. }
  1140. }
  1141. static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
  1142. {
  1143. int interior_limit, filter_level;
  1144. if (s->segmentation.enabled) {
  1145. filter_level = s->segmentation.filter_level[s->segment];
  1146. if (!s->segmentation.absolute_vals)
  1147. filter_level += s->filter.level;
  1148. } else
  1149. filter_level = s->filter.level;
  1150. if (s->lf_delta.enabled) {
  1151. filter_level += s->lf_delta.ref[mb->ref_frame];
  1152. if (mb->ref_frame == VP56_FRAME_CURRENT) {
  1153. if (mb->mode == MODE_I4x4)
  1154. filter_level += s->lf_delta.mode[0];
  1155. } else {
  1156. if (mb->mode == VP8_MVMODE_ZERO)
  1157. filter_level += s->lf_delta.mode[1];
  1158. else if (mb->mode == VP8_MVMODE_SPLIT)
  1159. filter_level += s->lf_delta.mode[3];
  1160. else
  1161. filter_level += s->lf_delta.mode[2];
  1162. }
  1163. }
  1164. filter_level = av_clip(filter_level, 0, 63);
  1165. interior_limit = filter_level;
  1166. if (s->filter.sharpness) {
  1167. interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
  1168. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  1169. }
  1170. interior_limit = FFMAX(interior_limit, 1);
  1171. f->filter_level = filter_level;
  1172. f->inner_limit = interior_limit;
  1173. f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
  1174. }
  1175. static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
  1176. {
  1177. int mbedge_lim, bedge_lim, hev_thresh;
  1178. int filter_level = f->filter_level;
  1179. int inner_limit = f->inner_limit;
  1180. int inner_filter = f->inner_filter;
  1181. int linesize = s->linesize;
  1182. int uvlinesize = s->uvlinesize;
  1183. if (!filter_level)
  1184. return;
  1185. mbedge_lim = 2*(filter_level+2) + inner_limit;
  1186. bedge_lim = 2* filter_level + inner_limit;
  1187. hev_thresh = filter_level >= 15;
  1188. if (s->keyframe) {
  1189. if (filter_level >= 40)
  1190. hev_thresh = 2;
  1191. } else {
  1192. if (filter_level >= 40)
  1193. hev_thresh = 3;
  1194. else if (filter_level >= 20)
  1195. hev_thresh = 2;
  1196. }
  1197. if (mb_x) {
  1198. s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
  1199. mbedge_lim, inner_limit, hev_thresh);
  1200. s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1201. mbedge_lim, inner_limit, hev_thresh);
  1202. }
  1203. if (inner_filter) {
  1204. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
  1205. inner_limit, hev_thresh);
  1206. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
  1207. inner_limit, hev_thresh);
  1208. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
  1209. inner_limit, hev_thresh);
  1210. s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
  1211. uvlinesize, bedge_lim,
  1212. inner_limit, hev_thresh);
  1213. }
  1214. if (mb_y) {
  1215. s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
  1216. mbedge_lim, inner_limit, hev_thresh);
  1217. s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1218. mbedge_lim, inner_limit, hev_thresh);
  1219. }
  1220. if (inner_filter) {
  1221. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
  1222. linesize, bedge_lim,
  1223. inner_limit, hev_thresh);
  1224. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
  1225. linesize, bedge_lim,
  1226. inner_limit, hev_thresh);
  1227. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
  1228. linesize, bedge_lim,
  1229. inner_limit, hev_thresh);
  1230. s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
  1231. dst[2] + 4 * uvlinesize,
  1232. uvlinesize, bedge_lim,
  1233. inner_limit, hev_thresh);
  1234. }
  1235. }
  1236. static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
  1237. {
  1238. int mbedge_lim, bedge_lim;
  1239. int filter_level = f->filter_level;
  1240. int inner_limit = f->inner_limit;
  1241. int inner_filter = f->inner_filter;
  1242. int linesize = s->linesize;
  1243. if (!filter_level)
  1244. return;
  1245. mbedge_lim = 2*(filter_level+2) + inner_limit;
  1246. bedge_lim = 2* filter_level + inner_limit;
  1247. if (mb_x)
  1248. s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
  1249. if (inner_filter) {
  1250. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
  1251. s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
  1252. s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
  1253. }
  1254. if (mb_y)
  1255. s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
  1256. if (inner_filter) {
  1257. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
  1258. s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
  1259. s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
  1260. }
  1261. }
  1262. static void filter_mb_row(VP8Context *s, int mb_y)
  1263. {
  1264. VP8FilterStrength *f = s->filter_strength;
  1265. uint8_t *dst[3] = {
  1266. s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
  1267. s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
  1268. s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
  1269. };
  1270. int mb_x;
  1271. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1272. backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  1273. filter_mb(s, dst, f++, mb_x, mb_y);
  1274. dst[0] += 16;
  1275. dst[1] += 8;
  1276. dst[2] += 8;
  1277. }
  1278. }
  1279. static void filter_mb_row_simple(VP8Context *s, int mb_y)
  1280. {
  1281. VP8FilterStrength *f = s->filter_strength;
  1282. uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
  1283. int mb_x;
  1284. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1285. backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
  1286. filter_mb_simple(s, dst, f++, mb_x, mb_y);
  1287. dst += 16;
  1288. }
  1289. }
  1290. static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  1291. AVPacket *avpkt)
  1292. {
  1293. VP8Context *s = avctx->priv_data;
  1294. int ret, mb_x, mb_y, i, y, referenced;
  1295. enum AVDiscard skip_thresh;
  1296. AVFrame *av_uninit(curframe);
  1297. if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
  1298. return ret;
  1299. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
  1300. || s->update_altref == VP56_FRAME_CURRENT;
  1301. skip_thresh = !referenced ? AVDISCARD_NONREF :
  1302. !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
  1303. if (avctx->skip_frame >= skip_thresh) {
  1304. s->invisible = 1;
  1305. goto skip_decode;
  1306. }
  1307. s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
  1308. for (i = 0; i < 4; i++)
  1309. if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1310. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1311. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  1312. curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
  1313. break;
  1314. }
  1315. if (curframe->data[0])
  1316. avctx->release_buffer(avctx, curframe);
  1317. curframe->key_frame = s->keyframe;
  1318. curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  1319. curframe->reference = referenced ? 3 : 0;
  1320. if ((ret = avctx->get_buffer(avctx, curframe))) {
  1321. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
  1322. return ret;
  1323. }
  1324. // Given that arithmetic probabilities are updated every frame, it's quite likely
  1325. // that the values we have on a random interframe are complete junk if we didn't
  1326. // start decode on a keyframe. So just don't display anything rather than junk.
  1327. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  1328. !s->framep[VP56_FRAME_GOLDEN] ||
  1329. !s->framep[VP56_FRAME_GOLDEN2])) {
  1330. av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
  1331. return AVERROR_INVALIDDATA;
  1332. }
  1333. s->linesize = curframe->linesize[0];
  1334. s->uvlinesize = curframe->linesize[1];
  1335. if (!s->edge_emu_buffer)
  1336. s->edge_emu_buffer = av_malloc(21*s->linesize);
  1337. memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
  1338. /* Zero macroblock structures for top/top-left prediction from outside the frame. */
  1339. memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
  1340. // top edge of 127 for intra prediction
  1341. memset(s->top_border, 127, (s->mb_width+1)*sizeof(*s->top_border));
  1342. memset(s->ref_count, 0, sizeof(s->ref_count));
  1343. if (s->keyframe)
  1344. memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
  1345. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1346. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
  1347. VP8Macroblock *mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
  1348. int mb_xy = mb_y*s->mb_width;
  1349. uint8_t *dst[3] = {
  1350. curframe->data[0] + 16*mb_y*s->linesize,
  1351. curframe->data[1] + 8*mb_y*s->uvlinesize,
  1352. curframe->data[2] + 8*mb_y*s->uvlinesize
  1353. };
  1354. memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
  1355. memset(s->left_nnz, 0, sizeof(s->left_nnz));
  1356. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
  1357. // left edge of 129 for intra prediction
  1358. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
  1359. for (i = 0; i < 3; i++)
  1360. for (y = 0; y < 16>>!!i; y++)
  1361. dst[i][y*curframe->linesize[i]-1] = 129;
  1362. if (mb_y)
  1363. memset(s->top_border, 129, sizeof(*s->top_border));
  1364. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  1365. /* Prefetch the current frame, 4 MBs ahead */
  1366. s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  1367. s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
  1368. decode_mb_mode(s, mb, mb_x, mb_y, s->segmentation_map + mb_xy);
  1369. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
  1370. if (!mb->skip)
  1371. decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
  1372. if (mb->mode <= MODE_I4x4)
  1373. intra_predict(s, dst, mb, mb_x, mb_y);
  1374. else
  1375. inter_predict(s, dst, mb, mb_x, mb_y);
  1376. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
  1377. if (!mb->skip) {
  1378. idct_mb(s, dst, mb);
  1379. } else {
  1380. AV_ZERO64(s->left_nnz);
  1381. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  1382. // Reset DC block predictors if they would exist if the mb had coefficients
  1383. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  1384. s->left_nnz[8] = 0;
  1385. s->top_nnz[mb_x][8] = 0;
  1386. }
  1387. }
  1388. if (s->deblock_filter)
  1389. filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
  1390. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
  1391. dst[0] += 16;
  1392. dst[1] += 8;
  1393. dst[2] += 8;
  1394. }
  1395. if (s->deblock_filter) {
  1396. if (s->filter.simple)
  1397. filter_mb_row_simple(s, mb_y);
  1398. else
  1399. filter_mb_row(s, mb_y);
  1400. }
  1401. }
  1402. skip_decode:
  1403. // if future frames don't use the updated probabilities,
  1404. // reset them to the values we saved
  1405. if (!s->update_probabilities)
  1406. s->prob[0] = s->prob[1];
  1407. // check if golden and altref are swapped
  1408. if (s->update_altref == VP56_FRAME_GOLDEN &&
  1409. s->update_golden == VP56_FRAME_GOLDEN2)
  1410. FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
  1411. else {
  1412. if (s->update_altref != VP56_FRAME_NONE)
  1413. s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  1414. if (s->update_golden != VP56_FRAME_NONE)
  1415. s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  1416. }
  1417. if (s->update_last) // move cur->prev
  1418. s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
  1419. // release no longer referenced frames
  1420. for (i = 0; i < 4; i++)
  1421. if (s->frames[i].data[0] &&
  1422. &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
  1423. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  1424. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  1425. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  1426. avctx->release_buffer(avctx, &s->frames[i]);
  1427. if (!s->invisible) {
  1428. *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
  1429. *data_size = sizeof(AVFrame);
  1430. }
  1431. return avpkt->size;
  1432. }
  1433. static av_cold int vp8_decode_init(AVCodecContext *avctx)
  1434. {
  1435. VP8Context *s = avctx->priv_data;
  1436. s->avctx = avctx;
  1437. avctx->pix_fmt = PIX_FMT_YUV420P;
  1438. dsputil_init(&s->dsp, avctx);
  1439. ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
  1440. ff_vp8dsp_init(&s->vp8dsp);
  1441. // intra pred needs edge emulation among other things
  1442. if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
  1443. av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported\n");
  1444. return AVERROR_PATCHWELCOME;
  1445. }
  1446. return 0;
  1447. }
  1448. static av_cold int vp8_decode_free(AVCodecContext *avctx)
  1449. {
  1450. vp8_decode_flush(avctx);
  1451. return 0;
  1452. }
  1453. AVCodec vp8_decoder = {
  1454. "vp8",
  1455. AVMEDIA_TYPE_VIDEO,
  1456. CODEC_ID_VP8,
  1457. sizeof(VP8Context),
  1458. vp8_decode_init,
  1459. NULL,
  1460. vp8_decode_free,
  1461. vp8_decode_frame,
  1462. CODEC_CAP_DR1,
  1463. .flush = vp8_decode_flush,
  1464. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  1465. };