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.

1500 lines
60KB

  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 void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
  968. {
  969. int sb_start = ( idx * n) >> log2_n;
  970. int sb_end = ((idx + 1) * n) >> log2_n;
  971. *start = FFMIN(sb_start, n) << 3;
  972. *end = FFMIN(sb_end, n) << 3;
  973. }
  974. static void free_buffers(VP9Context *s)
  975. {
  976. av_freep(&s->intra_pred_data[0]);
  977. av_freep(&s->b_base);
  978. av_freep(&s->block_base);
  979. }
  980. static av_cold int vp9_decode_free(AVCodecContext *avctx)
  981. {
  982. VP9Context *s = avctx->priv_data;
  983. int i;
  984. for (i = 0; i < 3; i++) {
  985. if (s->s.frames[i].tf.f->buf[0])
  986. vp9_frame_unref(avctx, &s->s.frames[i]);
  987. av_frame_free(&s->s.frames[i].tf.f);
  988. }
  989. for (i = 0; i < 8; i++) {
  990. if (s->s.refs[i].f->buf[0])
  991. ff_thread_release_buffer(avctx, &s->s.refs[i]);
  992. av_frame_free(&s->s.refs[i].f);
  993. if (s->next_refs[i].f->buf[0])
  994. ff_thread_release_buffer(avctx, &s->next_refs[i]);
  995. av_frame_free(&s->next_refs[i].f);
  996. }
  997. free_buffers(s);
  998. av_freep(&s->c_b);
  999. s->c_b_size = 0;
  1000. return 0;
  1001. }
  1002. static int vp9_decode_frame(AVCodecContext *avctx, void *frame,
  1003. int *got_frame, AVPacket *pkt)
  1004. {
  1005. const uint8_t *data = pkt->data;
  1006. int size = pkt->size;
  1007. VP9Context *s = avctx->priv_data;
  1008. int ret, tile_row, tile_col, i, ref, row, col;
  1009. int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
  1010. (!s->s.h.segmentation.enabled || !s->s.h.segmentation.update_map);
  1011. ptrdiff_t yoff, uvoff, ls_y, ls_uv;
  1012. AVFrame *f;
  1013. int bytesperpixel;
  1014. if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
  1015. return ret;
  1016. } else if (ret == 0) {
  1017. if (!s->s.refs[ref].f->buf[0]) {
  1018. av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
  1019. return AVERROR_INVALIDDATA;
  1020. }
  1021. if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
  1022. return ret;
  1023. ((AVFrame *)frame)->pts = pkt->pts;
  1024. #if FF_API_PKT_PTS
  1025. FF_DISABLE_DEPRECATION_WARNINGS
  1026. ((AVFrame *)frame)->pkt_pts = pkt->pts;
  1027. FF_ENABLE_DEPRECATION_WARNINGS
  1028. #endif
  1029. ((AVFrame *)frame)->pkt_dts = pkt->dts;
  1030. for (i = 0; i < 8; i++) {
  1031. if (s->next_refs[i].f->buf[0])
  1032. ff_thread_release_buffer(avctx, &s->next_refs[i]);
  1033. if (s->s.refs[i].f->buf[0] &&
  1034. (ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i])) < 0)
  1035. return ret;
  1036. }
  1037. *got_frame = 1;
  1038. return pkt->size;
  1039. }
  1040. data += ret;
  1041. size -= ret;
  1042. if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly) {
  1043. if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0])
  1044. vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
  1045. if (!s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
  1046. (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_SEGMAP], &s->s.frames[CUR_FRAME])) < 0)
  1047. return ret;
  1048. }
  1049. if (s->s.frames[REF_FRAME_MVPAIR].tf.f->buf[0])
  1050. vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_MVPAIR]);
  1051. if (!s->s.h.intraonly && !s->s.h.keyframe && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
  1052. (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_MVPAIR], &s->s.frames[CUR_FRAME])) < 0)
  1053. return ret;
  1054. if (s->s.frames[CUR_FRAME].tf.f->buf[0])
  1055. vp9_frame_unref(avctx, &s->s.frames[CUR_FRAME]);
  1056. if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0)
  1057. return ret;
  1058. f = s->s.frames[CUR_FRAME].tf.f;
  1059. f->key_frame = s->s.h.keyframe;
  1060. f->pict_type = (s->s.h.keyframe || s->s.h.intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1061. ls_y = f->linesize[0];
  1062. ls_uv =f->linesize[1];
  1063. if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0] &&
  1064. (s->s.frames[REF_FRAME_MVPAIR].tf.f->width != s->s.frames[CUR_FRAME].tf.f->width ||
  1065. s->s.frames[REF_FRAME_MVPAIR].tf.f->height != s->s.frames[CUR_FRAME].tf.f->height)) {
  1066. vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
  1067. }
  1068. // ref frame setup
  1069. for (i = 0; i < 8; i++) {
  1070. if (s->next_refs[i].f->buf[0])
  1071. ff_thread_release_buffer(avctx, &s->next_refs[i]);
  1072. if (s->s.h.refreshrefmask & (1 << i)) {
  1073. ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.frames[CUR_FRAME].tf);
  1074. } else if (s->s.refs[i].f->buf[0]) {
  1075. ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i]);
  1076. }
  1077. if (ret < 0)
  1078. return ret;
  1079. }
  1080. if (avctx->hwaccel) {
  1081. ret = avctx->hwaccel->start_frame(avctx, NULL, 0);
  1082. if (ret < 0)
  1083. return ret;
  1084. ret = avctx->hwaccel->decode_slice(avctx, pkt->data, pkt->size);
  1085. if (ret < 0)
  1086. return ret;
  1087. ret = avctx->hwaccel->end_frame(avctx);
  1088. if (ret < 0)
  1089. return ret;
  1090. goto finish;
  1091. }
  1092. // main tile decode loop
  1093. bytesperpixel = s->bytesperpixel;
  1094. memset(s->above_partition_ctx, 0, s->cols);
  1095. memset(s->above_skip_ctx, 0, s->cols);
  1096. if (s->s.h.keyframe || s->s.h.intraonly) {
  1097. memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
  1098. } else {
  1099. memset(s->above_mode_ctx, NEARESTMV, s->cols);
  1100. }
  1101. memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
  1102. memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
  1103. memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
  1104. memset(s->above_segpred_ctx, 0, s->cols);
  1105. s->pass = s->s.frames[CUR_FRAME].uses_2pass =
  1106. avctx->active_thread_type == FF_THREAD_FRAME && s->s.h.refreshctx && !s->s.h.parallelmode;
  1107. if ((ret = update_block_buffers(avctx)) < 0) {
  1108. av_log(avctx, AV_LOG_ERROR,
  1109. "Failed to allocate block buffers\n");
  1110. return ret;
  1111. }
  1112. if (s->s.h.refreshctx && s->s.h.parallelmode) {
  1113. int j, k, l, m;
  1114. for (i = 0; i < 4; i++) {
  1115. for (j = 0; j < 2; j++)
  1116. for (k = 0; k < 2; k++)
  1117. for (l = 0; l < 6; l++)
  1118. for (m = 0; m < 6; m++)
  1119. memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
  1120. s->prob.coef[i][j][k][l][m], 3);
  1121. if (s->s.h.txfmmode == i)
  1122. break;
  1123. }
  1124. s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
  1125. ff_thread_finish_setup(avctx);
  1126. } else if (!s->s.h.refreshctx) {
  1127. ff_thread_finish_setup(avctx);
  1128. }
  1129. do {
  1130. yoff = uvoff = 0;
  1131. s->b = s->b_base;
  1132. s->block = s->block_base;
  1133. s->uvblock[0] = s->uvblock_base[0];
  1134. s->uvblock[1] = s->uvblock_base[1];
  1135. s->eob = s->eob_base;
  1136. s->uveob[0] = s->uveob_base[0];
  1137. s->uveob[1] = s->uveob_base[1];
  1138. for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
  1139. set_tile_offset(&s->tile_row_start, &s->tile_row_end,
  1140. tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
  1141. if (s->pass != 2) {
  1142. for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
  1143. int64_t tile_size;
  1144. if (tile_col == s->s.h.tiling.tile_cols - 1 &&
  1145. tile_row == s->s.h.tiling.tile_rows - 1) {
  1146. tile_size = size;
  1147. } else {
  1148. tile_size = AV_RB32(data);
  1149. data += 4;
  1150. size -= 4;
  1151. }
  1152. if (tile_size > size) {
  1153. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
  1154. return AVERROR_INVALIDDATA;
  1155. }
  1156. ret = ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
  1157. if (ret < 0)
  1158. return ret;
  1159. if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit
  1160. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
  1161. return AVERROR_INVALIDDATA;
  1162. }
  1163. data += tile_size;
  1164. size -= tile_size;
  1165. }
  1166. }
  1167. for (row = s->tile_row_start; row < s->tile_row_end;
  1168. row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
  1169. VP9Filter *lflvl_ptr = s->lflvl;
  1170. ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
  1171. for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
  1172. set_tile_offset(&s->tile_col_start, &s->tile_col_end,
  1173. tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
  1174. if (s->pass != 2) {
  1175. memset(s->left_partition_ctx, 0, 8);
  1176. memset(s->left_skip_ctx, 0, 8);
  1177. if (s->s.h.keyframe || s->s.h.intraonly) {
  1178. memset(s->left_mode_ctx, DC_PRED, 16);
  1179. } else {
  1180. memset(s->left_mode_ctx, NEARESTMV, 8);
  1181. }
  1182. memset(s->left_y_nnz_ctx, 0, 16);
  1183. memset(s->left_uv_nnz_ctx, 0, 32);
  1184. memset(s->left_segpred_ctx, 0, 8);
  1185. memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
  1186. }
  1187. for (col = s->tile_col_start;
  1188. col < s->tile_col_end;
  1189. col += 8, yoff2 += 64 * bytesperpixel,
  1190. uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
  1191. // FIXME integrate with lf code (i.e. zero after each
  1192. // use, similar to invtxfm coefficients, or similar)
  1193. if (s->pass != 1) {
  1194. memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
  1195. }
  1196. if (s->pass == 2) {
  1197. decode_sb_mem(avctx, row, col, lflvl_ptr,
  1198. yoff2, uvoff2, BL_64X64);
  1199. } else {
  1200. decode_sb(avctx, row, col, lflvl_ptr,
  1201. yoff2, uvoff2, BL_64X64);
  1202. }
  1203. }
  1204. if (s->pass != 2)
  1205. memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
  1206. }
  1207. if (s->pass == 1)
  1208. continue;
  1209. // backup pre-loopfilter reconstruction data for intra
  1210. // prediction of next row of sb64s
  1211. if (row + 8 < s->rows) {
  1212. memcpy(s->intra_pred_data[0],
  1213. f->data[0] + yoff + 63 * ls_y,
  1214. 8 * s->cols * bytesperpixel);
  1215. memcpy(s->intra_pred_data[1],
  1216. f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
  1217. 8 * s->cols * bytesperpixel >> s->ss_h);
  1218. memcpy(s->intra_pred_data[2],
  1219. f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
  1220. 8 * s->cols * bytesperpixel >> s->ss_h);
  1221. }
  1222. // loopfilter one row
  1223. if (s->s.h.filter.level) {
  1224. yoff2 = yoff;
  1225. uvoff2 = uvoff;
  1226. lflvl_ptr = s->lflvl;
  1227. for (col = 0; col < s->cols;
  1228. col += 8, yoff2 += 64 * bytesperpixel,
  1229. uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
  1230. ff_vp9_loopfilter_sb(avctx, lflvl_ptr, row, col,
  1231. yoff2, uvoff2);
  1232. }
  1233. }
  1234. // FIXME maybe we can make this more finegrained by running the
  1235. // loopfilter per-block instead of after each sbrow
  1236. // In fact that would also make intra pred left preparation easier?
  1237. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, row >> 3, 0);
  1238. }
  1239. }
  1240. if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
  1241. ff_vp9_adapt_probs(s);
  1242. ff_thread_finish_setup(avctx);
  1243. }
  1244. } while (s->pass++ == 1);
  1245. ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
  1246. finish:
  1247. // ref frame setup
  1248. for (i = 0; i < 8; i++) {
  1249. if (s->s.refs[i].f->buf[0])
  1250. ff_thread_release_buffer(avctx, &s->s.refs[i]);
  1251. if (s->next_refs[i].f->buf[0] &&
  1252. (ret = ff_thread_ref_frame(&s->s.refs[i], &s->next_refs[i])) < 0)
  1253. return ret;
  1254. }
  1255. if (!s->s.h.invisible) {
  1256. if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
  1257. return ret;
  1258. *got_frame = 1;
  1259. }
  1260. return pkt->size;
  1261. }
  1262. static void vp9_decode_flush(AVCodecContext *avctx)
  1263. {
  1264. VP9Context *s = avctx->priv_data;
  1265. int i;
  1266. for (i = 0; i < 3; i++)
  1267. vp9_frame_unref(avctx, &s->s.frames[i]);
  1268. for (i = 0; i < 8; i++)
  1269. ff_thread_release_buffer(avctx, &s->s.refs[i]);
  1270. }
  1271. static int init_frames(AVCodecContext *avctx)
  1272. {
  1273. VP9Context *s = avctx->priv_data;
  1274. int i;
  1275. for (i = 0; i < 3; i++) {
  1276. s->s.frames[i].tf.f = av_frame_alloc();
  1277. if (!s->s.frames[i].tf.f) {
  1278. vp9_decode_free(avctx);
  1279. av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
  1280. return AVERROR(ENOMEM);
  1281. }
  1282. }
  1283. for (i = 0; i < 8; i++) {
  1284. s->s.refs[i].f = av_frame_alloc();
  1285. s->next_refs[i].f = av_frame_alloc();
  1286. if (!s->s.refs[i].f || !s->next_refs[i].f) {
  1287. vp9_decode_free(avctx);
  1288. av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
  1289. return AVERROR(ENOMEM);
  1290. }
  1291. }
  1292. return 0;
  1293. }
  1294. static av_cold int vp9_decode_init(AVCodecContext *avctx)
  1295. {
  1296. VP9Context *s = avctx->priv_data;
  1297. avctx->internal->allocate_progress = 1;
  1298. s->last_bpp = 0;
  1299. s->s.h.filter.sharpness = -1;
  1300. return init_frames(avctx);
  1301. }
  1302. #if HAVE_THREADS
  1303. static av_cold int vp9_decode_init_thread_copy(AVCodecContext *avctx)
  1304. {
  1305. return init_frames(avctx);
  1306. }
  1307. static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1308. {
  1309. int i, ret;
  1310. VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
  1311. for (i = 0; i < 3; i++) {
  1312. if (s->s.frames[i].tf.f->buf[0])
  1313. vp9_frame_unref(dst, &s->s.frames[i]);
  1314. if (ssrc->s.frames[i].tf.f->buf[0]) {
  1315. if ((ret = vp9_frame_ref(dst, &s->s.frames[i], &ssrc->s.frames[i])) < 0)
  1316. return ret;
  1317. }
  1318. }
  1319. for (i = 0; i < 8; i++) {
  1320. if (s->s.refs[i].f->buf[0])
  1321. ff_thread_release_buffer(dst, &s->s.refs[i]);
  1322. if (ssrc->next_refs[i].f->buf[0]) {
  1323. if ((ret = ff_thread_ref_frame(&s->s.refs[i], &ssrc->next_refs[i])) < 0)
  1324. return ret;
  1325. }
  1326. }
  1327. s->s.h.invisible = ssrc->s.h.invisible;
  1328. s->s.h.keyframe = ssrc->s.h.keyframe;
  1329. s->s.h.intraonly = ssrc->s.h.intraonly;
  1330. s->ss_v = ssrc->ss_v;
  1331. s->ss_h = ssrc->ss_h;
  1332. s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
  1333. s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
  1334. s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
  1335. s->bytesperpixel = ssrc->bytesperpixel;
  1336. s->gf_fmt = ssrc->gf_fmt;
  1337. s->w = ssrc->w;
  1338. s->h = ssrc->h;
  1339. s->s.h.bpp = ssrc->s.h.bpp;
  1340. s->bpp_index = ssrc->bpp_index;
  1341. s->pix_fmt = ssrc->pix_fmt;
  1342. memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
  1343. memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
  1344. memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
  1345. sizeof(s->s.h.segmentation.feat));
  1346. return 0;
  1347. }
  1348. #endif
  1349. AVCodec ff_vp9_decoder = {
  1350. .name = "vp9",
  1351. .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
  1352. .type = AVMEDIA_TYPE_VIDEO,
  1353. .id = AV_CODEC_ID_VP9,
  1354. .priv_data_size = sizeof(VP9Context),
  1355. .init = vp9_decode_init,
  1356. .close = vp9_decode_free,
  1357. .decode = vp9_decode_frame,
  1358. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1359. .flush = vp9_decode_flush,
  1360. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp9_decode_init_thread_copy),
  1361. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp9_decode_update_thread_context),
  1362. .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
  1363. };