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.

3566 lines
143KB

  1. /*
  2. * VP9 compatible video decoder
  3. *
  4. * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. * Copyright (C) 2013 Clément Bœsch <u pkh me>
  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 "get_bits.h"
  25. #include "internal.h"
  26. #include "videodsp.h"
  27. #include "vp56.h"
  28. #include "vp9.h"
  29. #include "vp9data.h"
  30. #include "vp9dsp.h"
  31. #include "libavutil/avassert.h"
  32. #define VP9_SYNCCODE 0x498342
  33. enum CompPredMode {
  34. PRED_SINGLEREF,
  35. PRED_COMPREF,
  36. PRED_SWITCHABLE,
  37. };
  38. enum BlockLevel {
  39. BL_64X64,
  40. BL_32X32,
  41. BL_16X16,
  42. BL_8X8,
  43. };
  44. enum BlockSize {
  45. BS_64x64,
  46. BS_64x32,
  47. BS_32x64,
  48. BS_32x32,
  49. BS_32x16,
  50. BS_16x32,
  51. BS_16x16,
  52. BS_16x8,
  53. BS_8x16,
  54. BS_8x8,
  55. BS_8x4,
  56. BS_4x8,
  57. BS_4x4,
  58. N_BS_SIZES,
  59. };
  60. struct VP9mvrefPair {
  61. VP56mv mv[2];
  62. int8_t ref[2];
  63. };
  64. struct VP9Filter {
  65. uint8_t level[8 * 8];
  66. uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
  67. [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
  68. };
  69. typedef struct VP9Block {
  70. uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
  71. enum FilterMode filter;
  72. VP56mv mv[4 /* b_idx */][2 /* ref */];
  73. enum BlockSize bs;
  74. enum TxfmMode tx, uvtx;
  75. int row, row7, col, col7;
  76. uint8_t *dst[3];
  77. ptrdiff_t y_stride, uv_stride;
  78. } VP9Block;
  79. typedef struct VP9Context {
  80. VP9DSPContext dsp;
  81. VideoDSPContext vdsp;
  82. GetBitContext gb;
  83. VP56RangeCoder c;
  84. VP56RangeCoder *c_b;
  85. unsigned c_b_size;
  86. VP9Block b;
  87. // bitstream header
  88. uint8_t profile;
  89. uint8_t keyframe, last_keyframe;
  90. uint8_t invisible;
  91. uint8_t use_last_frame_mvs;
  92. uint8_t errorres;
  93. uint8_t colorspace;
  94. uint8_t fullrange;
  95. uint8_t intraonly;
  96. uint8_t resetctx;
  97. uint8_t refreshrefmask;
  98. uint8_t highprecisionmvs;
  99. enum FilterMode filtermode;
  100. uint8_t allowcompinter;
  101. uint8_t fixcompref;
  102. uint8_t refreshctx;
  103. uint8_t parallelmode;
  104. uint8_t framectxid;
  105. uint8_t refidx[3];
  106. uint8_t signbias[3];
  107. uint8_t varcompref[2];
  108. AVFrame *refs[8], *f;
  109. struct {
  110. uint8_t level;
  111. int8_t sharpness;
  112. uint8_t lim_lut[64];
  113. uint8_t mblim_lut[64];
  114. } filter;
  115. struct {
  116. uint8_t enabled;
  117. int8_t mode[2];
  118. int8_t ref[4];
  119. } lf_delta;
  120. uint8_t yac_qi;
  121. int8_t ydc_qdelta, uvdc_qdelta, uvac_qdelta;
  122. uint8_t lossless;
  123. struct {
  124. uint8_t enabled;
  125. uint8_t temporal;
  126. uint8_t absolute_vals;
  127. uint8_t update_map;
  128. struct {
  129. uint8_t q_enabled;
  130. uint8_t lf_enabled;
  131. uint8_t ref_enabled;
  132. uint8_t skip_enabled;
  133. uint8_t ref_val;
  134. int16_t q_val;
  135. int8_t lf_val;
  136. int16_t qmul[2][2];
  137. uint8_t lflvl[4][2];
  138. } feat[8];
  139. } segmentation;
  140. struct {
  141. unsigned log2_tile_cols, log2_tile_rows;
  142. unsigned tile_cols, tile_rows;
  143. unsigned tile_row_start, tile_row_end, tile_col_start, tile_col_end;
  144. } tiling;
  145. unsigned sb_cols, sb_rows, rows, cols;
  146. struct {
  147. prob_context p;
  148. uint8_t coef[4][2][2][6][6][3];
  149. } prob_ctx[4];
  150. struct {
  151. prob_context p;
  152. uint8_t coef[4][2][2][6][6][11];
  153. uint8_t seg[7];
  154. uint8_t segpred[3];
  155. } prob;
  156. struct {
  157. unsigned y_mode[4][10];
  158. unsigned uv_mode[10][10];
  159. unsigned filter[4][3];
  160. unsigned mv_mode[7][4];
  161. unsigned intra[4][2];
  162. unsigned comp[5][2];
  163. unsigned single_ref[5][2][2];
  164. unsigned comp_ref[5][2];
  165. unsigned tx32p[2][4];
  166. unsigned tx16p[2][3];
  167. unsigned tx8p[2][2];
  168. unsigned skip[3][2];
  169. unsigned mv_joint[4];
  170. struct {
  171. unsigned sign[2];
  172. unsigned classes[11];
  173. unsigned class0[2];
  174. unsigned bits[10][2];
  175. unsigned class0_fp[2][4];
  176. unsigned fp[4];
  177. unsigned class0_hp[2];
  178. unsigned hp[2];
  179. } mv_comp[2];
  180. unsigned partition[4][4][4];
  181. unsigned coef[4][2][2][6][6][3];
  182. unsigned eob[4][2][2][6][6][2];
  183. } counts;
  184. enum TxfmMode txfmmode;
  185. enum CompPredMode comppredmode;
  186. // contextual (left/above) cache
  187. uint8_t left_partition_ctx[8], *above_partition_ctx;
  188. uint8_t left_mode_ctx[16], *above_mode_ctx;
  189. // FIXME maybe merge some of the below in a flags field?
  190. uint8_t left_y_nnz_ctx[16], *above_y_nnz_ctx;
  191. uint8_t left_uv_nnz_ctx[2][8], *above_uv_nnz_ctx[2];
  192. uint8_t left_skip_ctx[8], *above_skip_ctx; // 1bit
  193. uint8_t left_txfm_ctx[8], *above_txfm_ctx; // 2bit
  194. uint8_t left_segpred_ctx[8], *above_segpred_ctx; // 1bit
  195. uint8_t left_intra_ctx[8], *above_intra_ctx; // 1bit
  196. uint8_t left_comp_ctx[8], *above_comp_ctx; // 1bit
  197. uint8_t left_ref_ctx[8], *above_ref_ctx; // 2bit
  198. uint8_t left_filter_ctx[8], *above_filter_ctx;
  199. VP56mv left_mv_ctx[16][2], (*above_mv_ctx)[2];
  200. // whole-frame cache
  201. uint8_t *intra_pred_data[3];
  202. uint8_t *segmentation_map;
  203. struct VP9mvrefPair *mv[2];
  204. struct VP9Filter *lflvl;
  205. DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[71*80];
  206. // block reconstruction intermediates
  207. DECLARE_ALIGNED(32, int16_t, block)[4096];
  208. DECLARE_ALIGNED(32, int16_t, uvblock)[2][1024];
  209. uint8_t eob[256];
  210. uint8_t uveob[2][64];
  211. VP56mv min_mv, max_mv;
  212. DECLARE_ALIGNED(32, uint8_t, tmp_y)[64*64];
  213. DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][32*32];
  214. } VP9Context;
  215. static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
  216. {
  217. { 16, 16 }, { 16, 8 }, { 8, 16 }, { 8, 8 }, { 8, 4 }, { 4, 8 },
  218. { 4, 4 }, { 4, 2 }, { 2, 4 }, { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 },
  219. }, {
  220. { 8, 8 }, { 8, 4 }, { 4, 8 }, { 4, 4 }, { 4, 2 }, { 2, 4 },
  221. { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
  222. }
  223. };
  224. static int update_size(AVCodecContext *ctx, int w, int h)
  225. {
  226. VP9Context *s = ctx->priv_data;
  227. uint8_t *p;
  228. av_assert0(w > 0 && h > 0);
  229. if (s->above_partition_ctx && w == ctx->width && h == ctx->height)
  230. return 0;
  231. ctx->width = w;
  232. ctx->height = h;
  233. s->sb_cols = (w + 63) >> 6;
  234. s->sb_rows = (h + 63) >> 6;
  235. s->cols = (w + 7) >> 3;
  236. s->rows = (h + 7) >> 3;
  237. #define assign(var, type, n) var = (type) p; p += s->sb_cols * n * sizeof(*var)
  238. av_freep(&s->above_partition_ctx);
  239. p = av_malloc(s->sb_cols * (240 + sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx) +
  240. 64 * s->sb_rows * (1 + sizeof(*s->mv[0]) * 2)));
  241. if (!p)
  242. return AVERROR(ENOMEM);
  243. assign(s->above_partition_ctx, uint8_t *, 8);
  244. assign(s->above_skip_ctx, uint8_t *, 8);
  245. assign(s->above_txfm_ctx, uint8_t *, 8);
  246. assign(s->above_mode_ctx, uint8_t *, 16);
  247. assign(s->above_y_nnz_ctx, uint8_t *, 16);
  248. assign(s->above_uv_nnz_ctx[0], uint8_t *, 8);
  249. assign(s->above_uv_nnz_ctx[1], uint8_t *, 8);
  250. assign(s->intra_pred_data[0], uint8_t *, 64);
  251. assign(s->intra_pred_data[1], uint8_t *, 32);
  252. assign(s->intra_pred_data[2], uint8_t *, 32);
  253. assign(s->above_segpred_ctx, uint8_t *, 8);
  254. assign(s->above_intra_ctx, uint8_t *, 8);
  255. assign(s->above_comp_ctx, uint8_t *, 8);
  256. assign(s->above_ref_ctx, uint8_t *, 8);
  257. assign(s->above_filter_ctx, uint8_t *, 8);
  258. assign(s->lflvl, struct VP9Filter *, 1);
  259. assign(s->above_mv_ctx, VP56mv(*)[2], 16);
  260. assign(s->segmentation_map, uint8_t *, 64 * s->sb_rows);
  261. assign(s->mv[0], struct VP9mvrefPair *, 64 * s->sb_rows);
  262. assign(s->mv[1], struct VP9mvrefPair *, 64 * s->sb_rows);
  263. #undef assign
  264. return 0;
  265. }
  266. // for some reason the sign bit is at the end, not the start, of a bit sequence
  267. static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
  268. {
  269. int v = get_bits(gb, n);
  270. return get_bits1(gb) ? -v : v;
  271. }
  272. static av_always_inline int inv_recenter_nonneg(int v, int m)
  273. {
  274. return v > 2 * m ? v : v & 1 ? m - ((v + 1) >> 1) : m + (v >> 1);
  275. }
  276. // differential forward probability updates
  277. static int update_prob(VP56RangeCoder *c, int p)
  278. {
  279. static const int inv_map_table[254] = {
  280. 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
  281. 189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
  282. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
  283. 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
  284. 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
  285. 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  286. 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
  287. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
  288. 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
  289. 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
  290. 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
  291. 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
  292. 161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  293. 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
  294. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
  295. 207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
  296. 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
  297. 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
  298. 252, 253,
  299. };
  300. int d;
  301. /* This code is trying to do a differential probability update. For a
  302. * current probability A in the range [1, 255], the difference to a new
  303. * probability of any value can be expressed differentially as 1-A,255-A
  304. * where some part of this (absolute range) exists both in positive as
  305. * well as the negative part, whereas another part only exists in one
  306. * half. We're trying to code this shared part differentially, i.e.
  307. * times two where the value of the lowest bit specifies the sign, and
  308. * the single part is then coded on top of this. This absolute difference
  309. * then again has a value of [0,254], but a bigger value in this range
  310. * indicates that we're further away from the original value A, so we
  311. * can code this as a VLC code, since higher values are increasingly
  312. * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
  313. * updates vs. the 'fine, exact' updates further down the range, which
  314. * adds one extra dimension to this differential update model. */
  315. if (!vp8_rac_get(c)) {
  316. d = vp8_rac_get_uint(c, 4) + 0;
  317. } else if (!vp8_rac_get(c)) {
  318. d = vp8_rac_get_uint(c, 4) + 16;
  319. } else if (!vp8_rac_get(c)) {
  320. d = vp8_rac_get_uint(c, 5) + 32;
  321. } else {
  322. d = vp8_rac_get_uint(c, 7);
  323. if (d >= 65)
  324. d = (d << 1) - 65 + vp8_rac_get(c);
  325. d += 64;
  326. }
  327. return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
  328. 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
  329. }
  330. static int decode_frame_header(AVCodecContext *ctx,
  331. const uint8_t *data, int size, int *ref)
  332. {
  333. VP9Context *s = ctx->priv_data;
  334. int c, i, j, k, l, m, n, w, h, max, size2, res, sharp;
  335. int last_invisible;
  336. const uint8_t *data2;
  337. /* general header */
  338. if ((res = init_get_bits8(&s->gb, data, size)) < 0) {
  339. av_log(ctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
  340. return res;
  341. }
  342. if (get_bits(&s->gb, 2) != 0x2) { // frame marker
  343. av_log(ctx, AV_LOG_ERROR, "Invalid frame marker\n");
  344. return AVERROR_INVALIDDATA;
  345. }
  346. s->profile = get_bits1(&s->gb);
  347. if (get_bits1(&s->gb)) { // reserved bit
  348. av_log(ctx, AV_LOG_ERROR, "Reserved bit should be zero\n");
  349. return AVERROR_INVALIDDATA;
  350. }
  351. if (get_bits1(&s->gb)) {
  352. *ref = get_bits(&s->gb, 3);
  353. return 0;
  354. }
  355. s->last_keyframe = s->keyframe;
  356. s->keyframe = !get_bits1(&s->gb);
  357. last_invisible = s->invisible;
  358. s->invisible = !get_bits1(&s->gb);
  359. s->errorres = get_bits1(&s->gb);
  360. // FIXME disable this upon resolution change
  361. s->use_last_frame_mvs = !s->errorres && !last_invisible;
  362. if (s->keyframe) {
  363. if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
  364. av_log(ctx, AV_LOG_ERROR, "Invalid sync code\n");
  365. return AVERROR_INVALIDDATA;
  366. }
  367. s->colorspace = get_bits(&s->gb, 3);
  368. if (s->colorspace == 7) { // RGB = profile 1
  369. av_log(ctx, AV_LOG_ERROR, "RGB not supported in profile 0\n");
  370. return AVERROR_INVALIDDATA;
  371. }
  372. s->fullrange = get_bits1(&s->gb);
  373. // for profile 1, here follows the subsampling bits
  374. s->refreshrefmask = 0xff;
  375. w = get_bits(&s->gb, 16) + 1;
  376. h = get_bits(&s->gb, 16) + 1;
  377. if (get_bits1(&s->gb)) // display size
  378. skip_bits(&s->gb, 32);
  379. } else {
  380. s->intraonly = s->invisible ? get_bits1(&s->gb) : 0;
  381. s->resetctx = s->errorres ? 0 : get_bits(&s->gb, 2);
  382. if (s->intraonly) {
  383. if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
  384. av_log(ctx, AV_LOG_ERROR, "Invalid sync code\n");
  385. return AVERROR_INVALIDDATA;
  386. }
  387. s->refreshrefmask = get_bits(&s->gb, 8);
  388. w = get_bits(&s->gb, 16) + 1;
  389. h = get_bits(&s->gb, 16) + 1;
  390. if (get_bits1(&s->gb)) // display size
  391. skip_bits(&s->gb, 32);
  392. } else {
  393. s->refreshrefmask = get_bits(&s->gb, 8);
  394. s->refidx[0] = get_bits(&s->gb, 3);
  395. s->signbias[0] = get_bits1(&s->gb);
  396. s->refidx[1] = get_bits(&s->gb, 3);
  397. s->signbias[1] = get_bits1(&s->gb);
  398. s->refidx[2] = get_bits(&s->gb, 3);
  399. s->signbias[2] = get_bits1(&s->gb);
  400. if (!s->refs[s->refidx[0]]->buf[0] ||
  401. !s->refs[s->refidx[1]]->buf[0] ||
  402. !s->refs[s->refidx[2]]->buf[0]) {
  403. av_log(ctx, AV_LOG_ERROR, "Not all references are available\n");
  404. return AVERROR_INVALIDDATA;
  405. }
  406. if (get_bits1(&s->gb)) {
  407. w = s->refs[s->refidx[0]]->width;
  408. h = s->refs[s->refidx[0]]->height;
  409. } else if (get_bits1(&s->gb)) {
  410. w = s->refs[s->refidx[1]]->width;
  411. h = s->refs[s->refidx[1]]->height;
  412. } else if (get_bits1(&s->gb)) {
  413. w = s->refs[s->refidx[2]]->width;
  414. h = s->refs[s->refidx[2]]->height;
  415. } else {
  416. w = get_bits(&s->gb, 16) + 1;
  417. h = get_bits(&s->gb, 16) + 1;
  418. }
  419. if (get_bits1(&s->gb)) // display size
  420. skip_bits(&s->gb, 32);
  421. s->highprecisionmvs = get_bits1(&s->gb);
  422. s->filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
  423. get_bits(&s->gb, 2);
  424. s->allowcompinter = s->signbias[0] != s->signbias[1] ||
  425. s->signbias[0] != s->signbias[2];
  426. if (s->allowcompinter) {
  427. if (s->signbias[0] == s->signbias[1]) {
  428. s->fixcompref = 2;
  429. s->varcompref[0] = 0;
  430. s->varcompref[1] = 1;
  431. } else if (s->signbias[0] == s->signbias[2]) {
  432. s->fixcompref = 1;
  433. s->varcompref[0] = 0;
  434. s->varcompref[1] = 2;
  435. } else {
  436. s->fixcompref = 0;
  437. s->varcompref[0] = 1;
  438. s->varcompref[1] = 2;
  439. }
  440. }
  441. }
  442. }
  443. s->refreshctx = s->errorres ? 0 : get_bits1(&s->gb);
  444. s->parallelmode = s->errorres ? 1 : get_bits1(&s->gb);
  445. s->framectxid = c = get_bits(&s->gb, 2);
  446. /* loopfilter header data */
  447. s->filter.level = get_bits(&s->gb, 6);
  448. sharp = get_bits(&s->gb, 3);
  449. // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
  450. // the old cache values since they are still valid
  451. if (s->filter.sharpness != sharp)
  452. memset(s->filter.lim_lut, 0, sizeof(s->filter.lim_lut));
  453. s->filter.sharpness = sharp;
  454. if ((s->lf_delta.enabled = get_bits1(&s->gb))) {
  455. if (get_bits1(&s->gb)) {
  456. for (i = 0; i < 4; i++)
  457. if (get_bits1(&s->gb))
  458. s->lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
  459. for (i = 0; i < 2; i++)
  460. if (get_bits1(&s->gb))
  461. s->lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
  462. }
  463. } else {
  464. memset(&s->lf_delta, 0, sizeof(s->lf_delta));
  465. }
  466. /* quantization header data */
  467. s->yac_qi = get_bits(&s->gb, 8);
  468. s->ydc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
  469. s->uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
  470. s->uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
  471. s->lossless = s->yac_qi == 0 && s->ydc_qdelta == 0 &&
  472. s->uvdc_qdelta == 0 && s->uvac_qdelta == 0;
  473. /* segmentation header info */
  474. if ((s->segmentation.enabled = get_bits1(&s->gb))) {
  475. if ((s->segmentation.update_map = get_bits1(&s->gb))) {
  476. for (i = 0; i < 7; i++)
  477. s->prob.seg[i] = get_bits1(&s->gb) ?
  478. get_bits(&s->gb, 8) : 255;
  479. if ((s->segmentation.temporal = get_bits1(&s->gb)))
  480. for (i = 0; i < 3; i++)
  481. s->prob.segpred[i] = get_bits1(&s->gb) ?
  482. get_bits(&s->gb, 8) : 255;
  483. }
  484. if (get_bits1(&s->gb)) {
  485. s->segmentation.absolute_vals = get_bits1(&s->gb);
  486. for (i = 0; i < 8; i++) {
  487. if ((s->segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
  488. s->segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
  489. if ((s->segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
  490. s->segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
  491. if ((s->segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
  492. s->segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
  493. s->segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
  494. }
  495. }
  496. } else {
  497. s->segmentation.feat[0].q_enabled = 0;
  498. s->segmentation.feat[0].lf_enabled = 0;
  499. s->segmentation.feat[0].skip_enabled = 0;
  500. s->segmentation.feat[0].ref_enabled = 0;
  501. }
  502. // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
  503. for (i = 0; i < (s->segmentation.enabled ? 8 : 1); i++) {
  504. int qyac, qydc, quvac, quvdc, lflvl, sh;
  505. if (s->segmentation.feat[i].q_enabled) {
  506. if (s->segmentation.absolute_vals)
  507. qyac = s->segmentation.feat[i].q_val;
  508. else
  509. qyac = s->yac_qi + s->segmentation.feat[i].q_val;
  510. } else {
  511. qyac = s->yac_qi;
  512. }
  513. qydc = av_clip_uintp2(qyac + s->ydc_qdelta, 8);
  514. quvdc = av_clip_uintp2(qyac + s->uvdc_qdelta, 8);
  515. quvac = av_clip_uintp2(qyac + s->uvac_qdelta, 8);
  516. qyac = av_clip_uintp2(qyac, 8);
  517. s->segmentation.feat[i].qmul[0][0] = vp9_dc_qlookup[qydc];
  518. s->segmentation.feat[i].qmul[0][1] = vp9_ac_qlookup[qyac];
  519. s->segmentation.feat[i].qmul[1][0] = vp9_dc_qlookup[quvdc];
  520. s->segmentation.feat[i].qmul[1][1] = vp9_ac_qlookup[quvac];
  521. sh = s->filter.level >= 32;
  522. if (s->segmentation.feat[i].lf_enabled) {
  523. if (s->segmentation.absolute_vals)
  524. lflvl = s->segmentation.feat[i].lf_val;
  525. else
  526. lflvl = s->filter.level + s->segmentation.feat[i].lf_val;
  527. } else {
  528. lflvl = s->filter.level;
  529. }
  530. s->segmentation.feat[i].lflvl[0][0] =
  531. s->segmentation.feat[i].lflvl[0][1] =
  532. av_clip_uintp2(lflvl + (s->lf_delta.ref[0] << sh), 6);
  533. for (j = 1; j < 4; j++) {
  534. s->segmentation.feat[i].lflvl[j][0] =
  535. av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
  536. s->lf_delta.mode[0]) << sh), 6);
  537. s->segmentation.feat[i].lflvl[j][1] =
  538. av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
  539. s->lf_delta.mode[1]) << sh), 6);
  540. }
  541. }
  542. /* tiling info */
  543. if ((res = update_size(ctx, w, h)) < 0) {
  544. av_log(ctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d\n", w, h);
  545. return res;
  546. }
  547. for (s->tiling.log2_tile_cols = 0;
  548. (s->sb_cols >> s->tiling.log2_tile_cols) > 64;
  549. s->tiling.log2_tile_cols++) ;
  550. for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
  551. max = FFMAX(0, max - 1);
  552. while (max > s->tiling.log2_tile_cols) {
  553. if (get_bits1(&s->gb))
  554. s->tiling.log2_tile_cols++;
  555. else
  556. break;
  557. }
  558. s->tiling.log2_tile_rows = decode012(&s->gb);
  559. s->tiling.tile_rows = 1 << s->tiling.log2_tile_rows;
  560. if (s->tiling.tile_cols != (1 << s->tiling.log2_tile_cols)) {
  561. s->tiling.tile_cols = 1 << s->tiling.log2_tile_cols;
  562. s->c_b = av_fast_realloc(s->c_b, &s->c_b_size,
  563. sizeof(VP56RangeCoder) * s->tiling.tile_cols);
  564. if (!s->c_b) {
  565. av_log(ctx, AV_LOG_ERROR, "Ran out of memory during range coder init\n");
  566. return AVERROR(ENOMEM);
  567. }
  568. }
  569. if (s->keyframe || s->errorres || s->intraonly) {
  570. s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
  571. s->prob_ctx[3].p = vp9_default_probs;
  572. memcpy(s->prob_ctx[0].coef, vp9_default_coef_probs,
  573. sizeof(vp9_default_coef_probs));
  574. memcpy(s->prob_ctx[1].coef, vp9_default_coef_probs,
  575. sizeof(vp9_default_coef_probs));
  576. memcpy(s->prob_ctx[2].coef, vp9_default_coef_probs,
  577. sizeof(vp9_default_coef_probs));
  578. memcpy(s->prob_ctx[3].coef, vp9_default_coef_probs,
  579. sizeof(vp9_default_coef_probs));
  580. }
  581. // next 16 bits is size of the rest of the header (arith-coded)
  582. size2 = get_bits(&s->gb, 16);
  583. data2 = align_get_bits(&s->gb);
  584. if (size2 > size - (data2 - data)) {
  585. av_log(ctx, AV_LOG_ERROR, "Invalid compressed header size\n");
  586. return AVERROR_INVALIDDATA;
  587. }
  588. ff_vp56_init_range_decoder(&s->c, data2, size2);
  589. if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
  590. av_log(ctx, AV_LOG_ERROR, "Marker bit was set\n");
  591. return AVERROR_INVALIDDATA;
  592. }
  593. if (s->keyframe || s->intraonly) {
  594. memset(s->counts.coef, 0, sizeof(s->counts.coef) + sizeof(s->counts.eob));
  595. } else {
  596. memset(&s->counts, 0, sizeof(s->counts));
  597. }
  598. // FIXME is it faster to not copy here, but do it down in the fw updates
  599. // as explicit copies if the fw update is missing (and skip the copy upon
  600. // fw update)?
  601. s->prob.p = s->prob_ctx[c].p;
  602. // txfm updates
  603. if (s->lossless) {
  604. s->txfmmode = TX_4X4;
  605. } else {
  606. s->txfmmode = vp8_rac_get_uint(&s->c, 2);
  607. if (s->txfmmode == 3)
  608. s->txfmmode += vp8_rac_get(&s->c);
  609. if (s->txfmmode == TX_SWITCHABLE) {
  610. for (i = 0; i < 2; i++)
  611. if (vp56_rac_get_prob_branchy(&s->c, 252))
  612. s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
  613. for (i = 0; i < 2; i++)
  614. for (j = 0; j < 2; j++)
  615. if (vp56_rac_get_prob_branchy(&s->c, 252))
  616. s->prob.p.tx16p[i][j] =
  617. update_prob(&s->c, s->prob.p.tx16p[i][j]);
  618. for (i = 0; i < 2; i++)
  619. for (j = 0; j < 3; j++)
  620. if (vp56_rac_get_prob_branchy(&s->c, 252))
  621. s->prob.p.tx32p[i][j] =
  622. update_prob(&s->c, s->prob.p.tx32p[i][j]);
  623. }
  624. }
  625. // coef updates
  626. for (i = 0; i < 4; i++) {
  627. uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
  628. if (vp8_rac_get(&s->c)) {
  629. for (j = 0; j < 2; j++)
  630. for (k = 0; k < 2; k++)
  631. for (l = 0; l < 6; l++)
  632. for (m = 0; m < 6; m++) {
  633. uint8_t *p = s->prob.coef[i][j][k][l][m];
  634. uint8_t *r = ref[j][k][l][m];
  635. if (m >= 3 && l == 0) // dc only has 3 pt
  636. break;
  637. for (n = 0; n < 3; n++) {
  638. if (vp56_rac_get_prob_branchy(&s->c, 252)) {
  639. p[n] = update_prob(&s->c, r[n]);
  640. } else {
  641. p[n] = r[n];
  642. }
  643. }
  644. p[3] = 0;
  645. }
  646. } else {
  647. for (j = 0; j < 2; j++)
  648. for (k = 0; k < 2; k++)
  649. for (l = 0; l < 6; l++)
  650. for (m = 0; m < 6; m++) {
  651. uint8_t *p = s->prob.coef[i][j][k][l][m];
  652. uint8_t *r = ref[j][k][l][m];
  653. if (m > 3 && l == 0) // dc only has 3 pt
  654. break;
  655. memcpy(p, r, 3);
  656. p[3] = 0;
  657. }
  658. }
  659. if (s->txfmmode == i)
  660. break;
  661. }
  662. // mode updates
  663. for (i = 0; i < 3; i++)
  664. if (vp56_rac_get_prob_branchy(&s->c, 252))
  665. s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
  666. if (!s->keyframe && !s->intraonly) {
  667. for (i = 0; i < 7; i++)
  668. for (j = 0; j < 3; j++)
  669. if (vp56_rac_get_prob_branchy(&s->c, 252))
  670. s->prob.p.mv_mode[i][j] =
  671. update_prob(&s->c, s->prob.p.mv_mode[i][j]);
  672. if (s->filtermode == FILTER_SWITCHABLE)
  673. for (i = 0; i < 4; i++)
  674. for (j = 0; j < 2; j++)
  675. if (vp56_rac_get_prob_branchy(&s->c, 252))
  676. s->prob.p.filter[i][j] =
  677. update_prob(&s->c, s->prob.p.filter[i][j]);
  678. for (i = 0; i < 4; i++)
  679. if (vp56_rac_get_prob_branchy(&s->c, 252))
  680. s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
  681. if (s->allowcompinter) {
  682. s->comppredmode = vp8_rac_get(&s->c);
  683. if (s->comppredmode)
  684. s->comppredmode += vp8_rac_get(&s->c);
  685. if (s->comppredmode == PRED_SWITCHABLE)
  686. for (i = 0; i < 5; i++)
  687. if (vp56_rac_get_prob_branchy(&s->c, 252))
  688. s->prob.p.comp[i] =
  689. update_prob(&s->c, s->prob.p.comp[i]);
  690. } else {
  691. s->comppredmode = PRED_SINGLEREF;
  692. }
  693. if (s->comppredmode != PRED_COMPREF) {
  694. for (i = 0; i < 5; i++) {
  695. if (vp56_rac_get_prob_branchy(&s->c, 252))
  696. s->prob.p.single_ref[i][0] =
  697. update_prob(&s->c, s->prob.p.single_ref[i][0]);
  698. if (vp56_rac_get_prob_branchy(&s->c, 252))
  699. s->prob.p.single_ref[i][1] =
  700. update_prob(&s->c, s->prob.p.single_ref[i][1]);
  701. }
  702. }
  703. if (s->comppredmode != PRED_SINGLEREF) {
  704. for (i = 0; i < 5; i++)
  705. if (vp56_rac_get_prob_branchy(&s->c, 252))
  706. s->prob.p.comp_ref[i] =
  707. update_prob(&s->c, s->prob.p.comp_ref[i]);
  708. }
  709. for (i = 0; i < 4; i++)
  710. for (j = 0; j < 9; j++)
  711. if (vp56_rac_get_prob_branchy(&s->c, 252))
  712. s->prob.p.y_mode[i][j] =
  713. update_prob(&s->c, s->prob.p.y_mode[i][j]);
  714. for (i = 0; i < 4; i++)
  715. for (j = 0; j < 4; j++)
  716. for (k = 0; k < 3; k++)
  717. if (vp56_rac_get_prob_branchy(&s->c, 252))
  718. s->prob.p.partition[3 - i][j][k] =
  719. update_prob(&s->c, s->prob.p.partition[3 - i][j][k]);
  720. // mv fields don't use the update_prob subexp model for some reason
  721. for (i = 0; i < 3; i++)
  722. if (vp56_rac_get_prob_branchy(&s->c, 252))
  723. s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  724. for (i = 0; i < 2; i++) {
  725. if (vp56_rac_get_prob_branchy(&s->c, 252))
  726. s->prob.p.mv_comp[i].sign = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  727. for (j = 0; j < 10; j++)
  728. if (vp56_rac_get_prob_branchy(&s->c, 252))
  729. s->prob.p.mv_comp[i].classes[j] =
  730. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  731. if (vp56_rac_get_prob_branchy(&s->c, 252))
  732. s->prob.p.mv_comp[i].class0 = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  733. for (j = 0; j < 10; j++)
  734. if (vp56_rac_get_prob_branchy(&s->c, 252))
  735. s->prob.p.mv_comp[i].bits[j] =
  736. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  737. }
  738. for (i = 0; i < 2; i++) {
  739. for (j = 0; j < 2; j++)
  740. for (k = 0; k < 3; k++)
  741. if (vp56_rac_get_prob_branchy(&s->c, 252))
  742. s->prob.p.mv_comp[i].class0_fp[j][k] =
  743. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  744. for (j = 0; j < 3; j++)
  745. if (vp56_rac_get_prob_branchy(&s->c, 252))
  746. s->prob.p.mv_comp[i].fp[j] =
  747. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  748. }
  749. if (s->highprecisionmvs) {
  750. for (i = 0; i < 2; i++) {
  751. if (vp56_rac_get_prob_branchy(&s->c, 252))
  752. s->prob.p.mv_comp[i].class0_hp =
  753. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  754. if (vp56_rac_get_prob_branchy(&s->c, 252))
  755. s->prob.p.mv_comp[i].hp =
  756. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  757. }
  758. }
  759. }
  760. return (data2 - data) + size2;
  761. }
  762. static av_always_inline void clamp_mv(VP56mv *dst, const VP56mv *src,
  763. VP9Context *s)
  764. {
  765. dst->x = av_clip(src->x, s->min_mv.x, s->max_mv.x);
  766. dst->y = av_clip(src->y, s->min_mv.y, s->max_mv.y);
  767. }
  768. static void find_ref_mvs(VP9Context *s,
  769. VP56mv *pmv, int ref, int z, int idx, int sb)
  770. {
  771. static const int8_t mv_ref_blk_off[N_BS_SIZES][8][2] = {
  772. [BS_64x64] = {{ 3, -1 }, { -1, 3 }, { 4, -1 }, { -1, 4 },
  773. { -1, -1 }, { 0, -1 }, { -1, 0 }, { 6, -1 }},
  774. [BS_64x32] = {{ 0, -1 }, { -1, 0 }, { 4, -1 }, { -1, 2 },
  775. { -1, -1 }, { 0, -3 }, { -3, 0 }, { 2, -1 }},
  776. [BS_32x64] = {{ -1, 0 }, { 0, -1 }, { -1, 4 }, { 2, -1 },
  777. { -1, -1 }, { -3, 0 }, { 0, -3 }, { -1, 2 }},
  778. [BS_32x32] = {{ 1, -1 }, { -1, 1 }, { 2, -1 }, { -1, 2 },
  779. { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 }},
  780. [BS_32x16] = {{ 0, -1 }, { -1, 0 }, { 2, -1 }, { -1, -1 },
  781. { -1, 1 }, { 0, -3 }, { -3, 0 }, { -3, -3 }},
  782. [BS_16x32] = {{ -1, 0 }, { 0, -1 }, { -1, 2 }, { -1, -1 },
  783. { 1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 }},
  784. [BS_16x16] = {{ 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, 1 },
  785. { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 }},
  786. [BS_16x8] = {{ 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, -1 },
  787. { 0, -2 }, { -2, 0 }, { -2, -1 }, { -1, -2 }},
  788. [BS_8x16] = {{ -1, 0 }, { 0, -1 }, { -1, 1 }, { -1, -1 },
  789. { -2, 0 }, { 0, -2 }, { -1, -2 }, { -2, -1 }},
  790. [BS_8x8] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  791. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
  792. [BS_8x4] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  793. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
  794. [BS_4x8] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  795. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
  796. [BS_4x4] = {{ 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  797. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
  798. };
  799. VP9Block *const b = &s->b;
  800. int row = b->row, col = b->col, row7 = b->row7;
  801. const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
  802. #define INVALID_MV 0x80008000U
  803. uint32_t mem = INVALID_MV;
  804. int i;
  805. #define RETURN_DIRECT_MV(mv) \
  806. do { \
  807. uint32_t m = AV_RN32A(&mv); \
  808. if (!idx) { \
  809. AV_WN32A(pmv, m); \
  810. return; \
  811. } else if (mem == INVALID_MV) { \
  812. mem = m; \
  813. } else if (m != mem) { \
  814. AV_WN32A(pmv, m); \
  815. return; \
  816. } \
  817. } while (0)
  818. if (sb >= 0) {
  819. if (sb == 2 || sb == 1) {
  820. RETURN_DIRECT_MV(b->mv[0][z]);
  821. } else if (sb == 3) {
  822. RETURN_DIRECT_MV(b->mv[2][z]);
  823. RETURN_DIRECT_MV(b->mv[1][z]);
  824. RETURN_DIRECT_MV(b->mv[0][z]);
  825. }
  826. #define RETURN_MV(mv) \
  827. do { \
  828. if (sb > 0) { \
  829. VP56mv tmp; \
  830. uint32_t m; \
  831. clamp_mv(&tmp, &mv, s); \
  832. m = AV_RN32A(&tmp); \
  833. if (!idx) { \
  834. AV_WN32A(pmv, m); \
  835. return; \
  836. } else if (mem == INVALID_MV) { \
  837. mem = m; \
  838. } else if (m != mem) { \
  839. AV_WN32A(pmv, m); \
  840. return; \
  841. } \
  842. } else { \
  843. uint32_t m = AV_RN32A(&mv); \
  844. if (!idx) { \
  845. clamp_mv(pmv, &mv, s); \
  846. return; \
  847. } else if (mem == INVALID_MV) { \
  848. mem = m; \
  849. } else if (m != mem) { \
  850. clamp_mv(pmv, &mv, s); \
  851. return; \
  852. } \
  853. } \
  854. } while (0)
  855. if (row > 0) {
  856. struct VP9mvrefPair *mv = &s->mv[0][(row - 1) * s->sb_cols * 8 + col];
  857. if (mv->ref[0] == ref) {
  858. RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
  859. } else if (mv->ref[1] == ref) {
  860. RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
  861. }
  862. }
  863. if (col > s->tiling.tile_col_start) {
  864. struct VP9mvrefPair *mv = &s->mv[0][row * s->sb_cols * 8 + col - 1];
  865. if (mv->ref[0] == ref) {
  866. RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
  867. } else if (mv->ref[1] == ref) {
  868. RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
  869. }
  870. }
  871. i = 2;
  872. } else {
  873. i = 0;
  874. }
  875. // previously coded MVs in this neighbourhood, using same reference frame
  876. for (; i < 8; i++) {
  877. int c = p[i][0] + col, r = p[i][1] + row;
  878. if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
  879. struct VP9mvrefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c];
  880. if (mv->ref[0] == ref) {
  881. RETURN_MV(mv->mv[0]);
  882. } else if (mv->ref[1] == ref) {
  883. RETURN_MV(mv->mv[1]);
  884. }
  885. }
  886. }
  887. // MV at this position in previous frame, using same reference frame
  888. if (s->use_last_frame_mvs) {
  889. struct VP9mvrefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
  890. if (mv->ref[0] == ref) {
  891. RETURN_MV(mv->mv[0]);
  892. } else if (mv->ref[1] == ref) {
  893. RETURN_MV(mv->mv[1]);
  894. }
  895. }
  896. #define RETURN_SCALE_MV(mv, scale) \
  897. do { \
  898. if (scale) { \
  899. VP56mv mv_temp = { -mv.x, -mv.y }; \
  900. RETURN_MV(mv_temp); \
  901. } else { \
  902. RETURN_MV(mv); \
  903. } \
  904. } while (0)
  905. // previously coded MVs in this neighbourhood, using different reference frame
  906. for (i = 0; i < 8; i++) {
  907. int c = p[i][0] + col, r = p[i][1] + row;
  908. if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
  909. struct VP9mvrefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c];
  910. if (mv->ref[0] != ref && mv->ref[0] >= 0) {
  911. RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
  912. }
  913. if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
  914. // BUG - libvpx has this condition regardless of whether
  915. // we used the first ref MV and pre-scaling
  916. AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
  917. RETURN_SCALE_MV(mv->mv[1], s->signbias[mv->ref[1]] != s->signbias[ref]);
  918. }
  919. }
  920. }
  921. // MV at this position in previous frame, using different reference frame
  922. if (s->use_last_frame_mvs) {
  923. struct VP9mvrefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
  924. if (mv->ref[0] != ref && mv->ref[0] >= 0) {
  925. RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
  926. }
  927. if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
  928. // BUG - libvpx has this condition regardless of whether
  929. // we used the first ref MV and pre-scaling
  930. AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
  931. RETURN_SCALE_MV(mv->mv[1], s->signbias[mv->ref[1]] != s->signbias[ref]);
  932. }
  933. }
  934. AV_ZERO32(pmv);
  935. #undef INVALID_MV
  936. #undef RETURN_MV
  937. #undef RETURN_SCALE_MV
  938. }
  939. static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
  940. {
  941. int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
  942. int n, c = vp8_rac_get_tree(&s->c, vp9_mv_class_tree,
  943. s->prob.p.mv_comp[idx].classes);
  944. s->counts.mv_comp[idx].sign[sign]++;
  945. s->counts.mv_comp[idx].classes[c]++;
  946. if (c) {
  947. int m;
  948. for (n = 0, m = 0; m < c; m++) {
  949. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
  950. n |= bit << m;
  951. s->counts.mv_comp[idx].bits[m][bit]++;
  952. }
  953. n <<= 3;
  954. bit = vp8_rac_get_tree(&s->c, vp9_mv_fp_tree, s->prob.p.mv_comp[idx].fp);
  955. n |= bit << 1;
  956. s->counts.mv_comp[idx].fp[bit]++;
  957. if (hp) {
  958. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
  959. s->counts.mv_comp[idx].hp[bit]++;
  960. n |= bit;
  961. } else {
  962. n |= 1;
  963. // bug in libvpx - we count for bw entropy purposes even if the
  964. // bit wasn't coded
  965. s->counts.mv_comp[idx].hp[1]++;
  966. }
  967. n += 8 << c;
  968. } else {
  969. n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
  970. s->counts.mv_comp[idx].class0[n]++;
  971. bit = vp8_rac_get_tree(&s->c, vp9_mv_fp_tree,
  972. s->prob.p.mv_comp[idx].class0_fp[n]);
  973. s->counts.mv_comp[idx].class0_fp[n][bit]++;
  974. n = (n << 3) | (bit << 1);
  975. if (hp) {
  976. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
  977. s->counts.mv_comp[idx].class0_hp[bit]++;
  978. n |= bit;
  979. } else {
  980. n |= 1;
  981. // bug in libvpx - we count for bw entropy purposes even if the
  982. // bit wasn't coded
  983. s->counts.mv_comp[idx].class0_hp[1]++;
  984. }
  985. }
  986. return sign ? -(n + 1) : (n + 1);
  987. }
  988. static void fill_mv(VP9Context *s,
  989. VP56mv *mv, int mode, int sb)
  990. {
  991. VP9Block *const b = &s->b;
  992. if (mode == ZEROMV) {
  993. memset(mv, 0, sizeof(*mv) * 2);
  994. } else {
  995. int hp;
  996. // FIXME cache this value and reuse for other subblocks
  997. find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
  998. mode == NEWMV ? -1 : sb);
  999. // FIXME maybe move this code into find_ref_mvs()
  1000. if ((mode == NEWMV || sb == -1) &&
  1001. !(hp = s->highprecisionmvs && abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
  1002. if (mv[0].y & 1) {
  1003. if (mv[0].y < 0)
  1004. mv[0].y++;
  1005. else
  1006. mv[0].y--;
  1007. }
  1008. if (mv[0].x & 1) {
  1009. if (mv[0].x < 0)
  1010. mv[0].x++;
  1011. else
  1012. mv[0].x--;
  1013. }
  1014. }
  1015. if (mode == NEWMV) {
  1016. enum MVJoint j = vp8_rac_get_tree(&s->c, vp9_mv_joint_tree,
  1017. s->prob.p.mv_joint);
  1018. s->counts.mv_joint[j]++;
  1019. if (j >= MV_JOINT_V)
  1020. mv[0].y += read_mv_component(s, 0, hp);
  1021. if (j & 1)
  1022. mv[0].x += read_mv_component(s, 1, hp);
  1023. }
  1024. if (b->comp) {
  1025. // FIXME cache this value and reuse for other subblocks
  1026. find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
  1027. mode == NEWMV ? -1 : sb);
  1028. if ((mode == NEWMV || sb == -1) &&
  1029. !(hp = s->highprecisionmvs && abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
  1030. if (mv[1].y & 1) {
  1031. if (mv[1].y < 0)
  1032. mv[1].y++;
  1033. else
  1034. mv[1].y--;
  1035. }
  1036. if (mv[1].x & 1) {
  1037. if (mv[1].x < 0)
  1038. mv[1].x++;
  1039. else
  1040. mv[1].x--;
  1041. }
  1042. }
  1043. if (mode == NEWMV) {
  1044. enum MVJoint j = vp8_rac_get_tree(&s->c, vp9_mv_joint_tree,
  1045. s->prob.p.mv_joint);
  1046. s->counts.mv_joint[j]++;
  1047. if (j >= MV_JOINT_V)
  1048. mv[1].y += read_mv_component(s, 0, hp);
  1049. if (j & 1)
  1050. mv[1].x += read_mv_component(s, 1, hp);
  1051. }
  1052. }
  1053. }
  1054. }
  1055. static void decode_mode(AVCodecContext *ctx)
  1056. {
  1057. static const uint8_t left_ctx[N_BS_SIZES] = {
  1058. 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
  1059. };
  1060. static const uint8_t above_ctx[N_BS_SIZES] = {
  1061. 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
  1062. };
  1063. static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
  1064. TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
  1065. TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
  1066. };
  1067. VP9Context *s = ctx->priv_data;
  1068. VP9Block *const b = &s->b;
  1069. int row = b->row, col = b->col, row7 = b->row7;
  1070. enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
  1071. int w4 = FFMIN(s->cols - col, bwh_tab[1][b->bs][0]);
  1072. int h4 = FFMIN(s->rows - row, bwh_tab[1][b->bs][1]), y;
  1073. int have_a = row > 0, have_l = col > s->tiling.tile_col_start;
  1074. if (!s->segmentation.enabled) {
  1075. b->seg_id = 0;
  1076. } else if (s->keyframe || s->intraonly) {
  1077. b->seg_id = s->segmentation.update_map ?
  1078. vp8_rac_get_tree(&s->c, vp9_segmentation_tree, s->prob.seg) : 0;
  1079. } else if (!s->segmentation.update_map ||
  1080. (s->segmentation.temporal &&
  1081. vp56_rac_get_prob_branchy(&s->c,
  1082. s->prob.segpred[s->above_segpred_ctx[col] +
  1083. s->left_segpred_ctx[row7]]))) {
  1084. int pred = 8, x;
  1085. for (y = 0; y < h4; y++)
  1086. for (x = 0; x < w4; x++)
  1087. pred = FFMIN(pred, s->segmentation_map[(y + row) * 8 * s->sb_cols + x + col]);
  1088. av_assert1(pred < 8);
  1089. b->seg_id = pred;
  1090. memset(&s->above_segpred_ctx[col], 1, w4);
  1091. memset(&s->left_segpred_ctx[row7], 1, h4);
  1092. } else {
  1093. b->seg_id = vp8_rac_get_tree(&s->c, vp9_segmentation_tree,
  1094. s->prob.seg);
  1095. memset(&s->above_segpred_ctx[col], 0, w4);
  1096. memset(&s->left_segpred_ctx[row7], 0, h4);
  1097. }
  1098. if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
  1099. for (y = 0; y < h4; y++)
  1100. memset(&s->segmentation_map[(y + row) * 8 * s->sb_cols + col],
  1101. b->seg_id, w4);
  1102. }
  1103. b->skip = s->segmentation.enabled &&
  1104. s->segmentation.feat[b->seg_id].skip_enabled;
  1105. if (!b->skip) {
  1106. int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
  1107. b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
  1108. s->counts.skip[c][b->skip]++;
  1109. }
  1110. if (s->keyframe || s->intraonly) {
  1111. b->intra = 1;
  1112. } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
  1113. b->intra = !s->segmentation.feat[b->seg_id].ref_val;
  1114. } else {
  1115. int c, bit;
  1116. if (have_a && have_l) {
  1117. c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
  1118. c += (c == 2);
  1119. } else {
  1120. c = have_a ? 2 * s->above_intra_ctx[col] :
  1121. have_l ? 2 * s->left_intra_ctx[row7] : 0;
  1122. }
  1123. bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
  1124. s->counts.intra[c][bit]++;
  1125. b->intra = !bit;
  1126. }
  1127. if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
  1128. int c;
  1129. if (have_a) {
  1130. if (have_l) {
  1131. c = (s->above_skip_ctx[col] ? max_tx :
  1132. s->above_txfm_ctx[col]) +
  1133. (s->left_skip_ctx[row7] ? max_tx :
  1134. s->left_txfm_ctx[row7]) > max_tx;
  1135. } else {
  1136. c = s->above_skip_ctx[col] ? 1 :
  1137. (s->above_txfm_ctx[col] * 2 > max_tx);
  1138. }
  1139. } else if (have_l) {
  1140. c = s->left_skip_ctx[row7] ? 1 :
  1141. (s->left_txfm_ctx[row7] * 2 > max_tx);
  1142. } else {
  1143. c = 1;
  1144. }
  1145. switch (max_tx) {
  1146. case TX_32X32:
  1147. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
  1148. if (b->tx) {
  1149. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
  1150. if (b->tx == 2)
  1151. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
  1152. }
  1153. s->counts.tx32p[c][b->tx]++;
  1154. break;
  1155. case TX_16X16:
  1156. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
  1157. if (b->tx)
  1158. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
  1159. s->counts.tx16p[c][b->tx]++;
  1160. break;
  1161. case TX_8X8:
  1162. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
  1163. s->counts.tx8p[c][b->tx]++;
  1164. break;
  1165. case TX_4X4:
  1166. b->tx = TX_4X4;
  1167. break;
  1168. }
  1169. } else {
  1170. b->tx = FFMIN(max_tx, s->txfmmode);
  1171. }
  1172. if (s->keyframe || s->intraonly) {
  1173. uint8_t *a = &s->above_mode_ctx[col * 2];
  1174. uint8_t *l = &s->left_mode_ctx[(row7) << 1];
  1175. b->comp = 0;
  1176. if (b->bs > BS_8x8) {
  1177. // FIXME the memory storage intermediates here aren't really
  1178. // necessary, they're just there to make the code slightly
  1179. // simpler for now
  1180. b->mode[0] = a[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1181. vp9_default_kf_ymode_probs[a[0]][l[0]]);
  1182. if (b->bs != BS_8x4) {
  1183. b->mode[1] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1184. vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
  1185. l[0] = a[1] = b->mode[1];
  1186. } else {
  1187. l[0] = a[1] = b->mode[1] = b->mode[0];
  1188. }
  1189. if (b->bs != BS_4x8) {
  1190. b->mode[2] = a[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1191. vp9_default_kf_ymode_probs[a[0]][l[1]]);
  1192. if (b->bs != BS_8x4) {
  1193. b->mode[3] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1194. vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
  1195. l[1] = a[1] = b->mode[3];
  1196. } else {
  1197. l[1] = a[1] = b->mode[3] = b->mode[2];
  1198. }
  1199. } else {
  1200. b->mode[2] = b->mode[0];
  1201. l[1] = a[1] = b->mode[3] = b->mode[1];
  1202. }
  1203. } else {
  1204. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1205. vp9_default_kf_ymode_probs[*a][*l]);
  1206. b->mode[3] = b->mode[2] = b->mode[1] = b->mode[0];
  1207. // FIXME this can probably be optimized
  1208. memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
  1209. memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
  1210. }
  1211. b->uvmode = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1212. vp9_default_kf_uvmode_probs[b->mode[3]]);
  1213. } else if (b->intra) {
  1214. b->comp = 0;
  1215. if (b->bs > BS_8x8) {
  1216. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1217. s->prob.p.y_mode[0]);
  1218. s->counts.y_mode[0][b->mode[0]]++;
  1219. if (b->bs != BS_8x4) {
  1220. b->mode[1] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1221. s->prob.p.y_mode[0]);
  1222. s->counts.y_mode[0][b->mode[1]]++;
  1223. } else {
  1224. b->mode[1] = b->mode[0];
  1225. }
  1226. if (b->bs != BS_4x8) {
  1227. b->mode[2] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1228. s->prob.p.y_mode[0]);
  1229. s->counts.y_mode[0][b->mode[2]]++;
  1230. if (b->bs != BS_8x4) {
  1231. b->mode[3] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1232. s->prob.p.y_mode[0]);
  1233. s->counts.y_mode[0][b->mode[3]]++;
  1234. } else {
  1235. b->mode[3] = b->mode[2];
  1236. }
  1237. } else {
  1238. b->mode[2] = b->mode[0];
  1239. b->mode[3] = b->mode[1];
  1240. }
  1241. } else {
  1242. static const uint8_t size_group[10] = {
  1243. 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
  1244. };
  1245. int sz = size_group[b->bs];
  1246. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1247. s->prob.p.y_mode[sz]);
  1248. b->mode[1] = b->mode[2] = b->mode[3] = b->mode[0];
  1249. s->counts.y_mode[sz][b->mode[3]]++;
  1250. }
  1251. b->uvmode = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1252. s->prob.p.uv_mode[b->mode[3]]);
  1253. s->counts.uv_mode[b->mode[3]][b->uvmode]++;
  1254. } else {
  1255. static const uint8_t inter_mode_ctx_lut[14][14] = {
  1256. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1257. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1258. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1259. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1260. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1261. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1262. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1263. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1264. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1265. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1266. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  1267. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  1268. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
  1269. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
  1270. };
  1271. if (s->segmentation.feat[b->seg_id].ref_enabled) {
  1272. av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
  1273. b->comp = 0;
  1274. b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
  1275. } else {
  1276. // read comp_pred flag
  1277. if (s->comppredmode != PRED_SWITCHABLE) {
  1278. b->comp = s->comppredmode == PRED_COMPREF;
  1279. } else {
  1280. int c;
  1281. // FIXME add intra as ref=0xff (or -1) to make these easier?
  1282. if (have_a) {
  1283. if (have_l) {
  1284. if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
  1285. c = 4;
  1286. } else if (s->above_comp_ctx[col]) {
  1287. c = 2 + (s->left_intra_ctx[row7] ||
  1288. s->left_ref_ctx[row7] == s->fixcompref);
  1289. } else if (s->left_comp_ctx[row7]) {
  1290. c = 2 + (s->above_intra_ctx[col] ||
  1291. s->above_ref_ctx[col] == s->fixcompref);
  1292. } else {
  1293. c = (!s->above_intra_ctx[col] &&
  1294. s->above_ref_ctx[col] == s->fixcompref) ^
  1295. (!s->left_intra_ctx[row7] &&
  1296. s->left_ref_ctx[row & 7] == s->fixcompref);
  1297. }
  1298. } else {
  1299. c = s->above_comp_ctx[col] ? 3 :
  1300. (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
  1301. }
  1302. } else if (have_l) {
  1303. c = s->left_comp_ctx[row7] ? 3 :
  1304. (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
  1305. } else {
  1306. c = 1;
  1307. }
  1308. b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
  1309. s->counts.comp[c][b->comp]++;
  1310. }
  1311. // read actual references
  1312. // FIXME probably cache a few variables here to prevent repetitive
  1313. // memory accesses below
  1314. if (b->comp) /* two references */ {
  1315. int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
  1316. b->ref[fix_idx] = s->fixcompref;
  1317. // FIXME can this codeblob be replaced by some sort of LUT?
  1318. if (have_a) {
  1319. if (have_l) {
  1320. if (s->above_intra_ctx[col]) {
  1321. if (s->left_intra_ctx[row7]) {
  1322. c = 2;
  1323. } else {
  1324. c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  1325. }
  1326. } else if (s->left_intra_ctx[row7]) {
  1327. c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
  1328. } else {
  1329. int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
  1330. if (refl == refa && refa == s->varcompref[1]) {
  1331. c = 0;
  1332. } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
  1333. if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
  1334. (refl == s->fixcompref && refa == s->varcompref[0])) {
  1335. c = 4;
  1336. } else {
  1337. c = (refa == refl) ? 3 : 1;
  1338. }
  1339. } else if (!s->left_comp_ctx[row7]) {
  1340. if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
  1341. c = 1;
  1342. } else {
  1343. c = (refl == s->varcompref[1] &&
  1344. refa != s->varcompref[1]) ? 2 : 4;
  1345. }
  1346. } else if (!s->above_comp_ctx[col]) {
  1347. if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
  1348. c = 1;
  1349. } else {
  1350. c = (refa == s->varcompref[1] &&
  1351. refl != s->varcompref[1]) ? 2 : 4;
  1352. }
  1353. } else {
  1354. c = (refl == refa) ? 4 : 2;
  1355. }
  1356. }
  1357. } else {
  1358. if (s->above_intra_ctx[col]) {
  1359. c = 2;
  1360. } else if (s->above_comp_ctx[col]) {
  1361. c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
  1362. } else {
  1363. c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
  1364. }
  1365. }
  1366. } else if (have_l) {
  1367. if (s->left_intra_ctx[row7]) {
  1368. c = 2;
  1369. } else if (s->left_comp_ctx[row7]) {
  1370. c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  1371. } else {
  1372. c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  1373. }
  1374. } else {
  1375. c = 2;
  1376. }
  1377. bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
  1378. b->ref[var_idx] = s->varcompref[bit];
  1379. s->counts.comp_ref[c][bit]++;
  1380. } else /* single reference */ {
  1381. int bit, c;
  1382. if (have_a && !s->above_intra_ctx[col]) {
  1383. if (have_l && !s->left_intra_ctx[row7]) {
  1384. if (s->left_comp_ctx[row7]) {
  1385. if (s->above_comp_ctx[col]) {
  1386. c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
  1387. !s->above_ref_ctx[col]);
  1388. } else {
  1389. c = (3 * !s->above_ref_ctx[col]) +
  1390. (!s->fixcompref || !s->left_ref_ctx[row7]);
  1391. }
  1392. } else if (s->above_comp_ctx[col]) {
  1393. c = (3 * !s->left_ref_ctx[row7]) +
  1394. (!s->fixcompref || !s->above_ref_ctx[col]);
  1395. } else {
  1396. c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
  1397. }
  1398. } else if (s->above_intra_ctx[col]) {
  1399. c = 2;
  1400. } else if (s->above_comp_ctx[col]) {
  1401. c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
  1402. } else {
  1403. c = 4 * (!s->above_ref_ctx[col]);
  1404. }
  1405. } else if (have_l && !s->left_intra_ctx[row7]) {
  1406. if (s->left_intra_ctx[row7]) {
  1407. c = 2;
  1408. } else if (s->left_comp_ctx[row7]) {
  1409. c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
  1410. } else {
  1411. c = 4 * (!s->left_ref_ctx[row7]);
  1412. }
  1413. } else {
  1414. c = 2;
  1415. }
  1416. bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
  1417. s->counts.single_ref[c][0][bit]++;
  1418. if (!bit) {
  1419. b->ref[0] = 0;
  1420. } else {
  1421. // FIXME can this codeblob be replaced by some sort of LUT?
  1422. if (have_a) {
  1423. if (have_l) {
  1424. if (s->left_intra_ctx[row7]) {
  1425. if (s->above_intra_ctx[col]) {
  1426. c = 2;
  1427. } else if (s->above_comp_ctx[col]) {
  1428. c = 1 + 2 * (s->fixcompref == 1 ||
  1429. s->above_ref_ctx[col] == 1);
  1430. } else if (!s->above_ref_ctx[col]) {
  1431. c = 3;
  1432. } else {
  1433. c = 4 * (s->above_ref_ctx[col] == 1);
  1434. }
  1435. } else if (s->above_intra_ctx[col]) {
  1436. if (s->left_intra_ctx[row7]) {
  1437. c = 2;
  1438. } else if (s->left_comp_ctx[row7]) {
  1439. c = 1 + 2 * (s->fixcompref == 1 ||
  1440. s->left_ref_ctx[row7] == 1);
  1441. } else if (!s->left_ref_ctx[row7]) {
  1442. c = 3;
  1443. } else {
  1444. c = 4 * (s->left_ref_ctx[row7] == 1);
  1445. }
  1446. } else if (s->above_comp_ctx[col]) {
  1447. if (s->left_comp_ctx[row7]) {
  1448. if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
  1449. c = 3 * (s->fixcompref == 1 ||
  1450. s->left_ref_ctx[row7] == 1);
  1451. } else {
  1452. c = 2;
  1453. }
  1454. } else if (!s->left_ref_ctx[row7]) {
  1455. c = 1 + 2 * (s->fixcompref == 1 ||
  1456. s->above_ref_ctx[col] == 1);
  1457. } else {
  1458. c = 3 * (s->left_ref_ctx[row7] == 1) +
  1459. (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
  1460. }
  1461. } else if (s->left_comp_ctx[row7]) {
  1462. if (!s->above_ref_ctx[col]) {
  1463. c = 1 + 2 * (s->fixcompref == 1 ||
  1464. s->left_ref_ctx[row7] == 1);
  1465. } else {
  1466. c = 3 * (s->above_ref_ctx[col] == 1) +
  1467. (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
  1468. }
  1469. } else if (!s->above_ref_ctx[col]) {
  1470. if (!s->left_ref_ctx[row7]) {
  1471. c = 3;
  1472. } else {
  1473. c = 4 * (s->left_ref_ctx[row7] == 1);
  1474. }
  1475. } else if (!s->left_ref_ctx[row7]) {
  1476. c = 4 * (s->above_ref_ctx[col] == 1);
  1477. } else {
  1478. c = 2 * (s->left_ref_ctx[row7] == 1) +
  1479. 2 * (s->above_ref_ctx[col] == 1);
  1480. }
  1481. } else {
  1482. if (s->above_intra_ctx[col] ||
  1483. (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
  1484. c = 2;
  1485. } else if (s->above_comp_ctx[col]) {
  1486. c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
  1487. } else {
  1488. c = 4 * (s->above_ref_ctx[col] == 1);
  1489. }
  1490. }
  1491. } else if (have_l) {
  1492. if (s->left_intra_ctx[row7] ||
  1493. (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
  1494. c = 2;
  1495. } else if (s->left_comp_ctx[row7]) {
  1496. c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
  1497. } else {
  1498. c = 4 * (s->left_ref_ctx[row7] == 1);
  1499. }
  1500. } else {
  1501. c = 2;
  1502. }
  1503. bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
  1504. s->counts.single_ref[c][1][bit]++;
  1505. b->ref[0] = 1 + bit;
  1506. }
  1507. }
  1508. }
  1509. if (b->bs <= BS_8x8) {
  1510. if (s->segmentation.feat[b->seg_id].skip_enabled) {
  1511. b->mode[0] = b->mode[1] = b->mode[2] = b->mode[3] = ZEROMV;
  1512. } else {
  1513. static const uint8_t off[10] = {
  1514. 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
  1515. };
  1516. // FIXME this needs to use the LUT tables from find_ref_mvs
  1517. // because not all are -1,0/0,-1
  1518. int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
  1519. [s->left_mode_ctx[row7 + off[b->bs]]];
  1520. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1521. s->prob.p.mv_mode[c]);
  1522. b->mode[1] = b->mode[2] = b->mode[3] = b->mode[0];
  1523. s->counts.mv_mode[c][b->mode[0] - 10]++;
  1524. }
  1525. }
  1526. if (s->filtermode == FILTER_SWITCHABLE) {
  1527. int c;
  1528. if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
  1529. if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
  1530. c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
  1531. s->left_filter_ctx[row7] : 3;
  1532. } else {
  1533. c = s->above_filter_ctx[col];
  1534. }
  1535. } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
  1536. c = s->left_filter_ctx[row7];
  1537. } else {
  1538. c = 3;
  1539. }
  1540. b->filter = vp8_rac_get_tree(&s->c, vp9_filter_tree,
  1541. s->prob.p.filter[c]);
  1542. s->counts.filter[c][b->filter]++;
  1543. } else {
  1544. b->filter = s->filtermode;
  1545. }
  1546. if (b->bs > BS_8x8) {
  1547. int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
  1548. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1549. s->prob.p.mv_mode[c]);
  1550. s->counts.mv_mode[c][b->mode[0] - 10]++;
  1551. fill_mv(s, b->mv[0], b->mode[0], 0);
  1552. if (b->bs != BS_8x4) {
  1553. b->mode[1] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1554. s->prob.p.mv_mode[c]);
  1555. s->counts.mv_mode[c][b->mode[1] - 10]++;
  1556. fill_mv(s, b->mv[1], b->mode[1], 1);
  1557. } else {
  1558. b->mode[1] = b->mode[0];
  1559. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  1560. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  1561. }
  1562. if (b->bs != BS_4x8) {
  1563. b->mode[2] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1564. s->prob.p.mv_mode[c]);
  1565. s->counts.mv_mode[c][b->mode[2] - 10]++;
  1566. fill_mv(s, b->mv[2], b->mode[2], 2);
  1567. if (b->bs != BS_8x4) {
  1568. b->mode[3] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1569. s->prob.p.mv_mode[c]);
  1570. s->counts.mv_mode[c][b->mode[3] - 10]++;
  1571. fill_mv(s, b->mv[3], b->mode[3], 3);
  1572. } else {
  1573. b->mode[3] = b->mode[2];
  1574. AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
  1575. AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
  1576. }
  1577. } else {
  1578. b->mode[2] = b->mode[0];
  1579. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  1580. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  1581. b->mode[3] = b->mode[1];
  1582. AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
  1583. AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
  1584. }
  1585. } else {
  1586. fill_mv(s, b->mv[0], b->mode[0], -1);
  1587. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  1588. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  1589. AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
  1590. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  1591. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  1592. AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
  1593. }
  1594. }
  1595. // FIXME this can probably be optimized
  1596. memset(&s->above_skip_ctx[col], b->skip, w4);
  1597. memset(&s->left_skip_ctx[row7], b->skip, h4);
  1598. memset(&s->above_txfm_ctx[col], b->tx, w4);
  1599. memset(&s->left_txfm_ctx[row7], b->tx, h4);
  1600. memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
  1601. memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
  1602. if (!s->keyframe && !s->intraonly) {
  1603. memset(&s->above_intra_ctx[col], b->intra, w4);
  1604. memset(&s->left_intra_ctx[row7], b->intra, h4);
  1605. memset(&s->above_comp_ctx[col], b->comp, w4);
  1606. memset(&s->left_comp_ctx[row7], b->comp, h4);
  1607. memset(&s->above_mode_ctx[col], b->mode[3], w4);
  1608. memset(&s->left_mode_ctx[row7], b->mode[3], h4);
  1609. if (s->filtermode == FILTER_SWITCHABLE && !b->intra ) {
  1610. memset(&s->above_filter_ctx[col], b->filter, w4);
  1611. memset(&s->left_filter_ctx[row7], b->filter, h4);
  1612. b->filter = vp9_filter_lut[b->filter];
  1613. }
  1614. if (b->bs > BS_8x8) {
  1615. int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  1616. AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
  1617. AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
  1618. AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
  1619. AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
  1620. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
  1621. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
  1622. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
  1623. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
  1624. } else {
  1625. int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  1626. for (n = 0; n < w4 * 2; n++) {
  1627. AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
  1628. AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
  1629. }
  1630. for (n = 0; n < h4 * 2; n++) {
  1631. AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
  1632. AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
  1633. }
  1634. }
  1635. if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
  1636. // as a direct check in above branches
  1637. int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
  1638. memset(&s->above_ref_ctx[col], vref, w4);
  1639. memset(&s->left_ref_ctx[row7], vref, h4);
  1640. }
  1641. }
  1642. // FIXME kinda ugly
  1643. for (y = 0; y < h4; y++) {
  1644. int x, o = (row + y) * s->sb_cols * 8 + col;
  1645. if (b->intra) {
  1646. for (x = 0; x < w4; x++) {
  1647. s->mv[0][o + x].ref[0] =
  1648. s->mv[0][o + x].ref[1] = -1;
  1649. }
  1650. } else if (b->comp) {
  1651. for (x = 0; x < w4; x++) {
  1652. s->mv[0][o + x].ref[0] = b->ref[0];
  1653. s->mv[0][o + x].ref[1] = b->ref[1];
  1654. AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
  1655. AV_COPY32(&s->mv[0][o + x].mv[1], &b->mv[3][1]);
  1656. }
  1657. } else {
  1658. for (x = 0; x < w4; x++) {
  1659. s->mv[0][o + x].ref[0] = b->ref[0];
  1660. s->mv[0][o + x].ref[1] = -1;
  1661. AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
  1662. }
  1663. }
  1664. }
  1665. }
  1666. // FIXME remove tx argument, and merge cnt/eob arguments?
  1667. static int decode_coeffs_b(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
  1668. enum TxfmMode tx, unsigned (*cnt)[6][3],
  1669. unsigned (*eob)[6][2], uint8_t (*p)[6][11],
  1670. int nnz, const int16_t *scan, const int16_t (*nb)[2],
  1671. const int16_t *band_counts, const int16_t *qmul)
  1672. {
  1673. int i = 0, band = 0, band_left = band_counts[band];
  1674. uint8_t *tp = p[0][nnz];
  1675. uint8_t cache[1024];
  1676. do {
  1677. int val, rc;
  1678. val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
  1679. eob[band][nnz][val]++;
  1680. if (!val)
  1681. break;
  1682. skip_eob:
  1683. if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
  1684. cnt[band][nnz][0]++;
  1685. if (!--band_left)
  1686. band_left = band_counts[++band];
  1687. cache[scan[i]] = 0;
  1688. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  1689. tp = p[band][nnz];
  1690. if (++i == n_coeffs)
  1691. break; //invalid input; blocks should end with EOB
  1692. goto skip_eob;
  1693. }
  1694. rc = scan[i];
  1695. if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
  1696. cnt[band][nnz][1]++;
  1697. val = 1;
  1698. cache[rc] = 1;
  1699. } else {
  1700. // fill in p[3-10] (model fill) - only once per frame for each pos
  1701. if (!tp[3])
  1702. memcpy(&tp[3], vp9_model_pareto8[tp[2]], 8);
  1703. cnt[band][nnz][2]++;
  1704. if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
  1705. if (!vp56_rac_get_prob_branchy(c, tp[4])) {
  1706. cache[rc] = val = 2;
  1707. } else {
  1708. val = 3 + vp56_rac_get_prob(c, tp[5]);
  1709. cache[rc] = 3;
  1710. }
  1711. } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
  1712. cache[rc] = 4;
  1713. if (!vp56_rac_get_prob_branchy(c, tp[7])) {
  1714. val = 5 + vp56_rac_get_prob(c, 159);
  1715. } else {
  1716. val = 7 + (vp56_rac_get_prob(c, 165) << 1);
  1717. val += vp56_rac_get_prob(c, 145);
  1718. }
  1719. } else { // cat 3-6
  1720. cache[rc] = 5;
  1721. if (!vp56_rac_get_prob_branchy(c, tp[8])) {
  1722. if (!vp56_rac_get_prob_branchy(c, tp[9])) {
  1723. val = 11 + (vp56_rac_get_prob(c, 173) << 2);
  1724. val += (vp56_rac_get_prob(c, 148) << 1);
  1725. val += vp56_rac_get_prob(c, 140);
  1726. } else {
  1727. val = 19 + (vp56_rac_get_prob(c, 176) << 3);
  1728. val += (vp56_rac_get_prob(c, 155) << 2);
  1729. val += (vp56_rac_get_prob(c, 140) << 1);
  1730. val += vp56_rac_get_prob(c, 135);
  1731. }
  1732. } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
  1733. val = 35 + (vp56_rac_get_prob(c, 180) << 4);
  1734. val += (vp56_rac_get_prob(c, 157) << 3);
  1735. val += (vp56_rac_get_prob(c, 141) << 2);
  1736. val += (vp56_rac_get_prob(c, 134) << 1);
  1737. val += vp56_rac_get_prob(c, 130);
  1738. } else {
  1739. val = 67 + (vp56_rac_get_prob(c, 254) << 13);
  1740. val += (vp56_rac_get_prob(c, 254) << 12);
  1741. val += (vp56_rac_get_prob(c, 254) << 11);
  1742. val += (vp56_rac_get_prob(c, 252) << 10);
  1743. val += (vp56_rac_get_prob(c, 249) << 9);
  1744. val += (vp56_rac_get_prob(c, 243) << 8);
  1745. val += (vp56_rac_get_prob(c, 230) << 7);
  1746. val += (vp56_rac_get_prob(c, 196) << 6);
  1747. val += (vp56_rac_get_prob(c, 177) << 5);
  1748. val += (vp56_rac_get_prob(c, 153) << 4);
  1749. val += (vp56_rac_get_prob(c, 140) << 3);
  1750. val += (vp56_rac_get_prob(c, 133) << 2);
  1751. val += (vp56_rac_get_prob(c, 130) << 1);
  1752. val += vp56_rac_get_prob(c, 129);
  1753. }
  1754. }
  1755. }
  1756. if (!--band_left)
  1757. band_left = band_counts[++band];
  1758. if (tx == TX_32X32) // FIXME slow
  1759. coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
  1760. else
  1761. coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
  1762. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  1763. tp = p[band][nnz];
  1764. } while (++i < n_coeffs);
  1765. return i;
  1766. }
  1767. static int decode_coeffs(AVCodecContext *ctx)
  1768. {
  1769. VP9Context *s = ctx->priv_data;
  1770. VP9Block *const b = &s->b;
  1771. int row = b->row, col = b->col;
  1772. uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
  1773. unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
  1774. unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
  1775. int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
  1776. int end_x = FFMIN(2 * (s->cols - col), w4);
  1777. int end_y = FFMIN(2 * (s->rows - row), h4);
  1778. int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
  1779. int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), res;
  1780. int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
  1781. int tx = 4 * s->lossless + b->tx;
  1782. const int16_t * const *yscans = vp9_scans[tx];
  1783. const int16_t (* const *ynbs)[2] = vp9_scans_nb[tx];
  1784. const int16_t *uvscan = vp9_scans[b->uvtx][DCT_DCT];
  1785. const int16_t (*uvnb)[2] = vp9_scans_nb[b->uvtx][DCT_DCT];
  1786. uint8_t *a = &s->above_y_nnz_ctx[col * 2];
  1787. uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
  1788. static const int16_t band_counts[4][8] = {
  1789. { 1, 2, 3, 4, 3, 16 - 13 },
  1790. { 1, 2, 3, 4, 11, 64 - 21 },
  1791. { 1, 2, 3, 4, 11, 256 - 21 },
  1792. { 1, 2, 3, 4, 11, 1024 - 21 },
  1793. };
  1794. const int16_t *y_band_counts = band_counts[b->tx];
  1795. const int16_t *uv_band_counts = band_counts[b->uvtx];
  1796. /* y tokens */
  1797. if (b->tx > TX_4X4) { // FIXME slow
  1798. for (y = 0; y < end_y; y += step1d)
  1799. for (x = 1; x < step1d; x++)
  1800. l[y] |= l[y + x];
  1801. for (x = 0; x < end_x; x += step1d)
  1802. for (y = 1; y < step1d; y++)
  1803. a[x] |= a[x + y];
  1804. }
  1805. for (n = 0, y = 0; y < end_y; y += step1d) {
  1806. for (x = 0; x < end_x; x += step1d, n += step) {
  1807. enum TxfmType txtp = vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
  1808. b->bs > BS_8x8 ?
  1809. n : 0]];
  1810. int nnz = a[x] + l[y];
  1811. if ((res = decode_coeffs_b(&s->c, s->block + 16 * n, 16 * step,
  1812. b->tx, c, e, p, nnz, yscans[txtp],
  1813. ynbs[txtp], y_band_counts, qmul[0])) < 0)
  1814. return res;
  1815. a[x] = l[y] = !!res;
  1816. if (b->tx > TX_8X8) {
  1817. AV_WN16A(&s->eob[n], res);
  1818. } else {
  1819. s->eob[n] = res;
  1820. }
  1821. }
  1822. }
  1823. if (b->tx > TX_4X4) { // FIXME slow
  1824. for (y = 0; y < end_y; y += step1d)
  1825. memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
  1826. for (x = 0; x < end_x; x += step1d)
  1827. memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
  1828. }
  1829. p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
  1830. c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
  1831. e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
  1832. w4 >>= 1;
  1833. h4 >>= 1;
  1834. end_x >>= 1;
  1835. end_y >>= 1;
  1836. for (pl = 0; pl < 2; pl++) {
  1837. a = &s->above_uv_nnz_ctx[pl][col];
  1838. l = &s->left_uv_nnz_ctx[pl][row & 7];
  1839. if (b->uvtx > TX_4X4) { // FIXME slow
  1840. for (y = 0; y < end_y; y += uvstep1d)
  1841. for (x = 1; x < uvstep1d; x++)
  1842. l[y] |= l[y + x];
  1843. for (x = 0; x < end_x; x += uvstep1d)
  1844. for (y = 1; y < uvstep1d; y++)
  1845. a[x] |= a[x + y];
  1846. }
  1847. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  1848. for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
  1849. int nnz = a[x] + l[y];
  1850. if ((res = decode_coeffs_b(&s->c, s->uvblock[pl] + 16 * n,
  1851. 16 * uvstep, b->uvtx, c, e, p, nnz,
  1852. uvscan, uvnb, uv_band_counts,
  1853. qmul[1])) < 0)
  1854. return res;
  1855. a[x] = l[y] = !!res;
  1856. if (b->uvtx > TX_8X8) {
  1857. AV_WN16A(&s->uveob[pl][n], res);
  1858. } else {
  1859. s->uveob[pl][n] = res;
  1860. }
  1861. }
  1862. }
  1863. if (b->uvtx > TX_4X4) { // FIXME slow
  1864. for (y = 0; y < end_y; y += uvstep1d)
  1865. memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
  1866. for (x = 0; x < end_x; x += uvstep1d)
  1867. memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
  1868. }
  1869. }
  1870. return 0;
  1871. }
  1872. static av_always_inline int check_intra_mode(VP9Context *s, int mode, uint8_t **a,
  1873. uint8_t *dst_edge, ptrdiff_t stride_edge,
  1874. uint8_t *dst_inner, ptrdiff_t stride_inner,
  1875. uint8_t *l, int col, int x, int w,
  1876. int row, int y, enum TxfmMode tx,
  1877. int p)
  1878. {
  1879. int have_top = row > 0 || y > 0;
  1880. int have_left = col > s->tiling.tile_col_start || x > 0;
  1881. int have_right = x < w - 1;
  1882. static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
  1883. [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
  1884. { DC_127_PRED, VERT_PRED } },
  1885. [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
  1886. { HOR_PRED, HOR_PRED } },
  1887. [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
  1888. { LEFT_DC_PRED, DC_PRED } },
  1889. [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
  1890. { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
  1891. [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
  1892. { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
  1893. [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
  1894. { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
  1895. [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
  1896. { HOR_DOWN_PRED, HOR_DOWN_PRED } },
  1897. [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
  1898. { DC_127_PRED, VERT_LEFT_PRED } },
  1899. [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
  1900. { HOR_UP_PRED, HOR_UP_PRED } },
  1901. [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
  1902. { HOR_PRED, TM_VP8_PRED } },
  1903. };
  1904. static const struct {
  1905. uint8_t needs_left:1;
  1906. uint8_t needs_top:1;
  1907. uint8_t needs_topleft:1;
  1908. uint8_t needs_topright:1;
  1909. } edges[N_INTRA_PRED_MODES] = {
  1910. [VERT_PRED] = { .needs_top = 1 },
  1911. [HOR_PRED] = { .needs_left = 1 },
  1912. [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
  1913. [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
  1914. [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1915. [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1916. [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1917. [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
  1918. [HOR_UP_PRED] = { .needs_left = 1 },
  1919. [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1920. [LEFT_DC_PRED] = { .needs_left = 1 },
  1921. [TOP_DC_PRED] = { .needs_top = 1 },
  1922. [DC_128_PRED] = { 0 },
  1923. [DC_127_PRED] = { 0 },
  1924. [DC_129_PRED] = { 0 }
  1925. };
  1926. av_assert2(mode >= 0 && mode < 10);
  1927. mode = mode_conv[mode][have_left][have_top];
  1928. if (edges[mode].needs_top) {
  1929. uint8_t *top, *topleft;
  1930. int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
  1931. int n_px_need_tr = 0;
  1932. if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
  1933. n_px_need_tr = 4;
  1934. // if top of sb64-row, use s->intra_pred_data[] instead of
  1935. // dst[-stride] for intra prediction (it contains pre- instead of
  1936. // post-loopfilter data)
  1937. if (have_top) {
  1938. top = !(row & 7) && !y ?
  1939. s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
  1940. y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
  1941. if (have_left)
  1942. topleft = !(row & 7) && !y ?
  1943. s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
  1944. y == 0 || x == 0 ? &dst_edge[-stride_edge] :
  1945. &dst_inner[-stride_inner];
  1946. }
  1947. if (have_top &&
  1948. (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
  1949. (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
  1950. n_px_need + n_px_need_tr <= n_px_have) {
  1951. *a = top;
  1952. } else {
  1953. if (have_top) {
  1954. if (n_px_need <= n_px_have) {
  1955. memcpy(*a, top, n_px_need);
  1956. } else {
  1957. memcpy(*a, top, n_px_have);
  1958. memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
  1959. n_px_need - n_px_have);
  1960. }
  1961. } else {
  1962. memset(*a, 127, n_px_need);
  1963. }
  1964. if (edges[mode].needs_topleft) {
  1965. if (have_left && have_top) {
  1966. (*a)[-1] = topleft[-1];
  1967. } else {
  1968. (*a)[-1] = have_top ? 129 : 127;
  1969. }
  1970. }
  1971. if (tx == TX_4X4 && edges[mode].needs_topright) {
  1972. if (have_top && have_right &&
  1973. n_px_need + n_px_need_tr <= n_px_have) {
  1974. memcpy(&(*a)[4], &top[4], 4);
  1975. } else {
  1976. memset(&(*a)[4], (*a)[3], 4);
  1977. }
  1978. }
  1979. }
  1980. }
  1981. if (edges[mode].needs_left) {
  1982. if (have_left) {
  1983. int n_px_need = 4 << tx, i, n_px_have = (((s->rows - row) << !p) - y) * 4;
  1984. uint8_t *dst = x == 0 ? dst_edge : dst_inner;
  1985. ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
  1986. if (n_px_need <= n_px_have) {
  1987. for (i = 0; i < n_px_need; i++)
  1988. l[i] = dst[i * stride - 1];
  1989. } else {
  1990. for (i = 0; i < n_px_have; i++)
  1991. l[i] = dst[i * stride - 1];
  1992. memset(&l[i], l[i - 1], n_px_need - n_px_have);
  1993. }
  1994. } else {
  1995. memset(l, 129, 4 << tx);
  1996. }
  1997. }
  1998. return mode;
  1999. }
  2000. static void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
  2001. {
  2002. VP9Context *s = ctx->priv_data;
  2003. VP9Block *const b = &s->b;
  2004. int row = b->row, col = b->col;
  2005. int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
  2006. int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
  2007. int end_x = FFMIN(2 * (s->cols - col), w4);
  2008. int end_y = FFMIN(2 * (s->rows - row), h4);
  2009. int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
  2010. int uvstep1d = 1 << b->uvtx, p;
  2011. uint8_t *dst = b->dst[0], *dst_r = s->f->data[0] + y_off;
  2012. for (n = 0, y = 0; y < end_y; y += step1d) {
  2013. uint8_t *ptr = dst, *ptr_r = dst_r;
  2014. for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d,
  2015. ptr_r += 4 * step1d, n += step) {
  2016. int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
  2017. y * 2 + x : 0];
  2018. LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
  2019. uint8_t *a = &a_buf[16], l[32];
  2020. enum TxfmType txtp = vp9_intra_txfm_type[mode];
  2021. int eob = b->skip ? 0 : b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
  2022. mode = check_intra_mode(s, mode, &a, ptr_r, s->f->linesize[0],
  2023. ptr, b->y_stride, l,
  2024. col, x, w4, row, y, b->tx, 0);
  2025. s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
  2026. if (eob)
  2027. s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
  2028. s->block + 16 * n, eob);
  2029. }
  2030. dst_r += 4 * s->f->linesize[0] * step1d;
  2031. dst += 4 * b->y_stride * step1d;
  2032. }
  2033. // U/V
  2034. h4 >>= 1;
  2035. w4 >>= 1;
  2036. end_x >>= 1;
  2037. end_y >>= 1;
  2038. step = 1 << (b->uvtx * 2);
  2039. for (p = 0; p < 2; p++) {
  2040. dst = b->dst[1 + p];
  2041. dst_r = s->f->data[1 + p] + uv_off;
  2042. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  2043. uint8_t *ptr = dst, *ptr_r = dst_r;
  2044. for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d,
  2045. ptr_r += 4 * uvstep1d, n += step) {
  2046. int mode = b->uvmode;
  2047. LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
  2048. uint8_t *a = &a_buf[16], l[32];
  2049. int eob = b->skip ? 0 : b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
  2050. mode = check_intra_mode(s, mode, &a, ptr_r, s->f->linesize[1],
  2051. ptr, b->uv_stride, l,
  2052. col, x, w4, row, y, b->uvtx, p + 1);
  2053. s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
  2054. if (eob)
  2055. s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
  2056. s->uvblock[p] + 16 * n, eob);
  2057. }
  2058. dst_r += 4 * uvstep1d * s->f->linesize[1];
  2059. dst += 4 * uvstep1d * b->uv_stride;
  2060. }
  2061. }
  2062. }
  2063. static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func (*mc)[2],
  2064. uint8_t *dst, ptrdiff_t dst_stride,
  2065. const uint8_t *ref, ptrdiff_t ref_stride,
  2066. ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
  2067. int bw, int bh, int w, int h)
  2068. {
  2069. int mx = mv->x, my = mv->y;
  2070. y += my >> 3;
  2071. x += mx >> 3;
  2072. ref += y * ref_stride + x;
  2073. mx &= 7;
  2074. my &= 7;
  2075. // FIXME bilinear filter only needs 0/1 pixels, not 3/4
  2076. if (x < !!mx * 3 || y < !!my * 3 ||
  2077. x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
  2078. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  2079. ref - !!my * 3 * ref_stride - !!mx * 3,
  2080. 80, ref_stride,
  2081. bw + !!mx * 7, bh + !!my * 7,
  2082. x - !!mx * 3, y - !!my * 3, w, h);
  2083. ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  2084. ref_stride = 80;
  2085. }
  2086. mc[!!mx][!!my](dst, dst_stride, ref, ref_stride, bh, mx << 1, my << 1);
  2087. }
  2088. static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func (*mc)[2],
  2089. uint8_t *dst_u, uint8_t *dst_v,
  2090. ptrdiff_t dst_stride,
  2091. const uint8_t *ref_u, ptrdiff_t src_stride_u,
  2092. const uint8_t *ref_v, ptrdiff_t src_stride_v,
  2093. ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
  2094. int bw, int bh, int w, int h)
  2095. {
  2096. int mx = mv->x, my = mv->y;
  2097. y += my >> 4;
  2098. x += mx >> 4;
  2099. ref_u += y * src_stride_u + x;
  2100. ref_v += y * src_stride_v + x;
  2101. mx &= 15;
  2102. my &= 15;
  2103. // FIXME bilinear filter only needs 0/1 pixels, not 3/4
  2104. if (x < !!mx * 3 || y < !!my * 3 ||
  2105. x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
  2106. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  2107. ref_u - !!my * 3 * src_stride_u - !!mx * 3,
  2108. 80, src_stride_u,
  2109. bw + !!mx * 7, bh + !!my * 7,
  2110. x - !!mx * 3, y - !!my * 3, w, h);
  2111. ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  2112. mc[!!mx][!!my](dst_u, dst_stride, ref_u, 80, bh, mx, my);
  2113. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  2114. ref_v - !!my * 3 * src_stride_v - !!mx * 3,
  2115. 80, src_stride_v,
  2116. bw + !!mx * 7, bh + !!my * 7,
  2117. x - !!mx * 3, y - !!my * 3, w, h);
  2118. ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  2119. mc[!!mx][!!my](dst_v, dst_stride, ref_v, 80, bh, mx, my);
  2120. } else {
  2121. mc[!!mx][!!my](dst_u, dst_stride, ref_u, src_stride_u, bh, mx, my);
  2122. mc[!!mx][!!my](dst_v, dst_stride, ref_v, src_stride_v, bh, mx, my);
  2123. }
  2124. }
  2125. static void inter_recon(AVCodecContext *ctx)
  2126. {
  2127. static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
  2128. { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
  2129. { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
  2130. };
  2131. VP9Context *s = ctx->priv_data;
  2132. VP9Block *const b = &s->b;
  2133. int row = b->row, col = b->col;
  2134. AVFrame *ref1 = s->refs[s->refidx[b->ref[0]]];
  2135. AVFrame *ref2 = b->comp ? s->refs[s->refidx[b->ref[1]]] : NULL;
  2136. int w = ctx->width, h = ctx->height;
  2137. ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
  2138. // y inter pred
  2139. if (b->bs > BS_8x8) {
  2140. if (b->bs == BS_8x4) {
  2141. mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
  2142. ref1->data[0], ref1->linesize[0],
  2143. row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
  2144. mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
  2145. b->dst[0] + 4 * ls_y, ls_y,
  2146. ref1->data[0], ref1->linesize[0],
  2147. (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
  2148. if (b->comp) {
  2149. mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
  2150. ref2->data[0], ref2->linesize[0],
  2151. row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
  2152. mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
  2153. b->dst[0] + 4 * ls_y, ls_y,
  2154. ref2->data[0], ref2->linesize[0],
  2155. (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
  2156. }
  2157. } else if (b->bs == BS_4x8) {
  2158. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
  2159. ref1->data[0], ref1->linesize[0],
  2160. row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
  2161. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
  2162. ref1->data[0], ref1->linesize[0],
  2163. row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
  2164. if (b->comp) {
  2165. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
  2166. ref2->data[0], ref2->linesize[0],
  2167. row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
  2168. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
  2169. ref2->data[0], ref2->linesize[0],
  2170. row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
  2171. }
  2172. } else {
  2173. av_assert2(b->bs == BS_4x4);
  2174. // FIXME if two horizontally adjacent blocks have the same MV,
  2175. // do a w8 instead of a w4 call
  2176. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
  2177. ref1->data[0], ref1->linesize[0],
  2178. row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
  2179. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
  2180. ref1->data[0], ref1->linesize[0],
  2181. row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
  2182. mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
  2183. b->dst[0] + 4 * ls_y, ls_y,
  2184. ref1->data[0], ref1->linesize[0],
  2185. (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
  2186. mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
  2187. b->dst[0] + 4 * ls_y + 4, ls_y,
  2188. ref1->data[0], ref1->linesize[0],
  2189. (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
  2190. if (b->comp) {
  2191. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
  2192. ref2->data[0], ref2->linesize[0],
  2193. row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
  2194. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
  2195. ref2->data[0], ref2->linesize[0],
  2196. row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
  2197. mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
  2198. b->dst[0] + 4 * ls_y, ls_y,
  2199. ref2->data[0], ref2->linesize[0],
  2200. (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
  2201. mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
  2202. b->dst[0] + 4 * ls_y + 4, ls_y,
  2203. ref2->data[0], ref2->linesize[0],
  2204. (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
  2205. }
  2206. }
  2207. } else {
  2208. int bwl = bwlog_tab[0][b->bs];
  2209. int bw = bwh_tab[0][b->bs][0] * 4, bh = bwh_tab[0][b->bs][1] * 4;
  2210. mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
  2211. ref1->data[0], ref1->linesize[0],
  2212. row << 3, col << 3, &b->mv[0][0],bw, bh, w, h);
  2213. if (b->comp)
  2214. mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
  2215. ref2->data[0], ref2->linesize[0],
  2216. row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
  2217. }
  2218. // uv inter pred
  2219. {
  2220. int bwl = bwlog_tab[1][b->bs];
  2221. int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
  2222. VP56mv mvuv;
  2223. w = (w + 1) >> 1;
  2224. h = (h + 1) >> 1;
  2225. if (b->bs > BS_8x8) {
  2226. mvuv.x = ROUNDED_DIV(b->mv[0][0].x + b->mv[1][0].x + b->mv[2][0].x + b->mv[3][0].x, 4);
  2227. mvuv.y = ROUNDED_DIV(b->mv[0][0].y + b->mv[1][0].y + b->mv[2][0].y + b->mv[3][0].y, 4);
  2228. } else {
  2229. mvuv = b->mv[0][0];
  2230. }
  2231. mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
  2232. b->dst[1], b->dst[2], ls_uv,
  2233. ref1->data[1], ref1->linesize[1],
  2234. ref1->data[2], ref1->linesize[2],
  2235. row << 2, col << 2, &mvuv, bw, bh, w, h);
  2236. if (b->comp) {
  2237. if (b->bs > BS_8x8) {
  2238. mvuv.x = ROUNDED_DIV(b->mv[0][1].x + b->mv[1][1].x + b->mv[2][1].x + b->mv[3][1].x, 4);
  2239. mvuv.y = ROUNDED_DIV(b->mv[0][1].y + b->mv[1][1].y + b->mv[2][1].y + b->mv[3][1].y, 4);
  2240. } else {
  2241. mvuv = b->mv[0][1];
  2242. }
  2243. mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
  2244. b->dst[1], b->dst[2], ls_uv,
  2245. ref2->data[1], ref2->linesize[1],
  2246. ref2->data[2], ref2->linesize[2],
  2247. row << 2, col << 2, &mvuv, bw, bh, w, h);
  2248. }
  2249. }
  2250. if (!b->skip) {
  2251. /* mostly copied intra_reconn() */
  2252. int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
  2253. int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
  2254. int end_x = FFMIN(2 * (s->cols - col), w4);
  2255. int end_y = FFMIN(2 * (s->rows - row), h4);
  2256. int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
  2257. int uvstep1d = 1 << b->uvtx, p;
  2258. uint8_t *dst = b->dst[0];
  2259. // y itxfm add
  2260. for (n = 0, y = 0; y < end_y; y += step1d) {
  2261. uint8_t *ptr = dst;
  2262. for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
  2263. int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
  2264. if (eob)
  2265. s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
  2266. s->block + 16 * n, eob);
  2267. }
  2268. dst += 4 * b->y_stride * step1d;
  2269. }
  2270. // uv itxfm add
  2271. h4 >>= 1;
  2272. w4 >>= 1;
  2273. end_x >>= 1;
  2274. end_y >>= 1;
  2275. step = 1 << (b->uvtx * 2);
  2276. for (p = 0; p < 2; p++) {
  2277. dst = b->dst[p + 1];
  2278. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  2279. uint8_t *ptr = dst;
  2280. for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
  2281. int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
  2282. if (eob)
  2283. s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
  2284. s->uvblock[p] + 16 * n, eob);
  2285. }
  2286. dst += 4 * uvstep1d * b->uv_stride;
  2287. }
  2288. }
  2289. }
  2290. }
  2291. static av_always_inline void mask_edges(struct VP9Filter *lflvl, int is_uv,
  2292. int row_and_7, int col_and_7,
  2293. int w, int h, int col_end, int row_end,
  2294. enum TxfmMode tx, int skip_inter)
  2295. {
  2296. // FIXME I'm pretty sure all loops can be replaced by a single LUT if
  2297. // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
  2298. // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
  2299. // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
  2300. // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
  2301. // edges. This means that for UV, we work on two subsampled blocks at
  2302. // a time, and we only use the topleft block's mode information to set
  2303. // things like block strength. Thus, for any block size smaller than
  2304. // 16x16, ignore the odd portion of the block.
  2305. if (tx == TX_4X4 && is_uv) {
  2306. if (h == 1) {
  2307. if (row_and_7 & 1)
  2308. return;
  2309. if (!row_end)
  2310. h += 1;
  2311. }
  2312. if (w == 1) {
  2313. if (col_and_7 & 1)
  2314. return;
  2315. if (!col_end)
  2316. w += 1;
  2317. }
  2318. }
  2319. if (tx == TX_4X4 && !skip_inter) {
  2320. int t = 1 << col_and_7, m_col = (t << w) - t, y;
  2321. int m_col_odd = (t << (w - 1)) - t;
  2322. // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
  2323. if (is_uv) {
  2324. int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
  2325. for (y = row_and_7; y < h + row_and_7; y++) {
  2326. int col_mask_id = 2 - !(y & 7);
  2327. lflvl->mask[is_uv][0][y][1] |= m_row_8;
  2328. lflvl->mask[is_uv][0][y][2] |= m_row_4;
  2329. // for odd lines, if the odd col is not being filtered,
  2330. // skip odd row also:
  2331. // .---. <-- a
  2332. // | |
  2333. // |___| <-- b
  2334. // ^ ^
  2335. // c d
  2336. //
  2337. // if a/c are even row/col and b/d are odd, and d is skipped,
  2338. // e.g. right edge of size-66x66.webm, then skip b also (bug)
  2339. if ((col_end & 1) && (y & 1)) {
  2340. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
  2341. } else {
  2342. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
  2343. }
  2344. }
  2345. } else {
  2346. int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
  2347. for (y = row_and_7; y < h + row_and_7; y++) {
  2348. int col_mask_id = 2 - !(y & 3);
  2349. lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
  2350. lflvl->mask[is_uv][0][y][2] |= m_row_4;
  2351. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
  2352. lflvl->mask[is_uv][0][y][3] |= m_col;
  2353. lflvl->mask[is_uv][1][y][3] |= m_col;
  2354. }
  2355. }
  2356. } else {
  2357. int y, t = 1 << col_and_7, m_col = (t << w) - t;
  2358. if (!skip_inter) {
  2359. int mask_id = (tx == TX_8X8);
  2360. int l2 = tx + is_uv - 1, step1d = 1 << l2;
  2361. static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
  2362. int m_row = m_col & masks[l2];
  2363. // at odd UV col/row edges tx16/tx32 loopfilter edges, force
  2364. // 8wd loopfilter to prevent going off the visible edge.
  2365. if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
  2366. int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
  2367. int m_row_8 = m_row - m_row_16;
  2368. for (y = row_and_7; y < h + row_and_7; y++) {
  2369. lflvl->mask[is_uv][0][y][0] |= m_row_16;
  2370. lflvl->mask[is_uv][0][y][1] |= m_row_8;
  2371. }
  2372. } else {
  2373. for (y = row_and_7; y < h + row_and_7; y++)
  2374. lflvl->mask[is_uv][0][y][mask_id] |= m_row;
  2375. }
  2376. if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
  2377. for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
  2378. lflvl->mask[is_uv][1][y][0] |= m_col;
  2379. if (y - row_and_7 == h - 1)
  2380. lflvl->mask[is_uv][1][y][1] |= m_col;
  2381. } else {
  2382. for (y = row_and_7; y < h + row_and_7; y += step1d)
  2383. lflvl->mask[is_uv][1][y][mask_id] |= m_col;
  2384. }
  2385. } else if (tx != TX_4X4) {
  2386. int mask_id;
  2387. mask_id = (tx == TX_8X8) || (is_uv && h == 1);
  2388. lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
  2389. mask_id = (tx == TX_8X8) || (is_uv && w == 1);
  2390. for (y = row_and_7; y < h + row_and_7; y++)
  2391. lflvl->mask[is_uv][0][y][mask_id] |= t;
  2392. } else if (is_uv) {
  2393. int t8 = t & 0x01, t4 = t - t8;
  2394. for (y = row_and_7; y < h + row_and_7; y++) {
  2395. lflvl->mask[is_uv][0][y][2] |= t4;
  2396. lflvl->mask[is_uv][0][y][1] |= t8;
  2397. }
  2398. lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
  2399. } else {
  2400. int t8 = t & 0x11, t4 = t - t8;
  2401. for (y = row_and_7; y < h + row_and_7; y++) {
  2402. lflvl->mask[is_uv][0][y][2] |= t4;
  2403. lflvl->mask[is_uv][0][y][1] |= t8;
  2404. }
  2405. lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
  2406. }
  2407. }
  2408. }
  2409. static int decode_b(AVCodecContext *ctx, int row, int col,
  2410. struct VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  2411. enum BlockLevel bl, enum BlockPartition bp)
  2412. {
  2413. VP9Context *s = ctx->priv_data;
  2414. VP9Block *const b = &s->b;
  2415. enum BlockSize bs = bl * 3 + bp;
  2416. int res, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
  2417. int emu[2];
  2418. b->row = row;
  2419. b->row7 = row & 7;
  2420. b->col = col;
  2421. b->col7 = col & 7;
  2422. s->min_mv.x = -(128 + col * 64);
  2423. s->min_mv.y = -(128 + row * 64);
  2424. s->max_mv.x = 128 + (s->cols - col - w4) * 64;
  2425. s->max_mv.y = 128 + (s->rows - row - h4) * 64;
  2426. b->bs = bs;
  2427. decode_mode(ctx);
  2428. b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
  2429. if (!b->skip) {
  2430. if ((res = decode_coeffs(ctx)) < 0)
  2431. return res;
  2432. } else {
  2433. int pl;
  2434. memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
  2435. memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
  2436. for (pl = 0; pl < 2; pl++) {
  2437. memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
  2438. memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
  2439. }
  2440. }
  2441. // emulated overhangs if the stride of the target buffer can't hold. This
  2442. // allows to support emu-edge and so on even if we have large block
  2443. // overhangs
  2444. emu[0] = (col + w4) * 8 > s->f->linesize[0] ||
  2445. (row + h4) > s->rows + 2 * !(ctx->flags & CODEC_FLAG_EMU_EDGE);
  2446. emu[1] = (col + w4) * 4 > s->f->linesize[1] ||
  2447. (row + h4) > s->rows + 2 * !(ctx->flags & CODEC_FLAG_EMU_EDGE);
  2448. if (emu[0]) {
  2449. b->dst[0] = s->tmp_y;
  2450. b->y_stride = 64;
  2451. } else {
  2452. b->dst[0] = s->f->data[0] + yoff;
  2453. b->y_stride = s->f->linesize[0];
  2454. }
  2455. if (emu[1]) {
  2456. b->dst[1] = s->tmp_uv[0];
  2457. b->dst[2] = s->tmp_uv[1];
  2458. b->uv_stride = 32;
  2459. } else {
  2460. b->dst[1] = s->f->data[1] + uvoff;
  2461. b->dst[2] = s->f->data[2] + uvoff;
  2462. b->uv_stride = s->f->linesize[1];
  2463. }
  2464. if (b->intra) {
  2465. intra_recon(ctx, yoff, uvoff);
  2466. } else {
  2467. inter_recon(ctx);
  2468. }
  2469. if (emu[0]) {
  2470. int w = FFMIN(s->cols - col, w4) * 8, h = FFMIN(s->rows - row, h4) * 8, n, o = 0;
  2471. for (n = 0; o < w; n++) {
  2472. int bw = 64 >> n;
  2473. av_assert2(n <= 4);
  2474. if (w & bw) {
  2475. s->dsp.mc[n][0][0][0][0](s->f->data[0] + yoff + o, s->f->linesize[0],
  2476. s->tmp_y + o, 64, h, 0, 0);
  2477. o += bw;
  2478. }
  2479. }
  2480. }
  2481. if (emu[1]) {
  2482. int w = FFMIN(s->cols - col, w4) * 4, h = FFMIN(s->rows - row, h4) * 4, n, o = 0;
  2483. for (n = 1; o < w; n++) {
  2484. int bw = 64 >> n;
  2485. av_assert2(n <= 4);
  2486. if (w & bw) {
  2487. s->dsp.mc[n][0][0][0][0](s->f->data[1] + uvoff + o, s->f->linesize[1],
  2488. s->tmp_uv[0] + o, 32, h, 0, 0);
  2489. s->dsp.mc[n][0][0][0][0](s->f->data[2] + uvoff + o, s->f->linesize[2],
  2490. s->tmp_uv[1] + o, 32, h, 0, 0);
  2491. o += bw;
  2492. }
  2493. }
  2494. }
  2495. // pick filter level and find edges to apply filter to
  2496. if (s->filter.level &&
  2497. (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
  2498. [b->mode[3] != ZEROMV]) > 0) {
  2499. int x_end = FFMIN(s->cols - col, w4), y_end = FFMIN(s->rows - row, h4);
  2500. int skip_inter = !b->intra && b->skip;
  2501. for (y = 0; y < h4; y++)
  2502. memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
  2503. mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
  2504. mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
  2505. s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
  2506. s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
  2507. b->uvtx, skip_inter);
  2508. if (!s->filter.lim_lut[lvl]) {
  2509. int sharp = s->filter.sharpness;
  2510. int limit = lvl;
  2511. if (sharp > 0) {
  2512. limit >>= (sharp + 3) >> 2;
  2513. limit = FFMIN(limit, 9 - sharp);
  2514. }
  2515. limit = FFMAX(limit, 1);
  2516. s->filter.lim_lut[lvl] = limit;
  2517. s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
  2518. }
  2519. }
  2520. return 0;
  2521. }
  2522. static int decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lflvl,
  2523. ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
  2524. {
  2525. VP9Context *s = ctx->priv_data;
  2526. int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
  2527. (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1), res;
  2528. const uint8_t *p = s->keyframe ? vp9_default_kf_partition_probs[bl][c] :
  2529. s->prob.p.partition[bl][c];
  2530. enum BlockPartition bp;
  2531. ptrdiff_t hbs = 4 >> bl;
  2532. if (bl == BL_8X8) {
  2533. bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
  2534. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2535. } else if (col + hbs < s->cols) {
  2536. if (row + hbs < s->rows) {
  2537. bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
  2538. switch (bp) {
  2539. case PARTITION_NONE:
  2540. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2541. break;
  2542. case PARTITION_H:
  2543. if (!(res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp))) {
  2544. yoff += hbs * 8 * s->f->linesize[0];
  2545. uvoff += hbs * 4 * s->f->linesize[1];
  2546. res = decode_b(ctx, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
  2547. }
  2548. break;
  2549. case PARTITION_V:
  2550. if (!(res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp))) {
  2551. yoff += hbs * 8;
  2552. uvoff += hbs * 4;
  2553. res = decode_b(ctx, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
  2554. }
  2555. break;
  2556. case PARTITION_SPLIT:
  2557. if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1))) {
  2558. if (!(res = decode_sb(ctx, row, col + hbs, lflvl,
  2559. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1))) {
  2560. yoff += hbs * 8 * s->f->linesize[0];
  2561. uvoff += hbs * 4 * s->f->linesize[1];
  2562. if (!(res = decode_sb(ctx, row + hbs, col, lflvl,
  2563. yoff, uvoff, bl + 1)))
  2564. res = decode_sb(ctx, row + hbs, col + hbs, lflvl,
  2565. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
  2566. }
  2567. }
  2568. break;
  2569. default:
  2570. av_assert0(0);
  2571. }
  2572. } else if (vp56_rac_get_prob_branchy(&s->c, p[1])) {
  2573. bp = PARTITION_SPLIT;
  2574. if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1)))
  2575. res = decode_sb(ctx, row, col + hbs, lflvl,
  2576. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
  2577. } else {
  2578. bp = PARTITION_H;
  2579. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2580. }
  2581. } else if (row + hbs < s->rows) {
  2582. if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
  2583. bp = PARTITION_SPLIT;
  2584. if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1))) {
  2585. yoff += hbs * 8 * s->f->linesize[0];
  2586. uvoff += hbs * 4 * s->f->linesize[1];
  2587. res = decode_sb(ctx, row + hbs, col, lflvl,
  2588. yoff, uvoff, bl + 1);
  2589. }
  2590. } else {
  2591. bp = PARTITION_V;
  2592. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2593. }
  2594. } else {
  2595. bp = PARTITION_SPLIT;
  2596. res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
  2597. }
  2598. s->counts.partition[bl][c][bp]++;
  2599. return res;
  2600. }
  2601. static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
  2602. int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
  2603. {
  2604. VP9Context *s = ctx->priv_data;
  2605. uint8_t *dst = s->f->data[0] + yoff, *lvl = lflvl->level;
  2606. ptrdiff_t ls_y = s->f->linesize[0], ls_uv = s->f->linesize[1];
  2607. int y, x, p;
  2608. // FIXME in how far can we interleave the v/h loopfilter calls? E.g.
  2609. // if you think of them as acting on a 8x8 block max, we can interleave
  2610. // each v/h within the single x loop, but that only works if we work on
  2611. // 8 pixel blocks, and we won't always do that (we want at least 16px
  2612. // to use SSE2 optimizations, perhaps 32 for AVX2)
  2613. // filter edges between columns, Y plane (e.g. block1 | block2)
  2614. for (y = 0; y < 8; y += 2, dst += 16 * ls_y, lvl += 16) {
  2615. uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[0][0][y];
  2616. uint8_t *hmask2 = lflvl->mask[0][0][y + 1];
  2617. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
  2618. unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
  2619. unsigned hm = hm1 | hm2 | hm13 | hm23;
  2620. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8, l++) {
  2621. if (hm1 & x) {
  2622. int L = *l, H = L >> 4;
  2623. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2624. if (col || x > 1) {
  2625. if (hmask1[0] & x) {
  2626. if (hmask2[0] & x) {
  2627. av_assert2(l[8] == L);
  2628. s->dsp.loop_filter_16[0](ptr, ls_y, E, I, H);
  2629. } else {
  2630. s->dsp.loop_filter_8[2][0](ptr, ls_y, E, I, H);
  2631. }
  2632. } else if (hm2 & x) {
  2633. L = l[8];
  2634. H |= (L >> 4) << 8;
  2635. E |= s->filter.mblim_lut[L] << 8;
  2636. I |= s->filter.lim_lut[L] << 8;
  2637. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  2638. [!!(hmask2[1] & x)]
  2639. [0](ptr, ls_y, E, I, H);
  2640. } else {
  2641. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  2642. [0](ptr, ls_y, E, I, H);
  2643. }
  2644. }
  2645. } else if (hm2 & x) {
  2646. int L = l[8], H = L >> 4;
  2647. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2648. if (col || x > 1) {
  2649. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  2650. [0](ptr + 8 * ls_y, ls_y, E, I, H);
  2651. }
  2652. }
  2653. if (hm13 & x) {
  2654. int L = *l, H = L >> 4;
  2655. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2656. if (hm23 & x) {
  2657. L = l[8];
  2658. H |= (L >> 4) << 8;
  2659. E |= s->filter.mblim_lut[L] << 8;
  2660. I |= s->filter.lim_lut[L] << 8;
  2661. s->dsp.loop_filter_mix2[0][0][0](ptr + 4, ls_y, E, I, H);
  2662. } else {
  2663. s->dsp.loop_filter_8[0][0](ptr + 4, ls_y, E, I, H);
  2664. }
  2665. } else if (hm23 & x) {
  2666. int L = l[8], H = L >> 4;
  2667. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2668. s->dsp.loop_filter_8[0][0](ptr + 8 * ls_y + 4, ls_y, E, I, H);
  2669. }
  2670. }
  2671. }
  2672. // block1
  2673. // filter edges between rows, Y plane (e.g. ------)
  2674. // block2
  2675. dst = s->f->data[0] + yoff;
  2676. lvl = lflvl->level;
  2677. for (y = 0; y < 8; y++, dst += 8 * ls_y, lvl += 8) {
  2678. uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[0][1][y];
  2679. unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
  2680. for (x = 1; vm & ~(x - 1); x <<= 2, ptr += 16, l += 2) {
  2681. if (row || y) {
  2682. if (vm & x) {
  2683. int L = *l, H = L >> 4;
  2684. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2685. if (vmask[0] & x) {
  2686. if (vmask[0] & (x << 1)) {
  2687. av_assert2(l[1] == L);
  2688. s->dsp.loop_filter_16[1](ptr, ls_y, E, I, H);
  2689. } else {
  2690. s->dsp.loop_filter_8[2][1](ptr, ls_y, E, I, H);
  2691. }
  2692. } else if (vm & (x << 1)) {
  2693. L = l[1];
  2694. H |= (L >> 4) << 8;
  2695. E |= s->filter.mblim_lut[L] << 8;
  2696. I |= s->filter.lim_lut[L] << 8;
  2697. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  2698. [!!(vmask[1] & (x << 1))]
  2699. [1](ptr, ls_y, E, I, H);
  2700. } else {
  2701. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  2702. [1](ptr, ls_y, E, I, H);
  2703. }
  2704. } else if (vm & (x << 1)) {
  2705. int L = l[1], H = L >> 4;
  2706. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2707. s->dsp.loop_filter_8[!!(vmask[1] & (x << 1))]
  2708. [1](ptr + 8, ls_y, E, I, H);
  2709. }
  2710. }
  2711. if (vm3 & x) {
  2712. int L = *l, H = L >> 4;
  2713. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2714. if (vm3 & (x << 1)) {
  2715. L = l[1];
  2716. H |= (L >> 4) << 8;
  2717. E |= s->filter.mblim_lut[L] << 8;
  2718. I |= s->filter.lim_lut[L] << 8;
  2719. s->dsp.loop_filter_mix2[0][0][1](ptr + ls_y * 4, ls_y, E, I, H);
  2720. } else {
  2721. s->dsp.loop_filter_8[0][1](ptr + ls_y * 4, ls_y, E, I, H);
  2722. }
  2723. } else if (vm3 & (x << 1)) {
  2724. int L = l[1], H = L >> 4;
  2725. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2726. s->dsp.loop_filter_8[0][1](ptr + ls_y * 4 + 8, ls_y, E, I, H);
  2727. }
  2728. }
  2729. }
  2730. // same principle but for U/V planes
  2731. for (p = 0; p < 2; p++) {
  2732. lvl = lflvl->level;
  2733. dst = s->f->data[1 + p] + uvoff;
  2734. for (y = 0; y < 8; y += 4, dst += 16 * ls_uv, lvl += 32) {
  2735. uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[1][0][y];
  2736. uint8_t *hmask2 = lflvl->mask[1][0][y + 2];
  2737. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2];
  2738. unsigned hm2 = hmask2[1] | hmask2[2], hm = hm1 | hm2;
  2739. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 4) {
  2740. if (col || x > 1) {
  2741. if (hm1 & x) {
  2742. int L = *l, H = L >> 4;
  2743. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2744. if (hmask1[0] & x) {
  2745. if (hmask2[0] & x) {
  2746. av_assert2(l[16] == L);
  2747. s->dsp.loop_filter_16[0](ptr, ls_uv, E, I, H);
  2748. } else {
  2749. s->dsp.loop_filter_8[2][0](ptr, ls_uv, E, I, H);
  2750. }
  2751. } else if (hm2 & x) {
  2752. L = l[16];
  2753. H |= (L >> 4) << 8;
  2754. E |= s->filter.mblim_lut[L] << 8;
  2755. I |= s->filter.lim_lut[L] << 8;
  2756. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  2757. [!!(hmask2[1] & x)]
  2758. [0](ptr, ls_uv, E, I, H);
  2759. } else {
  2760. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  2761. [0](ptr, ls_uv, E, I, H);
  2762. }
  2763. } else if (hm2 & x) {
  2764. int L = l[16], H = L >> 4;
  2765. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2766. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  2767. [0](ptr + 8 * ls_uv, ls_uv, E, I, H);
  2768. }
  2769. }
  2770. if (x & 0xAA)
  2771. l += 2;
  2772. }
  2773. }
  2774. lvl = lflvl->level;
  2775. dst = s->f->data[1 + p] + uvoff;
  2776. for (y = 0; y < 8; y++, dst += 4 * ls_uv) {
  2777. uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[1][1][y];
  2778. unsigned vm = vmask[0] | vmask[1] | vmask[2];
  2779. for (x = 1; vm & ~(x - 1); x <<= 4, ptr += 16, l += 4) {
  2780. if (row || y) {
  2781. if (vm & x) {
  2782. int L = *l, H = L >> 4;
  2783. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2784. if (vmask[0] & x) {
  2785. if (vmask[0] & (x << 2)) {
  2786. av_assert2(l[2] == L);
  2787. s->dsp.loop_filter_16[1](ptr, ls_uv, E, I, H);
  2788. } else {
  2789. s->dsp.loop_filter_8[2][1](ptr, ls_uv, E, I, H);
  2790. }
  2791. } else if (vm & (x << 2)) {
  2792. L = l[2];
  2793. H |= (L >> 4) << 8;
  2794. E |= s->filter.mblim_lut[L] << 8;
  2795. I |= s->filter.lim_lut[L] << 8;
  2796. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  2797. [!!(vmask[1] & (x << 2))]
  2798. [1](ptr, ls_uv, E, I, H);
  2799. } else {
  2800. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  2801. [1](ptr, ls_uv, E, I, H);
  2802. }
  2803. } else if (vm & (x << 2)) {
  2804. int L = l[2], H = L >> 4;
  2805. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2806. s->dsp.loop_filter_8[!!(vmask[1] & (x << 2))]
  2807. [1](ptr + 8, ls_uv, E, I, H);
  2808. }
  2809. }
  2810. }
  2811. if (y & 1)
  2812. lvl += 16;
  2813. }
  2814. }
  2815. }
  2816. static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
  2817. {
  2818. int sb_start = ( idx * n) >> log2_n;
  2819. int sb_end = ((idx + 1) * n) >> log2_n;
  2820. *start = FFMIN(sb_start, n) << 3;
  2821. *end = FFMIN(sb_end, n) << 3;
  2822. }
  2823. static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
  2824. int max_count, int update_factor)
  2825. {
  2826. unsigned ct = ct0 + ct1, p2, p1;
  2827. if (!ct)
  2828. return;
  2829. p1 = *p;
  2830. p2 = ((ct0 << 8) + (ct >> 1)) / ct;
  2831. p2 = av_clip(p2, 1, 255);
  2832. ct = FFMIN(ct, max_count);
  2833. update_factor = FASTDIV(update_factor * ct, max_count);
  2834. // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
  2835. *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
  2836. }
  2837. static void adapt_probs(VP9Context *s)
  2838. {
  2839. int i, j, k, l, m;
  2840. prob_context *p = &s->prob_ctx[s->framectxid].p;
  2841. int uf = (s->keyframe || s->intraonly || !s->last_keyframe) ? 112 : 128;
  2842. // coefficients
  2843. for (i = 0; i < 4; i++)
  2844. for (j = 0; j < 2; j++)
  2845. for (k = 0; k < 2; k++)
  2846. for (l = 0; l < 6; l++)
  2847. for (m = 0; m < 6; m++) {
  2848. uint8_t *pp = s->prob_ctx[s->framectxid].coef[i][j][k][l][m];
  2849. unsigned *e = s->counts.eob[i][j][k][l][m];
  2850. unsigned *c = s->counts.coef[i][j][k][l][m];
  2851. if (l == 0 && m >= 3) // dc only has 3 pt
  2852. break;
  2853. adapt_prob(&pp[0], e[0], e[1], 24, uf);
  2854. adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
  2855. adapt_prob(&pp[2], c[1], c[2], 24, uf);
  2856. }
  2857. if (s->keyframe || s->intraonly) {
  2858. memcpy(p->skip, s->prob.p.skip, sizeof(p->skip));
  2859. memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
  2860. memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
  2861. memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p));
  2862. return;
  2863. }
  2864. // skip flag
  2865. for (i = 0; i < 3; i++)
  2866. adapt_prob(&p->skip[i], s->counts.skip[i][0], s->counts.skip[i][1], 20, 128);
  2867. // intra/inter flag
  2868. for (i = 0; i < 4; i++)
  2869. adapt_prob(&p->intra[i], s->counts.intra[i][0], s->counts.intra[i][1], 20, 128);
  2870. // comppred flag
  2871. if (s->comppredmode == PRED_SWITCHABLE) {
  2872. for (i = 0; i < 5; i++)
  2873. adapt_prob(&p->comp[i], s->counts.comp[i][0], s->counts.comp[i][1], 20, 128);
  2874. }
  2875. // reference frames
  2876. if (s->comppredmode != PRED_SINGLEREF) {
  2877. for (i = 0; i < 5; i++)
  2878. adapt_prob(&p->comp_ref[i], s->counts.comp_ref[i][0],
  2879. s->counts.comp_ref[i][1], 20, 128);
  2880. }
  2881. if (s->comppredmode != PRED_COMPREF) {
  2882. for (i = 0; i < 5; i++) {
  2883. uint8_t *pp = p->single_ref[i];
  2884. unsigned (*c)[2] = s->counts.single_ref[i];
  2885. adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
  2886. adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
  2887. }
  2888. }
  2889. // block partitioning
  2890. for (i = 0; i < 4; i++)
  2891. for (j = 0; j < 4; j++) {
  2892. uint8_t *pp = p->partition[i][j];
  2893. unsigned *c = s->counts.partition[i][j];
  2894. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2895. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2896. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2897. }
  2898. // tx size
  2899. if (s->txfmmode == TX_SWITCHABLE) {
  2900. for (i = 0; i < 2; i++) {
  2901. unsigned *c16 = s->counts.tx16p[i], *c32 = s->counts.tx32p[i];
  2902. adapt_prob(&p->tx8p[i], s->counts.tx8p[i][0], s->counts.tx8p[i][1], 20, 128);
  2903. adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
  2904. adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
  2905. adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
  2906. adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
  2907. adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
  2908. }
  2909. }
  2910. // interpolation filter
  2911. if (s->filtermode == FILTER_SWITCHABLE) {
  2912. for (i = 0; i < 4; i++) {
  2913. uint8_t *pp = p->filter[i];
  2914. unsigned *c = s->counts.filter[i];
  2915. adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
  2916. adapt_prob(&pp[1], c[1], c[2], 20, 128);
  2917. }
  2918. }
  2919. // inter modes
  2920. for (i = 0; i < 7; i++) {
  2921. uint8_t *pp = p->mv_mode[i];
  2922. unsigned *c = s->counts.mv_mode[i];
  2923. adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
  2924. adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
  2925. adapt_prob(&pp[2], c[1], c[3], 20, 128);
  2926. }
  2927. // mv joints
  2928. {
  2929. uint8_t *pp = p->mv_joint;
  2930. unsigned *c = s->counts.mv_joint;
  2931. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2932. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2933. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2934. }
  2935. // mv components
  2936. for (i = 0; i < 2; i++) {
  2937. uint8_t *pp;
  2938. unsigned *c, (*c2)[2], sum;
  2939. adapt_prob(&p->mv_comp[i].sign, s->counts.mv_comp[i].sign[0],
  2940. s->counts.mv_comp[i].sign[1], 20, 128);
  2941. pp = p->mv_comp[i].classes;
  2942. c = s->counts.mv_comp[i].classes;
  2943. sum = c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9] + c[10];
  2944. adapt_prob(&pp[0], c[0], sum, 20, 128);
  2945. sum -= c[1];
  2946. adapt_prob(&pp[1], c[1], sum, 20, 128);
  2947. sum -= c[2] + c[3];
  2948. adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
  2949. adapt_prob(&pp[3], c[2], c[3], 20, 128);
  2950. sum -= c[4] + c[5];
  2951. adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
  2952. adapt_prob(&pp[5], c[4], c[5], 20, 128);
  2953. sum -= c[6];
  2954. adapt_prob(&pp[6], c[6], sum, 20, 128);
  2955. adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
  2956. adapt_prob(&pp[8], c[7], c[8], 20, 128);
  2957. adapt_prob(&pp[9], c[9], c[10], 20, 128);
  2958. adapt_prob(&p->mv_comp[i].class0, s->counts.mv_comp[i].class0[0],
  2959. s->counts.mv_comp[i].class0[1], 20, 128);
  2960. pp = p->mv_comp[i].bits;
  2961. c2 = s->counts.mv_comp[i].bits;
  2962. for (j = 0; j < 10; j++)
  2963. adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
  2964. for (j = 0; j < 2; j++) {
  2965. pp = p->mv_comp[i].class0_fp[j];
  2966. c = s->counts.mv_comp[i].class0_fp[j];
  2967. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2968. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2969. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2970. }
  2971. pp = p->mv_comp[i].fp;
  2972. c = s->counts.mv_comp[i].fp;
  2973. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2974. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2975. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2976. if (s->highprecisionmvs) {
  2977. adapt_prob(&p->mv_comp[i].class0_hp, s->counts.mv_comp[i].class0_hp[0],
  2978. s->counts.mv_comp[i].class0_hp[1], 20, 128);
  2979. adapt_prob(&p->mv_comp[i].hp, s->counts.mv_comp[i].hp[0],
  2980. s->counts.mv_comp[i].hp[1], 20, 128);
  2981. }
  2982. }
  2983. // y intra modes
  2984. for (i = 0; i < 4; i++) {
  2985. uint8_t *pp = p->y_mode[i];
  2986. unsigned *c = s->counts.y_mode[i], sum, s2;
  2987. sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
  2988. adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
  2989. sum -= c[TM_VP8_PRED];
  2990. adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
  2991. sum -= c[VERT_PRED];
  2992. adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
  2993. s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
  2994. sum -= s2;
  2995. adapt_prob(&pp[3], s2, sum, 20, 128);
  2996. s2 -= c[HOR_PRED];
  2997. adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
  2998. adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], 20, 128);
  2999. sum -= c[DIAG_DOWN_LEFT_PRED];
  3000. adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
  3001. sum -= c[VERT_LEFT_PRED];
  3002. adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
  3003. adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
  3004. }
  3005. // uv intra modes
  3006. for (i = 0; i < 10; i++) {
  3007. uint8_t *pp = p->uv_mode[i];
  3008. unsigned *c = s->counts.uv_mode[i], sum, s2;
  3009. sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
  3010. adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
  3011. sum -= c[TM_VP8_PRED];
  3012. adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
  3013. sum -= c[VERT_PRED];
  3014. adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
  3015. s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
  3016. sum -= s2;
  3017. adapt_prob(&pp[3], s2, sum, 20, 128);
  3018. s2 -= c[HOR_PRED];
  3019. adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
  3020. adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], 20, 128);
  3021. sum -= c[DIAG_DOWN_LEFT_PRED];
  3022. adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
  3023. sum -= c[VERT_LEFT_PRED];
  3024. adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
  3025. adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
  3026. }
  3027. }
  3028. static av_cold int vp9_decode_free(AVCodecContext *ctx)
  3029. {
  3030. VP9Context *s = ctx->priv_data;
  3031. int i;
  3032. for (i = 0; i < 8; i++)
  3033. av_frame_free(&s->refs[i]);
  3034. av_freep(&s->above_partition_ctx);
  3035. av_freep(&s->c_b);
  3036. return 0;
  3037. }
  3038. static int vp9_decode_frame(AVCodecContext *ctx, AVFrame *frame,
  3039. int *got_frame, const uint8_t *data, int size)
  3040. {
  3041. VP9Context *s = ctx->priv_data;
  3042. int res, tile_row, tile_col, i, ref, row, col;
  3043. ptrdiff_t yoff = 0, uvoff = 0;
  3044. //AVFrame *prev_frame = s->f; // for segmentation map
  3045. if ((res = decode_frame_header(ctx, data, size, &ref)) < 0) {
  3046. return res;
  3047. } else if (res == 0) {
  3048. if (!s->refs[ref]->buf[0]) {
  3049. av_log(ctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
  3050. return AVERROR_INVALIDDATA;
  3051. }
  3052. if ((res = av_frame_ref(frame, s->refs[ref])) < 0)
  3053. return res;
  3054. *got_frame = 1;
  3055. return 0;
  3056. }
  3057. data += res;
  3058. size -= res;
  3059. s->f = frame;
  3060. if ((res = ff_get_buffer(ctx, s->f,
  3061. s->refreshrefmask ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
  3062. return res;
  3063. s->f->key_frame = s->keyframe;
  3064. s->f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  3065. // main tile decode loop
  3066. memset(s->above_partition_ctx, 0, s->cols);
  3067. memset(s->above_skip_ctx, 0, s->cols);
  3068. if (s->keyframe || s->intraonly) {
  3069. memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
  3070. } else {
  3071. memset(s->above_mode_ctx, NEARESTMV, s->cols);
  3072. }
  3073. memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
  3074. memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 8);
  3075. memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 8);
  3076. memset(s->above_segpred_ctx, 0, s->cols);
  3077. for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) {
  3078. set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end,
  3079. tile_row, s->tiling.log2_tile_rows, s->sb_rows);
  3080. for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
  3081. unsigned tile_size;
  3082. if (tile_col == s->tiling.tile_cols - 1 &&
  3083. tile_row == s->tiling.tile_rows - 1) {
  3084. tile_size = size;
  3085. } else {
  3086. tile_size = AV_RB32(data);
  3087. data += 4;
  3088. size -= 4;
  3089. }
  3090. if (tile_size > size)
  3091. return AVERROR_INVALIDDATA;
  3092. ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
  3093. if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) // marker bit
  3094. return AVERROR_INVALIDDATA;
  3095. data += tile_size;
  3096. size -= tile_size;
  3097. }
  3098. for (row = s->tiling.tile_row_start;
  3099. row < s->tiling.tile_row_end;
  3100. row += 8, yoff += s->f->linesize[0] * 64,
  3101. uvoff += s->f->linesize[1] * 32) {
  3102. struct VP9Filter *lflvl_ptr = s->lflvl;
  3103. ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
  3104. for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
  3105. set_tile_offset(&s->tiling.tile_col_start, &s->tiling.tile_col_end,
  3106. tile_col, s->tiling.log2_tile_cols, s->sb_cols);
  3107. memset(s->left_partition_ctx, 0, 8);
  3108. memset(s->left_skip_ctx, 0, 8);
  3109. if (s->keyframe || s->intraonly) {
  3110. memset(s->left_mode_ctx, DC_PRED, 16);
  3111. } else {
  3112. memset(s->left_mode_ctx, NEARESTMV, 8);
  3113. }
  3114. memset(s->left_y_nnz_ctx, 0, 16);
  3115. memset(s->left_uv_nnz_ctx, 0, 16);
  3116. memset(s->left_segpred_ctx, 0, 8);
  3117. memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
  3118. for (col = s->tiling.tile_col_start;
  3119. col < s->tiling.tile_col_end;
  3120. col += 8, yoff2 += 64, uvoff2 += 32, lflvl_ptr++) {
  3121. // FIXME integrate with lf code (i.e. zero after each
  3122. // use, similar to invtxfm coefficients, or similar)
  3123. memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
  3124. if ((res = decode_sb(ctx, row, col, lflvl_ptr,
  3125. yoff2, uvoff2, BL_64X64)) < 0)
  3126. return res;
  3127. }
  3128. memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
  3129. }
  3130. // backup pre-loopfilter reconstruction data for intra
  3131. // prediction of next row of sb64s
  3132. if (row + 8 < s->rows) {
  3133. memcpy(s->intra_pred_data[0],
  3134. s->f->data[0] + yoff + 63 * s->f->linesize[0],
  3135. 8 * s->cols);
  3136. memcpy(s->intra_pred_data[1],
  3137. s->f->data[1] + uvoff + 31 * s->f->linesize[1],
  3138. 4 * s->cols);
  3139. memcpy(s->intra_pred_data[2],
  3140. s->f->data[2] + uvoff + 31 * s->f->linesize[2],
  3141. 4 * s->cols);
  3142. }
  3143. // loopfilter one row
  3144. if (s->filter.level) {
  3145. yoff2 = yoff;
  3146. uvoff2 = uvoff;
  3147. lflvl_ptr = s->lflvl;
  3148. for (col = 0; col < s->cols;
  3149. col += 8, yoff2 += 64, uvoff2 += 32, lflvl_ptr++) {
  3150. loopfilter_sb(ctx, lflvl_ptr, row, col, yoff2, uvoff2);
  3151. }
  3152. }
  3153. }
  3154. }
  3155. // bw adaptivity (or in case of parallel decoding mode, fw adaptivity
  3156. // probability maintenance between frames)
  3157. if (s->refreshctx) {
  3158. if (s->parallelmode) {
  3159. int i, j, k, l, m;
  3160. for (i = 0; i < 4; i++)
  3161. for (j = 0; j < 2; j++)
  3162. for (k = 0; k < 2; k++)
  3163. for (l = 0; l < 6; l++)
  3164. for (m = 0; m < 6; m++)
  3165. memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m],
  3166. s->prob.coef[i][j][k][l][m], 3);
  3167. s->prob_ctx[s->framectxid].p = s->prob.p;
  3168. } else {
  3169. adapt_probs(s);
  3170. }
  3171. }
  3172. FFSWAP(struct VP9mvrefPair *, s->mv[0], s->mv[1]);
  3173. // ref frame setup
  3174. for (i = 0; i < 8; i++)
  3175. if (s->refreshrefmask & (1 << i)) {
  3176. av_frame_unref(s->refs[i]);
  3177. if ((res = av_frame_ref(s->refs[i], s->f)) < 0)
  3178. return res;
  3179. }
  3180. if (s->invisible) {
  3181. av_frame_unref(s->f);
  3182. } else {
  3183. *got_frame = 1;
  3184. }
  3185. return 0;
  3186. }
  3187. static int vp9_decode_packet(AVCodecContext *avctx, AVFrame *frame,
  3188. int *got_frame, AVPacket *avpkt)
  3189. {
  3190. const uint8_t *data = avpkt->data;
  3191. int size = avpkt->size, marker, res;
  3192. // read superframe index - this is a collection of individual frames that
  3193. // together lead to one visible frame
  3194. av_assert1(size > 0); // without CODEC_CAP_DELAY, this is implied
  3195. marker = data[size - 1];
  3196. if ((marker & 0xe0) == 0xc0) {
  3197. int nbytes = 1 + ((marker >> 3) & 0x3);
  3198. int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
  3199. if (size >= idx_sz && data[size - idx_sz] == marker) {
  3200. const uint8_t *idx = data + size + 1 - idx_sz;
  3201. switch (nbytes) {
  3202. #define case_n(a, rd) \
  3203. case a: \
  3204. while (n_frames--) { \
  3205. int sz = rd; \
  3206. idx += a; \
  3207. if (sz > size) { \
  3208. av_log(avctx, AV_LOG_ERROR, \
  3209. "Superframe packet size too big: %d > %d\n", \
  3210. sz, size); \
  3211. return AVERROR_INVALIDDATA; \
  3212. } \
  3213. res = vp9_decode_frame(avctx, frame, got_frame, \
  3214. data, sz); \
  3215. if (res < 0) \
  3216. return res; \
  3217. data += sz; \
  3218. size -= sz; \
  3219. } \
  3220. break;
  3221. case_n(1, *idx);
  3222. case_n(2, AV_RL16(idx));
  3223. case_n(3, AV_RL24(idx));
  3224. case_n(4, AV_RL32(idx));
  3225. }
  3226. return avpkt->size;
  3227. }
  3228. }
  3229. // if we get here, there was no valid superframe index, i.e. this is just
  3230. // one whole single frame - decode it as such from the complete input buf
  3231. if ((res = vp9_decode_frame(avctx, frame, got_frame, data, size)) < 0)
  3232. return res;
  3233. return avpkt->size;
  3234. }
  3235. static void vp9_decode_flush(AVCodecContext *ctx)
  3236. {
  3237. VP9Context *s = ctx->priv_data;
  3238. int i;
  3239. for (i = 0; i < 8; i++)
  3240. av_frame_unref(s->refs[i]);
  3241. s->f = NULL;
  3242. }
  3243. static av_cold int vp9_decode_init(AVCodecContext *ctx)
  3244. {
  3245. VP9Context *s = ctx->priv_data;
  3246. int i;
  3247. ctx->pix_fmt = AV_PIX_FMT_YUV420P;
  3248. ff_vp9dsp_init(&s->dsp);
  3249. ff_videodsp_init(&s->vdsp, 8);
  3250. for (i = 0; i < 8; i++) {
  3251. s->refs[i] = av_frame_alloc();
  3252. if (!s->refs[i]) {
  3253. vp9_decode_free(ctx);
  3254. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
  3255. return AVERROR(ENOMEM);
  3256. }
  3257. }
  3258. s->filter.sharpness = -1;
  3259. return 0;
  3260. }
  3261. AVCodec ff_vp9_decoder = {
  3262. .name = "vp9",
  3263. .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
  3264. .type = AVMEDIA_TYPE_VIDEO,
  3265. .id = AV_CODEC_ID_VP9,
  3266. .priv_data_size = sizeof(VP9Context),
  3267. .init = vp9_decode_init,
  3268. .close = vp9_decode_free,
  3269. .decode = vp9_decode_packet,
  3270. .capabilities = CODEC_CAP_DR1,
  3271. .flush = vp9_decode_flush,
  3272. };