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