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.

4092 lines
160KB

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