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.

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