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.

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