]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_filter.c
avcodec/hevc: more whitespaces to reduce difference to 064698d381e1e7790f21b0199a8930...
[ffmpeg] / libavcodec / hevc_filter.c
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
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27
28 #include "cabac_functions.h"
29 #include "golomb.h"
30 #include "hevc.h"
31
32 #include "bit_depth_template.c"
33
34 #define LUMA 0
35 #define CB 1
36 #define CR 2
37
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
42 };
43
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
48 };
49
50 static int chroma_tc(HEVCContext *s, int qp_y, int c_idx, int tc_offset)
51 {
52     static const int qp_c[] = {
53         29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
54     };
55     int qp_i, offset;
56     int qp;
57     int idxt;
58
59     // slice qp offset is not used for deblocking
60     if (c_idx == 1)
61         offset = s->pps->cb_qp_offset;
62     else
63         offset = s->pps->cr_qp_offset;
64
65     qp_i = av_clip_c(qp_y + offset, 0, 57);
66     if (qp_i < 30)
67         qp = qp_i;
68     else if (qp_i > 43)
69         qp = qp_i - 6;
70     else
71         qp = qp_c[qp_i - 30];
72
73     idxt = av_clip_c(qp + DEFAULT_INTRA_TC_OFFSET + tc_offset, 0, 53);
74     return tctable[idxt];
75 }
76
77 static int get_qPy_pred(HEVCContext *s, int xC, int yC,
78                         int xBase, int yBase, int log2_cb_size)
79 {
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);
94     int qPy_pred;
95     int qPy_a;
96     int qPy_b;
97
98     // qPy_pred
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;
102     } else {
103         qPy_pred = lc->qp_y;
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 }
115             };
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 }
125             };
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;
131             int x, y;
132
133             x = FFMIN(xC0b +  offsetX[idxX][idxY],             min_cb_width  - 1);
134             y = FFMIN(yC0b + (offsetY[idxX][idxY] & idx_mask), min_cb_height - 1);
135
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;
139                 y = yC0b - 1;
140             }
141             qPy_pred = s->qp_y_tab[y * min_cb_width + x];
142         }
143     }
144
145     // qPy_a
146     if (availableA == 0)
147         qPy_a = qPy_pred;
148     else
149         qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
150
151     // qPy_b
152     if (availableB == 0)
153         qPy_b = qPy_pred;
154     else
155         qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
156
157     return (qPy_a + qPy_b + 1) >> 1;
158 }
159
160 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC,
161                      int xBase, int yBase, int log2_cb_size)
162 {
163     int qp_y = get_qPy_pred(s, xC, yC, xBase, yBase, log2_cb_size);
164
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) %
168                           (52 + off)) - off;
169     } else
170         s->HEVClc->qp_y = qp_y;
171 }
172
173 static int get_qPy(HEVCContext *s, int xC, int yC)
174 {
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];
179 }
180
181 static void copy_CTB(uint8_t *dst, uint8_t *src,
182                      int width, int height, int stride)
183 {
184     int i;
185
186     for (i = 0; i < height; i++) {
187         memcpy(dst, src, width);
188         dst += stride;
189         src += stride;
190     }
191 }
192
193 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
194
195 static void sao_filter_CTB(HEVCContext *s, int x, int y)
196 {
197     //  TODO: This should be easily parallelizable
198     //  TODO: skip CBs when (cu_transquant_bypass_flag || (pcm_loop_filter_disable_flag && pcm_flag))
199     int c_idx = 0;
200     int class = 1, class_index;
201     int edges[4];  // 0 left 1 top 2 right 3 bottom
202     SAOParams *sao[4];
203     int classes[4];
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];
209
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;
219
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);
226     classes[0] = 0;
227
228     if (!edges[0]) {
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);
234         classes[class] = 2;
235         class++;
236         x_shift = 8;
237     }
238
239     if (!edges[1]) {
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);
245         classes[class] = 1;
246         class++;
247         y_shift = 4;
248
249         if (!edges[0]) {
250             classes[class] = 3;
251             sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb - 1);
252             class++;
253
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];
262
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];
272             } else {
273                 // Same slice, only consider tiles
274                 diag_edge[2] = left_tile_edge || up_tile_edge;
275                 diag_edge[1] = diag_edge[2];
276             }
277         }
278     }
279
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);
290
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);
294
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);
298
299         for (class_index = 0; class_index < class; class_index++) {
300
301             switch (sao[class_index]->type_idx[c_idx]) {
302             case SAO_BAND:
303                 s->hevcdsp.sao_band_filter[classes[class_index]](dst, src,
304                                                                  stride,
305                                                                  sao[class_index],
306                                                                  edges, width,
307                                                                  height, c_idx);
308                 break;
309             case SAO_EDGE:
310                 s->hevcdsp.sao_edge_filter[classes[class_index]](dst, src,
311                                                                  stride,
312                                                                  sao[class_index],
313                                                                  edges, width,
314                                                                  height, c_idx,
315                                                                  vert_edge[classes[class_index]],
316                                                                  horiz_edge[classes[class_index]],
317                                                                  diag_edge[classes[class_index]]);
318                 break;
319             }
320         }
321     }
322 }
323
324 static int get_pcm(HEVCContext *s, int x, int y)
325 {
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;
329
330     if (x < 0 || x_pu >= s->sps->min_pu_width ||
331         y < 0 || y_pu >= s->sps->min_pu_height)
332         return 2;
333     return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
334 }
335
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)]
337
338 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
339 {
340     uint8_t *src;
341     int x, y;
342     int chroma;
343     int c_tc[2];
344     int beta[2];
345     int tc[2];
346     uint8_t no_p[2] = { 0 };
347     uint8_t no_q[2] = { 0 };
348
349     int log2_ctb_size = s->sps->log2_ctb_size;
350     int x_end, y_end;
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;
361
362     if (x0) {
363         left_tc_offset   = s->deblock[ctb - 1].tc_offset;
364         left_beta_offset = s->deblock[ctb - 1].beta_offset;
365     }
366
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;
373
374     tc_offset   = cur_tc_offset;
375     beta_offset = cur_beta_offset;
376
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];
382             if (bs0 || bs1) {
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;
385
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)];
391                 if (pcmf) {
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);
399                 } else
400                     s->hevcdsp.hevc_v_loop_filter_luma(src,
401                                                        s->frame->linesize[LUMA],
402                                                        beta, tc, no_p, no_q);
403             }
404         }
405     }
406
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;
416
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)];
420                     if (pcmf) {
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],
427                                                                c_tc, no_p, no_q);
428                     } else
429                         s->hevcdsp.hevc_v_loop_filter_chroma(src,
430                                                              s->frame->linesize[chroma],
431                                                              c_tc, no_p, no_q);
432                 }
433             }
434         }
435     }
436
437     // horizontal filtering luma
438     if (x_end != s->sps->width)
439         x_end -= 8;
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];
444             if (bs0 || bs1) {
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;
447
448                 tc_offset   = x >= x0 ? cur_tc_offset : left_tc_offset;
449                 beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
450
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)];
456                 if (pcmf) {
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);
464                 } else
465                     s->hevcdsp.hevc_h_loop_filter_luma(src,
466                                                        s->frame->linesize[LUMA],
467                                                        beta, tc, no_p, no_q);
468             }
469         }
470     }
471
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) {
476                 int bs0, bs1;
477                 // to make sure no memory access over boundary when x = -8
478                 // TODO: simplify with row based deblocking
479                 if (x < 0) {
480                     bs0 = 0;
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];
484                     bs1 = 0;
485                 } else {
486                     bs0 = s->horizontal_bs[(x + y     * s->bs_width) >> 2];
487                     bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
488                 }
489
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;
493
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)];
498                     if (pcmf) {
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],
505                                                                c_tc, no_p, no_q);
506                     } else
507                         s->hevcdsp.hevc_h_loop_filter_chroma(src,
508                                                              s->frame->linesize[chroma],
509                                                              c_tc, no_p, no_q);
510                 }
511             }
512         }
513     }
514 }
515
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,
520                              int tu_border)
521 {
522     int mvs = curr->pred_flag[0] + curr->pred_flag[1];
523
524     if (tu_border) {
525         if (curr->is_intra || neigh->is_intra)
526             return 2;
527         if (curr_cbf_luma || neigh_cbf_luma)
528             return 1;
529     }
530
531     if (mvs == neigh->pred_flag[0] + neigh->pred_flag[1]) {
532         if (mvs == 2) {
533             // same L0 and L1
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))
541                     return 1;
542                 else
543                     return 0;
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)
548                     return 1;
549                 else
550                     return 0;
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)
555                     return 1;
556                 else
557                     return 0;
558             } else {
559                 return 1;
560             }
561         } else { // 1 MV
562             Mv A, B;
563             int ref_A;
564             int ref_B;
565
566             if (curr->pred_flag[0]) {
567                 A     = curr->mv[0];
568                 ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
569             } else {
570                 A     = curr->mv[1];
571                 ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
572             }
573
574             if (neigh->pred_flag[0]) {
575                 B     = neigh->mv[0];
576                 ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
577             } else {
578                 B     = neigh->mv[1];
579                 ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
580             }
581
582             if (ref_A == ref_B) {
583                 if (abs(A.x - B.x) >= 4 || abs(A.y - B.y) >= 4)
584                     return 1;
585                 else
586                     return 0;
587             } else
588                 return 1;
589         }
590     }
591
592     return 1;
593 }
594
595 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
596                                            int log2_trafo_size,
597                                            int slice_or_tiles_up_boundary,
598                                            int slice_or_tiles_left_boundary)
599 {
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;
607     int i, j;
608     int bs;
609
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;
615
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,
624                                                               x0 + i, y0 - 1);
625
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)
631                 bs = 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)
635                 bs = 0;
636             if (y0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
637                 bs = 0;
638             if (bs)
639                 s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
640         }
641     }
642
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;
650
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,
659                                                                   x0 + i,
660                                                                   y0 + j - 1);
661
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)
665                     bs = 0;
666                 if (bs)
667                     s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
668             }
669         }
670
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;
677
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];
683
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,
687                                                                x0 - 1, y0 + i);
688
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)
694                 bs = 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)
698                 bs = 0;
699             if (x0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
700                 bs = 0;
701             if (bs)
702                 s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
703         }
704     }
705
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;
711
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,
722                                                                    x0 + i - 1,
723                                                                    y0 + j);
724
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)
728                     bs = 0;
729                 if (bs)
730                     s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
731             }
732         }
733 }
734
735 #undef LUMA
736 #undef CB
737 #undef CR
738
739 void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
740 {
741     deblocking_filter_CTB(s, x, y);
742     if (s->sps->sao_enabled)
743         sao_filter_CTB(s, x, y);
744 }
745
746 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
747 {
748     if (y_ctb && x_ctb)
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);
754     }
755     if (x_ctb && y_ctb >= s->sps->height - ctb_size)
756         ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb);
757 }