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.

3617 lines
144KB

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