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.

3636 lines
144KB

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