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.

3819 lines
152KB

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