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.

4296 lines
174KB

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