Split h264 loop filter off h264.c.
[libav.git] / libavcodec / h264_loopfilter.c
CommitLineData
082cf971
MN
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... loop filter
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file libavcodec/h264_loopfilter.c
24 * H.264 / AVC / MPEG4 part10 loop filter.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28#include "internal.h"
29#include "dsputil.h"
30#include "avcodec.h"
31#include "mpegvideo.h"
32#include "h264.h"
33#include "mathops.h"
34#include "rectangle.h"
35
36#if ARCH_X86
37#include "x86/h264_i386.h"
38#endif
39
40//#undef NDEBUG
41#include <assert.h>
42
43/* Deblocking filter (p153) */
44static const uint8_t alpha_table[52*3] = {
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 4, 4, 5, 6,
51 7, 8, 9, 10, 12, 13, 15, 17, 20, 22,
52 25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
53 80, 90,101,113,127,144,162,182,203,226,
54 255,255,
55 255,255,255,255,255,255,255,255,255,255,255,255,255,
56 255,255,255,255,255,255,255,255,255,255,255,255,255,
57 255,255,255,255,255,255,255,255,255,255,255,255,255,
58 255,255,255,255,255,255,255,255,255,255,255,255,255,
59};
60static const uint8_t beta_table[52*3] = {
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 2, 2, 2, 3,
67 3, 3, 3, 4, 4, 4, 6, 6, 7, 7,
68 8, 8, 9, 9, 10, 10, 11, 11, 12, 12,
69 13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
70 18, 18,
71 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
72 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
73 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
74 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
75};
76static const uint8_t tc0_table[52*3][4] = {
77 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
78 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
79 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
80 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
81 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
82 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
83 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
84 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
85 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
86 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
87 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
88 {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 1 },
89 {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 1, 1 }, {-1, 0, 1, 1 }, {-1, 1, 1, 1 },
90 {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 },
91 {-1, 1, 1, 2 }, {-1, 1, 2, 3 }, {-1, 1, 2, 3 }, {-1, 2, 2, 3 }, {-1, 2, 2, 4 }, {-1, 2, 3, 4 },
92 {-1, 2, 3, 4 }, {-1, 3, 3, 5 }, {-1, 3, 4, 6 }, {-1, 3, 4, 6 }, {-1, 4, 5, 7 }, {-1, 4, 5, 8 },
93 {-1, 4, 6, 9 }, {-1, 5, 7,10 }, {-1, 6, 8,11 }, {-1, 6, 8,13 }, {-1, 7,10,14 }, {-1, 8,11,16 },
94 {-1, 9,12,18 }, {-1,10,13,20 }, {-1,11,15,23 }, {-1,13,17,25 },
95 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
96 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
97 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
98 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
99 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
100 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
101 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
102 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
103 {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
104};
105
106static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
107 const int index_a = qp + h->slice_alpha_c0_offset;
108 const int alpha = (alpha_table+52)[index_a];
109 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
110 if (alpha ==0 || beta == 0) return;
111
112 if( bS[0] < 4 ) {
113 int8_t tc[4];
114 tc[0] = (tc0_table+52)[index_a][bS[0]];
115 tc[1] = (tc0_table+52)[index_a][bS[1]];
116 tc[2] = (tc0_table+52)[index_a][bS[2]];
117 tc[3] = (tc0_table+52)[index_a][bS[3]];
118 h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
119 } else {
120 h->s.dsp.h264_h_loop_filter_luma_intra(pix, stride, alpha, beta);
121 }
122}
123static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
124 const int index_a = qp + h->slice_alpha_c0_offset;
125 const int alpha = (alpha_table+52)[index_a];
126 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
127 if (alpha ==0 || beta == 0) return;
128
129 if( bS[0] < 4 ) {
130 int8_t tc[4];
131 tc[0] = (tc0_table+52)[index_a][bS[0]]+1;
132 tc[1] = (tc0_table+52)[index_a][bS[1]]+1;
133 tc[2] = (tc0_table+52)[index_a][bS[2]]+1;
134 tc[3] = (tc0_table+52)[index_a][bS[3]]+1;
135 h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
136 } else {
137 h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
138 }
139}
140
141static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
142 int i;
143 for( i = 0; i < 16; i++, pix += stride) {
144 int index_a;
145 int alpha;
146 int beta;
147
148 int qp_index;
149 int bS_index = (i >> 1);
150 if (!MB_FIELD) {
151 bS_index &= ~1;
152 bS_index |= (i & 1);
153 }
154
155 if( bS[bS_index] == 0 ) {
156 continue;
157 }
158
159 qp_index = MB_FIELD ? (i >> 3) : (i & 1);
160 index_a = qp[qp_index] + h->slice_alpha_c0_offset;
161 alpha = (alpha_table+52)[index_a];
162 beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
163
164 if( bS[bS_index] < 4 ) {
165 const int tc0 = (tc0_table+52)[index_a][bS[bS_index]];
166 const int p0 = pix[-1];
167 const int p1 = pix[-2];
168 const int p2 = pix[-3];
169 const int q0 = pix[0];
170 const int q1 = pix[1];
171 const int q2 = pix[2];
172
173 if( FFABS( p0 - q0 ) < alpha &&
174 FFABS( p1 - p0 ) < beta &&
175 FFABS( q1 - q0 ) < beta ) {
176 int tc = tc0;
177 int i_delta;
178
179 if( FFABS( p2 - p0 ) < beta ) {
180 pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
181 tc++;
182 }
183 if( FFABS( q2 - q0 ) < beta ) {
184 pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
185 tc++;
186 }
187
188 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
189 pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
190 pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
191 tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
192 }
193 }else{
194 const int p0 = pix[-1];
195 const int p1 = pix[-2];
196 const int p2 = pix[-3];
197
198 const int q0 = pix[0];
199 const int q1 = pix[1];
200 const int q2 = pix[2];
201
202 if( FFABS( p0 - q0 ) < alpha &&
203 FFABS( p1 - p0 ) < beta &&
204 FFABS( q1 - q0 ) < beta ) {
205
206 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
207 if( FFABS( p2 - p0 ) < beta)
208 {
209 const int p3 = pix[-4];
210 /* p0', p1', p2' */
211 pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
212 pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
213 pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
214 } else {
215 /* p0' */
216 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
217 }
218 if( FFABS( q2 - q0 ) < beta)
219 {
220 const int q3 = pix[3];
221 /* q0', q1', q2' */
222 pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
223 pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
224 pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
225 } else {
226 /* q0' */
227 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
228 }
229 }else{
230 /* p0', q0' */
231 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
232 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
233 }
234 tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
235 }
236 }
237 }
238}
239static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
240 int i;
241 for( i = 0; i < 8; i++, pix += stride) {
242 int index_a;
243 int alpha;
244 int beta;
245
246 int qp_index;
247 int bS_index = i;
248
249 if( bS[bS_index] == 0 ) {
250 continue;
251 }
252
253 qp_index = MB_FIELD ? (i >> 2) : (i & 1);
254 index_a = qp[qp_index] + h->slice_alpha_c0_offset;
255 alpha = (alpha_table+52)[index_a];
256 beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
257
258 if( bS[bS_index] < 4 ) {
259 const int tc = (tc0_table+52)[index_a][bS[bS_index]] + 1;
260 const int p0 = pix[-1];
261 const int p1 = pix[-2];
262 const int q0 = pix[0];
263 const int q1 = pix[1];
264
265 if( FFABS( p0 - q0 ) < alpha &&
266 FFABS( p1 - p0 ) < beta &&
267 FFABS( q1 - q0 ) < beta ) {
268 const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
269
270 pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
271 pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
272 tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
273 }
274 }else{
275 const int p0 = pix[-1];
276 const int p1 = pix[-2];
277 const int q0 = pix[0];
278 const int q1 = pix[1];
279
280 if( FFABS( p0 - q0 ) < alpha &&
281 FFABS( p1 - p0 ) < beta &&
282 FFABS( q1 - q0 ) < beta ) {
283
284 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
285 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
286 tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
287 }
288 }
289 }
290}
291
292static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
293 const int index_a = qp + h->slice_alpha_c0_offset;
294 const int alpha = (alpha_table+52)[index_a];
295 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
296 if (alpha ==0 || beta == 0) return;
297
298 if( bS[0] < 4 ) {
299 int8_t tc[4];
300 tc[0] = (tc0_table+52)[index_a][bS[0]];
301 tc[1] = (tc0_table+52)[index_a][bS[1]];
302 tc[2] = (tc0_table+52)[index_a][bS[2]];
303 tc[3] = (tc0_table+52)[index_a][bS[3]];
304 h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
305 } else {
306 h->s.dsp.h264_v_loop_filter_luma_intra(pix, stride, alpha, beta);
307 }
308}
309
310static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
311 const int index_a = qp + h->slice_alpha_c0_offset;
312 const int alpha = (alpha_table+52)[index_a];
313 const int beta = (beta_table+52)[qp + h->slice_beta_offset];
314 if (alpha ==0 || beta == 0) return;
315
316 if( bS[0] < 4 ) {
317 int8_t tc[4];
318 tc[0] = (tc0_table+52)[index_a][bS[0]]+1;
319 tc[1] = (tc0_table+52)[index_a][bS[1]]+1;
320 tc[2] = (tc0_table+52)[index_a][bS[2]]+1;
321 tc[3] = (tc0_table+52)[index_a][bS[3]]+1;
322 h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
323 } else {
324 h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
325 }
326}
327
328void ff_h264_filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
329 MpegEncContext * const s = &h->s;
330 int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD;
331 int mb_xy, mb_type;
332 int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
333
334 mb_xy = h->mb_xy;
335
336 if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
337 !(s->flags2 & CODEC_FLAG2_FAST) || //FIXME filter_mb_fast is broken, thus hasto be, but should not under CODEC_FLAG2_FAST
338 (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] ||
339 h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
340 ff_h264_filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
341 return;
342 }
343 assert(!FRAME_MBAFF);
344
345 mb_type = s->current_picture.mb_type[mb_xy];
346 qp = s->current_picture.qscale_table[mb_xy];
347 qp0 = s->current_picture.qscale_table[mb_xy-1];
348 qp1 = s->current_picture.qscale_table[h->top_mb_xy];
349 qpc = get_chroma_qp( h, 0, qp );
350 qpc0 = get_chroma_qp( h, 0, qp0 );
351 qpc1 = get_chroma_qp( h, 0, qp1 );
352 qp0 = (qp + qp0 + 1) >> 1;
353 qp1 = (qp + qp1 + 1) >> 1;
354 qpc0 = (qpc + qpc0 + 1) >> 1;
355 qpc1 = (qpc + qpc1 + 1) >> 1;
356 qp_thresh = 15 - h->slice_alpha_c0_offset;
357 if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
358 qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
359 return;
360
361 if( IS_INTRA(mb_type) ) {
362 int16_t bS4[4] = {4,4,4,4};
363 int16_t bS3[4] = {3,3,3,3};
364 int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
365 if( IS_8x8DCT(mb_type) ) {
366 filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
367 filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
368 filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
369 filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
370 } else {
371 filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
372 filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
373 filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
374 filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
375 filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
376 filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
377 filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
378 filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
379 }
380 filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
381 filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
382 filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
383 filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
384 filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
385 filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
386 filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
387 filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
388 return;
389 } else {
390 DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
391 uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
392 int edges;
393 if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
394 edges = 4;
395 bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
396 } else {
397 int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
398 (mb_type & MB_TYPE_16x8) ? 1 : 0;
399 int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
400 && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
401 ? 3 : 0;
402 int step = IS_8x8DCT(mb_type) ? 2 : 1;
403 edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
404 s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
405 (h->slice_type_nos == FF_B_TYPE), edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
406 }
407 if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
408 bSv[0][0] = 0x0004000400040004ULL;
409 if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
410 bSv[1][0] = FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL;
411
412#define FILTER(hv,dir,edge)\
413 if(bSv[dir][edge]) {\
414 filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
415 if(!(edge&1)) {\
416 filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
417 filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
418 }\
419 }
420 if( edges == 1 ) {
421 FILTER(v,0,0);
422 FILTER(h,1,0);
423 } else if( IS_8x8DCT(mb_type) ) {
424 FILTER(v,0,0);
425 FILTER(v,0,2);
426 FILTER(h,1,0);
427 FILTER(h,1,2);
428 } else {
429 FILTER(v,0,0);
430 FILTER(v,0,1);
431 FILTER(v,0,2);
432 FILTER(v,0,3);
433 FILTER(h,1,0);
434 FILTER(h,1,1);
435 FILTER(h,1,2);
436 FILTER(h,1,3);
437 }
438#undef FILTER
439 }
440}
441
442
443static av_always_inline void filter_mb_dir(H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize, int mb_xy, int mb_type, int mvy_limit, int first_vertical_edge_done, int dir) {
444 MpegEncContext * const s = &h->s;
445 int edge;
446 const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
447 const int mbm_type = s->current_picture.mb_type[mbm_xy];
448 int (*ref2frm) [64] = h->ref2frm[ h->slice_num &(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
449 int (*ref2frmm)[64] = h->ref2frm[ h->slice_table[mbm_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
450 int start = h->slice_table[mbm_xy] == 0xFFFF ? 1 : 0;
451
452 const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
453 == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
454 // how often to recheck mv-based bS when iterating between edges
455 const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
456 (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
457 // how often to recheck mv-based bS when iterating along each edge
458 const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
459
460 if (first_vertical_edge_done) {
461 start = 1;
462 }
463
464 if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
465 start = 1;
466
467 if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
468 && !IS_INTERLACED(mb_type)
469 && IS_INTERLACED(mbm_type)
470 ) {
471 // This is a special case in the norm where the filtering must
472 // be done twice (one each of the field) even if we are in a
473 // frame macroblock.
474 //
475 static const int nnz_idx[4] = {4,5,6,3};
476 unsigned int tmp_linesize = 2 * linesize;
477 unsigned int tmp_uvlinesize = 2 * uvlinesize;
478 int mbn_xy = mb_xy - 2 * s->mb_stride;
479 int qp;
480 int i, j;
481 int16_t bS[4];
482
483 for(j=0; j<2; j++, mbn_xy += s->mb_stride){
484 if( IS_INTRA(mb_type) ||
485 IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
486 bS[0] = bS[1] = bS[2] = bS[3] = 3;
487 } else {
488 const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
489 for( i = 0; i < 4; i++ ) {
490 if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
491 mbn_nnz[nnz_idx[i]] != 0 )
492 bS[i] = 2;
493 else
494 bS[i] = 1;
495 }
496 }
497 // Do not use s->qscale as luma quantizer because it has not the same
498 // value in IPCM macroblocks.
499 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
500 tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
501 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
502 filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
503 filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
504 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
505 filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
506 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
507 }
508
509 start = 1;
510 }
511
512 /* Calculate bS */
513 for( edge = start; edge < edges; edge++ ) {
514 /* mbn_xy: neighbor macroblock */
515 const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
516 const int mbn_type = s->current_picture.mb_type[mbn_xy];
517 int (*ref2frmn)[64] = edge > 0 ? ref2frm : ref2frmm;
518 int16_t bS[4];
519 int qp;
520
521 if( (edge&1) && IS_8x8DCT(mb_type) )
522 continue;
523
524 if( IS_INTRA(mb_type) ||
525 IS_INTRA(mbn_type) ) {
526 int value;
527 if (edge == 0) {
528 if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
529 || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
530 ) {
531 value = 4;
532 } else {
533 value = 3;
534 }
535 } else {
536 value = 3;
537 }
538 bS[0] = bS[1] = bS[2] = bS[3] = value;
539 } else {
540 int i, l;
541 int mv_done;
542
543 if( edge & mask_edge ) {
544 bS[0] = bS[1] = bS[2] = bS[3] = 0;
545 mv_done = 1;
546 }
547 else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
548 bS[0] = bS[1] = bS[2] = bS[3] = 1;
549 mv_done = 1;
550 }
551 else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
552 int b_idx= 8 + 4 + edge * (dir ? 8:1);
553 int bn_idx= b_idx - (dir ? 8:1);
554 int v = 0;
555
556 for( l = 0; !v && l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
557 v |= ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[l][h->ref_cache[l][bn_idx]] ||
558 FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
559 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
560 }
561
562 if(h->slice_type_nos == FF_B_TYPE && v){
563 v=0;
564 for( l = 0; !v && l < 2; l++ ) {
565 int ln= 1-l;
566 v |= ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[ln][h->ref_cache[ln][bn_idx]] ||
567 FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] ) >= 4 ||
568 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit;
569 }
570 }
571
572 bS[0] = bS[1] = bS[2] = bS[3] = v;
573 mv_done = 1;
574 }
575 else
576 mv_done = 0;
577
578 for( i = 0; i < 4; i++ ) {
579 int x = dir == 0 ? edge : i;
580 int y = dir == 0 ? i : edge;
581 int b_idx= 8 + 4 + x + 8*y;
582 int bn_idx= b_idx - (dir ? 8:1);
583
584 if( h->non_zero_count_cache[b_idx] |
585 h->non_zero_count_cache[bn_idx] ) {
586 bS[i] = 2;
587 }
588 else if(!mv_done)
589 {
590 bS[i] = 0;
591 for( l = 0; l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
592 if( ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[l][h->ref_cache[l][bn_idx]] ||
593 FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
594 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
595 bS[i] = 1;
596 break;
597 }
598 }
599
600 if(h->slice_type_nos == FF_B_TYPE && bS[i]){
601 bS[i] = 0;
602 for( l = 0; l < 2; l++ ) {
603 int ln= 1-l;
604 if( ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[ln][h->ref_cache[ln][bn_idx]] ||
605 FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] ) >= 4 ||
606 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit ) {
607 bS[i] = 1;
608 break;
609 }
610 }
611 }
612 }
613 }
614
615 if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
616 continue;
617 }
618
619 /* Filter edge */
620 // Do not use s->qscale as luma quantizer because it has not the same
621 // value in IPCM macroblocks.
622 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
623 //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
624 tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
625 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
626 if( dir == 0 ) {
627 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
628 if( (edge&1) == 0 ) {
629 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
630 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
631 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
632 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
633 }
634 } else {
635 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
636 if( (edge&1) == 0 ) {
637 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
638 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
639 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
640 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
641 }
642 }
643 }
644}
645
646void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
647 MpegEncContext * const s = &h->s;
648 const int mb_xy= mb_x + mb_y*s->mb_stride;
649 const int mb_type = s->current_picture.mb_type[mb_xy];
650 const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
651 int first_vertical_edge_done = 0;
652 av_unused int dir;
653
654 //for sufficiently low qp, filtering wouldn't do anything
655 //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
656 if(!FRAME_MBAFF){
657 int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
658 int qp = s->current_picture.qscale_table[mb_xy];
659 if(qp <= qp_thresh
660 && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
661 && (h->top_mb_xy < 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
662 return;
663 }
664 }
665
666 // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
667 if(!h->pps.cabac && h->pps.transform_8x8_mode){
668 int top_type, left_type[2];
669 top_type = s->current_picture.mb_type[h->top_mb_xy] ;
670 left_type[0] = s->current_picture.mb_type[h->left_mb_xy[0]];
671 left_type[1] = s->current_picture.mb_type[h->left_mb_xy[1]];
672
673 if(IS_8x8DCT(top_type)){
674 h->non_zero_count_cache[4+8*0]=
675 h->non_zero_count_cache[5+8*0]= h->cbp_table[h->top_mb_xy] & 4;
676 h->non_zero_count_cache[6+8*0]=
677 h->non_zero_count_cache[7+8*0]= h->cbp_table[h->top_mb_xy] & 8;
678 }
679 if(IS_8x8DCT(left_type[0])){
680 h->non_zero_count_cache[3+8*1]=
681 h->non_zero_count_cache[3+8*2]= h->cbp_table[h->left_mb_xy[0]]&2; //FIXME check MBAFF
682 }
683 if(IS_8x8DCT(left_type[1])){
684 h->non_zero_count_cache[3+8*3]=
685 h->non_zero_count_cache[3+8*4]= h->cbp_table[h->left_mb_xy[1]]&8; //FIXME check MBAFF
686 }
687
688 if(IS_8x8DCT(mb_type)){
689 h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
690 h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
691
692 h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
693 h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
694
695 h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
696 h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
697
698 h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
699 h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
700 }
701 }
702
703 if (FRAME_MBAFF
704 // left mb is in picture
705 && h->slice_table[mb_xy-1] != 0xFFFF
706 // and current and left pair do not have the same interlaced type
707 && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
708 // and left mb is in the same slice if deblocking_filter == 2
709 && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
710 /* First vertical edge is different in MBAFF frames
711 * There are 8 different bS to compute and 2 different Qp
712 */
713 const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
714 const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
715 int16_t bS[8];
716 int qp[2];
717 int bqp[2];
718 int rqp[2];
719 int mb_qp, mbn0_qp, mbn1_qp;
720 int i;
721 first_vertical_edge_done = 1;
722
723 if( IS_INTRA(mb_type) )
724 bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
725 else {
726 for( i = 0; i < 8; i++ ) {
727 int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
728
729 if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
730 bS[i] = 4;
731 else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
732 ((!h->pps.cabac && IS_8x8DCT(s->current_picture.mb_type[mbn_xy])) ?
733 (h->cbp_table[mbn_xy] & ((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2))
734 :
735 h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2]))
736 bS[i] = 2;
737 else
738 bS[i] = 1;
739 }
740 }
741
742 mb_qp = s->current_picture.qscale_table[mb_xy];
743 mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
744 mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
745 qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
746 bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
747 get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
748 rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
749 get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
750 qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
751 bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
752 get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
753 rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
754 get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
755
756 /* Filter edge */
757 tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
758 { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
759 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
760 filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
761 filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
762 }
763
764#if CONFIG_SMALL
765 for( dir = 0; dir < 2; dir++ )
766 filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, dir);
767#else
768 filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, 0);
769 filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, 1);
770#endif
771}