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->ps.pps->cb_qp_offset;
61 offset = s->ps.pps->cr_qp_offset;
63 qp_i = av_clip(qp_y + offset, 0, 57);
64 if (s->ps.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 xBase, int yBase, int log2_cb_size)
81 HEVCLocalContext *lc = s->HEVClc;
82 int ctb_size_mask = (1 << s->ps.sps->log2_ctb_size) - 1;
83 int MinCuQpDeltaSizeMask = (1 << (s->ps.sps->log2_ctb_size -
84 s->ps.pps->diff_cu_qp_delta_depth)) - 1;
85 int xQgBase = xBase - (xBase & MinCuQpDeltaSizeMask);
86 int yQgBase = yBase - (yBase & MinCuQpDeltaSizeMask);
87 int min_cb_width = s->ps.sps->min_cb_width;
88 int x_cb = xQgBase >> s->ps.sps->log2_min_cb_size;
89 int y_cb = yQgBase >> s->ps.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);
94 int qPy_pred, qPy_a, qPy_b;
97 if (lc->first_qp_group || (!xQgBase && !yQgBase)) {
98 lc->first_qp_group = !lc->tu.is_cu_qp_delta_coded;
99 qPy_pred = s->sh.slice_qp;
101 qPy_pred = lc->qPy_pred;
108 qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
114 qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
116 av_assert2(qPy_a >= -s->ps.sps->qp_bd_offset && qPy_a < 52);
117 av_assert2(qPy_b >= -s->ps.sps->qp_bd_offset && qPy_b < 52);
119 return (qPy_a + qPy_b + 1) >> 1;
122 void ff_hevc_set_qPy(HEVCContext *s, int xBase, int yBase, int log2_cb_size)
124 int qp_y = get_qPy_pred(s, xBase, yBase, log2_cb_size);
126 if (s->HEVClc->tu.cu_qp_delta != 0) {
127 int off = s->ps.sps->qp_bd_offset;
128 s->HEVClc->qp_y = FFUMOD(qp_y + s->HEVClc->tu.cu_qp_delta + 52 + 2 * off,
131 s->HEVClc->qp_y = qp_y;
134 static int get_qPy(HEVCContext *s, int xC, int yC)
136 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
137 int x = xC >> log2_min_cb_size;
138 int y = yC >> log2_min_cb_size;
139 return s->qp_y_tab[x + y * s->ps.sps->min_cb_width];
142 static void copy_CTB(uint8_t *dst, const uint8_t *src, int width, int height,
143 intptr_t stride_dst, intptr_t stride_src)
147 if (((intptr_t)dst | (intptr_t)src | stride_dst | stride_src) & 15) {
148 for (i = 0; i < height; i++) {
149 for (j = 0; j < width; j+=8)
150 AV_COPY64U(dst+j, src+j);
155 for (i = 0; i < height; i++) {
156 for (j = 0; j < width; j+=16)
157 AV_COPY128(dst+j, src+j);
164 static void copy_pixel(uint8_t *dst, const uint8_t *src, int pixel_shift)
167 *(uint16_t *)dst = *(uint16_t *)src;
172 static void copy_vert(uint8_t *dst, const uint8_t *src,
173 int pixel_shift, int height,
174 int stride_dst, int stride_src)
177 if (pixel_shift == 0) {
178 for (i = 0; i < height; i++) {
184 for (i = 0; i < height; i++) {
185 *(uint16_t *)dst = *(uint16_t *)src;
192 static void copy_CTB_to_hv(HEVCContext *s, const uint8_t *src,
193 int stride_src, int x, int y, int width, int height,
194 int c_idx, int x_ctb, int y_ctb)
196 int sh = s->ps.sps->pixel_shift;
197 int w = s->ps.sps->width >> s->ps.sps->hshift[c_idx];
198 int h = s->ps.sps->height >> s->ps.sps->vshift[c_idx];
200 /* copy horizontal edges */
201 memcpy(s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb) * w + x) << sh),
203 memcpy(s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb + 1) * w + x) << sh),
204 src + stride_src * (height - 1), width << sh);
206 /* copy vertical edges */
207 copy_vert(s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb) * h + y) << sh), src, sh, height, 1 << sh, stride_src);
209 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);
212 static void restore_tqb_pixels(HEVCContext *s,
213 uint8_t *src1, const uint8_t *dst1,
214 ptrdiff_t stride_src, ptrdiff_t stride_dst,
215 int x0, int y0, int width, int height, int c_idx)
217 if ( s->ps.pps->transquant_bypass_enable_flag ||
218 (s->ps.sps->pcm.loop_filter_disable_flag && s->ps.sps->pcm_enabled_flag)) {
220 int min_pu_size = 1 << s->ps.sps->log2_min_pu_size;
221 int hshift = s->ps.sps->hshift[c_idx];
222 int vshift = s->ps.sps->vshift[c_idx];
223 int x_min = ((x0 ) >> s->ps.sps->log2_min_pu_size);
224 int y_min = ((y0 ) >> s->ps.sps->log2_min_pu_size);
225 int x_max = ((x0 + width ) >> s->ps.sps->log2_min_pu_size);
226 int y_max = ((y0 + height) >> s->ps.sps->log2_min_pu_size);
227 int len = (min_pu_size >> hshift) << s->ps.sps->pixel_shift;
228 for (y = y_min; y < y_max; y++) {
229 for (x = x_min; x < x_max; x++) {
230 if (s->is_pcm[y * s->ps.sps->min_pu_width + x]) {
232 uint8_t *src = src1 + (((y << s->ps.sps->log2_min_pu_size) - y0) >> vshift) * stride_src + ((((x << s->ps.sps->log2_min_pu_size) - x0) >> hshift) << s->ps.sps->pixel_shift);
233 const uint8_t *dst = dst1 + (((y << s->ps.sps->log2_min_pu_size) - y0) >> vshift) * stride_dst + ((((x << s->ps.sps->log2_min_pu_size) - x0) >> hshift) << s->ps.sps->pixel_shift);
234 for (n = 0; n < (min_pu_size >> vshift); n++) {
235 memcpy(src, dst, len);
245 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
247 static void sao_filter_CTB(HEVCContext *s, int x, int y)
249 static const uint8_t sao_tab[8] = { 0, 1, 2, 2, 3, 3, 4, 4 };
250 HEVCLocalContext *lc = s->HEVClc;
252 int edges[4]; // 0 left 1 top 2 right 3 bottom
253 int x_ctb = x >> s->ps.sps->log2_ctb_size;
254 int y_ctb = y >> s->ps.sps->log2_ctb_size;
255 int ctb_addr_rs = y_ctb * s->ps.sps->ctb_width + x_ctb;
256 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
257 SAOParams *sao = &CTB(s->sao, x_ctb, y_ctb);
258 // flags indicating unfilterable edges
259 uint8_t vert_edge[] = { 0, 0 };
260 uint8_t horiz_edge[] = { 0, 0 };
261 uint8_t diag_edge[] = { 0, 0, 0, 0 };
262 uint8_t lfase = CTB(s->filter_slice_edges, x_ctb, y_ctb);
263 uint8_t no_tile_filter = s->ps.pps->tiles_enabled_flag &&
264 !s->ps.pps->loop_filter_across_tiles_enabled_flag;
265 uint8_t restore = no_tile_filter || !lfase;
266 uint8_t left_tile_edge = 0;
267 uint8_t right_tile_edge = 0;
268 uint8_t up_tile_edge = 0;
269 uint8_t bottom_tile_edge = 0;
271 edges[0] = x_ctb == 0;
272 edges[1] = y_ctb == 0;
273 edges[2] = x_ctb == s->ps.sps->ctb_width - 1;
274 edges[3] = y_ctb == s->ps.sps->ctb_height - 1;
278 left_tile_edge = no_tile_filter && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]];
279 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;
282 right_tile_edge = no_tile_filter && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1]];
283 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;
286 up_tile_edge = no_tile_filter && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]];
287 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;
290 bottom_tile_edge = no_tile_filter && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs + s->ps.sps->ctb_width]];
291 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;
293 if (!edges[0] && !edges[1]) {
294 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;
296 if (!edges[1] && !edges[2]) {
297 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;
299 if (!edges[2] && !edges[3]) {
300 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;
302 if (!edges[0] && !edges[3]) {
303 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;
307 for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
308 int x0 = x >> s->ps.sps->hshift[c_idx];
309 int y0 = y >> s->ps.sps->vshift[c_idx];
310 int stride_src = s->frame->linesize[c_idx];
311 int ctb_size_h = (1 << (s->ps.sps->log2_ctb_size)) >> s->ps.sps->hshift[c_idx];
312 int ctb_size_v = (1 << (s->ps.sps->log2_ctb_size)) >> s->ps.sps->vshift[c_idx];
313 int width = FFMIN(ctb_size_h, (s->ps.sps->width >> s->ps.sps->hshift[c_idx]) - x0);
314 int height = FFMIN(ctb_size_v, (s->ps.sps->height >> s->ps.sps->vshift[c_idx]) - y0);
315 int tab = sao_tab[(FFALIGN(width, 8) >> 3) - 1];
316 uint8_t *src = &s->frame->data[c_idx][y0 * stride_src + (x0 << s->ps.sps->pixel_shift)];
320 switch (sao->type_idx[c_idx]) {
322 copy_CTB_to_hv(s, src, stride_src, x0, y0, width, height, c_idx,
324 if (s->ps.pps->transquant_bypass_enable_flag ||
325 (s->ps.sps->pcm.loop_filter_disable_flag && s->ps.sps->pcm_enabled_flag)) {
326 dst = lc->edge_emu_buffer;
327 stride_dst = 2*MAX_PB_SIZE;
328 copy_CTB(dst, src, width << s->ps.sps->pixel_shift, height, stride_dst, stride_src);
329 s->hevcdsp.sao_band_filter[tab](src, dst, stride_src, stride_dst,
330 sao->offset_val[c_idx], sao->band_position[c_idx],
332 restore_tqb_pixels(s, src, dst, stride_src, stride_dst,
333 x, y, width, height, c_idx);
335 s->hevcdsp.sao_band_filter[tab](src, src, stride_src, stride_src,
336 sao->offset_val[c_idx], sao->band_position[c_idx],
339 sao->type_idx[c_idx] = SAO_APPLIED;
343 int w = s->ps.sps->width >> s->ps.sps->hshift[c_idx];
344 int h = s->ps.sps->height >> s->ps.sps->vshift[c_idx];
345 int left_edge = edges[0];
346 int top_edge = edges[1];
347 int right_edge = edges[2];
348 int bottom_edge = edges[3];
349 int sh = s->ps.sps->pixel_shift;
350 int left_pixels, right_pixels;
352 stride_dst = 2*MAX_PB_SIZE + AV_INPUT_BUFFER_PADDING_SIZE;
353 dst = lc->edge_emu_buffer + stride_dst + AV_INPUT_BUFFER_PADDING_SIZE;
356 int left = 1 - left_edge;
357 int right = 1 - right_edge;
358 const uint8_t *src1[2];
362 dst1 = dst - stride_dst - (left << sh);
363 src1[0] = src - stride_src - (left << sh);
364 src1[1] = s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb - 1) * w + x0 - left) << sh);
367 src_idx = (CTB(s->sao, x_ctb-1, y_ctb-1).type_idx[c_idx] ==
369 copy_pixel(dst1, src1[src_idx], sh);
372 src_idx = (CTB(s->sao, x_ctb, y_ctb-1).type_idx[c_idx] ==
374 memcpy(dst1 + pos, src1[src_idx] + pos, width << sh);
377 src_idx = (CTB(s->sao, x_ctb+1, y_ctb-1).type_idx[c_idx] ==
379 copy_pixel(dst1 + pos, src1[src_idx] + pos, sh);
383 int left = 1 - left_edge;
384 int right = 1 - right_edge;
385 const uint8_t *src1[2];
389 dst1 = dst + height * stride_dst - (left << sh);
390 src1[0] = src + height * stride_src - (left << sh);
391 src1[1] = s->sao_pixel_buffer_h[c_idx] + (((2 * y_ctb + 2) * w + x0 - left) << sh);
394 src_idx = (CTB(s->sao, x_ctb-1, y_ctb+1).type_idx[c_idx] ==
396 copy_pixel(dst1, src1[src_idx], sh);
399 src_idx = (CTB(s->sao, x_ctb, y_ctb+1).type_idx[c_idx] ==
401 memcpy(dst1 + pos, src1[src_idx] + pos, width << sh);
404 src_idx = (CTB(s->sao, x_ctb+1, y_ctb+1).type_idx[c_idx] ==
406 copy_pixel(dst1 + pos, src1[src_idx] + pos, sh);
411 if (CTB(s->sao, x_ctb-1, y_ctb).type_idx[c_idx] == SAO_APPLIED) {
412 copy_vert(dst - (1 << sh),
413 s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb - 1) * h + y0) << sh),
414 sh, height, stride_dst, 1 << sh);
421 if (CTB(s->sao, x_ctb+1, y_ctb).type_idx[c_idx] == SAO_APPLIED) {
422 copy_vert(dst + (width << sh),
423 s->sao_pixel_buffer_v[c_idx] + (((2 * x_ctb + 2) * h + y0) << sh),
424 sh, height, stride_dst, 1 << sh);
430 copy_CTB(dst - (left_pixels << sh),
431 src - (left_pixels << sh),
432 (width + left_pixels + right_pixels) << sh,
433 height, stride_dst, stride_src);
435 copy_CTB_to_hv(s, src, stride_src, x0, y0, width, height, c_idx,
437 s->hevcdsp.sao_edge_filter[tab](src, dst, stride_src, sao->offset_val[c_idx],
438 sao->eo_class[c_idx], width, height);
439 s->hevcdsp.sao_edge_restore[restore](src, dst,
440 stride_src, stride_dst,
447 restore_tqb_pixels(s, src, dst, stride_src, stride_dst,
448 x, y, width, height, c_idx);
449 sao->type_idx[c_idx] = SAO_APPLIED;
456 static int get_pcm(HEVCContext *s, int x, int y)
458 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
464 x_pu = x >> log2_min_pu_size;
465 y_pu = y >> log2_min_pu_size;
467 if (x_pu >= s->ps.sps->min_pu_width || y_pu >= s->ps.sps->min_pu_height)
469 return s->is_pcm[y_pu * s->ps.sps->min_pu_width + x_pu];
472 #define TC_CALC(qp, bs) \
473 tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) + \
474 (tc_offset >> 1 << 1), \
475 0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
477 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
482 int32_t c_tc[2], tc[2];
483 uint8_t no_p[2] = { 0 };
484 uint8_t no_q[2] = { 0 };
486 int log2_ctb_size = s->ps.sps->log2_ctb_size;
487 int x_end, x_end2, y_end;
488 int ctb_size = 1 << log2_ctb_size;
489 int ctb = (x0 >> log2_ctb_size) +
490 (y0 >> log2_ctb_size) * s->ps.sps->ctb_width;
491 int cur_tc_offset = s->deblock[ctb].tc_offset;
492 int cur_beta_offset = s->deblock[ctb].beta_offset;
493 int left_tc_offset, left_beta_offset;
494 int tc_offset, beta_offset;
495 int pcmf = (s->ps.sps->pcm_enabled_flag &&
496 s->ps.sps->pcm.loop_filter_disable_flag) ||
497 s->ps.pps->transquant_bypass_enable_flag;
500 left_tc_offset = s->deblock[ctb - 1].tc_offset;
501 left_beta_offset = s->deblock[ctb - 1].beta_offset;
504 left_beta_offset = 0;
507 x_end = x0 + ctb_size;
508 if (x_end > s->ps.sps->width)
509 x_end = s->ps.sps->width;
510 y_end = y0 + ctb_size;
511 if (y_end > s->ps.sps->height)
512 y_end = s->ps.sps->height;
514 tc_offset = cur_tc_offset;
515 beta_offset = cur_beta_offset;
518 if (x_end2 != s->ps.sps->width)
520 for (y = y0; y < y_end; y += 8) {
521 // vertical filtering luma
522 for (x = x0 ? x0 : 8; x < x_end; x += 8) {
523 const int bs0 = s->vertical_bs[(x + y * s->bs_width) >> 2];
524 const int bs1 = s->vertical_bs[(x + (y + 4) * s->bs_width) >> 2];
526 const int qp = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
528 beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
530 tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
531 tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
532 src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->ps.sps->pixel_shift)];
534 no_p[0] = get_pcm(s, x - 1, y);
535 no_p[1] = get_pcm(s, x - 1, y + 4);
536 no_q[0] = get_pcm(s, x, y);
537 no_q[1] = get_pcm(s, x, y + 4);
538 s->hevcdsp.hevc_v_loop_filter_luma_c(src,
539 s->frame->linesize[LUMA],
540 beta, tc, no_p, no_q);
542 s->hevcdsp.hevc_v_loop_filter_luma(src,
543 s->frame->linesize[LUMA],
544 beta, tc, no_p, no_q);
551 // horizontal filtering luma
552 for (x = x0 ? x0 - 8 : 0; x < x_end2; x += 8) {
553 const int bs0 = s->horizontal_bs[( x + y * s->bs_width) >> 2];
554 const int bs1 = s->horizontal_bs[((x + 4) + y * s->bs_width) >> 2];
556 const int qp = (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1;
558 tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
559 beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
561 beta = betatable[av_clip(qp + beta_offset, 0, MAX_QP)];
562 tc[0] = bs0 ? TC_CALC(qp, bs0) : 0;
563 tc[1] = bs1 ? TC_CALC(qp, bs1) : 0;
564 src = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->ps.sps->pixel_shift)];
566 no_p[0] = get_pcm(s, x, y - 1);
567 no_p[1] = get_pcm(s, x + 4, y - 1);
568 no_q[0] = get_pcm(s, x, y);
569 no_q[1] = get_pcm(s, x + 4, y);
570 s->hevcdsp.hevc_h_loop_filter_luma_c(src,
571 s->frame->linesize[LUMA],
572 beta, tc, no_p, no_q);
574 s->hevcdsp.hevc_h_loop_filter_luma(src,
575 s->frame->linesize[LUMA],
576 beta, tc, no_p, no_q);
581 if (s->ps.sps->chroma_format_idc) {
582 for (chroma = 1; chroma <= 2; chroma++) {
583 int h = 1 << s->ps.sps->hshift[chroma];
584 int v = 1 << s->ps.sps->vshift[chroma];
586 // vertical filtering chroma
587 for (y = y0; y < y_end; y += (8 * v)) {
588 for (x = x0 ? x0 : 8 * h; x < x_end; x += (8 * h)) {
589 const int bs0 = s->vertical_bs[(x + y * s->bs_width) >> 2];
590 const int bs1 = s->vertical_bs[(x + (y + (4 * v)) * s->bs_width) >> 2];
592 if ((bs0 == 2) || (bs1 == 2)) {
593 const int qp0 = (get_qPy(s, x - 1, y) + get_qPy(s, x, y) + 1) >> 1;
594 const int qp1 = (get_qPy(s, x - 1, y + (4 * v)) + get_qPy(s, x, y + (4 * v)) + 1) >> 1;
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, tc_offset) : 0;
598 src = &s->frame->data[chroma][(y >> s->ps.sps->vshift[chroma]) * s->frame->linesize[chroma] + ((x >> s->ps.sps->hshift[chroma]) << s->ps.sps->pixel_shift)];
600 no_p[0] = get_pcm(s, x - 1, y);
601 no_p[1] = get_pcm(s, x - 1, y + (4 * v));
602 no_q[0] = get_pcm(s, x, y);
603 no_q[1] = get_pcm(s, x, y + (4 * v));
604 s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
605 s->frame->linesize[chroma],
608 s->hevcdsp.hevc_v_loop_filter_chroma(src,
609 s->frame->linesize[chroma],
617 // horizontal filtering chroma
618 tc_offset = x0 ? left_tc_offset : cur_tc_offset;
620 if (x_end != s->ps.sps->width)
621 x_end2 = x_end - 8 * h;
622 for (x = x0 ? x0 - 8 * h : 0; x < x_end2; x += (8 * h)) {
623 const int bs0 = s->horizontal_bs[( x + y * s->bs_width) >> 2];
624 const int bs1 = s->horizontal_bs[((x + 4 * h) + y * s->bs_width) >> 2];
625 if ((bs0 == 2) || (bs1 == 2)) {
626 const int qp0 = bs0 == 2 ? (get_qPy(s, x, y - 1) + get_qPy(s, x, y) + 1) >> 1 : 0;
627 const int qp1 = bs1 == 2 ? (get_qPy(s, x + (4 * h), y - 1) + get_qPy(s, x + (4 * h), y) + 1) >> 1 : 0;
629 c_tc[0] = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
630 c_tc[1] = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
631 src = &s->frame->data[chroma][(y >> s->ps.sps->vshift[1]) * s->frame->linesize[chroma] + ((x >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
633 no_p[0] = get_pcm(s, x, y - 1);
634 no_p[1] = get_pcm(s, x + (4 * h), y - 1);
635 no_q[0] = get_pcm(s, x, y);
636 no_q[1] = get_pcm(s, x + (4 * h), y);
637 s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
638 s->frame->linesize[chroma],
641 s->hevcdsp.hevc_h_loop_filter_chroma(src,
642 s->frame->linesize[chroma],
651 static int boundary_strength(HEVCContext *s, MvField *curr, MvField *neigh,
652 RefPicList *neigh_refPicList)
654 if (curr->pred_flag == PF_BI && neigh->pred_flag == PF_BI) {
656 if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]] &&
657 s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
658 neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
659 if ((FFABS(neigh->mv[0].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
660 FFABS(neigh->mv[1].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
661 (FFABS(neigh->mv[1].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
662 FFABS(neigh->mv[0].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[1].y) >= 4))
666 } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
667 neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
668 if (FFABS(neigh->mv[0].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
669 FFABS(neigh->mv[1].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[1].y) >= 4)
673 } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
674 neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
675 if (FFABS(neigh->mv[1].x - curr->mv[0].x) >= 4 || FFABS(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
676 FFABS(neigh->mv[0].x - curr->mv[1].x) >= 4 || FFABS(neigh->mv[0].y - curr->mv[1].y) >= 4)
683 } else if ((curr->pred_flag != PF_BI) && (neigh->pred_flag != PF_BI)){ // 1 MV
687 if (curr->pred_flag & 1) {
689 ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
692 ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
695 if (neigh->pred_flag & 1) {
697 ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
700 ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
703 if (ref_A == ref_B) {
704 if (FFABS(A.x - B.x) >= 4 || FFABS(A.y - B.y) >= 4)
715 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
718 HEVCLocalContext *lc = s->HEVClc;
719 MvField *tab_mvf = s->ref->tab_mvf;
720 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
721 int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
722 int min_pu_width = s->ps.sps->min_pu_width;
723 int min_tu_width = s->ps.sps->min_tb_width;
724 int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
725 (x0 >> log2_min_pu_size)].pred_flag == PF_INTRA;
726 int boundary_upper, boundary_left;
729 boundary_upper = y0 > 0 && !(y0 & 7);
730 if (boundary_upper &&
731 ((!s->sh.slice_loop_filter_across_slices_enabled_flag &&
732 lc->boundary_flags & BOUNDARY_UPPER_SLICE &&
733 (y0 % (1 << s->ps.sps->log2_ctb_size)) == 0) ||
734 (!s->ps.pps->loop_filter_across_tiles_enabled_flag &&
735 lc->boundary_flags & BOUNDARY_UPPER_TILE &&
736 (y0 % (1 << s->ps.sps->log2_ctb_size)) == 0)))
739 if (boundary_upper) {
740 RefPicList *rpl_top = (lc->boundary_flags & BOUNDARY_UPPER_SLICE) ?
741 ff_hevc_get_ref_list(s, s->ref, x0, y0 - 1) :
743 int yp_pu = (y0 - 1) >> log2_min_pu_size;
744 int yq_pu = y0 >> log2_min_pu_size;
745 int yp_tu = (y0 - 1) >> log2_min_tu_size;
746 int yq_tu = y0 >> log2_min_tu_size;
748 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
749 int x_pu = (x0 + i) >> log2_min_pu_size;
750 int x_tu = (x0 + i) >> log2_min_tu_size;
751 MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
752 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
753 uint8_t top_cbf_luma = s->cbf_luma[yp_tu * min_tu_width + x_tu];
754 uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
756 if (curr->pred_flag == PF_INTRA || top->pred_flag == PF_INTRA)
758 else if (curr_cbf_luma || top_cbf_luma)
761 bs = boundary_strength(s, curr, top, rpl_top);
762 s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
766 // bs for vertical TU boundaries
767 boundary_left = x0 > 0 && !(x0 & 7);
769 ((!s->sh.slice_loop_filter_across_slices_enabled_flag &&
770 lc->boundary_flags & BOUNDARY_LEFT_SLICE &&
771 (x0 % (1 << s->ps.sps->log2_ctb_size)) == 0) ||
772 (!s->ps.pps->loop_filter_across_tiles_enabled_flag &&
773 lc->boundary_flags & BOUNDARY_LEFT_TILE &&
774 (x0 % (1 << s->ps.sps->log2_ctb_size)) == 0)))
778 RefPicList *rpl_left = (lc->boundary_flags & BOUNDARY_LEFT_SLICE) ?
779 ff_hevc_get_ref_list(s, s->ref, x0 - 1, y0) :
781 int xp_pu = (x0 - 1) >> log2_min_pu_size;
782 int xq_pu = x0 >> log2_min_pu_size;
783 int xp_tu = (x0 - 1) >> log2_min_tu_size;
784 int xq_tu = x0 >> log2_min_tu_size;
786 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
787 int y_pu = (y0 + i) >> log2_min_pu_size;
788 int y_tu = (y0 + i) >> log2_min_tu_size;
789 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
790 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
791 uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
792 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
794 if (curr->pred_flag == PF_INTRA || left->pred_flag == PF_INTRA)
796 else if (curr_cbf_luma || left_cbf_luma)
799 bs = boundary_strength(s, curr, left, rpl_left);
800 s->vertical_bs[(x0 + (y0 + i) * s->bs_width) >> 2] = bs;
804 if (log2_trafo_size > log2_min_pu_size && !is_intra) {
805 RefPicList *rpl = s->ref->refPicList;
807 // bs for TU internal horizontal PU boundaries
808 for (j = 8; j < (1 << log2_trafo_size); j += 8) {
809 int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
810 int yq_pu = (y0 + j) >> log2_min_pu_size;
812 for (i = 0; i < (1 << log2_trafo_size); i += 4) {
813 int x_pu = (x0 + i) >> log2_min_pu_size;
814 MvField *top = &tab_mvf[yp_pu * min_pu_width + x_pu];
815 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
817 bs = boundary_strength(s, curr, top, rpl);
818 s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
822 // bs for TU internal vertical PU boundaries
823 for (j = 0; j < (1 << log2_trafo_size); j += 4) {
824 int y_pu = (y0 + j) >> log2_min_pu_size;
826 for (i = 8; i < (1 << log2_trafo_size); i += 8) {
827 int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
828 int xq_pu = (x0 + i) >> log2_min_pu_size;
829 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
830 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
832 bs = boundary_strength(s, curr, left, rpl);
833 s->vertical_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
843 void ff_hevc_hls_filter(HEVCContext *s, int x, int y, int ctb_size)
845 int x_end = x >= s->ps.sps->width - ctb_size;
846 if (s->avctx->skip_loop_filter < AVDISCARD_ALL)
847 deblocking_filter_CTB(s, x, y);
848 if (s->ps.sps->sao_enabled) {
849 int y_end = y >= s->ps.sps->height - ctb_size;
851 sao_filter_CTB(s, x - ctb_size, y - ctb_size);
853 sao_filter_CTB(s, x - ctb_size, y);
855 sao_filter_CTB(s, x, y - ctb_size);
856 if (s->threads_type & FF_THREAD_FRAME )
857 ff_thread_report_progress(&s->ref->tf, y, 0);
859 if (x_end && y_end) {
860 sao_filter_CTB(s, x , y);
861 if (s->threads_type & FF_THREAD_FRAME )
862 ff_thread_report_progress(&s->ref->tf, y + ctb_size, 0);
864 } else if (s->threads_type & FF_THREAD_FRAME && x_end)
865 ff_thread_report_progress(&s->ref->tf, y + ctb_size - 4, 0);
868 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
870 int x_end = x_ctb >= s->ps.sps->width - ctb_size;
871 int y_end = y_ctb >= s->ps.sps->height - ctb_size;
873 ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size, ctb_size);
875 ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size, ctb_size);
877 ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb, ctb_size);