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.

1750 lines
74KB

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