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.

3878 lines
154KB

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