2 * H.26L/H.264/AVC/JVT/14496-10/... loop filter
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file libavcodec/h264_loopfilter.c
24 * H.264 / AVC / MPEG4 part10 loop filter.
25 * @author Michael Niedermayer <michaelni@gmx.at>
31 #include "mpegvideo.h"
34 #include "rectangle.h"
37 #include "x86/h264_i386.h"
43 /* Deblocking filter (p153) */
44 static 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,
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,
60 static 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,
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,
76 static 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 },
106 static void av_noinline 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;
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);
120 h->s.dsp.h264_h_loop_filter_luma_intra(pix, stride, alpha, beta);
123 static void av_noinline 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;
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);
137 h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
141 static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
143 for( i = 0; i < 16; i++, pix += stride) {
149 int bS_index = (i >> 1);
155 if( bS[bS_index] == 0 ) {
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];
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];
173 if( FFABS( p0 - q0 ) < alpha &&
174 FFABS( p1 - p0 ) < beta &&
175 FFABS( q1 - q0 ) < beta ) {
179 if( FFABS( p2 - p0 ) < beta ) {
180 pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
183 if( FFABS( q2 - q0 ) < beta ) {
184 pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
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);
194 const int p0 = pix[-1];
195 const int p1 = pix[-2];
196 const int p2 = pix[-3];
198 const int q0 = pix[0];
199 const int q1 = pix[1];
200 const int q2 = pix[2];
202 if( FFABS( p0 - q0 ) < alpha &&
203 FFABS( p1 - p0 ) < beta &&
204 FFABS( q1 - q0 ) < beta ) {
206 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
207 if( FFABS( p2 - p0 ) < beta)
209 const int p3 = pix[-4];
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;
216 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
218 if( FFABS( q2 - q0 ) < beta)
220 const int q3 = pix[3];
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;
227 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
231 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
232 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
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]);
239 static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
241 for( i = 0; i < 8; i++, pix += stride) {
249 if( bS[bS_index] == 0 ) {
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];
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];
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 );
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);
275 const int p0 = pix[-1];
276 const int p1 = pix[-2];
277 const int q0 = pix[0];
278 const int q1 = pix[1];
280 if( FFABS( p0 - q0 ) < alpha &&
281 FFABS( p1 - p0 ) < beta &&
282 FFABS( q1 - q0 ) < beta ) {
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]);
292 static void av_noinline 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;
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);
306 h->s.dsp.h264_v_loop_filter_luma_intra(pix, stride, alpha, beta);
310 static void av_noinline 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;
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);
324 h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
328 void 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;
332 int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
336 if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
337 !(h->slice_type_nos == FF_I_TYPE ||
338 h->slice_type_nos == FF_P_TYPE ||
339 (s->flags2 & CODEC_FLAG2_FAST)) ||
340 (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] || //use slice_num
341 h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
342 ff_h264_filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
345 assert(!FRAME_MBAFF);
347 mb_type = s->current_picture.mb_type[mb_xy];
348 qp = s->current_picture.qscale_table[mb_xy];
349 qp0 = s->current_picture.qscale_table[mb_xy-1];
350 qp1 = s->current_picture.qscale_table[h->top_mb_xy];
351 qpc = get_chroma_qp( h, 0, qp );
352 qpc0 = get_chroma_qp( h, 0, qp0 );
353 qpc1 = get_chroma_qp( h, 0, qp1 );
354 qp0 = (qp + qp0 + 1) >> 1;
355 qp1 = (qp + qp1 + 1) >> 1;
356 qpc0 = (qpc + qpc0 + 1) >> 1;
357 qpc1 = (qpc + qpc1 + 1) >> 1;
358 qp_thresh = 15 - h->slice_alpha_c0_offset;
359 if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
360 qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
363 if( IS_INTRA(mb_type) ) {
364 int16_t bS4[4] = {4,4,4,4};
365 int16_t bS3[4] = {3,3,3,3};
366 int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
367 if( IS_8x8DCT(mb_type) ) {
368 filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
369 filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
370 filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
371 filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
373 filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
374 filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
375 filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
376 filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
377 filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
378 filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
379 filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
380 filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
382 filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
383 filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
384 filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
385 filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
386 filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
387 filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
388 filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
389 filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
392 DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
393 uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
395 if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
397 bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
399 int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
400 (mb_type & MB_TYPE_16x8) ? 1 : 0;
401 int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
402 && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
404 int step = IS_8x8DCT(mb_type) ? 2 : 1;
405 edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
406 s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
407 (h->slice_type_nos == FF_B_TYPE), edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
409 if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
410 bSv[0][0] = 0x0004000400040004ULL;
411 if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
412 bSv[1][0] = FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL;
414 #define FILTER(hv,dir,edge)\
415 if(bSv[dir][edge]) {\
416 filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
418 filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
419 filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
425 } else if( IS_8x8DCT(mb_type) ) {
445 static 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) {
446 MpegEncContext * const s = &h->s;
448 const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
449 const int mbm_type = s->current_picture.mb_type[mbm_xy];
450 int start = h->slice_table[mbm_xy] == 0xFFFF ? 1 : 0;
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));
460 if (first_vertical_edge_done) {
464 if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
467 if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
468 && !IS_INTERLACED(mb_type)
469 && IS_INTERLACED(mbm_type)
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
475 unsigned int tmp_linesize = 2 * linesize;
476 unsigned int tmp_uvlinesize = 2 * uvlinesize;
477 int mbn_xy = mb_xy - 2 * s->mb_stride;
482 for(j=0; j<2; j++, mbn_xy += s->mb_stride){
483 if( IS_INTRA(mb_type) ||
484 IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
485 bS[0] = bS[1] = bS[2] = bS[3] = 3;
487 const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
488 for( i = 0; i < 4; i++ ) {
489 if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
490 mbn_nnz[i+4+3*8] != 0 )
496 // Do not use s->qscale as luma quantizer because it has not the same
497 // value in IPCM macroblocks.
498 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
499 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);
500 { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
501 filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
502 filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
503 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
504 filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
505 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
512 for( edge = start; edge < edges; edge++ ) {
513 /* mbn_xy: neighbor macroblock */
514 const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
515 const int mbn_type = s->current_picture.mb_type[mbn_xy];
519 if( (edge&1) && IS_8x8DCT(mb_type) )
522 if( IS_INTRA(mb_type) ||
523 IS_INTRA(mbn_type) ) {
526 if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
527 || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
536 bS[0] = bS[1] = bS[2] = bS[3] = value;
541 if( edge & mask_edge ) {
542 bS[0] = bS[1] = bS[2] = bS[3] = 0;
545 else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
546 bS[0] = bS[1] = bS[2] = bS[3] = 1;
549 else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
550 int b_idx= 8 + 4 + edge * (dir ? 8:1);
551 int bn_idx= b_idx - (dir ? 8:1);
554 for( l = 0; !v && l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
555 v |= h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] |
556 h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] + 3 >= 7U |
557 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
560 if(h->slice_type_nos == FF_B_TYPE && v){
562 for( l = 0; !v && l < 2; l++ ) {
564 v |= h->ref_cache[l][b_idx] != h->ref_cache[ln][bn_idx] |
565 h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] + 3 >= 7U |
566 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit;
570 bS[0] = bS[1] = bS[2] = bS[3] = v;
576 for( i = 0; i < 4; i++ ) {
577 int x = dir == 0 ? edge : i;
578 int y = dir == 0 ? i : edge;
579 int b_idx= 8 + 4 + x + 8*y;
580 int bn_idx= b_idx - (dir ? 8:1);
582 if( h->non_zero_count_cache[b_idx] |
583 h->non_zero_count_cache[bn_idx] ) {
589 for( l = 0; l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
590 if( h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] |
591 h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] + 3 >= 7U |
592 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
598 if(h->slice_type_nos == FF_B_TYPE && bS[i]){
600 for( l = 0; l < 2; l++ ) {
602 if( h->ref_cache[l][b_idx] != h->ref_cache[ln][bn_idx] |
603 h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] + 3 >= 7U |
604 FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit ) {
613 if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
618 // Do not use s->qscale as luma quantizer because it has not the same
619 // value in IPCM macroblocks.
620 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
621 //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[0], s->current_picture.qscale_table[mbn_xy]);
622 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);
623 //{ int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
625 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
626 if( (edge&1) == 0 ) {
627 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
628 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
629 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
630 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
633 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
634 if( (edge&1) == 0 ) {
635 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
636 ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
637 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
638 ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
644 void 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) {
645 MpegEncContext * const s = &h->s;
646 const int mb_xy= mb_x + mb_y*s->mb_stride;
647 const int mb_type = s->current_picture.mb_type[mb_xy];
648 const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
649 int first_vertical_edge_done = 0;
654 // left mb is in picture
655 && h->slice_table[mb_xy-1] != 0xFFFF
656 // and current and left pair do not have the same interlaced type
657 && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
658 // and left mb is in the same slice if deblocking_filter == 2
659 && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
660 /* First vertical edge is different in MBAFF frames
661 * There are 8 different bS to compute and 2 different Qp
663 const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
664 const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
669 int mb_qp, mbn0_qp, mbn1_qp;
671 first_vertical_edge_done = 1;
673 if( IS_INTRA(mb_type) )
674 bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
676 for( i = 0; i < 8; i++ ) {
677 int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
679 if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
681 else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
682 ((!h->pps.cabac && IS_8x8DCT(s->current_picture.mb_type[mbn_xy])) ?
683 (h->cbp_table[mbn_xy] & ((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2))
685 h->non_zero_count[mbn_xy][7+(MB_FIELD ? (i&3) : (i>>2)+(mb_y&1)*2)*8]))
692 mb_qp = s->current_picture.qscale_table[mb_xy];
693 mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
694 mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
695 qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
696 bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
697 get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
698 rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
699 get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
700 qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
701 bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
702 get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
703 rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
704 get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
707 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);
708 { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
709 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
710 filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
711 filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
715 for( dir = 0; dir < 2; dir++ )
716 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);
718 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);
719 filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, 1);