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.

4111 lines
161KB

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