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.

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