2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003-2011 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
24 * H.264 / AVC / MPEG4 part10 DSP functions.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "high_bit_depth.h"
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 *p_block, int stride, int log2_denom, int weight, int offset){ \
35 pixel *block = (pixel*)p_block; \
36 stride >>= sizeof(pixel)-1; \
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 *p_dst, uint8_t *p_src, int stride, int log2_denom, int weightd, int weights, int offset){ \
63 pixel *dst = (pixel*)p_dst; \
64 pixel *src = (pixel*)p_src; \
65 stride >>= sizeof(pixel)-1; \
66 offset = ((offset + 1) | 1) << log2_denom; \
67 for(y=0; y<H; y++, dst += stride, src += stride){ \
105 static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma)(uint8_t *p_pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
107 pixel *pix = (pixel*)p_pix;
109 xstride >>= sizeof(pixel)-1;
110 ystride >>= sizeof(pixel)-1;
111 alpha <<= BIT_DEPTH - 8;
112 beta <<= BIT_DEPTH - 8;
113 for( i = 0; i < 4; i++ ) {
114 const int tc_orig = tc0[i] << (BIT_DEPTH - 8);
116 pix += inner_iters*ystride;
119 for( d = 0; d < inner_iters; d++ ) {
120 const int p0 = pix[-1*xstride];
121 const int p1 = pix[-2*xstride];
122 const int p2 = pix[-3*xstride];
123 const int q0 = pix[0];
124 const int q1 = pix[1*xstride];
125 const int q2 = pix[2*xstride];
127 if( FFABS( p0 - q0 ) < alpha &&
128 FFABS( p1 - p0 ) < beta &&
129 FFABS( q1 - q0 ) < beta ) {
134 if( FFABS( p2 - p0 ) < beta ) {
136 pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc_orig, tc_orig );
139 if( FFABS( q2 - q0 ) < beta ) {
141 pix[ xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc_orig, tc_orig );
145 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
146 pix[-xstride] = av_clip_pixel( p0 + i_delta ); /* p0' */
147 pix[0] = av_clip_pixel( q0 - i_delta ); /* q0' */
153 static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
155 FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), 4, alpha, beta, tc0);
157 static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
159 FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
161 static void FUNCC(h264_h_loop_filter_luma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
163 FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
166 static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma_intra)(uint8_t *p_pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
168 pixel *pix = (pixel*)p_pix;
170 xstride >>= sizeof(pixel)-1;
171 ystride >>= sizeof(pixel)-1;
172 alpha <<= BIT_DEPTH - 8;
173 beta <<= BIT_DEPTH - 8;
174 for( d = 0; d < 4 * inner_iters; d++ ) {
175 const int p2 = pix[-3*xstride];
176 const int p1 = pix[-2*xstride];
177 const int p0 = pix[-1*xstride];
179 const int q0 = pix[ 0*xstride];
180 const int q1 = pix[ 1*xstride];
181 const int q2 = pix[ 2*xstride];
183 if( FFABS( p0 - q0 ) < alpha &&
184 FFABS( p1 - p0 ) < beta &&
185 FFABS( q1 - q0 ) < beta ) {
187 if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
188 if( FFABS( p2 - p0 ) < beta)
190 const int p3 = pix[-4*xstride];
192 pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
193 pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
194 pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
197 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
199 if( FFABS( q2 - q0 ) < beta)
201 const int q3 = pix[3*xstride];
203 pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
204 pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
205 pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
208 pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
212 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
213 pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
219 static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
221 FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), 4, alpha, beta);
223 static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
225 FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
227 static void FUNCC(h264_h_loop_filter_luma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
229 FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
232 static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma)(uint8_t *p_pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
234 pixel *pix = (pixel*)p_pix;
236 alpha <<= BIT_DEPTH - 8;
237 beta <<= BIT_DEPTH - 8;
238 xstride >>= sizeof(pixel)-1;
239 ystride >>= sizeof(pixel)-1;
240 for( i = 0; i < 4; i++ ) {
241 const int tc = ((tc0[i] - 1) << (BIT_DEPTH - 8)) + 1;
243 pix += inner_iters*ystride;
246 for( d = 0; d < inner_iters; d++ ) {
247 const int p0 = pix[-1*xstride];
248 const int p1 = pix[-2*xstride];
249 const int q0 = pix[0];
250 const int q1 = pix[1*xstride];
252 if( FFABS( p0 - q0 ) < alpha &&
253 FFABS( p1 - p0 ) < beta &&
254 FFABS( q1 - q0 ) < beta ) {
256 int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
258 pix[-xstride] = av_clip_pixel( p0 + delta ); /* p0' */
259 pix[0] = av_clip_pixel( q0 - delta ); /* q0' */
265 static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
267 FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), 2, alpha, beta, tc0);
269 static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
271 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
273 static void FUNCC(h264_h_loop_filter_chroma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
275 FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 1, alpha, beta, tc0);
278 static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma_intra)(uint8_t *p_pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
280 pixel *pix = (pixel*)p_pix;
282 xstride >>= sizeof(pixel)-1;
283 ystride >>= sizeof(pixel)-1;
284 alpha <<= BIT_DEPTH - 8;
285 beta <<= BIT_DEPTH - 8;
286 for( d = 0; d < 4 * inner_iters; d++ ) {
287 const int p0 = pix[-1*xstride];
288 const int p1 = pix[-2*xstride];
289 const int q0 = pix[0];
290 const int q1 = pix[1*xstride];
292 if( FFABS( p0 - q0 ) < alpha &&
293 FFABS( p1 - p0 ) < beta &&
294 FFABS( q1 - q0 ) < beta ) {
296 pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
297 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
302 static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
304 FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), 2, alpha, beta);
306 static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
308 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
310 static void FUNCC(h264_h_loop_filter_chroma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
312 FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 1, alpha, beta);