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.

909 lines
39KB

  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 xBase, int yBase, int log2_cb_size)
  69. {
  70. HEVCLocalContext *lc = s->HEVClc;
  71. int ctb_size_mask = (1 << s->sps->log2_ctb_size) - 1;
  72. int MinCuQpDeltaSizeMask = (1 << (s->sps->log2_ctb_size -
  73. s->pps->diff_cu_qp_delta_depth)) - 1;
  74. int xQgBase = xBase - (xBase & MinCuQpDeltaSizeMask);
  75. int yQgBase = yBase - (yBase & MinCuQpDeltaSizeMask);
  76. int min_cb_width = s->sps->min_cb_width;
  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, qPy_a, qPy_b;
  84. // qPy_pred
  85. if (lc->first_qp_group || (!xQgBase && !yQgBase)) {
  86. lc->first_qp_group = !lc->tu.is_cu_qp_delta_coded;
  87. qPy_pred = s->sh.slice_qp;
  88. } else {
  89. qPy_pred = lc->qPy_pred;
  90. }
  91. // qPy_a
  92. if (availableA == 0)
  93. qPy_a = qPy_pred;
  94. else
  95. qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
  96. // qPy_b
  97. if (availableB == 0)
  98. qPy_b = qPy_pred;
  99. else
  100. qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
  101. av_assert2(qPy_a >= -s->sps->qp_bd_offset && qPy_a < 52);
  102. av_assert2(qPy_b >= -s->sps->qp_bd_offset && qPy_b < 52);
  103. return (qPy_a + qPy_b + 1) >> 1;
  104. }
  105. void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase, int log2_cb_size)
  106. {
  107. int qp_y = get_qPy_pred(s, xBase, yBase, log2_cb_size);
  108. if (s->HEVClc->tu.cu_qp_delta != 0) {
  109. int off = s->sps->qp_bd_offset;
  110. s->HEVClc->qp_y = FFUMOD(qp_y + s->HEVClc->tu.cu_qp_delta + 52 + 2 * off,
  111. 52 + off) - off;
  112. } else
  113. s->HEVClc->qp_y = qp_y;
  114. }
  115. static int get_qPy(HEVCContext *s, int xC, int yC)
  116. {
  117. int log2_min_cb_size = s->sps->log2_min_cb_size;
  118. int x = xC >> log2_min_cb_size;
  119. int y = yC >> log2_min_cb_size;
  120. return s->qp_y_tab[x + y * s->sps->min_cb_width];
  121. }
  122. static void copy_CTB(uint8_t *dst, const uint8_t *src, int width, int height,
  123. intptr_t stride_dst, intptr_t stride_src)
  124. {
  125. int i, j;
  126. if (((intptr_t)dst | (intptr_t)src | stride_dst | stride_src) & 15) {
  127. for (i = 0; i < height; i++) {
  128. for (j = 0; j < width; j+=8)
  129. AV_COPY64(dst+j, src+j);
  130. dst += stride_dst;
  131. src += stride_src;
  132. }
  133. } else {
  134. for (i = 0; i < height; i++) {
  135. for (j = 0; j < width; j+=16)
  136. AV_COPY128(dst+j, src+j);
  137. dst += stride_dst;
  138. src += stride_src;
  139. }
  140. }
  141. }
  142. #if defined(USE_SAO_SMALL_BUFFER)
  143. static void copy_pixel(uint8_t *dst, const uint8_t *src, int pixel_shift)
  144. {
  145. if (pixel_shift)
  146. *(uint16_t *)dst = *(uint16_t *)src;
  147. else
  148. *dst = *src;
  149. }
  150. static void copy_vert(uint8_t *dst, const uint8_t *src,
  151. int pixel_shift, int height,
  152. int stride_dst, int stride_src)
  153. {
  154. int i;
  155. if (pixel_shift == 0) {
  156. for (i = 0; i < height; i++) {
  157. *dst = *src;
  158. dst += stride_dst;
  159. src += stride_src;
  160. }
  161. } else {
  162. for (i = 0; i < height; i++) {
  163. *(uint16_t *)dst = *(uint16_t *)src;
  164. dst += stride_dst;
  165. src += stride_src;
  166. }
  167. }
  168. }
  169. static void copy_CTB_to_hv(HEVCContext *s, const uint8_t *src,
  170. int stride_src, int x, int y, int width, int height,
  171. int c_idx, int x_ctb, int y_ctb)
  172. {
  173. int sh = s->sps->pixel_shift;
  174. int w = s->sps->width >> s->sps->hshift[c_idx];
  175. int h = s->sps->height >> s->sps->vshift[c_idx];
  176. /* copy horizontal edges */
  177. memcpy(s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb) * w + x) << sh),
  178. src, width << sh);
  179. memcpy(s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb + 1) * w + x) << sh),
  180. src + stride_src * (height - 1), width << sh);
  181. /* copy vertical edges */
  182. copy_vert(s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb) * h + y) << sh), src, sh, height, 1 << sh, stride_src);
  183. copy_vert(s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb + 1) * h + y) << sh), src + ((width - 1) << sh), sh, height, 1 << sh, stride_src);
  184. }
  185. #endif
  186. static void restore_tqb_pixels(HEVCContext *s,
  187. uint8_t *src1, const uint8_t *dst1,
  188. ptrdiff_t stride_src, ptrdiff_t stride_dst,
  189. int x0, int y0, int width, int height, int c_idx)
  190. {
  191. if ( s->pps->transquant_bypass_enable_flag ||
  192. (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) {
  193. int x, y;
  194. int min_pu_size = 1 << s->sps->log2_min_pu_size;
  195. int hshift = s->sps->hshift[c_idx];
  196. int vshift = s->sps->vshift[c_idx];
  197. int x_min = ((x0 ) >> s->sps->log2_min_pu_size);
  198. int y_min = ((y0 ) >> s->sps->log2_min_pu_size);
  199. int x_max = ((x0 + width ) >> s->sps->log2_min_pu_size);
  200. int y_max = ((y0 + height) >> s->sps->log2_min_pu_size);
  201. int len = (min_pu_size >> hshift) << s->sps->pixel_shift;
  202. for (y = y_min; y < y_max; y++) {
  203. for (x = x_min; x < x_max; x++) {
  204. if (s->is_pcm[y * s->sps->min_pu_width + x]) {
  205. int n;
  206. uint8_t *src = src1 + (((y << s->sps->log2_min_pu_size) - y0) >> vshift) * stride_src + ((((x << s->sps->log2_min_pu_size) - x0) >> hshift) << s->sps->pixel_shift);
  207. const uint8_t *dst = dst1 + (((y << s->sps->log2_min_pu_size) - y0) >> vshift) * stride_dst + ((((x << s->sps->log2_min_pu_size) - x0) >> hshift) << s->sps->pixel_shift);
  208. for (n = 0; n < (min_pu_size >> vshift); n++) {
  209. memcpy(src, dst, len);
  210. src += stride_src;
  211. dst += stride_dst;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
  219. static void sao_filter_CTB(HEVCContext *s, int x, int y)
  220. {
  221. static const uint8_t band_tab[8] = { 0, 1, 2, 2, 3, 3, 4, 4 };
  222. HEVCLocalContext *lc = s->HEVClc;
  223. int c_idx;
  224. int edges[4]; // 0 left 1 top 2 right 3 bottom
  225. int x_ctb = x >> s->sps->log2_ctb_size;
  226. int y_ctb = y >> s->sps->log2_ctb_size;
  227. int ctb_addr_rs = y_ctb * s->sps->ctb_width + x_ctb;
  228. int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
  229. SAOParams *sao = &CTB(s->sao, x_ctb, y_ctb);
  230. // flags indicating unfilterable edges
  231. uint8_t vert_edge[] = { 0, 0 };
  232. uint8_t horiz_edge[] = { 0, 0 };
  233. uint8_t diag_edge[] = { 0, 0, 0, 0 };
  234. uint8_t lfase = CTB(s->filter_slice_edges, x_ctb, y_ctb);
  235. uint8_t no_tile_filter = s->pps->tiles_enabled_flag &&
  236. !s->pps->loop_filter_across_tiles_enabled_flag;
  237. uint8_t restore = no_tile_filter || !lfase;
  238. uint8_t left_tile_edge = 0;
  239. uint8_t right_tile_edge = 0;
  240. uint8_t up_tile_edge = 0;
  241. uint8_t bottom_tile_edge = 0;
  242. edges[0] = x_ctb == 0;
  243. edges[1] = y_ctb == 0;
  244. edges[2] = x_ctb == s->sps->ctb_width - 1;
  245. edges[3] = y_ctb == s->sps->ctb_height - 1;
  246. if (restore) {
  247. if (!edges[0]) {
  248. 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]];
  249. 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;
  250. }
  251. if (!edges[2]) {
  252. 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]];
  253. 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;
  254. }
  255. if (!edges[1]) {
  256. 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]];
  257. 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;
  258. }
  259. if (!edges[3]) {
  260. 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]];
  261. 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;
  262. }
  263. if (!edges[0] && !edges[1]) {
  264. 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;
  265. }
  266. if (!edges[1] && !edges[2]) {
  267. 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;
  268. }
  269. if (!edges[2] && !edges[3]) {
  270. 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;
  271. }
  272. if (!edges[0] && !edges[3]) {
  273. 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;
  274. }
  275. }
  276. for (c_idx = 0; c_idx < (s->sps->chroma_format_idc ? 3 : 1); c_idx++) {
  277. int x0 = x >> s->sps->hshift[c_idx];
  278. int y0 = y >> s->sps->vshift[c_idx];
  279. int stride_src = s->frame->linesize[c_idx];
  280. int ctb_size_h = (1 << (s->sps->log2_ctb_size)) >> s->sps->hshift[c_idx];
  281. int ctb_size_v = (1 << (s->sps->log2_ctb_size)) >> s->sps->vshift[c_idx];
  282. int width = FFMIN(ctb_size_h, (s->sps->width >> s->sps->hshift[c_idx]) - x0);
  283. int height = FFMIN(ctb_size_v, (s->sps->height >> s->sps->vshift[c_idx]) - y0);
  284. int tab = band_tab[(FFALIGN(width, 8) >> 3) - 1];
  285. uint8_t *src = &s->frame->data[c_idx][y0 * stride_src + (x0 << s->sps->pixel_shift)];
  286. #if defined(USE_SAO_SMALL_BUFFER)
  287. int stride_dst = ((1 << (s->sps->log2_ctb_size)) + 2) << s->sps->pixel_shift;
  288. uint8_t *dst = lc->sao_pixel_buffer + (1 * stride_dst) + (1 << s->sps->pixel_shift);
  289. #else
  290. int stride_dst = s->sao_frame->linesize[c_idx];
  291. uint8_t *dst = &s->sao_frame->data[c_idx][y0 * stride_dst + (x0 << s->sps->pixel_shift)];
  292. #endif
  293. switch (sao->type_idx[c_idx]) {
  294. case SAO_BAND:
  295. copy_CTB(dst, src, width << s->sps->pixel_shift, height, stride_dst, stride_src);
  296. #if defined(USE_SAO_SMALL_BUFFER)
  297. copy_CTB_to_hv(s, src, stride_src, x0, y0, width, height, c_idx,
  298. x_ctb, y_ctb);
  299. #endif
  300. s->hevcdsp.sao_band_filter[tab](src, dst, stride_src, stride_dst,
  301. sao->offset_val[c_idx], sao->band_position[c_idx],
  302. width, height);
  303. restore_tqb_pixels(s, src, dst, stride_src, stride_dst,
  304. x, y, width, height, c_idx);
  305. sao->type_idx[c_idx] = SAO_APPLIED;
  306. break;
  307. case SAO_EDGE:
  308. {
  309. #if defined(USE_SAO_SMALL_BUFFER)
  310. int w = s->sps->width >> s->sps->hshift[c_idx];
  311. int h = s->sps->height >> s->sps->vshift[c_idx];
  312. int left_edge = edges[0];
  313. int top_edge = edges[1];
  314. int right_edge = edges[2];
  315. int bottom_edge = edges[3];
  316. int sh = s->sps->pixel_shift;
  317. int left_pixels, right_pixels;
  318. if (!top_edge) {
  319. int left = 1 - left_edge;
  320. int right = 1 - right_edge;
  321. const uint8_t *src1[2];
  322. uint8_t *dst1;
  323. int src_idx, pos;
  324. dst1 = dst - stride_dst - (left << sh);
  325. src1[0] = src - stride_src - (left << sh);
  326. src1[1] = s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb - 1) * w + x0 - left) << sh);
  327. pos = 0;
  328. if (left) {
  329. src_idx = (CTB(s->sao, x_ctb-1, y_ctb-1).type_idx[c_idx] ==
  330. SAO_APPLIED);
  331. copy_pixel(dst1, src1[src_idx], sh);
  332. pos += (1 << sh);
  333. }
  334. src_idx = (CTB(s->sao, x_ctb, y_ctb-1).type_idx[c_idx] ==
  335. SAO_APPLIED);
  336. memcpy(dst1 + pos, src1[src_idx] + pos, width << sh);
  337. if (right) {
  338. pos += width << sh;
  339. src_idx = (CTB(s->sao, x_ctb+1, y_ctb-1).type_idx[c_idx] ==
  340. SAO_APPLIED);
  341. copy_pixel(dst1 + pos, src1[src_idx] + pos, sh);
  342. }
  343. }
  344. if (!bottom_edge) {
  345. int left = 1 - left_edge;
  346. int right = 1 - right_edge;
  347. const uint8_t *src1[2];
  348. uint8_t *dst1;
  349. int src_idx, pos;
  350. dst1 = dst + height * stride_dst - (left << sh);
  351. src1[0] = src + height * stride_src - (left << sh);
  352. src1[1] = s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb + 2) * w + x0 - left) << sh);
  353. pos = 0;
  354. if (left) {
  355. src_idx = (CTB(s->sao, x_ctb-1, y_ctb+1).type_idx[c_idx] ==
  356. SAO_APPLIED);
  357. copy_pixel(dst1, src1[src_idx], sh);
  358. pos += (1 << sh);
  359. }
  360. src_idx = (CTB(s->sao, x_ctb, y_ctb+1).type_idx[c_idx] ==
  361. SAO_APPLIED);
  362. memcpy(dst1 + pos, src1[src_idx] + pos, width << sh);
  363. if (right) {
  364. pos += width << sh;
  365. src_idx = (CTB(s->sao, x_ctb+1, y_ctb+1).type_idx[c_idx] ==
  366. SAO_APPLIED);
  367. copy_pixel(dst1 + pos, src1[src_idx] + pos, sh);
  368. }
  369. }
  370. left_pixels = 0;
  371. if (!left_edge) {
  372. if (CTB(s->sao, x_ctb-1, y_ctb).type_idx[c_idx] == SAO_APPLIED) {
  373. copy_vert(dst - (1 << sh),
  374. s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb - 1) * h + y0) << sh),
  375. sh, height, stride_dst, 1 << sh);
  376. } else {
  377. left_pixels = 1;
  378. }
  379. }
  380. right_pixels = 0;
  381. if (!right_edge) {
  382. if (CTB(s->sao, x_ctb+1, y_ctb).type_idx[c_idx] == SAO_APPLIED) {
  383. copy_vert(dst + (width << sh),
  384. s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb + 2) * h + y0) << sh),
  385. sh, height, stride_dst, 1 << sh);
  386. } else {
  387. right_pixels = 1;
  388. }
  389. }
  390. copy_CTB(dst - (left_pixels << sh),
  391. src - (left_pixels << sh),
  392. (width + left_pixels + right_pixels) << sh,
  393. height, stride_dst, stride_src);
  394. copy_CTB_to_hv(s, src, stride_src, x0, y0, width, height, c_idx,
  395. x_ctb, y_ctb);
  396. #else
  397. uint8_t left_pixels;
  398. /* get the CTB edge pixels from the SAO pixel buffer */
  399. left_pixels = !edges[0] && (CTB(s->sao, x_ctb-1, y_ctb).type_idx[c_idx] != SAO_APPLIED);
  400. if (!edges[1]) {
  401. uint8_t top_left = !edges[0] && (CTB(s->sao, x_ctb-1, y_ctb-1).type_idx[c_idx] != SAO_APPLIED);
  402. uint8_t top_right = !edges[2] && (CTB(s->sao, x_ctb+1, y_ctb-1).type_idx[c_idx] != SAO_APPLIED);
  403. if (CTB(s->sao, x_ctb , y_ctb-1).type_idx[c_idx] == 0)
  404. memcpy( dst - stride_dst - (top_left << s->sps->pixel_shift),
  405. src - stride_src - (top_left << s->sps->pixel_shift),
  406. (top_left + width + top_right) << s->sps->pixel_shift);
  407. else {
  408. if (top_left)
  409. memcpy( dst - stride_dst - (1 << s->sps->pixel_shift),
  410. src - stride_src - (1 << s->sps->pixel_shift),
  411. 1 << s->sps->pixel_shift);
  412. if(top_right)
  413. memcpy( dst - stride_dst + (width << s->sps->pixel_shift),
  414. src - stride_src + (width << s->sps->pixel_shift),
  415. 1 << s->sps->pixel_shift);
  416. }
  417. }
  418. if (!edges[3]) { // bottom and bottom right
  419. uint8_t bottom_left = !edges[0] && (CTB(s->sao, x_ctb-1, y_ctb+1).type_idx[c_idx] != SAO_APPLIED);
  420. memcpy( dst + height * stride_dst - (bottom_left << s->sps->pixel_shift),
  421. src + height * stride_src - (bottom_left << s->sps->pixel_shift),
  422. (width + 1 + bottom_left) << s->sps->pixel_shift);
  423. }
  424. copy_CTB(dst - (left_pixels << s->sps->pixel_shift),
  425. src - (left_pixels << s->sps->pixel_shift),
  426. (width + 1 + left_pixels) << s->sps->pixel_shift, height, stride_dst, stride_src);
  427. #endif
  428. /* XXX: could handle the restoration here to simplify the
  429. DSP functions */
  430. s->hevcdsp.sao_edge_filter[restore](src, dst,
  431. stride_src, stride_dst,
  432. sao,
  433. edges, width,
  434. height, c_idx,
  435. vert_edge,
  436. horiz_edge,
  437. diag_edge);
  438. restore_tqb_pixels(s, src, dst, stride_src, stride_dst,
  439. x, y, width, height, c_idx);
  440. sao->type_idx[c_idx] = SAO_APPLIED;
  441. break;
  442. }
  443. }
  444. }
  445. }
  446. static int get_pcm(HEVCContext *s, int x, int y)
  447. {
  448. int log2_min_pu_size = s->sps->log2_min_pu_size;
  449. int x_pu, y_pu;
  450. if (x < 0 || y < 0)
  451. return 2;
  452. x_pu = x >> log2_min_pu_size;
  453. y_pu = y >> log2_min_pu_size;
  454. if (x_pu >= s->sps->min_pu_width || y_pu >= s->sps->min_pu_height)
  455. return 2;
  456. return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
  457. }
  458. #define TC_CALC(qp, bs) \
  459. tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) + \
  460. (tc_offset >> 1 << 1), \
  461. 0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
  462. static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
  463. {
  464. uint8_t *src;
  465. int x, y;
  466. int chroma, beta;
  467. int32_t c_tc[2], tc[2];
  468. uint8_t no_p[2] = { 0 };
  469. uint8_t no_q[2] = { 0 };
  470. int log2_ctb_size = s->sps->log2_ctb_size;
  471. int x_end, x_end2, y_end;
  472. int ctb_size = 1 << log2_ctb_size;
  473. int ctb = (x0 >> log2_ctb_size) +
  474. (y0 >> log2_ctb_size) * s->sps->ctb_width;
  475. int cur_tc_offset = s->deblock[ctb].tc_offset;
  476. int cur_beta_offset = s->deblock[ctb].beta_offset;
  477. int left_tc_offset, left_beta_offset;
  478. int tc_offset, beta_offset;
  479. int pcmf = (s->sps->pcm_enabled_flag &&
  480. s->sps->pcm.loop_filter_disable_flag) ||
  481. s->pps->transquant_bypass_enable_flag;
  482. if (x0) {
  483. left_tc_offset = s->deblock[ctb - 1].tc_offset;
  484. left_beta_offset = s->deblock[ctb - 1].beta_offset;
  485. } else {
  486. left_tc_offset = 0;
  487. left_beta_offset = 0;
  488. }
  489. x_end = x0 + ctb_size;
  490. if (x_end > s->sps->width)
  491. x_end = s->sps->width;
  492. y_end = y0 + ctb_size;
  493. if (y_end > s->sps->height)
  494. y_end = s->sps->height;
  495. tc_offset = cur_tc_offset;
  496. beta_offset = cur_beta_offset;
  497. x_end2 = x_end;
  498. if (x_end2 != s->sps->width)
  499. x_end2 -= 8;
  500. for (y = y0; y < y_end; y += 8) {
  501. // vertical filtering luma
  502. for (x = x0 ? x0 : 8; x < x_end; x += 8) {
  503. const int bs0 = s->vertical_bs[(x + y * s->bs_width) >> 2];
  504. const int bs1 = s->vertical_bs[(x + (y + 4) * s->bs_width) >> 2];
  505. if (bs0 || bs1) {
  506. const int qp = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
  507. beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
  508. tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
  509. tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
  510. src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
  511. if (pcmf) {
  512. no_p[0] = get_pcm(s, x - 1, y);
  513. no_p[1] = get_pcm(s, x - 1, y + 4);
  514. no_q[0] = get_pcm(s, x, y);
  515. no_q[1] = get_pcm(s, x, y + 4);
  516. s->hevcdsp.hevc_v_loop_filter_luma_c(src,
  517. s->frame->linesize[LUMA],
  518. beta, tc, no_p, no_q);
  519. } else
  520. s->hevcdsp.hevc_v_loop_filter_luma(src,
  521. s->frame->linesize[LUMA],
  522. beta, tc, no_p, no_q);
  523. }
  524. }
  525. if(!y)
  526. continue;
  527. // horizontal filtering luma
  528. for (x = x0 ? x0 - 8 : 0; x < x_end2; x += 8) {
  529. const int bs0 = s->horizontal_bs[( x + y * s->bs_width) >> 2];
  530. const int bs1 = s->horizontal_bs[((x + 4) + y * s->bs_width) >> 2];
  531. if (bs0 || bs1) {
  532. const int qp = (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1;
  533. tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
  534. beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
  535. beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
  536. tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
  537. tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
  538. src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
  539. if (pcmf) {
  540. no_p[0] = get_pcm(s, x, y - 1);
  541. no_p[1] = get_pcm(s, x + 4, y - 1);
  542. no_q[0] = get_pcm(s, x, y);
  543. no_q[1] = get_pcm(s, x + 4, y);
  544. s->hevcdsp.hevc_h_loop_filter_luma_c(src,
  545. s->frame->linesize[LUMA],
  546. beta, tc, no_p, no_q);
  547. } else
  548. s->hevcdsp.hevc_h_loop_filter_luma(src,
  549. s->frame->linesize[LUMA],
  550. beta, tc, no_p, no_q);
  551. }
  552. }
  553. }
  554. if (s->sps->chroma_format_idc) {
  555. for (chroma = 1; chroma <= 2; chroma++) {
  556. int h = 1 << s->sps->hshift[chroma];
  557. int v = 1 << s->sps->vshift[chroma];
  558. // vertical filtering chroma
  559. for (y = y0; y < y_end; y += (8 * v)) {
  560. for (x = x0 ? x0 : 8 * h; x < x_end; x += (8 * h)) {
  561. const int bs0 = s->vertical_bs[(x + y * s->bs_width) >> 2];
  562. const int bs1 = s->vertical_bs[(x + (y + (4 * v)) * s->bs_width) >> 2];
  563. if ((bs0 == 2) || (bs1 == 2)) {
  564. const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
  565. const int qp1 = (get_qPy(s, x - 1, y + (4 * v)) + get_qPy(s, x, y + (4 * v)) + 1) >> 1;
  566. c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
  567. c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
  568. src = &s->frame->data[chroma][(y >> s->sps->vshift[chroma]) * s->frame->linesize[chroma] + ((x >> s->sps->hshift[chroma]) << s->sps->pixel_shift)];
  569. if (pcmf) {
  570. no_p[0] = get_pcm(s, x - 1, y);
  571. no_p[1] = get_pcm(s, x - 1, y + (4 * v));
  572. no_q[0] = get_pcm(s, x, y);
  573. no_q[1] = get_pcm(s, x, y + (4 * v));
  574. s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
  575. s->frame->linesize[chroma],
  576. c_tc, no_p, no_q);
  577. } else
  578. s->hevcdsp.hevc_v_loop_filter_chroma(src,
  579. s->frame->linesize[chroma],
  580. c_tc, no_p, no_q);
  581. }
  582. }
  583. if(!y)
  584. continue;
  585. // horizontal filtering chroma
  586. tc_offset = x0 ? left_tc_offset : cur_tc_offset;
  587. x_end2 = x_end;
  588. if (x_end != s->sps->width)
  589. x_end2 = x_end - 8 * h;
  590. for (x = x0 ? x0 - 8 * h : 0; x < x_end2; x += (8 * h)) {
  591. const int bs0 = s->horizontal_bs[( x + y * s->bs_width) >> 2];
  592. const int bs1 = s->horizontal_bs[((x + 4 * h) + y * s->bs_width) >> 2];
  593. if ((bs0 == 2) || (bs1 == 2)) {
  594. const int qp0 = bs0 == 2 ? (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1 : 0;
  595. const int qp1 = bs1 == 2 ? (get_qPy(s, x + (4 * h), y - 1) + get_qPy(s, x + (4 * h), y) + 1) >> 1 : 0;
  596. c_tc[0] = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
  597. c_tc[1] = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
  598. src = &s->frame->data[chroma][(y >> s->sps->vshift[1]) * s->frame->linesize[chroma] + ((x >> s->sps->hshift[1]) << s->sps->pixel_shift)];
  599. if (pcmf) {
  600. no_p[0] = get_pcm(s, x, y - 1);
  601. no_p[1] = get_pcm(s, x + (4 * h), y - 1);
  602. no_q[0] = get_pcm(s, x, y);
  603. no_q[1] = get_pcm(s, x + (4 * h), y);
  604. s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
  605. s->frame->linesize[chroma],
  606. c_tc, no_p, no_q);
  607. } else
  608. s->hevcdsp.hevc_h_loop_filter_chroma(src,
  609. s->frame->linesize[chroma],
  610. c_tc, no_p, no_q);
  611. }
  612. }
  613. }
  614. }
  615. }
  616. }
  617. static int boundary_strength(HEVCContext *s, MvField *curr, MvField *neigh,
  618. RefPicList *neigh_refPicList)
  619. {
  620. if (curr->pred_flag == PF_BI && neigh->pred_flag == PF_BI) {
  621. // same L0 and L1
  622. if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]] &&
  623. s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
  624. neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
  625. if ((FFABS(neigh->mv[0].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
  626. FFABS(neigh->mv[1].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
  627. (FFABS(neigh->mv[1].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
  628. FFABS(neigh->mv[0].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[1].y) >= 4))
  629. return 1;
  630. else
  631. return 0;
  632. } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
  633. neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
  634. if (FFABS(neigh->mv[0].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
  635. FFABS(neigh->mv[1].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[1].y) >= 4)
  636. return 1;
  637. else
  638. return 0;
  639. } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
  640. neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
  641. if (FFABS(neigh->mv[1].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
  642. FFABS(neigh->mv[0].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[1].y) >= 4)
  643. return 1;
  644. else
  645. return 0;
  646. } else {
  647. return 1;
  648. }
  649. } else if ((curr->pred_flag != PF_BI) && (neigh->pred_flag != PF_BI)){ // 1 MV
  650. Mv A, B;
  651. int ref_A, ref_B;
  652. if (curr->pred_flag & 1) {
  653. A = curr->mv[0];
  654. ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
  655. } else {
  656. A = curr->mv[1];
  657. ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
  658. }
  659. if (neigh->pred_flag & 1) {
  660. B = neigh->mv[0];
  661. ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
  662. } else {
  663. B = neigh->mv[1];
  664. ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
  665. }
  666. if (ref_A == ref_B) {
  667. if (FFABS(A.x - B.x) >= 4 || FFABS(A.y - B.y) >= 4)
  668. return 1;
  669. else
  670. return 0;
  671. } else
  672. return 1;
  673. }
  674. return 1;
  675. }
  676. void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
  677. int log2_trafo_size)
  678. {
  679. HEVCLocalContext *lc = s->HEVClc;
  680. MvField *tab_mvf = s->ref->tab_mvf;
  681. int log2_min_pu_size = s->sps->log2_min_pu_size;
  682. int log2_min_tu_size = s->sps->log2_min_tb_size;
  683. int min_pu_width = s->sps->min_pu_width;
  684. int min_tu_width = s->sps->min_tb_width;
  685. int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
  686. (x0 >> log2_min_pu_size)].pred_flag == PF_INTRA;
  687. int boundary_upper, boundary_left;
  688. int i, j, bs;
  689. boundary_upper = y0 > 0 && !(y0 & 7);
  690. if (boundary_upper &&
  691. ((!s->sh.slice_loop_filter_across_slices_enabled_flag &&
  692. lc->boundary_flags & BOUNDARY_UPPER_SLICE &&
  693. (y0 % (1 << s->sps->log2_ctb_size)) == 0) ||
  694. (!s->pps->loop_filter_across_tiles_enabled_flag &&
  695. lc->boundary_flags & BOUNDARY_UPPER_TILE &&
  696. (y0 % (1 << s->sps->log2_ctb_size)) == 0)))
  697. boundary_upper = 0;
  698. if (boundary_upper) {
  699. RefPicList *rpl_top = (lc->boundary_flags & BOUNDARY_UPPER_SLICE) ?
  700. ff_hevc_get_ref_list(s, s->ref, x0, y0 - 1) :
  701. s->ref->refPicList;
  702. int yp_pu = (y0 - 1) >> log2_min_pu_size;
  703. int yq_pu = y0 >> log2_min_pu_size;
  704. int yp_tu = (y0 - 1) >> log2_min_tu_size;
  705. int yq_tu = y0 >> log2_min_tu_size;
  706. for (i = 0; i < (1 << log2_trafo_size); i += 4) {
  707. int x_pu = (x0 + i) >> log2_min_pu_size;
  708. int x_tu = (x0 + i) >> log2_min_tu_size;
  709. MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
  710. MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
  711. uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
  712. uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
  713. if (curr->pred_flag == PF_INTRA || top->pred_flag == PF_INTRA)
  714. bs = 2;
  715. else if (curr_cbf_luma || top_cbf_luma)
  716. bs = 1;
  717. else
  718. bs = boundary_strength(s, curr, top, rpl_top);
  719. s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
  720. }
  721. }
  722. // bs for vertical TU boundaries
  723. boundary_left = x0 > 0 && !(x0 & 7);
  724. if (boundary_left &&
  725. ((!s->sh.slice_loop_filter_across_slices_enabled_flag &&
  726. lc->boundary_flags & BOUNDARY_LEFT_SLICE &&
  727. (x0 % (1 << s->sps->log2_ctb_size)) == 0) ||
  728. (!s->pps->loop_filter_across_tiles_enabled_flag &&
  729. lc->boundary_flags & BOUNDARY_LEFT_TILE &&
  730. (x0 % (1 << s->sps->log2_ctb_size)) == 0)))
  731. boundary_left = 0;
  732. if (boundary_left) {
  733. RefPicList *rpl_left = (lc->boundary_flags & BOUNDARY_LEFT_SLICE) ?
  734. ff_hevc_get_ref_list(s, s->ref, x0 - 1, y0) :
  735. s->ref->refPicList;
  736. int xp_pu = (x0 - 1) >> log2_min_pu_size;
  737. int xq_pu = x0 >> log2_min_pu_size;
  738. int xp_tu = (x0 - 1) >> log2_min_tu_size;
  739. int xq_tu = x0 >> log2_min_tu_size;
  740. for (i = 0; i < (1 << log2_trafo_size); i += 4) {
  741. int y_pu = (y0 + i) >> log2_min_pu_size;
  742. int y_tu = (y0 + i) >> log2_min_tu_size;
  743. MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
  744. MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
  745. uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
  746. uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
  747. if (curr->pred_flag == PF_INTRA || left->pred_flag == PF_INTRA)
  748. bs = 2;
  749. else if (curr_cbf_luma || left_cbf_luma)
  750. bs = 1;
  751. else
  752. bs = boundary_strength(s, curr, left, rpl_left);
  753. s->vertical_bs[(x0 + (y0 + i) * s->bs_width) >> 2] = bs;
  754. }
  755. }
  756. if (log2_trafo_size > log2_min_pu_size && !is_intra) {
  757. RefPicList *rpl = s->ref->refPicList;
  758. // bs for TU internal horizontal PU boundaries
  759. for (j = 8; j < (1 << log2_trafo_size); j += 8) {
  760. int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
  761. int yq_pu = (y0 + j) >> log2_min_pu_size;
  762. for (i = 0; i < (1 << log2_trafo_size); i += 4) {
  763. int x_pu = (x0 + i) >> log2_min_pu_size;
  764. MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
  765. MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
  766. bs = boundary_strength(s, curr, top, rpl);
  767. s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
  768. }
  769. }
  770. // bs for TU internal vertical PU boundaries
  771. for (j = 0; j < (1 << log2_trafo_size); j += 4) {
  772. int y_pu = (y0 + j) >> log2_min_pu_size;
  773. for (i = 8; i < (1 << log2_trafo_size); i += 8) {
  774. int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
  775. int xq_pu = (x0 + i) >> log2_min_pu_size;
  776. MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
  777. MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
  778. bs = boundary_strength(s, curr, left, rpl);
  779. s->vertical_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
  780. }
  781. }
  782. }
  783. }
  784. #undef LUMA
  785. #undef CB
  786. #undef CR
  787. void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size)
  788. {
  789. int x_end = x >= s->sps->width - ctb_size;
  790. deblocking_filter_CTB(s, x, y);
  791. if (s->sps->sao_enabled) {
  792. int y_end = y >= s->sps->height - ctb_size;
  793. if (y && x)
  794. sao_filter_CTB(s, x - ctb_size, y - ctb_size);
  795. if (x && y_end)
  796. sao_filter_CTB(s, x - ctb_size, y);
  797. if (y && x_end) {
  798. sao_filter_CTB(s, x, y - ctb_size);
  799. if (s->threads_type & FF_THREAD_FRAME )
  800. ff_thread_report_progress(&s->ref->tf, y, 0);
  801. }
  802. if (x_end && y_end) {
  803. sao_filter_CTB(s, x , y);
  804. if (s->threads_type & FF_THREAD_FRAME )
  805. ff_thread_report_progress(&s->ref->tf, y + ctb_size, 0);
  806. }
  807. } else if (s->threads_type & FF_THREAD_FRAME && x_end)
  808. ff_thread_report_progress(&s->ref->tf, y + ctb_size - 4, 0);
  809. }
  810. void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
  811. {
  812. int x_end = x_ctb >= s->sps->width - ctb_size;
  813. int y_end = y_ctb >= s->sps->height - ctb_size;
  814. if (y_ctb && x_ctb)
  815. ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size, ctb_size);
  816. if (y_ctb && x_end)
  817. ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size, ctb_size);
  818. if (x_ctb && y_end)
  819. ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb, ctb_size);
  820. }