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.

1677 lines
68KB

  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 "profiles.h"
  27. #include "thread.h"
  28. #include "videodsp.h"
  29. #include "vp56.h"
  30. #include "vp9.h"
  31. #include "vp9data.h"
  32. #include "vp9dec.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/pixdesc.h"
  35. #define VP9_SYNCCODE 0x498342
  36. static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
  37. {
  38. ff_thread_release_buffer(avctx, &f->tf);
  39. av_buffer_unref(&f->extradata);
  40. av_buffer_unref(&f->hwaccel_priv_buf);
  41. f->segmentation_map = NULL;
  42. f->hwaccel_picture_private = NULL;
  43. }
  44. static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
  45. {
  46. VP9Context *s = avctx->priv_data;
  47. int ret, sz;
  48. ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF);
  49. if (ret < 0)
  50. return ret;
  51. sz = 64 * s->sb_cols * s->sb_rows;
  52. f->extradata = av_buffer_allocz(sz * (1 + sizeof(VP9mvrefPair)));
  53. if (!f->extradata) {
  54. goto fail;
  55. }
  56. f->segmentation_map = f->extradata->data;
  57. f->mv = (VP9mvrefPair *) (f->extradata->data + sz);
  58. if (avctx->hwaccel) {
  59. const AVHWAccel *hwaccel = avctx->hwaccel;
  60. av_assert0(!f->hwaccel_picture_private);
  61. if (hwaccel->frame_priv_data_size) {
  62. f->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
  63. if (!f->hwaccel_priv_buf)
  64. goto fail;
  65. f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
  66. }
  67. }
  68. return 0;
  69. fail:
  70. vp9_frame_unref(avctx, f);
  71. return AVERROR(ENOMEM);
  72. }
  73. static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src)
  74. {
  75. int ret;
  76. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  77. if (ret < 0)
  78. return ret;
  79. dst->extradata = av_buffer_ref(src->extradata);
  80. if (!dst->extradata)
  81. goto fail;
  82. dst->segmentation_map = src->segmentation_map;
  83. dst->mv = src->mv;
  84. dst->uses_2pass = src->uses_2pass;
  85. if (src->hwaccel_picture_private) {
  86. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  87. if (!dst->hwaccel_priv_buf)
  88. goto fail;
  89. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  90. }
  91. return 0;
  92. fail:
  93. vp9_frame_unref(avctx, dst);
  94. return AVERROR(ENOMEM);
  95. }
  96. static int update_size(AVCodecContext *avctx, int w, int h)
  97. {
  98. #define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + CONFIG_VP9_D3D11VA_HWACCEL + CONFIG_VP9_VAAPI_HWACCEL)
  99. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
  100. VP9Context *s = avctx->priv_data;
  101. uint8_t *p;
  102. int bytesperpixel = s->bytesperpixel, ret, cols, rows;
  103. av_assert0(w > 0 && h > 0);
  104. if (!(s->pix_fmt == s->gf_fmt && w == s->w && h == s->h)) {
  105. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  106. return ret;
  107. switch (s->pix_fmt) {
  108. case AV_PIX_FMT_YUV420P:
  109. #if CONFIG_VP9_DXVA2_HWACCEL
  110. *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
  111. #endif
  112. #if CONFIG_VP9_D3D11VA_HWACCEL
  113. *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
  114. #endif
  115. #if CONFIG_VP9_VAAPI_HWACCEL
  116. *fmtp++ = AV_PIX_FMT_VAAPI;
  117. #endif
  118. break;
  119. case AV_PIX_FMT_YUV420P10:
  120. case AV_PIX_FMT_YUV420P12:
  121. #if CONFIG_VP9_VAAPI_HWACCEL
  122. *fmtp++ = AV_PIX_FMT_VAAPI;
  123. #endif
  124. break;
  125. }
  126. *fmtp++ = s->pix_fmt;
  127. *fmtp = AV_PIX_FMT_NONE;
  128. ret = ff_thread_get_format(avctx, pix_fmts);
  129. if (ret < 0)
  130. return ret;
  131. avctx->pix_fmt = ret;
  132. s->gf_fmt = s->pix_fmt;
  133. s->w = w;
  134. s->h = h;
  135. }
  136. cols = (w + 7) >> 3;
  137. rows = (h + 7) >> 3;
  138. if (s->intra_pred_data[0] && cols == s->cols && rows == s->rows && s->pix_fmt == s->last_fmt)
  139. return 0;
  140. s->last_fmt = s->pix_fmt;
  141. s->sb_cols = (w + 63) >> 6;
  142. s->sb_rows = (h + 63) >> 6;
  143. s->cols = (w + 7) >> 3;
  144. s->rows = (h + 7) >> 3;
  145. #define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
  146. av_freep(&s->intra_pred_data[0]);
  147. // FIXME we slightly over-allocate here for subsampled chroma, but a little
  148. // bit of padding shouldn't affect performance...
  149. p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
  150. sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
  151. if (!p)
  152. return AVERROR(ENOMEM);
  153. assign(s->intra_pred_data[0], uint8_t *, 64 * bytesperpixel);
  154. assign(s->intra_pred_data[1], uint8_t *, 64 * bytesperpixel);
  155. assign(s->intra_pred_data[2], uint8_t *, 64 * bytesperpixel);
  156. assign(s->above_y_nnz_ctx, uint8_t *, 16);
  157. assign(s->above_mode_ctx, uint8_t *, 16);
  158. assign(s->above_mv_ctx, VP56mv(*)[2], 16);
  159. assign(s->above_uv_nnz_ctx[0], uint8_t *, 16);
  160. assign(s->above_uv_nnz_ctx[1], uint8_t *, 16);
  161. assign(s->above_partition_ctx, uint8_t *, 8);
  162. assign(s->above_skip_ctx, uint8_t *, 8);
  163. assign(s->above_txfm_ctx, uint8_t *, 8);
  164. assign(s->above_segpred_ctx, uint8_t *, 8);
  165. assign(s->above_intra_ctx, uint8_t *, 8);
  166. assign(s->above_comp_ctx, uint8_t *, 8);
  167. assign(s->above_ref_ctx, uint8_t *, 8);
  168. assign(s->above_filter_ctx, uint8_t *, 8);
  169. assign(s->lflvl, VP9Filter *, 1);
  170. #undef assign
  171. // these will be re-allocated a little later
  172. av_freep(&s->b_base);
  173. av_freep(&s->block_base);
  174. if (s->s.h.bpp != s->last_bpp) {
  175. ff_vp9dsp_init(&s->dsp, s->s.h.bpp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
  176. ff_videodsp_init(&s->vdsp, s->s.h.bpp);
  177. s->last_bpp = s->s.h.bpp;
  178. }
  179. return 0;
  180. }
  181. static int update_block_buffers(AVCodecContext *avctx)
  182. {
  183. VP9Context *s = avctx->priv_data;
  184. int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
  185. if (s->b_base && s->block_base && s->block_alloc_using_2pass == s->s.frames[CUR_FRAME].uses_2pass)
  186. return 0;
  187. av_free(s->b_base);
  188. av_free(s->block_base);
  189. chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
  190. chroma_eobs = 16 * 16 >> (s->ss_h + s->ss_v);
  191. if (s->s.frames[CUR_FRAME].uses_2pass) {
  192. int sbs = s->sb_cols * s->sb_rows;
  193. s->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
  194. s->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
  195. 16 * 16 + 2 * chroma_eobs) * sbs);
  196. if (!s->b_base || !s->block_base)
  197. return AVERROR(ENOMEM);
  198. s->uvblock_base[0] = s->block_base + sbs * 64 * 64 * bytesperpixel;
  199. s->uvblock_base[1] = s->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
  200. s->eob_base = (uint8_t *) (s->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
  201. s->uveob_base[0] = s->eob_base + 16 * 16 * sbs;
  202. s->uveob_base[1] = s->uveob_base[0] + chroma_eobs * sbs;
  203. } else {
  204. s->b_base = av_malloc(sizeof(VP9Block));
  205. s->block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
  206. 16 * 16 + 2 * chroma_eobs);
  207. if (!s->b_base || !s->block_base)
  208. return AVERROR(ENOMEM);
  209. s->uvblock_base[0] = s->block_base + 64 * 64 * bytesperpixel;
  210. s->uvblock_base[1] = s->uvblock_base[0] + chroma_blocks * bytesperpixel;
  211. s->eob_base = (uint8_t *) (s->uvblock_base[1] + chroma_blocks * bytesperpixel);
  212. s->uveob_base[0] = s->eob_base + 16 * 16;
  213. s->uveob_base[1] = s->uveob_base[0] + chroma_eobs;
  214. }
  215. s->block_alloc_using_2pass = s->s.frames[CUR_FRAME].uses_2pass;
  216. return 0;
  217. }
  218. // The sign bit is at the end, not the start, of a bit sequence
  219. static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
  220. {
  221. int v = get_bits(gb, n);
  222. return get_bits1(gb) ? -v : v;
  223. }
  224. static av_always_inline int inv_recenter_nonneg(int v, int m)
  225. {
  226. if (v > 2 * m)
  227. return v;
  228. if (v & 1)
  229. return m - ((v + 1) >> 1);
  230. return m + (v >> 1);
  231. }
  232. // differential forward probability updates
  233. static int update_prob(VP56RangeCoder *c, int p)
  234. {
  235. static const int inv_map_table[255] = {
  236. 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
  237. 189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
  238. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
  239. 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
  240. 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
  241. 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  242. 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
  243. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
  244. 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
  245. 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
  246. 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
  247. 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
  248. 161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  249. 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
  250. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
  251. 207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
  252. 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
  253. 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
  254. 252, 253, 253,
  255. };
  256. int d;
  257. /* This code is trying to do a differential probability update. For a
  258. * current probability A in the range [1, 255], the difference to a new
  259. * probability of any value can be expressed differentially as 1-A, 255-A
  260. * where some part of this (absolute range) exists both in positive as
  261. * well as the negative part, whereas another part only exists in one
  262. * half. We're trying to code this shared part differentially, i.e.
  263. * times two where the value of the lowest bit specifies the sign, and
  264. * the single part is then coded on top of this. This absolute difference
  265. * then again has a value of [0, 254], but a bigger value in this range
  266. * indicates that we're further away from the original value A, so we
  267. * can code this as a VLC code, since higher values are increasingly
  268. * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
  269. * updates vs. the 'fine, exact' updates further down the range, which
  270. * adds one extra dimension to this differential update model. */
  271. if (!vp8_rac_get(c)) {
  272. d = vp8_rac_get_uint(c, 4) + 0;
  273. } else if (!vp8_rac_get(c)) {
  274. d = vp8_rac_get_uint(c, 4) + 16;
  275. } else if (!vp8_rac_get(c)) {
  276. d = vp8_rac_get_uint(c, 5) + 32;
  277. } else {
  278. d = vp8_rac_get_uint(c, 7);
  279. if (d >= 65)
  280. d = (d << 1) - 65 + vp8_rac_get(c);
  281. d += 64;
  282. av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
  283. }
  284. return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
  285. 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
  286. }
  287. static int read_colorspace_details(AVCodecContext *avctx)
  288. {
  289. static const enum AVColorSpace colorspaces[8] = {
  290. AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
  291. AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
  292. };
  293. VP9Context *s = avctx->priv_data;
  294. int bits = avctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
  295. s->bpp_index = bits;
  296. s->s.h.bpp = 8 + bits * 2;
  297. s->bytesperpixel = (7 + s->s.h.bpp) >> 3;
  298. avctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
  299. if (avctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
  300. static const enum AVPixelFormat pix_fmt_rgb[3] = {
  301. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12
  302. };
  303. s->ss_h = s->ss_v = 0;
  304. avctx->color_range = AVCOL_RANGE_JPEG;
  305. s->pix_fmt = pix_fmt_rgb[bits];
  306. if (avctx->profile & 1) {
  307. if (get_bits1(&s->gb)) {
  308. av_log(avctx, AV_LOG_ERROR, "Reserved bit set in RGB\n");
  309. return AVERROR_INVALIDDATA;
  310. }
  311. } else {
  312. av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
  313. avctx->profile);
  314. return AVERROR_INVALIDDATA;
  315. }
  316. } else {
  317. static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
  318. { { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P },
  319. { AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV420P } },
  320. { { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10 },
  321. { AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV420P10 } },
  322. { { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12 },
  323. { AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV420P12 } }
  324. };
  325. avctx->color_range = get_bits1(&s->gb) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  326. if (avctx->profile & 1) {
  327. s->ss_h = get_bits1(&s->gb);
  328. s->ss_v = get_bits1(&s->gb);
  329. s->pix_fmt = pix_fmt_for_ss[bits][s->ss_v][s->ss_h];
  330. if (s->pix_fmt == AV_PIX_FMT_YUV420P) {
  331. av_log(avctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
  332. avctx->profile);
  333. return AVERROR_INVALIDDATA;
  334. } else if (get_bits1(&s->gb)) {
  335. av_log(avctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
  336. avctx->profile);
  337. return AVERROR_INVALIDDATA;
  338. }
  339. } else {
  340. s->ss_h = s->ss_v = 1;
  341. s->pix_fmt = pix_fmt_for_ss[bits][1][1];
  342. }
  343. }
  344. return 0;
  345. }
  346. static int decode_frame_header(AVCodecContext *avctx,
  347. const uint8_t *data, int size, int *ref)
  348. {
  349. VP9Context *s = avctx->priv_data;
  350. int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
  351. int last_invisible;
  352. const uint8_t *data2;
  353. /* general header */
  354. if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
  355. av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
  356. return ret;
  357. }
  358. if (get_bits(&s->gb, 2) != 0x2) { // frame marker
  359. av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
  360. return AVERROR_INVALIDDATA;
  361. }
  362. avctx->profile = get_bits1(&s->gb);
  363. avctx->profile |= get_bits1(&s->gb) << 1;
  364. if (avctx->profile == 3) avctx->profile += get_bits1(&s->gb);
  365. if (avctx->profile > 3) {
  366. av_log(avctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", avctx->profile);
  367. return AVERROR_INVALIDDATA;
  368. }
  369. s->s.h.profile = avctx->profile;
  370. if (get_bits1(&s->gb)) {
  371. *ref = get_bits(&s->gb, 3);
  372. return 0;
  373. }
  374. s->last_keyframe = s->s.h.keyframe;
  375. s->s.h.keyframe = !get_bits1(&s->gb);
  376. last_invisible = s->s.h.invisible;
  377. s->s.h.invisible = !get_bits1(&s->gb);
  378. s->s.h.errorres = get_bits1(&s->gb);
  379. s->s.h.use_last_frame_mvs = !s->s.h.errorres && !last_invisible;
  380. if (s->s.h.keyframe) {
  381. if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
  382. av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
  383. return AVERROR_INVALIDDATA;
  384. }
  385. if ((ret = read_colorspace_details(avctx)) < 0)
  386. return ret;
  387. // for profile 1, here follows the subsampling bits
  388. s->s.h.refreshrefmask = 0xff;
  389. w = get_bits(&s->gb, 16) + 1;
  390. h = get_bits(&s->gb, 16) + 1;
  391. if (get_bits1(&s->gb)) // display size
  392. skip_bits(&s->gb, 32);
  393. } else {
  394. s->s.h.intraonly = s->s.h.invisible ? get_bits1(&s->gb) : 0;
  395. s->s.h.resetctx = s->s.h.errorres ? 0 : get_bits(&s->gb, 2);
  396. if (s->s.h.intraonly) {
  397. if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
  398. av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
  399. return AVERROR_INVALIDDATA;
  400. }
  401. if (avctx->profile >= 1) {
  402. if ((ret = read_colorspace_details(avctx)) < 0)
  403. return ret;
  404. } else {
  405. s->ss_h = s->ss_v = 1;
  406. s->s.h.bpp = 8;
  407. s->bpp_index = 0;
  408. s->bytesperpixel = 1;
  409. s->pix_fmt = AV_PIX_FMT_YUV420P;
  410. avctx->colorspace = AVCOL_SPC_BT470BG;
  411. avctx->color_range = AVCOL_RANGE_JPEG;
  412. }
  413. s->s.h.refreshrefmask = get_bits(&s->gb, 8);
  414. w = get_bits(&s->gb, 16) + 1;
  415. h = get_bits(&s->gb, 16) + 1;
  416. if (get_bits1(&s->gb)) // display size
  417. skip_bits(&s->gb, 32);
  418. } else {
  419. s->s.h.refreshrefmask = get_bits(&s->gb, 8);
  420. s->s.h.refidx[0] = get_bits(&s->gb, 3);
  421. s->s.h.signbias[0] = get_bits1(&s->gb) && !s->s.h.errorres;
  422. s->s.h.refidx[1] = get_bits(&s->gb, 3);
  423. s->s.h.signbias[1] = get_bits1(&s->gb) && !s->s.h.errorres;
  424. s->s.h.refidx[2] = get_bits(&s->gb, 3);
  425. s->s.h.signbias[2] = get_bits1(&s->gb) && !s->s.h.errorres;
  426. if (!s->s.refs[s->s.h.refidx[0]].f->buf[0] ||
  427. !s->s.refs[s->s.h.refidx[1]].f->buf[0] ||
  428. !s->s.refs[s->s.h.refidx[2]].f->buf[0]) {
  429. av_log(avctx, AV_LOG_ERROR, "Not all references are available\n");
  430. return AVERROR_INVALIDDATA;
  431. }
  432. if (get_bits1(&s->gb)) {
  433. w = s->s.refs[s->s.h.refidx[0]].f->width;
  434. h = s->s.refs[s->s.h.refidx[0]].f->height;
  435. } else if (get_bits1(&s->gb)) {
  436. w = s->s.refs[s->s.h.refidx[1]].f->width;
  437. h = s->s.refs[s->s.h.refidx[1]].f->height;
  438. } else if (get_bits1(&s->gb)) {
  439. w = s->s.refs[s->s.h.refidx[2]].f->width;
  440. h = s->s.refs[s->s.h.refidx[2]].f->height;
  441. } else {
  442. w = get_bits(&s->gb, 16) + 1;
  443. h = get_bits(&s->gb, 16) + 1;
  444. }
  445. // Note that in this code, "CUR_FRAME" is actually before we
  446. // have formally allocated a frame, and thus actually represents
  447. // the _last_ frame
  448. s->s.h.use_last_frame_mvs &= s->s.frames[CUR_FRAME].tf.f->width == w &&
  449. s->s.frames[CUR_FRAME].tf.f->height == h;
  450. if (get_bits1(&s->gb)) // display size
  451. skip_bits(&s->gb, 32);
  452. s->s.h.highprecisionmvs = get_bits1(&s->gb);
  453. s->s.h.filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
  454. get_bits(&s->gb, 2);
  455. s->s.h.allowcompinter = s->s.h.signbias[0] != s->s.h.signbias[1] ||
  456. s->s.h.signbias[0] != s->s.h.signbias[2];
  457. if (s->s.h.allowcompinter) {
  458. if (s->s.h.signbias[0] == s->s.h.signbias[1]) {
  459. s->s.h.fixcompref = 2;
  460. s->s.h.varcompref[0] = 0;
  461. s->s.h.varcompref[1] = 1;
  462. } else if (s->s.h.signbias[0] == s->s.h.signbias[2]) {
  463. s->s.h.fixcompref = 1;
  464. s->s.h.varcompref[0] = 0;
  465. s->s.h.varcompref[1] = 2;
  466. } else {
  467. s->s.h.fixcompref = 0;
  468. s->s.h.varcompref[0] = 1;
  469. s->s.h.varcompref[1] = 2;
  470. }
  471. }
  472. }
  473. }
  474. s->s.h.refreshctx = s->s.h.errorres ? 0 : get_bits1(&s->gb);
  475. s->s.h.parallelmode = s->s.h.errorres ? 1 : get_bits1(&s->gb);
  476. s->s.h.framectxid = c = get_bits(&s->gb, 2);
  477. if (s->s.h.keyframe || s->s.h.intraonly)
  478. s->s.h.framectxid = 0; // BUG: libvpx ignores this field in keyframes
  479. /* loopfilter header data */
  480. if (s->s.h.keyframe || s->s.h.errorres || s->s.h.intraonly) {
  481. // reset loopfilter defaults
  482. s->s.h.lf_delta.ref[0] = 1;
  483. s->s.h.lf_delta.ref[1] = 0;
  484. s->s.h.lf_delta.ref[2] = -1;
  485. s->s.h.lf_delta.ref[3] = -1;
  486. s->s.h.lf_delta.mode[0] = 0;
  487. s->s.h.lf_delta.mode[1] = 0;
  488. memset(s->s.h.segmentation.feat, 0, sizeof(s->s.h.segmentation.feat));
  489. }
  490. s->s.h.filter.level = get_bits(&s->gb, 6);
  491. sharp = get_bits(&s->gb, 3);
  492. // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
  493. // the old cache values since they are still valid
  494. if (s->s.h.filter.sharpness != sharp)
  495. memset(s->filter_lut.lim_lut, 0, sizeof(s->filter_lut.lim_lut));
  496. s->s.h.filter.sharpness = sharp;
  497. if ((s->s.h.lf_delta.enabled = get_bits1(&s->gb))) {
  498. if ((s->s.h.lf_delta.updated = get_bits1(&s->gb))) {
  499. for (i = 0; i < 4; i++)
  500. if (get_bits1(&s->gb))
  501. s->s.h.lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
  502. for (i = 0; i < 2; i++)
  503. if (get_bits1(&s->gb))
  504. s->s.h.lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
  505. }
  506. }
  507. /* quantization header data */
  508. s->s.h.yac_qi = get_bits(&s->gb, 8);
  509. s->s.h.ydc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
  510. s->s.h.uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
  511. s->s.h.uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
  512. s->s.h.lossless = s->s.h.yac_qi == 0 && s->s.h.ydc_qdelta == 0 &&
  513. s->s.h.uvdc_qdelta == 0 && s->s.h.uvac_qdelta == 0;
  514. if (s->s.h.lossless)
  515. avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
  516. /* segmentation header info */
  517. if ((s->s.h.segmentation.enabled = get_bits1(&s->gb))) {
  518. if ((s->s.h.segmentation.update_map = get_bits1(&s->gb))) {
  519. for (i = 0; i < 7; i++)
  520. s->s.h.segmentation.prob[i] = get_bits1(&s->gb) ?
  521. get_bits(&s->gb, 8) : 255;
  522. if ((s->s.h.segmentation.temporal = get_bits1(&s->gb)))
  523. for (i = 0; i < 3; i++)
  524. s->s.h.segmentation.pred_prob[i] = get_bits1(&s->gb) ?
  525. get_bits(&s->gb, 8) : 255;
  526. }
  527. if (get_bits1(&s->gb)) {
  528. s->s.h.segmentation.absolute_vals = get_bits1(&s->gb);
  529. for (i = 0; i < 8; i++) {
  530. if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
  531. s->s.h.segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
  532. if ((s->s.h.segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
  533. s->s.h.segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
  534. if ((s->s.h.segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
  535. s->s.h.segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
  536. s->s.h.segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
  537. }
  538. }
  539. }
  540. // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
  541. for (i = 0; i < (s->s.h.segmentation.enabled ? 8 : 1); i++) {
  542. int qyac, qydc, quvac, quvdc, lflvl, sh;
  543. if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].q_enabled) {
  544. if (s->s.h.segmentation.absolute_vals)
  545. qyac = av_clip_uintp2(s->s.h.segmentation.feat[i].q_val, 8);
  546. else
  547. qyac = av_clip_uintp2(s->s.h.yac_qi + s->s.h.segmentation.feat[i].q_val, 8);
  548. } else {
  549. qyac = s->s.h.yac_qi;
  550. }
  551. qydc = av_clip_uintp2(qyac + s->s.h.ydc_qdelta, 8);
  552. quvdc = av_clip_uintp2(qyac + s->s.h.uvdc_qdelta, 8);
  553. quvac = av_clip_uintp2(qyac + s->s.h.uvac_qdelta, 8);
  554. qyac = av_clip_uintp2(qyac, 8);
  555. s->s.h.segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[s->bpp_index][qydc];
  556. s->s.h.segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[s->bpp_index][qyac];
  557. s->s.h.segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[s->bpp_index][quvdc];
  558. s->s.h.segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[s->bpp_index][quvac];
  559. sh = s->s.h.filter.level >= 32;
  560. if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].lf_enabled) {
  561. if (s->s.h.segmentation.absolute_vals)
  562. lflvl = av_clip_uintp2(s->s.h.segmentation.feat[i].lf_val, 6);
  563. else
  564. lflvl = av_clip_uintp2(s->s.h.filter.level + s->s.h.segmentation.feat[i].lf_val, 6);
  565. } else {
  566. lflvl = s->s.h.filter.level;
  567. }
  568. if (s->s.h.lf_delta.enabled) {
  569. s->s.h.segmentation.feat[i].lflvl[0][0] =
  570. s->s.h.segmentation.feat[i].lflvl[0][1] =
  571. av_clip_uintp2(lflvl + (s->s.h.lf_delta.ref[0] * (1 << sh)), 6);
  572. for (j = 1; j < 4; j++) {
  573. s->s.h.segmentation.feat[i].lflvl[j][0] =
  574. av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
  575. s->s.h.lf_delta.mode[0]) * (1 << sh)), 6);
  576. s->s.h.segmentation.feat[i].lflvl[j][1] =
  577. av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
  578. s->s.h.lf_delta.mode[1]) * (1 << sh)), 6);
  579. }
  580. } else {
  581. memset(s->s.h.segmentation.feat[i].lflvl, lflvl,
  582. sizeof(s->s.h.segmentation.feat[i].lflvl));
  583. }
  584. }
  585. /* tiling info */
  586. if ((ret = update_size(avctx, w, h)) < 0) {
  587. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n",
  588. w, h, s->pix_fmt);
  589. return ret;
  590. }
  591. for (s->s.h.tiling.log2_tile_cols = 0;
  592. s->sb_cols > (64 << s->s.h.tiling.log2_tile_cols);
  593. s->s.h.tiling.log2_tile_cols++) ;
  594. for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
  595. max = FFMAX(0, max - 1);
  596. while (max > s->s.h.tiling.log2_tile_cols) {
  597. if (get_bits1(&s->gb))
  598. s->s.h.tiling.log2_tile_cols++;
  599. else
  600. break;
  601. }
  602. s->s.h.tiling.log2_tile_rows = decode012(&s->gb);
  603. s->s.h.tiling.tile_rows = 1 << s->s.h.tiling.log2_tile_rows;
  604. if (s->s.h.tiling.tile_cols != (1 << s->s.h.tiling.log2_tile_cols)) {
  605. s->s.h.tiling.tile_cols = 1 << s->s.h.tiling.log2_tile_cols;
  606. s->c_b = av_fast_realloc(s->c_b, &s->c_b_size,
  607. sizeof(VP56RangeCoder) * s->s.h.tiling.tile_cols);
  608. if (!s->c_b) {
  609. av_log(avctx, AV_LOG_ERROR, "Ran out of memory during range coder init\n");
  610. return AVERROR(ENOMEM);
  611. }
  612. }
  613. /* check reference frames */
  614. if (!s->s.h.keyframe && !s->s.h.intraonly) {
  615. for (i = 0; i < 3; i++) {
  616. AVFrame *ref = s->s.refs[s->s.h.refidx[i]].f;
  617. int refw = ref->width, refh = ref->height;
  618. if (ref->format != avctx->pix_fmt) {
  619. av_log(avctx, AV_LOG_ERROR,
  620. "Ref pixfmt (%s) did not match current frame (%s)",
  621. av_get_pix_fmt_name(ref->format),
  622. av_get_pix_fmt_name(avctx->pix_fmt));
  623. return AVERROR_INVALIDDATA;
  624. } else if (refw == w && refh == h) {
  625. s->mvscale[i][0] = s->mvscale[i][1] = 0;
  626. } else {
  627. if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
  628. av_log(avctx, AV_LOG_ERROR,
  629. "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
  630. refw, refh, w, h);
  631. return AVERROR_INVALIDDATA;
  632. }
  633. s->mvscale[i][0] = (refw << 14) / w;
  634. s->mvscale[i][1] = (refh << 14) / h;
  635. s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
  636. s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
  637. }
  638. }
  639. }
  640. if (s->s.h.keyframe || s->s.h.errorres || (s->s.h.intraonly && s->s.h.resetctx == 3)) {
  641. s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
  642. s->prob_ctx[3].p = ff_vp9_default_probs;
  643. memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
  644. sizeof(ff_vp9_default_coef_probs));
  645. memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
  646. sizeof(ff_vp9_default_coef_probs));
  647. memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
  648. sizeof(ff_vp9_default_coef_probs));
  649. memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
  650. sizeof(ff_vp9_default_coef_probs));
  651. } else if (s->s.h.intraonly && s->s.h.resetctx == 2) {
  652. s->prob_ctx[c].p = ff_vp9_default_probs;
  653. memcpy(s->prob_ctx[c].coef, ff_vp9_default_coef_probs,
  654. sizeof(ff_vp9_default_coef_probs));
  655. }
  656. // next 16 bits is size of the rest of the header (arith-coded)
  657. s->s.h.compressed_header_size = size2 = get_bits(&s->gb, 16);
  658. s->s.h.uncompressed_header_size = (get_bits_count(&s->gb) + 7) / 8;
  659. data2 = align_get_bits(&s->gb);
  660. if (size2 > size - (data2 - data)) {
  661. av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
  662. return AVERROR_INVALIDDATA;
  663. }
  664. ret = ff_vp56_init_range_decoder(&s->c, data2, size2);
  665. if (ret < 0)
  666. return ret;
  667. if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
  668. av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
  669. return AVERROR_INVALIDDATA;
  670. }
  671. if (s->s.h.keyframe || s->s.h.intraonly) {
  672. memset(s->counts.coef, 0, sizeof(s->counts.coef));
  673. memset(s->counts.eob, 0, sizeof(s->counts.eob));
  674. } else {
  675. memset(&s->counts, 0, sizeof(s->counts));
  676. }
  677. /* FIXME is it faster to not copy here, but do it down in the fw updates
  678. * as explicit copies if the fw update is missing (and skip the copy upon
  679. * fw update)? */
  680. s->prob.p = s->prob_ctx[c].p;
  681. // txfm updates
  682. if (s->s.h.lossless) {
  683. s->s.h.txfmmode = TX_4X4;
  684. } else {
  685. s->s.h.txfmmode = vp8_rac_get_uint(&s->c, 2);
  686. if (s->s.h.txfmmode == 3)
  687. s->s.h.txfmmode += vp8_rac_get(&s->c);
  688. if (s->s.h.txfmmode == TX_SWITCHABLE) {
  689. for (i = 0; i < 2; i++)
  690. if (vp56_rac_get_prob_branchy(&s->c, 252))
  691. s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
  692. for (i = 0; i < 2; i++)
  693. for (j = 0; j < 2; j++)
  694. if (vp56_rac_get_prob_branchy(&s->c, 252))
  695. s->prob.p.tx16p[i][j] =
  696. update_prob(&s->c, s->prob.p.tx16p[i][j]);
  697. for (i = 0; i < 2; i++)
  698. for (j = 0; j < 3; j++)
  699. if (vp56_rac_get_prob_branchy(&s->c, 252))
  700. s->prob.p.tx32p[i][j] =
  701. update_prob(&s->c, s->prob.p.tx32p[i][j]);
  702. }
  703. }
  704. // coef updates
  705. for (i = 0; i < 4; i++) {
  706. uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
  707. if (vp8_rac_get(&s->c)) {
  708. for (j = 0; j < 2; j++)
  709. for (k = 0; k < 2; k++)
  710. for (l = 0; l < 6; l++)
  711. for (m = 0; m < 6; m++) {
  712. uint8_t *p = s->prob.coef[i][j][k][l][m];
  713. uint8_t *r = ref[j][k][l][m];
  714. if (m >= 3 && l == 0) // dc only has 3 pt
  715. break;
  716. for (n = 0; n < 3; n++) {
  717. if (vp56_rac_get_prob_branchy(&s->c, 252))
  718. p[n] = update_prob(&s->c, r[n]);
  719. else
  720. p[n] = r[n];
  721. }
  722. p[3] = 0;
  723. }
  724. } else {
  725. for (j = 0; j < 2; j++)
  726. for (k = 0; k < 2; k++)
  727. for (l = 0; l < 6; l++)
  728. for (m = 0; m < 6; m++) {
  729. uint8_t *p = s->prob.coef[i][j][k][l][m];
  730. uint8_t *r = ref[j][k][l][m];
  731. if (m > 3 && l == 0) // dc only has 3 pt
  732. break;
  733. memcpy(p, r, 3);
  734. p[3] = 0;
  735. }
  736. }
  737. if (s->s.h.txfmmode == i)
  738. break;
  739. }
  740. // mode updates
  741. for (i = 0; i < 3; i++)
  742. if (vp56_rac_get_prob_branchy(&s->c, 252))
  743. s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
  744. if (!s->s.h.keyframe && !s->s.h.intraonly) {
  745. for (i = 0; i < 7; i++)
  746. for (j = 0; j < 3; j++)
  747. if (vp56_rac_get_prob_branchy(&s->c, 252))
  748. s->prob.p.mv_mode[i][j] =
  749. update_prob(&s->c, s->prob.p.mv_mode[i][j]);
  750. if (s->s.h.filtermode == FILTER_SWITCHABLE)
  751. for (i = 0; i < 4; i++)
  752. for (j = 0; j < 2; j++)
  753. if (vp56_rac_get_prob_branchy(&s->c, 252))
  754. s->prob.p.filter[i][j] =
  755. update_prob(&s->c, s->prob.p.filter[i][j]);
  756. for (i = 0; i < 4; i++)
  757. if (vp56_rac_get_prob_branchy(&s->c, 252))
  758. s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
  759. if (s->s.h.allowcompinter) {
  760. s->s.h.comppredmode = vp8_rac_get(&s->c);
  761. if (s->s.h.comppredmode)
  762. s->s.h.comppredmode += vp8_rac_get(&s->c);
  763. if (s->s.h.comppredmode == PRED_SWITCHABLE)
  764. for (i = 0; i < 5; i++)
  765. if (vp56_rac_get_prob_branchy(&s->c, 252))
  766. s->prob.p.comp[i] =
  767. update_prob(&s->c, s->prob.p.comp[i]);
  768. } else {
  769. s->s.h.comppredmode = PRED_SINGLEREF;
  770. }
  771. if (s->s.h.comppredmode != PRED_COMPREF) {
  772. for (i = 0; i < 5; i++) {
  773. if (vp56_rac_get_prob_branchy(&s->c, 252))
  774. s->prob.p.single_ref[i][0] =
  775. update_prob(&s->c, s->prob.p.single_ref[i][0]);
  776. if (vp56_rac_get_prob_branchy(&s->c, 252))
  777. s->prob.p.single_ref[i][1] =
  778. update_prob(&s->c, s->prob.p.single_ref[i][1]);
  779. }
  780. }
  781. if (s->s.h.comppredmode != PRED_SINGLEREF) {
  782. for (i = 0; i < 5; i++)
  783. if (vp56_rac_get_prob_branchy(&s->c, 252))
  784. s->prob.p.comp_ref[i] =
  785. update_prob(&s->c, s->prob.p.comp_ref[i]);
  786. }
  787. for (i = 0; i < 4; i++)
  788. for (j = 0; j < 9; j++)
  789. if (vp56_rac_get_prob_branchy(&s->c, 252))
  790. s->prob.p.y_mode[i][j] =
  791. update_prob(&s->c, s->prob.p.y_mode[i][j]);
  792. for (i = 0; i < 4; i++)
  793. for (j = 0; j < 4; j++)
  794. for (k = 0; k < 3; k++)
  795. if (vp56_rac_get_prob_branchy(&s->c, 252))
  796. s->prob.p.partition[3 - i][j][k] =
  797. update_prob(&s->c,
  798. s->prob.p.partition[3 - i][j][k]);
  799. // mv fields don't use the update_prob subexp model for some reason
  800. for (i = 0; i < 3; i++)
  801. if (vp56_rac_get_prob_branchy(&s->c, 252))
  802. s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  803. for (i = 0; i < 2; i++) {
  804. if (vp56_rac_get_prob_branchy(&s->c, 252))
  805. s->prob.p.mv_comp[i].sign =
  806. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  807. for (j = 0; j < 10; j++)
  808. if (vp56_rac_get_prob_branchy(&s->c, 252))
  809. s->prob.p.mv_comp[i].classes[j] =
  810. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  811. if (vp56_rac_get_prob_branchy(&s->c, 252))
  812. s->prob.p.mv_comp[i].class0 =
  813. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  814. for (j = 0; j < 10; j++)
  815. if (vp56_rac_get_prob_branchy(&s->c, 252))
  816. s->prob.p.mv_comp[i].bits[j] =
  817. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  818. }
  819. for (i = 0; i < 2; i++) {
  820. for (j = 0; j < 2; j++)
  821. for (k = 0; k < 3; k++)
  822. if (vp56_rac_get_prob_branchy(&s->c, 252))
  823. s->prob.p.mv_comp[i].class0_fp[j][k] =
  824. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  825. for (j = 0; j < 3; j++)
  826. if (vp56_rac_get_prob_branchy(&s->c, 252))
  827. s->prob.p.mv_comp[i].fp[j] =
  828. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  829. }
  830. if (s->s.h.highprecisionmvs) {
  831. for (i = 0; i < 2; i++) {
  832. if (vp56_rac_get_prob_branchy(&s->c, 252))
  833. s->prob.p.mv_comp[i].class0_hp =
  834. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  835. if (vp56_rac_get_prob_branchy(&s->c, 252))
  836. s->prob.p.mv_comp[i].hp =
  837. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  838. }
  839. }
  840. }
  841. return (data2 - data) + size2;
  842. }
  843. static void decode_sb(AVCodecContext *avctx, int row, int col, VP9Filter *lflvl,
  844. ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
  845. {
  846. VP9Context *s = avctx->priv_data;
  847. int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
  848. (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
  849. const uint8_t *p = s->s.h.keyframe || s->s.h.intraonly ? ff_vp9_default_kf_partition_probs[bl][c] :
  850. s->prob.p.partition[bl][c];
  851. enum BlockPartition bp;
  852. ptrdiff_t hbs = 4 >> bl;
  853. AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
  854. ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
  855. int bytesperpixel = s->bytesperpixel;
  856. if (bl == BL_8X8) {
  857. bp = vp8_rac_get_tree(&s->c, ff_vp9_partition_tree, p);
  858. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  859. } else if (col + hbs < s->cols) { // FIXME why not <=?
  860. if (row + hbs < s->rows) { // FIXME why not <=?
  861. bp = vp8_rac_get_tree(&s->c, ff_vp9_partition_tree, p);
  862. switch (bp) {
  863. case PARTITION_NONE:
  864. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  865. break;
  866. case PARTITION_H:
  867. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  868. yoff += hbs * 8 * y_stride;
  869. uvoff += hbs * 8 * uv_stride >> s->ss_v;
  870. ff_vp9_decode_block(avctx, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
  871. break;
  872. case PARTITION_V:
  873. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  874. yoff += hbs * 8 * bytesperpixel;
  875. uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
  876. ff_vp9_decode_block(avctx, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
  877. break;
  878. case PARTITION_SPLIT:
  879. decode_sb(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  880. decode_sb(avctx, row, col + hbs, lflvl,
  881. yoff + 8 * hbs * bytesperpixel,
  882. uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
  883. yoff += hbs * 8 * y_stride;
  884. uvoff += hbs * 8 * uv_stride >> s->ss_v;
  885. decode_sb(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
  886. decode_sb(avctx, row + hbs, col + hbs, lflvl,
  887. yoff + 8 * hbs * bytesperpixel,
  888. uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
  889. break;
  890. default:
  891. av_assert0(0);
  892. }
  893. } else if (vp56_rac_get_prob_branchy(&s->c, p[1])) {
  894. bp = PARTITION_SPLIT;
  895. decode_sb(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  896. decode_sb(avctx, row, col + hbs, lflvl,
  897. yoff + 8 * hbs * bytesperpixel,
  898. uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
  899. } else {
  900. bp = PARTITION_H;
  901. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  902. }
  903. } else if (row + hbs < s->rows) { // FIXME why not <=?
  904. if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
  905. bp = PARTITION_SPLIT;
  906. decode_sb(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  907. yoff += hbs * 8 * y_stride;
  908. uvoff += hbs * 8 * uv_stride >> s->ss_v;
  909. decode_sb(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
  910. } else {
  911. bp = PARTITION_V;
  912. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  913. }
  914. } else {
  915. bp = PARTITION_SPLIT;
  916. decode_sb(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  917. }
  918. s->counts.partition[bl][c][bp]++;
  919. }
  920. static void decode_sb_mem(AVCodecContext *avctx, int row, int col, VP9Filter *lflvl,
  921. ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
  922. {
  923. VP9Context *s = avctx->priv_data;
  924. VP9Block *b = s->b;
  925. ptrdiff_t hbs = 4 >> bl;
  926. AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
  927. ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
  928. int bytesperpixel = s->bytesperpixel;
  929. if (bl == BL_8X8) {
  930. av_assert2(b->bl == BL_8X8);
  931. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
  932. } else if (s->b->bl == bl) {
  933. ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
  934. if (b->bp == PARTITION_H && row + hbs < s->rows) {
  935. yoff += hbs * 8 * y_stride;
  936. uvoff += hbs * 8 * uv_stride >> s->ss_v;
  937. ff_vp9_decode_block(avctx, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
  938. } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
  939. yoff += hbs * 8 * bytesperpixel;
  940. uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
  941. ff_vp9_decode_block(avctx, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
  942. }
  943. } else {
  944. decode_sb_mem(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  945. if (col + hbs < s->cols) { // FIXME why not <=?
  946. if (row + hbs < s->rows) {
  947. decode_sb_mem(avctx, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
  948. uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
  949. yoff += hbs * 8 * y_stride;
  950. uvoff += hbs * 8 * uv_stride >> s->ss_v;
  951. decode_sb_mem(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
  952. decode_sb_mem(avctx, row + hbs, col + hbs, lflvl,
  953. yoff + 8 * hbs * bytesperpixel,
  954. uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
  955. } else {
  956. yoff += hbs * 8 * bytesperpixel;
  957. uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
  958. decode_sb_mem(avctx, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
  959. }
  960. } else if (row + hbs < s->rows) {
  961. yoff += hbs * 8 * y_stride;
  962. uvoff += hbs * 8 * uv_stride >> s->ss_v;
  963. decode_sb_mem(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
  964. }
  965. }
  966. }
  967. static av_always_inline void filter_plane_cols(VP9Context *s, int col, int ss_h, int ss_v,
  968. uint8_t *lvl, uint8_t (*mask)[4],
  969. uint8_t *dst, ptrdiff_t ls)
  970. {
  971. int y, x, bytesperpixel = s->bytesperpixel;
  972. // filter edges between columns (e.g. block1 | block2)
  973. for (y = 0; y < 8; y += 2 << ss_v, dst += 16 * ls, lvl += 16 << ss_v) {
  974. uint8_t *ptr = dst, *l = lvl, *hmask1 = mask[y], *hmask2 = mask[y + 1 + ss_v];
  975. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
  976. unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
  977. unsigned hm = hm1 | hm2 | hm13 | hm23;
  978. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8 * bytesperpixel >> ss_h) {
  979. if (col || x > 1) {
  980. if (hm1 & x) {
  981. int L = *l, H = L >> 4;
  982. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  983. if (hmask1[0] & x) {
  984. if (hmask2[0] & x) {
  985. av_assert2(l[8 << ss_v] == L);
  986. s->dsp.loop_filter_16[0](ptr, ls, E, I, H);
  987. } else {
  988. s->dsp.loop_filter_8[2][0](ptr, ls, E, I, H);
  989. }
  990. } else if (hm2 & x) {
  991. L = l[8 << ss_v];
  992. H |= (L >> 4) << 8;
  993. E |= s->filter_lut.mblim_lut[L] << 8;
  994. I |= s->filter_lut.lim_lut[L] << 8;
  995. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  996. [!!(hmask2[1] & x)]
  997. [0](ptr, ls, E, I, H);
  998. } else {
  999. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  1000. [0](ptr, ls, E, I, H);
  1001. }
  1002. } else if (hm2 & x) {
  1003. int L = l[8 << ss_v], H = L >> 4;
  1004. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1005. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  1006. [0](ptr + 8 * ls, ls, E, I, H);
  1007. }
  1008. }
  1009. if (ss_h) {
  1010. if (x & 0xAA)
  1011. l += 2;
  1012. } else {
  1013. if (hm13 & x) {
  1014. int L = *l, H = L >> 4;
  1015. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1016. if (hm23 & x) {
  1017. L = l[8 << ss_v];
  1018. H |= (L >> 4) << 8;
  1019. E |= s->filter_lut.mblim_lut[L] << 8;
  1020. I |= s->filter_lut.lim_lut[L] << 8;
  1021. s->dsp.loop_filter_mix2[0][0][0](ptr + 4 * bytesperpixel, ls, E, I, H);
  1022. } else {
  1023. s->dsp.loop_filter_8[0][0](ptr + 4 * bytesperpixel, ls, E, I, H);
  1024. }
  1025. } else if (hm23 & x) {
  1026. int L = l[8 << ss_v], H = L >> 4;
  1027. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1028. s->dsp.loop_filter_8[0][0](ptr + 8 * ls + 4 * bytesperpixel, ls, E, I, H);
  1029. }
  1030. l++;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. static av_always_inline void filter_plane_rows(VP9Context *s, int row, int ss_h, int ss_v,
  1036. uint8_t *lvl, uint8_t (*mask)[4],
  1037. uint8_t *dst, ptrdiff_t ls)
  1038. {
  1039. int y, x, bytesperpixel = s->bytesperpixel;
  1040. // block1
  1041. // filter edges between rows (e.g. ------)
  1042. // block2
  1043. for (y = 0; y < 8; y++, dst += 8 * ls >> ss_v) {
  1044. uint8_t *ptr = dst, *l = lvl, *vmask = mask[y];
  1045. unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
  1046. for (x = 1; vm & ~(x - 1); x <<= (2 << ss_h), ptr += 16 * bytesperpixel, l += 2 << ss_h) {
  1047. if (row || y) {
  1048. if (vm & x) {
  1049. int L = *l, H = L >> 4;
  1050. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1051. if (vmask[0] & x) {
  1052. if (vmask[0] & (x << (1 + ss_h))) {
  1053. av_assert2(l[1 + ss_h] == L);
  1054. s->dsp.loop_filter_16[1](ptr, ls, E, I, H);
  1055. } else {
  1056. s->dsp.loop_filter_8[2][1](ptr, ls, E, I, H);
  1057. }
  1058. } else if (vm & (x << (1 + ss_h))) {
  1059. L = l[1 + ss_h];
  1060. H |= (L >> 4) << 8;
  1061. E |= s->filter_lut.mblim_lut[L] << 8;
  1062. I |= s->filter_lut.lim_lut[L] << 8;
  1063. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  1064. [!!(vmask[1] & (x << (1 + ss_h)))]
  1065. [1](ptr, ls, E, I, H);
  1066. } else {
  1067. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  1068. [1](ptr, ls, E, I, H);
  1069. }
  1070. } else if (vm & (x << (1 + ss_h))) {
  1071. int L = l[1 + ss_h], H = L >> 4;
  1072. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1073. s->dsp.loop_filter_8[!!(vmask[1] & (x << (1 + ss_h)))]
  1074. [1](ptr + 8 * bytesperpixel, ls, E, I, H);
  1075. }
  1076. }
  1077. if (!ss_v) {
  1078. if (vm3 & x) {
  1079. int L = *l, H = L >> 4;
  1080. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1081. if (vm3 & (x << (1 + ss_h))) {
  1082. L = l[1 + ss_h];
  1083. H |= (L >> 4) << 8;
  1084. E |= s->filter_lut.mblim_lut[L] << 8;
  1085. I |= s->filter_lut.lim_lut[L] << 8;
  1086. s->dsp.loop_filter_mix2[0][0][1](ptr + ls * 4, ls, E, I, H);
  1087. } else {
  1088. s->dsp.loop_filter_8[0][1](ptr + ls * 4, ls, E, I, H);
  1089. }
  1090. } else if (vm3 & (x << (1 + ss_h))) {
  1091. int L = l[1 + ss_h], H = L >> 4;
  1092. int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L];
  1093. s->dsp.loop_filter_8[0][1](ptr + ls * 4 + 8 * bytesperpixel, ls, E, I, H);
  1094. }
  1095. }
  1096. }
  1097. if (ss_v) {
  1098. if (y & 1)
  1099. lvl += 16;
  1100. } else {
  1101. lvl += 8;
  1102. }
  1103. }
  1104. }
  1105. static void loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl,
  1106. int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
  1107. {
  1108. VP9Context *s = avctx->priv_data;
  1109. AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
  1110. uint8_t *dst = f->data[0] + yoff;
  1111. ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1];
  1112. uint8_t (*uv_masks)[8][4] = lflvl->mask[s->ss_h | s->ss_v];
  1113. int p;
  1114. /* FIXME: In how far can we interleave the v/h loopfilter calls? E.g.
  1115. * if you think of them as acting on a 8x8 block max, we can interleave
  1116. * each v/h within the single x loop, but that only works if we work on
  1117. * 8 pixel blocks, and we won't always do that (we want at least 16px
  1118. * to use SSE2 optimizations, perhaps 32 for AVX2) */
  1119. filter_plane_cols(s, col, 0, 0, lflvl->level, lflvl->mask[0][0], dst, ls_y);
  1120. filter_plane_rows(s, row, 0, 0, lflvl->level, lflvl->mask[0][1], dst, ls_y);
  1121. for (p = 0; p < 2; p++) {
  1122. dst = f->data[1 + p] + uvoff;
  1123. filter_plane_cols(s, col, s->ss_h, s->ss_v, lflvl->level, uv_masks[0], dst, ls_uv);
  1124. filter_plane_rows(s, row, s->ss_h, s->ss_v, lflvl->level, uv_masks[1], dst, ls_uv);
  1125. }
  1126. }
  1127. static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
  1128. {
  1129. int sb_start = ( idx * n) >> log2_n;
  1130. int sb_end = ((idx + 1) * n) >> log2_n;
  1131. *start = FFMIN(sb_start, n) << 3;
  1132. *end = FFMIN(sb_end, n) << 3;
  1133. }
  1134. static void free_buffers(VP9Context *s)
  1135. {
  1136. av_freep(&s->intra_pred_data[0]);
  1137. av_freep(&s->b_base);
  1138. av_freep(&s->block_base);
  1139. }
  1140. static av_cold int vp9_decode_free(AVCodecContext *avctx)
  1141. {
  1142. VP9Context *s = avctx->priv_data;
  1143. int i;
  1144. for (i = 0; i < 3; i++) {
  1145. if (s->s.frames[i].tf.f->buf[0])
  1146. vp9_frame_unref(avctx, &s->s.frames[i]);
  1147. av_frame_free(&s->s.frames[i].tf.f);
  1148. }
  1149. for (i = 0; i < 8; i++) {
  1150. if (s->s.refs[i].f->buf[0])
  1151. ff_thread_release_buffer(avctx, &s->s.refs[i]);
  1152. av_frame_free(&s->s.refs[i].f);
  1153. if (s->next_refs[i].f->buf[0])
  1154. ff_thread_release_buffer(avctx, &s->next_refs[i]);
  1155. av_frame_free(&s->next_refs[i].f);
  1156. }
  1157. free_buffers(s);
  1158. av_freep(&s->c_b);
  1159. s->c_b_size = 0;
  1160. return 0;
  1161. }
  1162. static int vp9_decode_frame(AVCodecContext *avctx, void *frame,
  1163. int *got_frame, AVPacket *pkt)
  1164. {
  1165. const uint8_t *data = pkt->data;
  1166. int size = pkt->size;
  1167. VP9Context *s = avctx->priv_data;
  1168. int ret, tile_row, tile_col, i, ref, row, col;
  1169. int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
  1170. (!s->s.h.segmentation.enabled || !s->s.h.segmentation.update_map);
  1171. ptrdiff_t yoff, uvoff, ls_y, ls_uv;
  1172. AVFrame *f;
  1173. int bytesperpixel;
  1174. if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
  1175. return ret;
  1176. } else if (ret == 0) {
  1177. if (!s->s.refs[ref].f->buf[0]) {
  1178. av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
  1179. return AVERROR_INVALIDDATA;
  1180. }
  1181. if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
  1182. return ret;
  1183. ((AVFrame *)frame)->pts = pkt->pts;
  1184. #if FF_API_PKT_PTS
  1185. FF_DISABLE_DEPRECATION_WARNINGS
  1186. ((AVFrame *)frame)->pkt_pts = pkt->pts;
  1187. FF_ENABLE_DEPRECATION_WARNINGS
  1188. #endif
  1189. ((AVFrame *)frame)->pkt_dts = pkt->dts;
  1190. for (i = 0; i < 8; i++) {
  1191. if (s->next_refs[i].f->buf[0])
  1192. ff_thread_release_buffer(avctx, &s->next_refs[i]);
  1193. if (s->s.refs[i].f->buf[0] &&
  1194. (ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i])) < 0)
  1195. return ret;
  1196. }
  1197. *got_frame = 1;
  1198. return pkt->size;
  1199. }
  1200. data += ret;
  1201. size -= ret;
  1202. if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly) {
  1203. if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0])
  1204. vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
  1205. if (!s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
  1206. (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_SEGMAP], &s->s.frames[CUR_FRAME])) < 0)
  1207. return ret;
  1208. }
  1209. if (s->s.frames[REF_FRAME_MVPAIR].tf.f->buf[0])
  1210. vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_MVPAIR]);
  1211. if (!s->s.h.intraonly && !s->s.h.keyframe && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
  1212. (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_MVPAIR], &s->s.frames[CUR_FRAME])) < 0)
  1213. return ret;
  1214. if (s->s.frames[CUR_FRAME].tf.f->buf[0])
  1215. vp9_frame_unref(avctx, &s->s.frames[CUR_FRAME]);
  1216. if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0)
  1217. return ret;
  1218. f = s->s.frames[CUR_FRAME].tf.f;
  1219. f->key_frame = s->s.h.keyframe;
  1220. f->pict_type = (s->s.h.keyframe || s->s.h.intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1221. ls_y = f->linesize[0];
  1222. ls_uv =f->linesize[1];
  1223. if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0] &&
  1224. (s->s.frames[REF_FRAME_MVPAIR].tf.f->width != s->s.frames[CUR_FRAME].tf.f->width ||
  1225. s->s.frames[REF_FRAME_MVPAIR].tf.f->height != s->s.frames[CUR_FRAME].tf.f->height)) {
  1226. vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
  1227. }
  1228. // ref frame setup
  1229. for (i = 0; i < 8; i++) {
  1230. if (s->next_refs[i].f->buf[0])
  1231. ff_thread_release_buffer(avctx, &s->next_refs[i]);
  1232. if (s->s.h.refreshrefmask & (1 << i)) {
  1233. ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.frames[CUR_FRAME].tf);
  1234. } else if (s->s.refs[i].f->buf[0]) {
  1235. ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i]);
  1236. }
  1237. if (ret < 0)
  1238. return ret;
  1239. }
  1240. if (avctx->hwaccel) {
  1241. ret = avctx->hwaccel->start_frame(avctx, NULL, 0);
  1242. if (ret < 0)
  1243. return ret;
  1244. ret = avctx->hwaccel->decode_slice(avctx, pkt->data, pkt->size);
  1245. if (ret < 0)
  1246. return ret;
  1247. ret = avctx->hwaccel->end_frame(avctx);
  1248. if (ret < 0)
  1249. return ret;
  1250. goto finish;
  1251. }
  1252. // main tile decode loop
  1253. bytesperpixel = s->bytesperpixel;
  1254. memset(s->above_partition_ctx, 0, s->cols);
  1255. memset(s->above_skip_ctx, 0, s->cols);
  1256. if (s->s.h.keyframe || s->s.h.intraonly) {
  1257. memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
  1258. } else {
  1259. memset(s->above_mode_ctx, NEARESTMV, s->cols);
  1260. }
  1261. memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
  1262. memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
  1263. memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
  1264. memset(s->above_segpred_ctx, 0, s->cols);
  1265. s->pass = s->s.frames[CUR_FRAME].uses_2pass =
  1266. avctx->active_thread_type == FF_THREAD_FRAME && s->s.h.refreshctx && !s->s.h.parallelmode;
  1267. if ((ret = update_block_buffers(avctx)) < 0) {
  1268. av_log(avctx, AV_LOG_ERROR,
  1269. "Failed to allocate block buffers\n");
  1270. return ret;
  1271. }
  1272. if (s->s.h.refreshctx && s->s.h.parallelmode) {
  1273. int j, k, l, m;
  1274. for (i = 0; i < 4; i++) {
  1275. for (j = 0; j < 2; j++)
  1276. for (k = 0; k < 2; k++)
  1277. for (l = 0; l < 6; l++)
  1278. for (m = 0; m < 6; m++)
  1279. memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
  1280. s->prob.coef[i][j][k][l][m], 3);
  1281. if (s->s.h.txfmmode == i)
  1282. break;
  1283. }
  1284. s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
  1285. ff_thread_finish_setup(avctx);
  1286. } else if (!s->s.h.refreshctx) {
  1287. ff_thread_finish_setup(avctx);
  1288. }
  1289. do {
  1290. yoff = uvoff = 0;
  1291. s->b = s->b_base;
  1292. s->block = s->block_base;
  1293. s->uvblock[0] = s->uvblock_base[0];
  1294. s->uvblock[1] = s->uvblock_base[1];
  1295. s->eob = s->eob_base;
  1296. s->uveob[0] = s->uveob_base[0];
  1297. s->uveob[1] = s->uveob_base[1];
  1298. for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
  1299. set_tile_offset(&s->tile_row_start, &s->tile_row_end,
  1300. tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
  1301. if (s->pass != 2) {
  1302. for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
  1303. int64_t tile_size;
  1304. if (tile_col == s->s.h.tiling.tile_cols - 1 &&
  1305. tile_row == s->s.h.tiling.tile_rows - 1) {
  1306. tile_size = size;
  1307. } else {
  1308. tile_size = AV_RB32(data);
  1309. data += 4;
  1310. size -= 4;
  1311. }
  1312. if (tile_size > size) {
  1313. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
  1314. return AVERROR_INVALIDDATA;
  1315. }
  1316. ret = ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
  1317. if (ret < 0)
  1318. return ret;
  1319. if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit
  1320. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
  1321. return AVERROR_INVALIDDATA;
  1322. }
  1323. data += tile_size;
  1324. size -= tile_size;
  1325. }
  1326. }
  1327. for (row = s->tile_row_start; row < s->tile_row_end;
  1328. row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
  1329. VP9Filter *lflvl_ptr = s->lflvl;
  1330. ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
  1331. for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
  1332. set_tile_offset(&s->tile_col_start, &s->tile_col_end,
  1333. tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
  1334. if (s->pass != 2) {
  1335. memset(s->left_partition_ctx, 0, 8);
  1336. memset(s->left_skip_ctx, 0, 8);
  1337. if (s->s.h.keyframe || s->s.h.intraonly) {
  1338. memset(s->left_mode_ctx, DC_PRED, 16);
  1339. } else {
  1340. memset(s->left_mode_ctx, NEARESTMV, 8);
  1341. }
  1342. memset(s->left_y_nnz_ctx, 0, 16);
  1343. memset(s->left_uv_nnz_ctx, 0, 32);
  1344. memset(s->left_segpred_ctx, 0, 8);
  1345. memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
  1346. }
  1347. for (col = s->tile_col_start;
  1348. col < s->tile_col_end;
  1349. col += 8, yoff2 += 64 * bytesperpixel,
  1350. uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
  1351. // FIXME integrate with lf code (i.e. zero after each
  1352. // use, similar to invtxfm coefficients, or similar)
  1353. if (s->pass != 1) {
  1354. memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
  1355. }
  1356. if (s->pass == 2) {
  1357. decode_sb_mem(avctx, row, col, lflvl_ptr,
  1358. yoff2, uvoff2, BL_64X64);
  1359. } else {
  1360. decode_sb(avctx, row, col, lflvl_ptr,
  1361. yoff2, uvoff2, BL_64X64);
  1362. }
  1363. }
  1364. if (s->pass != 2)
  1365. memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
  1366. }
  1367. if (s->pass == 1)
  1368. continue;
  1369. // backup pre-loopfilter reconstruction data for intra
  1370. // prediction of next row of sb64s
  1371. if (row + 8 < s->rows) {
  1372. memcpy(s->intra_pred_data[0],
  1373. f->data[0] + yoff + 63 * ls_y,
  1374. 8 * s->cols * bytesperpixel);
  1375. memcpy(s->intra_pred_data[1],
  1376. f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
  1377. 8 * s->cols * bytesperpixel >> s->ss_h);
  1378. memcpy(s->intra_pred_data[2],
  1379. f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
  1380. 8 * s->cols * bytesperpixel >> s->ss_h);
  1381. }
  1382. // loopfilter one row
  1383. if (s->s.h.filter.level) {
  1384. yoff2 = yoff;
  1385. uvoff2 = uvoff;
  1386. lflvl_ptr = s->lflvl;
  1387. for (col = 0; col < s->cols;
  1388. col += 8, yoff2 += 64 * bytesperpixel,
  1389. uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
  1390. loopfilter_sb(avctx, lflvl_ptr, row, col, yoff2, uvoff2);
  1391. }
  1392. }
  1393. // FIXME maybe we can make this more finegrained by running the
  1394. // loopfilter per-block instead of after each sbrow
  1395. // In fact that would also make intra pred left preparation easier?
  1396. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, row >> 3, 0);
  1397. }
  1398. }
  1399. if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
  1400. ff_vp9_adapt_probs(s);
  1401. ff_thread_finish_setup(avctx);
  1402. }
  1403. } while (s->pass++ == 1);
  1404. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
  1405. finish:
  1406. // ref frame setup
  1407. for (i = 0; i < 8; i++) {
  1408. if (s->s.refs[i].f->buf[0])
  1409. ff_thread_release_buffer(avctx, &s->s.refs[i]);
  1410. if (s->next_refs[i].f->buf[0] &&
  1411. (ret = ff_thread_ref_frame(&s->s.refs[i], &s->next_refs[i])) < 0)
  1412. return ret;
  1413. }
  1414. if (!s->s.h.invisible) {
  1415. if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
  1416. return ret;
  1417. *got_frame = 1;
  1418. }
  1419. return pkt->size;
  1420. }
  1421. static void vp9_decode_flush(AVCodecContext *avctx)
  1422. {
  1423. VP9Context *s = avctx->priv_data;
  1424. int i;
  1425. for (i = 0; i < 3; i++)
  1426. vp9_frame_unref(avctx, &s->s.frames[i]);
  1427. for (i = 0; i < 8; i++)
  1428. ff_thread_release_buffer(avctx, &s->s.refs[i]);
  1429. }
  1430. static int init_frames(AVCodecContext *avctx)
  1431. {
  1432. VP9Context *s = avctx->priv_data;
  1433. int i;
  1434. for (i = 0; i < 3; i++) {
  1435. s->s.frames[i].tf.f = av_frame_alloc();
  1436. if (!s->s.frames[i].tf.f) {
  1437. vp9_decode_free(avctx);
  1438. av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
  1439. return AVERROR(ENOMEM);
  1440. }
  1441. }
  1442. for (i = 0; i < 8; i++) {
  1443. s->s.refs[i].f = av_frame_alloc();
  1444. s->next_refs[i].f = av_frame_alloc();
  1445. if (!s->s.refs[i].f || !s->next_refs[i].f) {
  1446. vp9_decode_free(avctx);
  1447. av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
  1448. return AVERROR(ENOMEM);
  1449. }
  1450. }
  1451. return 0;
  1452. }
  1453. static av_cold int vp9_decode_init(AVCodecContext *avctx)
  1454. {
  1455. VP9Context *s = avctx->priv_data;
  1456. avctx->internal->allocate_progress = 1;
  1457. s->last_bpp = 0;
  1458. s->s.h.filter.sharpness = -1;
  1459. return init_frames(avctx);
  1460. }
  1461. #if HAVE_THREADS
  1462. static av_cold int vp9_decode_init_thread_copy(AVCodecContext *avctx)
  1463. {
  1464. return init_frames(avctx);
  1465. }
  1466. static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1467. {
  1468. int i, ret;
  1469. VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
  1470. for (i = 0; i < 3; i++) {
  1471. if (s->s.frames[i].tf.f->buf[0])
  1472. vp9_frame_unref(dst, &s->s.frames[i]);
  1473. if (ssrc->s.frames[i].tf.f->buf[0]) {
  1474. if ((ret = vp9_frame_ref(dst, &s->s.frames[i], &ssrc->s.frames[i])) < 0)
  1475. return ret;
  1476. }
  1477. }
  1478. for (i = 0; i < 8; i++) {
  1479. if (s->s.refs[i].f->buf[0])
  1480. ff_thread_release_buffer(dst, &s->s.refs[i]);
  1481. if (ssrc->next_refs[i].f->buf[0]) {
  1482. if ((ret = ff_thread_ref_frame(&s->s.refs[i], &ssrc->next_refs[i])) < 0)
  1483. return ret;
  1484. }
  1485. }
  1486. s->s.h.invisible = ssrc->s.h.invisible;
  1487. s->s.h.keyframe = ssrc->s.h.keyframe;
  1488. s->s.h.intraonly = ssrc->s.h.intraonly;
  1489. s->ss_v = ssrc->ss_v;
  1490. s->ss_h = ssrc->ss_h;
  1491. s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
  1492. s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
  1493. s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
  1494. s->bytesperpixel = ssrc->bytesperpixel;
  1495. s->gf_fmt = ssrc->gf_fmt;
  1496. s->w = ssrc->w;
  1497. s->h = ssrc->h;
  1498. s->s.h.bpp = ssrc->s.h.bpp;
  1499. s->bpp_index = ssrc->bpp_index;
  1500. s->pix_fmt = ssrc->pix_fmt;
  1501. memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
  1502. memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
  1503. memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
  1504. sizeof(s->s.h.segmentation.feat));
  1505. return 0;
  1506. }
  1507. #endif
  1508. AVCodec ff_vp9_decoder = {
  1509. .name = "vp9",
  1510. .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
  1511. .type = AVMEDIA_TYPE_VIDEO,
  1512. .id = AV_CODEC_ID_VP9,
  1513. .priv_data_size = sizeof(VP9Context),
  1514. .init = vp9_decode_init,
  1515. .close = vp9_decode_free,
  1516. .decode = vp9_decode_frame,
  1517. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1518. .flush = vp9_decode_flush,
  1519. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp9_decode_init_thread_copy),
  1520. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp9_decode_update_thread_context),
  1521. .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
  1522. };