]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_filter.c
lavu/mem: add av_dynarray_add_nofree function
[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, y_pu;
287
288     if (x < 0 || y < 0)
289         return 2;
290
291     x_pu = x >> log2_min_pu_size;
292     y_pu = y >> log2_min_pu_size;
293
294     if (x_pu >= s->sps->min_pu_width || y_pu >= s->sps->min_pu_height)
295         return 2;
296     return s->is_pcm[y_pu * s->sps->min_pu_width + x_pu];
297 }
298
299 #define TC_CALC(qp, bs)                                                 \
300     tctable[av_clip((qp) + DEFAULT_INTRA_TC_OFFSET * ((bs) - 1) +       \
301                     (tc_offset >> 1 << 1),                              \
302                     0, MAX_QP + DEFAULT_INTRA_TC_OFFSET)]
303
304 static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
305 {
306     uint8_t *src;
307     int x, y;
308     int chroma;
309     int c_tc[2], beta[2], tc[2];
310     uint8_t no_p[2] = { 0 };
311     uint8_t no_q[2] = { 0 };
312
313     int log2_ctb_size = s->sps->log2_ctb_size;
314     int x_end, y_end;
315     int ctb_size        = 1 << log2_ctb_size;
316     int ctb             = (x0 >> log2_ctb_size) +
317                           (y0 >> log2_ctb_size) * s->sps->ctb_width;
318     int cur_tc_offset   = s->deblock[ctb].tc_offset;
319     int cur_beta_offset = s->deblock[ctb].beta_offset;
320     int left_tc_offset, left_beta_offset;
321     int tc_offset, beta_offset;
322     int pcmf = (s->sps->pcm_enabled_flag &&
323                 s->sps->pcm.loop_filter_disable_flag) ||
324                s->pps->transquant_bypass_enable_flag;
325
326     if (x0) {
327         left_tc_offset   = s->deblock[ctb - 1].tc_offset;
328         left_beta_offset = s->deblock[ctb - 1].beta_offset;
329     }
330
331     x_end = x0 + ctb_size;
332     if (x_end > s->sps->width)
333         x_end = s->sps->width;
334     y_end = y0 + ctb_size;
335     if (y_end > s->sps->height)
336         y_end = s->sps->height;
337
338     tc_offset   = cur_tc_offset;
339     beta_offset = cur_beta_offset;
340
341     // vertical filtering luma
342     for (y = y0; y < y_end; y += 8) {
343         for (x = x0 ? x0 : 8; x < x_end; x += 8) {
344             const int bs0 = s->vertical_bs[(x >> 3) + (y       >> 2) * s->bs_width];
345             const int bs1 = s->vertical_bs[(x >> 3) + ((y + 4) >> 2) * s->bs_width];
346             if (bs0 || bs1) {
347                 const int qp0 = (get_qPy(s, x - 1, y)     + get_qPy(s, x, y)     + 1) >> 1;
348                 const int qp1 = (get_qPy(s, x - 1, y + 4) + get_qPy(s, x, y + 4) + 1) >> 1;
349
350                 beta[0] = betatable[av_clip(qp0 + beta_offset, 0, MAX_QP)];
351                 beta[1] = betatable[av_clip(qp1 + beta_offset, 0, MAX_QP)];
352                 tc[0]   = bs0 ? TC_CALC(qp0, bs0) : 0;
353                 tc[1]   = bs1 ? TC_CALC(qp1, bs1) : 0;
354                 src     = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
355                 if (pcmf) {
356                     no_p[0] = get_pcm(s, x - 1, y);
357                     no_p[1] = get_pcm(s, x - 1, y + 4);
358                     no_q[0] = get_pcm(s, x, y);
359                     no_q[1] = get_pcm(s, x, y + 4);
360                     s->hevcdsp.hevc_v_loop_filter_luma_c(src,
361                                                          s->frame->linesize[LUMA],
362                                                          beta, tc, no_p, no_q);
363                 } else
364                     s->hevcdsp.hevc_v_loop_filter_luma(src,
365                                                        s->frame->linesize[LUMA],
366                                                        beta, tc, no_p, no_q);
367             }
368         }
369     }
370
371     // vertical filtering chroma
372     for (chroma = 1; chroma <= 2; chroma++) {
373         for (y = y0; y < y_end; y += 16) {
374             for (x = x0 ? x0 : 16; x < x_end; x += 16) {
375                 const int bs0 = s->vertical_bs[(x >> 3) + (y       >> 2) * s->bs_width];
376                 const int bs1 = s->vertical_bs[(x >> 3) + ((y + 8) >> 2) * s->bs_width];
377                 if ((bs0 == 2) || (bs1 == 2)) {
378                     const int qp0 = (get_qPy(s, x - 1, y)     + get_qPy(s, x, y)     + 1) >> 1;
379                     const int qp1 = (get_qPy(s, x - 1, y + 8) + get_qPy(s, x, y + 8) + 1) >> 1;
380
381                     c_tc[0] = (bs0 == 2) ? chroma_tc(s, qp0, chroma, tc_offset) : 0;
382                     c_tc[1] = (bs1 == 2) ? chroma_tc(s, qp1, chroma, tc_offset) : 0;
383                     src     = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
384                     if (pcmf) {
385                         no_p[0] = get_pcm(s, x - 1, y);
386                         no_p[1] = get_pcm(s, x - 1, y + 8);
387                         no_q[0] = get_pcm(s, x, y);
388                         no_q[1] = get_pcm(s, x, y + 8);
389                         s->hevcdsp.hevc_v_loop_filter_chroma_c(src,
390                                                                s->frame->linesize[chroma],
391                                                                c_tc, no_p, no_q);
392                     } else
393                         s->hevcdsp.hevc_v_loop_filter_chroma(src,
394                                                              s->frame->linesize[chroma],
395                                                              c_tc, no_p, no_q);
396                 }
397             }
398         }
399     }
400
401     // horizontal filtering luma
402     if (x_end != s->sps->width)
403         x_end -= 8;
404     for (y = y0 ? y0 : 8; y < y_end; y += 8) {
405         for (x = x0 ? x0 - 8 : 0; x < x_end; x += 8) {
406             const int bs0 = s->horizontal_bs[(x +     y * s->bs_width) >> 2];
407             const int bs1 = s->horizontal_bs[(x + 4 + y * s->bs_width) >> 2];
408             if (bs0 || bs1) {
409                 const int qp0 = (get_qPy(s, x, y - 1)     + get_qPy(s, x, y)     + 1) >> 1;
410                 const int qp1 = (get_qPy(s, x + 4, y - 1) + get_qPy(s, x + 4, y) + 1) >> 1;
411
412                 tc_offset   = x >= x0 ? cur_tc_offset : left_tc_offset;
413                 beta_offset = x >= x0 ? cur_beta_offset : left_beta_offset;
414
415                 beta[0] = betatable[av_clip(qp0 + beta_offset, 0, MAX_QP)];
416                 beta[1] = betatable[av_clip(qp1 + beta_offset, 0, MAX_QP)];
417                 tc[0]   = bs0 ? TC_CALC(qp0, bs0) : 0;
418                 tc[1]   = bs1 ? TC_CALC(qp1, bs1) : 0;
419                 src     = &s->frame->data[LUMA][y * s->frame->linesize[LUMA] + (x << s->sps->pixel_shift)];
420                 if (pcmf) {
421                     no_p[0] = get_pcm(s, x, y - 1);
422                     no_p[1] = get_pcm(s, x + 4, y - 1);
423                     no_q[0] = get_pcm(s, x, y);
424                     no_q[1] = get_pcm(s, x + 4, y);
425                     s->hevcdsp.hevc_h_loop_filter_luma_c(src,
426                                                          s->frame->linesize[LUMA],
427                                                          beta, tc, no_p, no_q);
428                 } else
429                     s->hevcdsp.hevc_h_loop_filter_luma(src,
430                                                        s->frame->linesize[LUMA],
431                                                        beta, tc, no_p, no_q);
432             }
433         }
434     }
435
436     // horizontal filtering chroma
437     for (chroma = 1; chroma <= 2; chroma++) {
438         for (y = y0 ? y0 : 16; y < y_end; y += 16) {
439             for (x = x0 - 8; x < x_end; x += 16) {
440                 int bs0, bs1;
441                 // to make sure no memory access over boundary when x = -8
442                 // TODO: simplify with row based deblocking
443                 if (x < 0) {
444                     bs0 = 0;
445                     bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
446                 } else if (x >= x_end - 8) {
447                     bs0 = s->horizontal_bs[(x +     y * s->bs_width) >> 2];
448                     bs1 = 0;
449                 } else {
450                     bs0 = s->horizontal_bs[(x + y     * s->bs_width) >> 2];
451                     bs1 = s->horizontal_bs[(x + 8 + y * s->bs_width) >> 2];
452                 }
453
454                 if ((bs0 == 2) || (bs1 == 2)) {
455                     const int qp0 = bs0 == 2 ? (get_qPy(s, x,     y - 1) + get_qPy(s, x,     y) + 1) >> 1 : 0;
456                     const int qp1 = bs1 == 2 ? (get_qPy(s, x + 8, y - 1) + get_qPy(s, x + 8, y) + 1) >> 1 : 0;
457
458                     tc_offset = x >= x0 ? cur_tc_offset : left_tc_offset;
459                     c_tc[0]   = bs0 == 2 ? chroma_tc(s, qp0, chroma, tc_offset)     : 0;
460                     c_tc[1]   = bs1 == 2 ? chroma_tc(s, qp1, chroma, cur_tc_offset) : 0;
461                     src       = &s->frame->data[chroma][y / 2 * s->frame->linesize[chroma] + ((x / 2) << s->sps->pixel_shift)];
462                     if (pcmf) {
463                         no_p[0] = get_pcm(s, x, y - 1);
464                         no_p[1] = get_pcm(s, x + 8, y - 1);
465                         no_q[0] = get_pcm(s, x, y);
466                         no_q[1] = get_pcm(s, x + 8, y);
467                         s->hevcdsp.hevc_h_loop_filter_chroma_c(src,
468                                                                s->frame->linesize[chroma],
469                                                                c_tc, no_p, no_q);
470                     } else
471                         s->hevcdsp.hevc_h_loop_filter_chroma(src,
472                                                              s->frame->linesize[chroma],
473                                                              c_tc, no_p, no_q);
474                 }
475             }
476         }
477     }
478 }
479
480 static int boundary_strength(HEVCContext *s, MvField *curr,
481                              uint8_t curr_cbf_luma, MvField *neigh,
482                              uint8_t neigh_cbf_luma,
483                              RefPicList *neigh_refPicList,
484                              int tu_border)
485 {
486     int mvs = curr->pred_flag[0] + curr->pred_flag[1];
487
488     if (tu_border) {
489         if (curr->is_intra || neigh->is_intra)
490             return 2;
491         if (curr_cbf_luma || neigh_cbf_luma)
492             return 1;
493     }
494
495     if (mvs == neigh->pred_flag[0] + neigh->pred_flag[1]) {
496         if (mvs == 2) {
497             // same L0 and L1
498             if (s->ref->refPicList[0].list[curr->ref_idx[0]] == neigh_refPicList[0].list[neigh->ref_idx[0]]  &&
499                 s->ref->refPicList[0].list[curr->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]] &&
500                 neigh_refPicList[0].list[neigh->ref_idx[0]] == neigh_refPicList[1].list[neigh->ref_idx[1]]) {
501                 if ((abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
502                      abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4) &&
503                     (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
504                      abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4))
505                     return 1;
506                 else
507                     return 0;
508             } else if (neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
509                        neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
510                 if (abs(neigh->mv[0].x - curr->mv[0].x) >= 4 || abs(neigh->mv[0].y - curr->mv[0].y) >= 4 ||
511                     abs(neigh->mv[1].x - curr->mv[1].x) >= 4 || abs(neigh->mv[1].y - curr->mv[1].y) >= 4)
512                     return 1;
513                 else
514                     return 0;
515             } else if (neigh_refPicList[1].list[neigh->ref_idx[1]] == s->ref->refPicList[0].list[curr->ref_idx[0]] &&
516                        neigh_refPicList[0].list[neigh->ref_idx[0]] == s->ref->refPicList[1].list[curr->ref_idx[1]]) {
517                 if (abs(neigh->mv[1].x - curr->mv[0].x) >= 4 || abs(neigh->mv[1].y - curr->mv[0].y) >= 4 ||
518                     abs(neigh->mv[0].x - curr->mv[1].x) >= 4 || abs(neigh->mv[0].y - curr->mv[1].y) >= 4)
519                     return 1;
520                 else
521                     return 0;
522             } else {
523                 return 1;
524             }
525         } else { // 1 MV
526             Mv A, B;
527             int ref_A, ref_B;
528
529             if (curr->pred_flag[0]) {
530                 A     = curr->mv[0];
531                 ref_A = s->ref->refPicList[0].list[curr->ref_idx[0]];
532             } else {
533                 A     = curr->mv[1];
534                 ref_A = s->ref->refPicList[1].list[curr->ref_idx[1]];
535             }
536
537             if (neigh->pred_flag[0]) {
538                 B     = neigh->mv[0];
539                 ref_B = neigh_refPicList[0].list[neigh->ref_idx[0]];
540             } else {
541                 B     = neigh->mv[1];
542                 ref_B = neigh_refPicList[1].list[neigh->ref_idx[1]];
543             }
544
545             if (ref_A == ref_B) {
546                 if (abs(A.x - B.x) >= 4 || abs(A.y - B.y) >= 4)
547                     return 1;
548                 else
549                     return 0;
550             } else
551                 return 1;
552         }
553     }
554
555     return 1;
556 }
557
558 void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0,
559                                            int log2_trafo_size,
560                                            int slice_or_tiles_up_boundary,
561                                            int slice_or_tiles_left_boundary)
562 {
563     MvField *tab_mvf     = s->ref->tab_mvf;
564     int log2_min_pu_size = s->sps->log2_min_pu_size;
565     int log2_min_tu_size = s->sps->log2_min_tb_size;
566     int min_pu_width     = s->sps->min_pu_width;
567     int min_tu_width     = s->sps->min_tb_width;
568     int is_intra = tab_mvf[(y0 >> log2_min_pu_size) * min_pu_width +
569                            (x0 >> log2_min_pu_size)].is_intra;
570     int i, j, bs;
571
572     if (y0 > 0 && (y0 & 7) == 0) {
573         int yp_pu = (y0 - 1) >> log2_min_pu_size;
574         int yq_pu =  y0      >> log2_min_pu_size;
575         int yp_tu = (y0 - 1) >> log2_min_tu_size;
576         int yq_tu =  y0      >> log2_min_tu_size;
577
578         for (i = 0; i < (1 << log2_trafo_size); i += 4) {
579             int x_pu = (x0 + i) >> log2_min_pu_size;
580             int x_tu = (x0 + i) >> log2_min_tu_size;
581             MvField *top  = &tab_mvf[yp_pu * min_pu_width + x_pu];
582             MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
583             uint8_t top_cbf_luma  = s->cbf_luma[yp_tu * min_tu_width + x_tu];
584             uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
585             RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
586                                                               x0 + i, y0 - 1);
587
588             bs = boundary_strength(s, curr, curr_cbf_luma,
589                                    top, top_cbf_luma, top_refPicList, 1);
590             if (!s->sh.slice_loop_filter_across_slices_enabled_flag &&
591                 (slice_or_tiles_up_boundary & 1) &&
592                 (y0 % (1 << s->sps->log2_ctb_size)) == 0)
593                 bs = 0;
594             else if (!s->pps->loop_filter_across_tiles_enabled_flag &&
595                      (slice_or_tiles_up_boundary & 2) &&
596                      (y0 % (1 << s->sps->log2_ctb_size)) == 0)
597                 bs = 0;
598             if (y0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
599                 bs = 0;
600             if (bs)
601                 s->horizontal_bs[((x0 + i) + y0 * s->bs_width) >> 2] = bs;
602         }
603     }
604
605     // bs for TU internal horizontal PU boundaries
606     if (log2_trafo_size > s->sps->log2_min_pu_size && !is_intra)
607         for (j = 8; j < (1 << log2_trafo_size); j += 8) {
608             int yp_pu = (y0 + j - 1) >> log2_min_pu_size;
609             int yq_pu = (y0 + j)     >> log2_min_pu_size;
610             int yp_tu = (y0 + j - 1) >> log2_min_tu_size;
611             int yq_tu = (y0 + j)     >> log2_min_tu_size;
612
613             for (i = 0; i < (1 << log2_trafo_size); i += 4) {
614                 int x_pu = (x0 + i) >> log2_min_pu_size;
615                 int x_tu = (x0 + i) >> log2_min_tu_size;
616                 MvField *top  = &tab_mvf[yp_pu * min_pu_width + x_pu];
617                 MvField *curr = &tab_mvf[yq_pu * min_pu_width + x_pu];
618                 uint8_t top_cbf_luma  = s->cbf_luma[yp_tu * min_tu_width + x_tu];
619                 uint8_t curr_cbf_luma = s->cbf_luma[yq_tu * min_tu_width + x_tu];
620                 RefPicList *top_refPicList = ff_hevc_get_ref_list(s, s->ref,
621                                                                   x0 + i,
622                                                                   y0 + j - 1);
623
624                 bs = boundary_strength(s, curr, curr_cbf_luma,
625                                        top, top_cbf_luma, top_refPicList, 0);
626                 if (s->sh.disable_deblocking_filter_flag == 1)
627                     bs = 0;
628                 if (bs)
629                     s->horizontal_bs[((x0 + i) + (y0 + j) * s->bs_width) >> 2] = bs;
630             }
631         }
632
633     // bs for vertical TU boundaries
634     if (x0 > 0 && (x0 & 7) == 0) {
635         int xp_pu = (x0 - 1) >> log2_min_pu_size;
636         int xq_pu =  x0      >> log2_min_pu_size;
637         int xp_tu = (x0 - 1) >> log2_min_tu_size;
638         int xq_tu =  x0      >> log2_min_tu_size;
639
640         for (i = 0; i < (1 << log2_trafo_size); i += 4) {
641             int y_pu      = (y0 + i) >> log2_min_pu_size;
642             int y_tu      = (y0 + i) >> log2_min_tu_size;
643             MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
644             MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
645
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             RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
649                                                                x0 - 1, y0 + i);
650
651             bs = boundary_strength(s, curr, curr_cbf_luma,
652                                    left, left_cbf_luma, left_refPicList, 1);
653             if (!s->sh.slice_loop_filter_across_slices_enabled_flag &&
654                 (slice_or_tiles_left_boundary & 1) &&
655                 (x0 % (1 << s->sps->log2_ctb_size)) == 0)
656                 bs = 0;
657             else if (!s->pps->loop_filter_across_tiles_enabled_flag &&
658                      (slice_or_tiles_left_boundary & 2) &&
659                      (x0 % (1 << s->sps->log2_ctb_size)) == 0)
660                 bs = 0;
661             if (x0 == 0 || s->sh.disable_deblocking_filter_flag == 1)
662                 bs = 0;
663             if (bs)
664                 s->vertical_bs[(x0 >> 3) + ((y0 + i) >> 2) * s->bs_width] = bs;
665         }
666     }
667
668     // bs for TU internal vertical PU boundaries
669     if (log2_trafo_size > log2_min_pu_size && !is_intra)
670         for (j = 0; j < (1 << log2_trafo_size); j += 4) {
671             int y_pu = (y0 + j) >> log2_min_pu_size;
672             int y_tu = (y0 + j) >> log2_min_tu_size;
673
674             for (i = 8; i < (1 << log2_trafo_size); i += 8) {
675                 int xp_pu = (x0 + i - 1) >> log2_min_pu_size;
676                 int xq_pu = (x0 + i)     >> log2_min_pu_size;
677                 int xp_tu = (x0 + i - 1) >> log2_min_tu_size;
678                 int xq_tu = (x0 + i)     >> log2_min_tu_size;
679                 MvField *left = &tab_mvf[y_pu * min_pu_width + xp_pu];
680                 MvField *curr = &tab_mvf[y_pu * min_pu_width + xq_pu];
681                 uint8_t left_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xp_tu];
682                 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * min_tu_width + xq_tu];
683                 RefPicList *left_refPicList = ff_hevc_get_ref_list(s, s->ref,
684                                                                    x0 + i - 1,
685                                                                    y0 + j);
686
687                 bs = boundary_strength(s, curr, curr_cbf_luma,
688                                        left, left_cbf_luma, left_refPicList, 0);
689                 if (s->sh.disable_deblocking_filter_flag == 1)
690                     bs = 0;
691                 if (bs)
692                     s->vertical_bs[((x0 + i) >> 3) + ((y0 + j) >> 2) * s->bs_width] = bs;
693             }
694         }
695 }
696
697 #undef LUMA
698 #undef CB
699 #undef CR
700
701 void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
702 {
703     deblocking_filter_CTB(s, x, y);
704     if (s->sps->sao_enabled)
705         sao_filter_CTB(s, x, y);
706 }
707
708 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
709 {
710     if (y_ctb && x_ctb)
711         ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb - ctb_size);
712     if (y_ctb && x_ctb >= s->sps->width - ctb_size) {
713         ff_hevc_hls_filter(s, x_ctb, y_ctb - ctb_size);
714         if (s->threads_type == FF_THREAD_FRAME )
715             ff_thread_report_progress(&s->ref->tf, y_ctb - ctb_size, 0);
716     }
717     if (x_ctb && y_ctb >= s->sps->height - ctb_size)
718         ff_hevc_hls_filter(s, x_ctb - ctb_size, y_ctb);
719 }