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