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.

1835 lines
71KB

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