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.

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