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.

746 lines
32KB

  1. /*
  2. * HEVC video decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. * Copyright (C) 2013 Seppo Tomperi
  6. * Copyright (C) 2013 Wassim Hamidouche
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/common.h"
  25. #include "libavutil/internal.h"
  26. #include "cabac_functions.h"
  27. #include "hevcdec.h"
  28. #define LUMA 0
  29. #define CB 1
  30. #define CR 2
  31. static const uint8_t tctable[54] = {
  32. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // QP 0...18
  33. 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, // QP 19...37
  34. 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 24 // QP 38...53
  35. };
  36. static const uint8_t betatable[52] = {
  37. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, // QP 0...18
  38. 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, // QP 19...37
  39. 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64 // QP 38...51
  40. };
  41. static int chroma_tc(HEVCContext *s, int qp_y, int c_idx, int tc_offset)
  42. {
  43. static const int qp_c[] = {
  44. 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
  45. };
  46. int qp, qp_i, offset, idxt;
  47. // slice qp offset is not used for deblocking
  48. if (c_idx == 1)
  49. offset = s->ps.pps->cb_qp_offset;
  50. else
  51. offset = s->ps.pps->cr_qp_offset;
  52. qp_i = av_clip(qp_y + offset, 0, 57);
  53. if (qp_i < 30)
  54. qp = qp_i;
  55. else if (qp_i > 43)
  56. qp = qp_i - 6;
  57. else
  58. qp = qp_c[qp_i - 30];
  59. idxt = av_clip(qp + DEFAULT_INTRA_TC_OFFSET + tc_offset, 0, 53);
  60. return tctable[idxt];
  61. }
  62. static int get_qPy_pred(HEVCContext *s, int xC, int yC,
  63. int xBase, int yBase, int log2_cb_size)
  64. {
  65. HEVCLocalContext *lc = &s->HEVClc;
  66. int ctb_size_mask = (1 << s->ps.sps->log2_ctb_size) - 1;
  67. int MinCuQpDeltaSizeMask = (1 << (s->ps.sps->log2_ctb_size -
  68. s->ps.pps->diff_cu_qp_delta_depth)) - 1;
  69. int xQgBase = xBase - (xBase & MinCuQpDeltaSizeMask);
  70. int yQgBase = yBase - (yBase & MinCuQpDeltaSizeMask);
  71. int min_cb_width = s->ps.sps->min_cb_width;
  72. int min_cb_height = s->ps.sps->min_cb_height;
  73. int x_cb = xQgBase >> s->ps.sps->log2_min_cb_size;
  74. int y_cb = yQgBase >> s->ps.sps->log2_min_cb_size;
  75. int availableA = (xBase & ctb_size_mask) &&
  76. (xQgBase & ctb_size_mask);
  77. int availableB = (yBase & ctb_size_mask) &&
  78. (yQgBase & ctb_size_mask);
  79. int qPy_pred, qPy_a, qPy_b;
  80. // qPy_pred
  81. if (lc->first_qp_group || (!xQgBase && !yQgBase)) {
  82. lc->first_qp_group = !lc->tu.is_cu_qp_delta_coded;
  83. qPy_pred = s->sh.slice_qp;
  84. } else {
  85. qPy_pred = lc->qp_y;
  86. if (log2_cb_size < s->ps.sps->log2_ctb_size -
  87. s->ps.pps->diff_cu_qp_delta_depth) {
  88. static const int offsetX[8][8] = {
  89. { -1, 1, 3, 1, 7, 1, 3, 1 },
  90. { 0, 0, 0, 0, 0, 0, 0, 0 },
  91. { 1, 3, 1, 3, 1, 3, 1, 3 },
  92. { 2, 2, 2, 2, 2, 2, 2, 2 },
  93. { 3, 5, 7, 5, 3, 5, 7, 5 },
  94. { 4, 4, 4, 4, 4, 4, 4, 4 },
  95. { 5, 7, 5, 7, 5, 7, 5, 7 },
  96. { 6, 6, 6, 6, 6, 6, 6, 6 }
  97. };
  98. static const int offsetY[8][8] = {
  99. { 7, 0, 1, 2, 3, 4, 5, 6 },
  100. { 0, 1, 2, 3, 4, 5, 6, 7 },
  101. { 1, 0, 3, 2, 5, 4, 7, 6 },
  102. { 0, 1, 2, 3, 4, 5, 6, 7 },
  103. { 3, 0, 1, 2, 7, 4, 5, 6 },
  104. { 0, 1, 2, 3, 4, 5, 6, 7 },
  105. { 1, 0, 3, 2, 5, 4, 7, 6 },
  106. { 0, 1, 2, 3, 4, 5, 6, 7 }
  107. };
  108. int xC0b = (xC - (xC & ctb_size_mask)) >> s->ps.sps->log2_min_cb_size;
  109. int yC0b = (yC - (yC & ctb_size_mask)) >> s->ps.sps->log2_min_cb_size;
  110. int idxX = (xQgBase & ctb_size_mask) >> s->ps.sps->log2_min_cb_size;
  111. int idxY = (yQgBase & ctb_size_mask) >> s->ps.sps->log2_min_cb_size;
  112. int idx_mask = ctb_size_mask >> s->ps.sps->log2_min_cb_size;
  113. int x, y;
  114. x = FFMIN(xC0b + offsetX[idxX][idxY], min_cb_width - 1);
  115. y = FFMIN(yC0b + (offsetY[idxX][idxY] & idx_mask), min_cb_height - 1);
  116. if (xC0b == (lc->start_of_tiles_x >> s->ps.sps->log2_min_cb_size) &&
  117. offsetX[idxX][idxY] == -1) {
  118. x = (lc->end_of_tiles_x >> s->ps.sps->log2_min_cb_size) - 1;
  119. y = yC0b - 1;
  120. }
  121. qPy_pred = s->qp_y_tab[y * min_cb_width + x];
  122. }
  123. }
  124. // qPy_a
  125. if (availableA == 0)
  126. qPy_a = qPy_pred;
  127. else
  128. qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
  129. // qPy_b
  130. if (availableB == 0)
  131. qPy_b = qPy_pred;
  132. else
  133. qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
  134. return (qPy_a + qPy_b + 1) >> 1;
  135. }
  136. void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC,
  137. int xBase, int yBase, int log2_cb_size)
  138. {
  139. int qp_y = get_qPy_pred(s, xC, yC, xBase, yBase, log2_cb_size);
  140. if (s->HEVClc.tu.cu_qp_delta != 0) {
  141. int off = s->ps.sps->qp_bd_offset;
  142. s->HEVClc.qp_y = FFUMOD(qp_y + s->HEVClc.tu.cu_qp_delta + 52 + 2 * off,
  143. 52 + off) - off;
  144. } else
  145. s->HEVClc.qp_y = qp_y;
  146. }
  147. static int get_qPy(HEVCContext *s, int xC, int yC)
  148. {
  149. int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
  150. int x = xC >> log2_min_cb_size;
  151. int y = yC >> log2_min_cb_size;
  152. return s->qp_y_tab[x + y * s->ps.sps->min_cb_width];
  153. }
  154. static void copy_CTB(uint8_t *dst, uint8_t *src,
  155. int width, int height, ptrdiff_t stride)
  156. {
  157. int i;
  158. for (i = 0; i < height; i++) {
  159. memcpy(dst, src, width);
  160. dst += stride;
  161. src += stride;
  162. }
  163. }
  164. #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
  165. static void sao_filter_CTB(HEVCContext *s, int x, int y)
  166. {
  167. // TODO: This should be easily parallelizable
  168. // TODO: skip CBs when (cu_transquant_bypass_flag || (pcm_loop_filter_disable_flag && pcm_flag))
  169. int c_idx = 0;
  170. int class = 1, class_index;
  171. int edges[4]; // 0 left 1 top 2 right 3 bottom
  172. SAOParams *sao[4];
  173. int classes[4];
  174. int x_shift = 0, y_shift = 0;
  175. int x_ctb = x >> s->ps.sps->log2_ctb_size;
  176. int y_ctb = y >> s->ps.sps->log2_ctb_size;
  177. int ctb_addr_rs = y_ctb * s->ps.sps->ctb_width + x_ctb;
  178. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
  179. // flags indicating unfilterable edges
  180. uint8_t vert_edge[] = { 0, 0, 0, 0 };
  181. uint8_t horiz_edge[] = { 0, 0, 0, 0 };
  182. uint8_t diag_edge[] = { 0, 0, 0, 0 };
  183. uint8_t lfase[3]; // current, above, left
  184. uint8_t no_tile_filter = s->ps.pps->tiles_enabled_flag &&
  185. !s->ps.pps->loop_filter_across_tiles_enabled_flag;
  186. uint8_t left_tile_edge = 0, up_tile_edge = 0;
  187. sao[0] = &CTB(s->sao, x_ctb, y_ctb);
  188. edges[0] = x_ctb == 0;
  189. edges[1] = y_ctb == 0;
  190. edges[2] = x_ctb == s->ps.sps->ctb_width - 1;
  191. edges[3] = y_ctb == s->ps.sps->ctb_height - 1;
  192. lfase[0] = CTB(s->filter_slice_edges, x_ctb, y_ctb);
  193. classes[0] = 0;
  194. if (!edges[0]) {
  195. left_tile_edge = no_tile_filter && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]];
  196. sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb);
  197. vert_edge[0] = (!lfase[0] && CTB(s->tab_slice_address, x_ctb, y_ctb) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb)) || left_tile_edge;
  198. vert_edge[2] = vert_edge[0];
  199. lfase[2] = CTB(s->filter_slice_edges, x_ctb - 1, y_ctb);
  200. classes[class] = 2;
  201. class++;
  202. x_shift = 8;
  203. }
  204. if (!edges[1]) {
  205. up_tile_edge = no_tile_filter && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]];
  206. sao[class] = &CTB(s->sao, x_ctb, y_ctb - 1);
  207. horiz_edge[0] = (!lfase[0] && CTB(s->tab_slice_address, x_ctb, y_ctb) != CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) || up_tile_edge;
  208. horiz_edge[1] = horiz_edge[0];
  209. lfase[1] = CTB(s->filter_slice_edges, x_ctb, y_ctb - 1);
  210. classes[class] = 1;
  211. class++;
  212. y_shift = 4;
  213. if (!edges[0]) {
  214. classes[class] = 3;
  215. sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb - 1);
  216. class++;
  217. // Tile check here is done current CTB row/col, not above/left like you'd expect,
  218. //but that is because the tile boundary always extends through the whole pic
  219. vert_edge[1] = (!lfase[1] && CTB(s->tab_slice_address, x_ctb, y_ctb - 1) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb - 1)) || left_tile_edge;
  220. vert_edge[3] = vert_edge[1];
  221. horiz_edge[2] = (!lfase[2] && CTB(s->tab_slice_address, x_ctb - 1, y_ctb) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb - 1)) || up_tile_edge;
  222. horiz_edge[3] = horiz_edge[2];
  223. diag_edge[0] = (!lfase[0] && CTB(s->tab_slice_address, x_ctb, y_ctb) != CTB(s->tab_slice_address, x_ctb - 1, y_ctb - 1)) || left_tile_edge || up_tile_edge;
  224. diag_edge[3] = diag_edge[0];
  225. // Does left CTB comes after above CTB?
  226. if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) >
  227. CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
  228. diag_edge[2] = !lfase[2] || left_tile_edge || up_tile_edge;
  229. diag_edge[1] = diag_edge[2];
  230. } else if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) <
  231. CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
  232. diag_edge[1] = !lfase[1] || left_tile_edge || up_tile_edge;
  233. diag_edge[2] = diag_edge[1];
  234. } else {
  235. // Same slice, only consider tiles
  236. diag_edge[2] = left_tile_edge || up_tile_edge;
  237. diag_edge[1] = diag_edge[2];
  238. }
  239. }
  240. }
  241. for (c_idx = 0; c_idx < 3; c_idx++) {
  242. int chroma = c_idx ? 1 : 0;
  243. int x0 = x >> chroma;
  244. int y0 = y >> chroma;
  245. ptrdiff_t stride = s->frame->linesize[c_idx];
  246. int ctb_size = (1 << (s->ps.sps->log2_ctb_size)) >> s->ps.sps->hshift[c_idx];
  247. int width = FFMIN(ctb_size,
  248. (s->ps.sps->width >> s->ps.sps->hshift[c_idx]) - x0);
  249. int height = FFMIN(ctb_size,
  250. (s->ps.sps->height >> s->ps.sps->vshift[c_idx]) - y0);
  251. uint8_t *src = &s->frame->data[c_idx][y0 * stride + (x0 << s->ps.sps->pixel_shift)];
  252. uint8_t *dst = &s->sao_frame->data[c_idx][y0 * stride + (x0 << s->ps.sps->pixel_shift)];
  253. int offset = (y_shift >> chroma) * stride + ((x_shift >> chroma) << s->ps.sps->pixel_shift);
  254. copy_CTB(dst - offset, src - offset,
  255. (edges[2] ? width + (x_shift >> chroma) : width) << s->ps.sps->pixel_shift,
  256. (edges[3] ? height + (y_shift >> chroma) : height), stride);
  257. for (class_index = 0; class_index < class; class_index++) {
  258. switch (sao[class_index]->type_idx[c_idx]) {
  259. case SAO_BAND:
  260. s->hevcdsp.sao_band_filter[classes[class_index]](dst, src,
  261. stride,
  262. sao[class_index],
  263. edges, width,
  264. height, c_idx);
  265. break;
  266. case SAO_EDGE:
  267. s->hevcdsp.sao_edge_filter[classes[class_index]](dst, src,
  268. stride,
  269. sao[class_index],
  270. edges, width,
  271. height, c_idx,
  272. vert_edge[classes[class_index]],
  273. horiz_edge[classes[class_index]],
  274. diag_edge[classes[class_index]]);
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. static int get_pcm(HEVCContext *s, int x, int y)
  281. {
  282. int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
  283. int x_pu, y_pu;
  284. if (x < 0 || y < 0)
  285. return 2;
  286. x_pu = x >> log2_min_pu_size;
  287. y_pu = y >> log2_min_pu_size;
  288. if (x_pu >= s->ps.sps->min_pu_width || y_pu >= s->ps.sps->min_pu_height)
  289. return 2;
  290. return s->is_pcm[y_pu * s->ps.sps->min_pu_width + x_pu];
  291. }
  292. #define TC_CALC(qp, bs) \
  293. tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) + \
  294. (tc_offset >> 1 << 1), \
  295. 0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
  296. static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
  297. {
  298. uint8_t *src;
  299. int x, y, x_end, y_end, chroma;
  300. int c_tc[2], tc[2], beta;
  301. uint8_t no_p[2] = { 0 };
  302. uint8_t no_q[2] = { 0 };
  303. int log2_ctb_size = s->ps.sps->log2_ctb_size;
  304. int ctb_size = 1 << log2_ctb_size;
  305. int ctb = (x0 >> log2_ctb_size) +
  306. (y0 >> log2_ctb_size) * s->ps.sps->ctb_width;
  307. int cur_tc_offset = s->deblock[ctb].tc_offset;
  308. int cur_beta_offset = s->deblock[ctb].beta_offset;
  309. int tc_offset, left_tc_offset, beta_offset, left_beta_offset;
  310. int pcmf = (s->ps.sps->pcm_enabled_flag &&
  311. s->ps.sps->pcm.loop_filter_disable_flag) ||
  312. s->ps.pps->transquant_bypass_enable_flag;
  313. if (x0) {
  314. left_tc_offset = s->deblock[ctb - 1].tc_offset;
  315. left_beta_offset = s->deblock[ctb - 1].beta_offset;
  316. }
  317. x_end = x0 + ctb_size;
  318. if (x_end > s->ps.sps->width)
  319. x_end = s->ps.sps->width;
  320. y_end = y0 + ctb_size;
  321. if (y_end > s->ps.sps->height)
  322. y_end = s->ps.sps->height;
  323. tc_offset = cur_tc_offset;
  324. beta_offset = cur_beta_offset;
  325. // vertical filtering luma
  326. for (y = y0; y < y_end; y += 8) {
  327. for (x = x0 ? x0 : 8; x < x_end; x += 8) {
  328. const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
  329. const int bs1 = s->vertical_bs[(x >> 3) + ((y + 4) >> 2) * s->bs_width];
  330. if (bs0 || bs1) {
  331. const int qp = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
  332. beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
  333. tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
  334. tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
  335. src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->ps.sps->pixel_shift)];
  336. if (pcmf) {
  337. no_p[0] = get_pcm(s, x - 1, y);
  338. no_p[1] = get_pcm(s, x - 1, y + 4);
  339. no_q[0] = get_pcm(s, x, y);
  340. no_q[1] = get_pcm(s, x, y + 4);
  341. s->hevcdsp.hevc_v_loop_filter_luma_c(src,
  342. s->frame->linesize[LUMA],
  343. beta, tc, no_p, no_q);
  344. } else
  345. s->hevcdsp.hevc_v_loop_filter_luma(src,
  346. s->frame->linesize[LUMA],
  347. beta, tc, no_p, no_q);
  348. }
  349. }
  350. }
  351. // vertical filtering chroma
  352. for (chroma = 1; chroma <= 2; chroma++) {
  353. for (y = y0; y < y_end; y += 16) {
  354. for (x = x0 ? x0 : 16; x < x_end; x += 16) {
  355. const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
  356. const int bs1 = s->vertical_bs[(x >> 3) + ((y + 8) >> 2) * s->bs_width];
  357. if ((bs0 == 2) || (bs1 == 2)) {
  358. const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
  359. const int qp1 = (get_qPy(s, x - 1, y + 8) + get_qPy(s, x, y + 8) + 1) >> 1;
  360. c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
  361. c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
  362. src = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->ps.sps->pixel_shift)];
  363. if (pcmf) {
  364. no_p[0] = get_pcm(s, x - 1, y);
  365. no_p[1] = get_pcm(s, x - 1, y + 8);
  366. no_q[0] = get_pcm(s, x, y);
  367. no_q[1] = get_pcm(s, x, y + 8);
  368. s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
  369. s->frame->linesize[chroma],
  370. c_tc, no_p, no_q);
  371. } else
  372. s->hevcdsp.hevc_v_loop_filter_chroma(src,
  373. s->frame->linesize[chroma],
  374. c_tc, no_p, no_q);
  375. }
  376. }
  377. }
  378. }
  379. // horizontal filtering luma
  380. if (x_end != s->ps.sps->width)
  381. x_end -= 8;
  382. for (y = y0 ? y0 : 8; y < y_end; y += 8) {
  383. for (x = x0 ? x0 - 8 : 0; x < x_end; x += 8) {
  384. const int bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
  385. const int bs1 = s->horizontal_bs[(x + 4 + y * s->bs_width) >> 2];
  386. if (bs0 || bs1) {
  387. const int qp = (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1;
  388. tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
  389. beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
  390. beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
  391. tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
  392. tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
  393. src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->ps.sps->pixel_shift)];
  394. if (pcmf) {
  395. no_p[0] = get_pcm(s, x, y - 1);
  396. no_p[1] = get_pcm(s, x + 4, y - 1);
  397. no_q[0] = get_pcm(s, x, y);
  398. no_q[1] = get_pcm(s, x + 4, y);
  399. s->hevcdsp.hevc_h_loop_filter_luma_c(src,
  400. s->frame->linesize[LUMA],
  401. beta, tc, no_p, no_q);
  402. } else
  403. s->hevcdsp.hevc_h_loop_filter_luma(src,
  404. s->frame->linesize[LUMA],
  405. beta, tc, no_p, no_q);
  406. }
  407. }
  408. }
  409. // horizontal filtering chroma
  410. for (chroma = 1; chroma <= 2; chroma++) {
  411. for (y = y0 ? y0 : 16; y < y_end; y += 16) {
  412. for (x = x0 - 8; x < x_end; x += 16) {
  413. int bs0, bs1;
  414. // to make sure no memory access over boundary when x = -8
  415. // TODO: simplify with row based deblocking
  416. if (x < 0) {
  417. bs0 = 0;
  418. bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
  419. } else if (x >= x_end - 8) {
  420. bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
  421. bs1 = 0;
  422. } else {
  423. bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
  424. bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
  425. }
  426. if ((bs0 == 2) || (bs1 == 2)) {
  427. const int qp0 = bs0 == 2 ? (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1 : 0;
  428. const int qp1 = bs1 == 2 ? (get_qPy(s, x + 8, y - 1) + get_qPy(s, x + 8, y) + 1) >> 1 : 0;
  429. tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
  430. c_tc[0] = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
  431. c_tc[1] = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
  432. src = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->ps.sps->pixel_shift)];
  433. if (pcmf) {
  434. no_p[0] = get_pcm(s, x, y - 1);
  435. no_p[1] = get_pcm(s, x + 8, y - 1);
  436. no_q[0] = get_pcm(s, x, y);
  437. no_q[1] = get_pcm(s, x + 8, y);
  438. s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
  439. s->frame->linesize[chroma],
  440. c_tc, no_p, no_q);
  441. } else
  442. s->hevcdsp.hevc_h_loop_filter_chroma(src,
  443. s->frame->linesize[chroma],
  444. c_tc, no_p, no_q);
  445. }
  446. }
  447. }
  448. }
  449. }
  450. static int boundary_strength(HEVCContext *s, MvField *curr,
  451. uint8_t curr_cbf_luma, MvField *neigh,
  452. uint8_t neigh_cbf_luma,
  453. RefPicList *neigh_refPicList,
  454. int tu_border)
  455. {
  456. int mvs = curr->pred_flag[0] + curr->pred_flag[1];
  457. if (tu_border) {
  458. if (curr->is_intra || neigh->is_intra)
  459. return 2;
  460. if (curr_cbf_luma || neigh_cbf_luma)
  461. return 1;
  462. }
  463. if (mvs == neigh->pred_flag[0] + neigh->pred_flag[1]) {
  464. if (mvs == 2) {
  465. // same L0 and L1
  466. if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]] &&
  467. s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
  468. neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
  469. if ((abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
  470. abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
  471. (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
  472. abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4))
  473. return 1;
  474. else
  475. return 0;
  476. } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
  477. neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
  478. if (abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
  479. abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4)
  480. return 1;
  481. else
  482. return 0;
  483. } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
  484. neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
  485. if (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
  486. abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4)
  487. return 1;
  488. else
  489. return 0;
  490. } else {
  491. return 1;
  492. }
  493. } else { // 1 MV
  494. Mv A, B;
  495. int ref_A, ref_B;
  496. if (curr->pred_flag[0]) {
  497. A = curr->mv[0];
  498. ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
  499. } else {
  500. A = curr->mv[1];
  501. ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
  502. }
  503. if (neigh->pred_flag[0]) {
  504. B = neigh->mv[0];
  505. ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
  506. } else {
  507. B = neigh->mv[1];
  508. ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
  509. }
  510. if (ref_A == ref_B) {
  511. if (abs(A.x - B.x) >= 4 || abs(A.y - B.y) >= 4)
  512. return 1;
  513. else
  514. return 0;
  515. } else
  516. return 1;
  517. }
  518. }
  519. return 1;
  520. }
  521. void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
  522. int log2_trafo_size)
  523. {
  524. HEVCLocalContext *lc = &s->HEVClc;
  525. MvField *tab_mvf = s->ref->tab_mvf;
  526. int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
  527. int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
  528. int min_pu_width = s->ps.sps->min_pu_width;
  529. int min_tu_width = s->ps.sps->min_tb_width;
  530. int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
  531. (x0 >> log2_min_pu_size)].is_intra;
  532. int boundary_upper, boundary_left;
  533. int i, j, bs;
  534. boundary_upper = y0 > 0 && !(y0 & 7);
  535. if (boundary_upper &&
  536. ((!s->sh.slice_loop_filter_across_slices_enabled_flag &&
  537. lc->boundary_flags & BOUNDARY_UPPER_SLICE &&
  538. (y0 % (1 << s->ps.sps->log2_ctb_size)) == 0) ||
  539. (!s->ps.pps->loop_filter_across_tiles_enabled_flag &&
  540. lc->boundary_flags & BOUNDARY_UPPER_TILE &&
  541. (y0 % (1 << s->ps.sps->log2_ctb_size)) == 0)))
  542. boundary_upper = 0;
  543. if (boundary_upper) {
  544. RefPicList *rpl_top = (lc->boundary_flags & BOUNDARY_UPPER_SLICE) ?
  545. ff_hevc_get_ref_list(s, s->ref, x0, y0 - 1) :
  546. s->ref->refPicList;
  547. int yp_pu = (y0 - 1) >> log2_min_pu_size;
  548. int yq_pu = y0 >> log2_min_pu_size;
  549. int yp_tu = (y0 - 1) >> log2_min_tu_size;
  550. int yq_tu = y0 >> log2_min_tu_size;
  551. for (i = 0; i < (1 << log2_trafo_size); i += 4) {
  552. int x_pu = (x0 + i) >> log2_min_pu_size;
  553. int x_tu = (x0 + i) >> log2_min_tu_size;
  554. MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
  555. MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
  556. uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
  557. uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
  558. bs = boundary_strength(s, curr, curr_cbf_luma,
  559. top, top_cbf_luma, rpl_top, 1);
  560. if (bs)
  561. s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
  562. }
  563. }
  564. // bs for TU internal horizontal PU boundaries
  565. if (log2_trafo_size > s->ps.sps->log2_min_pu_size && !is_intra) {
  566. RefPicList *rpl = s->ref->refPicList;
  567. for (j = 8; j < (1 << log2_trafo_size); j += 8) {
  568. int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
  569. int yq_pu = (y0 + j) >> log2_min_pu_size;
  570. int yp_tu = (y0 + j - 1) >> log2_min_tu_size;
  571. int yq_tu = (y0 + j) >> log2_min_tu_size;
  572. for (i = 0; i < (1 << log2_trafo_size); i += 4) {
  573. int x_pu = (x0 + i) >> log2_min_pu_size;
  574. int x_tu = (x0 + i) >> log2_min_tu_size;
  575. MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
  576. MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
  577. uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
  578. uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
  579. bs = boundary_strength(s, curr, curr_cbf_luma,
  580. top, top_cbf_luma, rpl, 0);
  581. if (bs)
  582. s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
  583. }
  584. }
  585. }
  586. // bs for vertical TU boundaries
  587. boundary_left = x0 > 0 && !(x0 & 7);
  588. if (boundary_left &&
  589. ((!s->sh.slice_loop_filter_across_slices_enabled_flag &&
  590. lc->boundary_flags & BOUNDARY_LEFT_SLICE &&
  591. (x0 % (1 << s->ps.sps->log2_ctb_size)) == 0) ||
  592. (!s->ps.pps->loop_filter_across_tiles_enabled_flag &&
  593. lc->boundary_flags & BOUNDARY_LEFT_TILE &&
  594. (x0 % (1 << s->ps.sps->log2_ctb_size)) == 0)))
  595. boundary_left = 0;
  596. if (boundary_left) {
  597. RefPicList *rpl_left = (lc->boundary_flags & BOUNDARY_LEFT_SLICE) ?
  598. ff_hevc_get_ref_list(s, s->ref, x0 - 1, y0) :
  599. s->ref->refPicList;
  600. int xp_pu = (x0 - 1) >> log2_min_pu_size;
  601. int xq_pu = x0 >> log2_min_pu_size;
  602. int xp_tu = (x0 - 1) >> log2_min_tu_size;
  603. int xq_tu = x0 >> log2_min_tu_size;
  604. for (i = 0; i < (1 << log2_trafo_size); i += 4) {
  605. int y_pu = (y0 + i) >> log2_min_pu_size;
  606. int y_tu = (y0 + i) >> log2_min_tu_size;
  607. MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
  608. MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
  609. uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
  610. uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
  611. bs = boundary_strength(s, curr, curr_cbf_luma,
  612. left, left_cbf_luma, rpl_left, 1);
  613. if (bs)
  614. s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
  615. }
  616. }
  617. // bs for TU internal vertical PU boundaries
  618. if (log2_trafo_size > log2_min_pu_size && !is_intra) {
  619. RefPicList *rpl = s->ref->refPicList;
  620. for (j = 0; j < (1 << log2_trafo_size); j += 4) {
  621. int y_pu = (y0 + j) >> log2_min_pu_size;
  622. int y_tu = (y0 + j) >> log2_min_tu_size;
  623. for (i = 8; i < (1 << log2_trafo_size); i += 8) {
  624. int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
  625. int xq_pu = (x0 + i) >> log2_min_pu_size;
  626. int xp_tu = (x0 + i - 1) >> log2_min_tu_size;
  627. int xq_tu = (x0 + i) >> log2_min_tu_size;
  628. MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
  629. MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
  630. uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
  631. uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
  632. bs = boundary_strength(s, curr, curr_cbf_luma,
  633. left, left_cbf_luma, rpl, 0);
  634. if (bs)
  635. s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
  636. }
  637. }
  638. }
  639. }
  640. #undef LUMA
  641. #undef CB
  642. #undef CR
  643. void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
  644. {
  645. deblocking_filter_CTB(s, x, y);
  646. if (s->ps.sps->sao_enabled)
  647. sao_filter_CTB(s, x, y);
  648. }
  649. void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
  650. {
  651. if (y_ctb && x_ctb)
  652. ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size);
  653. if (y_ctb && x_ctb >= s->ps.sps->width - ctb_size) {
  654. ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size);
  655. ff_thread_report_progress(&s->ref->tf, y_ctb - ctb_size, 0);
  656. }
  657. if (x_ctb && y_ctb >= s->ps.sps->height - ctb_size)
  658. ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb);
  659. }