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.

3585 lines
144KB

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