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.

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