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.

1292 lines
51KB

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