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.

1919 lines
75KB

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