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.

4351 lines
172KB

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