intra.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * VVC intra prediction
  3. *
  4. * Copyright (C) 2021 Nuo Mi
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/frame.h"
  23. #include "libavutil/imgutils.h"
  24. #include "data.h"
  25. #include "inter.h"
  26. #include "intra.h"
  27. #include "itx_1d.h"
  28. #define POS(c_idx, x, y) \
  29. &fc->frame->data[c_idx][((y) >> fc->ps.sps->vshift[c_idx]) * fc->frame->linesize[c_idx] + \
  30. (((x) >> fc->ps.sps->hshift[c_idx]) << fc->ps.sps->pixel_shift)]
  31. static int is_cclm(enum IntraPredMode mode)
  32. {
  33. return mode == INTRA_LT_CCLM || mode == INTRA_L_CCLM || mode == INTRA_T_CCLM;
  34. }
  35. static int derive_ilfnst_pred_mode_intra(const VVCLocalContext *lc, const TransformBlock *tb)
  36. {
  37. const VVCFrameContext *fc = lc->fc;
  38. const VVCSPS *sps = fc->ps.sps;
  39. const CodingUnit *cu = lc->cu;
  40. const int x_tb = tb->x0 >> fc->ps.sps->min_cb_log2_size_y;
  41. const int y_tb = tb->y0 >> fc->ps.sps->min_cb_log2_size_y;
  42. const int x_c = (tb->x0 + (tb->tb_width << sps->hshift[1] >> 1) ) >> fc->ps.sps->min_cb_log2_size_y;
  43. const int y_c = (tb->y0 + (tb->tb_height << sps->vshift[1] >> 1)) >> fc->ps.sps->min_cb_log2_size_y;
  44. const int min_cb_width = fc->ps.pps->min_cb_width;
  45. const int intra_mip_flag = SAMPLE_CTB(fc->tab.imf, x_tb, y_tb);
  46. int pred_mode_intra = tb->c_idx == 0 ? cu->intra_pred_mode_y : cu->intra_pred_mode_c;
  47. if (intra_mip_flag && !tb->c_idx) {
  48. pred_mode_intra = INTRA_PLANAR;
  49. } else if (is_cclm(pred_mode_intra)) {
  50. int intra_mip_flag_c = SAMPLE_CTB(fc->tab.imf, x_c, y_c);
  51. int cu_pred_mode = SAMPLE_CTB(fc->tab.cpm[0], x_c, y_c);
  52. if (intra_mip_flag_c) {
  53. pred_mode_intra = INTRA_PLANAR;
  54. } else if (cu_pred_mode == MODE_IBC || cu_pred_mode == MODE_PLT) {
  55. pred_mode_intra = INTRA_DC;
  56. } else {
  57. pred_mode_intra = SAMPLE_CTB(fc->tab.ipm, x_c, y_c);
  58. }
  59. }
  60. pred_mode_intra = ff_vvc_wide_angle_mode_mapping(cu, tb->tb_width, tb->tb_height, tb->c_idx, pred_mode_intra);
  61. return pred_mode_intra;
  62. }
  63. //8.7.4 Transformation process for scaled transform coefficients
  64. static void ilfnst_transform(const VVCLocalContext *lc, TransformBlock *tb)
  65. {
  66. const VVCSPS *sps = lc->fc->ps.sps;
  67. const CodingUnit *cu = lc->cu;
  68. const int w = tb->tb_width;
  69. const int h = tb->tb_height;
  70. const int n_lfnst_out_size = (w >= 8 && h >= 8) ? 48 : 16; ///< nLfnstOutSize
  71. const int log2_lfnst_size = (w >= 8 && h >= 8) ? 3 : 2; ///< log2LfnstSize
  72. const int n_lfnst_size = 1 << log2_lfnst_size; ///< nLfnstSize
  73. const int non_zero_size = ((w == 8 && h == 8) || (w == 4 && h == 4)) ? 8 : 16; ///< nonZeroSize
  74. const int pred_mode_intra = derive_ilfnst_pred_mode_intra(lc, tb);
  75. const int transpose = pred_mode_intra > 34;
  76. int u[16], v[48];
  77. for (int x = 0; x < non_zero_size; x++) {
  78. int xc = ff_vvc_diag_scan_x[2][2][x];
  79. int yc = ff_vvc_diag_scan_y[2][2][x];
  80. u[x] = tb->coeffs[w * yc + xc];
  81. }
  82. ff_vvc_inv_lfnst_1d(v, u, non_zero_size, n_lfnst_out_size, pred_mode_intra,
  83. cu->lfnst_idx, sps->log2_transform_range);
  84. if (transpose) {
  85. int *dst = tb->coeffs;
  86. const int *src = v;
  87. if (n_lfnst_size == 4) {
  88. for (int y = 0; y < 4; y++) {
  89. dst[0] = src[0];
  90. dst[1] = src[4];
  91. dst[2] = src[8];
  92. dst[3] = src[12];
  93. src++;
  94. dst += w;
  95. }
  96. } else {
  97. for (int y = 0; y < 8; y++) {
  98. dst[0] = src[0];
  99. dst[1] = src[8];
  100. dst[2] = src[16];
  101. dst[3] = src[24];
  102. if (y < 4) {
  103. dst[4] = src[32];
  104. dst[5] = src[36];
  105. dst[6] = src[40];
  106. dst[7] = src[44];
  107. }
  108. src++;
  109. dst += w;
  110. }
  111. }
  112. } else {
  113. int *dst = tb->coeffs;
  114. const int *src = v;
  115. for (int y = 0; y < n_lfnst_size; y++) {
  116. int size = (y < 4) ? n_lfnst_size : 4;
  117. memcpy(dst, src, size * sizeof(int));
  118. src += size;
  119. dst += w;
  120. }
  121. }
  122. tb->max_scan_x = n_lfnst_size - 1;
  123. tb->max_scan_y = n_lfnst_size - 1;
  124. }
  125. //part of 8.7.4 Transformation process for scaled transform coefficients
  126. static void derive_transform_type(const VVCFrameContext *fc, const VVCLocalContext *lc, const TransformBlock *tb, enum VVCTxType *trh, enum VVCTxType *trv)
  127. {
  128. const CodingUnit *cu = lc->cu;
  129. static const enum VVCTxType mts_to_trh[] = { VVC_DCT2, VVC_DST7, VVC_DCT8, VVC_DST7, VVC_DCT8 };
  130. static const enum VVCTxType mts_to_trv[] = { VVC_DCT2, VVC_DST7, VVC_DST7, VVC_DCT8, VVC_DCT8 };
  131. const VVCSPS *sps = fc->ps.sps;
  132. int implicit_mts_enabled = 0;
  133. if (tb->c_idx || (cu->isp_split_type != ISP_NO_SPLIT && cu->lfnst_idx)) {
  134. *trh = *trv = VVC_DCT2;
  135. return;
  136. }
  137. if (sps->r->sps_mts_enabled_flag) {
  138. if (cu->isp_split_type != ISP_NO_SPLIT ||
  139. (cu->sbt_flag && FFMAX(tb->tb_width, tb->tb_height) <= 32) ||
  140. (!sps->r->sps_explicit_mts_intra_enabled_flag && cu->pred_mode == MODE_INTRA &&
  141. !cu->lfnst_idx && !cu->intra_mip_flag)) {
  142. implicit_mts_enabled = 1;
  143. }
  144. }
  145. if (implicit_mts_enabled) {
  146. const int w = tb->tb_width;
  147. const int h = tb->tb_height;
  148. if (cu->sbt_flag) {
  149. *trh = (cu->sbt_horizontal_flag || cu->sbt_pos_flag) ? VVC_DST7 : VVC_DCT8;
  150. *trv = (!cu->sbt_horizontal_flag || cu->sbt_pos_flag) ? VVC_DST7 : VVC_DCT8;
  151. } else {
  152. *trh = (w >= 4 && w <= 16) ? VVC_DST7 : VVC_DCT2;
  153. *trv = (h >= 4 && h <= 16) ? VVC_DST7 : VVC_DCT2;
  154. }
  155. return;
  156. }
  157. *trh = mts_to_trh[cu->mts_idx];
  158. *trv = mts_to_trv[cu->mts_idx];
  159. }
  160. static int add_reconstructed_area(VVCLocalContext *lc, const int ch_type, const int x0, const int y0, const int w, const int h)
  161. {
  162. const VVCSPS *sps = lc->fc->ps.sps;
  163. const int hs = sps->hshift[ch_type];
  164. const int vs = sps->vshift[ch_type];
  165. ReconstructedArea *a;
  166. if (lc->num_ras[ch_type] >= FF_ARRAY_ELEMS(lc->ras[ch_type]))
  167. return AVERROR_INVALIDDATA;
  168. a = &lc->ras[ch_type][lc->num_ras[ch_type]];
  169. a->x = x0 >> hs;
  170. a->y = y0 >> vs;
  171. a->w = w >> hs;
  172. a->h = h >> vs;
  173. lc->num_ras[ch_type]++;
  174. return 0;
  175. }
  176. static void add_tu_area(const TransformUnit *tu, int *x0, int *y0, int *w, int *h)
  177. {
  178. *x0 = tu->x0;
  179. *y0 = tu->y0;
  180. *w = tu->width;
  181. *h = tu->height;
  182. }
  183. #define MIN_ISP_PRED_WIDTH 4
  184. static int get_luma_predict_unit(const CodingUnit *cu, const TransformUnit *tu, const int idx, int *x0, int *y0, int *w, int *h)
  185. {
  186. int has_luma = 1;
  187. add_tu_area(tu, x0, y0, w, h);
  188. if (cu->isp_split_type == ISP_VER_SPLIT && tu->width < MIN_ISP_PRED_WIDTH) {
  189. *w = MIN_ISP_PRED_WIDTH;
  190. has_luma = !(idx % (MIN_ISP_PRED_WIDTH / tu->width));
  191. }
  192. return has_luma;
  193. }
  194. static int get_chroma_predict_unit(const CodingUnit *cu, const TransformUnit *tu, const int idx, int *x0, int *y0, int *w, int *h)
  195. {
  196. if (cu->isp_split_type == ISP_NO_SPLIT) {
  197. add_tu_area(tu, x0, y0, w, h);
  198. return 1;
  199. }
  200. if (idx == cu->num_intra_subpartitions - 1) {
  201. *x0 = cu->x0;
  202. *y0 = cu->y0;
  203. *w = cu->cb_width;
  204. *h = cu->cb_height;
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. //8.4.5.1 General decoding process for intra blocks
  210. static void predict_intra(VVCLocalContext *lc, const TransformUnit *tu, const int idx, const int target_ch_type)
  211. {
  212. const VVCFrameContext *fc = lc->fc;
  213. const CodingUnit *cu = lc->cu;
  214. const VVCTreeType tree_type = cu->tree_type;
  215. int x0, y0, w, h;
  216. if (cu->pred_mode != MODE_INTRA) {
  217. add_reconstructed_area(lc, target_ch_type, tu->x0, tu->y0, tu->width, tu->height);
  218. return;
  219. }
  220. if (!target_ch_type && tree_type != DUAL_TREE_CHROMA) {
  221. if (get_luma_predict_unit(cu, tu, idx, &x0, &y0, &w, &h)) {
  222. ff_vvc_set_neighbour_available(lc, x0, y0, w, h);
  223. fc->vvcdsp.intra.intra_pred(lc, x0, y0, w, h, 0);
  224. add_reconstructed_area(lc, 0, x0, y0, w, h);
  225. }
  226. }
  227. if (target_ch_type && tree_type != DUAL_TREE_LUMA) {
  228. if (get_chroma_predict_unit(cu, tu, idx, &x0, &y0, &w, &h)){
  229. ff_vvc_set_neighbour_available(lc, x0, y0, w, h);
  230. if (is_cclm(cu->intra_pred_mode_c)) {
  231. fc->vvcdsp.intra.intra_cclm_pred(lc, x0, y0, w, h);
  232. } else {
  233. fc->vvcdsp.intra.intra_pred(lc, x0, y0, w, h, 1);
  234. fc->vvcdsp.intra.intra_pred(lc, x0, y0, w, h, 2);
  235. }
  236. add_reconstructed_area(lc, 1, x0, y0, w, h);
  237. }
  238. }
  239. }
  240. static void scale_clip(int *coeff, const int nzw, const int w, const int h,
  241. const int shift, const int log2_transform_range)
  242. {
  243. const int add = 1 << (shift - 1);
  244. for (int y = 0; y < h; y++) {
  245. int *p = coeff + y * w;
  246. for (int x = 0; x < nzw; x++) {
  247. *p = av_clip_intp2((*p + add) >> shift, log2_transform_range);
  248. p++;
  249. }
  250. memset(p, 0, sizeof(*p) * (w - nzw));
  251. }
  252. }
  253. static void scale(int *out, const int *in, const int w, const int h, const int shift)
  254. {
  255. const int add = 1 << (shift - 1);
  256. for (int y = 0; y < h; y++) {
  257. for (int x = 0; x < w; x++) {
  258. int *o = out + y * w + x;
  259. const int *i = in + y * w + x;
  260. *o = (*i + add) >> shift;
  261. }
  262. }
  263. }
  264. // part of 8.7.3 Scaling process for transform coefficients
  265. static void derive_qp(const VVCLocalContext *lc, const TransformUnit *tu, TransformBlock *tb)
  266. {
  267. const VVCSPS *sps = lc->fc->ps.sps;
  268. const H266RawSliceHeader *rsh = lc->sc->sh.r;
  269. const CodingUnit *cu = lc->cu;
  270. const bool is_jcbcr = tb->c_idx && tu->joint_cbcr_residual_flag && tu->coded_flag[CB] && tu->coded_flag[CR];
  271. const int idx = is_jcbcr ? JCBCR : tb->c_idx;
  272. const int qp = cu->qp[idx] + (idx ? 0 : sps->qp_bd_offset);
  273. const int act_offset[] = { -5, 1, 3, 1 };
  274. const int qp_act_offset = cu->act_enabled_flag ? act_offset[idx] : 0;
  275. if (tb->ts) {
  276. const int qp_prime_ts_min = 4 + 6 * sps->r->sps_min_qp_prime_ts;
  277. tb->qp = av_clip(qp + qp_act_offset, qp_prime_ts_min, 63 + sps->qp_bd_offset);
  278. tb->rect_non_ts_flag = 0;
  279. tb->bd_shift = 10;
  280. } else {
  281. const int log_sum = tb->log2_tb_width + tb->log2_tb_height;
  282. const int rect_non_ts_flag = log_sum & 1;
  283. tb->qp = av_clip(qp + qp_act_offset, 0, 63 + sps->qp_bd_offset);
  284. tb->rect_non_ts_flag = rect_non_ts_flag;
  285. tb->bd_shift = sps->bit_depth + rect_non_ts_flag + (log_sum / 2)
  286. + 10 - sps->log2_transform_range + rsh->sh_dep_quant_used_flag;
  287. }
  288. tb->bd_offset = (1 << tb->bd_shift) >> 1;
  289. }
  290. static const uint8_t rem6[63 + 8 * 6 + 1] = {
  291. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  292. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  293. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  294. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  295. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  296. };
  297. static const uint8_t div6[63 + 8 * 6 + 1] = {
  298. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
  299. 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
  300. 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11,
  301. 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15,
  302. 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
  303. };
  304. const static int level_scale[2][6] = {
  305. { 40, 45, 51, 57, 64, 72 },
  306. { 57, 64, 72, 80, 90, 102 }
  307. };
  308. //8.7.3 Scaling process for transform coefficients
  309. static av_always_inline int derive_scale(const TransformBlock *tb, const int sh_dep_quant_used_flag)
  310. {
  311. const int addin = sh_dep_quant_used_flag && !tb->ts;
  312. const int qp = tb->qp + addin;
  313. return level_scale[tb->rect_non_ts_flag][rem6[qp]] << div6[qp];
  314. }
  315. //8.7.3 Scaling process for transform coefficients
  316. static const uint8_t* derive_scale_m(const VVCLocalContext *lc, const TransformBlock *tb, uint8_t *scale_m)
  317. {
  318. //Table 38 – Specification of the scaling matrix identifier variable id according to predMode, cIdx, nTbW, and nTbH
  319. const int ids[2][3][6] = {
  320. {
  321. { 0, 2, 8, 14, 20, 26 },
  322. { 0, 3, 9, 15, 21, 21 },
  323. { 0, 4, 10, 16, 22, 22 }
  324. },
  325. {
  326. { 0, 5, 11, 17, 23, 27 },
  327. { 0, 6, 12, 18, 24, 24 },
  328. { 1, 7, 13, 19, 25, 25 },
  329. }
  330. };
  331. const VVCFrameParamSets *ps = &lc->fc->ps;
  332. const VVCSPS *sps = ps->sps;
  333. const H266RawSliceHeader *rsh = lc->sc->sh.r;
  334. const CodingUnit *cu = lc->cu;
  335. const VVCScalingList *sl = ps->sl;
  336. const int id = ids[cu->pred_mode != MODE_INTRA][tb->c_idx][FFMAX(tb->log2_tb_height, tb->log2_tb_width) - 1];
  337. const int log2_matrix_size = (id < 2) ? 1 : (id < 8) ? 2 : 3;
  338. uint8_t *p = scale_m;
  339. if (!rsh->sh_explicit_scaling_list_used_flag || tb->ts ||
  340. (sps->r->sps_scaling_matrix_for_lfnst_disabled_flag && cu->apply_lfnst_flag[tb->c_idx]) ||
  341. (sps->r->sps_scaling_matrix_for_alternative_colour_space_disabled_flag &&
  342. sps->r->sps_scaling_matrix_designated_colour_space_flag == cu->act_enabled_flag))
  343. return ff_vvc_default_scale_m;
  344. if (!sl) {
  345. av_log(lc->fc->log_ctx, AV_LOG_WARNING, "bug: no scaling list aps, id = %d", ps->ph.r->ph_scaling_list_aps_id);
  346. return ff_vvc_default_scale_m;
  347. }
  348. for (int y = tb->min_scan_y; y <= tb->max_scan_y; y++) {
  349. const int off = y << log2_matrix_size >> tb->log2_tb_height << log2_matrix_size;
  350. const uint8_t *m = &sl->scaling_matrix_rec[id][off];
  351. for (int x = tb->min_scan_x; x <= tb->max_scan_x; x++)
  352. *p++ = m[x << log2_matrix_size >> tb->log2_tb_width];
  353. }
  354. if (id >= SL_START_16x16 && !tb->min_scan_x && !tb->min_scan_y)
  355. *scale_m = sl->scaling_matrix_dc_rec[id - SL_START_16x16];
  356. return scale_m;
  357. }
  358. //8.7.3 Scaling process for transform coefficients
  359. static av_always_inline int scale_coeff(const TransformBlock *tb, int coeff,
  360. const int scale, const int scale_m, const int log2_transform_range)
  361. {
  362. coeff = ((int64_t) coeff * scale * scale_m + tb->bd_offset) >> tb->bd_shift;
  363. coeff = av_clip_intp2(coeff, log2_transform_range);
  364. return coeff;
  365. }
  366. static void dequant(const VVCLocalContext *lc, const TransformUnit *tu, TransformBlock *tb)
  367. {
  368. uint8_t tmp[MAX_TB_SIZE * MAX_TB_SIZE];
  369. const H266RawSliceHeader *rsh = lc->sc->sh.r;
  370. const VVCSPS *sps = lc->fc->ps.sps;
  371. const uint8_t *scale_m = derive_scale_m(lc, tb, tmp);
  372. int scale;
  373. derive_qp(lc, tu, tb);
  374. scale = derive_scale(tb, rsh->sh_dep_quant_used_flag);
  375. for (int y = tb->min_scan_y; y <= tb->max_scan_y; y++) {
  376. for (int x = tb->min_scan_x; x <= tb->max_scan_x; x++) {
  377. int *coeff = tb->coeffs + y * tb->tb_width + x;
  378. if (*coeff)
  379. *coeff = scale_coeff(tb, *coeff, scale, *scale_m, sps->log2_transform_range);
  380. scale_m++;
  381. }
  382. }
  383. }
  384. //transmatrix[0][0]
  385. #define DCT_A 64
  386. static void itx_2d(const VVCFrameContext *fc, TransformBlock *tb, const enum VVCTxType trh, const enum VVCTxType trv)
  387. {
  388. const VVCSPS *sps = fc->ps.sps;
  389. const int w = tb->tb_width;
  390. const int h = tb->tb_height;
  391. const size_t nzw = tb->max_scan_x + 1;
  392. const size_t nzh = tb->max_scan_y + 1;
  393. const int shift[] = { 7, 5 + sps->log2_transform_range - sps->bit_depth };
  394. if (w == h && nzw == 1 && nzh == 1 && trh == VVC_DCT2 && trv == VVC_DCT2) {
  395. const int add[] = { 1 << (shift[0] - 1), 1 << (shift[1] - 1) };
  396. const int t = (tb->coeffs[0] * DCT_A + add[0]) >> shift[0];
  397. const int dc = (t * DCT_A + add[1]) >> shift[1];
  398. for (int i = 0; i < w * h; i++)
  399. tb->coeffs[i] = dc;
  400. return;
  401. }
  402. for (int x = 0; x < nzw; x++)
  403. fc->vvcdsp.itx.itx[trv][tb->log2_tb_height - 1](tb->coeffs + x, w, nzh);
  404. scale_clip(tb->coeffs, nzw, w, h, shift[0], sps->log2_transform_range);
  405. for (int y = 0; y < h; y++)
  406. fc->vvcdsp.itx.itx[trh][tb->log2_tb_width - 1](tb->coeffs + y * w, 1, nzw);
  407. scale(tb->coeffs, tb->coeffs, w, h, shift[1]);
  408. }
  409. static void itx_1d(const VVCFrameContext *fc, TransformBlock *tb, const enum VVCTxType trh, const enum VVCTxType trv)
  410. {
  411. const VVCSPS *sps = fc->ps.sps;
  412. const int w = tb->tb_width;
  413. const int h = tb->tb_height;
  414. const size_t nzw = tb->max_scan_x + 1;
  415. const size_t nzh = tb->max_scan_y + 1;
  416. if ((w > 1 && nzw == 1 && trh == VVC_DCT2) || (h > 1 && nzh == 1 && trv == VVC_DCT2)) {
  417. const int shift = 6 + sps->log2_transform_range - sps->bit_depth;
  418. const int add = 1 << (shift - 1);
  419. const int dc = (tb->coeffs[0] * DCT_A + add) >> shift;
  420. for (int i = 0; i < w * h; i++)
  421. tb->coeffs[i] = dc;
  422. return;
  423. }
  424. if (w > 1)
  425. fc->vvcdsp.itx.itx[trh][tb->log2_tb_width - 1](tb->coeffs, 1, nzw);
  426. else
  427. fc->vvcdsp.itx.itx[trv][tb->log2_tb_height - 1](tb->coeffs, 1, nzh);
  428. scale(tb->coeffs, tb->coeffs, w, h, 6 + sps->log2_transform_range - sps->bit_depth);
  429. }
  430. static void transform_bdpcm(TransformBlock *tb, const VVCLocalContext *lc, const CodingUnit *cu)
  431. {
  432. const VVCSPS *sps = lc->fc->ps.sps;
  433. const IntraPredMode mode = tb->c_idx ? cu->intra_pred_mode_c : cu->intra_pred_mode_y;
  434. const int vertical = mode == INTRA_VERT;
  435. lc->fc->vvcdsp.itx.transform_bdpcm(tb->coeffs, tb->tb_width, tb->tb_height,
  436. vertical, sps->log2_transform_range);
  437. if (vertical)
  438. tb->max_scan_y = tb->tb_height - 1;
  439. else
  440. tb->max_scan_x = tb->tb_width - 1;
  441. }
  442. static void lmcs_scale_chroma(VVCLocalContext *lc, TransformUnit *tu, TransformBlock *tb, const int target_ch_type)
  443. {
  444. const VVCFrameContext *fc = lc->fc;
  445. const VVCSH *sh = &lc->sc->sh;
  446. const CodingUnit *cu = lc->cu;
  447. const int c_idx = tb->c_idx;
  448. const int ch_type = c_idx > 0;
  449. const int w = tb->tb_width;
  450. const int h = tb->tb_height;
  451. const int chroma_scale = ch_type && sh->r->sh_lmcs_used_flag && fc->ps.ph.r->ph_chroma_residual_scale_flag && (w * h > 4);
  452. const int has_jcbcr = tu->joint_cbcr_residual_flag && c_idx;
  453. for (int j = 0; j < 1 + has_jcbcr; j++) {
  454. const bool is_jcbcr = j > 0;
  455. const int jcbcr_idx = CB + tu->coded_flag[CB];
  456. TransformBlock *jcbcr = &tu->tbs[jcbcr_idx - tu->tbs[0].c_idx];
  457. int *coeffs = is_jcbcr ? jcbcr->coeffs : tb->coeffs;
  458. if (!j && has_jcbcr) {
  459. const int c_sign = 1 - 2 * fc->ps.ph.r->ph_joint_cbcr_sign_flag;
  460. const int shift = tu->coded_flag[CB] ^ tu->coded_flag[CR];
  461. fc->vvcdsp.itx.pred_residual_joint(jcbcr->coeffs, tb->coeffs, w, h, c_sign, shift);
  462. }
  463. if (chroma_scale)
  464. fc->vvcdsp.intra.lmcs_scale_chroma(lc, coeffs, w, h, cu->x0, cu->y0);
  465. }
  466. }
  467. static void add_residual(const VVCLocalContext *lc, TransformUnit *tu, const int target_ch_type)
  468. {
  469. const VVCFrameContext *fc = lc->fc;
  470. const CodingUnit *cu = lc->cu;
  471. for (int i = 0; i < tu->nb_tbs; i++) {
  472. TransformBlock *tb = tu->tbs + i;
  473. const int c_idx = tb->c_idx;
  474. const int ch_type = c_idx > 0;
  475. const ptrdiff_t stride = fc->frame->linesize[c_idx];
  476. const bool has_residual = tb->has_coeffs || cu->act_enabled_flag ||
  477. (c_idx && tu->joint_cbcr_residual_flag);
  478. uint8_t *dst = POS(c_idx, tb->x0, tb->y0);
  479. if (ch_type == target_ch_type && has_residual)
  480. fc->vvcdsp.itx.add_residual(dst, tb->coeffs, tb->tb_width, tb->tb_height, stride);
  481. }
  482. }
  483. static void itransform(VVCLocalContext *lc, TransformUnit *tu, const int target_ch_type)
  484. {
  485. const VVCFrameContext *fc = lc->fc;
  486. const CodingUnit *cu = lc->cu;
  487. TransformBlock *tbs = tu->tbs;
  488. const bool is_act_luma = cu->act_enabled_flag && target_ch_type == LUMA;
  489. for (int i = 0; i < tu->nb_tbs; i++) {
  490. TransformBlock *tb = tbs + i;
  491. const int c_idx = tb->c_idx;
  492. const int ch_type = c_idx > 0;
  493. const bool do_itx = is_act_luma || !cu->act_enabled_flag && ch_type == target_ch_type;
  494. if (tb->has_coeffs && do_itx) {
  495. if (cu->bdpcm_flag[tb->c_idx])
  496. transform_bdpcm(tb, lc, cu);
  497. dequant(lc, tu, tb);
  498. if (!tb->ts) {
  499. enum VVCTxType trh, trv;
  500. if (cu->apply_lfnst_flag[c_idx])
  501. ilfnst_transform(lc, tb);
  502. derive_transform_type(fc, lc, tb, &trh, &trv);
  503. if (tb->tb_width > 1 && tb->tb_height > 1)
  504. itx_2d(fc, tb, trh, trv);
  505. else
  506. itx_1d(fc, tb, trh, trv);
  507. }
  508. lmcs_scale_chroma(lc, tu, tb, target_ch_type);
  509. }
  510. }
  511. if (is_act_luma) {
  512. fc->vvcdsp.itx.adaptive_color_transform(
  513. tbs[LUMA].coeffs, tbs[CB].coeffs, tbs[CR].coeffs,
  514. tbs[LUMA].tb_width, tbs[LUMA].tb_height);
  515. }
  516. add_residual(lc, tu, target_ch_type);
  517. }
  518. static int reconstruct(VVCLocalContext *lc)
  519. {
  520. VVCFrameContext *fc = lc->fc;
  521. CodingUnit *cu = lc->cu;
  522. const int start = cu->tree_type == DUAL_TREE_CHROMA;
  523. const int end = fc->ps.sps->r->sps_chroma_format_idc && (cu->tree_type != DUAL_TREE_LUMA);
  524. for (int ch_type = start; ch_type <= end; ch_type++) {
  525. TransformUnit *tu = cu->tus.head;
  526. for (int i = 0; tu; i++) {
  527. predict_intra(lc, tu, i, ch_type);
  528. itransform(lc, tu, ch_type);
  529. tu = tu->next;
  530. }
  531. }
  532. return 0;
  533. }
  534. #define IBC_POS(c_idx, x, y) \
  535. (fc->tab.ibc_vir_buf[c_idx] + \
  536. (x << ps) + (y + ((cu->y0 & ~(sps->ctb_size_y - 1)) >> vs)) * ibc_stride)
  537. #define IBC_X(x) ((x) & ((fc->tab.sz.ibc_buffer_width >> hs) - 1))
  538. #define IBC_Y(y) ((y) & ((1 << sps->ctb_log2_size_y >> vs) - 1))
  539. static void intra_block_copy(const VVCLocalContext *lc, const int c_idx)
  540. {
  541. const CodingUnit *cu = lc->cu;
  542. const PredictionUnit *pu = &cu->pu;
  543. const VVCFrameContext *fc = lc->fc;
  544. const VVCSPS *sps = fc->ps.sps;
  545. const Mv *bv = &pu->mi.mv[L0][0];
  546. const int hs = sps->hshift[c_idx];
  547. const int vs = sps->vshift[c_idx];
  548. const int ps = sps->pixel_shift;
  549. const int ref_x = IBC_X((cu->x0 >> hs) + (bv->x >> (4 + hs)));
  550. const int ref_y = IBC_Y((cu->y0 >> vs) + (bv->y >> (4 + vs)));
  551. const int w = cu->cb_width >> hs;
  552. const int h = cu->cb_height >> vs;
  553. const int ibc_buf_width = fc->tab.sz.ibc_buffer_width >> hs; ///< IbcBufWidthY and IbcBufWidthC
  554. const int rw = FFMIN(w, ibc_buf_width - ref_x);
  555. const int ibc_stride = ibc_buf_width << ps;
  556. const int dst_stride = fc->frame->linesize[c_idx];
  557. const uint8_t *ibc_buf = IBC_POS(c_idx, ref_x, ref_y);
  558. uint8_t *dst = POS(c_idx, cu->x0, cu->y0);
  559. av_image_copy_plane(dst, dst_stride, ibc_buf, ibc_stride, rw << ps, h);
  560. if (w > rw) {
  561. //wrap around, left part
  562. ibc_buf = IBC_POS(c_idx, 0, ref_y);
  563. dst += rw << ps;
  564. av_image_copy_plane(dst, dst_stride, ibc_buf, ibc_stride, (w - rw) << ps, h);
  565. }
  566. }
  567. static void vvc_predict_ibc(const VVCLocalContext *lc)
  568. {
  569. const H266RawSPS *rsps = lc->fc->ps.sps->r;
  570. intra_block_copy(lc, LUMA);
  571. if (lc->cu->tree_type == SINGLE_TREE && rsps->sps_chroma_format_idc) {
  572. intra_block_copy(lc, CB);
  573. intra_block_copy(lc, CR);
  574. }
  575. }
  576. static void ibc_fill_vir_buf(const VVCLocalContext *lc, const CodingUnit *cu)
  577. {
  578. const VVCFrameContext *fc = lc->fc;
  579. const VVCSPS *sps = fc->ps.sps;
  580. int start, end;
  581. ff_vvc_channel_range(&start, &end, cu->tree_type, sps->r->sps_chroma_format_idc);
  582. for (int c_idx = start; c_idx < end; c_idx++) {
  583. const int hs = sps->hshift[c_idx];
  584. const int vs = sps->vshift[c_idx];
  585. const int ps = sps->pixel_shift;
  586. const int x = IBC_X(cu->x0 >> hs);
  587. const int y = IBC_Y(cu->y0 >> vs);
  588. const int src_stride = fc->frame->linesize[c_idx];
  589. const int ibc_stride = fc->tab.sz.ibc_buffer_width >> hs << ps;
  590. const uint8_t *src = POS(c_idx, cu->x0, cu->y0);
  591. uint8_t *ibc_buf = IBC_POS(c_idx, x, y);
  592. av_image_copy_plane(ibc_buf, ibc_stride, src, src_stride, cu->cb_width >> hs << ps , cu->cb_height >> vs);
  593. }
  594. }
  595. int ff_vvc_palette_derive_scale(VVCLocalContext *lc, const TransformUnit *tu, TransformBlock *tb)
  596. {
  597. const VVCSPS *sps = lc->fc->ps.sps;
  598. const int qp_prime_ts_min = 4 + 6 * sps->r->sps_min_qp_prime_ts;
  599. int qp;
  600. derive_qp(lc, tu, tb);
  601. qp = FFMAX(qp_prime_ts_min, tb->qp);
  602. return level_scale[0][rem6[qp]] << div6[qp];
  603. }
  604. // 8.4.5.3 Decoding process for palette mode
  605. static void vvc_predict_palette(VVCLocalContext *lc)
  606. {
  607. const VVCFrameContext *fc = lc->fc;
  608. const CodingUnit *cu = lc->cu;
  609. TransformUnit *tu = cu->tus.head;
  610. const VVCSPS *sps = fc->ps.sps;
  611. const int ps = sps->pixel_shift;
  612. for (int i = 0; i < tu->nb_tbs; i++) {
  613. TransformBlock *tb = &tu->tbs[i];
  614. const int c_idx = tb->c_idx;
  615. const int w = tb->tb_width;
  616. const int h = tb->tb_height;
  617. const ptrdiff_t stride = fc->frame->linesize[c_idx];
  618. uint8_t *dst = POS(c_idx, cu->x0, cu->y0);
  619. av_image_copy_plane(dst, stride, (uint8_t*)tb->coeffs, w << ps, w << ps, h);
  620. }
  621. }
  622. int ff_vvc_reconstruct(VVCLocalContext *lc, const int rs, const int rx, const int ry)
  623. {
  624. const VVCFrameContext *fc = lc->fc;
  625. const VVCSPS *sps = fc->ps.sps;
  626. const int x_ctb = rx << sps->ctb_log2_size_y;
  627. const int y_ctb = ry << sps->ctb_log2_size_y;
  628. CodingUnit *cu = fc->tab.cus[rs];
  629. int ret = 0;
  630. lc->num_ras[0] = lc->num_ras[1] = 0;
  631. lc->lmcs.x_vpdu = -1;
  632. lc->lmcs.y_vpdu = -1;
  633. ff_vvc_decode_neighbour(lc, x_ctb, y_ctb, rx, ry, rs);
  634. while (cu) {
  635. lc->cu = cu;
  636. if (cu->ciip_flag)
  637. ff_vvc_predict_ciip(lc);
  638. else if (cu->pred_mode == MODE_IBC)
  639. vvc_predict_ibc(lc);
  640. else if (cu->pred_mode == MODE_PLT)
  641. vvc_predict_palette(lc);
  642. if (cu->coded_flag) {
  643. ret = reconstruct(lc);
  644. } else {
  645. if (cu->tree_type != DUAL_TREE_CHROMA)
  646. add_reconstructed_area(lc, LUMA, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
  647. if (sps->r->sps_chroma_format_idc && cu->tree_type != DUAL_TREE_LUMA)
  648. add_reconstructed_area(lc, CHROMA, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
  649. }
  650. if (sps->r->sps_ibc_enabled_flag)
  651. ibc_fill_vir_buf(lc, cu);
  652. cu = cu->next;
  653. }
  654. ff_vvc_ctu_free_cus(fc->tab.cus + rs);
  655. return ret;
  656. }