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
59 // slice qp offset is not used for deblocking
61 offset = s->pps->cb_qp_offset;
63 offset = s->pps->cr_qp_offset;
65 qp_i = av_clip_c(qp_y + offset, 0, 57);
73 idxt = av_clip_c(qp + DEFAULT_INTRA_TC_OFFSET + tc_offset, 0, 53);
77 static int get_qPy_pred(HEVCContext *s, int xC, int yC,
78 int xBase, int yBase, int log2_cb_size)
80 HEVCLocalContext *lc = s->HEVClc;
81 int ctb_size_mask = (1 << s->sps->log2_ctb_size) - 1;
82 int MinCuQpDeltaSizeMask = (1 << (s->sps->log2_ctb_size -
83 s->pps->diff_cu_qp_delta_depth)) - 1;
84 int xQgBase = xBase - (xBase & MinCuQpDeltaSizeMask);
85 int yQgBase = yBase - (yBase & MinCuQpDeltaSizeMask);
86 int min_cb_width = s->sps->min_cb_width;
87 int min_cb_height = s->sps->min_cb_height;
88 int x_cb = xQgBase >> s->sps->log2_min_cb_size;
89 int y_cb = yQgBase >> s->sps->log2_min_cb_size;
90 int availableA = (xBase & ctb_size_mask) &&
91 (xQgBase & ctb_size_mask);
92 int availableB = (yBase & ctb_size_mask) &&
93 (yQgBase & ctb_size_mask);
99 if (lc->first_qp_group) {
100 lc->first_qp_group = !lc->tu.is_cu_qp_delta_coded;
101 qPy_pred = s->sh.slice_qp;
104 if (log2_cb_size < s->sps->log2_ctb_size -
105 s->pps->diff_cu_qp_delta_depth) {
106 static const int offsetX[8][8] = {
107 { -1, 1, 3, 1, 7, 1, 3, 1 },
108 { 0, 0, 0, 0, 0, 0, 0, 0 },
109 { 1, 3, 1, 3, 1, 3, 1, 3 },
110 { 2, 2, 2, 2, 2, 2, 2, 2 },
111 { 3, 5, 7, 5, 3, 5, 7, 5 },
112 { 4, 4, 4, 4, 4, 4, 4, 4 },
113 { 5, 7, 5, 7, 5, 7, 5, 7 },
114 { 6, 6, 6, 6, 6, 6, 6, 6 }
116 static const int offsetY[8][8] = {
117 { 7, 0, 1, 2, 3, 4, 5, 6 },
118 { 0, 1, 2, 3, 4, 5, 6, 7 },
119 { 1, 0, 3, 2, 5, 4, 7, 6 },
120 { 0, 1, 2, 3, 4, 5, 6, 7 },
121 { 3, 0, 1, 2, 7, 4, 5, 6 },
122 { 0, 1, 2, 3, 4, 5, 6, 7 },
123 { 1, 0, 3, 2, 5, 4, 7, 6 },
124 { 0, 1, 2, 3, 4, 5, 6, 7 }
126 int xC0b = (xC - (xC & ctb_size_mask)) >> s->sps->log2_min_cb_size;
127 int yC0b = (yC - (yC & ctb_size_mask)) >> s->sps->log2_min_cb_size;
128 int idxX = (xQgBase & ctb_size_mask) >> s->sps->log2_min_cb_size;
129 int idxY = (yQgBase & ctb_size_mask) >> s->sps->log2_min_cb_size;
130 int idx_mask = ctb_size_mask >> s->sps->log2_min_cb_size;
133 x = FFMIN(xC0b + offsetX[idxX][idxY], min_cb_width - 1);
134 y = FFMIN(yC0b + (offsetY[idxX][idxY] & idx_mask), min_cb_height - 1);
136 if (xC0b == (lc->start_of_tiles_x >> s->sps->log2_min_cb_size) &&
137 offsetX[idxX][idxY] == -1) {
138 x = (lc->end_of_tiles_x >> s->sps->log2_min_cb_size) - 1;
141 qPy_pred = s->qp_y_tab[y * min_cb_width + x];
149 qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
155 qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
157 return (qPy_a + qPy_b + 1) >> 1;
160 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC,
161 int xBase, int yBase, int log2_cb_size)
163 int qp_y = get_qPy_pred(s, xC, yC, xBase, yBase, log2_cb_size);
165 if (s->HEVClc->tu.cu_qp_delta != 0) {
166 int off = s->sps->qp_bd_offset;
167 s->HEVClc->qp_y = ((qp_y + s->HEVClc->tu.cu_qp_delta + 52 + 2 * off) %
170 s->HEVClc->qp_y = qp_y;
173 static int get_qPy(HEVCContext *s, int xC, int yC)
175 int log2_min_cb_size = s->sps->log2_min_cb_size;
176 int x = xC >> log2_min_cb_size;
177 int y = yC >> log2_min_cb_size;
178 return s->qp_y_tab[x + y * s->sps->min_cb_width];
181 static void copy_CTB(uint8_t *dst, uint8_t *src,
182 int width, int height, int stride)
186 for (i = 0; i < height; i++) {
187 memcpy(dst, src, width);
193 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
195 static void sao_filter_CTB(HEVCContext *s, int x, int y)
197 // TODO: This should be easily parallelizable
198 // TODO: skip CBs when (cu_transquant_bypass_flag || (pcm_loop_filter_disable_flag && pcm_flag))
200 int class = 1, class_index;
201 int edges[4]; // 0 left 1 top 2 right 3 bottom
204 int x_shift = 0, y_shift = 0;
205 int x_ctb = x >> s->sps->log2_ctb_size;
206 int y_ctb = y >> s->sps->log2_ctb_size;
207 int ctb_addr_rs = y_ctb * s->sps->ctb_width + x_ctb;
208 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
210 // flags indicating unfilterable edges
211 uint8_t vert_edge[] = { 0, 0, 0, 0 };
212 uint8_t horiz_edge[] = { 0, 0, 0, 0 };
213 uint8_t diag_edge[] = { 0, 0, 0, 0 };
214 uint8_t lfase[3]; // current, above, left
215 uint8_t no_tile_filter = s->pps->tiles_enabled_flag &&
216 !s->pps->loop_filter_across_tiles_enabled_flag;
217 uint8_t left_tile_edge = 0;
218 uint8_t up_tile_edge = 0;
220 sao[0] = &CTB(s->sao, x_ctb, y_ctb);
221 edges[0] = x_ctb == 0;
222 edges[1] = y_ctb == 0;
223 edges[2] = x_ctb == (s->sps->ctb_width - 1);
224 edges[3] = y_ctb == (s->sps->ctb_height - 1);
225 lfase[0] = CTB(s->filter_slice_edges, x_ctb, y_ctb);
229 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]];
230 sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb);
231 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;
232 vert_edge[2] = vert_edge[0];
233 lfase[2] = CTB(s->filter_slice_edges, x_ctb - 1, y_ctb);
240 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]];
241 sao[class] = &CTB(s->sao, x_ctb, y_ctb - 1);
242 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;
243 horiz_edge[1] = horiz_edge[0];
244 lfase[1] = CTB(s->filter_slice_edges, x_ctb, y_ctb - 1);
251 sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb - 1);
254 // Tile check here is done current CTB row/col, not above/left like you'd expect,
255 //but that is because the tile boundary always extends through the whole pic
256 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;
257 vert_edge[3] = vert_edge[1];
258 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;
259 horiz_edge[3] = horiz_edge[2];
260 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;
261 diag_edge[3] = diag_edge[0];
263 // Does left CTB comes after above CTB?
264 if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) >
265 CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
266 diag_edge[2] = !lfase[2] || left_tile_edge || up_tile_edge;
267 diag_edge[1] = diag_edge[2];
268 } else if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) <
269 CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
270 diag_edge[1] = !lfase[1] || left_tile_edge || up_tile_edge;
271 diag_edge[2] = diag_edge[1];
273 // Same slice, only consider tiles
274 diag_edge[2] = left_tile_edge || up_tile_edge;
275 diag_edge[1] = diag_edge[2];
280 for (c_idx = 0; c_idx < 3; c_idx++) {
281 int chroma = c_idx ? 1 : 0;
282 int x0 = x >> chroma;
283 int y0 = y >> chroma;
284 int stride = s->frame->linesize[c_idx];
285 int ctb_size = (1 << (s->sps->log2_ctb_size)) >> s->sps->hshift[c_idx];
286 int width = FFMIN(ctb_size,
287 (s->sps->width >> s->sps->hshift[c_idx]) - x0);
288 int height = FFMIN(ctb_size,
289 (s->sps->height >> s->sps->vshift[c_idx]) - y0);
291 uint8_t *src = &s->frame->data[c_idx][y0 * stride + (x0 << s->sps->pixel_shift)];
292 uint8_t *dst = &s->sao_frame->data[c_idx][y0 * stride + (x0 << s->sps->pixel_shift)];
293 int offset = (y_shift >> chroma) * stride + ((x_shift >> chroma) << s->sps->pixel_shift);
295 copy_CTB(dst - offset, src - offset,
296 (edges[2] ? width + (x_shift >> chroma) : width) << s->sps->pixel_shift,
297 (edges[3] ? height + (y_shift >> chroma) : height), stride);
299 for (class_index = 0; class_index < class; class_index++) {
301 switch (sao[class_index]->type_idx[c_idx]) {
303 s->hevcdsp.sao_band_filter[classes[class_index]](dst, src,
310 s->hevcdsp.sao_edge_filter[classes[class_index]](dst, src,
315 vert_edge[classes[class_index]],
316 horiz_edge[classes[class_index]],
317 diag_edge[classes[class_index]]);
324 static int get_pcm(HEVCContext *s, int x, int y)
326 int log2_min_pu_size = s->sps->log2_min_pu_size;
327 int x_pu = x >> log2_min_pu_size;
328 int y_pu = y >> log2_min_pu_size;
330 if (x < 0 || x_pu >= s->sps->min_pu_width ||
331 y < 0 || y_pu >= s->sps->min_pu_height)
333 return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
336 #define TC_CALC(qp, bs) tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) + ((tc_offset >> 1) << 1), 0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
338 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
346 uint8_t no_p[2] = { 0 };
347 uint8_t no_q[2] = { 0 };
349 int log2_ctb_size = s->sps->log2_ctb_size;
351 int ctb_size = 1 << log2_ctb_size;
352 int ctb = (x0 >> log2_ctb_size) +
353 (y0 >> log2_ctb_size) * s->sps->ctb_width;
354 int cur_tc_offset = s->deblock[ctb].tc_offset;
355 int cur_beta_offset = s->deblock[ctb].beta_offset;
356 int left_tc_offset, left_beta_offset;
357 int tc_offset, beta_offset;
358 int pcmf = (s->sps->pcm_enabled_flag &&
359 s->sps->pcm.loop_filter_disable_flag) ||
360 s->pps->transquant_bypass_enable_flag;
363 left_tc_offset = s->deblock[ctb - 1].tc_offset;
364 left_beta_offset = s->deblock[ctb - 1].beta_offset;
367 x_end = x0 + ctb_size;
368 if (x_end > s->sps->width)
369 x_end = s->sps->width;
370 y_end = y0 + ctb_size;
371 if (y_end > s->sps->height)
372 y_end = s->sps->height;
374 tc_offset = cur_tc_offset;
375 beta_offset = cur_beta_offset;
377 // vertical filtering luma
378 for (y = y0; y < y_end; y += 8) {
379 for (x = x0 ? x0 : 8; x < x_end; x += 8) {
380 const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
381 const int bs1 = s->vertical_bs[(x >> 3) + ((y + 4) >> 2) * s->bs_width];
383 const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
384 const int qp1 = (get_qPy(s, x - 1, y + 4) + get_qPy(s, x, y + 4) + 1) >> 1;
386 beta[0] = betatable[av_clip(qp0 + ((beta_offset >> 1) << 1), 0, MAX_QP)];
387 beta[1] = betatable[av_clip(qp1 + ((beta_offset >> 1) << 1), 0, MAX_QP)];
388 tc[0] = bs0 ? TC_CALC(qp0, bs0) : 0;
389 tc[1] = bs1 ? TC_CALC(qp1, bs1) : 0;
390 src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
392 no_p[0] = get_pcm(s, x - 1, y);
393 no_p[1] = get_pcm(s, x - 1, y + 4);
394 no_q[0] = get_pcm(s, x, y);
395 no_q[1] = get_pcm(s, x, y + 4);
396 s->hevcdsp.hevc_v_loop_filter_luma_c(src,
397 s->frame->linesize[LUMA],
398 beta, tc, no_p, no_q);
400 s->hevcdsp.hevc_v_loop_filter_luma(src,
401 s->frame->linesize[LUMA],
402 beta, tc, no_p, no_q);
407 // vertical filtering chroma
408 for (chroma = 1; chroma <= 2; chroma++) {
409 for (y = y0; y < y_end; y += 16) {
410 for (x = x0 ? x0 : 16; x < x_end; x += 16) {
411 const int bs0 = s->vertical_bs[(x >> 3) + (y >> 2) * s->bs_width];
412 const int bs1 = s->vertical_bs[(x >> 3) + ((y + 8) >> 2) * s->bs_width];
413 if ((bs0 == 2) || (bs1 == 2)) {
414 const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
415 const int qp1 = (get_qPy(s, x - 1, y + 8) + get_qPy(s, x, y + 8) + 1) >> 1;
417 c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
418 c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
419 src = &s->frame->data[chroma][(y / 2) * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
421 no_p[0] = get_pcm(s, x - 1, y);
422 no_p[1] = get_pcm(s, x - 1, y + 8);
423 no_q[0] = get_pcm(s, x, y);
424 no_q[1] = get_pcm(s, x, y + 8);
425 s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
426 s->frame->linesize[chroma],
429 s->hevcdsp.hevc_v_loop_filter_chroma(src,
430 s->frame->linesize[chroma],
437 // horizontal filtering luma
438 if (x_end != s->sps->width)
440 for (y = y0 ? y0 : 8; y < y_end; y += 8) {
441 for (x = x0 ? x0 - 8 : 0; x < x_end; x += 8) {
442 const int bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
443 const int bs1 = s->horizontal_bs[(x + 4 + y * s->bs_width) >> 2];
445 const int qp0 = (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1;
446 const int qp1 = (get_qPy(s, x + 4, y - 1) + get_qPy(s, x + 4, y) + 1) >> 1;
448 tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
449 beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
451 beta[0] = betatable[av_clip(qp0 + ((beta_offset >> 1) << 1), 0, MAX_QP)];
452 beta[1] = betatable[av_clip(qp1 + ((beta_offset >> 1) << 1), 0, MAX_QP)];
453 tc[0] = bs0 ? TC_CALC(qp0, bs0) : 0;
454 tc[1] = bs1 ? TC_CALC(qp1, bs1) : 0;
455 src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
457 no_p[0] = get_pcm(s, x, y - 1);
458 no_p[1] = get_pcm(s, x + 4, y - 1);
459 no_q[0] = get_pcm(s, x, y);
460 no_q[1] = get_pcm(s, x + 4, y);
461 s->hevcdsp.hevc_h_loop_filter_luma_c(src,
462 s->frame->linesize[LUMA],
463 beta, tc, no_p, no_q);
465 s->hevcdsp.hevc_h_loop_filter_luma(src,
466 s->frame->linesize[LUMA],
467 beta, tc, no_p, no_q);
472 // horizontal filtering chroma
473 for (chroma = 1; chroma <= 2; chroma++) {
474 for (y = y0 ? y0 : 16; y < y_end; y += 16) {
475 for (x = x0 - 8; x < x_end; x += 16) {
477 // to make sure no memory access over boundary when x = -8
478 // TODO: simplify with row based deblocking
481 bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
482 } else if (x >= x_end - 8) {
483 bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
486 bs0 = s->horizontal_bs[(x + y * s->bs_width) >> 2];
487 bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
490 if ((bs0 == 2) || (bs1 == 2)) {
491 const int qp0 = (bs0 == 2) ? ((get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1) : 0;
492 const int qp1 = (bs1 == 2) ? ((get_qPy(s, x + 8, y - 1) + get_qPy(s, x + 8, y) + 1) >> 1) : 0;
494 tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
495 c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
496 c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
497 src = &s->frame->data[chroma][(y / 2) * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
499 no_p[0] = get_pcm(s, x, y - 1);
500 no_p[1] = get_pcm(s, x + 8, y - 1);
501 no_q[0] = get_pcm(s, x, y);
502 no_q[1] = get_pcm(s, x + 8, y);
503 s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
504 s->frame->linesize[chroma],
507 s->hevcdsp.hevc_h_loop_filter_chroma(src,
508 s->frame->linesize[chroma],
516 static int boundary_strength(HEVCContext *s, MvField *curr,
517 uint8_t curr_cbf_luma, MvField *neigh,
518 uint8_t neigh_cbf_luma,
519 RefPicList *neigh_refPicList,
522 int mvs = curr->pred_flag[0] + curr->pred_flag[1];
525 if (curr->is_intra || neigh->is_intra)
527 if (curr_cbf_luma || neigh_cbf_luma)
531 if (mvs == neigh->pred_flag[0] + neigh->pred_flag[1]) {
534 if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]] &&
535 s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
536 neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
537 if ((abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
538 abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
539 (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
540 abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4))
544 } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
545 neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
546 if (abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
547 abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4)
551 } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
552 neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
553 if (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
554 abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4)
566 if (curr->pred_flag[0]) {
568 ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
571 ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
574 if (neigh->pred_flag[0]) {
576 ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
579 ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
582 if (ref_A == ref_B) {
583 if (abs(A.x - B.x) >= 4 || abs(A.y - B.y) >= 4)
595 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
597 int slice_or_tiles_up_boundary,
598 int slice_or_tiles_left_boundary)
600 MvField *tab_mvf = s->ref->tab_mvf;
601 int log2_min_pu_size = s->sps->log2_min_pu_size;
602 int log2_min_tu_size = s->sps->log2_min_tb_size;
603 int min_pu_width = s->sps->min_pu_width;
604 int min_tu_width = s->sps->min_tb_width;
605 int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
606 (x0 >> log2_min_pu_size)].is_intra;
610 if (y0 > 0 && (y0 & 7) == 0) {
611 int yp_pu = (y0 - 1) >> log2_min_pu_size;
612 int yq_pu = y0 >> log2_min_pu_size;
613 int yp_tu = (y0 - 1) >> log2_min_tu_size;
614 int yq_tu = y0 >> log2_min_tu_size;
616 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
617 int x_pu = (x0 + i) >> log2_min_pu_size;
618 int x_tu = (x0 + i) >> log2_min_tu_size;
619 MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
620 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
621 uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
622 uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
623 RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
626 bs = boundary_strength(s, curr, curr_cbf_luma,
627 top, top_cbf_luma, top_refPicList, 1);
628 if (!s->sh.slice_loop_filter_across_slices_enabled_flag &&
629 (slice_or_tiles_up_boundary & 1) &&
630 (y0 % (1 << s->sps->log2_ctb_size)) == 0)
632 else if (!s->pps->loop_filter_across_tiles_enabled_flag &&
633 (slice_or_tiles_up_boundary & 2) &&
634 (y0 % (1 << s->sps->log2_ctb_size)) == 0)
636 if (y0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
639 s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
643 // bs for TU internal horizontal PU boundaries
644 if (log2_trafo_size > s->sps->log2_min_pu_size && !is_intra)
645 for (j = 8; j < (1 << log2_trafo_size); j += 8) {
646 int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
647 int yq_pu = (y0 + j) >> log2_min_pu_size;
648 int yp_tu = (y0 + j - 1) >> log2_min_tu_size;
649 int yq_tu = (y0 + j) >> log2_min_tu_size;
651 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
652 int x_pu = (x0 + i) >> log2_min_pu_size;
653 int x_tu = (x0 + i) >> log2_min_tu_size;
654 MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
655 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
656 uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
657 uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
658 RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
662 bs = boundary_strength(s, curr, curr_cbf_luma,
663 top, top_cbf_luma, top_refPicList, 0);
664 if (s->sh.disable_deblocking_filter_flag == 1)
667 s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
671 // bs for vertical TU boundaries
672 if (x0 > 0 && (x0 & 7) == 0) {
673 int xp_pu = (x0 - 1) >> log2_min_pu_size;
674 int xq_pu = x0 >> log2_min_pu_size;
675 int xp_tu = (x0 - 1) >> log2_min_tu_size;
676 int xq_tu = x0 >> log2_min_tu_size;
678 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
679 int y_pu = (y0 + i) >> log2_min_pu_size;
680 int y_tu = (y0 + i) >> log2_min_tu_size;
681 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
682 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
684 uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
685 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
686 RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
689 bs = boundary_strength(s, curr, curr_cbf_luma,
690 left, left_cbf_luma, left_refPicList, 1);
691 if (!s->sh.slice_loop_filter_across_slices_enabled_flag &&
692 (slice_or_tiles_left_boundary & 1) &&
693 (x0 % (1 << s->sps->log2_ctb_size)) == 0)
695 else if (!s->pps->loop_filter_across_tiles_enabled_flag &&
696 (slice_or_tiles_left_boundary & 2) &&
697 (x0 % (1 << s->sps->log2_ctb_size)) == 0)
699 if (x0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
702 s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
706 // bs for TU internal vertical PU boundaries
707 if (log2_trafo_size > log2_min_pu_size && !is_intra)
708 for (j = 0; j < (1 << log2_trafo_size); j += 4) {
709 int y_pu = (y0 + j) >> log2_min_pu_size;
710 int y_tu = (y0 + j) >> log2_min_tu_size;
712 for (i = 8; i < (1 << log2_trafo_size); i += 8) {
713 int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
714 int xq_pu = (x0 + i) >> log2_min_pu_size;
715 int xp_tu = (x0 + i - 1) >> log2_min_tu_size;
716 int xq_tu = (x0 + i) >> log2_min_tu_size;
717 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
718 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
719 uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
720 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
721 RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
725 bs = boundary_strength(s, curr, curr_cbf_luma,
726 left, left_cbf_luma, left_refPicList, 0);
727 if (s->sh.disable_deblocking_filter_flag == 1)
730 s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
739 void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
741 deblocking_filter_CTB(s, x, y);
742 if (s->sps->sao_enabled)
743 sao_filter_CTB(s, x, y);
746 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
749 ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size);
750 if (y_ctb && x_ctb >= s->sps->width - ctb_size) {
751 ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size);
752 if (s->threads_type == FF_THREAD_FRAME )
753 ff_thread_report_progress(&s->ref->tf, y_ctb - ctb_size, 0);
755 if (x_ctb && y_ctb >= s->sps->height - ctb_size)
756 ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb);