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.

1673 lines
58KB

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