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.

1789 lines
70KB

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