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.

1314 lines
52KB

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