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,H) \
33 static void FUNCC(weight_h264_pixels ## W ## x ## H)(uint8_t *_block, int stride, int log2_denom, int weight, int offset){ \
35 pixel *block = (pixel*)_block; \
36 stride /= sizeof(pixel); \
37 offset <<= (log2_denom + (BIT_DEPTH-8)); \
38 if(log2_denom) offset += 1<<(log2_denom-1); \
39 for(y=0; y<H; y++, block += stride){ \
61 static void FUNCC(biweight_h264_pixels ## W ## x ## H)(uint8_t *_dst, uint8_t *_src, int stride, int log2_denom, int weightd, int weights, int offset){ \
63 pixel *dst = (pixel*)_dst; \
64 pixel *src = (pixel*)_src; \
65 stride /= sizeof(pixel); \
66 offset <<= (BIT_DEPTH-8); \
67 offset = ((offset + 1) | 1) << log2_denom; \
68 for(y=0; y<H; y++, dst += stride, src += stride){ \
106 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)
108 pixel *pix = (pixel*)_pix;
110 xstride /= sizeof(pixel);
111 ystride /= sizeof(pixel);
112 alpha <<= BIT_DEPTH - 8;
113 beta <<= BIT_DEPTH - 8;
114 for( i = 0; i < 4; i++ ) {
115 const int tc_orig = tc0[i] << (BIT_DEPTH - 8);
117 pix += inner_iters*ystride;
120 for( d = 0; d < inner_iters; d++ ) {
121 const int p0 = pix[-1*xstride];
122 const int p1 = pix[-2*xstride];
123 const int p2 = pix[-3*xstride];
124 const int q0 = pix[0];
125 const int q1 = pix[1*xstride];
126 const int q2 = pix[2*xstride];
128 if( FFABS( p0 - q0 ) < alpha &&
129 FFABS( p1 - p0 ) < beta &&
130 FFABS( q1 - q0 ) < beta ) {
135 if( FFABS( p2 - p0 ) < beta ) {
137 pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc_orig, tc_orig );
140 if( FFABS( q2 - q0 ) < beta ) {
142 pix[ xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc_orig, tc_orig );
146 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
147 pix[-xstride] = av_clip_pixel( p0 + i_delta ); /* p0' */
148 pix[0] = av_clip_pixel( q0 - i_delta ); /* q0' */
154 static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
156 FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), 4, alpha, beta, tc0);
158 static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
160 FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
162 static void FUNCC(h264_h_loop_filter_luma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
164 FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
167 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)
169 pixel *pix = (pixel*)_pix;
171 xstride /= sizeof(pixel);
172 ystride /= sizeof(pixel);
173 alpha <<= BIT_DEPTH - 8;
174 beta <<= BIT_DEPTH - 8;
175 for( d = 0; d < 4 * inner_iters; d++ ) {
176 const int p2 = pix[-3*xstride];
177 const int p1 = pix[-2*xstride];
178 const int p0 = pix[-1*xstride];
180 const int q0 = pix[ 0*xstride];
181 const int q1 = pix[ 1*xstride];
182 const int q2 = pix[ 2*xstride];
184 if( FFABS( p0 - q0 ) < alpha &&
185 FFABS( p1 - p0 ) < beta &&
186 FFABS( q1 - q0 ) < beta ) {
188 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
189 if( FFABS( p2 - p0 ) < beta)
191 const int p3 = pix[-4*xstride];
193 pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
194 pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
195 pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
198 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
200 if( FFABS( q2 - q0 ) < beta)
202 const int q3 = pix[3*xstride];
204 pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
205 pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
206 pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
209 pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
213 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
214 pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
220 static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
222 FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), 4, alpha, beta);
224 static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
226 FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
228 static void FUNCC(h264_h_loop_filter_luma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
230 FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
233 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)
235 pixel *pix = (pixel*)_pix;
237 xstride /= sizeof(pixel);
238 ystride /= sizeof(pixel);
239 alpha <<= BIT_DEPTH - 8;
240 beta <<= BIT_DEPTH - 8;
241 for( i = 0; i < 4; i++ ) {
242 const int tc = ((tc0[i] - 1) << (BIT_DEPTH - 8)) + 1;
244 pix += inner_iters*ystride;
247 for( d = 0; d < inner_iters; d++ ) {
248 const int p0 = pix[-1*xstride];
249 const int p1 = pix[-2*xstride];
250 const int q0 = pix[0];
251 const int q1 = pix[1*xstride];
253 if( FFABS( p0 - q0 ) < alpha &&
254 FFABS( p1 - p0 ) < beta &&
255 FFABS( q1 - q0 ) < beta ) {
257 int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
259 pix[-xstride] = av_clip_pixel( p0 + delta ); /* p0' */
260 pix[0] = av_clip_pixel( q0 - delta ); /* q0' */
266 static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
268 FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), 2, alpha, beta, tc0);
270 static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
272 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
274 static void FUNCC(h264_h_loop_filter_chroma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
276 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 1, alpha, beta, tc0);
279 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)
281 pixel *pix = (pixel*)_pix;
283 xstride /= sizeof(pixel);
284 ystride /= sizeof(pixel);
285 alpha <<= BIT_DEPTH - 8;
286 beta <<= BIT_DEPTH - 8;
287 for( d = 0; d < 4 * inner_iters; d++ ) {
288 const int p0 = pix[-1*xstride];
289 const int p1 = pix[-2*xstride];
290 const int q0 = pix[0];
291 const int q1 = pix[1*xstride];
293 if( FFABS( p0 - q0 ) < alpha &&
294 FFABS( p1 - p0 ) < beta &&
295 FFABS( q1 - q0 ) < beta ) {
297 pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
298 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
303 static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
305 FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), 2, alpha, beta);
307 static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
309 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
311 static void FUNCC(h264_h_loop_filter_chroma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
313 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 1, alpha, beta);