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.

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