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.

750 lines
33KB

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