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.

716 lines
31KB

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