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.

1450 lines
58KB

  1. /*
  2. * VP9 compatible video decoder
  3. *
  4. * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. * Copyright (C) 2013 Clément Bœsch <u pkh me>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/avassert.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. #include "videodsp.h"
  27. #include "vp56.h"
  28. #include "vp9.h"
  29. #include "vp9data.h"
  30. #include "vp9dec.h"
  31. static av_always_inline void setctx_2d(uint8_t *ptr, int w, int h,
  32. ptrdiff_t stride, int v)
  33. {
  34. switch (w) {
  35. case 1:
  36. do {
  37. *ptr = v;
  38. ptr += stride;
  39. } while (--h);
  40. break;
  41. case 2: {
  42. int v16 = v * 0x0101;
  43. do {
  44. AV_WN16A(ptr, v16);
  45. ptr += stride;
  46. } while (--h);
  47. break;
  48. }
  49. case 4: {
  50. uint32_t v32 = v * 0x01010101;
  51. do {
  52. AV_WN32A(ptr, v32);
  53. ptr += stride;
  54. } while (--h);
  55. break;
  56. }
  57. case 8: {
  58. #if HAVE_FAST_64BIT
  59. uint64_t v64 = v * 0x0101010101010101ULL;
  60. do {
  61. AV_WN64A(ptr, v64);
  62. ptr += stride;
  63. } while (--h);
  64. #else
  65. uint32_t v32 = v * 0x01010101;
  66. do {
  67. AV_WN32A(ptr, v32);
  68. AV_WN32A(ptr + 4, v32);
  69. ptr += stride;
  70. } while (--h);
  71. #endif
  72. break;
  73. }
  74. }
  75. }
  76. static void decode_mode(VP9TileData *td)
  77. {
  78. static const uint8_t left_ctx[N_BS_SIZES] = {
  79. 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
  80. };
  81. static const uint8_t above_ctx[N_BS_SIZES] = {
  82. 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
  83. };
  84. static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
  85. TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
  86. TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
  87. };
  88. VP9Context *s = td->s;
  89. VP9Block *b = td->b;
  90. int row = td->row, col = td->col, row7 = td->row7;
  91. enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
  92. int bw4 = ff_vp9_bwh_tab[1][b->bs][0], w4 = FFMIN(s->cols - col, bw4);
  93. int bh4 = ff_vp9_bwh_tab[1][b->bs][1], h4 = FFMIN(s->rows - row, bh4), y;
  94. int have_a = row > 0, have_l = col > td->tile_col_start;
  95. int vref, filter_id;
  96. if (!s->s.h.segmentation.enabled) {
  97. b->seg_id = 0;
  98. } else if (s->s.h.keyframe || s->s.h.intraonly) {
  99. b->seg_id = !s->s.h.segmentation.update_map ? 0 :
  100. vp8_rac_get_tree(td->c, ff_vp9_segmentation_tree, s->s.h.segmentation.prob);
  101. } else if (!s->s.h.segmentation.update_map ||
  102. (s->s.h.segmentation.temporal &&
  103. vp56_rac_get_prob_branchy(td->c,
  104. s->s.h.segmentation.pred_prob[s->above_segpred_ctx[col] +
  105. td->left_segpred_ctx[row7]]))) {
  106. if (!s->s.h.errorres && s->s.frames[REF_FRAME_SEGMAP].segmentation_map) {
  107. int pred = 8, x;
  108. uint8_t *refsegmap = s->s.frames[REF_FRAME_SEGMAP].segmentation_map;
  109. if (!s->s.frames[REF_FRAME_SEGMAP].uses_2pass)
  110. ff_thread_await_progress(&s->s.frames[REF_FRAME_SEGMAP].tf, row >> 3, 0);
  111. for (y = 0; y < h4; y++) {
  112. int idx_base = (y + row) * 8 * s->sb_cols + col;
  113. for (x = 0; x < w4; x++)
  114. pred = FFMIN(pred, refsegmap[idx_base + x]);
  115. }
  116. av_assert1(pred < 8);
  117. b->seg_id = pred;
  118. } else {
  119. b->seg_id = 0;
  120. }
  121. memset(&s->above_segpred_ctx[col], 1, w4);
  122. memset(&td->left_segpred_ctx[row7], 1, h4);
  123. } else {
  124. b->seg_id = vp8_rac_get_tree(td->c, ff_vp9_segmentation_tree,
  125. s->s.h.segmentation.prob);
  126. memset(&s->above_segpred_ctx[col], 0, w4);
  127. memset(&td->left_segpred_ctx[row7], 0, h4);
  128. }
  129. if (s->s.h.segmentation.enabled &&
  130. (s->s.h.segmentation.update_map || s->s.h.keyframe || s->s.h.intraonly)) {
  131. setctx_2d(&s->s.frames[CUR_FRAME].segmentation_map[row * 8 * s->sb_cols + col],
  132. bw4, bh4, 8 * s->sb_cols, b->seg_id);
  133. }
  134. b->skip = s->s.h.segmentation.enabled &&
  135. s->s.h.segmentation.feat[b->seg_id].skip_enabled;
  136. if (!b->skip) {
  137. int c = td->left_skip_ctx[row7] + s->above_skip_ctx[col];
  138. b->skip = vp56_rac_get_prob(td->c, s->prob.p.skip[c]);
  139. td->counts.skip[c][b->skip]++;
  140. }
  141. if (s->s.h.keyframe || s->s.h.intraonly) {
  142. b->intra = 1;
  143. } else if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[b->seg_id].ref_enabled) {
  144. b->intra = !s->s.h.segmentation.feat[b->seg_id].ref_val;
  145. } else {
  146. int c, bit;
  147. if (have_a && have_l) {
  148. c = s->above_intra_ctx[col] + td->left_intra_ctx[row7];
  149. c += (c == 2);
  150. } else {
  151. c = have_a ? 2 * s->above_intra_ctx[col] :
  152. have_l ? 2 * td->left_intra_ctx[row7] : 0;
  153. }
  154. bit = vp56_rac_get_prob(td->c, s->prob.p.intra[c]);
  155. td->counts.intra[c][bit]++;
  156. b->intra = !bit;
  157. }
  158. if ((b->intra || !b->skip) && s->s.h.txfmmode == TX_SWITCHABLE) {
  159. int c;
  160. if (have_a) {
  161. if (have_l) {
  162. c = (s->above_skip_ctx[col] ? max_tx :
  163. s->above_txfm_ctx[col]) +
  164. (td->left_skip_ctx[row7] ? max_tx :
  165. td->left_txfm_ctx[row7]) > max_tx;
  166. } else {
  167. c = s->above_skip_ctx[col] ? 1 :
  168. (s->above_txfm_ctx[col] * 2 > max_tx);
  169. }
  170. } else if (have_l) {
  171. c = td->left_skip_ctx[row7] ? 1 :
  172. (td->left_txfm_ctx[row7] * 2 > max_tx);
  173. } else {
  174. c = 1;
  175. }
  176. switch (max_tx) {
  177. case TX_32X32:
  178. b->tx = vp56_rac_get_prob(td->c, s->prob.p.tx32p[c][0]);
  179. if (b->tx) {
  180. b->tx += vp56_rac_get_prob(td->c, s->prob.p.tx32p[c][1]);
  181. if (b->tx == 2)
  182. b->tx += vp56_rac_get_prob(td->c, s->prob.p.tx32p[c][2]);
  183. }
  184. td->counts.tx32p[c][b->tx]++;
  185. break;
  186. case TX_16X16:
  187. b->tx = vp56_rac_get_prob(td->c, s->prob.p.tx16p[c][0]);
  188. if (b->tx)
  189. b->tx += vp56_rac_get_prob(td->c, s->prob.p.tx16p[c][1]);
  190. td->counts.tx16p[c][b->tx]++;
  191. break;
  192. case TX_8X8:
  193. b->tx = vp56_rac_get_prob(td->c, s->prob.p.tx8p[c]);
  194. td->counts.tx8p[c][b->tx]++;
  195. break;
  196. case TX_4X4:
  197. b->tx = TX_4X4;
  198. break;
  199. }
  200. } else {
  201. b->tx = FFMIN(max_tx, s->s.h.txfmmode);
  202. }
  203. if (s->s.h.keyframe || s->s.h.intraonly) {
  204. uint8_t *a = &s->above_mode_ctx[col * 2];
  205. uint8_t *l = &td->left_mode_ctx[(row7) << 1];
  206. b->comp = 0;
  207. if (b->bs > BS_8x8) {
  208. // FIXME the memory storage intermediates here aren't really
  209. // necessary, they're just there to make the code slightly
  210. // simpler for now
  211. b->mode[0] =
  212. a[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  213. ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
  214. if (b->bs != BS_8x4) {
  215. b->mode[1] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  216. ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
  217. l[0] =
  218. a[1] = b->mode[1];
  219. } else {
  220. l[0] =
  221. a[1] =
  222. b->mode[1] = b->mode[0];
  223. }
  224. if (b->bs != BS_4x8) {
  225. b->mode[2] =
  226. a[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  227. ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
  228. if (b->bs != BS_8x4) {
  229. b->mode[3] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  230. ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
  231. l[1] =
  232. a[1] = b->mode[3];
  233. } else {
  234. l[1] =
  235. a[1] =
  236. b->mode[3] = b->mode[2];
  237. }
  238. } else {
  239. b->mode[2] = b->mode[0];
  240. l[1] =
  241. a[1] =
  242. b->mode[3] = b->mode[1];
  243. }
  244. } else {
  245. b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  246. ff_vp9_default_kf_ymode_probs[*a][*l]);
  247. b->mode[3] =
  248. b->mode[2] =
  249. b->mode[1] = b->mode[0];
  250. // FIXME this can probably be optimized
  251. memset(a, b->mode[0], ff_vp9_bwh_tab[0][b->bs][0]);
  252. memset(l, b->mode[0], ff_vp9_bwh_tab[0][b->bs][1]);
  253. }
  254. b->uvmode = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  255. ff_vp9_default_kf_uvmode_probs[b->mode[3]]);
  256. } else if (b->intra) {
  257. b->comp = 0;
  258. if (b->bs > BS_8x8) {
  259. b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  260. s->prob.p.y_mode[0]);
  261. td->counts.y_mode[0][b->mode[0]]++;
  262. if (b->bs != BS_8x4) {
  263. b->mode[1] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  264. s->prob.p.y_mode[0]);
  265. td->counts.y_mode[0][b->mode[1]]++;
  266. } else {
  267. b->mode[1] = b->mode[0];
  268. }
  269. if (b->bs != BS_4x8) {
  270. b->mode[2] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  271. s->prob.p.y_mode[0]);
  272. td->counts.y_mode[0][b->mode[2]]++;
  273. if (b->bs != BS_8x4) {
  274. b->mode[3] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  275. s->prob.p.y_mode[0]);
  276. td->counts.y_mode[0][b->mode[3]]++;
  277. } else {
  278. b->mode[3] = b->mode[2];
  279. }
  280. } else {
  281. b->mode[2] = b->mode[0];
  282. b->mode[3] = b->mode[1];
  283. }
  284. } else {
  285. static const uint8_t size_group[10] = {
  286. 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
  287. };
  288. int sz = size_group[b->bs];
  289. b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  290. s->prob.p.y_mode[sz]);
  291. b->mode[1] =
  292. b->mode[2] =
  293. b->mode[3] = b->mode[0];
  294. td->counts.y_mode[sz][b->mode[3]]++;
  295. }
  296. b->uvmode = vp8_rac_get_tree(td->c, ff_vp9_intramode_tree,
  297. s->prob.p.uv_mode[b->mode[3]]);
  298. td->counts.uv_mode[b->mode[3]][b->uvmode]++;
  299. } else {
  300. static const uint8_t inter_mode_ctx_lut[14][14] = {
  301. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  302. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  303. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  304. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  305. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  306. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  307. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  308. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  309. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  310. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  311. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  312. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  313. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
  314. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
  315. };
  316. if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[b->seg_id].ref_enabled) {
  317. av_assert2(s->s.h.segmentation.feat[b->seg_id].ref_val != 0);
  318. b->comp = 0;
  319. b->ref[0] = s->s.h.segmentation.feat[b->seg_id].ref_val - 1;
  320. } else {
  321. // read comp_pred flag
  322. if (s->s.h.comppredmode != PRED_SWITCHABLE) {
  323. b->comp = s->s.h.comppredmode == PRED_COMPREF;
  324. } else {
  325. int c;
  326. // FIXME add intra as ref=0xff (or -1) to make these easier?
  327. if (have_a) {
  328. if (have_l) {
  329. if (s->above_comp_ctx[col] && td->left_comp_ctx[row7]) {
  330. c = 4;
  331. } else if (s->above_comp_ctx[col]) {
  332. c = 2 + (td->left_intra_ctx[row7] ||
  333. td->left_ref_ctx[row7] == s->s.h.fixcompref);
  334. } else if (td->left_comp_ctx[row7]) {
  335. c = 2 + (s->above_intra_ctx[col] ||
  336. s->above_ref_ctx[col] == s->s.h.fixcompref);
  337. } else {
  338. c = (!s->above_intra_ctx[col] &&
  339. s->above_ref_ctx[col] == s->s.h.fixcompref) ^
  340. (!td->left_intra_ctx[row7] &&
  341. td->left_ref_ctx[row & 7] == s->s.h.fixcompref);
  342. }
  343. } else {
  344. c = s->above_comp_ctx[col] ? 3 :
  345. (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->s.h.fixcompref);
  346. }
  347. } else if (have_l) {
  348. c = td->left_comp_ctx[row7] ? 3 :
  349. (!td->left_intra_ctx[row7] && td->left_ref_ctx[row7] == s->s.h.fixcompref);
  350. } else {
  351. c = 1;
  352. }
  353. b->comp = vp56_rac_get_prob(td->c, s->prob.p.comp[c]);
  354. td->counts.comp[c][b->comp]++;
  355. }
  356. // read actual references
  357. // FIXME probably cache a few variables here to prevent repetitive
  358. // memory accesses below
  359. if (b->comp) { /* two references */
  360. int fix_idx = s->s.h.signbias[s->s.h.fixcompref], var_idx = !fix_idx, c, bit;
  361. b->ref[fix_idx] = s->s.h.fixcompref;
  362. // FIXME can this codeblob be replaced by some sort of LUT?
  363. if (have_a) {
  364. if (have_l) {
  365. if (s->above_intra_ctx[col]) {
  366. if (td->left_intra_ctx[row7]) {
  367. c = 2;
  368. } else {
  369. c = 1 + 2 * (td->left_ref_ctx[row7] != s->s.h.varcompref[1]);
  370. }
  371. } else if (td->left_intra_ctx[row7]) {
  372. c = 1 + 2 * (s->above_ref_ctx[col] != s->s.h.varcompref[1]);
  373. } else {
  374. int refl = td->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
  375. if (refl == refa && refa == s->s.h.varcompref[1]) {
  376. c = 0;
  377. } else if (!td->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
  378. if ((refa == s->s.h.fixcompref && refl == s->s.h.varcompref[0]) ||
  379. (refl == s->s.h.fixcompref && refa == s->s.h.varcompref[0])) {
  380. c = 4;
  381. } else {
  382. c = (refa == refl) ? 3 : 1;
  383. }
  384. } else if (!td->left_comp_ctx[row7]) {
  385. if (refa == s->s.h.varcompref[1] && refl != s->s.h.varcompref[1]) {
  386. c = 1;
  387. } else {
  388. c = (refl == s->s.h.varcompref[1] &&
  389. refa != s->s.h.varcompref[1]) ? 2 : 4;
  390. }
  391. } else if (!s->above_comp_ctx[col]) {
  392. if (refl == s->s.h.varcompref[1] && refa != s->s.h.varcompref[1]) {
  393. c = 1;
  394. } else {
  395. c = (refa == s->s.h.varcompref[1] &&
  396. refl != s->s.h.varcompref[1]) ? 2 : 4;
  397. }
  398. } else {
  399. c = (refl == refa) ? 4 : 2;
  400. }
  401. }
  402. } else {
  403. if (s->above_intra_ctx[col]) {
  404. c = 2;
  405. } else if (s->above_comp_ctx[col]) {
  406. c = 4 * (s->above_ref_ctx[col] != s->s.h.varcompref[1]);
  407. } else {
  408. c = 3 * (s->above_ref_ctx[col] != s->s.h.varcompref[1]);
  409. }
  410. }
  411. } else if (have_l) {
  412. if (td->left_intra_ctx[row7]) {
  413. c = 2;
  414. } else if (td->left_comp_ctx[row7]) {
  415. c = 4 * (td->left_ref_ctx[row7] != s->s.h.varcompref[1]);
  416. } else {
  417. c = 3 * (td->left_ref_ctx[row7] != s->s.h.varcompref[1]);
  418. }
  419. } else {
  420. c = 2;
  421. }
  422. bit = vp56_rac_get_prob(td->c, s->prob.p.comp_ref[c]);
  423. b->ref[var_idx] = s->s.h.varcompref[bit];
  424. td->counts.comp_ref[c][bit]++;
  425. } else /* single reference */ {
  426. int bit, c;
  427. if (have_a && !s->above_intra_ctx[col]) {
  428. if (have_l && !td->left_intra_ctx[row7]) {
  429. if (td->left_comp_ctx[row7]) {
  430. if (s->above_comp_ctx[col]) {
  431. c = 1 + (!s->s.h.fixcompref || !td->left_ref_ctx[row7] ||
  432. !s->above_ref_ctx[col]);
  433. } else {
  434. c = (3 * !s->above_ref_ctx[col]) +
  435. (!s->s.h.fixcompref || !td->left_ref_ctx[row7]);
  436. }
  437. } else if (s->above_comp_ctx[col]) {
  438. c = (3 * !td->left_ref_ctx[row7]) +
  439. (!s->s.h.fixcompref || !s->above_ref_ctx[col]);
  440. } else {
  441. c = 2 * !td->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
  442. }
  443. } else if (s->above_intra_ctx[col]) {
  444. c = 2;
  445. } else if (s->above_comp_ctx[col]) {
  446. c = 1 + (!s->s.h.fixcompref || !s->above_ref_ctx[col]);
  447. } else {
  448. c = 4 * (!s->above_ref_ctx[col]);
  449. }
  450. } else if (have_l && !td->left_intra_ctx[row7]) {
  451. if (td->left_intra_ctx[row7]) {
  452. c = 2;
  453. } else if (td->left_comp_ctx[row7]) {
  454. c = 1 + (!s->s.h.fixcompref || !td->left_ref_ctx[row7]);
  455. } else {
  456. c = 4 * (!td->left_ref_ctx[row7]);
  457. }
  458. } else {
  459. c = 2;
  460. }
  461. bit = vp56_rac_get_prob(td->c, s->prob.p.single_ref[c][0]);
  462. td->counts.single_ref[c][0][bit]++;
  463. if (!bit) {
  464. b->ref[0] = 0;
  465. } else {
  466. // FIXME can this codeblob be replaced by some sort of LUT?
  467. if (have_a) {
  468. if (have_l) {
  469. if (td->left_intra_ctx[row7]) {
  470. if (s->above_intra_ctx[col]) {
  471. c = 2;
  472. } else if (s->above_comp_ctx[col]) {
  473. c = 1 + 2 * (s->s.h.fixcompref == 1 ||
  474. s->above_ref_ctx[col] == 1);
  475. } else if (!s->above_ref_ctx[col]) {
  476. c = 3;
  477. } else {
  478. c = 4 * (s->above_ref_ctx[col] == 1);
  479. }
  480. } else if (s->above_intra_ctx[col]) {
  481. if (td->left_intra_ctx[row7]) {
  482. c = 2;
  483. } else if (td->left_comp_ctx[row7]) {
  484. c = 1 + 2 * (s->s.h.fixcompref == 1 ||
  485. td->left_ref_ctx[row7] == 1);
  486. } else if (!td->left_ref_ctx[row7]) {
  487. c = 3;
  488. } else {
  489. c = 4 * (td->left_ref_ctx[row7] == 1);
  490. }
  491. } else if (s->above_comp_ctx[col]) {
  492. if (td->left_comp_ctx[row7]) {
  493. if (td->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
  494. c = 3 * (s->s.h.fixcompref == 1 ||
  495. td->left_ref_ctx[row7] == 1);
  496. } else {
  497. c = 2;
  498. }
  499. } else if (!td->left_ref_ctx[row7]) {
  500. c = 1 + 2 * (s->s.h.fixcompref == 1 ||
  501. s->above_ref_ctx[col] == 1);
  502. } else {
  503. c = 3 * (td->left_ref_ctx[row7] == 1) +
  504. (s->s.h.fixcompref == 1 || s->above_ref_ctx[col] == 1);
  505. }
  506. } else if (td->left_comp_ctx[row7]) {
  507. if (!s->above_ref_ctx[col]) {
  508. c = 1 + 2 * (s->s.h.fixcompref == 1 ||
  509. td->left_ref_ctx[row7] == 1);
  510. } else {
  511. c = 3 * (s->above_ref_ctx[col] == 1) +
  512. (s->s.h.fixcompref == 1 || td->left_ref_ctx[row7] == 1);
  513. }
  514. } else if (!s->above_ref_ctx[col]) {
  515. if (!td->left_ref_ctx[row7]) {
  516. c = 3;
  517. } else {
  518. c = 4 * (td->left_ref_ctx[row7] == 1);
  519. }
  520. } else if (!td->left_ref_ctx[row7]) {
  521. c = 4 * (s->above_ref_ctx[col] == 1);
  522. } else {
  523. c = 2 * (td->left_ref_ctx[row7] == 1) +
  524. 2 * (s->above_ref_ctx[col] == 1);
  525. }
  526. } else {
  527. if (s->above_intra_ctx[col] ||
  528. (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
  529. c = 2;
  530. } else if (s->above_comp_ctx[col]) {
  531. c = 3 * (s->s.h.fixcompref == 1 || s->above_ref_ctx[col] == 1);
  532. } else {
  533. c = 4 * (s->above_ref_ctx[col] == 1);
  534. }
  535. }
  536. } else if (have_l) {
  537. if (td->left_intra_ctx[row7] ||
  538. (!td->left_comp_ctx[row7] && !td->left_ref_ctx[row7])) {
  539. c = 2;
  540. } else if (td->left_comp_ctx[row7]) {
  541. c = 3 * (s->s.h.fixcompref == 1 || td->left_ref_ctx[row7] == 1);
  542. } else {
  543. c = 4 * (td->left_ref_ctx[row7] == 1);
  544. }
  545. } else {
  546. c = 2;
  547. }
  548. bit = vp56_rac_get_prob(td->c, s->prob.p.single_ref[c][1]);
  549. td->counts.single_ref[c][1][bit]++;
  550. b->ref[0] = 1 + bit;
  551. }
  552. }
  553. }
  554. if (b->bs <= BS_8x8) {
  555. if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[b->seg_id].skip_enabled) {
  556. b->mode[0] =
  557. b->mode[1] =
  558. b->mode[2] =
  559. b->mode[3] = ZEROMV;
  560. } else {
  561. static const uint8_t off[10] = {
  562. 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
  563. };
  564. // FIXME this needs to use the LUT tables from find_ref_mvs
  565. // because not all are -1,0/0,-1
  566. int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
  567. [td->left_mode_ctx[row7 + off[b->bs]]];
  568. b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
  569. s->prob.p.mv_mode[c]);
  570. b->mode[1] =
  571. b->mode[2] =
  572. b->mode[3] = b->mode[0];
  573. td->counts.mv_mode[c][b->mode[0] - 10]++;
  574. }
  575. }
  576. if (s->s.h.filtermode == FILTER_SWITCHABLE) {
  577. int c;
  578. if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
  579. if (have_l && td->left_mode_ctx[row7] >= NEARESTMV) {
  580. c = s->above_filter_ctx[col] == td->left_filter_ctx[row7] ?
  581. td->left_filter_ctx[row7] : 3;
  582. } else {
  583. c = s->above_filter_ctx[col];
  584. }
  585. } else if (have_l && td->left_mode_ctx[row7] >= NEARESTMV) {
  586. c = td->left_filter_ctx[row7];
  587. } else {
  588. c = 3;
  589. }
  590. filter_id = vp8_rac_get_tree(td->c, ff_vp9_filter_tree,
  591. s->prob.p.filter[c]);
  592. td->counts.filter[c][filter_id]++;
  593. b->filter = ff_vp9_filter_lut[filter_id];
  594. } else {
  595. b->filter = s->s.h.filtermode;
  596. }
  597. if (b->bs > BS_8x8) {
  598. int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][td->left_mode_ctx[row7]];
  599. b->mode[0] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
  600. s->prob.p.mv_mode[c]);
  601. td->counts.mv_mode[c][b->mode[0] - 10]++;
  602. ff_vp9_fill_mv(td, b->mv[0], b->mode[0], 0);
  603. if (b->bs != BS_8x4) {
  604. b->mode[1] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
  605. s->prob.p.mv_mode[c]);
  606. td->counts.mv_mode[c][b->mode[1] - 10]++;
  607. ff_vp9_fill_mv(td, b->mv[1], b->mode[1], 1);
  608. } else {
  609. b->mode[1] = b->mode[0];
  610. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  611. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  612. }
  613. if (b->bs != BS_4x8) {
  614. b->mode[2] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
  615. s->prob.p.mv_mode[c]);
  616. td->counts.mv_mode[c][b->mode[2] - 10]++;
  617. ff_vp9_fill_mv(td, b->mv[2], b->mode[2], 2);
  618. if (b->bs != BS_8x4) {
  619. b->mode[3] = vp8_rac_get_tree(td->c, ff_vp9_inter_mode_tree,
  620. s->prob.p.mv_mode[c]);
  621. td->counts.mv_mode[c][b->mode[3] - 10]++;
  622. ff_vp9_fill_mv(td, b->mv[3], b->mode[3], 3);
  623. } else {
  624. b->mode[3] = b->mode[2];
  625. AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
  626. AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
  627. }
  628. } else {
  629. b->mode[2] = b->mode[0];
  630. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  631. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  632. b->mode[3] = b->mode[1];
  633. AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
  634. AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
  635. }
  636. } else {
  637. ff_vp9_fill_mv(td, b->mv[0], b->mode[0], -1);
  638. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  639. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  640. AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
  641. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  642. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  643. AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
  644. }
  645. vref = b->ref[b->comp ? s->s.h.signbias[s->s.h.varcompref[0]] : 0];
  646. }
  647. #if HAVE_FAST_64BIT
  648. #define SPLAT_CTX(var, val, n) \
  649. switch (n) { \
  650. case 1: var = val; break; \
  651. case 2: AV_WN16A(&var, val * 0x0101); break; \
  652. case 4: AV_WN32A(&var, val * 0x01010101); break; \
  653. case 8: AV_WN64A(&var, val * 0x0101010101010101ULL); break; \
  654. case 16: { \
  655. uint64_t v64 = val * 0x0101010101010101ULL; \
  656. AV_WN64A( &var, v64); \
  657. AV_WN64A(&((uint8_t *) &var)[8], v64); \
  658. break; \
  659. } \
  660. }
  661. #else
  662. #define SPLAT_CTX(var, val, n) \
  663. switch (n) { \
  664. case 1: var = val; break; \
  665. case 2: AV_WN16A(&var, val * 0x0101); break; \
  666. case 4: AV_WN32A(&var, val * 0x01010101); break; \
  667. case 8: { \
  668. uint32_t v32 = val * 0x01010101; \
  669. AV_WN32A( &var, v32); \
  670. AV_WN32A(&((uint8_t *) &var)[4], v32); \
  671. break; \
  672. } \
  673. case 16: { \
  674. uint32_t v32 = val * 0x01010101; \
  675. AV_WN32A( &var, v32); \
  676. AV_WN32A(&((uint8_t *) &var)[4], v32); \
  677. AV_WN32A(&((uint8_t *) &var)[8], v32); \
  678. AV_WN32A(&((uint8_t *) &var)[12], v32); \
  679. break; \
  680. } \
  681. }
  682. #endif
  683. switch (ff_vp9_bwh_tab[1][b->bs][0]) {
  684. #define SET_CTXS(perf, dir, off, n) \
  685. do { \
  686. SPLAT_CTX(perf->dir##_skip_ctx[off], b->skip, n); \
  687. SPLAT_CTX(perf->dir##_txfm_ctx[off], b->tx, n); \
  688. SPLAT_CTX(perf->dir##_partition_ctx[off], dir##_ctx[b->bs], n); \
  689. if (!s->s.h.keyframe && !s->s.h.intraonly) { \
  690. SPLAT_CTX(perf->dir##_intra_ctx[off], b->intra, n); \
  691. SPLAT_CTX(perf->dir##_comp_ctx[off], b->comp, n); \
  692. SPLAT_CTX(perf->dir##_mode_ctx[off], b->mode[3], n); \
  693. if (!b->intra) { \
  694. SPLAT_CTX(perf->dir##_ref_ctx[off], vref, n); \
  695. if (s->s.h.filtermode == FILTER_SWITCHABLE) { \
  696. SPLAT_CTX(perf->dir##_filter_ctx[off], filter_id, n); \
  697. } \
  698. } \
  699. } \
  700. } while (0)
  701. case 1: SET_CTXS(s, above, col, 1); break;
  702. case 2: SET_CTXS(s, above, col, 2); break;
  703. case 4: SET_CTXS(s, above, col, 4); break;
  704. case 8: SET_CTXS(s, above, col, 8); break;
  705. }
  706. switch (ff_vp9_bwh_tab[1][b->bs][1]) {
  707. case 1: SET_CTXS(td, left, row7, 1); break;
  708. case 2: SET_CTXS(td, left, row7, 2); break;
  709. case 4: SET_CTXS(td, left, row7, 4); break;
  710. case 8: SET_CTXS(td, left, row7, 8); break;
  711. }
  712. #undef SPLAT_CTX
  713. #undef SET_CTXS
  714. if (!s->s.h.keyframe && !s->s.h.intraonly) {
  715. if (b->bs > BS_8x8) {
  716. int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  717. AV_COPY32(&td->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
  718. AV_COPY32(&td->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
  719. AV_WN32A(&td->left_mv_ctx[row7 * 2 + 1][0], mv0);
  720. AV_WN32A(&td->left_mv_ctx[row7 * 2 + 1][1], mv1);
  721. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
  722. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
  723. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
  724. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
  725. } else {
  726. int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  727. for (n = 0; n < w4 * 2; n++) {
  728. AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
  729. AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
  730. }
  731. for (n = 0; n < h4 * 2; n++) {
  732. AV_WN32A(&td->left_mv_ctx[row7 * 2 + n][0], mv0);
  733. AV_WN32A(&td->left_mv_ctx[row7 * 2 + n][1], mv1);
  734. }
  735. }
  736. }
  737. // FIXME kinda ugly
  738. for (y = 0; y < h4; y++) {
  739. int x, o = (row + y) * s->sb_cols * 8 + col;
  740. VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[o];
  741. if (b->intra) {
  742. for (x = 0; x < w4; x++) {
  743. mv[x].ref[0] =
  744. mv[x].ref[1] = -1;
  745. }
  746. } else if (b->comp) {
  747. for (x = 0; x < w4; x++) {
  748. mv[x].ref[0] = b->ref[0];
  749. mv[x].ref[1] = b->ref[1];
  750. AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
  751. AV_COPY32(&mv[x].mv[1], &b->mv[3][1]);
  752. }
  753. } else {
  754. for (x = 0; x < w4; x++) {
  755. mv[x].ref[0] = b->ref[0];
  756. mv[x].ref[1] = -1;
  757. AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
  758. }
  759. }
  760. }
  761. }
  762. // FIXME merge cnt/eob arguments?
  763. static av_always_inline int
  764. decode_coeffs_b_generic(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
  765. int is_tx32x32, int is8bitsperpixel, int bpp, unsigned (*cnt)[6][3],
  766. unsigned (*eob)[6][2], uint8_t (*p)[6][11],
  767. int nnz, const int16_t *scan, const int16_t (*nb)[2],
  768. const int16_t *band_counts, int16_t *qmul)
  769. {
  770. int i = 0, band = 0, band_left = band_counts[band];
  771. const uint8_t *tp = p[0][nnz];
  772. uint8_t cache[1024];
  773. do {
  774. int val, rc;
  775. val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
  776. eob[band][nnz][val]++;
  777. if (!val)
  778. break;
  779. skip_eob:
  780. if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
  781. cnt[band][nnz][0]++;
  782. if (!--band_left)
  783. band_left = band_counts[++band];
  784. cache[scan[i]] = 0;
  785. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  786. tp = p[band][nnz];
  787. if (++i == n_coeffs)
  788. break; //invalid input; blocks should end with EOB
  789. goto skip_eob;
  790. }
  791. rc = scan[i];
  792. if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
  793. cnt[band][nnz][1]++;
  794. val = 1;
  795. cache[rc] = 1;
  796. } else {
  797. cnt[band][nnz][2]++;
  798. if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
  799. if (!vp56_rac_get_prob_branchy(c, tp[4])) {
  800. cache[rc] = val = 2;
  801. } else {
  802. val = 3 + vp56_rac_get_prob(c, tp[5]);
  803. cache[rc] = 3;
  804. }
  805. } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
  806. cache[rc] = 4;
  807. if (!vp56_rac_get_prob_branchy(c, tp[7])) {
  808. val = vp56_rac_get_prob(c, 159) + 5;
  809. } else {
  810. val = (vp56_rac_get_prob(c, 165) << 1) + 7;
  811. val += vp56_rac_get_prob(c, 145);
  812. }
  813. } else { // cat 3-6
  814. cache[rc] = 5;
  815. if (!vp56_rac_get_prob_branchy(c, tp[8])) {
  816. if (!vp56_rac_get_prob_branchy(c, tp[9])) {
  817. val = 11 + (vp56_rac_get_prob(c, 173) << 2);
  818. val += (vp56_rac_get_prob(c, 148) << 1);
  819. val += vp56_rac_get_prob(c, 140);
  820. } else {
  821. val = 19 + (vp56_rac_get_prob(c, 176) << 3);
  822. val += (vp56_rac_get_prob(c, 155) << 2);
  823. val += (vp56_rac_get_prob(c, 140) << 1);
  824. val += vp56_rac_get_prob(c, 135);
  825. }
  826. } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
  827. val = (vp56_rac_get_prob(c, 180) << 4) + 35;
  828. val += (vp56_rac_get_prob(c, 157) << 3);
  829. val += (vp56_rac_get_prob(c, 141) << 2);
  830. val += (vp56_rac_get_prob(c, 134) << 1);
  831. val += vp56_rac_get_prob(c, 130);
  832. } else {
  833. val = 67;
  834. if (!is8bitsperpixel) {
  835. if (bpp == 12) {
  836. val += vp56_rac_get_prob(c, 255) << 17;
  837. val += vp56_rac_get_prob(c, 255) << 16;
  838. }
  839. val += (vp56_rac_get_prob(c, 255) << 15);
  840. val += (vp56_rac_get_prob(c, 255) << 14);
  841. }
  842. val += (vp56_rac_get_prob(c, 254) << 13);
  843. val += (vp56_rac_get_prob(c, 254) << 12);
  844. val += (vp56_rac_get_prob(c, 254) << 11);
  845. val += (vp56_rac_get_prob(c, 252) << 10);
  846. val += (vp56_rac_get_prob(c, 249) << 9);
  847. val += (vp56_rac_get_prob(c, 243) << 8);
  848. val += (vp56_rac_get_prob(c, 230) << 7);
  849. val += (vp56_rac_get_prob(c, 196) << 6);
  850. val += (vp56_rac_get_prob(c, 177) << 5);
  851. val += (vp56_rac_get_prob(c, 153) << 4);
  852. val += (vp56_rac_get_prob(c, 140) << 3);
  853. val += (vp56_rac_get_prob(c, 133) << 2);
  854. val += (vp56_rac_get_prob(c, 130) << 1);
  855. val += vp56_rac_get_prob(c, 129);
  856. }
  857. }
  858. }
  859. #define STORE_COEF(c, i, v) do { \
  860. if (is8bitsperpixel) { \
  861. c[i] = v; \
  862. } else { \
  863. AV_WN32A(&c[i * 2], v); \
  864. } \
  865. } while (0)
  866. if (!--band_left)
  867. band_left = band_counts[++band];
  868. if (is_tx32x32)
  869. STORE_COEF(coef, rc, (int)((vp8_rac_get(c) ? -val : val) * (unsigned)qmul[!!i]) / 2);
  870. else
  871. STORE_COEF(coef, rc, (vp8_rac_get(c) ? -val : val) * (unsigned)qmul[!!i]);
  872. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  873. tp = p[band][nnz];
  874. } while (++i < n_coeffs);
  875. return i;
  876. }
  877. static int decode_coeffs_b_8bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
  878. unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
  879. uint8_t (*p)[6][11], int nnz, const int16_t *scan,
  880. const int16_t (*nb)[2], const int16_t *band_counts,
  881. int16_t *qmul)
  882. {
  883. return decode_coeffs_b_generic(td->c, coef, n_coeffs, 0, 1, 8, cnt, eob, p,
  884. nnz, scan, nb, band_counts, qmul);
  885. }
  886. static int decode_coeffs_b32_8bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
  887. unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
  888. uint8_t (*p)[6][11], int nnz, const int16_t *scan,
  889. const int16_t (*nb)[2], const int16_t *band_counts,
  890. int16_t *qmul)
  891. {
  892. return decode_coeffs_b_generic(td->c, coef, n_coeffs, 1, 1, 8, cnt, eob, p,
  893. nnz, scan, nb, band_counts, qmul);
  894. }
  895. static int decode_coeffs_b_16bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
  896. unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
  897. uint8_t (*p)[6][11], int nnz, const int16_t *scan,
  898. const int16_t (*nb)[2], const int16_t *band_counts,
  899. int16_t *qmul)
  900. {
  901. return decode_coeffs_b_generic(td->c, coef, n_coeffs, 0, 0, td->s->s.h.bpp, cnt, eob, p,
  902. nnz, scan, nb, band_counts, qmul);
  903. }
  904. static int decode_coeffs_b32_16bpp(VP9TileData *td, int16_t *coef, int n_coeffs,
  905. unsigned (*cnt)[6][3], unsigned (*eob)[6][2],
  906. uint8_t (*p)[6][11], int nnz, const int16_t *scan,
  907. const int16_t (*nb)[2], const int16_t *band_counts,
  908. int16_t *qmul)
  909. {
  910. return decode_coeffs_b_generic(td->c, coef, n_coeffs, 1, 0, td->s->s.h.bpp, cnt, eob, p,
  911. nnz, scan, nb, band_counts, qmul);
  912. }
  913. static av_always_inline int decode_coeffs(VP9TileData *td, int is8bitsperpixel)
  914. {
  915. VP9Context *s = td->s;
  916. VP9Block *b = td->b;
  917. int row = td->row, col = td->col;
  918. uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
  919. unsigned (*c)[6][3] = td->counts.coef[b->tx][0 /* y */][!b->intra];
  920. unsigned (*e)[6][2] = td->counts.eob[b->tx][0 /* y */][!b->intra];
  921. int w4 = ff_vp9_bwh_tab[1][b->bs][0] << 1, h4 = ff_vp9_bwh_tab[1][b->bs][1] << 1;
  922. int end_x = FFMIN(2 * (s->cols - col), w4);
  923. int end_y = FFMIN(2 * (s->rows - row), h4);
  924. int n, pl, x, y, ret;
  925. int16_t (*qmul)[2] = s->s.h.segmentation.feat[b->seg_id].qmul;
  926. int tx = 4 * s->s.h.lossless + b->tx;
  927. const int16_t * const *yscans = ff_vp9_scans[tx];
  928. const int16_t (* const * ynbs)[2] = ff_vp9_scans_nb[tx];
  929. const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
  930. const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
  931. uint8_t *a = &s->above_y_nnz_ctx[col * 2];
  932. uint8_t *l = &td->left_y_nnz_ctx[(row & 7) << 1];
  933. static const int16_t band_counts[4][8] = {
  934. { 1, 2, 3, 4, 3, 16 - 13 },
  935. { 1, 2, 3, 4, 11, 64 - 21 },
  936. { 1, 2, 3, 4, 11, 256 - 21 },
  937. { 1, 2, 3, 4, 11, 1024 - 21 },
  938. };
  939. const int16_t *y_band_counts = band_counts[b->tx];
  940. const int16_t *uv_band_counts = band_counts[b->uvtx];
  941. int bytesperpixel = is8bitsperpixel ? 1 : 2;
  942. int total_coeff = 0;
  943. #define MERGE(la, end, step, rd) \
  944. for (n = 0; n < end; n += step) \
  945. la[n] = !!rd(&la[n])
  946. #define MERGE_CTX(step, rd) \
  947. do { \
  948. MERGE(l, end_y, step, rd); \
  949. MERGE(a, end_x, step, rd); \
  950. } while (0)
  951. #define DECODE_Y_COEF_LOOP(step, mode_index, v) \
  952. for (n = 0, y = 0; y < end_y; y += step) { \
  953. for (x = 0; x < end_x; x += step, n += step * step) { \
  954. enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[mode_index]]; \
  955. ret = (is8bitsperpixel ? decode_coeffs_b##v##_8bpp : decode_coeffs_b##v##_16bpp) \
  956. (td, td->block + 16 * n * bytesperpixel, 16 * step * step, \
  957. c, e, p, a[x] + l[y], yscans[txtp], \
  958. ynbs[txtp], y_band_counts, qmul[0]); \
  959. a[x] = l[y] = !!ret; \
  960. total_coeff |= !!ret; \
  961. if (step >= 4) { \
  962. AV_WN16A(&td->eob[n], ret); \
  963. } else { \
  964. td->eob[n] = ret; \
  965. } \
  966. } \
  967. }
  968. #define SPLAT(la, end, step, cond) \
  969. if (step == 2) { \
  970. for (n = 1; n < end; n += step) \
  971. la[n] = la[n - 1]; \
  972. } else if (step == 4) { \
  973. if (cond) { \
  974. for (n = 0; n < end; n += step) \
  975. AV_WN32A(&la[n], la[n] * 0x01010101); \
  976. } else { \
  977. for (n = 0; n < end; n += step) \
  978. memset(&la[n + 1], la[n], FFMIN(end - n - 1, 3)); \
  979. } \
  980. } else /* step == 8 */ { \
  981. if (cond) { \
  982. if (HAVE_FAST_64BIT) { \
  983. for (n = 0; n < end; n += step) \
  984. AV_WN64A(&la[n], la[n] * 0x0101010101010101ULL); \
  985. } else { \
  986. for (n = 0; n < end; n += step) { \
  987. uint32_t v32 = la[n] * 0x01010101; \
  988. AV_WN32A(&la[n], v32); \
  989. AV_WN32A(&la[n + 4], v32); \
  990. } \
  991. } \
  992. } else { \
  993. for (n = 0; n < end; n += step) \
  994. memset(&la[n + 1], la[n], FFMIN(end - n - 1, 7)); \
  995. } \
  996. }
  997. #define SPLAT_CTX(step) \
  998. do { \
  999. SPLAT(a, end_x, step, end_x == w4); \
  1000. SPLAT(l, end_y, step, end_y == h4); \
  1001. } while (0)
  1002. /* y tokens */
  1003. switch (b->tx) {
  1004. case TX_4X4:
  1005. DECODE_Y_COEF_LOOP(1, b->bs > BS_8x8 ? n : 0,);
  1006. break;
  1007. case TX_8X8:
  1008. MERGE_CTX(2, AV_RN16A);
  1009. DECODE_Y_COEF_LOOP(2, 0,);
  1010. SPLAT_CTX(2);
  1011. break;
  1012. case TX_16X16:
  1013. MERGE_CTX(4, AV_RN32A);
  1014. DECODE_Y_COEF_LOOP(4, 0,);
  1015. SPLAT_CTX(4);
  1016. break;
  1017. case TX_32X32:
  1018. MERGE_CTX(8, AV_RN64A);
  1019. DECODE_Y_COEF_LOOP(8, 0, 32);
  1020. SPLAT_CTX(8);
  1021. break;
  1022. }
  1023. #define DECODE_UV_COEF_LOOP(step, v) \
  1024. for (n = 0, y = 0; y < end_y; y += step) { \
  1025. for (x = 0; x < end_x; x += step, n += step * step) { \
  1026. ret = (is8bitsperpixel ? decode_coeffs_b##v##_8bpp : decode_coeffs_b##v##_16bpp) \
  1027. (td, td->uvblock[pl] + 16 * n * bytesperpixel, \
  1028. 16 * step * step, c, e, p, a[x] + l[y], \
  1029. uvscan, uvnb, uv_band_counts, qmul[1]); \
  1030. a[x] = l[y] = !!ret; \
  1031. total_coeff |= !!ret; \
  1032. if (step >= 4) { \
  1033. AV_WN16A(&td->uveob[pl][n], ret); \
  1034. } else { \
  1035. td->uveob[pl][n] = ret; \
  1036. } \
  1037. } \
  1038. }
  1039. p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
  1040. c = td->counts.coef[b->uvtx][1 /* uv */][!b->intra];
  1041. e = td->counts.eob[b->uvtx][1 /* uv */][!b->intra];
  1042. w4 >>= s->ss_h;
  1043. end_x >>= s->ss_h;
  1044. h4 >>= s->ss_v;
  1045. end_y >>= s->ss_v;
  1046. for (pl = 0; pl < 2; pl++) {
  1047. a = &s->above_uv_nnz_ctx[pl][col << !s->ss_h];
  1048. l = &td->left_uv_nnz_ctx[pl][(row & 7) << !s->ss_v];
  1049. switch (b->uvtx) {
  1050. case TX_4X4:
  1051. DECODE_UV_COEF_LOOP(1,);
  1052. break;
  1053. case TX_8X8:
  1054. MERGE_CTX(2, AV_RN16A);
  1055. DECODE_UV_COEF_LOOP(2,);
  1056. SPLAT_CTX(2);
  1057. break;
  1058. case TX_16X16:
  1059. MERGE_CTX(4, AV_RN32A);
  1060. DECODE_UV_COEF_LOOP(4,);
  1061. SPLAT_CTX(4);
  1062. break;
  1063. case TX_32X32:
  1064. MERGE_CTX(8, AV_RN64A);
  1065. DECODE_UV_COEF_LOOP(8, 32);
  1066. SPLAT_CTX(8);
  1067. break;
  1068. }
  1069. }
  1070. return total_coeff;
  1071. }
  1072. static int decode_coeffs_8bpp(VP9TileData *td)
  1073. {
  1074. return decode_coeffs(td, 1);
  1075. }
  1076. static int decode_coeffs_16bpp(VP9TileData *td)
  1077. {
  1078. return decode_coeffs(td, 0);
  1079. }
  1080. static av_always_inline void mask_edges(uint8_t (*mask)[8][4], int ss_h, int ss_v,
  1081. int row_and_7, int col_and_7,
  1082. int w, int h, int col_end, int row_end,
  1083. enum TxfmMode tx, int skip_inter)
  1084. {
  1085. static const unsigned wide_filter_col_mask[2] = { 0x11, 0x01 };
  1086. static const unsigned wide_filter_row_mask[2] = { 0x03, 0x07 };
  1087. // FIXME I'm pretty sure all loops can be replaced by a single LUT if
  1088. // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
  1089. // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
  1090. // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
  1091. // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
  1092. // edges. This means that for UV, we work on two subsampled blocks at
  1093. // a time, and we only use the topleft block's mode information to set
  1094. // things like block strength. Thus, for any block size smaller than
  1095. // 16x16, ignore the odd portion of the block.
  1096. if (tx == TX_4X4 && (ss_v | ss_h)) {
  1097. if (h == ss_v) {
  1098. if (row_and_7 & 1)
  1099. return;
  1100. if (!row_end)
  1101. h += 1;
  1102. }
  1103. if (w == ss_h) {
  1104. if (col_and_7 & 1)
  1105. return;
  1106. if (!col_end)
  1107. w += 1;
  1108. }
  1109. }
  1110. if (tx == TX_4X4 && !skip_inter) {
  1111. int t = 1 << col_and_7, m_col = (t << w) - t, y;
  1112. // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
  1113. int m_row_8 = m_col & wide_filter_col_mask[ss_h], m_row_4 = m_col - m_row_8;
  1114. for (y = row_and_7; y < h + row_and_7; y++) {
  1115. int col_mask_id = 2 - !(y & wide_filter_row_mask[ss_v]);
  1116. mask[0][y][1] |= m_row_8;
  1117. mask[0][y][2] |= m_row_4;
  1118. // for odd lines, if the odd col is not being filtered,
  1119. // skip odd row also:
  1120. // .---. <-- a
  1121. // | |
  1122. // |___| <-- b
  1123. // ^ ^
  1124. // c d
  1125. //
  1126. // if a/c are even row/col and b/d are odd, and d is skipped,
  1127. // e.g. right edge of size-66x66.webm, then skip b also (bug)
  1128. if ((ss_h & ss_v) && (col_end & 1) && (y & 1)) {
  1129. mask[1][y][col_mask_id] |= (t << (w - 1)) - t;
  1130. } else {
  1131. mask[1][y][col_mask_id] |= m_col;
  1132. }
  1133. if (!ss_h)
  1134. mask[0][y][3] |= m_col;
  1135. if (!ss_v) {
  1136. if (ss_h && (col_end & 1))
  1137. mask[1][y][3] |= (t << (w - 1)) - t;
  1138. else
  1139. mask[1][y][3] |= m_col;
  1140. }
  1141. }
  1142. } else {
  1143. int y, t = 1 << col_and_7, m_col = (t << w) - t;
  1144. if (!skip_inter) {
  1145. int mask_id = (tx == TX_8X8);
  1146. int l2 = tx + ss_h - 1, step1d;
  1147. static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
  1148. int m_row = m_col & masks[l2];
  1149. // at odd UV col/row edges tx16/tx32 loopfilter edges, force
  1150. // 8wd loopfilter to prevent going off the visible edge.
  1151. if (ss_h && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
  1152. int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
  1153. int m_row_8 = m_row - m_row_16;
  1154. for (y = row_and_7; y < h + row_and_7; y++) {
  1155. mask[0][y][0] |= m_row_16;
  1156. mask[0][y][1] |= m_row_8;
  1157. }
  1158. } else {
  1159. for (y = row_and_7; y < h + row_and_7; y++)
  1160. mask[0][y][mask_id] |= m_row;
  1161. }
  1162. l2 = tx + ss_v - 1;
  1163. step1d = 1 << l2;
  1164. if (ss_v && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
  1165. for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
  1166. mask[1][y][0] |= m_col;
  1167. if (y - row_and_7 == h - 1)
  1168. mask[1][y][1] |= m_col;
  1169. } else {
  1170. for (y = row_and_7; y < h + row_and_7; y += step1d)
  1171. mask[1][y][mask_id] |= m_col;
  1172. }
  1173. } else if (tx != TX_4X4) {
  1174. int mask_id;
  1175. mask_id = (tx == TX_8X8) || (h == ss_v);
  1176. mask[1][row_and_7][mask_id] |= m_col;
  1177. mask_id = (tx == TX_8X8) || (w == ss_h);
  1178. for (y = row_and_7; y < h + row_and_7; y++)
  1179. mask[0][y][mask_id] |= t;
  1180. } else {
  1181. int t8 = t & wide_filter_col_mask[ss_h], t4 = t - t8;
  1182. for (y = row_and_7; y < h + row_and_7; y++) {
  1183. mask[0][y][2] |= t4;
  1184. mask[0][y][1] |= t8;
  1185. }
  1186. mask[1][row_and_7][2 - !(row_and_7 & wide_filter_row_mask[ss_v])] |= m_col;
  1187. }
  1188. }
  1189. }
  1190. void ff_vp9_decode_block(VP9TileData *td, int row, int col,
  1191. VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  1192. enum BlockLevel bl, enum BlockPartition bp)
  1193. {
  1194. VP9Context *s = td->s;
  1195. VP9Block *b = td->b;
  1196. enum BlockSize bs = bl * 3 + bp;
  1197. int bytesperpixel = s->bytesperpixel;
  1198. int w4 = ff_vp9_bwh_tab[1][bs][0], h4 = ff_vp9_bwh_tab[1][bs][1], lvl;
  1199. int emu[2];
  1200. AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
  1201. td->row = row;
  1202. td->row7 = row & 7;
  1203. td->col = col;
  1204. td->col7 = col & 7;
  1205. td->min_mv.x = -(128 + col * 64);
  1206. td->min_mv.y = -(128 + row * 64);
  1207. td->max_mv.x = 128 + (s->cols - col - w4) * 64;
  1208. td->max_mv.y = 128 + (s->rows - row - h4) * 64;
  1209. if (s->pass < 2) {
  1210. b->bs = bs;
  1211. b->bl = bl;
  1212. b->bp = bp;
  1213. decode_mode(td);
  1214. b->uvtx = b->tx - ((s->ss_h && w4 * 2 == (1 << b->tx)) ||
  1215. (s->ss_v && h4 * 2 == (1 << b->tx)));
  1216. if (!b->skip) {
  1217. int has_coeffs;
  1218. if (bytesperpixel == 1) {
  1219. has_coeffs = decode_coeffs_8bpp(td);
  1220. } else {
  1221. has_coeffs = decode_coeffs_16bpp(td);
  1222. }
  1223. if (!has_coeffs && b->bs <= BS_8x8 && !b->intra) {
  1224. b->skip = 1;
  1225. memset(&s->above_skip_ctx[col], 1, w4);
  1226. memset(&td->left_skip_ctx[td->row7], 1, h4);
  1227. }
  1228. } else {
  1229. int row7 = td->row7;
  1230. #define SPLAT_ZERO_CTX(v, n) \
  1231. switch (n) { \
  1232. case 1: v = 0; break; \
  1233. case 2: AV_ZERO16(&v); break; \
  1234. case 4: AV_ZERO32(&v); break; \
  1235. case 8: AV_ZERO64(&v); break; \
  1236. case 16: AV_ZERO128(&v); break; \
  1237. }
  1238. #define SPLAT_ZERO_YUV(dir, var, off, n, dir2) \
  1239. do { \
  1240. SPLAT_ZERO_CTX(dir##_y_##var[off * 2], n * 2); \
  1241. if (s->ss_##dir2) { \
  1242. SPLAT_ZERO_CTX(dir##_uv_##var[0][off], n); \
  1243. SPLAT_ZERO_CTX(dir##_uv_##var[1][off], n); \
  1244. } else { \
  1245. SPLAT_ZERO_CTX(dir##_uv_##var[0][off * 2], n * 2); \
  1246. SPLAT_ZERO_CTX(dir##_uv_##var[1][off * 2], n * 2); \
  1247. } \
  1248. } while (0)
  1249. switch (w4) {
  1250. case 1: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 1, h); break;
  1251. case 2: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 2, h); break;
  1252. case 4: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 4, h); break;
  1253. case 8: SPLAT_ZERO_YUV(s->above, nnz_ctx, col, 8, h); break;
  1254. }
  1255. switch (h4) {
  1256. case 1: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 1, v); break;
  1257. case 2: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 2, v); break;
  1258. case 4: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 4, v); break;
  1259. case 8: SPLAT_ZERO_YUV(td->left, nnz_ctx, row7, 8, v); break;
  1260. }
  1261. }
  1262. if (s->pass == 1) {
  1263. s->td[0].b++;
  1264. s->td[0].block += w4 * h4 * 64 * bytesperpixel;
  1265. s->td[0].uvblock[0] += w4 * h4 * 64 * bytesperpixel >> (s->ss_h + s->ss_v);
  1266. s->td[0].uvblock[1] += w4 * h4 * 64 * bytesperpixel >> (s->ss_h + s->ss_v);
  1267. s->td[0].eob += 4 * w4 * h4;
  1268. s->td[0].uveob[0] += 4 * w4 * h4 >> (s->ss_h + s->ss_v);
  1269. s->td[0].uveob[1] += 4 * w4 * h4 >> (s->ss_h + s->ss_v);
  1270. return;
  1271. }
  1272. }
  1273. // emulated overhangs if the stride of the target buffer can't hold. This
  1274. // makes it possible to support emu-edge and so on even if we have large block
  1275. // overhangs
  1276. emu[0] = (col + w4) * 8 * bytesperpixel > f->linesize[0] ||
  1277. (row + h4) > s->rows;
  1278. emu[1] = ((col + w4) * 8 >> s->ss_h) * bytesperpixel > f->linesize[1] ||
  1279. (row + h4) > s->rows;
  1280. if (emu[0]) {
  1281. td->dst[0] = td->tmp_y;
  1282. td->y_stride = 128;
  1283. } else {
  1284. td->dst[0] = f->data[0] + yoff;
  1285. td->y_stride = f->linesize[0];
  1286. }
  1287. if (emu[1]) {
  1288. td->dst[1] = td->tmp_uv[0];
  1289. td->dst[2] = td->tmp_uv[1];
  1290. td->uv_stride = 128;
  1291. } else {
  1292. td->dst[1] = f->data[1] + uvoff;
  1293. td->dst[2] = f->data[2] + uvoff;
  1294. td->uv_stride = f->linesize[1];
  1295. }
  1296. if (b->intra) {
  1297. if (s->s.h.bpp > 8) {
  1298. ff_vp9_intra_recon_16bpp(td, yoff, uvoff);
  1299. } else {
  1300. ff_vp9_intra_recon_8bpp(td, yoff, uvoff);
  1301. }
  1302. } else {
  1303. if (s->s.h.bpp > 8) {
  1304. ff_vp9_inter_recon_16bpp(td);
  1305. } else {
  1306. ff_vp9_inter_recon_8bpp(td);
  1307. }
  1308. }
  1309. if (emu[0]) {
  1310. int w = FFMIN(s->cols - col, w4) * 8, h = FFMIN(s->rows - row, h4) * 8, n, o = 0;
  1311. for (n = 0; o < w; n++) {
  1312. int bw = 64 >> n;
  1313. av_assert2(n <= 4);
  1314. if (w & bw) {
  1315. s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o * bytesperpixel, f->linesize[0],
  1316. td->tmp_y + o * bytesperpixel, 128, h, 0, 0);
  1317. o += bw;
  1318. }
  1319. }
  1320. }
  1321. if (emu[1]) {
  1322. int w = FFMIN(s->cols - col, w4) * 8 >> s->ss_h;
  1323. int h = FFMIN(s->rows - row, h4) * 8 >> s->ss_v, n, o = 0;
  1324. for (n = s->ss_h; o < w; n++) {
  1325. int bw = 64 >> n;
  1326. av_assert2(n <= 4);
  1327. if (w & bw) {
  1328. s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o * bytesperpixel, f->linesize[1],
  1329. td->tmp_uv[0] + o * bytesperpixel, 128, h, 0, 0);
  1330. s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o * bytesperpixel, f->linesize[2],
  1331. td->tmp_uv[1] + o * bytesperpixel, 128, h, 0, 0);
  1332. o += bw;
  1333. }
  1334. }
  1335. }
  1336. // pick filter level and find edges to apply filter to
  1337. if (s->s.h.filter.level &&
  1338. (lvl = s->s.h.segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
  1339. [b->mode[3] != ZEROMV]) > 0) {
  1340. int x_end = FFMIN(s->cols - col, w4), y_end = FFMIN(s->rows - row, h4);
  1341. int skip_inter = !b->intra && b->skip, col7 = td->col7, row7 = td->row7;
  1342. setctx_2d(&lflvl->level[row7 * 8 + col7], w4, h4, 8, lvl);
  1343. mask_edges(lflvl->mask[0], 0, 0, row7, col7, x_end, y_end, 0, 0, b->tx, skip_inter);
  1344. if (s->ss_h || s->ss_v)
  1345. mask_edges(lflvl->mask[1], s->ss_h, s->ss_v, row7, col7, x_end, y_end,
  1346. s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
  1347. s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
  1348. b->uvtx, skip_inter);
  1349. }
  1350. if (s->pass == 2) {
  1351. s->td[0].b++;
  1352. s->td[0].block += w4 * h4 * 64 * bytesperpixel;
  1353. s->td[0].uvblock[0] += w4 * h4 * 64 * bytesperpixel >> (s->ss_v + s->ss_h);
  1354. s->td[0].uvblock[1] += w4 * h4 * 64 * bytesperpixel >> (s->ss_v + s->ss_h);
  1355. s->td[0].eob += 4 * w4 * h4;
  1356. s->td[0].uveob[0] += 4 * w4 * h4 >> (s->ss_v + s->ss_h);
  1357. s->td[0].uveob[1] += 4 * w4 * h4 >> (s->ss_v + s->ss_h);
  1358. }
  1359. }