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.

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