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.

4350 lines
175KB

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