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.

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