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.

1552 lines
60KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/avassert.h"
  24. #include "avcodec.h"
  25. #include "get_bits.h"
  26. #include "internal.h"
  27. #include "videodsp.h"
  28. #include "vp56.h"
  29. #include "vp9.h"
  30. #include "vp9data.h"
  31. #define VP9_SYNCCODE 0x498342
  32. #define MAX_PROB 255
  33. static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
  34. {
  35. ff_thread_release_buffer(avctx, &f->tf);
  36. av_buffer_unref(&f->segmentation_map_buf);
  37. av_buffer_unref(&f->mv_buf);
  38. f->segmentation_map = NULL;
  39. f->mv = NULL;
  40. }
  41. static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
  42. {
  43. VP9Context *s = avctx->priv_data;
  44. int ret, sz;
  45. ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF);
  46. if (ret < 0)
  47. return ret;
  48. sz = 64 * s->sb_cols * s->sb_rows;
  49. f->segmentation_map_buf = av_buffer_allocz(sz * sizeof(*f->segmentation_map));
  50. f->mv_buf = av_buffer_allocz(sz * sizeof(*f->mv));
  51. if (!f->segmentation_map_buf || !f->mv_buf) {
  52. vp9_frame_unref(avctx, f);
  53. return AVERROR(ENOMEM);
  54. }
  55. f->segmentation_map = f->segmentation_map_buf->data;
  56. f->mv = (VP9MVRefPair*)f->mv_buf->data;
  57. if (s->segmentation.enabled && !s->segmentation.update_map &&
  58. !s->keyframe && !s->intraonly && !s->errorres)
  59. memcpy(f->segmentation_map, s->frames[LAST_FRAME].segmentation_map, sz);
  60. return 0;
  61. }
  62. static int vp9_frame_ref(VP9Frame *dst, VP9Frame *src)
  63. {
  64. int ret;
  65. dst->segmentation_map_buf = av_buffer_ref(src->segmentation_map_buf);
  66. dst->mv_buf = av_buffer_ref(src->mv_buf);
  67. if (!dst->segmentation_map_buf || !dst->mv_buf) {
  68. ret = AVERROR(ENOMEM);
  69. goto fail;
  70. }
  71. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  72. if (ret < 0)
  73. goto fail;
  74. dst->segmentation_map = src->segmentation_map;
  75. dst->mv = src->mv;
  76. return 0;
  77. fail:
  78. av_buffer_unref(&dst->segmentation_map_buf);
  79. av_buffer_unref(&dst->mv_buf);
  80. return ret;
  81. }
  82. static void vp9_decode_flush(AVCodecContext *avctx)
  83. {
  84. VP9Context *s = avctx->priv_data;
  85. int i;
  86. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
  87. vp9_frame_unref(avctx, &s->frames[i]);
  88. for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++)
  89. ff_thread_release_buffer(avctx, &s->refs[i]);
  90. s->use_last_frame_mvs = 0;
  91. s->alloc_width = 0;
  92. s->alloc_height = 0;
  93. }
  94. static int update_size(AVCodecContext *avctx, int w, int h)
  95. {
  96. VP9Context *s = avctx->priv_data;
  97. uint8_t *p;
  98. int nb_blocks, nb_superblocks;
  99. if (s->above_partition_ctx && w == s->alloc_width && h == s->alloc_height)
  100. return 0;
  101. vp9_decode_flush(avctx);
  102. if (w <= 0 || h <= 0)
  103. return AVERROR_INVALIDDATA;
  104. avctx->width = w;
  105. avctx->height = h;
  106. s->sb_cols = (w + 63) >> 6;
  107. s->sb_rows = (h + 63) >> 6;
  108. s->cols = (w + 7) >> 3;
  109. s->rows = (h + 7) >> 3;
  110. #define assign(var, type, n) var = (type)p; p += s->sb_cols * n * sizeof(*var)
  111. av_free(s->above_partition_ctx);
  112. p = av_malloc(s->sb_cols *
  113. (240 + sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
  114. if (!p)
  115. return AVERROR(ENOMEM);
  116. assign(s->above_partition_ctx, uint8_t *, 8);
  117. assign(s->above_skip_ctx, uint8_t *, 8);
  118. assign(s->above_txfm_ctx, uint8_t *, 8);
  119. assign(s->above_mode_ctx, uint8_t *, 16);
  120. assign(s->above_y_nnz_ctx, uint8_t *, 16);
  121. assign(s->above_uv_nnz_ctx[0], uint8_t *, 8);
  122. assign(s->above_uv_nnz_ctx[1], uint8_t *, 8);
  123. assign(s->intra_pred_data[0], uint8_t *, 64);
  124. assign(s->intra_pred_data[1], uint8_t *, 32);
  125. assign(s->intra_pred_data[2], uint8_t *, 32);
  126. assign(s->above_segpred_ctx, uint8_t *, 8);
  127. assign(s->above_intra_ctx, uint8_t *, 8);
  128. assign(s->above_comp_ctx, uint8_t *, 8);
  129. assign(s->above_ref_ctx, uint8_t *, 8);
  130. assign(s->above_filter_ctx, uint8_t *, 8);
  131. assign(s->lflvl, VP9Filter *, 1);
  132. assign(s->above_mv_ctx, VP56mv(*)[2], 16);
  133. #undef assign
  134. av_freep(&s->b_base);
  135. av_freep(&s->block_base);
  136. if (avctx->active_thread_type & FF_THREAD_FRAME) {
  137. nb_blocks = s->cols * s->rows;
  138. nb_superblocks = s->sb_cols * s->sb_rows;
  139. } else {
  140. nb_blocks = nb_superblocks = 1;
  141. }
  142. s->b_base = av_malloc_array(nb_blocks, sizeof(*s->b_base));
  143. s->block_base = av_mallocz_array(nb_superblocks, (64 * 64 + 128) * 3);
  144. if (!s->b_base || !s->block_base)
  145. return AVERROR(ENOMEM);
  146. s->uvblock_base[0] = s->block_base + nb_superblocks * 64 * 64;
  147. s->uvblock_base[1] = s->uvblock_base[0] + nb_superblocks * 32 * 32;
  148. s->eob_base = (uint8_t *)(s->uvblock_base[1] + nb_superblocks * 32 * 32);
  149. s->uveob_base[0] = s->eob_base + nb_superblocks * 256;
  150. s->uveob_base[1] = s->uveob_base[0] + nb_superblocks * 64;
  151. s->alloc_width = w;
  152. s->alloc_height = h;
  153. return 0;
  154. }
  155. // The sign bit is at the end, not the start, of a bit sequence
  156. static av_always_inline int get_bits_with_sign(GetBitContext *gb, int n)
  157. {
  158. int v = get_bits(gb, n);
  159. return get_bits1(gb) ? -v : v;
  160. }
  161. static av_always_inline int inv_recenter_nonneg(int v, int m)
  162. {
  163. if (v > 2 * m)
  164. return v;
  165. if (v & 1)
  166. return m - ((v + 1) >> 1);
  167. return m + (v >> 1);
  168. }
  169. // differential forward probability updates
  170. static int update_prob(VP56RangeCoder *c, int p)
  171. {
  172. static const int inv_map_table[MAX_PROB - 1] = {
  173. 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
  174. 189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
  175. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
  176. 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
  177. 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
  178. 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  179. 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
  180. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
  181. 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
  182. 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
  183. 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
  184. 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
  185. 161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  186. 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
  187. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
  188. 207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
  189. 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
  190. 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
  191. 252, 253,
  192. };
  193. int d;
  194. /* This code is trying to do a differential probability update. For a
  195. * current probability A in the range [1, 255], the difference to a new
  196. * probability of any value can be expressed differentially as 1-A, 255-A
  197. * where some part of this (absolute range) exists both in positive as
  198. * well as the negative part, whereas another part only exists in one
  199. * half. We're trying to code this shared part differentially, i.e.
  200. * times two where the value of the lowest bit specifies the sign, and
  201. * the single part is then coded on top of this. This absolute difference
  202. * then again has a value of [0, 254], but a bigger value in this range
  203. * indicates that we're further away from the original value A, so we
  204. * can code this as a VLC code, since higher values are increasingly
  205. * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
  206. * updates vs. the 'fine, exact' updates further down the range, which
  207. * adds one extra dimension to this differential update model. */
  208. if (!vp8_rac_get(c)) {
  209. d = vp8_rac_get_uint(c, 4) + 0;
  210. } else if (!vp8_rac_get(c)) {
  211. d = vp8_rac_get_uint(c, 4) + 16;
  212. } else if (!vp8_rac_get(c)) {
  213. d = vp8_rac_get_uint(c, 5) + 32;
  214. } else {
  215. d = vp8_rac_get_uint(c, 7);
  216. if (d >= 65) {
  217. d = (d << 1) - 65 + vp8_rac_get(c);
  218. d = av_clip(d, 0, MAX_PROB - 65 - 1);
  219. }
  220. d += 64;
  221. }
  222. return p <= 128
  223. ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1)
  224. : 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
  225. }
  226. static int decode_frame_header(AVCodecContext *avctx,
  227. const uint8_t *data, int size, int *ref)
  228. {
  229. VP9Context *s = avctx->priv_data;
  230. int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
  231. int last_invisible;
  232. const uint8_t *data2;
  233. /* general header */
  234. if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
  235. av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
  236. return ret;
  237. }
  238. if (get_bits(&s->gb, 2) != 0x2) { // frame marker
  239. av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
  240. return AVERROR_INVALIDDATA;
  241. }
  242. s->profile = get_bits1(&s->gb);
  243. if (get_bits1(&s->gb)) { // reserved bit
  244. av_log(avctx, AV_LOG_ERROR, "Reserved bit should be zero\n");
  245. return AVERROR_INVALIDDATA;
  246. }
  247. if (get_bits1(&s->gb)) {
  248. *ref = get_bits(&s->gb, 3);
  249. return 0;
  250. }
  251. s->last_keyframe = s->keyframe;
  252. s->keyframe = !get_bits1(&s->gb);
  253. last_invisible = s->invisible;
  254. s->invisible = !get_bits1(&s->gb);
  255. s->errorres = get_bits1(&s->gb);
  256. s->use_last_frame_mvs = !s->errorres && !last_invisible;
  257. if (s->keyframe) {
  258. if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
  259. av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
  260. return AVERROR_INVALIDDATA;
  261. }
  262. s->colorspace = get_bits(&s->gb, 3);
  263. if (s->colorspace == 7) { // RGB = profile 1
  264. av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile 0\n");
  265. return AVERROR_INVALIDDATA;
  266. }
  267. s->fullrange = get_bits1(&s->gb);
  268. // subsampling bits
  269. if (s->profile == 1 || s->profile == 3) {
  270. s->sub_x = get_bits1(&s->gb);
  271. s->sub_y = get_bits1(&s->gb);
  272. if (s->sub_x && s->sub_y) {
  273. av_log(avctx, AV_LOG_ERROR,
  274. "4:2:0 color not supported in profile 1 or 3\n");
  275. return AVERROR_INVALIDDATA;
  276. }
  277. if (get_bits1(&s->gb)) { // reserved bit
  278. av_log(avctx, AV_LOG_ERROR, "Reserved bit should be zero\n");
  279. return AVERROR_INVALIDDATA;
  280. }
  281. } else {
  282. s->sub_x = s->sub_y = 1;
  283. }
  284. if (!s->sub_x || !s->sub_y) {
  285. avpriv_report_missing_feature(avctx, "Subsampling %d:%d",
  286. s->sub_x, s->sub_y);
  287. return AVERROR_PATCHWELCOME;
  288. }
  289. s->refreshrefmask = 0xff;
  290. w = get_bits(&s->gb, 16) + 1;
  291. h = get_bits(&s->gb, 16) + 1;
  292. if (get_bits1(&s->gb)) // display size
  293. skip_bits(&s->gb, 32);
  294. } else {
  295. s->intraonly = s->invisible ? get_bits1(&s->gb) : 0;
  296. s->resetctx = s->errorres ? 0 : get_bits(&s->gb, 2);
  297. if (s->intraonly) {
  298. if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
  299. av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
  300. return AVERROR_INVALIDDATA;
  301. }
  302. s->refreshrefmask = get_bits(&s->gb, 8);
  303. w = get_bits(&s->gb, 16) + 1;
  304. h = get_bits(&s->gb, 16) + 1;
  305. if (get_bits1(&s->gb)) // display size
  306. skip_bits(&s->gb, 32);
  307. } else {
  308. s->refreshrefmask = get_bits(&s->gb, 8);
  309. s->refidx[0] = get_bits(&s->gb, 3);
  310. s->signbias[0] = get_bits1(&s->gb);
  311. s->refidx[1] = get_bits(&s->gb, 3);
  312. s->signbias[1] = get_bits1(&s->gb);
  313. s->refidx[2] = get_bits(&s->gb, 3);
  314. s->signbias[2] = get_bits1(&s->gb);
  315. if (!s->refs[s->refidx[0]].f->buf[0] ||
  316. !s->refs[s->refidx[1]].f->buf[0] ||
  317. !s->refs[s->refidx[2]].f->buf[0]) {
  318. av_log(avctx, AV_LOG_ERROR,
  319. "Not all references are available\n");
  320. return AVERROR_INVALIDDATA;
  321. }
  322. if (get_bits1(&s->gb)) {
  323. w = s->refs[s->refidx[0]].f->width;
  324. h = s->refs[s->refidx[0]].f->height;
  325. } else if (get_bits1(&s->gb)) {
  326. w = s->refs[s->refidx[1]].f->width;
  327. h = s->refs[s->refidx[1]].f->height;
  328. } else if (get_bits1(&s->gb)) {
  329. w = s->refs[s->refidx[2]].f->width;
  330. h = s->refs[s->refidx[2]].f->height;
  331. } else {
  332. w = get_bits(&s->gb, 16) + 1;
  333. h = get_bits(&s->gb, 16) + 1;
  334. }
  335. if (get_bits1(&s->gb)) // display size
  336. skip_bits(&s->gb, 32);
  337. s->highprecisionmvs = get_bits1(&s->gb);
  338. s->filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
  339. get_bits(&s->gb, 2);
  340. s->allowcompinter = s->signbias[0] != s->signbias[1] ||
  341. s->signbias[0] != s->signbias[2];
  342. if (s->allowcompinter) {
  343. if (s->signbias[0] == s->signbias[1]) {
  344. s->fixcompref = 2;
  345. s->varcompref[0] = 0;
  346. s->varcompref[1] = 1;
  347. } else if (s->signbias[0] == s->signbias[2]) {
  348. s->fixcompref = 1;
  349. s->varcompref[0] = 0;
  350. s->varcompref[1] = 2;
  351. } else {
  352. s->fixcompref = 0;
  353. s->varcompref[0] = 1;
  354. s->varcompref[1] = 2;
  355. }
  356. }
  357. }
  358. }
  359. s->refreshctx = s->errorres ? 0 : get_bits1(&s->gb);
  360. s->parallelmode = s->errorres ? 1 : get_bits1(&s->gb);
  361. s->framectxid = c = get_bits(&s->gb, 2);
  362. /* loopfilter header data */
  363. s->filter.level = get_bits(&s->gb, 6);
  364. sharp = get_bits(&s->gb, 3);
  365. /* If sharpness changed, reinit lim/mblim LUTs. if it didn't change,
  366. * keep the old cache values since they are still valid. */
  367. if (s->filter.sharpness != sharp)
  368. memset(s->filter.lim_lut, 0, sizeof(s->filter.lim_lut));
  369. s->filter.sharpness = sharp;
  370. if ((s->lf_delta.enabled = get_bits1(&s->gb))) {
  371. if (get_bits1(&s->gb)) {
  372. for (i = 0; i < 4; i++)
  373. if (get_bits1(&s->gb))
  374. s->lf_delta.ref[i] = get_bits_with_sign(&s->gb, 6);
  375. for (i = 0; i < 2; i++)
  376. if (get_bits1(&s->gb))
  377. s->lf_delta.mode[i] = get_bits_with_sign(&s->gb, 6);
  378. }
  379. } else {
  380. memset(&s->lf_delta, 0, sizeof(s->lf_delta));
  381. }
  382. /* quantization header data */
  383. s->yac_qi = get_bits(&s->gb, 8);
  384. s->ydc_qdelta = get_bits1(&s->gb) ? get_bits_with_sign(&s->gb, 4) : 0;
  385. s->uvdc_qdelta = get_bits1(&s->gb) ? get_bits_with_sign(&s->gb, 4) : 0;
  386. s->uvac_qdelta = get_bits1(&s->gb) ? get_bits_with_sign(&s->gb, 4) : 0;
  387. s->lossless = s->yac_qi == 0 && s->ydc_qdelta == 0 &&
  388. s->uvdc_qdelta == 0 && s->uvac_qdelta == 0;
  389. /* segmentation header info */
  390. if ((s->segmentation.enabled = get_bits1(&s->gb))) {
  391. if ((s->segmentation.update_map = get_bits1(&s->gb))) {
  392. for (i = 0; i < 7; i++)
  393. s->prob.seg[i] = get_bits1(&s->gb) ?
  394. get_bits(&s->gb, 8) : 255;
  395. if ((s->segmentation.temporal = get_bits1(&s->gb)))
  396. for (i = 0; i < 3; i++)
  397. s->prob.segpred[i] = get_bits1(&s->gb) ?
  398. get_bits(&s->gb, 8) : 255;
  399. }
  400. if (get_bits1(&s->gb)) {
  401. s->segmentation.absolute_vals = get_bits1(&s->gb);
  402. for (i = 0; i < 8; i++) {
  403. if ((s->segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
  404. s->segmentation.feat[i].q_val = get_bits_with_sign(&s->gb, 8);
  405. if ((s->segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
  406. s->segmentation.feat[i].lf_val = get_bits_with_sign(&s->gb, 6);
  407. if ((s->segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
  408. s->segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
  409. s->segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
  410. }
  411. }
  412. } else {
  413. s->segmentation.feat[0].q_enabled = 0;
  414. s->segmentation.feat[0].lf_enabled = 0;
  415. s->segmentation.feat[0].skip_enabled = 0;
  416. s->segmentation.feat[0].ref_enabled = 0;
  417. }
  418. // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
  419. for (i = 0; i < (s->segmentation.enabled ? 8 : 1); i++) {
  420. int qyac, qydc, quvac, quvdc, lflvl, sh;
  421. if (s->segmentation.feat[i].q_enabled) {
  422. if (s->segmentation.absolute_vals)
  423. qyac = s->segmentation.feat[i].q_val;
  424. else
  425. qyac = s->yac_qi + s->segmentation.feat[i].q_val;
  426. } else {
  427. qyac = s->yac_qi;
  428. }
  429. qydc = av_clip_uintp2(qyac + s->ydc_qdelta, 8);
  430. quvdc = av_clip_uintp2(qyac + s->uvdc_qdelta, 8);
  431. quvac = av_clip_uintp2(qyac + s->uvac_qdelta, 8);
  432. qyac = av_clip_uintp2(qyac, 8);
  433. s->segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[qydc];
  434. s->segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[qyac];
  435. s->segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[quvdc];
  436. s->segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[quvac];
  437. sh = s->filter.level >= 32;
  438. if (s->segmentation.feat[i].lf_enabled) {
  439. if (s->segmentation.absolute_vals)
  440. lflvl = s->segmentation.feat[i].lf_val;
  441. else
  442. lflvl = s->filter.level + s->segmentation.feat[i].lf_val;
  443. } else {
  444. lflvl = s->filter.level;
  445. }
  446. s->segmentation.feat[i].lflvl[0][0] =
  447. s->segmentation.feat[i].lflvl[0][1] =
  448. av_clip_uintp2(lflvl + (s->lf_delta.ref[0] << sh), 6);
  449. for (j = 1; j < 4; j++) {
  450. s->segmentation.feat[i].lflvl[j][0] =
  451. av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
  452. s->lf_delta.mode[0]) << sh), 6);
  453. s->segmentation.feat[i].lflvl[j][1] =
  454. av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
  455. s->lf_delta.mode[1]) << sh), 6);
  456. }
  457. }
  458. /* tiling info */
  459. if ((ret = update_size(avctx, w, h)) < 0) {
  460. av_log(avctx, AV_LOG_ERROR,
  461. "Failed to initialize decoder for %dx%d\n", w, h);
  462. return ret;
  463. }
  464. for (s->tiling.log2_tile_cols = 0;
  465. (s->sb_cols >> s->tiling.log2_tile_cols) > 64;
  466. s->tiling.log2_tile_cols++) ;
  467. for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
  468. max = FFMAX(0, max - 1);
  469. while (max > s->tiling.log2_tile_cols) {
  470. if (get_bits1(&s->gb))
  471. s->tiling.log2_tile_cols++;
  472. else
  473. break;
  474. }
  475. s->tiling.log2_tile_rows = decode012(&s->gb);
  476. s->tiling.tile_rows = 1 << s->tiling.log2_tile_rows;
  477. if (s->tiling.tile_cols != (1 << s->tiling.log2_tile_cols)) {
  478. s->tiling.tile_cols = 1 << s->tiling.log2_tile_cols;
  479. s->c_b = av_fast_realloc(s->c_b, &s->c_b_size,
  480. sizeof(VP56RangeCoder) *
  481. s->tiling.tile_cols);
  482. if (!s->c_b) {
  483. av_log(avctx, AV_LOG_ERROR,
  484. "Ran out of memory during range coder init\n");
  485. return AVERROR(ENOMEM);
  486. }
  487. }
  488. if (s->keyframe || s->errorres || s->intraonly) {
  489. s->prob_ctx[0].p =
  490. s->prob_ctx[1].p =
  491. s->prob_ctx[2].p =
  492. s->prob_ctx[3].p = ff_vp9_default_probs;
  493. memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
  494. sizeof(ff_vp9_default_coef_probs));
  495. memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
  496. sizeof(ff_vp9_default_coef_probs));
  497. memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
  498. sizeof(ff_vp9_default_coef_probs));
  499. memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
  500. sizeof(ff_vp9_default_coef_probs));
  501. }
  502. // next 16 bits is size of the rest of the header (arith-coded)
  503. size2 = get_bits(&s->gb, 16);
  504. data2 = align_get_bits(&s->gb);
  505. if (size2 > size - (data2 - data)) {
  506. av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
  507. return AVERROR_INVALIDDATA;
  508. }
  509. ff_vp56_init_range_decoder(&s->c, data2, size2);
  510. if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
  511. av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
  512. return AVERROR_INVALIDDATA;
  513. }
  514. if (s->keyframe || s->intraonly)
  515. memset(s->counts.coef, 0,
  516. sizeof(s->counts.coef) + sizeof(s->counts.eob));
  517. else
  518. memset(&s->counts, 0, sizeof(s->counts));
  519. /* FIXME is it faster to not copy here, but do it down in the fw updates
  520. * as explicit copies if the fw update is missing (and skip the copy upon
  521. * fw update)? */
  522. s->prob.p = s->prob_ctx[c].p;
  523. // txfm updates
  524. if (s->lossless) {
  525. s->txfmmode = TX_4X4;
  526. } else {
  527. s->txfmmode = vp8_rac_get_uint(&s->c, 2);
  528. if (s->txfmmode == 3)
  529. s->txfmmode += vp8_rac_get(&s->c);
  530. if (s->txfmmode == TX_SWITCHABLE) {
  531. for (i = 0; i < 2; i++)
  532. if (vp56_rac_get_prob_branchy(&s->c, 252))
  533. s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
  534. for (i = 0; i < 2; i++)
  535. for (j = 0; j < 2; j++)
  536. if (vp56_rac_get_prob_branchy(&s->c, 252))
  537. s->prob.p.tx16p[i][j] =
  538. update_prob(&s->c, s->prob.p.tx16p[i][j]);
  539. for (i = 0; i < 2; i++)
  540. for (j = 0; j < 3; j++)
  541. if (vp56_rac_get_prob_branchy(&s->c, 252))
  542. s->prob.p.tx32p[i][j] =
  543. update_prob(&s->c, s->prob.p.tx32p[i][j]);
  544. }
  545. }
  546. // coef updates
  547. for (i = 0; i < 4; i++) {
  548. uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
  549. if (vp8_rac_get(&s->c)) {
  550. for (j = 0; j < 2; j++)
  551. for (k = 0; k < 2; k++)
  552. for (l = 0; l < 6; l++)
  553. for (m = 0; m < 6; m++) {
  554. uint8_t *p = s->prob.coef[i][j][k][l][m];
  555. uint8_t *r = ref[j][k][l][m];
  556. if (m >= 3 && l == 0) // dc only has 3 pt
  557. break;
  558. for (n = 0; n < 3; n++) {
  559. if (vp56_rac_get_prob_branchy(&s->c, 252))
  560. p[n] = update_prob(&s->c, r[n]);
  561. else
  562. p[n] = r[n];
  563. }
  564. p[3] = 0;
  565. }
  566. } else {
  567. for (j = 0; j < 2; j++)
  568. for (k = 0; k < 2; k++)
  569. for (l = 0; l < 6; l++)
  570. for (m = 0; m < 6; m++) {
  571. uint8_t *p = s->prob.coef[i][j][k][l][m];
  572. uint8_t *r = ref[j][k][l][m];
  573. if (m > 3 && l == 0) // dc only has 3 pt
  574. break;
  575. memcpy(p, r, 3);
  576. p[3] = 0;
  577. }
  578. }
  579. if (s->txfmmode == i)
  580. break;
  581. }
  582. // mode updates
  583. for (i = 0; i < 3; i++)
  584. if (vp56_rac_get_prob_branchy(&s->c, 252))
  585. s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
  586. if (!s->keyframe && !s->intraonly) {
  587. for (i = 0; i < 7; i++)
  588. for (j = 0; j < 3; j++)
  589. if (vp56_rac_get_prob_branchy(&s->c, 252))
  590. s->prob.p.mv_mode[i][j] =
  591. update_prob(&s->c, s->prob.p.mv_mode[i][j]);
  592. if (s->filtermode == FILTER_SWITCHABLE)
  593. for (i = 0; i < 4; i++)
  594. for (j = 0; j < 2; j++)
  595. if (vp56_rac_get_prob_branchy(&s->c, 252))
  596. s->prob.p.filter[i][j] =
  597. update_prob(&s->c, s->prob.p.filter[i][j]);
  598. for (i = 0; i < 4; i++)
  599. if (vp56_rac_get_prob_branchy(&s->c, 252))
  600. s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
  601. if (s->allowcompinter) {
  602. s->comppredmode = vp8_rac_get(&s->c);
  603. if (s->comppredmode)
  604. s->comppredmode += vp8_rac_get(&s->c);
  605. if (s->comppredmode == PRED_SWITCHABLE)
  606. for (i = 0; i < 5; i++)
  607. if (vp56_rac_get_prob_branchy(&s->c, 252))
  608. s->prob.p.comp[i] =
  609. update_prob(&s->c, s->prob.p.comp[i]);
  610. } else {
  611. s->comppredmode = PRED_SINGLEREF;
  612. }
  613. if (s->comppredmode != PRED_COMPREF) {
  614. for (i = 0; i < 5; i++) {
  615. if (vp56_rac_get_prob_branchy(&s->c, 252))
  616. s->prob.p.single_ref[i][0] =
  617. update_prob(&s->c, s->prob.p.single_ref[i][0]);
  618. if (vp56_rac_get_prob_branchy(&s->c, 252))
  619. s->prob.p.single_ref[i][1] =
  620. update_prob(&s->c, s->prob.p.single_ref[i][1]);
  621. }
  622. }
  623. if (s->comppredmode != PRED_SINGLEREF) {
  624. for (i = 0; i < 5; i++)
  625. if (vp56_rac_get_prob_branchy(&s->c, 252))
  626. s->prob.p.comp_ref[i] =
  627. update_prob(&s->c, s->prob.p.comp_ref[i]);
  628. }
  629. for (i = 0; i < 4; i++)
  630. for (j = 0; j < 9; j++)
  631. if (vp56_rac_get_prob_branchy(&s->c, 252))
  632. s->prob.p.y_mode[i][j] =
  633. update_prob(&s->c, s->prob.p.y_mode[i][j]);
  634. for (i = 0; i < 4; i++)
  635. for (j = 0; j < 4; j++)
  636. for (k = 0; k < 3; k++)
  637. if (vp56_rac_get_prob_branchy(&s->c, 252))
  638. s->prob.p.partition[3 - i][j][k] =
  639. update_prob(&s->c,
  640. s->prob.p.partition[3 - i][j][k]);
  641. // mv fields don't use the update_prob subexp model for some reason
  642. for (i = 0; i < 3; i++)
  643. if (vp56_rac_get_prob_branchy(&s->c, 252))
  644. s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  645. for (i = 0; i < 2; i++) {
  646. if (vp56_rac_get_prob_branchy(&s->c, 252))
  647. s->prob.p.mv_comp[i].sign =
  648. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  649. for (j = 0; j < 10; j++)
  650. if (vp56_rac_get_prob_branchy(&s->c, 252))
  651. s->prob.p.mv_comp[i].classes[j] =
  652. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  653. if (vp56_rac_get_prob_branchy(&s->c, 252))
  654. s->prob.p.mv_comp[i].class0 =
  655. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  656. for (j = 0; j < 10; j++)
  657. if (vp56_rac_get_prob_branchy(&s->c, 252))
  658. s->prob.p.mv_comp[i].bits[j] =
  659. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  660. }
  661. for (i = 0; i < 2; i++) {
  662. for (j = 0; j < 2; j++)
  663. for (k = 0; k < 3; k++)
  664. if (vp56_rac_get_prob_branchy(&s->c, 252))
  665. s->prob.p.mv_comp[i].class0_fp[j][k] =
  666. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  667. for (j = 0; j < 3; j++)
  668. if (vp56_rac_get_prob_branchy(&s->c, 252))
  669. s->prob.p.mv_comp[i].fp[j] =
  670. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  671. }
  672. if (s->highprecisionmvs) {
  673. for (i = 0; i < 2; i++) {
  674. if (vp56_rac_get_prob_branchy(&s->c, 252))
  675. s->prob.p.mv_comp[i].class0_hp =
  676. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  677. if (vp56_rac_get_prob_branchy(&s->c, 252))
  678. s->prob.p.mv_comp[i].hp =
  679. (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
  680. }
  681. }
  682. }
  683. return (data2 - data) + size2;
  684. }
  685. static int decode_subblock(AVCodecContext *avctx, int row, int col,
  686. VP9Filter *lflvl,
  687. ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
  688. {
  689. VP9Context *s = avctx->priv_data;
  690. AVFrame *f = s->frames[CUR_FRAME].tf.f;
  691. int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
  692. (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
  693. int ret;
  694. const uint8_t *p = s->keyframe ? ff_vp9_default_kf_partition_probs[bl][c]
  695. : s->prob.p.partition[bl][c];
  696. enum BlockPartition bp;
  697. ptrdiff_t hbs = 4 >> bl;
  698. if (bl == BL_8X8) {
  699. bp = vp8_rac_get_tree(&s->c, ff_vp9_partition_tree, p);
  700. ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
  701. } else if (col + hbs < s->cols) {
  702. if (row + hbs < s->rows) {
  703. bp = vp8_rac_get_tree(&s->c, ff_vp9_partition_tree, p);
  704. switch (bp) {
  705. case PARTITION_NONE:
  706. ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
  707. bl, bp);
  708. break;
  709. case PARTITION_H:
  710. ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
  711. bl, bp);
  712. if (!ret) {
  713. yoff += hbs * 8 * f->linesize[0];
  714. uvoff += hbs * 4 * f->linesize[1];
  715. ret = ff_vp9_decode_block(avctx, row + hbs, col, lflvl,
  716. yoff, uvoff, bl, bp);
  717. }
  718. break;
  719. case PARTITION_V:
  720. ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
  721. bl, bp);
  722. if (!ret) {
  723. yoff += hbs * 8;
  724. uvoff += hbs * 4;
  725. ret = ff_vp9_decode_block(avctx, row, col + hbs, lflvl,
  726. yoff, uvoff, bl, bp);
  727. }
  728. break;
  729. case PARTITION_SPLIT:
  730. ret = decode_subblock(avctx, row, col, lflvl,
  731. yoff, uvoff, bl + 1);
  732. if (!ret) {
  733. ret = decode_subblock(avctx, row, col + hbs, lflvl,
  734. yoff + 8 * hbs, uvoff + 4 * hbs,
  735. bl + 1);
  736. if (!ret) {
  737. yoff += hbs * 8 * f->linesize[0];
  738. uvoff += hbs * 4 * f->linesize[1];
  739. ret = decode_subblock(avctx, row + hbs, col, lflvl,
  740. yoff, uvoff, bl + 1);
  741. if (!ret) {
  742. ret = decode_subblock(avctx, row + hbs, col + hbs,
  743. lflvl, yoff + 8 * hbs,
  744. uvoff + 4 * hbs, bl + 1);
  745. }
  746. }
  747. }
  748. break;
  749. default:
  750. av_log(avctx, AV_LOG_ERROR, "Unexpected partition %d.", bp);
  751. return AVERROR_INVALIDDATA;
  752. }
  753. } else if (vp56_rac_get_prob_branchy(&s->c, p[1])) {
  754. bp = PARTITION_SPLIT;
  755. ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  756. if (!ret)
  757. ret = decode_subblock(avctx, row, col + hbs, lflvl,
  758. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
  759. } else {
  760. bp = PARTITION_H;
  761. ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
  762. bl, bp);
  763. }
  764. } else if (row + hbs < s->rows) {
  765. if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
  766. bp = PARTITION_SPLIT;
  767. ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  768. if (!ret) {
  769. yoff += hbs * 8 * f->linesize[0];
  770. uvoff += hbs * 4 * f->linesize[1];
  771. ret = decode_subblock(avctx, row + hbs, col, lflvl,
  772. yoff, uvoff, bl + 1);
  773. }
  774. } else {
  775. bp = PARTITION_V;
  776. ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
  777. bl, bp);
  778. }
  779. } else {
  780. bp = PARTITION_SPLIT;
  781. ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
  782. }
  783. s->counts.partition[bl][c][bp]++;
  784. return ret;
  785. }
  786. static int decode_superblock_mem(AVCodecContext *avctx, int row, int col, struct VP9Filter *lflvl,
  787. ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
  788. {
  789. VP9Context *s = avctx->priv_data;
  790. VP9Block *b = s->b;
  791. ptrdiff_t hbs = 4 >> bl;
  792. AVFrame *f = s->frames[CUR_FRAME].tf.f;
  793. ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
  794. int res;
  795. if (bl == BL_8X8) {
  796. av_assert2(b->bl == BL_8X8);
  797. res = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
  798. } else if (s->b->bl == bl) {
  799. if ((res = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp)) < 0)
  800. return res;
  801. if (b->bp == PARTITION_H && row + hbs < s->rows) {
  802. yoff += hbs * 8 * y_stride;
  803. uvoff += hbs * 4 * uv_stride;
  804. res = ff_vp9_decode_block(avctx, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
  805. } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
  806. yoff += hbs * 8;
  807. uvoff += hbs * 4;
  808. res = ff_vp9_decode_block(avctx, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
  809. }
  810. } else {
  811. if ((res = decode_superblock_mem(avctx, row, col, lflvl, yoff, uvoff, bl + 1)) < 0)
  812. return res;
  813. if (col + hbs < s->cols) { // FIXME why not <=?
  814. if (row + hbs < s->rows) {
  815. if ((res = decode_superblock_mem(avctx, row, col + hbs, lflvl, yoff + 8 * hbs,
  816. uvoff + 4 * hbs, bl + 1)) < 0)
  817. return res;
  818. yoff += hbs * 8 * y_stride;
  819. uvoff += hbs * 4 * uv_stride;
  820. if ((res = decode_superblock_mem(avctx, row + hbs, col, lflvl, yoff,
  821. uvoff, bl + 1)) < 0)
  822. return res;
  823. res = decode_superblock_mem(avctx, row + hbs, col + hbs, lflvl,
  824. yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
  825. } else {
  826. yoff += hbs * 8;
  827. uvoff += hbs * 4;
  828. res = decode_superblock_mem(avctx, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
  829. }
  830. } else if (row + hbs < s->rows) {
  831. yoff += hbs * 8 * y_stride;
  832. uvoff += hbs * 4 * uv_stride;
  833. res = decode_superblock_mem(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
  834. }
  835. }
  836. return res;
  837. }
  838. static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl,
  839. int row, int col,
  840. ptrdiff_t yoff, ptrdiff_t uvoff)
  841. {
  842. VP9Context *s = avctx->priv_data;
  843. AVFrame *f = s->frames[CUR_FRAME].tf.f;
  844. uint8_t *dst = f->data[0] + yoff;
  845. ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1];
  846. uint8_t *lvl = lflvl->level;
  847. int y, x, p;
  848. /* FIXME: In how far can we interleave the v/h loopfilter calls? E.g.
  849. * if you think of them as acting on a 8x8 block max, we can interleave
  850. * each v/h within the single x loop, but that only works if we work on
  851. * 8 pixel blocks, and we won't always do that (we want at least 16px
  852. * to use SSE2 optimizations, perhaps 32 for AVX2). */
  853. // filter edges between columns, Y plane (e.g. block1 | block2)
  854. for (y = 0; y < 8; y += 2, dst += 16 * ls_y, lvl += 16) {
  855. uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[0][0][y];
  856. uint8_t *hmask2 = lflvl->mask[0][0][y + 1];
  857. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
  858. unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
  859. unsigned hm = hm1 | hm2 | hm13 | hm23;
  860. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8, l++) {
  861. if (hm1 & x) {
  862. int L = *l, H = L >> 4;
  863. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  864. if (col || x > 1) {
  865. if (hmask1[0] & x) {
  866. if (hmask2[0] & x) {
  867. av_assert2(l[8] == L);
  868. s->dsp.loop_filter_16[0](ptr, ls_y, E, I, H);
  869. } else {
  870. s->dsp.loop_filter_8[2][0](ptr, ls_y, E, I, H);
  871. }
  872. } else if (hm2 & x) {
  873. L = l[8];
  874. H |= (L >> 4) << 8;
  875. E |= s->filter.mblim_lut[L] << 8;
  876. I |= s->filter.lim_lut[L] << 8;
  877. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  878. [!!(hmask2[1] & x)]
  879. [0](ptr, ls_y, E, I, H);
  880. } else {
  881. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  882. [0](ptr, ls_y, E, I, H);
  883. }
  884. }
  885. } else if (hm2 & x) {
  886. int L = l[8], H = L >> 4;
  887. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  888. if (col || x > 1) {
  889. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  890. [0](ptr + 8 * ls_y, ls_y, E, I, H);
  891. }
  892. }
  893. if (hm13 & x) {
  894. int L = *l, H = L >> 4;
  895. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  896. if (hm23 & x) {
  897. L = l[8];
  898. H |= (L >> 4) << 8;
  899. E |= s->filter.mblim_lut[L] << 8;
  900. I |= s->filter.lim_lut[L] << 8;
  901. s->dsp.loop_filter_mix2[0][0][0](ptr + 4, ls_y, E, I, H);
  902. } else {
  903. s->dsp.loop_filter_8[0][0](ptr + 4, ls_y, E, I, H);
  904. }
  905. } else if (hm23 & x) {
  906. int L = l[8], H = L >> 4;
  907. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  908. s->dsp.loop_filter_8[0][0](ptr + 8 * ls_y + 4, ls_y, E, I, H);
  909. }
  910. }
  911. }
  912. // block1
  913. // filter edges between rows, Y plane (e.g. ------)
  914. // block2
  915. dst = f->data[0] + yoff;
  916. lvl = lflvl->level;
  917. for (y = 0; y < 8; y++, dst += 8 * ls_y, lvl += 8) {
  918. uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[0][1][y];
  919. unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
  920. for (x = 1; vm & ~(x - 1); x <<= 2, ptr += 16, l += 2) {
  921. if (row || y) {
  922. if (vm & x) {
  923. int L = *l, H = L >> 4;
  924. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  925. if (vmask[0] & x) {
  926. if (vmask[0] & (x << 1)) {
  927. av_assert2(l[1] == L);
  928. s->dsp.loop_filter_16[1](ptr, ls_y, E, I, H);
  929. } else {
  930. s->dsp.loop_filter_8[2][1](ptr, ls_y, E, I, H);
  931. }
  932. } else if (vm & (x << 1)) {
  933. L = l[1];
  934. H |= (L >> 4) << 8;
  935. E |= s->filter.mblim_lut[L] << 8;
  936. I |= s->filter.lim_lut[L] << 8;
  937. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  938. [!!(vmask[1] & (x << 1))]
  939. [1](ptr, ls_y, E, I, H);
  940. } else {
  941. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  942. [1](ptr, ls_y, E, I, H);
  943. }
  944. } else if (vm & (x << 1)) {
  945. int L = l[1], H = L >> 4;
  946. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  947. s->dsp.loop_filter_8[!!(vmask[1] & (x << 1))]
  948. [1](ptr + 8, ls_y, E, I, H);
  949. }
  950. }
  951. if (vm3 & x) {
  952. int L = *l, H = L >> 4;
  953. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  954. if (vm3 & (x << 1)) {
  955. L = l[1];
  956. H |= (L >> 4) << 8;
  957. E |= s->filter.mblim_lut[L] << 8;
  958. I |= s->filter.lim_lut[L] << 8;
  959. s->dsp.loop_filter_mix2[0][0][1](ptr + ls_y * 4, ls_y, E, I, H);
  960. } else {
  961. s->dsp.loop_filter_8[0][1](ptr + ls_y * 4, ls_y, E, I, H);
  962. }
  963. } else if (vm3 & (x << 1)) {
  964. int L = l[1], H = L >> 4;
  965. int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
  966. s->dsp.loop_filter_8[0][1](ptr + ls_y * 4 + 8, ls_y, E, I, H);
  967. }
  968. }
  969. }
  970. // same principle but for U/V planes
  971. for (p = 0; p < 2; p++) {
  972. lvl = lflvl->level;
  973. dst = f->data[1 + p] + uvoff;
  974. for (y = 0; y < 8; y += 4, dst += 16 * ls_uv, lvl += 32) {
  975. uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[1][0][y];
  976. uint8_t *hmask2 = lflvl->mask[1][0][y + 2];
  977. unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2];
  978. unsigned hm2 = hmask2[1] | hmask2[2], hm = hm1 | hm2;
  979. for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 4) {
  980. if (col || x > 1) {
  981. if (hm1 & x) {
  982. int L = *l, H = L >> 4;
  983. int E = s->filter.mblim_lut[L];
  984. int I = s->filter.lim_lut[L];
  985. if (hmask1[0] & x) {
  986. if (hmask2[0] & x) {
  987. av_assert2(l[16] == L);
  988. s->dsp.loop_filter_16[0](ptr, ls_uv, E, I, H);
  989. } else {
  990. s->dsp.loop_filter_8[2][0](ptr, ls_uv, E, I, H);
  991. }
  992. } else if (hm2 & x) {
  993. L = l[16];
  994. H |= (L >> 4) << 8;
  995. E |= s->filter.mblim_lut[L] << 8;
  996. I |= s->filter.lim_lut[L] << 8;
  997. s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
  998. [!!(hmask2[1] & x)]
  999. [0](ptr, ls_uv, E, I, H);
  1000. } else {
  1001. s->dsp.loop_filter_8[!!(hmask1[1] & x)]
  1002. [0](ptr, ls_uv, E, I, H);
  1003. }
  1004. } else if (hm2 & x) {
  1005. int L = l[16], H = L >> 4;
  1006. int E = s->filter.mblim_lut[L];
  1007. int I = s->filter.lim_lut[L];
  1008. s->dsp.loop_filter_8[!!(hmask2[1] & x)]
  1009. [0](ptr + 8 * ls_uv, ls_uv, E, I, H);
  1010. }
  1011. }
  1012. if (x & 0xAA)
  1013. l += 2;
  1014. }
  1015. }
  1016. lvl = lflvl->level;
  1017. dst = f->data[1 + p] + uvoff;
  1018. for (y = 0; y < 8; y++, dst += 4 * ls_uv) {
  1019. uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[1][1][y];
  1020. unsigned vm = vmask[0] | vmask[1] | vmask[2];
  1021. for (x = 1; vm & ~(x - 1); x <<= 4, ptr += 16, l += 4) {
  1022. if (row || y) {
  1023. if (vm & x) {
  1024. int L = *l, H = L >> 4;
  1025. int E = s->filter.mblim_lut[L];
  1026. int I = s->filter.lim_lut[L];
  1027. if (vmask[0] & x) {
  1028. if (vmask[0] & (x << 2)) {
  1029. av_assert2(l[2] == L);
  1030. s->dsp.loop_filter_16[1](ptr, ls_uv, E, I, H);
  1031. } else {
  1032. s->dsp.loop_filter_8[2][1](ptr, ls_uv, E, I, H);
  1033. }
  1034. } else if (vm & (x << 2)) {
  1035. L = l[2];
  1036. H |= (L >> 4) << 8;
  1037. E |= s->filter.mblim_lut[L] << 8;
  1038. I |= s->filter.lim_lut[L] << 8;
  1039. s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
  1040. [!!(vmask[1] & (x << 2))]
  1041. [1](ptr, ls_uv, E, I, H);
  1042. } else {
  1043. s->dsp.loop_filter_8[!!(vmask[1] & x)]
  1044. [1](ptr, ls_uv, E, I, H);
  1045. }
  1046. } else if (vm & (x << 2)) {
  1047. int L = l[2], H = L >> 4;
  1048. int E = s->filter.mblim_lut[L];
  1049. int I = s->filter.lim_lut[L];
  1050. s->dsp.loop_filter_8[!!(vmask[1] & (x << 2))]
  1051. [1](ptr + 8, ls_uv, E, I, H);
  1052. }
  1053. }
  1054. }
  1055. if (y & 1)
  1056. lvl += 16;
  1057. }
  1058. }
  1059. }
  1060. static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
  1061. {
  1062. int sb_start = (idx * n) >> log2_n;
  1063. int sb_end = ((idx + 1) * n) >> log2_n;
  1064. *start = FFMIN(sb_start, n) << 3;
  1065. *end = FFMIN(sb_end, n) << 3;
  1066. }
  1067. static int update_refs(AVCodecContext *avctx)
  1068. {
  1069. VP9Context *s = avctx->priv_data;
  1070. int i, ret;
  1071. for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++)
  1072. if (s->refreshrefmask & (1 << i)) {
  1073. ff_thread_release_buffer(avctx, &s->refs[i]);
  1074. ret = ff_thread_ref_frame(&s->refs[i], &s->frames[CUR_FRAME].tf);
  1075. if (ret < 0)
  1076. return ret;
  1077. }
  1078. return 0;
  1079. }
  1080. static int vp9_decode_frame(AVCodecContext *avctx, void *output,
  1081. int *got_frame, AVPacket *pkt)
  1082. {
  1083. VP9Context *s = avctx->priv_data;
  1084. AVFrame *frame = output;
  1085. const uint8_t *data = pkt->data;
  1086. int size = pkt->size;
  1087. AVFrame *f;
  1088. int ret, tile_row, tile_col, i, ref = -1, row, col;
  1089. s->setup_finished = 0;
  1090. ret = decode_frame_header(avctx, data, size, &ref);
  1091. if (ret < 0) {
  1092. return ret;
  1093. } else if (!ret) {
  1094. if (!s->refs[ref].f->buf[0]) {
  1095. av_log(avctx, AV_LOG_ERROR,
  1096. "Requested reference %d not available\n", ref);
  1097. return AVERROR_INVALIDDATA;
  1098. }
  1099. ret = av_frame_ref(frame, s->refs[ref].f);
  1100. if (ret < 0)
  1101. return ret;
  1102. *got_frame = 1;
  1103. return pkt->size;
  1104. }
  1105. data += ret;
  1106. size -= ret;
  1107. vp9_frame_unref(avctx, &s->frames[LAST_FRAME]);
  1108. if (!s->keyframe && s->frames[CUR_FRAME].tf.f->buf[0]) {
  1109. ret = vp9_frame_ref(&s->frames[LAST_FRAME], &s->frames[CUR_FRAME]);
  1110. if (ret < 0)
  1111. return ret;
  1112. }
  1113. vp9_frame_unref(avctx, &s->frames[CUR_FRAME]);
  1114. ret = vp9_frame_alloc(avctx, &s->frames[CUR_FRAME]);
  1115. if (ret < 0)
  1116. return ret;
  1117. f = s->frames[CUR_FRAME].tf.f;
  1118. f->key_frame = s->keyframe;
  1119. f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1120. if (s->fullrange)
  1121. avctx->color_range = AVCOL_RANGE_JPEG;
  1122. else
  1123. avctx->color_range = AVCOL_RANGE_MPEG;
  1124. switch (s->colorspace) {
  1125. case 1: avctx->colorspace = AVCOL_SPC_BT470BG; break;
  1126. case 2: avctx->colorspace = AVCOL_SPC_BT709; break;
  1127. case 3: avctx->colorspace = AVCOL_SPC_SMPTE170M; break;
  1128. case 4: avctx->colorspace = AVCOL_SPC_SMPTE240M; break;
  1129. }
  1130. s->pass = s->uses_2pass =
  1131. avctx->active_thread_type & FF_THREAD_FRAME && s->refreshctx && !s->parallelmode;
  1132. if (s->refreshctx && s->parallelmode) {
  1133. int j, k, l, m;
  1134. for (i = 0; i < 4; i++) {
  1135. for (j = 0; j < 2; j++)
  1136. for (k = 0; k < 2; k++)
  1137. for (l = 0; l < 6; l++)
  1138. for (m = 0; m < 6; m++)
  1139. memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m],
  1140. s->prob.coef[i][j][k][l][m], 3);
  1141. if (s->txfmmode == i)
  1142. break;
  1143. }
  1144. s->prob_ctx[s->framectxid].p = s->prob.p;
  1145. }
  1146. if ((s->parallelmode || !s->refreshctx) &&
  1147. avctx->active_thread_type & FF_THREAD_FRAME) {
  1148. ff_thread_finish_setup(avctx);
  1149. s->setup_finished = 1;
  1150. }
  1151. // main tile decode loop
  1152. memset(s->above_partition_ctx, 0, s->cols);
  1153. memset(s->above_skip_ctx, 0, s->cols);
  1154. if (s->keyframe || s->intraonly)
  1155. memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
  1156. else
  1157. memset(s->above_mode_ctx, NEARESTMV, s->cols);
  1158. memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
  1159. memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 8);
  1160. memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 8);
  1161. memset(s->above_segpred_ctx, 0, s->cols);
  1162. do {
  1163. ptrdiff_t yoff = 0, uvoff = 0;
  1164. s->b = s->b_base;
  1165. s->block = s->block_base;
  1166. s->uvblock[0] = s->uvblock_base[0];
  1167. s->uvblock[1] = s->uvblock_base[1];
  1168. s->eob = s->eob_base;
  1169. s->uveob[0] = s->uveob_base[0];
  1170. s->uveob[1] = s->uveob_base[1];
  1171. for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) {
  1172. set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end,
  1173. tile_row, s->tiling.log2_tile_rows, s->sb_rows);
  1174. if (s->pass != 2) {
  1175. for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
  1176. int64_t tile_size;
  1177. if (tile_col == s->tiling.tile_cols - 1 &&
  1178. tile_row == s->tiling.tile_rows - 1) {
  1179. tile_size = size;
  1180. } else {
  1181. tile_size = AV_RB32(data);
  1182. data += 4;
  1183. size -= 4;
  1184. }
  1185. if (tile_size > size) {
  1186. ret = AVERROR_INVALIDDATA;
  1187. goto fail;
  1188. }
  1189. ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
  1190. if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit
  1191. ret = AVERROR_INVALIDDATA;
  1192. goto fail;
  1193. }
  1194. data += tile_size;
  1195. size -= tile_size;
  1196. }
  1197. }
  1198. for (row = s->tiling.tile_row_start;
  1199. row < s->tiling.tile_row_end;
  1200. row += 8, yoff += f->linesize[0] * 64,
  1201. uvoff += f->linesize[1] * 32) {
  1202. VP9Filter *lflvl = s->lflvl;
  1203. ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
  1204. for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
  1205. set_tile_offset(&s->tiling.tile_col_start,
  1206. &s->tiling.tile_col_end,
  1207. tile_col, s->tiling.log2_tile_cols, s->sb_cols);
  1208. memset(s->left_partition_ctx, 0, 8);
  1209. memset(s->left_skip_ctx, 0, 8);
  1210. if (s->keyframe || s->intraonly)
  1211. memset(s->left_mode_ctx, DC_PRED, 16);
  1212. else
  1213. memset(s->left_mode_ctx, NEARESTMV, 8);
  1214. memset(s->left_y_nnz_ctx, 0, 16);
  1215. memset(s->left_uv_nnz_ctx, 0, 16);
  1216. memset(s->left_segpred_ctx, 0, 8);
  1217. memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
  1218. for (col = s->tiling.tile_col_start;
  1219. col < s->tiling.tile_col_end;
  1220. col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) {
  1221. // FIXME integrate with lf code (i.e. zero after each
  1222. // use, similar to invtxfm coefficients, or similar)
  1223. if (s->pass != 1)
  1224. memset(lflvl->mask, 0, sizeof(lflvl->mask));
  1225. if (s->pass == 2) {
  1226. ret = decode_superblock_mem(avctx, row, col, lflvl,
  1227. yoff2, uvoff2, BL_64X64);
  1228. } else {
  1229. ret = decode_subblock(avctx, row, col, lflvl,
  1230. yoff2, uvoff2, BL_64X64);
  1231. }
  1232. if (ret < 0)
  1233. goto fail;
  1234. }
  1235. if (s->pass != 2)
  1236. memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
  1237. }
  1238. if (s->pass == 1)
  1239. continue;
  1240. // backup pre-loopfilter reconstruction data for intra
  1241. // prediction of next row of sb64s
  1242. if (row + 8 < s->rows) {
  1243. memcpy(s->intra_pred_data[0],
  1244. f->data[0] + yoff +
  1245. 63 * f->linesize[0],
  1246. 8 * s->cols);
  1247. memcpy(s->intra_pred_data[1],
  1248. f->data[1] + uvoff +
  1249. 31 * f->linesize[1],
  1250. 4 * s->cols);
  1251. memcpy(s->intra_pred_data[2],
  1252. f->data[2] + uvoff +
  1253. 31 * f->linesize[2],
  1254. 4 * s->cols);
  1255. }
  1256. // loopfilter one row
  1257. if (s->filter.level) {
  1258. yoff2 = yoff;
  1259. uvoff2 = uvoff;
  1260. lflvl = s->lflvl;
  1261. for (col = 0; col < s->cols;
  1262. col += 8, yoff2 += 64, uvoff2 += 32, lflvl++)
  1263. loopfilter_subblock(avctx, lflvl, row, col, yoff2, uvoff2);
  1264. }
  1265. // FIXME maybe we can make this more finegrained by running the
  1266. // loopfilter per-block instead of after each sbrow
  1267. // In fact that would also make intra pred left preparation easier?
  1268. ff_thread_report_progress(&s->frames[CUR_FRAME].tf, row >> 3, 0);
  1269. }
  1270. }
  1271. if (s->pass < 2 && s->refreshctx && !s->parallelmode) {
  1272. ff_vp9_adapt_probs(s);
  1273. if (avctx->active_thread_type & FF_THREAD_FRAME) {
  1274. ff_thread_finish_setup(avctx);
  1275. s->setup_finished = 1;
  1276. }
  1277. }
  1278. } while (s->pass++ == 1);
  1279. fail:
  1280. ff_thread_report_progress(&s->frames[CUR_FRAME].tf, INT_MAX, 0);
  1281. if (ret < 0)
  1282. return ret;
  1283. // ref frame setup
  1284. if (!s->setup_finished) {
  1285. ret = update_refs(avctx);
  1286. if (ret < 0)
  1287. return ret;
  1288. }
  1289. if (!s->invisible) {
  1290. av_frame_unref(frame);
  1291. ret = av_frame_ref(frame, s->frames[CUR_FRAME].tf.f);
  1292. if (ret < 0)
  1293. return ret;
  1294. *got_frame = 1;
  1295. }
  1296. return pkt->size;
  1297. }
  1298. static av_cold int vp9_decode_free(AVCodecContext *avctx)
  1299. {
  1300. VP9Context *s = avctx->priv_data;
  1301. int i;
  1302. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
  1303. vp9_frame_unref(avctx, &s->frames[i]);
  1304. av_frame_free(&s->frames[i].tf.f);
  1305. }
  1306. for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) {
  1307. ff_thread_release_buffer(avctx, &s->refs[i]);
  1308. av_frame_free(&s->refs[i].f);
  1309. }
  1310. av_freep(&s->c_b);
  1311. av_freep(&s->above_partition_ctx);
  1312. av_freep(&s->b_base);
  1313. av_freep(&s->block_base);
  1314. return 0;
  1315. }
  1316. static av_cold int vp9_decode_init(AVCodecContext *avctx)
  1317. {
  1318. VP9Context *s = avctx->priv_data;
  1319. int i;
  1320. memset(s, 0, sizeof(*s));
  1321. avctx->internal->allocate_progress = 1;
  1322. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  1323. ff_vp9dsp_init(&s->dsp);
  1324. ff_videodsp_init(&s->vdsp, 8);
  1325. s->frames[0].tf.f = av_frame_alloc();
  1326. s->frames[1].tf.f = av_frame_alloc();
  1327. if (!s->frames[0].tf.f || !s->frames[1].tf.f)
  1328. goto fail;
  1329. for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) {
  1330. s->refs[i].f = av_frame_alloc();
  1331. if (!s->refs[i].f)
  1332. goto fail;
  1333. }
  1334. s->filter.sharpness = -1;
  1335. return 0;
  1336. fail:
  1337. vp9_decode_free(avctx);
  1338. return AVERROR(ENOMEM);
  1339. }
  1340. static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1341. {
  1342. VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
  1343. int i, ret;
  1344. ret = update_size(dst, ssrc->alloc_width, ssrc->alloc_height);
  1345. if (ret < 0)
  1346. return ret;
  1347. for (i = 0; i < 2; i++) {
  1348. if (s->frames[i].tf.f->data[0])
  1349. vp9_frame_unref(dst, &s->frames[i]);
  1350. if (ssrc->frames[i].tf.f->data[0]) {
  1351. if ((ret = vp9_frame_ref(&s->frames[i], &ssrc->frames[i])) < 0)
  1352. return ret;
  1353. }
  1354. }
  1355. for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) {
  1356. ff_thread_release_buffer(dst, &s->refs[i]);
  1357. if (ssrc->refs[i].f->buf[0]) {
  1358. ret = ff_thread_ref_frame(&s->refs[i], &ssrc->refs[i]);
  1359. if (ret < 0)
  1360. return ret;
  1361. }
  1362. }
  1363. s->refreshrefmask = ssrc->refreshrefmask;
  1364. ret = update_refs(dst);
  1365. if (ret < 0)
  1366. return ret;
  1367. s->invisible = ssrc->invisible;
  1368. s->keyframe = ssrc->keyframe;
  1369. s->last_uses_2pass = ssrc->uses_2pass;
  1370. memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
  1371. memcpy(&s->lf_delta, &ssrc->lf_delta, sizeof(s->lf_delta));
  1372. memcpy(&s->segmentation.feat, &ssrc->segmentation.feat,
  1373. sizeof(s->segmentation.feat));
  1374. return 0;
  1375. }
  1376. AVCodec ff_vp9_decoder = {
  1377. .name = "vp9",
  1378. .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
  1379. .type = AVMEDIA_TYPE_VIDEO,
  1380. .id = AV_CODEC_ID_VP9,
  1381. .priv_data_size = sizeof(VP9Context),
  1382. .init = vp9_decode_init,
  1383. .decode = vp9_decode_frame,
  1384. .flush = vp9_decode_flush,
  1385. .close = vp9_decode_free,
  1386. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1387. .init_thread_copy = vp9_decode_init,
  1388. .update_thread_context = vp9_decode_update_thread_context,
  1389. .bsfs = "vp9_superframe_split",
  1390. };