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.

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