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.

1686 lines
72KB

  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. static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
  32. {
  33. { 16, 16 }, { 16, 8 }, { 8, 16 }, { 8, 8 }, { 8, 4 }, { 4, 8 },
  34. { 4, 4 }, { 4, 2 }, { 2, 4 }, { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 },
  35. }, {
  36. { 8, 8 }, { 8, 4 }, { 4, 8 }, { 4, 4 }, { 4, 2 }, { 2, 4 },
  37. { 2, 2 }, { 2, 1 }, { 1, 2 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
  38. }
  39. };
  40. // differential forward probability updates
  41. static void decode_mode(VP9Context *s, VP9Block *const b)
  42. {
  43. static const uint8_t left_ctx[N_BS_SIZES] = {
  44. 0x0, 0x8, 0x0, 0x8, 0xc, 0x8, 0xc, 0xe, 0xc, 0xe, 0xf, 0xe, 0xf
  45. };
  46. static const uint8_t above_ctx[N_BS_SIZES] = {
  47. 0x0, 0x0, 0x8, 0x8, 0x8, 0xc, 0xc, 0xc, 0xe, 0xe, 0xe, 0xf, 0xf
  48. };
  49. static const uint8_t max_tx_for_bl_bp[N_BS_SIZES] = {
  50. TX_32X32, TX_32X32, TX_32X32, TX_32X32, TX_16X16, TX_16X16,
  51. TX_16X16, TX_8X8, TX_8X8, TX_8X8, TX_4X4, TX_4X4, TX_4X4
  52. };
  53. int row = b->row, col = b->col, row7 = b->row7;
  54. enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
  55. int w4 = FFMIN(s->cols - col, bwh_tab[1][b->bs][0]);
  56. int h4 = FFMIN(s->rows - row, bwh_tab[1][b->bs][1]);
  57. int have_a = row > 0, have_l = col > s->tiling.tile_col_start;
  58. int y;
  59. if (!s->segmentation.enabled) {
  60. b->seg_id = 0;
  61. } else if (s->keyframe || s->intraonly) {
  62. b->seg_id = s->segmentation.update_map ?
  63. vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree, s->prob.seg) : 0;
  64. } else if (!s->segmentation.update_map ||
  65. (s->segmentation.temporal &&
  66. vp56_rac_get_prob_branchy(&s->c,
  67. s->prob.segpred[s->above_segpred_ctx[col] +
  68. s->left_segpred_ctx[row7]]))) {
  69. int pred = MAX_SEGMENT - 1;
  70. int x;
  71. for (y = 0; y < h4; y++)
  72. for (x = 0; x < w4; x++)
  73. pred = FFMIN(pred,
  74. s->segmentation_map[(y + row) * 8 * s->sb_cols + x + col]);
  75. b->seg_id = pred;
  76. memset(&s->above_segpred_ctx[col], 1, w4);
  77. memset(&s->left_segpred_ctx[row7], 1, h4);
  78. } else {
  79. b->seg_id = vp8_rac_get_tree(&s->c, ff_vp9_segmentation_tree,
  80. s->prob.seg);
  81. memset(&s->above_segpred_ctx[col], 0, w4);
  82. memset(&s->left_segpred_ctx[row7], 0, h4);
  83. }
  84. if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
  85. for (y = 0; y < h4; y++)
  86. memset(&s->segmentation_map[(y + row) * 8 * s->sb_cols + col],
  87. b->seg_id, w4);
  88. }
  89. b->skip = s->segmentation.enabled &&
  90. s->segmentation.feat[b->seg_id].skip_enabled;
  91. if (!b->skip) {
  92. int c = s->left_skip_ctx[row7] + s->above_skip_ctx[col];
  93. b->skip = vp56_rac_get_prob(&s->c, s->prob.p.skip[c]);
  94. s->counts.skip[c][b->skip]++;
  95. }
  96. if (s->keyframe || s->intraonly) {
  97. b->intra = 1;
  98. } else if (s->segmentation.feat[b->seg_id].ref_enabled) {
  99. b->intra = !s->segmentation.feat[b->seg_id].ref_val;
  100. } else {
  101. int c, bit;
  102. if (have_a && have_l) {
  103. c = s->above_intra_ctx[col] + s->left_intra_ctx[row7];
  104. c += (c == 2);
  105. } else {
  106. c = have_a ? 2 * s->above_intra_ctx[col] :
  107. have_l ? 2 * s->left_intra_ctx[row7] : 0;
  108. }
  109. bit = vp56_rac_get_prob(&s->c, s->prob.p.intra[c]);
  110. s->counts.intra[c][bit]++;
  111. b->intra = !bit;
  112. }
  113. if ((b->intra || !b->skip) && s->txfmmode == TX_SWITCHABLE) {
  114. int c;
  115. if (have_a) {
  116. if (have_l) {
  117. c = (s->above_skip_ctx[col] ? max_tx :
  118. s->above_txfm_ctx[col]) +
  119. (s->left_skip_ctx[row7] ? max_tx :
  120. s->left_txfm_ctx[row7]) > max_tx;
  121. } else {
  122. c = s->above_skip_ctx[col] ? 1 :
  123. (s->above_txfm_ctx[col] * 2 > max_tx);
  124. }
  125. } else if (have_l) {
  126. c = s->left_skip_ctx[row7] ? 1 :
  127. (s->left_txfm_ctx[row7] * 2 > max_tx);
  128. } else {
  129. c = 1;
  130. }
  131. switch (max_tx) {
  132. case TX_32X32:
  133. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][0]);
  134. if (b->tx) {
  135. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][1]);
  136. if (b->tx == 2)
  137. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx32p[c][2]);
  138. }
  139. s->counts.tx32p[c][b->tx]++;
  140. break;
  141. case TX_16X16:
  142. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][0]);
  143. if (b->tx)
  144. b->tx += vp56_rac_get_prob(&s->c, s->prob.p.tx16p[c][1]);
  145. s->counts.tx16p[c][b->tx]++;
  146. break;
  147. case TX_8X8:
  148. b->tx = vp56_rac_get_prob(&s->c, s->prob.p.tx8p[c]);
  149. s->counts.tx8p[c][b->tx]++;
  150. break;
  151. case TX_4X4:
  152. b->tx = TX_4X4;
  153. break;
  154. }
  155. } else {
  156. b->tx = FFMIN(max_tx, s->txfmmode);
  157. }
  158. if (s->keyframe || s->intraonly) {
  159. uint8_t *a = &s->above_mode_ctx[col * 2];
  160. uint8_t *l = &s->left_mode_ctx[(row7) << 1];
  161. b->comp = 0;
  162. if (b->bs > BS_8x8) {
  163. // FIXME the memory storage intermediates here aren't really
  164. // necessary, they're just there to make the code slightly
  165. // simpler for now
  166. b->mode[0] =
  167. a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  168. ff_vp9_default_kf_ymode_probs[a[0]][l[0]]);
  169. if (b->bs != BS_8x4) {
  170. b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  171. ff_vp9_default_kf_ymode_probs[a[1]][b->mode[0]]);
  172. l[0] =
  173. a[1] = b->mode[1];
  174. } else {
  175. l[0] =
  176. a[1] =
  177. b->mode[1] = b->mode[0];
  178. }
  179. if (b->bs != BS_4x8) {
  180. b->mode[2] =
  181. a[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  182. ff_vp9_default_kf_ymode_probs[a[0]][l[1]]);
  183. if (b->bs != BS_8x4) {
  184. b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  185. ff_vp9_default_kf_ymode_probs[a[1]][b->mode[2]]);
  186. l[1] =
  187. a[1] = b->mode[3];
  188. } else {
  189. l[1] =
  190. a[1] =
  191. b->mode[3] = b->mode[2];
  192. }
  193. } else {
  194. b->mode[2] = b->mode[0];
  195. l[1] =
  196. a[1] =
  197. b->mode[3] = b->mode[1];
  198. }
  199. } else {
  200. b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  201. ff_vp9_default_kf_ymode_probs[*a][*l]);
  202. b->mode[3] =
  203. b->mode[2] =
  204. b->mode[1] = b->mode[0];
  205. // FIXME this can probably be optimized
  206. memset(a, b->mode[0], bwh_tab[0][b->bs][0]);
  207. memset(l, b->mode[0], bwh_tab[0][b->bs][1]);
  208. }
  209. b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  210. ff_vp9_default_kf_uvmode_probs[b->mode[3]]);
  211. } else if (b->intra) {
  212. b->comp = 0;
  213. if (b->bs > BS_8x8) {
  214. b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  215. s->prob.p.y_mode[0]);
  216. s->counts.y_mode[0][b->mode[0]]++;
  217. if (b->bs != BS_8x4) {
  218. b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  219. s->prob.p.y_mode[0]);
  220. s->counts.y_mode[0][b->mode[1]]++;
  221. } else {
  222. b->mode[1] = b->mode[0];
  223. }
  224. if (b->bs != BS_4x8) {
  225. b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  226. s->prob.p.y_mode[0]);
  227. s->counts.y_mode[0][b->mode[2]]++;
  228. if (b->bs != BS_8x4) {
  229. b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  230. s->prob.p.y_mode[0]);
  231. s->counts.y_mode[0][b->mode[3]]++;
  232. } else {
  233. b->mode[3] = b->mode[2];
  234. }
  235. } else {
  236. b->mode[2] = b->mode[0];
  237. b->mode[3] = b->mode[1];
  238. }
  239. } else {
  240. static const uint8_t size_group[10] = {
  241. 3, 3, 3, 3, 2, 2, 2, 1, 1, 1
  242. };
  243. int sz = size_group[b->bs];
  244. b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  245. s->prob.p.y_mode[sz]);
  246. b->mode[1] =
  247. b->mode[2] =
  248. b->mode[3] = b->mode[0];
  249. s->counts.y_mode[sz][b->mode[3]]++;
  250. }
  251. b->uvmode = vp8_rac_get_tree(&s->c, ff_vp9_intramode_tree,
  252. s->prob.p.uv_mode[b->mode[3]]);
  253. s->counts.uv_mode[b->mode[3]][b->uvmode]++;
  254. } else {
  255. static const uint8_t inter_mode_ctx_lut[14][14] = {
  256. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  257. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  258. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  259. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  260. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  261. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  262. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  263. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  264. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  265. { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5 },
  266. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  267. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 1, 3 },
  268. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 3 },
  269. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 4 },
  270. };
  271. if (s->segmentation.feat[b->seg_id].ref_enabled) {
  272. av_assert2(s->segmentation.feat[b->seg_id].ref_val != 0);
  273. b->comp = 0;
  274. b->ref[0] = s->segmentation.feat[b->seg_id].ref_val - 1;
  275. } else {
  276. // read comp_pred flag
  277. if (s->comppredmode != PRED_SWITCHABLE) {
  278. b->comp = s->comppredmode == PRED_COMPREF;
  279. } else {
  280. int c;
  281. // FIXME add intra as ref=0xff (or -1) to make these easier?
  282. if (have_a) {
  283. if (have_l) {
  284. if (s->above_comp_ctx[col] && s->left_comp_ctx[row7]) {
  285. c = 4;
  286. } else if (s->above_comp_ctx[col]) {
  287. c = 2 + (s->left_intra_ctx[row7] ||
  288. s->left_ref_ctx[row7] == s->fixcompref);
  289. } else if (s->left_comp_ctx[row7]) {
  290. c = 2 + (s->above_intra_ctx[col] ||
  291. s->above_ref_ctx[col] == s->fixcompref);
  292. } else {
  293. c = (!s->above_intra_ctx[col] &&
  294. s->above_ref_ctx[col] == s->fixcompref) ^
  295. (!s->left_intra_ctx[row7] &&
  296. s->left_ref_ctx[row & 7] == s->fixcompref);
  297. }
  298. } else {
  299. c = s->above_comp_ctx[col] ? 3 :
  300. (!s->above_intra_ctx[col] && s->above_ref_ctx[col] == s->fixcompref);
  301. }
  302. } else if (have_l) {
  303. c = s->left_comp_ctx[row7] ? 3 :
  304. (!s->left_intra_ctx[row7] && s->left_ref_ctx[row7] == s->fixcompref);
  305. } else {
  306. c = 1;
  307. }
  308. b->comp = vp56_rac_get_prob(&s->c, s->prob.p.comp[c]);
  309. s->counts.comp[c][b->comp]++;
  310. }
  311. // read actual references
  312. // FIXME probably cache a few variables here to prevent repetitive
  313. // memory accesses below
  314. if (b->comp) { /* two references */
  315. int fix_idx = s->signbias[s->fixcompref], var_idx = !fix_idx, c, bit;
  316. b->ref[fix_idx] = s->fixcompref;
  317. // FIXME can this codeblob be replaced by some sort of LUT?
  318. if (have_a) {
  319. if (have_l) {
  320. if (s->above_intra_ctx[col]) {
  321. if (s->left_intra_ctx[row7]) {
  322. c = 2;
  323. } else {
  324. c = 1 + 2 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  325. }
  326. } else if (s->left_intra_ctx[row7]) {
  327. c = 1 + 2 * (s->above_ref_ctx[col] != s->varcompref[1]);
  328. } else {
  329. int refl = s->left_ref_ctx[row7], refa = s->above_ref_ctx[col];
  330. if (refl == refa && refa == s->varcompref[1]) {
  331. c = 0;
  332. } else if (!s->left_comp_ctx[row7] && !s->above_comp_ctx[col]) {
  333. if ((refa == s->fixcompref && refl == s->varcompref[0]) ||
  334. (refl == s->fixcompref && refa == s->varcompref[0])) {
  335. c = 4;
  336. } else {
  337. c = (refa == refl) ? 3 : 1;
  338. }
  339. } else if (!s->left_comp_ctx[row7]) {
  340. if (refa == s->varcompref[1] && refl != s->varcompref[1]) {
  341. c = 1;
  342. } else {
  343. c = (refl == s->varcompref[1] &&
  344. refa != s->varcompref[1]) ? 2 : 4;
  345. }
  346. } else if (!s->above_comp_ctx[col]) {
  347. if (refl == s->varcompref[1] && refa != s->varcompref[1]) {
  348. c = 1;
  349. } else {
  350. c = (refa == s->varcompref[1] &&
  351. refl != s->varcompref[1]) ? 2 : 4;
  352. }
  353. } else {
  354. c = (refl == refa) ? 4 : 2;
  355. }
  356. }
  357. } else {
  358. if (s->above_intra_ctx[col]) {
  359. c = 2;
  360. } else if (s->above_comp_ctx[col]) {
  361. c = 4 * (s->above_ref_ctx[col] != s->varcompref[1]);
  362. } else {
  363. c = 3 * (s->above_ref_ctx[col] != s->varcompref[1]);
  364. }
  365. }
  366. } else if (have_l) {
  367. if (s->left_intra_ctx[row7]) {
  368. c = 2;
  369. } else if (s->left_comp_ctx[row7]) {
  370. c = 4 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  371. } else {
  372. c = 3 * (s->left_ref_ctx[row7] != s->varcompref[1]);
  373. }
  374. } else {
  375. c = 2;
  376. }
  377. bit = vp56_rac_get_prob(&s->c, s->prob.p.comp_ref[c]);
  378. b->ref[var_idx] = s->varcompref[bit];
  379. s->counts.comp_ref[c][bit]++;
  380. } else { /* single reference */
  381. int bit, c;
  382. if (have_a && !s->above_intra_ctx[col]) {
  383. if (have_l && !s->left_intra_ctx[row7]) {
  384. if (s->left_comp_ctx[row7]) {
  385. if (s->above_comp_ctx[col]) {
  386. c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7] ||
  387. !s->above_ref_ctx[col]);
  388. } else {
  389. c = (3 * !s->above_ref_ctx[col]) +
  390. (!s->fixcompref || !s->left_ref_ctx[row7]);
  391. }
  392. } else if (s->above_comp_ctx[col]) {
  393. c = (3 * !s->left_ref_ctx[row7]) +
  394. (!s->fixcompref || !s->above_ref_ctx[col]);
  395. } else {
  396. c = 2 * !s->left_ref_ctx[row7] + 2 * !s->above_ref_ctx[col];
  397. }
  398. } else if (s->above_intra_ctx[col]) {
  399. c = 2;
  400. } else if (s->above_comp_ctx[col]) {
  401. c = 1 + (!s->fixcompref || !s->above_ref_ctx[col]);
  402. } else {
  403. c = 4 * (!s->above_ref_ctx[col]);
  404. }
  405. } else if (have_l && !s->left_intra_ctx[row7]) {
  406. if (s->left_intra_ctx[row7]) {
  407. c = 2;
  408. } else if (s->left_comp_ctx[row7]) {
  409. c = 1 + (!s->fixcompref || !s->left_ref_ctx[row7]);
  410. } else {
  411. c = 4 * (!s->left_ref_ctx[row7]);
  412. }
  413. } else {
  414. c = 2;
  415. }
  416. bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][0]);
  417. s->counts.single_ref[c][0][bit]++;
  418. if (!bit) {
  419. b->ref[0] = 0;
  420. } else {
  421. // FIXME can this codeblob be replaced by some sort of LUT?
  422. if (have_a) {
  423. if (have_l) {
  424. if (s->left_intra_ctx[row7]) {
  425. if (s->above_intra_ctx[col]) {
  426. c = 2;
  427. } else if (s->above_comp_ctx[col]) {
  428. c = 1 + 2 * (s->fixcompref == 1 ||
  429. s->above_ref_ctx[col] == 1);
  430. } else if (!s->above_ref_ctx[col]) {
  431. c = 3;
  432. } else {
  433. c = 4 * (s->above_ref_ctx[col] == 1);
  434. }
  435. } else if (s->above_intra_ctx[col]) {
  436. if (s->left_intra_ctx[row7]) {
  437. c = 2;
  438. } else if (s->left_comp_ctx[row7]) {
  439. c = 1 + 2 * (s->fixcompref == 1 ||
  440. s->left_ref_ctx[row7] == 1);
  441. } else if (!s->left_ref_ctx[row7]) {
  442. c = 3;
  443. } else {
  444. c = 4 * (s->left_ref_ctx[row7] == 1);
  445. }
  446. } else if (s->above_comp_ctx[col]) {
  447. if (s->left_comp_ctx[row7]) {
  448. if (s->left_ref_ctx[row7] == s->above_ref_ctx[col]) {
  449. c = 3 * (s->fixcompref == 1 ||
  450. s->left_ref_ctx[row7] == 1);
  451. } else {
  452. c = 2;
  453. }
  454. } else if (!s->left_ref_ctx[row7]) {
  455. c = 1 + 2 * (s->fixcompref == 1 ||
  456. s->above_ref_ctx[col] == 1);
  457. } else {
  458. c = 3 * (s->left_ref_ctx[row7] == 1) +
  459. (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
  460. }
  461. } else if (s->left_comp_ctx[row7]) {
  462. if (!s->above_ref_ctx[col]) {
  463. c = 1 + 2 * (s->fixcompref == 1 ||
  464. s->left_ref_ctx[row7] == 1);
  465. } else {
  466. c = 3 * (s->above_ref_ctx[col] == 1) +
  467. (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
  468. }
  469. } else if (!s->above_ref_ctx[col]) {
  470. if (!s->left_ref_ctx[row7]) {
  471. c = 3;
  472. } else {
  473. c = 4 * (s->left_ref_ctx[row7] == 1);
  474. }
  475. } else if (!s->left_ref_ctx[row7]) {
  476. c = 4 * (s->above_ref_ctx[col] == 1);
  477. } else {
  478. c = 2 * (s->left_ref_ctx[row7] == 1) +
  479. 2 * (s->above_ref_ctx[col] == 1);
  480. }
  481. } else {
  482. if (s->above_intra_ctx[col] ||
  483. (!s->above_comp_ctx[col] && !s->above_ref_ctx[col])) {
  484. c = 2;
  485. } else if (s->above_comp_ctx[col]) {
  486. c = 3 * (s->fixcompref == 1 || s->above_ref_ctx[col] == 1);
  487. } else {
  488. c = 4 * (s->above_ref_ctx[col] == 1);
  489. }
  490. }
  491. } else if (have_l) {
  492. if (s->left_intra_ctx[row7] ||
  493. (!s->left_comp_ctx[row7] && !s->left_ref_ctx[row7])) {
  494. c = 2;
  495. } else if (s->left_comp_ctx[row7]) {
  496. c = 3 * (s->fixcompref == 1 || s->left_ref_ctx[row7] == 1);
  497. } else {
  498. c = 4 * (s->left_ref_ctx[row7] == 1);
  499. }
  500. } else {
  501. c = 2;
  502. }
  503. bit = vp56_rac_get_prob(&s->c, s->prob.p.single_ref[c][1]);
  504. s->counts.single_ref[c][1][bit]++;
  505. b->ref[0] = 1 + bit;
  506. }
  507. }
  508. }
  509. if (b->bs <= BS_8x8) {
  510. if (s->segmentation.feat[b->seg_id].skip_enabled) {
  511. b->mode[0] =
  512. b->mode[1] =
  513. b->mode[2] =
  514. b->mode[3] = ZEROMV;
  515. } else {
  516. static const uint8_t off[10] = {
  517. 3, 0, 0, 1, 0, 0, 0, 0, 0, 0
  518. };
  519. // FIXME this needs to use the LUT tables from find_ref_mvs
  520. // because not all are -1,0/0,-1
  521. int c = inter_mode_ctx_lut[s->above_mode_ctx[col + off[b->bs]]]
  522. [s->left_mode_ctx[row7 + off[b->bs]]];
  523. b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
  524. s->prob.p.mv_mode[c]);
  525. b->mode[1] =
  526. b->mode[2] =
  527. b->mode[3] = b->mode[0];
  528. s->counts.mv_mode[c][b->mode[0] - 10]++;
  529. }
  530. }
  531. if (s->filtermode == FILTER_SWITCHABLE) {
  532. int c;
  533. if (have_a && s->above_mode_ctx[col] >= NEARESTMV) {
  534. if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
  535. c = s->above_filter_ctx[col] == s->left_filter_ctx[row7] ?
  536. s->left_filter_ctx[row7] : 3;
  537. } else {
  538. c = s->above_filter_ctx[col];
  539. }
  540. } else if (have_l && s->left_mode_ctx[row7] >= NEARESTMV) {
  541. c = s->left_filter_ctx[row7];
  542. } else {
  543. c = 3;
  544. }
  545. b->filter = vp8_rac_get_tree(&s->c, ff_vp9_filter_tree,
  546. s->prob.p.filter[c]);
  547. s->counts.filter[c][b->filter]++;
  548. } else {
  549. b->filter = s->filtermode;
  550. }
  551. if (b->bs > BS_8x8) {
  552. int c = inter_mode_ctx_lut[s->above_mode_ctx[col]][s->left_mode_ctx[row7]];
  553. b->mode[0] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
  554. s->prob.p.mv_mode[c]);
  555. s->counts.mv_mode[c][b->mode[0] - 10]++;
  556. ff_vp9_fill_mv(s, b->mv[0], b->mode[0], 0);
  557. if (b->bs != BS_8x4) {
  558. b->mode[1] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
  559. s->prob.p.mv_mode[c]);
  560. s->counts.mv_mode[c][b->mode[1] - 10]++;
  561. ff_vp9_fill_mv(s, b->mv[1], b->mode[1], 1);
  562. } else {
  563. b->mode[1] = b->mode[0];
  564. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  565. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  566. }
  567. if (b->bs != BS_4x8) {
  568. b->mode[2] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
  569. s->prob.p.mv_mode[c]);
  570. s->counts.mv_mode[c][b->mode[2] - 10]++;
  571. ff_vp9_fill_mv(s, b->mv[2], b->mode[2], 2);
  572. if (b->bs != BS_8x4) {
  573. b->mode[3] = vp8_rac_get_tree(&s->c, ff_vp9_inter_mode_tree,
  574. s->prob.p.mv_mode[c]);
  575. s->counts.mv_mode[c][b->mode[3] - 10]++;
  576. ff_vp9_fill_mv(s, b->mv[3], b->mode[3], 3);
  577. } else {
  578. b->mode[3] = b->mode[2];
  579. AV_COPY32(&b->mv[3][0], &b->mv[2][0]);
  580. AV_COPY32(&b->mv[3][1], &b->mv[2][1]);
  581. }
  582. } else {
  583. b->mode[2] = b->mode[0];
  584. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  585. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  586. b->mode[3] = b->mode[1];
  587. AV_COPY32(&b->mv[3][0], &b->mv[1][0]);
  588. AV_COPY32(&b->mv[3][1], &b->mv[1][1]);
  589. }
  590. } else {
  591. ff_vp9_fill_mv(s, b->mv[0], b->mode[0], -1);
  592. AV_COPY32(&b->mv[1][0], &b->mv[0][0]);
  593. AV_COPY32(&b->mv[2][0], &b->mv[0][0]);
  594. AV_COPY32(&b->mv[3][0], &b->mv[0][0]);
  595. AV_COPY32(&b->mv[1][1], &b->mv[0][1]);
  596. AV_COPY32(&b->mv[2][1], &b->mv[0][1]);
  597. AV_COPY32(&b->mv[3][1], &b->mv[0][1]);
  598. }
  599. }
  600. // FIXME this can probably be optimized
  601. memset(&s->above_skip_ctx[col], b->skip, w4);
  602. memset(&s->left_skip_ctx[row7], b->skip, h4);
  603. memset(&s->above_txfm_ctx[col], b->tx, w4);
  604. memset(&s->left_txfm_ctx[row7], b->tx, h4);
  605. memset(&s->above_partition_ctx[col], above_ctx[b->bs], w4);
  606. memset(&s->left_partition_ctx[row7], left_ctx[b->bs], h4);
  607. if (!s->keyframe && !s->intraonly) {
  608. memset(&s->above_intra_ctx[col], b->intra, w4);
  609. memset(&s->left_intra_ctx[row7], b->intra, h4);
  610. memset(&s->above_comp_ctx[col], b->comp, w4);
  611. memset(&s->left_comp_ctx[row7], b->comp, h4);
  612. memset(&s->above_mode_ctx[col], b->mode[3], w4);
  613. memset(&s->left_mode_ctx[row7], b->mode[3], h4);
  614. if (s->filtermode == FILTER_SWITCHABLE && !b->intra) {
  615. memset(&s->above_filter_ctx[col], b->filter, w4);
  616. memset(&s->left_filter_ctx[row7], b->filter, h4);
  617. b->filter = ff_vp9_filter_lut[b->filter];
  618. }
  619. if (b->bs > BS_8x8) {
  620. int mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  621. AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][0], &b->mv[1][0]);
  622. AV_COPY32(&s->left_mv_ctx[row7 * 2 + 0][1], &b->mv[1][1]);
  623. AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][0], mv0);
  624. AV_WN32A(&s->left_mv_ctx[row7 * 2 + 1][1], mv1);
  625. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][0], &b->mv[2][0]);
  626. AV_COPY32(&s->above_mv_ctx[col * 2 + 0][1], &b->mv[2][1]);
  627. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][0], mv0);
  628. AV_WN32A(&s->above_mv_ctx[col * 2 + 1][1], mv1);
  629. } else {
  630. int n, mv0 = AV_RN32A(&b->mv[3][0]), mv1 = AV_RN32A(&b->mv[3][1]);
  631. for (n = 0; n < w4 * 2; n++) {
  632. AV_WN32A(&s->above_mv_ctx[col * 2 + n][0], mv0);
  633. AV_WN32A(&s->above_mv_ctx[col * 2 + n][1], mv1);
  634. }
  635. for (n = 0; n < h4 * 2; n++) {
  636. AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][0], mv0);
  637. AV_WN32A(&s->left_mv_ctx[row7 * 2 + n][1], mv1);
  638. }
  639. }
  640. if (!b->intra) { // FIXME write 0xff or -1 if intra, so we can use this
  641. // as a direct check in above branches
  642. int vref = b->ref[b->comp ? s->signbias[s->varcompref[0]] : 0];
  643. memset(&s->above_ref_ctx[col], vref, w4);
  644. memset(&s->left_ref_ctx[row7], vref, h4);
  645. }
  646. }
  647. // FIXME kinda ugly
  648. for (y = 0; y < h4; y++) {
  649. int x, o = (row + y) * s->sb_cols * 8 + col;
  650. if (b->intra) {
  651. for (x = 0; x < w4; x++) {
  652. s->mv[0][o + x].ref[0] =
  653. s->mv[0][o + x].ref[1] = -1;
  654. }
  655. } else if (b->comp) {
  656. for (x = 0; x < w4; x++) {
  657. s->mv[0][o + x].ref[0] = b->ref[0];
  658. s->mv[0][o + x].ref[1] = b->ref[1];
  659. AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
  660. AV_COPY32(&s->mv[0][o + x].mv[1], &b->mv[3][1]);
  661. }
  662. } else {
  663. for (x = 0; x < w4; x++) {
  664. s->mv[0][o + x].ref[0] = b->ref[0];
  665. s->mv[0][o + x].ref[1] = -1;
  666. AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
  667. }
  668. }
  669. }
  670. }
  671. // FIXME remove tx argument, and merge cnt/eob arguments?
  672. static int decode_block_coeffs(VP56RangeCoder *c, int16_t *coef, int n_coeffs,
  673. enum TxfmMode tx, unsigned (*cnt)[6][3],
  674. unsigned (*eob)[6][2], uint8_t(*p)[6][11],
  675. int nnz, const int16_t *scan,
  676. const int16_t(*nb)[2],
  677. const int16_t *band_counts, const int16_t *qmul)
  678. {
  679. int i = 0, band = 0, band_left = band_counts[band];
  680. uint8_t *tp = p[0][nnz];
  681. uint8_t cache[1024];
  682. do {
  683. int val, rc;
  684. val = vp56_rac_get_prob_branchy(c, tp[0]); // eob
  685. eob[band][nnz][val]++;
  686. if (!val)
  687. break;
  688. skip_eob:
  689. if (!vp56_rac_get_prob_branchy(c, tp[1])) { // zero
  690. cnt[band][nnz][0]++;
  691. if (!--band_left)
  692. band_left = band_counts[++band];
  693. cache[scan[i]] = 0;
  694. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  695. tp = p[band][nnz];
  696. if (++i == n_coeffs)
  697. break; //invalid input; blocks should end with EOB
  698. goto skip_eob;
  699. }
  700. rc = scan[i];
  701. if (!vp56_rac_get_prob_branchy(c, tp[2])) { // one
  702. cnt[band][nnz][1]++;
  703. val = 1;
  704. cache[rc] = 1;
  705. } else {
  706. // fill in p[3-10] (model fill) - only once per frame for each pos
  707. if (!tp[3])
  708. memcpy(&tp[3], ff_vp9_model_pareto8[tp[2]], 8);
  709. cnt[band][nnz][2]++;
  710. if (!vp56_rac_get_prob_branchy(c, tp[3])) { // 2, 3, 4
  711. if (!vp56_rac_get_prob_branchy(c, tp[4])) {
  712. cache[rc] = val = 2;
  713. } else {
  714. val = 3 + vp56_rac_get_prob(c, tp[5]);
  715. cache[rc] = 3;
  716. }
  717. } else if (!vp56_rac_get_prob_branchy(c, tp[6])) { // cat1/2
  718. cache[rc] = 4;
  719. if (!vp56_rac_get_prob_branchy(c, tp[7])) {
  720. val = vp56_rac_get_prob(c, 159) + 5;
  721. } else {
  722. val = (vp56_rac_get_prob(c, 165) << 1) + 7;
  723. val += vp56_rac_get_prob(c, 145);
  724. }
  725. } else { // cat 3-6
  726. cache[rc] = 5;
  727. if (!vp56_rac_get_prob_branchy(c, tp[8])) {
  728. if (!vp56_rac_get_prob_branchy(c, tp[9])) {
  729. val = (vp56_rac_get_prob(c, 173) << 2) + 11;
  730. val += (vp56_rac_get_prob(c, 148) << 1);
  731. val += vp56_rac_get_prob(c, 140);
  732. } else {
  733. val = (vp56_rac_get_prob(c, 176) << 3) + 19;
  734. val += (vp56_rac_get_prob(c, 155) << 2);
  735. val += (vp56_rac_get_prob(c, 140) << 1);
  736. val += vp56_rac_get_prob(c, 135);
  737. }
  738. } else if (!vp56_rac_get_prob_branchy(c, tp[10])) {
  739. val = (vp56_rac_get_prob(c, 180) << 4) + 35;
  740. val += (vp56_rac_get_prob(c, 157) << 3);
  741. val += (vp56_rac_get_prob(c, 141) << 2);
  742. val += (vp56_rac_get_prob(c, 134) << 1);
  743. val += vp56_rac_get_prob(c, 130);
  744. } else {
  745. val = (vp56_rac_get_prob(c, 254) << 13) + 67;
  746. val += (vp56_rac_get_prob(c, 254) << 12);
  747. val += (vp56_rac_get_prob(c, 254) << 11);
  748. val += (vp56_rac_get_prob(c, 252) << 10);
  749. val += (vp56_rac_get_prob(c, 249) << 9);
  750. val += (vp56_rac_get_prob(c, 243) << 8);
  751. val += (vp56_rac_get_prob(c, 230) << 7);
  752. val += (vp56_rac_get_prob(c, 196) << 6);
  753. val += (vp56_rac_get_prob(c, 177) << 5);
  754. val += (vp56_rac_get_prob(c, 153) << 4);
  755. val += (vp56_rac_get_prob(c, 140) << 3);
  756. val += (vp56_rac_get_prob(c, 133) << 2);
  757. val += (vp56_rac_get_prob(c, 130) << 1);
  758. val += vp56_rac_get_prob(c, 129);
  759. }
  760. }
  761. }
  762. if (!--band_left)
  763. band_left = band_counts[++band];
  764. if (tx == TX_32X32) // FIXME slow
  765. coef[rc] = ((vp8_rac_get(c) ? -val : val) * qmul[!!i]) / 2;
  766. else
  767. coef[rc] = (vp8_rac_get(c) ? -val : val) * qmul[!!i];
  768. nnz = (1 + cache[nb[i][0]] + cache[nb[i][1]]) >> 1;
  769. tp = p[band][nnz];
  770. } while (++i < n_coeffs);
  771. return i;
  772. }
  773. static int decode_coeffs(AVCodecContext *avctx)
  774. {
  775. VP9Context *s = avctx->priv_data;
  776. VP9Block *const b = &s->b;
  777. int row = b->row, col = b->col;
  778. uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
  779. unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
  780. unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
  781. int w4 = bwh_tab[1][b->bs][0] << 1, h4 = bwh_tab[1][b->bs][1] << 1;
  782. int end_x = FFMIN(2 * (s->cols - col), w4);
  783. int end_y = FFMIN(2 * (s->rows - row), h4);
  784. int n, pl, x, y, step1d = 1 << b->tx, step = 1 << (b->tx * 2);
  785. int uvstep1d = 1 << b->uvtx, uvstep = 1 << (b->uvtx * 2), ret;
  786. int16_t (*qmul)[2] = s->segmentation.feat[b->seg_id].qmul;
  787. int tx = 4 * s->lossless + b->tx;
  788. const int16_t **yscans = ff_vp9_scans[tx];
  789. const int16_t (**ynbs)[2] = ff_vp9_scans_nb[tx];
  790. const int16_t *uvscan = ff_vp9_scans[b->uvtx][DCT_DCT];
  791. const int16_t (*uvnb)[2] = ff_vp9_scans_nb[b->uvtx][DCT_DCT];
  792. uint8_t *a = &s->above_y_nnz_ctx[col * 2];
  793. uint8_t *l = &s->left_y_nnz_ctx[(row & 7) << 1];
  794. static const int16_t band_counts[4][8] = {
  795. { 1, 2, 3, 4, 3, 16 - 13, 0 },
  796. { 1, 2, 3, 4, 11, 64 - 21, 0 },
  797. { 1, 2, 3, 4, 11, 256 - 21, 0 },
  798. { 1, 2, 3, 4, 11, 1024 - 21, 0 },
  799. };
  800. const int16_t *y_band_counts = band_counts[b->tx];
  801. const int16_t *uv_band_counts = band_counts[b->uvtx];
  802. /* y tokens */
  803. if (b->tx > TX_4X4) { // FIXME slow
  804. for (y = 0; y < end_y; y += step1d)
  805. for (x = 1; x < step1d; x++)
  806. l[y] |= l[y + x];
  807. for (x = 0; x < end_x; x += step1d)
  808. for (y = 1; y < step1d; y++)
  809. a[x] |= a[x + y];
  810. }
  811. for (n = 0, y = 0; y < end_y; y += step1d) {
  812. for (x = 0; x < end_x; x += step1d, n += step) {
  813. enum TxfmType txtp = ff_vp9_intra_txfm_type[b->mode[b->tx == TX_4X4 &&
  814. b->bs > BS_8x8 ?
  815. n : 0]];
  816. int nnz = a[x] + l[y];
  817. if ((ret = decode_block_coeffs(&s->c, s->block + 16 * n, 16 * step,
  818. b->tx, c, e, p, nnz, yscans[txtp],
  819. ynbs[txtp], y_band_counts,
  820. qmul[0])) < 0)
  821. return ret;
  822. a[x] = l[y] = !!ret;
  823. if (b->tx > TX_8X8)
  824. AV_WN16A(&s->eob[n], ret);
  825. else
  826. s->eob[n] = ret;
  827. }
  828. }
  829. if (b->tx > TX_4X4) { // FIXME slow
  830. for (y = 0; y < end_y; y += step1d)
  831. memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, step1d - 1));
  832. for (x = 0; x < end_x; x += step1d)
  833. memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, step1d - 1));
  834. }
  835. p = s->prob.coef[b->uvtx][1 /* uv */][!b->intra];
  836. c = s->counts.coef[b->uvtx][1 /* uv */][!b->intra];
  837. e = s->counts.eob[b->uvtx][1 /* uv */][!b->intra];
  838. w4 >>= 1;
  839. h4 >>= 1;
  840. end_x >>= 1;
  841. end_y >>= 1;
  842. for (pl = 0; pl < 2; pl++) {
  843. a = &s->above_uv_nnz_ctx[pl][col];
  844. l = &s->left_uv_nnz_ctx[pl][row & 7];
  845. if (b->uvtx > TX_4X4) { // FIXME slow
  846. for (y = 0; y < end_y; y += uvstep1d)
  847. for (x = 1; x < uvstep1d; x++)
  848. l[y] |= l[y + x];
  849. for (x = 0; x < end_x; x += uvstep1d)
  850. for (y = 1; y < uvstep1d; y++)
  851. a[x] |= a[x + y];
  852. }
  853. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  854. for (x = 0; x < end_x; x += uvstep1d, n += uvstep) {
  855. int nnz = a[x] + l[y];
  856. if ((ret = decode_block_coeffs(&s->c, s->uvblock[pl] + 16 * n,
  857. 16 * uvstep, b->uvtx, c, e, p,
  858. nnz, uvscan, uvnb,
  859. uv_band_counts, qmul[1])) < 0)
  860. return ret;
  861. a[x] = l[y] = !!ret;
  862. if (b->uvtx > TX_8X8)
  863. AV_WN16A(&s->uveob[pl][n], ret);
  864. else
  865. s->uveob[pl][n] = ret;
  866. }
  867. }
  868. if (b->uvtx > TX_4X4) { // FIXME slow
  869. for (y = 0; y < end_y; y += uvstep1d)
  870. memset(&l[y + 1], l[y], FFMIN(end_y - y - 1, uvstep1d - 1));
  871. for (x = 0; x < end_x; x += uvstep1d)
  872. memset(&a[x + 1], a[x], FFMIN(end_x - x - 1, uvstep1d - 1));
  873. }
  874. }
  875. return 0;
  876. }
  877. static av_always_inline int check_intra_mode(VP9Context *s, int mode,
  878. uint8_t **a,
  879. uint8_t *dst_edge,
  880. ptrdiff_t stride_edge,
  881. uint8_t *dst_inner,
  882. ptrdiff_t stride_inner,
  883. uint8_t *l, int col, int x, int w,
  884. int row, int y, enum TxfmMode tx,
  885. int p)
  886. {
  887. int have_top = row > 0 || y > 0;
  888. int have_left = col > s->tiling.tile_col_start || x > 0;
  889. int have_right = x < w - 1;
  890. static const uint8_t mode_conv[10][2 /* have_left */][2 /* have_top */] = {
  891. [VERT_PRED] = { { DC_127_PRED, VERT_PRED },
  892. { DC_127_PRED, VERT_PRED } },
  893. [HOR_PRED] = { { DC_129_PRED, DC_129_PRED },
  894. { HOR_PRED, HOR_PRED } },
  895. [DC_PRED] = { { DC_128_PRED, TOP_DC_PRED },
  896. { LEFT_DC_PRED, DC_PRED } },
  897. [DIAG_DOWN_LEFT_PRED] = { { DC_127_PRED, DIAG_DOWN_LEFT_PRED },
  898. { DC_127_PRED, DIAG_DOWN_LEFT_PRED } },
  899. [DIAG_DOWN_RIGHT_PRED] = { { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED },
  900. { DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_RIGHT_PRED } },
  901. [VERT_RIGHT_PRED] = { { VERT_RIGHT_PRED, VERT_RIGHT_PRED },
  902. { VERT_RIGHT_PRED, VERT_RIGHT_PRED } },
  903. [HOR_DOWN_PRED] = { { HOR_DOWN_PRED, HOR_DOWN_PRED },
  904. { HOR_DOWN_PRED, HOR_DOWN_PRED } },
  905. [VERT_LEFT_PRED] = { { DC_127_PRED, VERT_LEFT_PRED },
  906. { DC_127_PRED, VERT_LEFT_PRED } },
  907. [HOR_UP_PRED] = { { DC_129_PRED, DC_129_PRED },
  908. { HOR_UP_PRED, HOR_UP_PRED } },
  909. [TM_VP8_PRED] = { { DC_129_PRED, VERT_PRED },
  910. { HOR_PRED, TM_VP8_PRED } },
  911. };
  912. static const struct {
  913. uint8_t needs_left:1;
  914. uint8_t needs_top:1;
  915. uint8_t needs_topleft:1;
  916. uint8_t needs_topright:1;
  917. } edges[N_INTRA_PRED_MODES] = {
  918. [VERT_PRED] = { .needs_top = 1 },
  919. [HOR_PRED] = { .needs_left = 1 },
  920. [DC_PRED] = { .needs_top = 1, .needs_left = 1 },
  921. [DIAG_DOWN_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
  922. [DIAG_DOWN_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
  923. .needs_topleft = 1 },
  924. [VERT_RIGHT_PRED] = { .needs_left = 1, .needs_top = 1,
  925. .needs_topleft = 1 },
  926. [HOR_DOWN_PRED] = { .needs_left = 1, .needs_top = 1,
  927. .needs_topleft = 1 },
  928. [VERT_LEFT_PRED] = { .needs_top = 1, .needs_topright = 1 },
  929. [HOR_UP_PRED] = { .needs_left = 1 },
  930. [TM_VP8_PRED] = { .needs_left = 1, .needs_top = 1,
  931. .needs_topleft = 1 },
  932. [LEFT_DC_PRED] = { .needs_left = 1 },
  933. [TOP_DC_PRED] = { .needs_top = 1 },
  934. [DC_128_PRED] = { 0 },
  935. [DC_127_PRED] = { 0 },
  936. [DC_129_PRED] = { 0 }
  937. };
  938. av_assert2(mode >= 0 && mode < 10);
  939. mode = mode_conv[mode][have_left][have_top];
  940. if (edges[mode].needs_top) {
  941. uint8_t *top = NULL, *topleft = NULL;
  942. int n_px_need = 4 << tx, n_px_have = (((s->cols - col) << !p) - x) * 4;
  943. int n_px_need_tr = 0;
  944. if (tx == TX_4X4 && edges[mode].needs_topright && have_right)
  945. n_px_need_tr = 4;
  946. // if top of sb64-row, use s->intra_pred_data[] instead of
  947. // dst[-stride] for intra prediction (it contains pre- instead of
  948. // post-loopfilter data)
  949. if (have_top) {
  950. top = !(row & 7) && !y ?
  951. s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
  952. y == 0 ? &dst_edge[-stride_edge] : &dst_inner[-stride_inner];
  953. if (have_left)
  954. topleft = !(row & 7) && !y ?
  955. s->intra_pred_data[p] + col * (8 >> !!p) + x * 4 :
  956. y == 0 || x == 0 ? &dst_edge[-stride_edge] :
  957. &dst_inner[-stride_inner];
  958. }
  959. if (have_top &&
  960. (!edges[mode].needs_topleft || (have_left && top == topleft)) &&
  961. (tx != TX_4X4 || !edges[mode].needs_topright || have_right) &&
  962. n_px_need + n_px_need_tr <= n_px_have) {
  963. *a = top;
  964. } else {
  965. if (have_top) {
  966. if (n_px_need <= n_px_have) {
  967. memcpy(*a, top, n_px_need);
  968. } else {
  969. memcpy(*a, top, n_px_have);
  970. memset(&(*a)[n_px_have], (*a)[n_px_have - 1],
  971. n_px_need - n_px_have);
  972. }
  973. } else {
  974. memset(*a, 127, n_px_need);
  975. }
  976. if (edges[mode].needs_topleft) {
  977. if (have_left && have_top)
  978. (*a)[-1] = topleft[-1];
  979. else
  980. (*a)[-1] = have_top ? 129 : 127;
  981. }
  982. if (tx == TX_4X4 && edges[mode].needs_topright) {
  983. if (have_top && have_right &&
  984. n_px_need + n_px_need_tr <= n_px_have) {
  985. memcpy(&(*a)[4], &top[4], 4);
  986. } else {
  987. memset(&(*a)[4], (*a)[3], 4);
  988. }
  989. }
  990. }
  991. }
  992. if (edges[mode].needs_left) {
  993. if (have_left) {
  994. int i;
  995. int n_px_need = 4 << tx;
  996. int n_px_have = (((s->rows - row) << !p) - y) * 4;
  997. uint8_t *dst = x == 0 ? dst_edge : dst_inner;
  998. ptrdiff_t stride = x == 0 ? stride_edge : stride_inner;
  999. if (n_px_need <= n_px_have) {
  1000. for (i = 0; i < n_px_need; i++)
  1001. l[i] = dst[i * stride - 1];
  1002. } else {
  1003. for (i = 0; i < n_px_have; i++)
  1004. l[i] = dst[i * stride - 1];
  1005. memset(&l[i], l[i - 1], n_px_need - n_px_have);
  1006. }
  1007. } else {
  1008. memset(l, 129, 4 << tx);
  1009. }
  1010. }
  1011. return mode;
  1012. }
  1013. static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off)
  1014. {
  1015. VP9Context *s = avctx->priv_data;
  1016. VP9Block *const b = &s->b;
  1017. int row = b->row, col = b->col;
  1018. int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
  1019. int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
  1020. int end_x = FFMIN(2 * (s->cols - col), w4);
  1021. int end_y = FFMIN(2 * (s->rows - row), h4);
  1022. int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
  1023. int uvstep1d = 1 << b->uvtx, p;
  1024. uint8_t *dst = b->dst[0], *dst_r = s->cur_frame->data[0] + y_off;
  1025. for (n = 0, y = 0; y < end_y; y += step1d) {
  1026. uint8_t *ptr = dst, *ptr_r = dst_r;
  1027. for (x = 0; x < end_x;
  1028. x += step1d, ptr += 4 * step1d, ptr_r += 4 * step1d, n += step) {
  1029. int mode = b->mode[b->bs > BS_8x8 && b->tx == TX_4X4 ?
  1030. y * 2 + x : 0];
  1031. LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
  1032. uint8_t *a = &a_buf[16], l[32];
  1033. enum TxfmType txtp = ff_vp9_intra_txfm_type[mode];
  1034. int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
  1035. mode = check_intra_mode(s, mode, &a, ptr_r,
  1036. s->cur_frame->linesize[0],
  1037. ptr, b->y_stride, l,
  1038. col, x, w4, row, y, b->tx, 0);
  1039. s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
  1040. if (eob)
  1041. s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
  1042. s->block + 16 * n, eob);
  1043. }
  1044. dst_r += 4 * s->cur_frame->linesize[0] * step1d;
  1045. dst += 4 * b->y_stride * step1d;
  1046. }
  1047. // U/V
  1048. h4 >>= 1;
  1049. w4 >>= 1;
  1050. end_x >>= 1;
  1051. end_y >>= 1;
  1052. step = 1 << (b->uvtx * 2);
  1053. for (p = 0; p < 2; p++) {
  1054. dst = b->dst[1 + p];
  1055. dst_r = s->cur_frame->data[1 + p] + uv_off;
  1056. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  1057. uint8_t *ptr = dst, *ptr_r = dst_r;
  1058. for (x = 0; x < end_x;
  1059. x += uvstep1d, ptr += 4 * uvstep1d,
  1060. ptr_r += 4 * uvstep1d, n += step) {
  1061. int mode = b->uvmode;
  1062. LOCAL_ALIGNED_16(uint8_t, a_buf, [48]);
  1063. uint8_t *a = &a_buf[16], l[32];
  1064. int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
  1065. : s->uveob[p][n];
  1066. mode = check_intra_mode(s, mode, &a, ptr_r,
  1067. s->cur_frame->linesize[1],
  1068. ptr, b->uv_stride, l,
  1069. col, x, w4, row, y, b->uvtx, p + 1);
  1070. s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
  1071. if (eob)
  1072. s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
  1073. s->uvblock[p] + 16 * n,
  1074. eob);
  1075. }
  1076. dst_r += 4 * uvstep1d * s->cur_frame->linesize[1];
  1077. dst += 4 * uvstep1d * b->uv_stride;
  1078. }
  1079. }
  1080. }
  1081. static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
  1082. uint8_t *dst, ptrdiff_t dst_stride,
  1083. const uint8_t *ref,
  1084. ptrdiff_t ref_stride,
  1085. ptrdiff_t y, ptrdiff_t x,
  1086. const VP56mv *mv,
  1087. int bw, int bh, int w, int h)
  1088. {
  1089. int mx = mv->x, my = mv->y;
  1090. y += my >> 3;
  1091. x += mx >> 3;
  1092. ref += y * ref_stride + x;
  1093. mx &= 7;
  1094. my &= 7;
  1095. // FIXME bilinear filter only needs 0/1 pixels, not 3/4
  1096. if (x < !!mx * 3 || y < !!my * 3 ||
  1097. x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
  1098. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  1099. ref - !!my * 3 * ref_stride - !!mx * 3,
  1100. 80,
  1101. ref_stride,
  1102. bw + !!mx * 7, bh + !!my * 7,
  1103. x - !!mx * 3, y - !!my * 3, w, h);
  1104. ref = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  1105. ref_stride = 80;
  1106. }
  1107. mc[!!mx][!!my](dst, ref, dst_stride, ref_stride, bh, mx << 1, my << 1);
  1108. }
  1109. static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2],
  1110. uint8_t *dst_u, uint8_t *dst_v,
  1111. ptrdiff_t dst_stride,
  1112. const uint8_t *ref_u,
  1113. ptrdiff_t src_stride_u,
  1114. const uint8_t *ref_v,
  1115. ptrdiff_t src_stride_v,
  1116. ptrdiff_t y, ptrdiff_t x,
  1117. const VP56mv *mv,
  1118. int bw, int bh, int w, int h)
  1119. {
  1120. int mx = mv->x, my = mv->y;
  1121. y += my >> 4;
  1122. x += mx >> 4;
  1123. ref_u += y * src_stride_u + x;
  1124. ref_v += y * src_stride_v + x;
  1125. mx &= 15;
  1126. my &= 15;
  1127. // FIXME bilinear filter only needs 0/1 pixels, not 3/4
  1128. if (x < !!mx * 3 || y < !!my * 3 ||
  1129. x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) {
  1130. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  1131. ref_u - !!my * 3 * src_stride_u - !!mx * 3,
  1132. 80,
  1133. src_stride_u,
  1134. bw + !!mx * 7, bh + !!my * 7,
  1135. x - !!mx * 3, y - !!my * 3, w, h);
  1136. ref_u = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  1137. mc[!!mx][!!my](dst_u, ref_u, dst_stride, 80, bh, mx, my);
  1138. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  1139. ref_v - !!my * 3 * src_stride_v - !!mx * 3,
  1140. 80,
  1141. src_stride_v,
  1142. bw + !!mx * 7, bh + !!my * 7,
  1143. x - !!mx * 3, y - !!my * 3, w, h);
  1144. ref_v = s->edge_emu_buffer + !!my * 3 * 80 + !!mx * 3;
  1145. mc[!!mx][!!my](dst_v, ref_v, dst_stride, 80, bh, mx, my);
  1146. } else {
  1147. mc[!!mx][!!my](dst_u, ref_u, dst_stride, src_stride_u, bh, mx, my);
  1148. mc[!!mx][!!my](dst_v, ref_v, dst_stride, src_stride_v, bh, mx, my);
  1149. }
  1150. }
  1151. static int inter_recon(AVCodecContext *avctx)
  1152. {
  1153. static const uint8_t bwlog_tab[2][N_BS_SIZES] = {
  1154. { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
  1155. { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 },
  1156. };
  1157. VP9Context *s = avctx->priv_data;
  1158. VP9Block *const b = &s->b;
  1159. int row = b->row, col = b->col;
  1160. AVFrame *ref1 = s->refs[s->refidx[b->ref[0]]];
  1161. AVFrame *ref2 = b->comp ? s->refs[s->refidx[b->ref[1]]] : NULL;
  1162. int w = avctx->width, h = avctx->height;
  1163. ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
  1164. if (!ref1->data[0] || (b->comp && !ref2->data[0]))
  1165. return AVERROR_INVALIDDATA;
  1166. // y inter pred
  1167. if (b->bs > BS_8x8) {
  1168. if (b->bs == BS_8x4) {
  1169. mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
  1170. ref1->data[0], ref1->linesize[0],
  1171. row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
  1172. mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
  1173. b->dst[0] + 4 * ls_y, ls_y,
  1174. ref1->data[0], ref1->linesize[0],
  1175. (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
  1176. if (b->comp) {
  1177. mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
  1178. ref2->data[0], ref2->linesize[0],
  1179. row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
  1180. mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
  1181. b->dst[0] + 4 * ls_y, ls_y,
  1182. ref2->data[0], ref2->linesize[0],
  1183. (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
  1184. }
  1185. } else if (b->bs == BS_4x8) {
  1186. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
  1187. ref1->data[0], ref1->linesize[0],
  1188. row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
  1189. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
  1190. ref1->data[0], ref1->linesize[0],
  1191. row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
  1192. if (b->comp) {
  1193. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
  1194. ref2->data[0], ref2->linesize[0],
  1195. row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
  1196. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
  1197. ref2->data[0], ref2->linesize[0],
  1198. row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
  1199. }
  1200. } else {
  1201. av_assert2(b->bs == BS_4x4);
  1202. // FIXME if two horizontally adjacent blocks have the same MV,
  1203. // do a w8 instead of a w4 call
  1204. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
  1205. ref1->data[0], ref1->linesize[0],
  1206. row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
  1207. mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
  1208. ref1->data[0], ref1->linesize[0],
  1209. row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
  1210. mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
  1211. b->dst[0] + 4 * ls_y, ls_y,
  1212. ref1->data[0], ref1->linesize[0],
  1213. (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
  1214. mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
  1215. b->dst[0] + 4 * ls_y + 4, ls_y,
  1216. ref1->data[0], ref1->linesize[0],
  1217. (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
  1218. if (b->comp) {
  1219. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
  1220. ref2->data[0], ref2->linesize[0],
  1221. row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
  1222. mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
  1223. ref2->data[0], ref2->linesize[0],
  1224. row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
  1225. mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
  1226. b->dst[0] + 4 * ls_y, ls_y,
  1227. ref2->data[0], ref2->linesize[0],
  1228. (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
  1229. mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
  1230. b->dst[0] + 4 * ls_y + 4, ls_y,
  1231. ref2->data[0], ref2->linesize[0],
  1232. (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
  1233. }
  1234. }
  1235. } else {
  1236. int bwl = bwlog_tab[0][b->bs];
  1237. int bw = bwh_tab[0][b->bs][0] * 4;
  1238. int bh = bwh_tab[0][b->bs][1] * 4;
  1239. mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
  1240. ref1->data[0], ref1->linesize[0],
  1241. row << 3, col << 3, &b->mv[0][0], bw, bh, w, h);
  1242. if (b->comp)
  1243. mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
  1244. ref2->data[0], ref2->linesize[0],
  1245. row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
  1246. }
  1247. // uv inter pred
  1248. {
  1249. int bwl = bwlog_tab[1][b->bs];
  1250. int bw = bwh_tab[1][b->bs][0] * 4, bh = bwh_tab[1][b->bs][1] * 4;
  1251. VP56mv mvuv;
  1252. w = (w + 1) >> 1;
  1253. h = (h + 1) >> 1;
  1254. if (b->bs > BS_8x8) {
  1255. mvuv.x = ROUNDED_DIV(b->mv[0][0].x + b->mv[1][0].x +
  1256. b->mv[2][0].x + b->mv[3][0].x, 4);
  1257. mvuv.y = ROUNDED_DIV(b->mv[0][0].y + b->mv[1][0].y +
  1258. b->mv[2][0].y + b->mv[3][0].y, 4);
  1259. } else {
  1260. mvuv = b->mv[0][0];
  1261. }
  1262. mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
  1263. b->dst[1], b->dst[2], ls_uv,
  1264. ref1->data[1], ref1->linesize[1],
  1265. ref1->data[2], ref1->linesize[2],
  1266. row << 2, col << 2, &mvuv, bw, bh, w, h);
  1267. if (b->comp) {
  1268. if (b->bs > BS_8x8) {
  1269. mvuv.x = ROUNDED_DIV(b->mv[0][1].x + b->mv[1][1].x +
  1270. b->mv[2][1].x + b->mv[3][1].x, 4);
  1271. mvuv.y = ROUNDED_DIV(b->mv[0][1].y + b->mv[1][1].y +
  1272. b->mv[2][1].y + b->mv[3][1].y, 4);
  1273. } else {
  1274. mvuv = b->mv[0][1];
  1275. }
  1276. mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
  1277. b->dst[1], b->dst[2], ls_uv,
  1278. ref2->data[1], ref2->linesize[1],
  1279. ref2->data[2], ref2->linesize[2],
  1280. row << 2, col << 2, &mvuv, bw, bh, w, h);
  1281. }
  1282. }
  1283. if (!b->skip) {
  1284. /* mostly copied intra_reconn() */
  1285. int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
  1286. int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
  1287. int end_x = FFMIN(2 * (s->cols - col), w4);
  1288. int end_y = FFMIN(2 * (s->rows - row), h4);
  1289. int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
  1290. int uvstep1d = 1 << b->uvtx, p;
  1291. uint8_t *dst = b->dst[0];
  1292. // y itxfm add
  1293. for (n = 0, y = 0; y < end_y; y += step1d) {
  1294. uint8_t *ptr = dst;
  1295. for (x = 0; x < end_x; x += step1d, ptr += 4 * step1d, n += step) {
  1296. int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
  1297. if (eob)
  1298. s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
  1299. s->block + 16 * n, eob);
  1300. }
  1301. dst += 4 * b->y_stride * step1d;
  1302. }
  1303. // uv itxfm add
  1304. h4 >>= 1;
  1305. w4 >>= 1;
  1306. end_x >>= 1;
  1307. end_y >>= 1;
  1308. step = 1 << (b->uvtx * 2);
  1309. for (p = 0; p < 2; p++) {
  1310. dst = b->dst[p + 1];
  1311. for (n = 0, y = 0; y < end_y; y += uvstep1d) {
  1312. uint8_t *ptr = dst;
  1313. for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
  1314. int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n])
  1315. : s->uveob[p][n];
  1316. if (eob)
  1317. s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
  1318. s->uvblock[p] + 16 * n, eob);
  1319. }
  1320. dst += 4 * uvstep1d * b->uv_stride;
  1321. }
  1322. }
  1323. }
  1324. return 0;
  1325. }
  1326. static av_always_inline void mask_edges(VP9Filter *lflvl, int is_uv,
  1327. int row_and_7, int col_and_7,
  1328. int w, int h, int col_end, int row_end,
  1329. enum TxfmMode tx, int skip_inter)
  1330. {
  1331. // FIXME I'm pretty sure all loops can be replaced by a single LUT if
  1332. // we make VP9Filter.mask uint64_t (i.e. row/col all single variable)
  1333. // and make the LUT 5-indexed (bl, bp, is_uv, tx and row/col), and then
  1334. // use row_and_7/col_and_7 as shifts (1*col_and_7+8*row_and_7)
  1335. // the intended behaviour of the vp9 loopfilter is to work on 8-pixel
  1336. // edges. This means that for UV, we work on two subsampled blocks at
  1337. // a time, and we only use the topleft block's mode information to set
  1338. // things like block strength. Thus, for any block size smaller than
  1339. // 16x16, ignore the odd portion of the block.
  1340. if (tx == TX_4X4 && is_uv) {
  1341. if (h == 1) {
  1342. if (row_and_7 & 1)
  1343. return;
  1344. if (!row_end)
  1345. h += 1;
  1346. }
  1347. if (w == 1) {
  1348. if (col_and_7 & 1)
  1349. return;
  1350. if (!col_end)
  1351. w += 1;
  1352. }
  1353. }
  1354. if (tx == TX_4X4 && !skip_inter) {
  1355. int t = 1 << col_and_7, m_col = (t << w) - t, y;
  1356. int m_col_odd = (t << (w - 1)) - t;
  1357. // on 32-px edges, use the 8-px wide loopfilter; else, use 4-px wide
  1358. if (is_uv) {
  1359. int m_row_8 = m_col & 0x01, m_row_4 = m_col - m_row_8;
  1360. for (y = row_and_7; y < h + row_and_7; y++) {
  1361. int col_mask_id = 2 - !(y & 7);
  1362. lflvl->mask[is_uv][0][y][1] |= m_row_8;
  1363. lflvl->mask[is_uv][0][y][2] |= m_row_4;
  1364. // for odd lines, if the odd col is not being filtered,
  1365. // skip odd row also:
  1366. // .---. <-- a
  1367. // | |
  1368. // |___| <-- b
  1369. // ^ ^
  1370. // c d
  1371. //
  1372. // if a/c are even row/col and b/d are odd, and d is skipped,
  1373. // e.g. right edge of size-66x66.webm, then skip b also (bug)
  1374. if ((col_end & 1) && (y & 1)) {
  1375. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col_odd;
  1376. } else {
  1377. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col;
  1378. }
  1379. }
  1380. } else {
  1381. int m_row_8 = m_col & 0x11, m_row_4 = m_col - m_row_8;
  1382. for (y = row_and_7; y < h + row_and_7; y++) {
  1383. int col_mask_id = 2 - !(y & 3);
  1384. lflvl->mask[is_uv][0][y][1] |= m_row_8; // row edge
  1385. lflvl->mask[is_uv][0][y][2] |= m_row_4;
  1386. lflvl->mask[is_uv][1][y][col_mask_id] |= m_col; // col edge
  1387. lflvl->mask[is_uv][0][y][3] |= m_col;
  1388. lflvl->mask[is_uv][1][y][3] |= m_col;
  1389. }
  1390. }
  1391. } else {
  1392. int y, t = 1 << col_and_7, m_col = (t << w) - t;
  1393. if (!skip_inter) {
  1394. int mask_id = (tx == TX_8X8);
  1395. int l2 = tx + is_uv - 1, step1d = 1 << l2;
  1396. static const unsigned masks[4] = { 0xff, 0x55, 0x11, 0x01 };
  1397. int m_row = m_col & masks[l2];
  1398. // at odd UV col/row edges tx16/tx32 loopfilter edges, force
  1399. // 8wd loopfilter to prevent going off the visible edge.
  1400. if (is_uv && tx > TX_8X8 && (w ^ (w - 1)) == 1) {
  1401. int m_row_16 = ((t << (w - 1)) - t) & masks[l2];
  1402. int m_row_8 = m_row - m_row_16;
  1403. for (y = row_and_7; y < h + row_and_7; y++) {
  1404. lflvl->mask[is_uv][0][y][0] |= m_row_16;
  1405. lflvl->mask[is_uv][0][y][1] |= m_row_8;
  1406. }
  1407. } else {
  1408. for (y = row_and_7; y < h + row_and_7; y++)
  1409. lflvl->mask[is_uv][0][y][mask_id] |= m_row;
  1410. }
  1411. if (is_uv && tx > TX_8X8 && (h ^ (h - 1)) == 1) {
  1412. for (y = row_and_7; y < h + row_and_7 - 1; y += step1d)
  1413. lflvl->mask[is_uv][1][y][0] |= m_col;
  1414. if (y - row_and_7 == h - 1)
  1415. lflvl->mask[is_uv][1][y][1] |= m_col;
  1416. } else {
  1417. for (y = row_and_7; y < h + row_and_7; y += step1d)
  1418. lflvl->mask[is_uv][1][y][mask_id] |= m_col;
  1419. }
  1420. } else if (tx != TX_4X4) {
  1421. int mask_id;
  1422. mask_id = (tx == TX_8X8) || (is_uv && h == 1);
  1423. lflvl->mask[is_uv][1][row_and_7][mask_id] |= m_col;
  1424. mask_id = (tx == TX_8X8) || (is_uv && w == 1);
  1425. for (y = row_and_7; y < h + row_and_7; y++)
  1426. lflvl->mask[is_uv][0][y][mask_id] |= t;
  1427. } else if (is_uv) {
  1428. int t8 = t & 0x01, t4 = t - t8;
  1429. for (y = row_and_7; y < h + row_and_7; y++) {
  1430. lflvl->mask[is_uv][0][y][2] |= t4;
  1431. lflvl->mask[is_uv][0][y][1] |= t8;
  1432. }
  1433. lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 7)] |= m_col;
  1434. } else {
  1435. int t8 = t & 0x11, t4 = t - t8;
  1436. for (y = row_and_7; y < h + row_and_7; y++) {
  1437. lflvl->mask[is_uv][0][y][2] |= t4;
  1438. lflvl->mask[is_uv][0][y][1] |= t8;
  1439. }
  1440. lflvl->mask[is_uv][1][row_and_7][2 - !(row_and_7 & 3)] |= m_col;
  1441. }
  1442. }
  1443. }
  1444. int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
  1445. VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  1446. enum BlockLevel bl, enum BlockPartition bp)
  1447. {
  1448. VP9Context *s = avctx->priv_data;
  1449. VP9Block *const b = &s->b;
  1450. enum BlockSize bs = bl * 3 + bp;
  1451. int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
  1452. int emu[2];
  1453. b->row = row;
  1454. b->row7 = row & 7;
  1455. b->col = col;
  1456. b->col7 = col & 7;
  1457. s->min_mv.x = -(128 + col * 64);
  1458. s->min_mv.y = -(128 + row * 64);
  1459. s->max_mv.x = 128 + (s->cols - col - w4) * 64;
  1460. s->max_mv.y = 128 + (s->rows - row - h4) * 64;
  1461. b->bs = bs;
  1462. decode_mode(s, b);
  1463. b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx));
  1464. if (!b->skip) {
  1465. if ((ret = decode_coeffs(avctx)) < 0)
  1466. return ret;
  1467. } else {
  1468. int pl;
  1469. memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2);
  1470. memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2);
  1471. for (pl = 0; pl < 2; pl++) {
  1472. memset(&s->above_uv_nnz_ctx[pl][col], 0, w4);
  1473. memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4);
  1474. }
  1475. }
  1476. /* Emulated overhangs if the stride of the target buffer can't hold.
  1477. * This allows to support emu-edge and so on even if we have large
  1478. * block overhangs. */
  1479. emu[0] = (col + w4) * 8 > s->cur_frame->linesize[0] ||
  1480. (row + h4) > s->rows;
  1481. emu[1] = (col + w4) * 4 > s->cur_frame->linesize[1] ||
  1482. (row + h4) > s->rows;
  1483. if (emu[0]) {
  1484. b->dst[0] = s->tmp_y;
  1485. b->y_stride = 64;
  1486. } else {
  1487. b->dst[0] = s->cur_frame->data[0] + yoff;
  1488. b->y_stride = s->cur_frame->linesize[0];
  1489. }
  1490. if (emu[1]) {
  1491. b->dst[1] = s->tmp_uv[0];
  1492. b->dst[2] = s->tmp_uv[1];
  1493. b->uv_stride = 32;
  1494. } else {
  1495. b->dst[1] = s->cur_frame->data[1] + uvoff;
  1496. b->dst[2] = s->cur_frame->data[2] + uvoff;
  1497. b->uv_stride = s->cur_frame->linesize[1];
  1498. }
  1499. if (b->intra) {
  1500. intra_recon(avctx, yoff, uvoff);
  1501. } else {
  1502. if ((ret = inter_recon(avctx)) < 0)
  1503. return ret;
  1504. }
  1505. if (emu[0]) {
  1506. int w = FFMIN(s->cols - col, w4) * 8;
  1507. int h = FFMIN(s->rows - row, h4) * 8;
  1508. int n, o = 0;
  1509. for (n = 0; o < w; n++) {
  1510. int bw = 64 >> n;
  1511. av_assert2(n <= 4);
  1512. if (w & bw) {
  1513. s->dsp.mc[n][0][0][0][0](s->cur_frame->data[0] + yoff + o,
  1514. s->tmp_y + o,
  1515. s->cur_frame->linesize[0],
  1516. 64, h, 0, 0);
  1517. o += bw;
  1518. }
  1519. }
  1520. }
  1521. if (emu[1]) {
  1522. int w = FFMIN(s->cols - col, w4) * 4;
  1523. int h = FFMIN(s->rows - row, h4) * 4;
  1524. int n, o = 0;
  1525. for (n = 1; o < w; n++) {
  1526. int bw = 64 >> n;
  1527. av_assert2(n <= 4);
  1528. if (w & bw) {
  1529. s->dsp.mc[n][0][0][0][0](s->cur_frame->data[1] + uvoff + o,
  1530. s->tmp_uv[0] + o,
  1531. s->cur_frame->linesize[1],
  1532. 32, h, 0, 0);
  1533. s->dsp.mc[n][0][0][0][0](s->cur_frame->data[2] + uvoff + o,
  1534. s->tmp_uv[1] + o,
  1535. s->cur_frame->linesize[2],
  1536. 32, h, 0, 0);
  1537. o += bw;
  1538. }
  1539. }
  1540. }
  1541. // pick filter level and find edges to apply filter to
  1542. if (s->filter.level &&
  1543. (lvl = s->segmentation.feat[b->seg_id].lflvl[b->intra ? 0 : b->ref[0] + 1]
  1544. [b->mode[3] != ZEROMV]) > 0) {
  1545. int x_end = FFMIN(s->cols - col, w4);
  1546. int y_end = FFMIN(s->rows - row, h4);
  1547. int skip_inter = !b->intra && b->skip;
  1548. for (y = 0; y < h4; y++)
  1549. memset(&lflvl->level[((row & 7) + y) * 8 + (col & 7)], lvl, w4);
  1550. mask_edges(lflvl, 0, row & 7, col & 7, x_end, y_end, 0, 0, b->tx, skip_inter);
  1551. mask_edges(lflvl, 1, row & 7, col & 7, x_end, y_end,
  1552. s->cols & 1 && col + w4 >= s->cols ? s->cols & 7 : 0,
  1553. s->rows & 1 && row + h4 >= s->rows ? s->rows & 7 : 0,
  1554. b->uvtx, skip_inter);
  1555. if (!s->filter.lim_lut[lvl]) {
  1556. int sharp = s->filter.sharpness;
  1557. int limit = lvl;
  1558. if (sharp > 0) {
  1559. limit >>= (sharp + 3) >> 2;
  1560. limit = FFMIN(limit, 9 - sharp);
  1561. }
  1562. limit = FFMAX(limit, 1);
  1563. s->filter.lim_lut[lvl] = limit;
  1564. s->filter.mblim_lut[lvl] = 2 * (lvl + 2) + limit;
  1565. }
  1566. }
  1567. return 0;
  1568. }