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