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.

4295 lines
169KB

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