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.

693 lines
30KB

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