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.

3590 lines
144KB

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