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.

1740 lines
73KB

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