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.

1916 lines
74KB

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