4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2013 Seppo Tomperi
6 * Copyright (C) 2013 Wassim Hamidouche
8 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
28 #include "cabac_functions.h"
32 #include "bit_depth_template.c"
38 static const uint8_t tctable[54] = {
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // QP 0...18
40 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, // QP 19...37
41 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 24 // QP 38...53
44 static const uint8_t betatable[52] = {
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, // QP 0...18
46 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, // QP 19...37
47 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64 // QP 38...51
50 static int chroma_tc(HEVCContext *s, int qp_y, int c_idx, int tc_offset)
52 static const int qp_c[] = {
53 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
55 int qp, qp_i, offset, idxt;
57 // slice qp offset is not used for deblocking
59 offset = s->pps->cb_qp_offset;
61 offset = s->pps->cr_qp_offset;
63 qp_i = av_clip(qp_y + offset, 0, 57);
64 if (s->sps->chroma_format_idc == 1) {
72 qp = av_clip(qp_i, 0, 51);
75 idxt = av_clip(qp + DEFAULT_INTRA_TC_OFFSET + tc_offset, 0, 53);
79 static int get_qPy_pred(HEVCContext *s, int xC, int yC,
80 int xBase, int yBase, int log2_cb_size)
82 HEVCLocalContext *lc = s->HEVClc;
83 int ctb_size_mask = (1 << s->sps->log2_ctb_size) - 1;
84 int MinCuQpDeltaSizeMask = (1 << (s->sps->log2_ctb_size -
85 s->pps->diff_cu_qp_delta_depth)) - 1;
86 int xQgBase = xBase - (xBase & MinCuQpDeltaSizeMask);
87 int yQgBase = yBase - (yBase & MinCuQpDeltaSizeMask);
88 int min_cb_width = s->sps->min_cb_width;
89 int x_cb = xQgBase >> s->sps->log2_min_cb_size;
90 int y_cb = yQgBase >> s->sps->log2_min_cb_size;
91 int availableA = (xBase & ctb_size_mask) &&
92 (xQgBase & ctb_size_mask);
93 int availableB = (yBase & ctb_size_mask) &&
94 (yQgBase & ctb_size_mask);
95 int qPy_pred, qPy_a, qPy_b;
98 if (lc->first_qp_group || (!xQgBase && !yQgBase)) {
99 lc->first_qp_group = !lc->tu.is_cu_qp_delta_coded;
100 qPy_pred = s->sh.slice_qp;
102 qPy_pred = lc->qPy_pred;
109 qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
115 qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
117 av_assert2(qPy_a >= -s->sps->qp_bd_offset && qPy_a < 52);
118 av_assert2(qPy_b >= -s->sps->qp_bd_offset && qPy_b < 52);
120 return (qPy_a + qPy_b + 1) >> 1;
123 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC,
124 int xBase, int yBase, int log2_cb_size)
126 int qp_y = get_qPy_pred(s, xC, yC, xBase, yBase, log2_cb_size);
128 if (s->HEVClc->tu.cu_qp_delta != 0) {
129 int off = s->sps->qp_bd_offset;
130 s->HEVClc->qp_y = FFUMOD(qp_y + s->HEVClc->tu.cu_qp_delta + 52 + 2 * off,
133 s->HEVClc->qp_y = qp_y;
136 static int get_qPy(HEVCContext *s, int xC, int yC)
138 int log2_min_cb_size = s->sps->log2_min_cb_size;
139 int x = xC >> log2_min_cb_size;
140 int y = yC >> log2_min_cb_size;
141 return s->qp_y_tab[x + y * s->sps->min_cb_width];
144 static void copy_CTB(uint8_t *dst, uint8_t *src,
145 int width, int height, int stride_dst, int stride_src)
149 for (i = 0; i < height; i++) {
150 memcpy(dst, src, width);
156 static void restore_tqb_pixels(HEVCContext *s, int x0, int y0, int width, int height, int c_idx)
158 if ( s->pps->transquant_bypass_enable_flag ||
159 (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) {
161 ptrdiff_t stride_dst = s->sao_frame->linesize[c_idx];
162 ptrdiff_t stride_src = s->frame->linesize[c_idx];
163 int min_pu_size = 1 << s->sps->log2_min_pu_size;
164 int hshift = s->sps->hshift[c_idx];
165 int vshift = s->sps->vshift[c_idx];
166 int x_min = ((x0 ) >> s->sps->log2_min_pu_size);
167 int y_min = ((y0 ) >> s->sps->log2_min_pu_size);
168 int x_max = ((x0 + width ) >> s->sps->log2_min_pu_size);
169 int y_max = ((y0 + height) >> s->sps->log2_min_pu_size);
170 int len = min_pu_size >> hshift;
171 for (y = y_min; y < y_max; y++) {
172 for (x = x_min; x < x_max; x++) {
173 if (s->is_pcm[y * s->sps->min_pu_width + x]) {
175 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)];
176 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)];
177 for (n = 0; n < (min_pu_size >> vshift); n++) {
178 memcpy(src, dst, len);
188 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
190 static void sao_filter_CTB(HEVCContext *s, int x, int y)
193 int edges[4]; // 0 left 1 top 2 right 3 bottom
194 int x_ctb = x >> s->sps->log2_ctb_size;
195 int y_ctb = y >> s->sps->log2_ctb_size;
196 int ctb_addr_rs = y_ctb * s->sps->ctb_width + x_ctb;
197 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
198 SAOParams *sao = &CTB(s->sao, x_ctb, y_ctb);
199 // flags indicating unfilterable edges
200 uint8_t vert_edge[] = { 0, 0 };
201 uint8_t horiz_edge[] = { 0, 0 };
202 uint8_t diag_edge[] = { 0, 0, 0, 0 };
203 uint8_t lfase = CTB(s->filter_slice_edges, x_ctb, y_ctb);
204 uint8_t no_tile_filter = s->pps->tiles_enabled_flag &&
205 !s->pps->loop_filter_across_tiles_enabled_flag;
206 uint8_t restore = no_tile_filter || !lfase;
207 uint8_t left_tile_edge = 0;
208 uint8_t right_tile_edge = 0;
209 uint8_t up_tile_edge = 0;
210 uint8_t bottom_tile_edge = 0;
212 edges[0] = x_ctb == 0;
213 edges[1] = y_ctb == 0;
214 edges[2] = x_ctb == s->sps->ctb_width - 1;
215 edges[3] = y_ctb == s->sps->ctb_height - 1;
219 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]];
220 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;
223 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]];
224 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;
227 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]];
228 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;
231 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]];
232 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;
234 if (!edges[0] && !edges[1]) {
235 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;
237 if (!edges[1] && !edges[2]) {
238 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;
240 if (!edges[2] && !edges[3]) {
241 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;
243 if (!edges[0] && !edges[3]) {
244 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;
248 for (c_idx = 0; c_idx < 3; c_idx++) {
249 int x0 = x >> s->sps->hshift[c_idx];
250 int y0 = y >> s->sps->vshift[c_idx];
251 int stride_src = s->frame->linesize[c_idx];
252 int stride_dst = s->sao_frame->linesize[c_idx];
253 int ctb_size_h = (1 << (s->sps->log2_ctb_size)) >> s->sps->hshift[c_idx];
254 int ctb_size_v = (1 << (s->sps->log2_ctb_size)) >> s->sps->vshift[c_idx];
255 int width = FFMIN(ctb_size_h, (s->sps->width >> s->sps->hshift[c_idx]) - x0);
256 int height = FFMIN(ctb_size_v, (s->sps->height >> s->sps->vshift[c_idx]) - y0);
257 uint8_t *src = &s->frame->data[c_idx][y0 * stride_src + (x0 << s->sps->pixel_shift)];
258 uint8_t *dst = &s->sao_frame->data[c_idx][y0 * stride_dst + (x0 << s->sps->pixel_shift)];
260 switch (sao->type_idx[c_idx]) {
262 copy_CTB(dst, src, width << s->sps->pixel_shift, height, stride_dst, stride_src);
263 s->hevcdsp.sao_band_filter(src, dst,
264 stride_src, stride_dst,
268 restore_tqb_pixels(s, x, y, width, height, c_idx);
269 sao->type_idx[c_idx] = SAO_APPLIED;
273 uint8_t left_pixels = !edges[0] && (CTB(s->sao, x_ctb-1, y_ctb).type_idx[c_idx] != SAO_APPLIED);
275 uint8_t top_left = !edges[0] && (CTB(s->sao, x_ctb-1, y_ctb-1).type_idx[c_idx] != SAO_APPLIED);
276 uint8_t top_right = !edges[2] && (CTB(s->sao, x_ctb+1, y_ctb-1).type_idx[c_idx] != SAO_APPLIED);
277 if (CTB(s->sao, x_ctb , y_ctb-1).type_idx[c_idx] == 0)
278 memcpy( dst - stride_dst - (top_left << s->sps->pixel_shift),
279 src - stride_src - (top_left << s->sps->pixel_shift),
280 (top_left + width + top_right) << s->sps->pixel_shift);
283 memcpy( dst - stride_dst - (1 << s->sps->pixel_shift),
284 src - stride_src - (1 << s->sps->pixel_shift),
285 1 << s->sps->pixel_shift);
287 memcpy( dst - stride_dst + (width << s->sps->pixel_shift),
288 src - stride_src + (width << s->sps->pixel_shift),
289 1 << s->sps->pixel_shift);
292 if (!edges[3]) { // bottom and bottom right
293 uint8_t bottom_left = !edges[0] && (CTB(s->sao, x_ctb-1, y_ctb+1).type_idx[c_idx] != SAO_APPLIED);
294 memcpy( dst + height * stride_dst - (bottom_left << s->sps->pixel_shift),
295 src + height * stride_src - (bottom_left << s->sps->pixel_shift),
296 (width + 1 + bottom_left) << s->sps->pixel_shift);
298 copy_CTB(dst - (left_pixels << s->sps->pixel_shift),
299 src - (left_pixels << s->sps->pixel_shift),
300 (width + 1 + left_pixels) << s->sps->pixel_shift, height, stride_dst, stride_src);
301 s->hevcdsp.sao_edge_filter[restore](src, dst,
302 stride_src, stride_dst,
309 restore_tqb_pixels(s, x, y, width, height, c_idx);
310 sao->type_idx[c_idx] = SAO_APPLIED;
317 static int get_pcm(HEVCContext *s, int x, int y)
319 int log2_min_pu_size = s->sps->log2_min_pu_size;
325 x_pu = x >> log2_min_pu_size;
326 y_pu = y >> log2_min_pu_size;
328 if (x_pu >= s->sps->min_pu_width || y_pu >= s->sps->min_pu_height)
330 return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
333 #define TC_CALC(qp, bs) \
334 tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) + \
335 (tc_offset >> 1 << 1), \
336 0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
338 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
343 int c_tc[2], beta[2], tc[2];
344 uint8_t no_p[2] = { 0 };
345 uint8_t no_q[2] = { 0 };
347 int log2_ctb_size = s->sps->log2_ctb_size;
349 int ctb_size = 1 << log2_ctb_size;
350 int ctb = (x0 >> log2_ctb_size) +
351 (y0 >> log2_ctb_size) * s->sps->ctb_width;
352 int cur_tc_offset = s->deblock[ctb].tc_offset;
353 int cur_beta_offset = s->deblock[ctb].beta_offset;
354 int left_tc_offset, left_beta_offset;
355 int tc_offset, beta_offset;
356 int pcmf = (s->sps->pcm_enabled_flag &&
357 s->sps->pcm.loop_filter_disable_flag) ||
358 s->pps->transquant_bypass_enable_flag;
361 left_tc_offset = s->deblock[ctb - 1].tc_offset;
362 left_beta_offset = s->deblock[ctb - 1].beta_offset;
365 left_beta_offset = 0;
368 x_end = x0 + ctb_size;
369 if (x_end > s->sps->width)
370 x_end = s->sps->width;
371 y_end = y0 + ctb_size;
372 if (y_end > s->sps->height)
373 y_end = s->sps->height;
375 tc_offset = cur_tc_offset;
376 beta_offset = cur_beta_offset;
378 // vertical filtering luma
379 for (y = y0; y < y_end; y += 8) {
380 for (x = x0 ? x0 : 8; x < x_end; x += 8) {
381 const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
382 const int bs1 = s->vertical_bs[(x >> 3) + ((y + 4) >> 2) * s->bs_width];
384 const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
385 const int qp1 = (get_qPy(s, x - 1, y + 4) + get_qPy(s, x, y + 4) + 1) >> 1;
387 beta[0] = betatable[av_clip(qp0 + beta_offset, 0, MAX_QP)];
388 beta[1] = betatable[av_clip(qp1 + beta_offset, 0, MAX_QP)];
389 tc[0] = bs0 ? TC_CALC(qp0, bs0) : 0;
390 tc[1] = bs1 ? TC_CALC(qp1, bs1) : 0;
391 src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
393 no_p[0] = get_pcm(s, x - 1, y);
394 no_p[1] = get_pcm(s, x - 1, y + 4);
395 no_q[0] = get_pcm(s, x, y);
396 no_q[1] = get_pcm(s, x, y + 4);
397 s->hevcdsp.hevc_v_loop_filter_luma_c(src,
398 s->frame->linesize[LUMA],
399 beta, tc, no_p, no_q);
401 s->hevcdsp.hevc_v_loop_filter_luma(src,
402 s->frame->linesize[LUMA],
403 beta, tc, no_p, no_q);
408 // vertical filtering chroma
409 for (chroma = 1; chroma <= 2; chroma++) {
410 int h = 1 << s->sps->hshift[chroma];
411 int v = 1 << s->sps->vshift[chroma];
412 for (y = y0; y < y_end; y += (8 * v)) {
413 for (x = x0 ? x0 : 8 * h; x < x_end; x += (8 * h)) {
414 const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
415 const int bs1 = s->vertical_bs[(x >> 3) + ((y + (4 * v)) >> 2) * s->bs_width];
417 if ((bs0 == 2) || (bs1 == 2)) {
418 const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
419 const int qp1 = (get_qPy(s, x - 1, y + (4 * v)) + get_qPy(s, x, y + (4 * v)) + 1) >> 1;
421 c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
422 c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
423 src = &s->frame->data[chroma][(y >> s->sps->vshift[chroma]) * s->frame->linesize[chroma] + ((x >> s->sps->hshift[chroma]) << s->sps->pixel_shift)];
425 no_p[0] = get_pcm(s, x - 1, y);
426 no_p[1] = get_pcm(s, x - 1, y + (4 * v));
427 no_q[0] = get_pcm(s, x, y);
428 no_q[1] = get_pcm(s, x, y + (4 * v));
429 s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
430 s->frame->linesize[chroma],
433 s->hevcdsp.hevc_v_loop_filter_chroma(src,
434 s->frame->linesize[chroma],
441 // horizontal filtering luma
442 if (x_end != s->sps->width)
444 for (y = y0 ? y0 : 8; y < y_end; y += 8) {
445 for (x = x0 ? x0 - 8 : 0; x < x_end; x += 8) {
446 const int bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
447 const int bs1 = s->horizontal_bs[(x + 4 + y * s->bs_width) >> 2];
449 const int qp0 = (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1;
450 const int qp1 = (get_qPy(s, x + 4, y - 1) + get_qPy(s, x + 4, y) + 1) >> 1;
452 tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
453 beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
455 beta[0] = betatable[av_clip(qp0 + beta_offset, 0, MAX_QP)];
456 beta[1] = betatable[av_clip(qp1 + beta_offset, 0, MAX_QP)];
457 tc[0] = bs0 ? TC_CALC(qp0, bs0) : 0;
458 tc[1] = bs1 ? TC_CALC(qp1, bs1) : 0;
459 src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
461 no_p[0] = get_pcm(s, x, y - 1);
462 no_p[1] = get_pcm(s, x + 4, y - 1);
463 no_q[0] = get_pcm(s, x, y);
464 no_q[1] = get_pcm(s, x + 4, y);
465 s->hevcdsp.hevc_h_loop_filter_luma_c(src,
466 s->frame->linesize[LUMA],
467 beta, tc, no_p, no_q);
469 s->hevcdsp.hevc_h_loop_filter_luma(src,
470 s->frame->linesize[LUMA],
471 beta, tc, no_p, no_q);
476 // horizontal filtering chroma
477 for (chroma = 1; chroma <= 2; chroma++) {
478 int h = 1 << s->sps->hshift[chroma];
479 int v = 1 << s->sps->vshift[chroma];
480 for (y = y0 ? y0 : 8 * v; y < y_end; y += (8 * v)) {
481 for (x = x0 - 8; x < x_end; x += (8 * h)) {
483 // to make sure no memory access over boundary when x = -8
484 // TODO: simplify with row based deblocking
487 bs1 = s->horizontal_bs[(x + (4 * h) + y * s->bs_width) >> 2];
488 } else if (x >= x_end - 4 * h) {
489 bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
492 bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
493 bs1 = s->horizontal_bs[(x + (4 * h) + y * s->bs_width) >> 2];
496 if ((bs0 == 2) || (bs1 == 2)) {
497 const int qp0 = bs0 == 2 ? (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1 : 0;
498 const int qp1 = bs1 == 2 ? (get_qPy(s, x + (4 * h), y - 1) + get_qPy(s, x + (4 * h), y) + 1) >> 1 : 0;
500 tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
501 c_tc[0] = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
502 c_tc[1] = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
503 src = &s->frame->data[chroma][(y >> s->sps->vshift[1]) * s->frame->linesize[chroma] + ((x >> s->sps->hshift[1]) << s->sps->pixel_shift)];
505 no_p[0] = get_pcm(s, x, y - 1);
506 no_p[1] = get_pcm(s, x + (4 * h), y - 1);
507 no_q[0] = get_pcm(s, x, y);
508 no_q[1] = get_pcm(s, x + (4 * h), y);
509 s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
510 s->frame->linesize[chroma],
513 s->hevcdsp.hevc_h_loop_filter_chroma(src,
514 s->frame->linesize[chroma],
522 static int boundary_strength(HEVCContext *s, MvField *curr, MvField *neigh,
523 RefPicList *neigh_refPicList)
525 if (curr->pred_flag == PF_BI && neigh->pred_flag == PF_BI) {
527 if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]] &&
528 s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
529 neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
530 if ((FFABS(neigh->mv[0].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
531 FFABS(neigh->mv[1].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
532 (FFABS(neigh->mv[1].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
533 FFABS(neigh->mv[0].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[1].y) >= 4))
537 } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
538 neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
539 if (FFABS(neigh->mv[0].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
540 FFABS(neigh->mv[1].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[1].y) >= 4)
544 } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
545 neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
546 if (FFABS(neigh->mv[1].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
547 FFABS(neigh->mv[0].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[1].y) >= 4)
554 } else if ((curr->pred_flag != PF_BI) && (neigh->pred_flag != PF_BI)){ // 1 MV
558 if (curr->pred_flag & 1) {
560 ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
563 ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
566 if (neigh->pred_flag & 1) {
568 ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
571 ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
574 if (ref_A == ref_B) {
575 if (FFABS(A.x - B.x) >= 4 || FFABS(A.y - B.y) >= 4)
586 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
589 HEVCLocalContext *lc = s->HEVClc;
590 MvField *tab_mvf = s->ref->tab_mvf;
591 int log2_min_pu_size = s->sps->log2_min_pu_size;
592 int log2_min_tu_size = s->sps->log2_min_tb_size;
593 int min_pu_width = s->sps->min_pu_width;
594 int min_tu_width = s->sps->min_tb_width;
595 int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
596 (x0 >> log2_min_pu_size)].pred_flag == PF_INTRA;
599 if (y0 > 0 && (y0 & 7) == 0) {
600 int bd_ctby = y0 & ((1 << s->sps->log2_ctb_size) - 1);
601 int bd_slice = s->sh.slice_loop_filter_across_slices_enabled_flag ||
602 !(lc->slice_or_tiles_up_boundary & 1);
603 int bd_tiles = s->pps->loop_filter_across_tiles_enabled_flag ||
604 !(lc->slice_or_tiles_up_boundary & 2);
605 if (((bd_slice && bd_tiles) || bd_ctby)) {
606 int yp_pu = (y0 - 1) >> log2_min_pu_size;
607 int yq_pu = y0 >> log2_min_pu_size;
608 int yp_tu = (y0 - 1) >> log2_min_tu_size;
609 int yq_tu = y0 >> log2_min_tu_size;
610 RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
613 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
614 int x_pu = (x0 + i) >> log2_min_pu_size;
615 int x_tu = (x0 + i) >> log2_min_tu_size;
616 MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
617 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
618 uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
619 uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
621 if (curr->pred_flag == PF_INTRA || top->pred_flag == PF_INTRA)
623 else if (curr_cbf_luma || top_cbf_luma)
626 bs = boundary_strength(s, curr, top, top_refPicList);
627 s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
632 // bs for vertical TU boundaries
633 if (x0 > 0 && (x0 & 7) == 0) {
634 int bd_ctbx = x0 & ((1 << s->sps->log2_ctb_size) - 1);
635 int bd_slice = s->sh.slice_loop_filter_across_slices_enabled_flag ||
636 !(lc->slice_or_tiles_left_boundary & 1);
637 int bd_tiles = s->pps->loop_filter_across_tiles_enabled_flag ||
638 !(lc->slice_or_tiles_left_boundary & 2);
639 if (((bd_slice && bd_tiles) || bd_ctbx)) {
640 int xp_pu = (x0 - 1) >> log2_min_pu_size;
641 int xq_pu = x0 >> log2_min_pu_size;
642 int xp_tu = (x0 - 1) >> log2_min_tu_size;
643 int xq_tu = x0 >> log2_min_tu_size;
644 RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
647 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
648 int y_pu = (y0 + i) >> log2_min_pu_size;
649 int y_tu = (y0 + i) >> log2_min_tu_size;
650 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
651 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
652 uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
653 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
655 if (curr->pred_flag == PF_INTRA || left->pred_flag == PF_INTRA)
657 else if (curr_cbf_luma || left_cbf_luma)
660 bs = boundary_strength(s, curr, left, left_refPicList);
661 s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
666 if (log2_trafo_size > log2_min_pu_size && !is_intra) {
667 RefPicList *refPicList = ff_hevc_get_ref_list(s, s->ref,
670 // bs for TU internal horizontal PU boundaries
671 for (j = 8; j < (1 << log2_trafo_size); j += 8) {
672 int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
673 int yq_pu = (y0 + j) >> log2_min_pu_size;
675 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
676 int x_pu = (x0 + i) >> log2_min_pu_size;
677 MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
678 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
680 bs = boundary_strength(s, curr, top, refPicList);
681 s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
685 // bs for TU internal vertical PU boundaries
686 for (j = 0; j < (1 << log2_trafo_size); j += 4) {
687 int y_pu = (y0 + j) >> log2_min_pu_size;
689 for (i = 8; i < (1 << log2_trafo_size); i += 8) {
690 int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
691 int xq_pu = (x0 + i) >> log2_min_pu_size;
692 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
693 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
695 bs = boundary_strength(s, curr, left, refPicList);
696 s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
706 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size)
708 int x_end = x >= s->sps->width - ctb_size;
709 deblocking_filter_CTB(s, x, y);
710 if (s->sps->sao_enabled) {
711 int y_end = y >= s->sps->height - ctb_size;
713 sao_filter_CTB(s, x - ctb_size, y - ctb_size);
715 sao_filter_CTB(s, x - ctb_size, y);
717 sao_filter_CTB(s, x, y - ctb_size);
718 if (s->threads_type & FF_THREAD_FRAME )
719 ff_thread_report_progress(&s->ref->tf, y, 0);
721 if (x_end && y_end) {
722 sao_filter_CTB(s, x , y);
723 if (s->threads_type & FF_THREAD_FRAME )
724 ff_thread_report_progress(&s->ref->tf, y + ctb_size, 0);
726 } else if (s->threads_type & FF_THREAD_FRAME && x_end)
727 ff_thread_report_progress(&s->ref->tf, y + ctb_size - 4, 0);
730 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
732 int x_end = x_ctb >= s->sps->width - ctb_size;
733 int y_end = y_ctb >= s->sps->height - ctb_size;
735 ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size, ctb_size);
737 ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size, ctb_size);
739 ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb, ctb_size);