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.

3579 lines
143KB

  1. /*
  2. * VP9 compatible video decoder
  3. *
  4. * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. * Copyright (C) 2013 Clément Bœsch <u pkh me>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "avcodec.h"
  24. #include "get_bits.h"
  25. #include "internal.h"
  26. #include "videodsp.h"
  27. #include "vp56.h"
  28. #include "vp9.h"
  29. #include "vp9data.h"
  30. #include "vp9dsp.h"
  31. #include "libavutil/avassert.h"
  32. #define VP9_SYNCCODE 0x498342
  33. enum CompPredMode {
  34. PRED_SINGLEREF,
  35. PRED_COMPREF,
  36. PRED_SWITCHABLE,
  37. };
  38. enum BlockLevel {
  39. BL_64X64,
  40. BL_32X32,
  41. BL_16X16,
  42. BL_8X8,
  43. };
  44. enum BlockSize {
  45. BS_64x64,
  46. BS_64x32,
  47. BS_32x64,
  48. BS_32x32,
  49. BS_32x16,
  50. BS_16x32,
  51. BS_16x16,
  52. BS_16x8,
  53. BS_8x16,
  54. BS_8x8,
  55. BS_8x4,
  56. BS_4x8,
  57. BS_4x4,
  58. N_BS_SIZES,
  59. };
  60. struct VP9mvrefPair {
  61. VP56mv mv[2];
  62. int8_t ref[2];
  63. };
  64. struct VP9Filter {
  65. uint8_t level[8 * 8];
  66. uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
  67. [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
  68. };
  69. typedef struct VP9Block {
  70. uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
  71. enum FilterMode filter;
  72. VP56mv mv[4 /* b_idx */][2 /* ref */];
  73. enum BlockSize bs;
  74. enum TxfmMode tx, uvtx;
  75. int row, row7, col, col7;
  76. uint8_t *dst[3];
  77. ptrdiff_t y_stride, uv_stride;
  78. } VP9Block;
  79. typedef struct VP9Context {
  80. VP9DSPContext dsp;
  81. VideoDSPContext vdsp;
  82. GetBitContext gb;
  83. VP56RangeCoder c;
  84. VP56RangeCoder *c_b;
  85. unsigned c_b_size;
  86. VP9Block b;
  87. // bitstream header
  88. uint8_t profile;
  89. uint8_t keyframe, last_keyframe;
  90. uint8_t invisible, 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_free(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. RETURN_SCALE_MV(mv->mv[1], s->signbias[mv->ref[1]] != s->signbias[ref]);
  912. }
  913. }
  914. }
  915. // MV at this position in previous frame, using different reference frame
  916. if (s->use_last_frame_mvs) {
  917. struct VP9mvrefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
  918. if (mv->ref[0] != ref && mv->ref[0] >= 0) {
  919. RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
  920. }
  921. if (mv->ref[1] != ref && mv->ref[1] >= 0) {
  922. RETURN_SCALE_MV(mv->mv[1], s->signbias[mv->ref[1]] != s->signbias[ref]);
  923. }
  924. }
  925. AV_ZERO32(pmv);
  926. #undef INVALID_MV
  927. #undef RETURN_MV
  928. #undef RETURN_SCALE_MV
  929. }
  930. static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
  931. {
  932. int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
  933. int n, c = vp8_rac_get_tree(&s->c, vp9_mv_class_tree,
  934. s->prob.p.mv_comp[idx].classes);
  935. s->counts.mv_comp[idx].sign[sign]++;
  936. s->counts.mv_comp[idx].classes[c]++;
  937. if (c) {
  938. int m;
  939. for (n = 0, m = 0; m < c; m++) {
  940. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
  941. n |= bit << m;
  942. s->counts.mv_comp[idx].bits[m][bit]++;
  943. }
  944. n <<= 3;
  945. bit = vp8_rac_get_tree(&s->c, vp9_mv_fp_tree, s->prob.p.mv_comp[idx].fp);
  946. n |= bit << 1;
  947. s->counts.mv_comp[idx].fp[bit]++;
  948. if (hp) {
  949. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
  950. s->counts.mv_comp[idx].hp[bit]++;
  951. n |= bit;
  952. } else {
  953. n |= 1;
  954. // bug in libvpx - we count for bw entropy purposes even if the
  955. // bit wasn't coded
  956. s->counts.mv_comp[idx].hp[1]++;
  957. }
  958. n += 8 << c;
  959. } else {
  960. n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
  961. s->counts.mv_comp[idx].class0[n]++;
  962. bit = vp8_rac_get_tree(&s->c, vp9_mv_fp_tree,
  963. s->prob.p.mv_comp[idx].class0_fp[n]);
  964. s->counts.mv_comp[idx].class0_fp[n][bit]++;
  965. n = (n << 3) | (bit << 1);
  966. if (hp) {
  967. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
  968. s->counts.mv_comp[idx].class0_hp[bit]++;
  969. n |= bit;
  970. } else {
  971. n |= 1;
  972. // bug in libvpx - we count for bw entropy purposes even if the
  973. // bit wasn't coded
  974. s->counts.mv_comp[idx].class0_hp[1]++;
  975. }
  976. }
  977. return sign ? -(n + 1) : (n + 1);
  978. }
  979. static void fill_mv(VP9Context *s,
  980. VP56mv *mv, int mode, int sb)
  981. {
  982. VP9Block *const b = &s->b;
  983. if (mode == ZEROMV) {
  984. memset(mv, 0, sizeof(*mv) * 2);
  985. } else {
  986. int hp;
  987. // FIXME cache this value and reuse for other subblocks
  988. find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
  989. mode == NEWMV ? -1 : sb);
  990. // FIXME maybe move this code into find_ref_mvs()
  991. if ((mode == NEWMV || sb == -1) &&
  992. !(hp = s->highprecisionmvs && abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
  993. if (mv[0].y & 1) {
  994. if (mv[0].y < 0)
  995. mv[0].y++;
  996. else
  997. mv[0].y--;
  998. }
  999. if (mv[0].x & 1) {
  1000. if (mv[0].x < 0)
  1001. mv[0].x++;
  1002. else
  1003. mv[0].x--;
  1004. }
  1005. }
  1006. if (mode == NEWMV) {
  1007. enum MVJoint j = vp8_rac_get_tree(&s->c, vp9_mv_joint_tree,
  1008. s->prob.p.mv_joint);
  1009. s->counts.mv_joint[j]++;
  1010. if (j >= MV_JOINT_V)
  1011. mv[0].y += read_mv_component(s, 0, hp);
  1012. if (j & 1)
  1013. mv[0].x += read_mv_component(s, 1, hp);
  1014. }
  1015. if (b->comp) {
  1016. // FIXME cache this value and reuse for other subblocks
  1017. find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
  1018. mode == NEWMV ? -1 : sb);
  1019. if ((mode == NEWMV || sb == -1) &&
  1020. !(hp = s->highprecisionmvs && abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
  1021. if (mv[1].y & 1) {
  1022. if (mv[1].y < 0)
  1023. mv[1].y++;
  1024. else
  1025. mv[1].y--;
  1026. }
  1027. if (mv[1].x & 1) {
  1028. if (mv[1].x < 0)
  1029. mv[1].x++;
  1030. else
  1031. mv[1].x--;
  1032. }
  1033. }
  1034. if (mode == NEWMV) {
  1035. enum MVJoint j = vp8_rac_get_tree(&s->c, vp9_mv_joint_tree,
  1036. s->prob.p.mv_joint);
  1037. s->counts.mv_joint[j]++;
  1038. if (j >= MV_JOINT_V)
  1039. mv[1].y += read_mv_component(s, 0, hp);
  1040. if (j & 1)
  1041. mv[1].x += read_mv_component(s, 1, hp);
  1042. }
  1043. }
  1044. }
  1045. }
  1046. static void decode_mode(AVCodecContext *ctx)
  1047. {
  1048. static const uint8_t left_ctx[N_BS_SIZES] = {
  1049. 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
  1050. };
  1051. static const uint8_t above_ctx[N_BS_SIZES] = {
  1052. 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
  1053. };
  1054. static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
  1055. TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
  1056. TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
  1057. };
  1058. VP9Context *s = ctx->priv_data;
  1059. VP9Block *const b = &s->b;
  1060. int row = b->row, col = b->col, row7 = b->row7;
  1061. enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
  1062. int w4 = FFMIN(s->cols - col, bwh_tab[1][b->bs][0]);
  1063. int h4 = FFMIN(s->rows - row, bwh_tab[1][b->bs][1]), y;
  1064. int have_a = row > 0, have_l = col > s->tiling.tile_col_start;
  1065. if (!s->segmentation.enabled) {
  1066. b->seg_id = 0;
  1067. } else if (s->keyframe || s->intraonly) {
  1068. b->seg_id = s->segmentation.update_map ?
  1069. vp8_rac_get_tree(&s->c, vp9_segmentation_tree, s->prob.seg) : 0;
  1070. } else if (!s->segmentation.update_map ||
  1071. (s->segmentation.temporal &&
  1072. vp56_rac_get_prob_branchy(&s->c,
  1073. s->prob.segpred[s->above_segpred_ctx[col] +
  1074. s->left_segpred_ctx[row7]]))) {
  1075. int pred = 8, x;
  1076. for (y = 0; y < h4; y++)
  1077. for (x = 0; x < w4; x++)
  1078. pred = FFMIN(pred, s->segmentation_map[(y + row) * 8 * s->sb_cols + x + col]);
  1079. av_assert1(pred < 8);
  1080. b->seg_id = pred;
  1081. memset(&s->above_segpred_ctx[col], 1, w4);
  1082. memset(&s->left_segpred_ctx[row7], 1, h4);
  1083. } else {
  1084. b->seg_id = vp8_rac_get_tree(&s->c, vp9_segmentation_tree,
  1085. s->prob.seg);
  1086. memset(&s->above_segpred_ctx[col], 0, w4);
  1087. memset(&s->left_segpred_ctx[row7], 0, h4);
  1088. }
  1089. if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
  1090. for (y = 0; y < h4; y++)
  1091. memset(&s->segmentation_map[(y + row) * 8 * s->sb_cols + col],
  1092. b->seg_id, w4);
  1093. }
  1094. b->skip = s->segmentation.enabled &&
  1095. s->segmentation.feat[b->seg_id].skip_enabled;
  1096. if (!b->skip) {
  1097. int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
  1098. b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
  1099. s->counts.skip[c][b->skip]++;
  1100. }
  1101. if (s->keyframe || s->intraonly) {
  1102. b->intra = 1;
  1103. } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
  1104. b->intra = !s->segmentation.feat[b->seg_id].ref_val;
  1105. } else {
  1106. int c, bit;
  1107. if (have_a && have_l) {
  1108. c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
  1109. c += (c == 2);
  1110. } else {
  1111. c = have_a ? 2 * s->above_intra_ctx[col] :
  1112. have_l ? 2 * s->left_intra_ctx[row7] : 0;
  1113. }
  1114. bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
  1115. s->counts.intra[c][bit]++;
  1116. b->intra = !bit;
  1117. }
  1118. if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
  1119. int c;
  1120. if (have_a) {
  1121. if (have_l) {
  1122. c = (s->above_skip_ctx[col] ? max_tx :
  1123. s->above_txfm_ctx[col]) +
  1124. (s->left_skip_ctx[row7] ? max_tx :
  1125. s->left_txfm_ctx[row7]) > max_tx;
  1126. } else {
  1127. c = s->above_skip_ctx[col] ? 1 :
  1128. (s->above_txfm_ctx[col] * 2 > max_tx);
  1129. }
  1130. } else if (have_l) {
  1131. c = s->left_skip_ctx[row7] ? 1 :
  1132. (s->left_txfm_ctx[row7] * 2 > max_tx);
  1133. } else {
  1134. c = 1;
  1135. }
  1136. switch (max_tx) {
  1137. case TX_32X32:
  1138. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
  1139. if (b->tx) {
  1140. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
  1141. if (b->tx == 2)
  1142. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
  1143. }
  1144. s->counts.tx32p[c][b->tx]++;
  1145. break;
  1146. case TX_16X16:
  1147. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
  1148. if (b->tx)
  1149. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
  1150. s->counts.tx16p[c][b->tx]++;
  1151. break;
  1152. case TX_8X8:
  1153. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
  1154. s->counts.tx8p[c][b->tx]++;
  1155. break;
  1156. case TX_4X4:
  1157. b->tx = TX_4X4;
  1158. break;
  1159. }
  1160. } else {
  1161. b->tx = FFMIN(max_tx, s->txfmmode);
  1162. }
  1163. if (s->keyframe || s->intraonly) {
  1164. uint8_t *a = &s->above_mode_ctx[col * 2];
  1165. uint8_t *l = &s->left_mode_ctx[(row7) << 1];
  1166. b->comp = 0;
  1167. if (b->bs > BS_8x8) {
  1168. // FIXME the memory storage intermediates here aren't really
  1169. // necessary, they're just there to make the code slightly
  1170. // simpler for now
  1171. b->mode[0] = a[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1172. vp9_default_kf_ymode_probs[a[0]][l[0]]);
  1173. if (b->bs != BS_8x4) {
  1174. b->mode[1] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1175. vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
  1176. l[0] = a[1] = b->mode[1];
  1177. } else {
  1178. l[0] = a[1] = b->mode[1] = b->mode[0];
  1179. }
  1180. if (b->bs != BS_4x8) {
  1181. b->mode[2] = a[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1182. vp9_default_kf_ymode_probs[a[0]][l[1]]);
  1183. if (b->bs != BS_8x4) {
  1184. b->mode[3] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1185. vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
  1186. l[1] = a[1] = b->mode[3];
  1187. } else {
  1188. l[1] = a[1] = b->mode[3] = b->mode[2];
  1189. }
  1190. } else {
  1191. b->mode[2] = b->mode[0];
  1192. l[1] = a[1] = b->mode[3] = b->mode[1];
  1193. }
  1194. } else {
  1195. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1196. vp9_default_kf_ymode_probs[*a][*l]);
  1197. b->mode[3] = b->mode[2] = b->mode[1] = b->mode[0];
  1198. // FIXME this can probably be optimized
  1199. memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
  1200. memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
  1201. }
  1202. b->uvmode = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1203. vp9_default_kf_uvmode_probs[b->mode[3]]);
  1204. } else if (b->intra) {
  1205. b->comp = 0;
  1206. if (b->bs > BS_8x8) {
  1207. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1208. s->prob.p.y_mode[0]);
  1209. s->counts.y_mode[0][b->mode[0]]++;
  1210. if (b->bs != BS_8x4) {
  1211. b->mode[1] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1212. s->prob.p.y_mode[0]);
  1213. s->counts.y_mode[0][b->mode[1]]++;
  1214. } else {
  1215. b->mode[1] = b->mode[0];
  1216. }
  1217. if (b->bs != BS_4x8) {
  1218. b->mode[2] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1219. s->prob.p.y_mode[0]);
  1220. s->counts.y_mode[0][b->mode[2]]++;
  1221. if (b->bs != BS_8x4) {
  1222. b->mode[3] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1223. s->prob.p.y_mode[0]);
  1224. s->counts.y_mode[0][b->mode[3]]++;
  1225. } else {
  1226. b->mode[3] = b->mode[2];
  1227. }
  1228. } else {
  1229. b->mode[2] = b->mode[0];
  1230. b->mode[3] = b->mode[1];
  1231. }
  1232. } else {
  1233. static const uint8_t size_group[10] = {
  1234. 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
  1235. };
  1236. int sz = size_group[b->bs];
  1237. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1238. s->prob.p.y_mode[sz]);
  1239. b->mode[1] = b->mode[2] = b->mode[3] = b->mode[0];
  1240. s->counts.y_mode[sz][b->mode[3]]++;
  1241. }
  1242. b->uvmode = vp8_rac_get_tree(&s->c, vp9_intramode_tree,
  1243. s->prob.p.uv_mode[b->mode[3]]);
  1244. s->counts.uv_mode[b->mode[3]][b->uvmode]++;
  1245. } else {
  1246. static const uint8_t inter_mode_ctx_lut[14][14] = {
  1247. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1248. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1249. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1250. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1251. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  1252. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  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. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  1258. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  1259. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
  1260. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
  1261. };
  1262. if (s->segmentation.feat[b->seg_id].ref_enabled) {
  1263. av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
  1264. b->comp = 0;
  1265. b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
  1266. } else {
  1267. // read comp_pred flag
  1268. if (s->comppredmode != PRED_SWITCHABLE) {
  1269. b->comp = s->comppredmode == PRED_COMPREF;
  1270. } else {
  1271. int c;
  1272. // FIXME add intra as ref=0xff (or -1) to make these easier?
  1273. if (have_a) {
  1274. if (have_l) {
  1275. if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
  1276. c = 4;
  1277. } else if (s->above_comp_ctx[col]) {
  1278. c = 2 + (s->left_intra_ctx[row7] ||
  1279. s->left_ref_ctx[row7] == s->fixcompref);
  1280. } else if (s->left_comp_ctx[row7]) {
  1281. c = 2 + (s->above_intra_ctx[col] ||
  1282. s->above_ref_ctx[col] == s->fixcompref);
  1283. } else {
  1284. c = (!s->above_intra_ctx[col] &&
  1285. s->above_ref_ctx[col] == s->fixcompref) ^
  1286. (!s->left_intra_ctx[row7] &&
  1287. s->left_ref_ctx[row & 7] == s->fixcompref);
  1288. }
  1289. } else {
  1290. c = s->above_comp_ctx[col] ? 3 :
  1291. (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
  1292. }
  1293. } else if (have_l) {
  1294. c = s->left_comp_ctx[row7] ? 3 :
  1295. (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
  1296. } else {
  1297. c = 1;
  1298. }
  1299. b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
  1300. s->counts.comp[c][b->comp]++;
  1301. }
  1302. // read actual references
  1303. // FIXME probably cache a few variables here to prevent repetitive
  1304. // memory accesses below
  1305. if (b->comp) /* two references */ {
  1306. int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
  1307. b->ref[fix_idx] = s->fixcompref;
  1308. // FIXME can this codeblob be replaced by some sort of LUT?
  1309. if (have_a) {
  1310. if (have_l) {
  1311. if (s->above_intra_ctx[col]) {
  1312. if (s->left_intra_ctx[row7]) {
  1313. c = 2;
  1314. } else {
  1315. c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  1316. }
  1317. } else if (s->left_intra_ctx[row7]) {
  1318. c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
  1319. } else {
  1320. int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
  1321. if (refl == refa && refa == s->varcompref[1]) {
  1322. c = 0;
  1323. } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
  1324. if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
  1325. (refl == s->fixcompref && refa == s->varcompref[0])) {
  1326. c = 4;
  1327. } else {
  1328. c = (refa == refl) ? 3 : 1;
  1329. }
  1330. } else if (!s->left_comp_ctx[row7]) {
  1331. if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
  1332. c = 1;
  1333. } else {
  1334. c = (refl == s->varcompref[1] &&
  1335. refa != s->varcompref[1]) ? 2 : 4;
  1336. }
  1337. } else if (!s->above_comp_ctx[col]) {
  1338. if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
  1339. c = 1;
  1340. } else {
  1341. c = (refa == s->varcompref[1] &&
  1342. refl != s->varcompref[1]) ? 2 : 4;
  1343. }
  1344. } else {
  1345. c = (refl == refa) ? 4 : 2;
  1346. }
  1347. }
  1348. } else {
  1349. if (s->above_intra_ctx[col]) {
  1350. c = 2;
  1351. } else if (s->above_comp_ctx[col]) {
  1352. c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
  1353. } else {
  1354. c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
  1355. }
  1356. }
  1357. } else if (have_l) {
  1358. if (s->left_intra_ctx[row7]) {
  1359. c = 2;
  1360. } else if (s->left_comp_ctx[row7]) {
  1361. c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  1362. } else {
  1363. c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  1364. }
  1365. } else {
  1366. c = 2;
  1367. }
  1368. bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
  1369. b->ref[var_idx] = s->varcompref[bit];
  1370. s->counts.comp_ref[c][bit]++;
  1371. } else /* single reference */ {
  1372. int bit, c;
  1373. if (have_a && !s->above_intra_ctx[col]) {
  1374. if (have_l && !s->left_intra_ctx[row7]) {
  1375. if (s->left_comp_ctx[row7]) {
  1376. if (s->above_comp_ctx[col]) {
  1377. c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
  1378. !s->above_ref_ctx[col]);
  1379. } else {
  1380. c = (3 * !s->above_ref_ctx[col]) +
  1381. (!s->fixcompref || !s->left_ref_ctx[row7]);
  1382. }
  1383. } else if (s->above_comp_ctx[col]) {
  1384. c = (3 * !s->left_ref_ctx[row7]) +
  1385. (!s->fixcompref || !s->above_ref_ctx[col]);
  1386. } else {
  1387. c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
  1388. }
  1389. } else if (s->above_intra_ctx[col]) {
  1390. c = 2;
  1391. } else if (s->above_comp_ctx[col]) {
  1392. c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
  1393. } else {
  1394. c = 4 * (!s->above_ref_ctx[col]);
  1395. }
  1396. } else if (have_l && !s->left_intra_ctx[row7]) {
  1397. if (s->left_intra_ctx[row7]) {
  1398. c = 2;
  1399. } else if (s->left_comp_ctx[row7]) {
  1400. c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
  1401. } else {
  1402. c = 4 * (!s->left_ref_ctx[row7]);
  1403. }
  1404. } else {
  1405. c = 2;
  1406. }
  1407. bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
  1408. s->counts.single_ref[c][0][bit]++;
  1409. if (!bit) {
  1410. b->ref[0] = 0;
  1411. } else {
  1412. // FIXME can this codeblob be replaced by some sort of LUT?
  1413. if (have_a) {
  1414. if (have_l) {
  1415. if (s->left_intra_ctx[row7]) {
  1416. if (s->above_intra_ctx[col]) {
  1417. c = 2;
  1418. } else if (s->above_comp_ctx[col]) {
  1419. c = 1 + 2 * (s->fixcompref == 1 ||
  1420. s->above_ref_ctx[col] == 1);
  1421. } else if (!s->above_ref_ctx[col]) {
  1422. c = 3;
  1423. } else {
  1424. c = 4 * (s->above_ref_ctx[col] == 1);
  1425. }
  1426. } else if (s->above_intra_ctx[col]) {
  1427. if (s->left_intra_ctx[row7]) {
  1428. c = 2;
  1429. } else if (s->left_comp_ctx[row7]) {
  1430. c = 1 + 2 * (s->fixcompref == 1 ||
  1431. s->left_ref_ctx[row7] == 1);
  1432. } else if (!s->left_ref_ctx[row7]) {
  1433. c = 3;
  1434. } else {
  1435. c = 4 * (s->left_ref_ctx[row7] == 1);
  1436. }
  1437. } else if (s->above_comp_ctx[col]) {
  1438. if (s->left_comp_ctx[row7]) {
  1439. if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
  1440. c = 3 * (s->fixcompref == 1 ||
  1441. s->left_ref_ctx[row7] == 1);
  1442. } else {
  1443. c = 2;
  1444. }
  1445. } else if (!s->left_ref_ctx[row7]) {
  1446. c = 1 + 2 * (s->fixcompref == 1 ||
  1447. s->above_ref_ctx[col] == 1);
  1448. } else {
  1449. c = 3 * (s->left_ref_ctx[row7] == 1) +
  1450. (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
  1451. }
  1452. } else if (s->left_comp_ctx[row7]) {
  1453. if (!s->above_ref_ctx[col]) {
  1454. c = 1 + 2 * (s->fixcompref == 1 ||
  1455. s->left_ref_ctx[row7] == 1);
  1456. } else {
  1457. c = 3 * (s->above_ref_ctx[col] == 1) +
  1458. (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
  1459. }
  1460. } else if (!s->above_ref_ctx[col]) {
  1461. if (!s->left_ref_ctx[row7]) {
  1462. c = 3;
  1463. } else {
  1464. c = 4 * (s->left_ref_ctx[row7] == 1);
  1465. }
  1466. } else if (!s->left_ref_ctx[row7]) {
  1467. c = 4 * (s->above_ref_ctx[col] == 1);
  1468. } else {
  1469. c = 2 * (s->left_ref_ctx[row7] == 1) +
  1470. 2 * (s->above_ref_ctx[col] == 1);
  1471. }
  1472. } else {
  1473. if (s->above_intra_ctx[col] ||
  1474. (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
  1475. c = 2;
  1476. } else if (s->above_comp_ctx[col]) {
  1477. c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
  1478. } else {
  1479. c = 4 * (s->above_ref_ctx[col] == 1);
  1480. }
  1481. }
  1482. } else if (have_l) {
  1483. if (s->left_intra_ctx[row7] ||
  1484. (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
  1485. c = 2;
  1486. } else if (s->left_comp_ctx[row7]) {
  1487. c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
  1488. } else {
  1489. c = 4 * (s->left_ref_ctx[row7] == 1);
  1490. }
  1491. } else {
  1492. c = 2;
  1493. }
  1494. bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
  1495. s->counts.single_ref[c][1][bit]++;
  1496. b->ref[0] = 1 + bit;
  1497. }
  1498. }
  1499. }
  1500. if (b->bs <= BS_8x8) {
  1501. if (s->segmentation.feat[b->seg_id].skip_enabled) {
  1502. b->mode[0] = b->mode[1] = b->mode[2] = b->mode[3] = ZEROMV;
  1503. } else {
  1504. static const uint8_t off[10] = {
  1505. 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
  1506. };
  1507. // FIXME this needs to use the LUT tables from find_ref_mvs
  1508. // because not all are -1,0/0,-1
  1509. int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
  1510. [s->left_mode_ctx[row7 + off[b->bs]]];
  1511. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1512. s->prob.p.mv_mode[c]);
  1513. b->mode[1] = b->mode[2] = b->mode[3] = b->mode[0];
  1514. s->counts.mv_mode[c][b->mode[0] - 10]++;
  1515. }
  1516. }
  1517. if (s->filtermode == FILTER_SWITCHABLE) {
  1518. int c;
  1519. if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
  1520. if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
  1521. c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
  1522. s->left_filter_ctx[row7] : 3;
  1523. } else {
  1524. c = s->above_filter_ctx[col];
  1525. }
  1526. } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
  1527. c = s->left_filter_ctx[row7];
  1528. } else {
  1529. c = 3;
  1530. }
  1531. b->filter = vp8_rac_get_tree(&s->c, vp9_filter_tree,
  1532. s->prob.p.filter[c]);
  1533. s->counts.filter[c][b->filter]++;
  1534. } else {
  1535. b->filter = s->filtermode;
  1536. }
  1537. if (b->bs > BS_8x8) {
  1538. int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
  1539. b->mode[0] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1540. s->prob.p.mv_mode[c]);
  1541. s->counts.mv_mode[c][b->mode[0] - 10]++;
  1542. fill_mv(s, b->mv[0], b->mode[0], 0);
  1543. if (b->bs != BS_8x4) {
  1544. b->mode[1] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1545. s->prob.p.mv_mode[c]);
  1546. s->counts.mv_mode[c][b->mode[1] - 10]++;
  1547. fill_mv(s, b->mv[1], b->mode[1], 1);
  1548. } else {
  1549. b->mode[1] = b->mode[0];
  1550. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  1551. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  1552. }
  1553. if (b->bs != BS_4x8) {
  1554. b->mode[2] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1555. s->prob.p.mv_mode[c]);
  1556. s->counts.mv_mode[c][b->mode[2] - 10]++;
  1557. fill_mv(s, b->mv[2], b->mode[2], 2);
  1558. if (b->bs != BS_8x4) {
  1559. b->mode[3] = vp8_rac_get_tree(&s->c, vp9_inter_mode_tree,
  1560. s->prob.p.mv_mode[c]);
  1561. s->counts.mv_mode[c][b->mode[3] - 10]++;
  1562. fill_mv(s, b->mv[3], b->mode[3], 3);
  1563. } else {
  1564. b->mode[3] = b->mode[2];
  1565. AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
  1566. AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
  1567. }
  1568. } else {
  1569. b->mode[2] = b->mode[0];
  1570. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  1571. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  1572. b->mode[3] = b->mode[1];
  1573. AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
  1574. AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
  1575. }
  1576. } else {
  1577. fill_mv(s, b->mv[0], b->mode[0], -1);
  1578. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  1579. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  1580. AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
  1581. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  1582. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  1583. AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
  1584. }
  1585. }
  1586. // FIXME this can probably be optimized
  1587. memset(&s->above_skip_ctx[col], b->skip, w4);
  1588. memset(&s->left_skip_ctx[row7], b->skip, h4);
  1589. memset(&s->above_txfm_ctx[col], b->tx, w4);
  1590. memset(&s->left_txfm_ctx[row7], b->tx, h4);
  1591. memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
  1592. memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
  1593. if (!s->keyframe && !s->intraonly) {
  1594. memset(&s->above_intra_ctx[col], b->intra, w4);
  1595. memset(&s->left_intra_ctx[row7], b->intra, h4);
  1596. memset(&s->above_comp_ctx[col], b->comp, w4);
  1597. memset(&s->left_comp_ctx[row7], b->comp, h4);
  1598. memset(&s->above_mode_ctx[col], b->mode[3], w4);
  1599. memset(&s->left_mode_ctx[row7], b->mode[3], h4);
  1600. if (s->filtermode == FILTER_SWITCHABLE && !b->intra ) {
  1601. memset(&s->above_filter_ctx[col], b->filter, w4);
  1602. memset(&s->left_filter_ctx[row7], b->filter, h4);
  1603. b->filter = vp9_filter_lut[b->filter];
  1604. }
  1605. if (b->bs > BS_8x8) {
  1606. int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  1607. AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
  1608. AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
  1609. AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
  1610. AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
  1611. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
  1612. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
  1613. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
  1614. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
  1615. } else {
  1616. int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  1617. for (n = 0; n < w4 * 2; n++) {
  1618. AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
  1619. AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
  1620. }
  1621. for (n = 0; n < h4 * 2; n++) {
  1622. AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
  1623. AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
  1624. }
  1625. }
  1626. if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
  1627. // as a direct check in above branches
  1628. int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
  1629. memset(&s->above_ref_ctx[col], vref, w4);
  1630. memset(&s->left_ref_ctx[row7], vref, h4);
  1631. }
  1632. }
  1633. // FIXME kinda ugly
  1634. for (y = 0; y < h4; y++) {
  1635. int x, o = (row + y) * s->sb_cols * 8 + col;
  1636. if (b->intra) {
  1637. for (x = 0; x < w4; x++) {
  1638. s->mv[0][o + x].ref[0] =
  1639. s->mv[0][o + x].ref[1] = -1;
  1640. }
  1641. } else if (b->comp) {
  1642. for (x = 0; x < w4; x++) {
  1643. s->mv[0][o + x].ref[0] = b->ref[0];
  1644. s->mv[0][o + x].ref[1] = b->ref[1];
  1645. AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
  1646. AV_COPY32(&s->mv[0][o + x].mv[1], &b->mv[3][1]);
  1647. }
  1648. } else {
  1649. for (x = 0; x < w4; x++) {
  1650. s->mv[0][o + x].ref[0] = b->ref[0];
  1651. s->mv[0][o + x].ref[1] = -1;
  1652. AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
  1653. }
  1654. }
  1655. }
  1656. }
  1657. // FIXME remove tx argument, and merge cnt/eob arguments?
  1658. static int decode_coeffs_b(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
  1659. enum TxfmMode tx, unsigned (*cnt)[6][3],
  1660. unsigned (*eob)[6][2], uint8_t (*p)[6][11],
  1661. int nnz, const int16_t *scan, const int16_t (*nb)[2],
  1662. const int16_t *band_counts, const int16_t *qmul)
  1663. {
  1664. int i = 0, band = 0, band_left = band_counts[band];
  1665. uint8_t *tp = p[0][nnz];
  1666. uint8_t cache[1024];
  1667. do {
  1668. int val, rc;
  1669. val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
  1670. eob[band][nnz][val]++;
  1671. if (!val)
  1672. break;
  1673. skip_eob:
  1674. if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
  1675. cnt[band][nnz][0]++;
  1676. if (!--band_left)
  1677. band_left = band_counts[++band];
  1678. cache[scan[i]] = 0;
  1679. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  1680. tp = p[band][nnz];
  1681. if (++i == n_coeffs)
  1682. break; //invalid input; blocks should end with EOB
  1683. goto skip_eob;
  1684. }
  1685. rc = scan[i];
  1686. if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
  1687. cnt[band][nnz][1]++;
  1688. val = 1;
  1689. cache[rc] = 1;
  1690. } else {
  1691. // fill in p[3-10] (model fill) - only once per frame for each pos
  1692. if (!tp[3])
  1693. memcpy(&tp[3], vp9_model_pareto8[tp[2]], 8);
  1694. cnt[band][nnz][2]++;
  1695. if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
  1696. if (!vp56_rac_get_prob_branchy(c, tp[4])) {
  1697. cache[rc] = val = 2;
  1698. } else {
  1699. val = 3 + vp56_rac_get_prob(c, tp[5]);
  1700. cache[rc] = 3;
  1701. }
  1702. } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
  1703. cache[rc] = 4;
  1704. if (!vp56_rac_get_prob_branchy(c, tp[7])) {
  1705. val = 5 + vp56_rac_get_prob(c, 159);
  1706. } else {
  1707. val = 7 + (vp56_rac_get_prob(c, 165) << 1) +
  1708. vp56_rac_get_prob(c, 145);
  1709. }
  1710. } else { // cat 3-6
  1711. cache[rc] = 5;
  1712. if (!vp56_rac_get_prob_branchy(c, tp[8])) {
  1713. if (!vp56_rac_get_prob_branchy(c, tp[9])) {
  1714. val = 11 + (vp56_rac_get_prob(c, 173) << 2) +
  1715. (vp56_rac_get_prob(c, 148) << 1) +
  1716. vp56_rac_get_prob(c, 140);
  1717. } else {
  1718. val = 19 + (vp56_rac_get_prob(c, 176) << 3) +
  1719. (vp56_rac_get_prob(c, 155) << 2) +
  1720. (vp56_rac_get_prob(c, 140) << 1) +
  1721. vp56_rac_get_prob(c, 135);
  1722. }
  1723. } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
  1724. val = 35 + (vp56_rac_get_prob(c, 180) << 4) +
  1725. (vp56_rac_get_prob(c, 157) << 3) +
  1726. (vp56_rac_get_prob(c, 141) << 2) +
  1727. (vp56_rac_get_prob(c, 134) << 1) +
  1728. vp56_rac_get_prob(c, 130);
  1729. } else {
  1730. val = 67 + (vp56_rac_get_prob(c, 254) << 13) +
  1731. (vp56_rac_get_prob(c, 254) << 12) +
  1732. (vp56_rac_get_prob(c, 254) << 11) +
  1733. (vp56_rac_get_prob(c, 252) << 10) +
  1734. (vp56_rac_get_prob(c, 249) << 9) +
  1735. (vp56_rac_get_prob(c, 243) << 8) +
  1736. (vp56_rac_get_prob(c, 230) << 7) +
  1737. (vp56_rac_get_prob(c, 196) << 6) +
  1738. (vp56_rac_get_prob(c, 177) << 5) +
  1739. (vp56_rac_get_prob(c, 153) << 4) +
  1740. (vp56_rac_get_prob(c, 140) << 3) +
  1741. (vp56_rac_get_prob(c, 133) << 2) +
  1742. (vp56_rac_get_prob(c, 130) << 1) +
  1743. vp56_rac_get_prob(c, 129);
  1744. }
  1745. }
  1746. }
  1747. if (!--band_left)
  1748. band_left = band_counts[++band];
  1749. if (tx == TX_32X32) // FIXME slow
  1750. coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
  1751. else
  1752. coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
  1753. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  1754. tp = p[band][nnz];
  1755. } while (++i < n_coeffs);
  1756. return i;
  1757. }
  1758. static int decode_coeffs(AVCodecContext *ctx)
  1759. {
  1760. VP9Context *s = ctx->priv_data;
  1761. VP9Block *const b = &s->b;
  1762. int row = b->row, col = b->col;
  1763. uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
  1764. unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
  1765. unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
  1766. int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
  1767. int end_x = FFMIN(2 * (s->cols - col), w4);
  1768. int end_y = FFMIN(2 * (s->rows - row), h4);
  1769. int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
  1770. int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), res;
  1771. int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
  1772. int tx = 4 * s->lossless + b->tx;
  1773. const int16_t **yscans = vp9_scans[tx];
  1774. const int16_t (**ynbs)[2] = vp9_scans_nb[tx];
  1775. const int16_t *uvscan = vp9_scans[b->uvtx][DCT_DCT];
  1776. const int16_t (*uvnb)[2] = vp9_scans_nb[b->uvtx][DCT_DCT];
  1777. uint8_t *a = &s->above_y_nnz_ctx[col * 2];
  1778. uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
  1779. static const int16_t band_counts[4][8] = {
  1780. { 1, 2, 3, 4, 3, 16 - 13 },
  1781. { 1, 2, 3, 4, 11, 64 - 21 },
  1782. { 1, 2, 3, 4, 11, 256 - 21 },
  1783. { 1, 2, 3, 4, 11, 1024 - 21 },
  1784. };
  1785. const int16_t *y_band_counts = band_counts[b->tx];
  1786. const int16_t *uv_band_counts = band_counts[b->uvtx];
  1787. /* y tokens */
  1788. if (b->tx > TX_4X4) { // FIXME slow
  1789. for (y = 0; y < end_y; y += step1d)
  1790. for (x = 1; x < step1d; x++)
  1791. l[y] |= l[y + x];
  1792. for (x = 0; x < end_x; x += step1d)
  1793. for (y = 1; y < step1d; y++)
  1794. a[x] |= a[x + y];
  1795. }
  1796. for (n = 0, y = 0; y < end_y; y += step1d) {
  1797. for (x = 0; x < end_x; x += step1d, n += step) {
  1798. enum TxfmType txtp = vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
  1799. b->bs > BS_8x8 ?
  1800. n : 0]];
  1801. int nnz = a[x] + l[y];
  1802. if ((res = decode_coeffs_b(&s->c, s->block + 16 * n, 16 * step,
  1803. b->tx, c, e, p, nnz, yscans[txtp],
  1804. ynbs[txtp], y_band_counts, qmul[0])) < 0)
  1805. return res;
  1806. a[x] = l[y] = !!res;
  1807. if (b->tx > TX_8X8) {
  1808. AV_WN16A(&s->eob[n], res);
  1809. } else {
  1810. s->eob[n] = res;
  1811. }
  1812. }
  1813. }
  1814. if (b->tx > TX_4X4) { // FIXME slow
  1815. for (y = 0; y < end_y; y += step1d)
  1816. memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
  1817. for (x = 0; x < end_x; x += step1d)
  1818. memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
  1819. }
  1820. p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
  1821. c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
  1822. e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
  1823. w4 >>= 1;
  1824. h4 >>= 1;
  1825. end_x >>= 1;
  1826. end_y >>= 1;
  1827. for (pl = 0; pl < 2; pl++) {
  1828. a = &s->above_uv_nnz_ctx[pl][col];
  1829. l = &s->left_uv_nnz_ctx[pl][row & 7];
  1830. if (b->uvtx > TX_4X4) { // FIXME slow
  1831. for (y = 0; y < end_y; y += uvstep1d)
  1832. for (x = 1; x < uvstep1d; x++)
  1833. l[y] |= l[y + x];
  1834. for (x = 0; x < end_x; x += uvstep1d)
  1835. for (y = 1; y < uvstep1d; y++)
  1836. a[x] |= a[x + y];
  1837. }
  1838. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  1839. for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
  1840. int nnz = a[x] + l[y];
  1841. if ((res = decode_coeffs_b(&s->c, s->uvblock[pl] + 16 * n,
  1842. 16 * uvstep, b->uvtx, c, e, p, nnz,
  1843. uvscan, uvnb, uv_band_counts,
  1844. qmul[1])) < 0)
  1845. return res;
  1846. a[x] = l[y] = !!res;
  1847. if (b->uvtx > TX_8X8) {
  1848. AV_WN16A(&s->uveob[pl][n], res);
  1849. } else {
  1850. s->uveob[pl][n] = res;
  1851. }
  1852. }
  1853. }
  1854. if (b->uvtx > TX_4X4) { // FIXME slow
  1855. for (y = 0; y < end_y; y += uvstep1d)
  1856. memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
  1857. for (x = 0; x < end_x; x += uvstep1d)
  1858. memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
  1859. }
  1860. }
  1861. return 0;
  1862. }
  1863. static av_always_inline int check_intra_mode(VP9Context *s, int mode, uint8_t **a,
  1864. uint8_t *dst_edge, ptrdiff_t stride_edge,
  1865. uint8_t *dst_inner, ptrdiff_t stride_inner,
  1866. uint8_t *l, int col, int x, int w,
  1867. int row, int y, enum TxfmMode tx,
  1868. int p)
  1869. {
  1870. int have_top = row > 0 || y > 0;
  1871. int have_left = col > s->tiling.tile_col_start || x > 0;
  1872. int have_right = x < w - 1;
  1873. static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
  1874. [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
  1875. { DC_127_PRED, VERT_PRED } },
  1876. [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
  1877. { HOR_PRED, HOR_PRED } },
  1878. [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
  1879. { LEFT_DC_PRED, DC_PRED } },
  1880. [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
  1881. { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
  1882. [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
  1883. { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
  1884. [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
  1885. { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
  1886. [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
  1887. { HOR_DOWN_PRED, HOR_DOWN_PRED } },
  1888. [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
  1889. { DC_127_PRED, VERT_LEFT_PRED } },
  1890. [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
  1891. { HOR_UP_PRED, HOR_UP_PRED } },
  1892. [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
  1893. { HOR_PRED, TM_VP8_PRED } },
  1894. };
  1895. static const struct {
  1896. uint8_t needs_left:1;
  1897. uint8_t needs_top:1;
  1898. uint8_t needs_topleft:1;
  1899. uint8_t needs_topright:1;
  1900. } edges[N_INTRA_PRED_MODES] = {
  1901. [VERT_PRED] = { .needs_top = 1 },
  1902. [HOR_PRED] = { .needs_left = 1 },
  1903. [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
  1904. [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
  1905. [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1906. [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1907. [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1908. [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
  1909. [HOR_UP_PRED] = { .needs_left = 1 },
  1910. [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
  1911. [LEFT_DC_PRED] = { .needs_left = 1 },
  1912. [TOP_DC_PRED] = { .needs_top = 1 },
  1913. [DC_128_PRED] = { 0 },
  1914. [DC_127_PRED] = { 0 },
  1915. [DC_129_PRED] = { 0 }
  1916. };
  1917. av_assert2(mode >= 0 && mode < 10);
  1918. mode = mode_conv[mode][have_left][have_top];
  1919. if (edges[mode].needs_top) {
  1920. uint8_t *top, *topleft;
  1921. int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
  1922. int n_px_need_tr = 0;
  1923. if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
  1924. n_px_need_tr = 4;
  1925. // if top of sb64-row, use s->intra_pred_data[] instead of
  1926. // dst[-stride] for intra prediction (it contains pre- instead of
  1927. // post-loopfilter data)
  1928. if (have_top) {
  1929. top = !(row & 7) && !y ?
  1930. s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
  1931. y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
  1932. if (have_left)
  1933. topleft = !(row & 7) && !y ?
  1934. s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
  1935. y == 0 || x == 0 ? &dst_edge[-stride_edge] :
  1936. &dst_inner[-stride_inner];
  1937. }
  1938. if (have_top &&
  1939. (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
  1940. (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
  1941. n_px_need + n_px_need_tr <= n_px_have) {
  1942. *a = top;
  1943. } else {
  1944. if (have_top) {
  1945. if (n_px_need <= n_px_have) {
  1946. memcpy(*a, top, n_px_need);
  1947. } else {
  1948. memcpy(*a, top, n_px_have);
  1949. memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
  1950. n_px_need - n_px_have);
  1951. }
  1952. } else {
  1953. memset(*a, 127, n_px_need);
  1954. }
  1955. if (edges[mode].needs_topleft) {
  1956. if (have_left && have_top) {
  1957. (*a)[-1] = topleft[-1];
  1958. } else {
  1959. (*a)[-1] = have_top ? 129 : 127;
  1960. }
  1961. }
  1962. if (tx == TX_4X4 && edges[mode].needs_topright) {
  1963. if (have_top && have_right &&
  1964. n_px_need + n_px_need_tr <= n_px_have) {
  1965. memcpy(&(*a)[4], &top[4], 4);
  1966. } else {
  1967. memset(&(*a)[4], (*a)[3], 4);
  1968. }
  1969. }
  1970. }
  1971. }
  1972. if (edges[mode].needs_left) {
  1973. if (have_left) {
  1974. int n_px_need = 4 << tx, i, n_px_have = (((s->rows - row) << !p) - y) * 4;
  1975. uint8_t *dst = x == 0 ? dst_edge : dst_inner;
  1976. ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
  1977. if (n_px_need <= n_px_have) {
  1978. for (i = 0; i < n_px_need; i++)
  1979. l[i] = dst[i * stride - 1];
  1980. } else {
  1981. for (i = 0; i < n_px_have; i++)
  1982. l[i] = dst[i * stride - 1];
  1983. memset(&l[i], l[i - 1], n_px_need - n_px_have);
  1984. }
  1985. } else {
  1986. memset(l, 129, 4 << tx);
  1987. }
  1988. }
  1989. return mode;
  1990. }
  1991. static void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
  1992. {
  1993. VP9Context *s = ctx->priv_data;
  1994. VP9Block *const b = &s->b;
  1995. int row = b->row, col = b->col;
  1996. int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
  1997. int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
  1998. int end_x = FFMIN(2 * (s->cols - col), w4);
  1999. int end_y = FFMIN(2 * (s->rows - row), h4);
  2000. int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
  2001. int uvstep1d = 1 << b->uvtx, p;
  2002. uint8_t *dst = b->dst[0], *dst_r = s->f->data[0] + y_off;
  2003. for (n = 0, y = 0; y < end_y; y += step1d) {
  2004. uint8_t *ptr = dst, *ptr_r = dst_r;
  2005. for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d,
  2006. ptr_r += 4 * step1d, n += step) {
  2007. int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
  2008. y * 2 + x : 0];
  2009. LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
  2010. uint8_t *a = &a_buf[16], l[32];
  2011. enum TxfmType txtp = vp9_intra_txfm_type[mode];
  2012. int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
  2013. mode = check_intra_mode(s, mode, &a, ptr_r, s->f->linesize[0],
  2014. ptr, b->y_stride, l,
  2015. col, x, w4, row, y, b->tx, 0);
  2016. s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
  2017. if (eob)
  2018. s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
  2019. s->block + 16 * n, eob);
  2020. }
  2021. dst_r += 4 * s->f->linesize[0] * step1d;
  2022. dst += 4 * b->y_stride * step1d;
  2023. }
  2024. // U/V
  2025. h4 >>= 1;
  2026. w4 >>= 1;
  2027. end_x >>= 1;
  2028. end_y >>= 1;
  2029. step = 1 << (b->uvtx * 2);
  2030. for (p = 0; p < 2; p++) {
  2031. dst = b->dst[1 + p];
  2032. dst_r = s->f->data[1 + p] + uv_off;
  2033. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  2034. uint8_t *ptr = dst, *ptr_r = dst_r;
  2035. for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d,
  2036. ptr_r += 4 * uvstep1d, n += step) {
  2037. int mode = b->uvmode;
  2038. LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
  2039. uint8_t *a = &a_buf[16], l[32];
  2040. int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
  2041. mode = check_intra_mode(s, mode, &a, ptr_r, s->f->linesize[1],
  2042. ptr, b->uv_stride, l,
  2043. col, x, w4, row, y, b->uvtx, p + 1);
  2044. s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
  2045. if (eob)
  2046. s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
  2047. s->uvblock[p] + 16 * n, eob);
  2048. }
  2049. dst_r += 4 * uvstep1d * s->f->linesize[1];
  2050. dst += 4 * uvstep1d * b->uv_stride;
  2051. }
  2052. }
  2053. }
  2054. static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func (*mc)[2],
  2055. uint8_t *dst, ptrdiff_t dst_stride,
  2056. const uint8_t *ref, ptrdiff_t ref_stride,
  2057. ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
  2058. int bw, int bh, int w, int h)
  2059. {
  2060. int mx = mv->x, my = mv->y;
  2061. y += my >> 3;
  2062. x += mx >> 3;
  2063. ref += y * ref_stride + x;
  2064. mx &= 7;
  2065. my &= 7;
  2066. // FIXME bilinear filter only needs 0/1 pixels, not 3/4
  2067. if (x < !!mx * 3 || y < !!my * 3 ||
  2068. x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
  2069. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, 80,
  2070. ref - !!my * 3 * ref_stride - !!mx * 3,
  2071. ref_stride,
  2072. bw + !!mx * 7, bh + !!my * 7,
  2073. x - !!mx * 3, y - !!my * 3, w, h);
  2074. ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  2075. ref_stride = 80;
  2076. }
  2077. mc[!!mx][!!my](dst, dst_stride, ref, ref_stride, bh, mx << 1, my << 1);
  2078. }
  2079. static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func (*mc)[2],
  2080. uint8_t *dst_u, uint8_t *dst_v,
  2081. ptrdiff_t dst_stride,
  2082. const uint8_t *ref_u, ptrdiff_t src_stride_u,
  2083. const uint8_t *ref_v, ptrdiff_t src_stride_v,
  2084. ptrdiff_t y, ptrdiff_t x, const VP56mv *mv,
  2085. int bw, int bh, int w, int h)
  2086. {
  2087. int mx = mv->x, my = mv->y;
  2088. y += my >> 4;
  2089. x += mx >> 4;
  2090. ref_u += y * src_stride_u + x;
  2091. ref_v += y * src_stride_v + x;
  2092. mx &= 15;
  2093. my &= 15;
  2094. // FIXME bilinear filter only needs 0/1 pixels, not 3/4
  2095. if (x < !!mx * 3 || y < !!my * 3 ||
  2096. x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
  2097. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, 80,
  2098. ref_u - !!my * 3 * src_stride_u - !!mx * 3, src_stride_u,
  2099. bw + !!mx * 7, bh + !!my * 7,
  2100. x - !!mx * 3, y - !!my * 3, w, h);
  2101. ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  2102. mc[!!mx][!!my](dst_u, dst_stride, ref_u, 80, bh, mx, my);
  2103. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, 80,
  2104. ref_v - !!my * 3 * src_stride_v - !!mx * 3, src_stride_v,
  2105. bw + !!mx * 7, bh + !!my * 7,
  2106. x - !!mx * 3, y - !!my * 3, w, h);
  2107. ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  2108. mc[!!mx][!!my](dst_v, dst_stride, ref_v, 80, bh, mx, my);
  2109. } else {
  2110. mc[!!mx][!!my](dst_u, dst_stride, ref_u, src_stride_u, bh, mx, my);
  2111. mc[!!mx][!!my](dst_v, dst_stride, ref_v, src_stride_v, bh, mx, my);
  2112. }
  2113. }
  2114. static void inter_recon(AVCodecContext *ctx)
  2115. {
  2116. static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
  2117. { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
  2118. { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
  2119. };
  2120. VP9Context *s = ctx->priv_data;
  2121. VP9Block *const b = &s->b;
  2122. int row = b->row, col = b->col;
  2123. AVFrame *ref1 = s->refs[s->refidx[b->ref[0]]];
  2124. AVFrame *ref2 = b->comp ? s->refs[s->refidx[b->ref[1]]] : NULL;
  2125. int w = ctx->width, h = ctx->height;
  2126. ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
  2127. // y inter pred
  2128. if (b->bs > BS_8x8) {
  2129. if (b->bs == BS_8x4) {
  2130. mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
  2131. ref1->data[0], ref1->linesize[0],
  2132. row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
  2133. mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
  2134. b->dst[0] + 4 * ls_y, ls_y,
  2135. ref1->data[0], ref1->linesize[0],
  2136. (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
  2137. if (b->comp) {
  2138. mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
  2139. ref2->data[0], ref2->linesize[0],
  2140. row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
  2141. mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
  2142. b->dst[0] + 4 * ls_y, ls_y,
  2143. ref2->data[0], ref2->linesize[0],
  2144. (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
  2145. }
  2146. } else if (b->bs == BS_4x8) {
  2147. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
  2148. ref1->data[0], ref1->linesize[0],
  2149. row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
  2150. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
  2151. ref1->data[0], ref1->linesize[0],
  2152. row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
  2153. if (b->comp) {
  2154. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
  2155. ref2->data[0], ref2->linesize[0],
  2156. row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
  2157. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
  2158. ref2->data[0], ref2->linesize[0],
  2159. row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
  2160. }
  2161. } else {
  2162. av_assert2(b->bs == BS_4x4);
  2163. // FIXME if two horizontally adjacent blocks have the same MV,
  2164. // do a w8 instead of a w4 call
  2165. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
  2166. ref1->data[0], ref1->linesize[0],
  2167. row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
  2168. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
  2169. ref1->data[0], ref1->linesize[0],
  2170. row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
  2171. mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
  2172. b->dst[0] + 4 * ls_y, ls_y,
  2173. ref1->data[0], ref1->linesize[0],
  2174. (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
  2175. mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
  2176. b->dst[0] + 4 * ls_y + 4, ls_y,
  2177. ref1->data[0], ref1->linesize[0],
  2178. (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
  2179. if (b->comp) {
  2180. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
  2181. ref2->data[0], ref2->linesize[0],
  2182. row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
  2183. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
  2184. ref2->data[0], ref2->linesize[0],
  2185. row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
  2186. mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
  2187. b->dst[0] + 4 * ls_y, ls_y,
  2188. ref2->data[0], ref2->linesize[0],
  2189. (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
  2190. mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
  2191. b->dst[0] + 4 * ls_y + 4, ls_y,
  2192. ref2->data[0], ref2->linesize[0],
  2193. (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
  2194. }
  2195. }
  2196. } else {
  2197. int bwl = bwlog_tab[0][b->bs];
  2198. int bw = bwh_tab[0][b->bs][0] * 4, bh = bwh_tab[0][b->bs][1] * 4;
  2199. mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
  2200. ref1->data[0], ref1->linesize[0],
  2201. row << 3, col << 3, &b->mv[0][0],bw, bh, w, h);
  2202. if (b->comp)
  2203. mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
  2204. ref2->data[0], ref2->linesize[0],
  2205. row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
  2206. }
  2207. // uv inter pred
  2208. {
  2209. int bwl = bwlog_tab[1][b->bs];
  2210. int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
  2211. VP56mv mvuv;
  2212. w = (w + 1) >> 1;
  2213. h = (h + 1) >> 1;
  2214. if (b->bs > BS_8x8) {
  2215. 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);
  2216. 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);
  2217. } else {
  2218. mvuv = b->mv[0][0];
  2219. }
  2220. mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
  2221. b->dst[1], b->dst[2], ls_uv,
  2222. ref1->data[1], ref1->linesize[1],
  2223. ref1->data[2], ref1->linesize[2],
  2224. row << 2, col << 2, &mvuv, bw, bh, w, h);
  2225. if (b->comp) {
  2226. if (b->bs > BS_8x8) {
  2227. 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);
  2228. 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);
  2229. } else {
  2230. mvuv = b->mv[0][1];
  2231. }
  2232. mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
  2233. b->dst[1], b->dst[2], ls_uv,
  2234. ref2->data[1], ref2->linesize[1],
  2235. ref2->data[2], ref2->linesize[2],
  2236. row << 2, col << 2, &mvuv, bw, bh, w, h);
  2237. }
  2238. }
  2239. if (!b->skip) {
  2240. /* mostly copied intra_reconn() */
  2241. int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
  2242. int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
  2243. int end_x = FFMIN(2 * (s->cols - col), w4);
  2244. int end_y = FFMIN(2 * (s->rows - row), h4);
  2245. int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
  2246. int uvstep1d = 1 << b->uvtx, p;
  2247. uint8_t *dst = b->dst[0];
  2248. // y itxfm add
  2249. for (n = 0, y = 0; y < end_y; y += step1d) {
  2250. uint8_t *ptr = dst;
  2251. for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
  2252. int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
  2253. if (eob)
  2254. s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
  2255. s->block + 16 * n, eob);
  2256. }
  2257. dst += 4 * b->y_stride * step1d;
  2258. }
  2259. // uv itxfm add
  2260. h4 >>= 1;
  2261. w4 >>= 1;
  2262. end_x >>= 1;
  2263. end_y >>= 1;
  2264. step = 1 << (b->uvtx * 2);
  2265. for (p = 0; p < 2; p++) {
  2266. dst = b->dst[p + 1];
  2267. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  2268. uint8_t *ptr = dst;
  2269. for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
  2270. int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
  2271. if (eob)
  2272. s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
  2273. s->uvblock[p] + 16 * n, eob);
  2274. }
  2275. dst += 4 * uvstep1d * b->uv_stride;
  2276. }
  2277. }
  2278. }
  2279. }
  2280. static av_always_inline void mask_edges(struct VP9Filter *lflvl, int is_uv,
  2281. int row_and_7, int col_and_7,
  2282. int w, int h, int col_end, int row_end,
  2283. enum TxfmMode tx, int skip_inter)
  2284. {
  2285. // FIXME I'm pretty sure all loops can be replaced by a single LUT if
  2286. // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
  2287. // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
  2288. // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
  2289. // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
  2290. // edges. This means that for UV, we work on two subsampled blocks at
  2291. // a time, and we only use the topleft block's mode information to set
  2292. // things like block strength. Thus, for any block size smaller than
  2293. // 16x16, ignore the odd portion of the block.
  2294. if (tx == TX_4X4 && is_uv) {
  2295. if (h == 1) {
  2296. if (row_and_7 & 1)
  2297. return;
  2298. if (!row_end)
  2299. h += 1;
  2300. }
  2301. if (w == 1) {
  2302. if (col_and_7 & 1)
  2303. return;
  2304. if (!col_end)
  2305. w += 1;
  2306. }
  2307. }
  2308. if (tx == TX_4X4 && !skip_inter) {
  2309. int t = 1 << col_and_7, m_col = (t << w) - t, y;
  2310. int m_col_odd = (t << (w - 1)) - t;
  2311. // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
  2312. if (is_uv) {
  2313. int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
  2314. for (y = row_and_7; y < h + row_and_7; y++) {
  2315. int col_mask_id = 2 - !(y & 7);
  2316. lflvl->mask[is_uv][0][y][1] |= m_row_8;
  2317. lflvl->mask[is_uv][0][y][2] |= m_row_4;
  2318. // for odd lines, if the odd col is not being filtered,
  2319. // skip odd row also:
  2320. // .---. <-- a
  2321. // | |
  2322. // |___| <-- b
  2323. // ^ ^
  2324. // c d
  2325. //
  2326. // if a/c are even row/col and b/d are odd, and d is skipped,
  2327. // e.g. right edge of size-66x66.webm, then skip b also (bug)
  2328. if ((col_end & 1) && (y & 1)) {
  2329. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
  2330. } else {
  2331. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
  2332. }
  2333. }
  2334. } else {
  2335. int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
  2336. for (y = row_and_7; y < h + row_and_7; y++) {
  2337. int col_mask_id = 2 - !(y & 3);
  2338. lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
  2339. lflvl->mask[is_uv][0][y][2] |= m_row_4;
  2340. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
  2341. lflvl->mask[is_uv][0][y][3] |= m_col;
  2342. lflvl->mask[is_uv][1][y][3] |= m_col;
  2343. }
  2344. }
  2345. } else {
  2346. int y, t = 1 << col_and_7, m_col = (t << w) - t;
  2347. if (!skip_inter) {
  2348. int mask_id = (tx == TX_8X8);
  2349. int l2 = tx + is_uv - 1, step1d = 1 << l2;
  2350. static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
  2351. int m_row = m_col & masks[l2];
  2352. // at odd UV col/row edges tx16/tx32 loopfilter edges, force
  2353. // 8wd loopfilter to prevent going off the visible edge.
  2354. if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
  2355. int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
  2356. int m_row_8 = m_row - m_row_16;
  2357. for (y = row_and_7; y < h + row_and_7; y++) {
  2358. lflvl->mask[is_uv][0][y][0] |= m_row_16;
  2359. lflvl->mask[is_uv][0][y][1] |= m_row_8;
  2360. }
  2361. } else {
  2362. for (y = row_and_7; y < h + row_and_7; y++)
  2363. lflvl->mask[is_uv][0][y][mask_id] |= m_row;
  2364. }
  2365. if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
  2366. for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
  2367. lflvl->mask[is_uv][1][y][0] |= m_col;
  2368. if (y - row_and_7 == h - 1)
  2369. lflvl->mask[is_uv][1][y][1] |= m_col;
  2370. } else {
  2371. for (y = row_and_7; y < h + row_and_7; y += step1d)
  2372. lflvl->mask[is_uv][1][y][mask_id] |= m_col;
  2373. }
  2374. } else if (tx != TX_4X4) {
  2375. int mask_id;
  2376. mask_id = (tx == TX_8X8) || (is_uv && h == 1);
  2377. lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
  2378. mask_id = (tx == TX_8X8) || (is_uv && w == 1);
  2379. for (y = row_and_7; y < h + row_and_7; y++)
  2380. lflvl->mask[is_uv][0][y][mask_id] |= t;
  2381. } else if (is_uv) {
  2382. int t8 = t & 0x01, t4 = t - t8;
  2383. for (y = row_and_7; y < h + row_and_7; y++) {
  2384. lflvl->mask[is_uv][0][y][2] |= t4;
  2385. lflvl->mask[is_uv][0][y][1] |= t8;
  2386. }
  2387. lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
  2388. } else {
  2389. int t8 = t & 0x11, t4 = t - t8;
  2390. for (y = row_and_7; y < h + row_and_7; y++) {
  2391. lflvl->mask[is_uv][0][y][2] |= t4;
  2392. lflvl->mask[is_uv][0][y][1] |= t8;
  2393. }
  2394. lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
  2395. }
  2396. }
  2397. }
  2398. static int decode_b(AVCodecContext *ctx, int row, int col,
  2399. struct VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  2400. enum BlockLevel bl, enum BlockPartition bp)
  2401. {
  2402. VP9Context *s = ctx->priv_data;
  2403. VP9Block *const b = &s->b;
  2404. enum BlockSize bs = bl * 3 + bp;
  2405. int res, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
  2406. int emu[2];
  2407. b->row = row;
  2408. b->row7 = row & 7;
  2409. b->col = col;
  2410. b->col7 = col & 7;
  2411. s->min_mv.x = -(128 + col * 64);
  2412. s->min_mv.y = -(128 + row * 64);
  2413. s->max_mv.x = 128 + (s->cols - col - w4) * 64;
  2414. s->max_mv.y = 128 + (s->rows - row - h4) * 64;
  2415. b->bs = bs;
  2416. decode_mode(ctx);
  2417. b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
  2418. if (!b->skip) {
  2419. if ((res = decode_coeffs(ctx)) < 0)
  2420. return res;
  2421. } else {
  2422. int pl;
  2423. memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
  2424. memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
  2425. for (pl = 0; pl < 2; pl++) {
  2426. memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
  2427. memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
  2428. }
  2429. }
  2430. // emulated overhangs if the stride of the target buffer can't hold. This
  2431. // allows to support emu-edge and so on even if we have large block
  2432. // overhangs
  2433. emu[0] = (col + w4) * 8 > s->f->linesize[0] ||
  2434. (row + h4) > s->rows + 2 * !(ctx->flags & CODEC_FLAG_EMU_EDGE);
  2435. emu[1] = (col + w4) * 4 > s->f->linesize[1] ||
  2436. (row + h4) > s->rows + 2 * !(ctx->flags & CODEC_FLAG_EMU_EDGE);
  2437. if (emu[0]) {
  2438. b->dst[0] = s->tmp_y;
  2439. b->y_stride = 64;
  2440. } else {
  2441. b->dst[0] = s->f->data[0] + yoff;
  2442. b->y_stride = s->f->linesize[0];
  2443. }
  2444. if (emu[1]) {
  2445. b->dst[1] = s->tmp_uv[0];
  2446. b->dst[2] = s->tmp_uv[1];
  2447. b->uv_stride = 32;
  2448. } else {
  2449. b->dst[1] = s->f->data[1] + uvoff;
  2450. b->dst[2] = s->f->data[2] + uvoff;
  2451. b->uv_stride = s->f->linesize[1];
  2452. }
  2453. if (b->intra) {
  2454. intra_recon(ctx, yoff, uvoff);
  2455. } else {
  2456. inter_recon(ctx);
  2457. }
  2458. if (emu[0]) {
  2459. int w = FFMIN(s->cols - col, w4) * 8, h = FFMIN(s->rows - row, h4) * 8, n, o = 0;
  2460. for (n = 0; o < w; n++) {
  2461. int bw = 64 >> n;
  2462. av_assert2(n <= 4);
  2463. if (w & bw) {
  2464. s->dsp.mc[n][0][0][0][0](s->f->data[0] + yoff + o, s->f->linesize[0],
  2465. s->tmp_y + o, 64, h, 0, 0);
  2466. o += bw;
  2467. }
  2468. }
  2469. }
  2470. if (emu[1]) {
  2471. int w = FFMIN(s->cols - col, w4) * 4, h = FFMIN(s->rows - row, h4) * 4, n, o = 0;
  2472. for (n = 1; o < w; n++) {
  2473. int bw = 64 >> n;
  2474. av_assert2(n <= 4);
  2475. if (w & bw) {
  2476. s->dsp.mc[n][0][0][0][0](s->f->data[1] + uvoff + o, s->f->linesize[1],
  2477. s->tmp_uv[0] + o, 32, h, 0, 0);
  2478. s->dsp.mc[n][0][0][0][0](s->f->data[2] + uvoff + o, s->f->linesize[2],
  2479. s->tmp_uv[1] + o, 32, h, 0, 0);
  2480. o += bw;
  2481. }
  2482. }
  2483. }
  2484. // pick filter level and find edges to apply filter to
  2485. if (s->filter.level &&
  2486. (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
  2487. [b->mode[3] != ZEROMV]) > 0) {
  2488. int x_end = FFMIN(s->cols - col, w4), y_end = FFMIN(s->rows - row, h4);
  2489. int skip_inter = !b->intra && b->skip;
  2490. for (y = 0; y < h4; y++)
  2491. memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
  2492. mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
  2493. mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
  2494. s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
  2495. s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
  2496. b->uvtx, skip_inter);
  2497. if (!s->filter.lim_lut[lvl]) {
  2498. int sharp = s->filter.sharpness;
  2499. int limit = lvl;
  2500. if (sharp > 0) {
  2501. limit >>= (sharp + 3) >> 2;
  2502. limit = FFMIN(limit, 9 - sharp);
  2503. }
  2504. limit = FFMAX(limit, 1);
  2505. s->filter.lim_lut[lvl] = limit;
  2506. s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
  2507. }
  2508. }
  2509. return 0;
  2510. }
  2511. static int decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lflvl,
  2512. ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
  2513. {
  2514. VP9Context *s = ctx->priv_data;
  2515. int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
  2516. (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1), res;
  2517. const uint8_t *p = s->keyframe ? vp9_default_kf_partition_probs[bl][c] :
  2518. s->prob.p.partition[bl][c];
  2519. enum BlockPartition bp;
  2520. ptrdiff_t hbs = 4 >> bl;
  2521. if (bl == BL_8X8) {
  2522. bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
  2523. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2524. } else if (col + hbs < s->cols) {
  2525. if (row + hbs < s->rows) {
  2526. bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
  2527. switch (bp) {
  2528. case PARTITION_NONE:
  2529. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2530. break;
  2531. case PARTITION_H:
  2532. if (!(res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp))) {
  2533. yoff += hbs * 8 * s->f->linesize[0];
  2534. uvoff += hbs * 4 * s->f->linesize[1];
  2535. res = decode_b(ctx, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
  2536. }
  2537. break;
  2538. case PARTITION_V:
  2539. if (!(res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp))) {
  2540. yoff += hbs * 8;
  2541. uvoff += hbs * 4;
  2542. res = decode_b(ctx, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
  2543. }
  2544. break;
  2545. case PARTITION_SPLIT:
  2546. if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1))) {
  2547. if (!(res = decode_sb(ctx, row, col + hbs, lflvl,
  2548. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1))) {
  2549. yoff += hbs * 8 * s->f->linesize[0];
  2550. uvoff += hbs * 4 * s->f->linesize[1];
  2551. if (!(res = decode_sb(ctx, row + hbs, col, lflvl,
  2552. yoff, uvoff, bl + 1)))
  2553. res = decode_sb(ctx, row + hbs, col + hbs, lflvl,
  2554. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
  2555. }
  2556. }
  2557. break;
  2558. }
  2559. } else if (vp56_rac_get_prob_branchy(&s->c, p[1])) {
  2560. bp = PARTITION_SPLIT;
  2561. if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1)))
  2562. res = decode_sb(ctx, row, col + hbs, lflvl,
  2563. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
  2564. } else {
  2565. bp = PARTITION_H;
  2566. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2567. }
  2568. } else if (row + hbs < s->rows) {
  2569. if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
  2570. bp = PARTITION_SPLIT;
  2571. if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1))) {
  2572. yoff += hbs * 8 * s->f->linesize[0];
  2573. uvoff += hbs * 4 * s->f->linesize[1];
  2574. res = decode_sb(ctx, row + hbs, col, lflvl,
  2575. yoff, uvoff, bl + 1);
  2576. }
  2577. } else {
  2578. bp = PARTITION_V;
  2579. res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp);
  2580. }
  2581. } else {
  2582. bp = PARTITION_SPLIT;
  2583. res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1);
  2584. }
  2585. s->counts.partition[bl][c][bp]++;
  2586. return res;
  2587. }
  2588. static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
  2589. int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
  2590. {
  2591. VP9Context *s = ctx->priv_data;
  2592. uint8_t *dst = s->f->data[0] + yoff, *lvl = lflvl->level;
  2593. ptrdiff_t ls_y = s->f->linesize[0], ls_uv = s->f->linesize[1];
  2594. int y, x, p;
  2595. // FIXME in how far can we interleave the v/h loopfilter calls? E.g.
  2596. // if you think of them as acting on a 8x8 block max, we can interleave
  2597. // each v/h within the single x loop, but that only works if we work on
  2598. // 8 pixel blocks, and we won't always do that (we want at least 16px
  2599. // to use SSE2 optimizations, perhaps 32 for AVX2)
  2600. // filter edges between columns, Y plane (e.g. block1 | block2)
  2601. for (y = 0; y < 8; y += 2, dst += 16 * ls_y, lvl += 16) {
  2602. uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[0][0][y];
  2603. uint8_t *hmask2 = lflvl->mask[0][0][y + 1];
  2604. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
  2605. unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
  2606. unsigned hm = hm1 | hm2 | hm13 | hm23;
  2607. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8, l++) {
  2608. if (hm1 & x) {
  2609. int L = *l, H = L >> 4;
  2610. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2611. if (col || x > 1) {
  2612. if (hmask1[0] & x) {
  2613. if (hmask2[0] & x) {
  2614. av_assert2(l[8] == L);
  2615. s->dsp.loop_filter_16[0](ptr, ls_y, E, I, H);
  2616. } else {
  2617. s->dsp.loop_filter_8[2][0](ptr, ls_y, E, I, H);
  2618. }
  2619. } else if (hm2 & x) {
  2620. L = l[8];
  2621. H |= (L >> 4) << 8;
  2622. E |= s->filter.mblim_lut[L] << 8;
  2623. I |= s->filter.lim_lut[L] << 8;
  2624. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  2625. [!!(hmask2[1] & x)]
  2626. [0](ptr, ls_y, E, I, H);
  2627. } else {
  2628. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  2629. [0](ptr, ls_y, E, I, H);
  2630. }
  2631. }
  2632. } else if (hm2 & x) {
  2633. int L = l[8], H = L >> 4;
  2634. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2635. if (col || x > 1) {
  2636. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  2637. [0](ptr + 8 * ls_y, ls_y, E, I, H);
  2638. }
  2639. }
  2640. if (hm13 & x) {
  2641. int L = *l, H = L >> 4;
  2642. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2643. if (hm23 & x) {
  2644. L = l[8];
  2645. H |= (L >> 4) << 8;
  2646. E |= s->filter.mblim_lut[L] << 8;
  2647. I |= s->filter.lim_lut[L] << 8;
  2648. s->dsp.loop_filter_mix2[0][0][0](ptr + 4, ls_y, E, I, H);
  2649. } else {
  2650. s->dsp.loop_filter_8[0][0](ptr + 4, ls_y, E, I, H);
  2651. }
  2652. } else if (hm23 & x) {
  2653. int L = l[8], H = L >> 4;
  2654. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2655. s->dsp.loop_filter_8[0][0](ptr + 8 * ls_y + 4, ls_y, E, I, H);
  2656. }
  2657. }
  2658. }
  2659. // block1
  2660. // filter edges between rows, Y plane (e.g. ------)
  2661. // block2
  2662. dst = s->f->data[0] + yoff;
  2663. lvl = lflvl->level;
  2664. for (y = 0; y < 8; y++, dst += 8 * ls_y, lvl += 8) {
  2665. uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[0][1][y];
  2666. unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
  2667. for (x = 1; vm & ~(x - 1); x <<= 2, ptr += 16, l += 2) {
  2668. if (row || y) {
  2669. if (vm & x) {
  2670. int L = *l, H = L >> 4;
  2671. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2672. if (vmask[0] & x) {
  2673. if (vmask[0] & (x << 1)) {
  2674. av_assert2(l[1] == L);
  2675. s->dsp.loop_filter_16[1](ptr, ls_y, E, I, H);
  2676. } else {
  2677. s->dsp.loop_filter_8[2][1](ptr, ls_y, E, I, H);
  2678. }
  2679. } else if (vm & (x << 1)) {
  2680. L = l[1];
  2681. H |= (L >> 4) << 8;
  2682. E |= s->filter.mblim_lut[L] << 8;
  2683. I |= s->filter.lim_lut[L] << 8;
  2684. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  2685. [!!(vmask[1] & (x << 1))]
  2686. [1](ptr, ls_y, E, I, H);
  2687. } else {
  2688. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  2689. [1](ptr, ls_y, E, I, H);
  2690. }
  2691. } else if (vm & (x << 1)) {
  2692. int L = l[1], H = L >> 4;
  2693. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2694. s->dsp.loop_filter_8[!!(vmask[1] & (x << 1))]
  2695. [1](ptr + 8, ls_y, E, I, H);
  2696. }
  2697. }
  2698. if (vm3 & x) {
  2699. int L = *l, H = L >> 4;
  2700. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2701. if (vm3 & (x << 1)) {
  2702. L = l[1];
  2703. H |= (L >> 4) << 8;
  2704. E |= s->filter.mblim_lut[L] << 8;
  2705. I |= s->filter.lim_lut[L] << 8;
  2706. s->dsp.loop_filter_mix2[0][0][1](ptr + ls_y * 4, ls_y, E, I, H);
  2707. } else {
  2708. s->dsp.loop_filter_8[0][1](ptr + ls_y * 4, ls_y, E, I, H);
  2709. }
  2710. } else if (vm3 & (x << 1)) {
  2711. int L = l[1], H = L >> 4;
  2712. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2713. s->dsp.loop_filter_8[0][1](ptr + ls_y * 4 + 8, ls_y, E, I, H);
  2714. }
  2715. }
  2716. }
  2717. // same principle but for U/V planes
  2718. for (p = 0; p < 2; p++) {
  2719. lvl = lflvl->level;
  2720. dst = s->f->data[1 + p] + uvoff;
  2721. for (y = 0; y < 8; y += 4, dst += 16 * ls_uv, lvl += 32) {
  2722. uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[1][0][y];
  2723. uint8_t *hmask2 = lflvl->mask[1][0][y + 2];
  2724. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2];
  2725. unsigned hm2 = hmask2[1] | hmask2[2], hm = hm1 | hm2;
  2726. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 4) {
  2727. if (col || x > 1) {
  2728. if (hm1 & x) {
  2729. int L = *l, H = L >> 4;
  2730. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2731. if (hmask1[0] & x) {
  2732. if (hmask2[0] & x) {
  2733. av_assert2(l[16] == L);
  2734. s->dsp.loop_filter_16[0](ptr, ls_uv, E, I, H);
  2735. } else {
  2736. s->dsp.loop_filter_8[2][0](ptr, ls_uv, E, I, H);
  2737. }
  2738. } else if (hm2 & x) {
  2739. L = l[16];
  2740. H |= (L >> 4) << 8;
  2741. E |= s->filter.mblim_lut[L] << 8;
  2742. I |= s->filter.lim_lut[L] << 8;
  2743. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  2744. [!!(hmask2[1] & x)]
  2745. [0](ptr, ls_uv, E, I, H);
  2746. } else {
  2747. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  2748. [0](ptr, ls_uv, E, I, H);
  2749. }
  2750. } else if (hm2 & x) {
  2751. int L = l[16], H = L >> 4;
  2752. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2753. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  2754. [0](ptr + 8 * ls_uv, ls_uv, E, I, H);
  2755. }
  2756. }
  2757. if (x & 0xAA)
  2758. l += 2;
  2759. }
  2760. }
  2761. lvl = lflvl->level;
  2762. dst = s->f->data[1 + p] + uvoff;
  2763. for (y = 0; y < 8; y++, dst += 4 * ls_uv) {
  2764. uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[1][1][y];
  2765. unsigned vm = vmask[0] | vmask[1] | vmask[2];
  2766. for (x = 1; vm & ~(x - 1); x <<= 4, ptr += 16, l += 4) {
  2767. if (row || y) {
  2768. if (vm & x) {
  2769. int L = *l, H = L >> 4;
  2770. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2771. if (vmask[0] & x) {
  2772. if (vmask[0] & (x << 2)) {
  2773. av_assert2(l[2] == L);
  2774. s->dsp.loop_filter_16[1](ptr, ls_uv, E, I, H);
  2775. } else {
  2776. s->dsp.loop_filter_8[2][1](ptr, ls_uv, E, I, H);
  2777. }
  2778. } else if (vm & (x << 2)) {
  2779. L = l[2];
  2780. H |= (L >> 4) << 8;
  2781. E |= s->filter.mblim_lut[L] << 8;
  2782. I |= s->filter.lim_lut[L] << 8;
  2783. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  2784. [!!(vmask[1] & (x << 2))]
  2785. [1](ptr, ls_uv, E, I, H);
  2786. } else {
  2787. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  2788. [1](ptr, ls_uv, E, I, H);
  2789. }
  2790. } else if (vm & (x << 2)) {
  2791. int L = l[2], H = L >> 4;
  2792. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  2793. s->dsp.loop_filter_8[!!(vmask[1] & (x << 2))]
  2794. [1](ptr + 8, ls_uv, E, I, H);
  2795. }
  2796. }
  2797. }
  2798. if (y & 1)
  2799. lvl += 16;
  2800. }
  2801. }
  2802. }
  2803. static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
  2804. {
  2805. int sb_start = ( idx * n) >> log2_n;
  2806. int sb_end = ((idx + 1) * n) >> log2_n;
  2807. *start = FFMIN(sb_start, n) << 3;
  2808. *end = FFMIN(sb_end, n) << 3;
  2809. }
  2810. static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
  2811. int max_count, int update_factor)
  2812. {
  2813. unsigned ct = ct0 + ct1, p2, p1;
  2814. if (!ct)
  2815. return;
  2816. p1 = *p;
  2817. p2 = ((ct0 << 8) + (ct >> 1)) / ct;
  2818. p2 = av_clip(p2, 1, 255);
  2819. ct = FFMIN(ct, max_count);
  2820. update_factor = FASTDIV(update_factor * ct, max_count);
  2821. // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
  2822. *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
  2823. }
  2824. static void adapt_probs(VP9Context *s)
  2825. {
  2826. int i, j, k, l, m;
  2827. prob_context *p = &s->prob_ctx[s->framectxid].p;
  2828. int uf = (s->keyframe || s->intraonly || !s->last_keyframe) ? 112 : 128;
  2829. // coefficients
  2830. for (i = 0; i < 4; i++)
  2831. for (j = 0; j < 2; j++)
  2832. for (k = 0; k < 2; k++)
  2833. for (l = 0; l < 6; l++)
  2834. for (m = 0; m < 6; m++) {
  2835. uint8_t *pp = s->prob_ctx[s->framectxid].coef[i][j][k][l][m];
  2836. unsigned *e = s->counts.eob[i][j][k][l][m];
  2837. unsigned *c = s->counts.coef[i][j][k][l][m];
  2838. if (l == 0 && m >= 3) // dc only has 3 pt
  2839. break;
  2840. adapt_prob(&pp[0], e[0], e[1], 24, uf);
  2841. adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
  2842. adapt_prob(&pp[2], c[1], c[2], 24, uf);
  2843. }
  2844. if (s->keyframe || s->intraonly) {
  2845. memcpy(p->skip, s->prob.p.skip, sizeof(p->skip));
  2846. memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
  2847. memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
  2848. memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p));
  2849. return;
  2850. }
  2851. // skip flag
  2852. for (i = 0; i < 3; i++)
  2853. adapt_prob(&p->skip[i], s->counts.skip[i][0], s->counts.skip[i][1], 20, 128);
  2854. // intra/inter flag
  2855. for (i = 0; i < 4; i++)
  2856. adapt_prob(&p->intra[i], s->counts.intra[i][0], s->counts.intra[i][1], 20, 128);
  2857. // comppred flag
  2858. if (s->comppredmode == PRED_SWITCHABLE) {
  2859. for (i = 0; i < 5; i++)
  2860. adapt_prob(&p->comp[i], s->counts.comp[i][0], s->counts.comp[i][1], 20, 128);
  2861. }
  2862. // reference frames
  2863. if (s->comppredmode != PRED_SINGLEREF) {
  2864. for (i = 0; i < 5; i++)
  2865. adapt_prob(&p->comp_ref[i], s->counts.comp_ref[i][0],
  2866. s->counts.comp_ref[i][1], 20, 128);
  2867. }
  2868. if (s->comppredmode != PRED_COMPREF) {
  2869. for (i = 0; i < 5; i++) {
  2870. uint8_t *pp = p->single_ref[i];
  2871. unsigned (*c)[2] = s->counts.single_ref[i];
  2872. adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
  2873. adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
  2874. }
  2875. }
  2876. // block partitioning
  2877. for (i = 0; i < 4; i++)
  2878. for (j = 0; j < 4; j++) {
  2879. uint8_t *pp = p->partition[i][j];
  2880. unsigned *c = s->counts.partition[i][j];
  2881. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2882. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2883. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2884. }
  2885. // tx size
  2886. if (s->txfmmode == TX_SWITCHABLE) {
  2887. for (i = 0; i < 2; i++) {
  2888. unsigned *c16 = s->counts.tx16p[i], *c32 = s->counts.tx32p[i];
  2889. adapt_prob(&p->tx8p[i], s->counts.tx8p[i][0], s->counts.tx8p[i][1], 20, 128);
  2890. adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
  2891. adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
  2892. adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
  2893. adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
  2894. adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
  2895. }
  2896. }
  2897. // interpolation filter
  2898. if (s->filtermode == FILTER_SWITCHABLE) {
  2899. for (i = 0; i < 4; i++) {
  2900. uint8_t *pp = p->filter[i];
  2901. unsigned *c = s->counts.filter[i];
  2902. adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
  2903. adapt_prob(&pp[1], c[1], c[2], 20, 128);
  2904. }
  2905. }
  2906. // inter modes
  2907. for (i = 0; i < 7; i++) {
  2908. uint8_t *pp = p->mv_mode[i];
  2909. unsigned *c = s->counts.mv_mode[i];
  2910. adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
  2911. adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
  2912. adapt_prob(&pp[2], c[1], c[3], 20, 128);
  2913. }
  2914. // mv joints
  2915. {
  2916. uint8_t *pp = p->mv_joint;
  2917. unsigned *c = s->counts.mv_joint;
  2918. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2919. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2920. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2921. }
  2922. // mv components
  2923. for (i = 0; i < 2; i++) {
  2924. uint8_t *pp;
  2925. unsigned *c, (*c2)[2], sum;
  2926. adapt_prob(&p->mv_comp[i].sign, s->counts.mv_comp[i].sign[0],
  2927. s->counts.mv_comp[i].sign[1], 20, 128);
  2928. pp = p->mv_comp[i].classes;
  2929. c = s->counts.mv_comp[i].classes;
  2930. sum = c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9] + c[10];
  2931. adapt_prob(&pp[0], c[0], sum, 20, 128);
  2932. sum -= c[1];
  2933. adapt_prob(&pp[1], c[1], sum, 20, 128);
  2934. sum -= c[2] + c[3];
  2935. adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
  2936. adapt_prob(&pp[3], c[2], c[3], 20, 128);
  2937. sum -= c[4] + c[5];
  2938. adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
  2939. adapt_prob(&pp[5], c[4], c[5], 20, 128);
  2940. sum -= c[6];
  2941. adapt_prob(&pp[6], c[6], sum, 20, 128);
  2942. adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
  2943. adapt_prob(&pp[8], c[7], c[8], 20, 128);
  2944. adapt_prob(&pp[9], c[9], c[10], 20, 128);
  2945. adapt_prob(&p->mv_comp[i].class0, s->counts.mv_comp[i].class0[0],
  2946. s->counts.mv_comp[i].class0[1], 20, 128);
  2947. pp = p->mv_comp[i].bits;
  2948. c2 = s->counts.mv_comp[i].bits;
  2949. for (j = 0; j < 10; j++)
  2950. adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
  2951. for (j = 0; j < 2; j++) {
  2952. pp = p->mv_comp[i].class0_fp[j];
  2953. c = s->counts.mv_comp[i].class0_fp[j];
  2954. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  2955. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  2956. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  2957. }
  2958. pp = p->mv_comp[i].fp;
  2959. c = s->counts.mv_comp[i].fp;
  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. if (s->highprecisionmvs) {
  2964. adapt_prob(&p->mv_comp[i].class0_hp, s->counts.mv_comp[i].class0_hp[0],
  2965. s->counts.mv_comp[i].class0_hp[1], 20, 128);
  2966. adapt_prob(&p->mv_comp[i].hp, s->counts.mv_comp[i].hp[0],
  2967. s->counts.mv_comp[i].hp[1], 20, 128);
  2968. }
  2969. }
  2970. // y intra modes
  2971. for (i = 0; i < 4; i++) {
  2972. uint8_t *pp = p->y_mode[i];
  2973. unsigned *c = s->counts.y_mode[i], sum, s2;
  2974. sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
  2975. adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
  2976. sum -= c[TM_VP8_PRED];
  2977. adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
  2978. sum -= c[VERT_PRED];
  2979. adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
  2980. s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
  2981. sum -= s2;
  2982. adapt_prob(&pp[3], s2, sum, 20, 128);
  2983. s2 -= c[HOR_PRED];
  2984. adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
  2985. adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], 20, 128);
  2986. sum -= c[DIAG_DOWN_LEFT_PRED];
  2987. adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
  2988. sum -= c[VERT_LEFT_PRED];
  2989. adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
  2990. adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
  2991. }
  2992. // uv intra modes
  2993. for (i = 0; i < 10; i++) {
  2994. uint8_t *pp = p->uv_mode[i];
  2995. unsigned *c = s->counts.uv_mode[i], sum, s2;
  2996. sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
  2997. adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
  2998. sum -= c[TM_VP8_PRED];
  2999. adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
  3000. sum -= c[VERT_PRED];
  3001. adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
  3002. s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
  3003. sum -= s2;
  3004. adapt_prob(&pp[3], s2, sum, 20, 128);
  3005. s2 -= c[HOR_PRED];
  3006. adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
  3007. adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], 20, 128);
  3008. sum -= c[DIAG_DOWN_LEFT_PRED];
  3009. adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
  3010. sum -= c[VERT_LEFT_PRED];
  3011. adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
  3012. adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
  3013. }
  3014. }
  3015. static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
  3016. int *got_frame, const uint8_t *data, int size)
  3017. {
  3018. VP9Context *s = ctx->priv_data;
  3019. int res, tile_row, tile_col, i, ref, row, col;
  3020. ptrdiff_t yoff = 0, uvoff = 0;
  3021. //AVFrame *prev_frame = s->f; // for segmentation map
  3022. if ((res = decode_frame_header(ctx, data, size, &ref)) < 0) {
  3023. return res;
  3024. } else if (res == 0) {
  3025. if (!s->refs[ref]) {
  3026. av_log(ctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
  3027. return AVERROR_INVALIDDATA;
  3028. }
  3029. if ((res = av_frame_ref(out_pic, s->refs[ref])) < 0)
  3030. return res;
  3031. *got_frame = 1;
  3032. return 0;
  3033. }
  3034. data += res;
  3035. size -= res;
  3036. // discard old references
  3037. for (i = 0; i < 10; i++) {
  3038. AVFrame *f = s->fb[i];
  3039. if (f->data[0] && f != s->f &&
  3040. f != s->refs[0] && f != s->refs[1] &&
  3041. f != s->refs[2] && f != s->refs[3] &&
  3042. f != s->refs[4] && f != s->refs[5] &&
  3043. f != s->refs[6] && f != s->refs[7])
  3044. av_frame_unref(f);
  3045. }
  3046. // find unused reference
  3047. for (i = 0; i < 10; i++)
  3048. if (!s->fb[i]->data[0])
  3049. break;
  3050. av_assert0(i < 10);
  3051. s->f = s->fb[i];
  3052. if ((res = ff_get_buffer(ctx, s->f,
  3053. s->refreshrefmask ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
  3054. return res;
  3055. s->f->key_frame = s->keyframe;
  3056. s->f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  3057. // main tile decode loop
  3058. memset(s->above_partition_ctx, 0, s->cols);
  3059. memset(s->above_skip_ctx, 0, s->cols);
  3060. if (s->keyframe || s->intraonly) {
  3061. memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
  3062. } else {
  3063. memset(s->above_mode_ctx, NEARESTMV, s->cols);
  3064. }
  3065. memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
  3066. memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 8);
  3067. memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 8);
  3068. memset(s->above_segpred_ctx, 0, s->cols);
  3069. for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) {
  3070. set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end,
  3071. tile_row, s->tiling.log2_tile_rows, s->sb_rows);
  3072. for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
  3073. unsigned tile_size;
  3074. if (tile_col == s->tiling.tile_cols - 1 &&
  3075. tile_row == s->tiling.tile_rows - 1) {
  3076. tile_size = size;
  3077. } else {
  3078. tile_size = AV_RB32(data);
  3079. data += 4;
  3080. size -= 4;
  3081. }
  3082. if (tile_size > size)
  3083. return AVERROR_INVALIDDATA;
  3084. ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
  3085. if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) // marker bit
  3086. return AVERROR_INVALIDDATA;
  3087. data += tile_size;
  3088. size -= tile_size;
  3089. }
  3090. for (row = s->tiling.tile_row_start;
  3091. row < s->tiling.tile_row_end;
  3092. row += 8, yoff += s->f->linesize[0] * 64,
  3093. uvoff += s->f->linesize[1] * 32) {
  3094. struct VP9Filter *lflvl_ptr = s->lflvl;
  3095. ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
  3096. for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
  3097. set_tile_offset(&s->tiling.tile_col_start, &s->tiling.tile_col_end,
  3098. tile_col, s->tiling.log2_tile_cols, s->sb_cols);
  3099. memset(s->left_partition_ctx, 0, 8);
  3100. memset(s->left_skip_ctx, 0, 8);
  3101. if (s->keyframe || s->intraonly) {
  3102. memset(s->left_mode_ctx, DC_PRED, 16);
  3103. } else {
  3104. memset(s->left_mode_ctx, NEARESTMV, 8);
  3105. }
  3106. memset(s->left_y_nnz_ctx, 0, 16);
  3107. memset(s->left_uv_nnz_ctx, 0, 16);
  3108. memset(s->left_segpred_ctx, 0, 8);
  3109. memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
  3110. for (col = s->tiling.tile_col_start;
  3111. col < s->tiling.tile_col_end;
  3112. col += 8, yoff2 += 64, uvoff2 += 32, lflvl_ptr++) {
  3113. // FIXME integrate with lf code (i.e. zero after each
  3114. // use, similar to invtxfm coefficients, or similar)
  3115. memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
  3116. if ((res = decode_sb(ctx, row, col, lflvl_ptr,
  3117. yoff2, uvoff2, BL_64X64)) < 0)
  3118. return res;
  3119. }
  3120. memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
  3121. }
  3122. // backup pre-loopfilter reconstruction data for intra
  3123. // prediction of next row of sb64s
  3124. if (row + 8 < s->rows) {
  3125. memcpy(s->intra_pred_data[0],
  3126. s->f->data[0] + yoff + 63 * s->f->linesize[0],
  3127. 8 * s->cols);
  3128. memcpy(s->intra_pred_data[1],
  3129. s->f->data[1] + uvoff + 31 * s->f->linesize[1],
  3130. 4 * s->cols);
  3131. memcpy(s->intra_pred_data[2],
  3132. s->f->data[2] + uvoff + 31 * s->f->linesize[2],
  3133. 4 * s->cols);
  3134. }
  3135. // loopfilter one row
  3136. if (s->filter.level) {
  3137. yoff2 = yoff;
  3138. uvoff2 = uvoff;
  3139. lflvl_ptr = s->lflvl;
  3140. for (col = 0; col < s->cols;
  3141. col += 8, yoff2 += 64, uvoff2 += 32, lflvl_ptr++) {
  3142. loopfilter_sb(ctx, lflvl_ptr, row, col, yoff2, uvoff2);
  3143. }
  3144. }
  3145. }
  3146. }
  3147. // bw adaptivity (or in case of parallel decoding mode, fw adaptivity
  3148. // probability maintenance between frames)
  3149. if (s->refreshctx) {
  3150. if (s->parallelmode) {
  3151. int i, j, k, l, m;
  3152. for (i = 0; i < 4; i++)
  3153. for (j = 0; j < 2; j++)
  3154. for (k = 0; k < 2; k++)
  3155. for (l = 0; l < 6; l++)
  3156. for (m = 0; m < 6; m++)
  3157. memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m],
  3158. s->prob.coef[i][j][k][l][m], 3);
  3159. s->prob_ctx[s->framectxid].p = s->prob.p;
  3160. } else {
  3161. adapt_probs(s);
  3162. }
  3163. }
  3164. FFSWAP(struct VP9mvrefPair *, s->mv[0], s->mv[1]);
  3165. // ref frame setup
  3166. for (i = 0; i < 8; i++)
  3167. if (s->refreshrefmask & (1 << i))
  3168. s->refs[i] = s->f;
  3169. if (!s->invisible) {
  3170. if ((res = av_frame_ref(out_pic, s->f)) < 0)
  3171. return res;
  3172. *got_frame = 1;
  3173. }
  3174. return 0;
  3175. }
  3176. static int vp9_decode_packet(AVCodecContext *avctx, void *out_pic,
  3177. int *got_frame, AVPacket *avpkt)
  3178. {
  3179. const uint8_t *data = avpkt->data;
  3180. int size = avpkt->size, marker, res;
  3181. // read superframe index - this is a collection of individual frames that
  3182. // together lead to one visible frame
  3183. av_assert1(size > 0); // without CODEC_CAP_DELAY, this is implied
  3184. marker = data[size - 1];
  3185. if ((marker & 0xe0) == 0xc0) {
  3186. int nbytes = 1 + ((marker >> 3) & 0x3);
  3187. int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
  3188. if (size >= idx_sz && data[size - idx_sz] == marker) {
  3189. const uint8_t *idx = data + size + 1 - idx_sz;
  3190. switch (nbytes) {
  3191. #define case_n(a, rd) \
  3192. case a: \
  3193. while (n_frames--) { \
  3194. int sz = rd; \
  3195. idx += a; \
  3196. if (sz > size) { \
  3197. av_log(avctx, AV_LOG_ERROR, \
  3198. "Superframe packet size too big: %d > %d\n", \
  3199. sz, size); \
  3200. return AVERROR_INVALIDDATA; \
  3201. } \
  3202. res = vp9_decode_frame(avctx, out_pic, got_frame, \
  3203. data, sz); \
  3204. if (res < 0) \
  3205. return res; \
  3206. data += sz; \
  3207. size -= sz; \
  3208. } \
  3209. break;
  3210. case_n(1, *idx);
  3211. case_n(2, AV_RL16(idx));
  3212. case_n(3, AV_RL24(idx));
  3213. case_n(4, AV_RL32(idx));
  3214. }
  3215. return size;
  3216. }
  3217. }
  3218. // if we get here, there was no valid superframe index, i.e. this is just
  3219. // one whole single frame - decode it as such from the complete input buf
  3220. if ((res = vp9_decode_frame(avctx, out_pic, got_frame, data, size)) < 0)
  3221. return res;
  3222. return size;
  3223. }
  3224. static void vp9_decode_flush(AVCodecContext *ctx)
  3225. {
  3226. VP9Context *s = ctx->priv_data;
  3227. int i;
  3228. for (i = 0; i < 10; i++)
  3229. if (s->fb[i]->data[0])
  3230. av_frame_unref(s->fb[i]);
  3231. for (i = 0; i < 8; i++)
  3232. s->refs[i] = NULL;
  3233. s->f = NULL;
  3234. }
  3235. static av_cold int vp9_decode_init(AVCodecContext *ctx)
  3236. {
  3237. VP9Context *s = ctx->priv_data;
  3238. int i;
  3239. ctx->pix_fmt = AV_PIX_FMT_YUV420P;
  3240. ff_vp9dsp_init(&s->dsp);
  3241. ff_videodsp_init(&s->vdsp, 8);
  3242. for (i = 0; i < 10; i++) {
  3243. s->fb[i] = av_frame_alloc();
  3244. if (!s->fb[i]) {
  3245. av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
  3246. return AVERROR(ENOMEM);
  3247. }
  3248. }
  3249. s->filter.sharpness = -1;
  3250. return 0;
  3251. }
  3252. static av_cold int vp9_decode_free(AVCodecContext *ctx)
  3253. {
  3254. VP9Context *s = ctx->priv_data;
  3255. int i;
  3256. for (i = 0; i < 10; i++) {
  3257. if (s->fb[i]->data[0])
  3258. av_frame_unref(s->fb[i]);
  3259. av_frame_free(&s->fb[i]);
  3260. }
  3261. av_freep(&s->above_partition_ctx);
  3262. s->above_skip_ctx = s->above_txfm_ctx = s->above_mode_ctx = NULL;
  3263. s->above_y_nnz_ctx = s->above_uv_nnz_ctx[0] = s->above_uv_nnz_ctx[1] = NULL;
  3264. s->intra_pred_data[0] = s->intra_pred_data[1] = s->intra_pred_data[2] = NULL;
  3265. s->above_segpred_ctx = s->above_intra_ctx = s->above_comp_ctx = NULL;
  3266. s->above_ref_ctx = s->above_filter_ctx = NULL;
  3267. s->above_mv_ctx = NULL;
  3268. s->segmentation_map = NULL;
  3269. s->mv[0] = s->mv[1] = NULL;
  3270. s->lflvl = NULL;
  3271. av_freep(&s->c_b);
  3272. s->c_b_size = 0;
  3273. return 0;
  3274. }
  3275. AVCodec ff_vp9_decoder = {
  3276. .name = "vp9",
  3277. .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
  3278. .type = AVMEDIA_TYPE_VIDEO,
  3279. .id = AV_CODEC_ID_VP9,
  3280. .priv_data_size = sizeof(VP9Context),
  3281. .init = vp9_decode_init,
  3282. .close = vp9_decode_free,
  3283. .decode = vp9_decode_packet,
  3284. .capabilities = CODEC_CAP_DR1,
  3285. .flush = vp9_decode_flush,
  3286. };