2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003-2010 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 DSP functions.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "bit_depth_template.c"
30 #define op_scale1(x) block[x] = av_clip_pixel( (block[x]*weight + offset) >> log2_denom )
31 #define op_scale2(x) dst[x] = av_clip_pixel( (src[x]*weights + dst[x]*weightd + offset) >> (log2_denom+1))
32 #define H264_WEIGHT(W) \
33 static void FUNCC(weight_h264_pixels ## W)(uint8_t *_block, int stride, int height, \
34 int log2_denom, int weight, int offset) \
37 pixel *block = (pixel*)_block; \
38 stride /= sizeof(pixel); \
39 offset <<= (log2_denom + (BIT_DEPTH-8)); \
40 if(log2_denom) offset += 1<<(log2_denom-1); \
41 for (y = 0; y < height; y++, block += stride) { \
63 static void FUNCC(biweight_h264_pixels ## W)(uint8_t *_dst, uint8_t *_src, int stride, int height, \
64 int log2_denom, int weightd, int weights, int offset) \
67 pixel *dst = (pixel*)_dst; \
68 pixel *src = (pixel*)_src; \
69 stride /= sizeof(pixel); \
70 offset <<= (BIT_DEPTH-8); \
71 offset = ((offset + 1) | 1) << log2_denom; \
72 for (y = 0; y < height; y++, dst += stride, src += stride) { \
104 static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
106 pixel *pix = (pixel*)_pix;
108 xstride /= sizeof(pixel);
109 ystride /= sizeof(pixel);
110 alpha <<= BIT_DEPTH - 8;
111 beta <<= BIT_DEPTH - 8;
112 for( i = 0; i < 4; i++ ) {
113 const int tc_orig = tc0[i] << (BIT_DEPTH - 8);
115 pix += inner_iters*ystride;
118 for( d = 0; d < inner_iters; d++ ) {
119 const int p0 = pix[-1*xstride];
120 const int p1 = pix[-2*xstride];
121 const int p2 = pix[-3*xstride];
122 const int q0 = pix[0];
123 const int q1 = pix[1*xstride];
124 const int q2 = pix[2*xstride];
126 if( FFABS( p0 - q0 ) < alpha &&
127 FFABS( p1 - p0 ) < beta &&
128 FFABS( q1 - q0 ) < beta ) {
133 if( FFABS( p2 - p0 ) < beta ) {
135 pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc_orig, tc_orig );
138 if( FFABS( q2 - q0 ) < beta ) {
140 pix[ xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc_orig, tc_orig );
144 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
145 pix[-xstride] = av_clip_pixel( p0 + i_delta ); /* p0' */
146 pix[0] = av_clip_pixel( q0 - i_delta ); /* q0' */
152 static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
154 FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), 4, alpha, beta, tc0);
156 static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
158 FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
160 static void FUNCC(h264_h_loop_filter_luma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
162 FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
165 static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma_intra)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
167 pixel *pix = (pixel*)_pix;
169 xstride /= sizeof(pixel);
170 ystride /= sizeof(pixel);
171 alpha <<= BIT_DEPTH - 8;
172 beta <<= BIT_DEPTH - 8;
173 for( d = 0; d < 4 * inner_iters; d++ ) {
174 const int p2 = pix[-3*xstride];
175 const int p1 = pix[-2*xstride];
176 const int p0 = pix[-1*xstride];
178 const int q0 = pix[ 0*xstride];
179 const int q1 = pix[ 1*xstride];
180 const int q2 = pix[ 2*xstride];
182 if( FFABS( p0 - q0 ) < alpha &&
183 FFABS( p1 - p0 ) < beta &&
184 FFABS( q1 - q0 ) < beta ) {
186 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
187 if( FFABS( p2 - p0 ) < beta)
189 const int p3 = pix[-4*xstride];
191 pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
192 pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
193 pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
196 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
198 if( FFABS( q2 - q0 ) < beta)
200 const int q3 = pix[3*xstride];
202 pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
203 pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
204 pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
207 pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
211 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
212 pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
218 static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
220 FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), 4, alpha, beta);
222 static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
224 FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
226 static void FUNCC(h264_h_loop_filter_luma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
228 FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
231 static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
233 pixel *pix = (pixel*)_pix;
235 xstride /= sizeof(pixel);
236 ystride /= sizeof(pixel);
237 alpha <<= BIT_DEPTH - 8;
238 beta <<= BIT_DEPTH - 8;
239 for( i = 0; i < 4; i++ ) {
240 const int tc = ((tc0[i] - 1) << (BIT_DEPTH - 8)) + 1;
242 pix += inner_iters*ystride;
245 for( d = 0; d < inner_iters; d++ ) {
246 const int p0 = pix[-1*xstride];
247 const int p1 = pix[-2*xstride];
248 const int q0 = pix[0];
249 const int q1 = pix[1*xstride];
251 if( FFABS( p0 - q0 ) < alpha &&
252 FFABS( p1 - p0 ) < beta &&
253 FFABS( q1 - q0 ) < beta ) {
255 int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
257 pix[-xstride] = av_clip_pixel( p0 + delta ); /* p0' */
258 pix[0] = av_clip_pixel( q0 - delta ); /* q0' */
264 static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
266 FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), 2, alpha, beta, tc0);
268 static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
270 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
272 static void FUNCC(h264_h_loop_filter_chroma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
274 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 1, alpha, beta, tc0);
276 static void FUNCC(h264_h_loop_filter_chroma422)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
278 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
280 static void FUNCC(h264_h_loop_filter_chroma422_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
282 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
285 static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma_intra)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
287 pixel *pix = (pixel*)_pix;
289 xstride /= sizeof(pixel);
290 ystride /= sizeof(pixel);
291 alpha <<= BIT_DEPTH - 8;
292 beta <<= BIT_DEPTH - 8;
293 for( d = 0; d < 4 * inner_iters; d++ ) {
294 const int p0 = pix[-1*xstride];
295 const int p1 = pix[-2*xstride];
296 const int q0 = pix[0];
297 const int q1 = pix[1*xstride];
299 if( FFABS( p0 - q0 ) < alpha &&
300 FFABS( p1 - p0 ) < beta &&
301 FFABS( q1 - q0 ) < beta ) {
303 pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
304 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
309 static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
311 FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), 2, alpha, beta);
313 static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
315 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
317 static void FUNCC(h264_h_loop_filter_chroma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
319 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 1, alpha, beta);
321 static void FUNCC(h264_h_loop_filter_chroma422_intra)(uint8_t *pix, int stride, int alpha, int beta)
323 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
325 static void FUNCC(h264_h_loop_filter_chroma422_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
327 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);