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.

1413 lines
48KB

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