]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_filter.c
Merge commit '61e7c7f27b0a2652bf5cd282b97762ee99d025ef'
[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, qp_i, offset, idxt;
56
57     // slice qp offset is not used for deblocking
58     if (c_idx == 1)
59         offset = s->pps->cb_qp_offset;
60     else
61         offset = s->pps->cr_qp_offset;
62
63     qp_i = av_clip_c(qp_y + offset, 0, 57);
64     if (qp_i < 30)
65         qp = qp_i;
66     else if (qp_i > 43)
67         qp = qp_i - 6;
68     else
69         qp = qp_c[qp_i - 30];
70
71     idxt = av_clip_c(qp + DEFAULT_INTRA_TC_OFFSET + tc_offset, 0, 53);
72     return tctable[idxt];
73 }
74
75 static int get_qPy_pred(HEVCContext *s, int xC, int yC,
76                         int xBase, int yBase, int log2_cb_size)
77 {
78     HEVCLocalContext *lc     = s->HEVClc;
79     int ctb_size_mask        = (1 << s->sps->log2_ctb_size) - 1;
80     int MinCuQpDeltaSizeMask = (1 << (s->sps->log2_ctb_size -
81                                       s->pps->diff_cu_qp_delta_depth)) - 1;
82     int xQgBase              = xBase - (xBase & MinCuQpDeltaSizeMask);
83     int yQgBase              = yBase - (yBase & MinCuQpDeltaSizeMask);
84     int min_cb_width         = s->sps->min_cb_width;
85     int x_cb                 = xQgBase >> s->sps->log2_min_cb_size;
86     int y_cb                 = yQgBase >> s->sps->log2_min_cb_size;
87     int availableA           = (xBase   & ctb_size_mask) &&
88                                (xQgBase & ctb_size_mask);
89     int availableB           = (yBase   & ctb_size_mask) &&
90                                (yQgBase & ctb_size_mask);
91     int qPy_pred, qPy_a, qPy_b;
92
93     // qPy_pred
94     if (lc->first_qp_group || (!xQgBase && !yQgBase)) {
95         lc->first_qp_group = !lc->tu.is_cu_qp_delta_coded;
96         qPy_pred = s->sh.slice_qp;
97     } else {
98         qPy_pred = lc->qPy_pred;
99     }
100
101     // qPy_a
102     if (availableA == 0)
103         qPy_a = qPy_pred;
104     else
105         qPy_a = s->qp_y_tab[(x_cb - 1) + y_cb * min_cb_width];
106
107     // qPy_b
108     if (availableB == 0)
109         qPy_b = qPy_pred;
110     else
111         qPy_b = s->qp_y_tab[x_cb + (y_cb - 1) * min_cb_width];
112
113     av_assert2(qPy_a >= -s->sps->qp_bd_offset && qPy_a < 52);
114     av_assert2(qPy_b >= -s->sps->qp_bd_offset && qPy_b < 52);
115
116     return (qPy_a + qPy_b + 1) >> 1;
117 }
118
119 void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC,
120                      int xBase, int yBase, int log2_cb_size)
121 {
122     int qp_y = get_qPy_pred(s, xC, yC, xBase, yBase, log2_cb_size);
123
124     if (s->HEVClc->tu.cu_qp_delta != 0) {
125         int off = s->sps->qp_bd_offset;
126         s->HEVClc->qp_y = FFUMOD(qp_y + s->HEVClc->tu.cu_qp_delta + 52 + 2 * off,
127                                  52 + off) - off;
128     } else
129         s->HEVClc->qp_y = qp_y;
130 }
131
132 static int get_qPy(HEVCContext *s, int xC, int yC)
133 {
134     int log2_min_cb_size  = s->sps->log2_min_cb_size;
135     int x                 = xC >> log2_min_cb_size;
136     int y                 = yC >> log2_min_cb_size;
137     return s->qp_y_tab[x + y * s->sps->min_cb_width];
138 }
139
140 static void copy_CTB(uint8_t *dst, uint8_t *src,
141                      int width, int height, int stride)
142 {
143     int i;
144
145     for (i = 0; i < height; i++) {
146         memcpy(dst, src, width);
147         dst += stride;
148         src += stride;
149     }
150 }
151
152 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
153
154 static void sao_filter_CTB(HEVCContext *s, int x, int y)
155 {
156     //  TODO: This should be easily parallelizable
157     //  TODO: skip CBs when (cu_transquant_bypass_flag || (pcm_loop_filter_disable_flag && pcm_flag))
158     int c_idx = 0;
159     int class = 1, class_index;
160     int edges[4];  // 0 left 1 top 2 right 3 bottom
161     SAOParams *sao[4];
162     int classes[4];
163     int x_shift = 0, y_shift = 0;
164     int x_ctb = x >> s->sps->log2_ctb_size;
165     int y_ctb = y >> s->sps->log2_ctb_size;
166     int ctb_addr_rs = y_ctb * s->sps->ctb_width + x_ctb;
167     int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
168
169     // flags indicating unfilterable edges
170     uint8_t vert_edge[]  = { 0, 0, 0, 0 };
171     uint8_t horiz_edge[] = { 0, 0, 0, 0 };
172     uint8_t diag_edge[]  = { 0, 0, 0, 0 };
173     uint8_t lfase[3]; // current, above, left
174     uint8_t no_tile_filter = s->pps->tiles_enabled_flag &&
175                              !s->pps->loop_filter_across_tiles_enabled_flag;
176     uint8_t left_tile_edge = 0;
177     uint8_t up_tile_edge = 0;
178
179     sao[0]     = &CTB(s->sao, x_ctb, y_ctb);
180     edges[0]   = x_ctb == 0;
181     edges[1]   = y_ctb == 0;
182     edges[2]   = x_ctb == s->sps->ctb_width  - 1;
183     edges[3]   = y_ctb == s->sps->ctb_height - 1;
184     lfase[0]   = CTB(s->filter_slice_edges, x_ctb, y_ctb);
185     classes[0] = 0;
186
187     if (!edges[0]) {
188         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]];
189         sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb);
190         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;
191         vert_edge[2] = vert_edge[0];
192         lfase[2]     = CTB(s->filter_slice_edges, x_ctb - 1, y_ctb);
193         classes[class] = 2;
194         class++;
195         x_shift = 8;
196     }
197
198     if (!edges[1]) {
199         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]];
200         sao[class] = &CTB(s->sao, x_ctb, y_ctb - 1);
201         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;
202         horiz_edge[1] = horiz_edge[0];
203         lfase[1] = CTB(s->filter_slice_edges, x_ctb, y_ctb - 1);
204         classes[class] = 1;
205         class++;
206         y_shift = 4;
207
208         if (!edges[0]) {
209             classes[class] = 3;
210             sao[class] = &CTB(s->sao, x_ctb - 1, y_ctb - 1);
211             class++;
212
213             // Tile check here is done current CTB row/col, not above/left like you'd expect,
214             //but that is because the tile boundary always extends through the whole pic
215             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;
216             vert_edge[3] = vert_edge[1];
217             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;
218             horiz_edge[3] = horiz_edge[2];
219             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;
220             diag_edge[3] = diag_edge[0];
221
222             // Does left CTB comes after above CTB?
223             if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) >
224                 CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
225                 diag_edge[2] = !lfase[2] || left_tile_edge || up_tile_edge;
226                 diag_edge[1] = diag_edge[2];
227             } else if (CTB(s->tab_slice_address, x_ctb - 1, y_ctb) <
228                        CTB(s->tab_slice_address, x_ctb, y_ctb - 1)) {
229                 diag_edge[1] = !lfase[1] || left_tile_edge || up_tile_edge;
230                 diag_edge[2] = diag_edge[1];
231             } else {
232                 // Same slice, only consider tiles
233                 diag_edge[2] = left_tile_edge || up_tile_edge;
234                 diag_edge[1] = diag_edge[2];
235             }
236         }
237     }
238
239     for (c_idx = 0; c_idx < 3; c_idx++) {
240         int chroma = c_idx ? 1 : 0;
241         int x0 = x >> chroma;
242         int y0 = y >> chroma;
243         int stride = s->frame->linesize[c_idx];
244         int ctb_size = (1 << (s->sps->log2_ctb_size)) >> s->sps->hshift[c_idx];
245         int width = FFMIN(ctb_size,
246                           (s->sps->width >> s->sps->hshift[c_idx]) - x0);
247         int height = FFMIN(ctb_size,
248                            (s->sps->height >> s->sps->vshift[c_idx]) - y0);
249
250         uint8_t *src = &s->frame->data[c_idx][y0 * stride + (x0 << s->sps->pixel_shift)];
251         uint8_t *dst = &s->sao_frame->data[c_idx][y0 * stride + (x0 << s->sps->pixel_shift)];
252         int offset = (y_shift >> chroma) * stride + ((x_shift >> chroma) << s->sps->pixel_shift);
253
254         copy_CTB(dst - offset, src - offset,
255                  (edges[2] ? width  + (x_shift >> chroma) : width)  << s->sps->pixel_shift,
256                  (edges[3] ? height + (y_shift >> chroma) : height), stride);
257
258         for (class_index = 0; class_index < class; class_index++) {
259
260             switch (sao[class_index]->type_idx[c_idx]) {
261             case SAO_BAND:
262                 s->hevcdsp.sao_band_filter[classes[class_index]](dst, src,
263                                                                  stride,
264                                                                  sao[class_index],
265                                                                  edges, width,
266                                                                  height, c_idx);
267                 break;
268             case SAO_EDGE:
269                 s->hevcdsp.sao_edge_filter[classes[class_index]](dst, src,
270                                                                  stride,
271                                                                  sao[class_index],
272                                                                  edges, width,
273                                                                  height, c_idx,
274                                                                  vert_edge[classes[class_index]],
275                                                                  horiz_edge[classes[class_index]],
276                                                                  diag_edge[classes[class_index]]);
277                 break;
278             }
279         }
280     }
281 }
282
283 static int get_pcm(HEVCContext *s, int x, int y)
284 {
285     int log2_min_pu_size = s->sps->log2_min_pu_size;
286     int x_pu             = x >> log2_min_pu_size;
287     int y_pu             = y >> log2_min_pu_size;
288
289     if (x < 0 || x_pu >= s->sps->min_pu_width ||
290         y < 0 || y_pu >= s->sps->min_pu_height)
291         return 2;
292     return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
293 }
294
295 #define TC_CALC(qp, bs)                                                 \
296     tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) +       \
297                     (tc_offset >> 1 << 1),                              \
298                     0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
299
300 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
301 {
302     uint8_t *src;
303     int x, y;
304     int chroma;
305     int c_tc[2], beta[2], tc[2];
306     uint8_t no_p[2] = { 0 };
307     uint8_t no_q[2] = { 0 };
308
309     int log2_ctb_size = s->sps->log2_ctb_size;
310     int x_end, y_end;
311     int ctb_size        = 1 << log2_ctb_size;
312     int ctb             = (x0 >> log2_ctb_size) +
313                           (y0 >> log2_ctb_size) * s->sps->ctb_width;
314     int cur_tc_offset   = s->deblock[ctb].tc_offset;
315     int cur_beta_offset = s->deblock[ctb].beta_offset;
316     int left_tc_offset, left_beta_offset;
317     int tc_offset, beta_offset;
318     int pcmf = (s->sps->pcm_enabled_flag &&
319                 s->sps->pcm.loop_filter_disable_flag) ||
320                s->pps->transquant_bypass_enable_flag;
321
322     if (x0) {
323         left_tc_offset   = s->deblock[ctb - 1].tc_offset;
324         left_beta_offset = s->deblock[ctb - 1].beta_offset;
325     }
326
327     x_end = x0 + ctb_size;
328     if (x_end > s->sps->width)
329         x_end = s->sps->width;
330     y_end = y0 + ctb_size;
331     if (y_end > s->sps->height)
332         y_end = s->sps->height;
333
334     tc_offset   = cur_tc_offset;
335     beta_offset = cur_beta_offset;
336
337     // vertical filtering luma
338     for (y = y0; y < y_end; y += 8) {
339         for (x = x0 ? x0 : 8; x < x_end; x += 8) {
340             const int bs0 = s->vertical_bs[(x >> 3) + (y       >> 2) * s->bs_width];
341             const int bs1 = s->vertical_bs[(x >> 3) + ((y + 4) >> 2) * s->bs_width];
342             if (bs0 || bs1) {
343                 const int qp0 = (get_qPy(s, x - 1, y)     + get_qPy(s, x, y)     + 1) >> 1;
344                 const int qp1 = (get_qPy(s, x - 1, y + 4) + get_qPy(s, x, y + 4) + 1) >> 1;
345
346                 beta[0] = betatable[av_clip(qp0 + (beta_offset >> 1 << 1), 0, MAX_QP)];
347                 beta[1] = betatable[av_clip(qp1 + (beta_offset >> 1 << 1), 0, MAX_QP)];
348                 tc[0]   = bs0 ? TC_CALC(qp0, bs0) : 0;
349                 tc[1]   = bs1 ? TC_CALC(qp1, bs1) : 0;
350                 src     = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
351                 if (pcmf) {
352                     no_p[0] = get_pcm(s, x - 1, y);
353                     no_p[1] = get_pcm(s, x - 1, y + 4);
354                     no_q[0] = get_pcm(s, x, y);
355                     no_q[1] = get_pcm(s, x, y + 4);
356                     s->hevcdsp.hevc_v_loop_filter_luma_c(src,
357                                                          s->frame->linesize[LUMA],
358                                                          beta, tc, no_p, no_q);
359                 } else
360                     s->hevcdsp.hevc_v_loop_filter_luma(src,
361                                                        s->frame->linesize[LUMA],
362                                                        beta, tc, no_p, no_q);
363             }
364         }
365     }
366
367     // vertical filtering chroma
368     for (chroma = 1; chroma <= 2; chroma++) {
369         for (y = y0; y < y_end; y += 16) {
370             for (x = x0 ? x0 : 16; x < x_end; x += 16) {
371                 const int bs0 = s->vertical_bs[(x >> 3) + (y       >> 2) * s->bs_width];
372                 const int bs1 = s->vertical_bs[(x >> 3) + ((y + 8) >> 2) * s->bs_width];
373                 if ((bs0 == 2) || (bs1 == 2)) {
374                     const int qp0 = (get_qPy(s, x - 1, y)     + get_qPy(s, x, y)     + 1) >> 1;
375                     const int qp1 = (get_qPy(s, x - 1, y + 8) + get_qPy(s, x, y + 8) + 1) >> 1;
376
377                     c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
378                     c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
379                     src     = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
380                     if (pcmf) {
381                         no_p[0] = get_pcm(s, x - 1, y);
382                         no_p[1] = get_pcm(s, x - 1, y + 8);
383                         no_q[0] = get_pcm(s, x, y);
384                         no_q[1] = get_pcm(s, x, y + 8);
385                         s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
386                                                                s->frame->linesize[chroma],
387                                                                c_tc, no_p, no_q);
388                     } else
389                         s->hevcdsp.hevc_v_loop_filter_chroma(src,
390                                                              s->frame->linesize[chroma],
391                                                              c_tc, no_p, no_q);
392                 }
393             }
394         }
395     }
396
397     // horizontal filtering luma
398     if (x_end != s->sps->width)
399         x_end -= 8;
400     for (y = y0 ? y0 : 8; y < y_end; y += 8) {
401         for (x = x0 ? x0 - 8 : 0; x < x_end; x += 8) {
402             const int bs0 = s->horizontal_bs[(x +     y * s->bs_width) >> 2];
403             const int bs1 = s->horizontal_bs[(x + 4 + y * s->bs_width) >> 2];
404             if (bs0 || bs1) {
405                 const int qp0 = (get_qPy(s, x, y - 1)     + get_qPy(s, x, y)     + 1) >> 1;
406                 const int qp1 = (get_qPy(s, x + 4, y - 1) + get_qPy(s, x + 4, y) + 1) >> 1;
407
408                 tc_offset   = x >= x0 ? cur_tc_offset : left_tc_offset;
409                 beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
410
411                 beta[0] = betatable[av_clip(qp0 + (beta_offset >> 1 << 1), 0, MAX_QP)];
412                 beta[1] = betatable[av_clip(qp1 + (beta_offset >> 1 << 1), 0, MAX_QP)];
413                 tc[0]   = bs0 ? TC_CALC(qp0, bs0) : 0;
414                 tc[1]   = bs1 ? TC_CALC(qp1, bs1) : 0;
415                 src     = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
416                 if (pcmf) {
417                     no_p[0] = get_pcm(s, x, y - 1);
418                     no_p[1] = get_pcm(s, x + 4, y - 1);
419                     no_q[0] = get_pcm(s, x, y);
420                     no_q[1] = get_pcm(s, x + 4, y);
421                     s->hevcdsp.hevc_h_loop_filter_luma_c(src,
422                                                          s->frame->linesize[LUMA],
423                                                          beta, tc, no_p, no_q);
424                 } else
425                     s->hevcdsp.hevc_h_loop_filter_luma(src,
426                                                        s->frame->linesize[LUMA],
427                                                        beta, tc, no_p, no_q);
428             }
429         }
430     }
431
432     // horizontal filtering chroma
433     for (chroma = 1; chroma <= 2; chroma++) {
434         for (y = y0 ? y0 : 16; y < y_end; y += 16) {
435             for (x = x0 - 8; x < x_end; x += 16) {
436                 int bs0, bs1;
437                 // to make sure no memory access over boundary when x = -8
438                 // TODO: simplify with row based deblocking
439                 if (x < 0) {
440                     bs0 = 0;
441                     bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
442                 } else if (x >= x_end - 8) {
443                     bs0 = s->horizontal_bs[(x +     y * s->bs_width) >> 2];
444                     bs1 = 0;
445                 } else {
446                     bs0 = s->horizontal_bs[(x + y     * s->bs_width) >> 2];
447                     bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
448                 }
449
450                 if ((bs0 == 2) || (bs1 == 2)) {
451                     const int qp0 = bs0 == 2 ? (get_qPy(s, x,     y - 1) + get_qPy(s, x,     y) + 1) >> 1 : 0;
452                     const int qp1 = bs1 == 2 ? (get_qPy(s, x + 8, y - 1) + get_qPy(s, x + 8, y) + 1) >> 1 : 0;
453
454                     tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
455                     c_tc[0]   = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset)     : 0;
456                     c_tc[1]   = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
457                     src       = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
458                     if (pcmf) {
459                         no_p[0] = get_pcm(s, x, y - 1);
460                         no_p[1] = get_pcm(s, x + 8, y - 1);
461                         no_q[0] = get_pcm(s, x, y);
462                         no_q[1] = get_pcm(s, x + 8, y);
463                         s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
464                                                                s->frame->linesize[chroma],
465                                                                c_tc, no_p, no_q);
466                     } else
467                         s->hevcdsp.hevc_h_loop_filter_chroma(src,
468                                                              s->frame->linesize[chroma],
469                                                              c_tc, no_p, no_q);
470                 }
471             }
472         }
473     }
474 }
475
476 static int boundary_strength(HEVCContext *s, MvField *curr,
477                              uint8_t curr_cbf_luma, MvField *neigh,
478                              uint8_t neigh_cbf_luma,
479                              RefPicList *neigh_refPicList,
480                              int tu_border)
481 {
482     int mvs = curr->pred_flag[0] + curr->pred_flag[1];
483
484     if (tu_border) {
485         if (curr->is_intra || neigh->is_intra)
486             return 2;
487         if (curr_cbf_luma || neigh_cbf_luma)
488             return 1;
489     }
490
491     if (mvs == neigh->pred_flag[0] + neigh->pred_flag[1]) {
492         if (mvs == 2) {
493             // same L0 and L1
494             if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]]  &&
495                 s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
496                 neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
497                 if ((abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
498                      abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
499                     (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
500                      abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4))
501                     return 1;
502                 else
503                     return 0;
504             } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
505                        neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
506                 if (abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
507                     abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4)
508                     return 1;
509                 else
510                     return 0;
511             } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
512                        neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
513                 if (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
514                     abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4)
515                     return 1;
516                 else
517                     return 0;
518             } else {
519                 return 1;
520             }
521         } else { // 1 MV
522             Mv A, B;
523             int ref_A, ref_B;
524
525             if (curr->pred_flag[0]) {
526                 A     = curr->mv[0];
527                 ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
528             } else {
529                 A     = curr->mv[1];
530                 ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
531             }
532
533             if (neigh->pred_flag[0]) {
534                 B     = neigh->mv[0];
535                 ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
536             } else {
537                 B     = neigh->mv[1];
538                 ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
539             }
540
541             if (ref_A == ref_B) {
542                 if (abs(A.x - B.x) >= 4 || abs(A.y - B.y) >= 4)
543                     return 1;
544                 else
545                     return 0;
546             } else
547                 return 1;
548         }
549     }
550
551     return 1;
552 }
553
554 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
555                                            int log2_trafo_size,
556                                            int slice_or_tiles_up_boundary,
557                                            int slice_or_tiles_left_boundary)
558 {
559     MvField *tab_mvf     = s->ref->tab_mvf;
560     int log2_min_pu_size = s->sps->log2_min_pu_size;
561     int log2_min_tu_size = s->sps->log2_min_tb_size;
562     int min_pu_width     = s->sps->min_pu_width;
563     int min_tu_width     = s->sps->min_tb_width;
564     int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
565                            (x0 >> log2_min_pu_size)].is_intra;
566     int i, j, bs;
567
568     if (y0 > 0 && (y0 & 7) == 0) {
569         int yp_pu = (y0 - 1) >> log2_min_pu_size;
570         int yq_pu =  y0      >> log2_min_pu_size;
571         int yp_tu = (y0 - 1) >> log2_min_tu_size;
572         int yq_tu =  y0      >> log2_min_tu_size;
573
574         for (i = 0; i < (1 << log2_trafo_size); i += 4) {
575             int x_pu = (x0 + i) >> log2_min_pu_size;
576             int x_tu = (x0 + i) >> log2_min_tu_size;
577             MvField *top  = &tab_mvf[yp_pu * min_pu_width + x_pu];
578             MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
579             uint8_t top_cbf_luma  = s->cbf_luma[yp_tu * min_tu_width + x_tu];
580             uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
581             RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
582                                                               x0 + i, y0 - 1);
583
584             bs = boundary_strength(s, curr, curr_cbf_luma,
585                                    top, top_cbf_luma, top_refPicList, 1);
586             if (!s->sh.slice_loop_filter_across_slices_enabled_flag &&
587                 (slice_or_tiles_up_boundary & 1) &&
588                 (y0 % (1 << s->sps->log2_ctb_size)) == 0)
589                 bs = 0;
590             else if (!s->pps->loop_filter_across_tiles_enabled_flag &&
591                      (slice_or_tiles_up_boundary & 2) &&
592                      (y0 % (1 << s->sps->log2_ctb_size)) == 0)
593                 bs = 0;
594             if (y0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
595                 bs = 0;
596             if (bs)
597                 s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
598         }
599     }
600
601     // bs for TU internal horizontal PU boundaries
602     if (log2_trafo_size > s->sps->log2_min_pu_size && !is_intra)
603         for (j = 8; j < (1 << log2_trafo_size); j += 8) {
604             int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
605             int yq_pu = (y0 + j)     >> log2_min_pu_size;
606             int yp_tu = (y0 + j - 1) >> log2_min_tu_size;
607             int yq_tu = (y0 + j)     >> log2_min_tu_size;
608
609             for (i = 0; i < (1 << log2_trafo_size); i += 4) {
610                 int x_pu = (x0 + i) >> log2_min_pu_size;
611                 int x_tu = (x0 + i) >> log2_min_tu_size;
612                 MvField *top  = &tab_mvf[yp_pu * min_pu_width + x_pu];
613                 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
614                 uint8_t top_cbf_luma  = s->cbf_luma[yp_tu * min_tu_width + x_tu];
615                 uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
616                 RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
617                                                                   x0 + i,
618                                                                   y0 + j - 1);
619
620                 bs = boundary_strength(s, curr, curr_cbf_luma,
621                                        top, top_cbf_luma, top_refPicList, 0);
622                 if (s->sh.disable_deblocking_filter_flag == 1)
623                     bs = 0;
624                 if (bs)
625                     s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
626             }
627         }
628
629     // bs for vertical TU boundaries
630     if (x0 > 0 && (x0 & 7) == 0) {
631         int xp_pu = (x0 - 1) >> log2_min_pu_size;
632         int xq_pu =  x0      >> log2_min_pu_size;
633         int xp_tu = (x0 - 1) >> log2_min_tu_size;
634         int xq_tu =  x0      >> log2_min_tu_size;
635
636         for (i = 0; i < (1 << log2_trafo_size); i += 4) {
637             int y_pu      = (y0 + i) >> log2_min_pu_size;
638             int y_tu      = (y0 + i) >> log2_min_tu_size;
639             MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
640             MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
641
642             uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
643             uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
644             RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
645                                                                x0 - 1, y0 + i);
646
647             bs = boundary_strength(s, curr, curr_cbf_luma,
648                                    left, left_cbf_luma, left_refPicList, 1);
649             if (!s->sh.slice_loop_filter_across_slices_enabled_flag &&
650                 (slice_or_tiles_left_boundary & 1) &&
651                 (x0 % (1 << s->sps->log2_ctb_size)) == 0)
652                 bs = 0;
653             else if (!s->pps->loop_filter_across_tiles_enabled_flag &&
654                      (slice_or_tiles_left_boundary & 2) &&
655                      (x0 % (1 << s->sps->log2_ctb_size)) == 0)
656                 bs = 0;
657             if (x0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
658                 bs = 0;
659             if (bs)
660                 s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
661         }
662     }
663
664     // bs for TU internal vertical PU boundaries
665     if (log2_trafo_size > log2_min_pu_size && !is_intra)
666         for (j = 0; j < (1 << log2_trafo_size); j += 4) {
667             int y_pu = (y0 + j) >> log2_min_pu_size;
668             int y_tu = (y0 + j) >> log2_min_tu_size;
669
670             for (i = 8; i < (1 << log2_trafo_size); i += 8) {
671                 int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
672                 int xq_pu = (x0 + i)     >> log2_min_pu_size;
673                 int xp_tu = (x0 + i - 1) >> log2_min_tu_size;
674                 int xq_tu = (x0 + i)     >> log2_min_tu_size;
675                 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
676                 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
677                 uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
678                 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
679                 RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
680                                                                    x0 + i - 1,
681                                                                    y0 + j);
682
683                 bs = boundary_strength(s, curr, curr_cbf_luma,
684                                        left, left_cbf_luma, left_refPicList, 0);
685                 if (s->sh.disable_deblocking_filter_flag == 1)
686                     bs = 0;
687                 if (bs)
688                     s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
689             }
690         }
691 }
692
693 #undef LUMA
694 #undef CB
695 #undef CR
696
697 void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
698 {
699     deblocking_filter_CTB(s, x, y);
700     if (s->sps->sao_enabled)
701         sao_filter_CTB(s, x, y);
702 }
703
704 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
705 {
706     if (y_ctb && x_ctb)
707         ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size);
708     if (y_ctb && x_ctb >= s->sps->width - ctb_size) {
709         ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size);
710         if (s->threads_type == FF_THREAD_FRAME )
711             ff_thread_report_progress(&s->ref->tf, y_ctb - ctb_size, 0);
712     }
713     if (x_ctb && y_ctb >= s->sps->height - ctb_size)
714         ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb);
715 }