• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/h264dsp_template.c

Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
00003  * Copyright (c) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "bit_depth_template.c"
00029 
00030 #define op_scale1(x)  block[x] = av_clip_pixel( (block[x]*weight + offset) >> log2_denom )
00031 #define op_scale2(x)  dst[x] = av_clip_pixel( (src[x]*weights + dst[x]*weightd + offset) >> (log2_denom+1))
00032 #define H264_WEIGHT(W) \
00033 static void FUNCC(weight_h264_pixels ## W)(uint8_t *_block, int stride, int height, \
00034                                            int log2_denom, int weight, int offset) \
00035 { \
00036     int y; \
00037     pixel *block = (pixel*)_block; \
00038     stride >>= sizeof(pixel)-1; \
00039     offset <<= (log2_denom + (BIT_DEPTH-8)); \
00040     if(log2_denom) offset += 1<<(log2_denom-1); \
00041     for (y = 0; y < height; y++, block += stride) { \
00042         op_scale1(0); \
00043         op_scale1(1); \
00044         if(W==2) continue; \
00045         op_scale1(2); \
00046         op_scale1(3); \
00047         if(W==4) continue; \
00048         op_scale1(4); \
00049         op_scale1(5); \
00050         op_scale1(6); \
00051         op_scale1(7); \
00052         if(W==8) continue; \
00053         op_scale1(8); \
00054         op_scale1(9); \
00055         op_scale1(10); \
00056         op_scale1(11); \
00057         op_scale1(12); \
00058         op_scale1(13); \
00059         op_scale1(14); \
00060         op_scale1(15); \
00061     } \
00062 } \
00063 static void FUNCC(biweight_h264_pixels ## W)(uint8_t *_dst, uint8_t *_src, int stride, int height, \
00064                                              int log2_denom, int weightd, int weights, int offset) \
00065 { \
00066     int y; \
00067     pixel *dst = (pixel*)_dst; \
00068     pixel *src = (pixel*)_src; \
00069     stride >>= sizeof(pixel)-1; \
00070     offset <<= (BIT_DEPTH-8); \
00071     offset = ((offset + 1) | 1) << log2_denom; \
00072     for (y = 0; y < height; y++, dst += stride, src += stride) { \
00073         op_scale2(0); \
00074         op_scale2(1); \
00075         if(W==2) continue; \
00076         op_scale2(2); \
00077         op_scale2(3); \
00078         if(W==4) continue; \
00079         op_scale2(4); \
00080         op_scale2(5); \
00081         op_scale2(6); \
00082         op_scale2(7); \
00083         if(W==8) continue; \
00084         op_scale2(8); \
00085         op_scale2(9); \
00086         op_scale2(10); \
00087         op_scale2(11); \
00088         op_scale2(12); \
00089         op_scale2(13); \
00090         op_scale2(14); \
00091         op_scale2(15); \
00092     } \
00093 }
00094 
00095 H264_WEIGHT(16)
00096 H264_WEIGHT(8)
00097 H264_WEIGHT(4)
00098 H264_WEIGHT(2)
00099 
00100 #undef op_scale1
00101 #undef op_scale2
00102 #undef H264_WEIGHT
00103 
00104 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)
00105 {
00106     pixel *pix = (pixel*)p_pix;
00107     int i, d;
00108     xstride >>= sizeof(pixel)-1;
00109     ystride >>= sizeof(pixel)-1;
00110     alpha <<= BIT_DEPTH - 8;
00111     beta  <<= BIT_DEPTH - 8;
00112     for( i = 0; i < 4; i++ ) {
00113         const int tc_orig = tc0[i] << (BIT_DEPTH - 8);
00114         if( tc_orig < 0 ) {
00115             pix += inner_iters*ystride;
00116             continue;
00117         }
00118         for( d = 0; d < inner_iters; d++ ) {
00119             const int p0 = pix[-1*xstride];
00120             const int p1 = pix[-2*xstride];
00121             const int p2 = pix[-3*xstride];
00122             const int q0 = pix[0];
00123             const int q1 = pix[1*xstride];
00124             const int q2 = pix[2*xstride];
00125 
00126             if( FFABS( p0 - q0 ) < alpha &&
00127                 FFABS( p1 - p0 ) < beta &&
00128                 FFABS( q1 - q0 ) < beta ) {
00129 
00130                 int tc = tc_orig;
00131                 int i_delta;
00132 
00133                 if( FFABS( p2 - p0 ) < beta ) {
00134                     if(tc_orig)
00135                     pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc_orig, tc_orig );
00136                     tc++;
00137                 }
00138                 if( FFABS( q2 - q0 ) < beta ) {
00139                     if(tc_orig)
00140                     pix[   xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc_orig, tc_orig );
00141                     tc++;
00142                 }
00143 
00144                 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
00145                 pix[-xstride] = av_clip_pixel( p0 + i_delta );    /* p0' */
00146                 pix[0]        = av_clip_pixel( q0 - i_delta );    /* q0' */
00147             }
00148             pix += ystride;
00149         }
00150     }
00151 }
00152 static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00153 {
00154     FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), 4, alpha, beta, tc0);
00155 }
00156 static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00157 {
00158     FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
00159 }
00160 static void FUNCC(h264_h_loop_filter_luma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00161 {
00162     FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
00163 }
00164 
00165 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)
00166 {
00167     pixel *pix = (pixel*)p_pix;
00168     int d;
00169     xstride >>= sizeof(pixel)-1;
00170     ystride >>= sizeof(pixel)-1;
00171     alpha <<= BIT_DEPTH - 8;
00172     beta  <<= BIT_DEPTH - 8;
00173     for( d = 0; d < 4 * inner_iters; d++ ) {
00174         const int p2 = pix[-3*xstride];
00175         const int p1 = pix[-2*xstride];
00176         const int p0 = pix[-1*xstride];
00177 
00178         const int q0 = pix[ 0*xstride];
00179         const int q1 = pix[ 1*xstride];
00180         const int q2 = pix[ 2*xstride];
00181 
00182         if( FFABS( p0 - q0 ) < alpha &&
00183             FFABS( p1 - p0 ) < beta &&
00184             FFABS( q1 - q0 ) < beta ) {
00185 
00186             if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
00187                 if( FFABS( p2 - p0 ) < beta)
00188                 {
00189                     const int p3 = pix[-4*xstride];
00190                     /* p0', p1', p2' */
00191                     pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
00192                     pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
00193                     pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
00194                 } else {
00195                     /* p0' */
00196                     pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
00197                 }
00198                 if( FFABS( q2 - q0 ) < beta)
00199                 {
00200                     const int q3 = pix[3*xstride];
00201                     /* q0', q1', q2' */
00202                     pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
00203                     pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
00204                     pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
00205                 } else {
00206                     /* q0' */
00207                     pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
00208                 }
00209             }else{
00210                 /* p0', q0' */
00211                 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
00212                 pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
00213             }
00214         }
00215         pix += ystride;
00216     }
00217 }
00218 static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
00219 {
00220     FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), 4, alpha, beta);
00221 }
00222 static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
00223 {
00224     FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
00225 }
00226 static void FUNCC(h264_h_loop_filter_luma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
00227 {
00228     FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
00229 }
00230 
00231 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)
00232 {
00233     pixel *pix = (pixel*)p_pix;
00234     int i, d;
00235     alpha <<= BIT_DEPTH - 8;
00236     beta  <<= BIT_DEPTH - 8;
00237     xstride >>= sizeof(pixel)-1;
00238     ystride >>= sizeof(pixel)-1;
00239     for( i = 0; i < 4; i++ ) {
00240         const int tc = ((tc0[i] - 1) << (BIT_DEPTH - 8)) + 1;
00241         if( tc <= 0 ) {
00242             pix += inner_iters*ystride;
00243             continue;
00244         }
00245         for( d = 0; d < inner_iters; d++ ) {
00246             const int p0 = pix[-1*xstride];
00247             const int p1 = pix[-2*xstride];
00248             const int q0 = pix[0];
00249             const int q1 = pix[1*xstride];
00250 
00251             if( FFABS( p0 - q0 ) < alpha &&
00252                 FFABS( p1 - p0 ) < beta &&
00253                 FFABS( q1 - q0 ) < beta ) {
00254 
00255                 int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
00256 
00257                 pix[-xstride] = av_clip_pixel( p0 + delta );    /* p0' */
00258                 pix[0]        = av_clip_pixel( q0 - delta );    /* q0' */
00259             }
00260             pix += ystride;
00261         }
00262     }
00263 }
00264 static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00265 {
00266     FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), 2, alpha, beta, tc0);
00267 }
00268 static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00269 {
00270     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
00271 }
00272 static void FUNCC(h264_h_loop_filter_chroma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00273 {
00274     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 1, alpha, beta, tc0);
00275 }
00276 static void FUNCC(h264_h_loop_filter_chroma422)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00277 {
00278     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
00279 }
00280 static void FUNCC(h264_h_loop_filter_chroma422_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
00281 {
00282     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
00283 }
00284 
00285 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)
00286 {
00287     pixel *pix = (pixel*)p_pix;
00288     int d;
00289     xstride >>= sizeof(pixel)-1;
00290     ystride >>= sizeof(pixel)-1;
00291     alpha <<= BIT_DEPTH - 8;
00292     beta  <<= BIT_DEPTH - 8;
00293     for( d = 0; d < 4 * inner_iters; d++ ) {
00294         const int p0 = pix[-1*xstride];
00295         const int p1 = pix[-2*xstride];
00296         const int q0 = pix[0];
00297         const int q1 = pix[1*xstride];
00298 
00299         if( FFABS( p0 - q0 ) < alpha &&
00300             FFABS( p1 - p0 ) < beta &&
00301             FFABS( q1 - q0 ) < beta ) {
00302 
00303             pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;   /* p0' */
00304             pix[0]        = ( 2*q1 + q0 + p1 + 2 ) >> 2;   /* q0' */
00305         }
00306         pix += ystride;
00307     }
00308 }
00309 static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
00310 {
00311     FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), 2, alpha, beta);
00312 }
00313 static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
00314 {
00315     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
00316 }
00317 static void FUNCC(h264_h_loop_filter_chroma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
00318 {
00319     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 1, alpha, beta);
00320 }
00321 static void FUNCC(h264_h_loop_filter_chroma422_intra)(uint8_t *pix, int stride, int alpha, int beta)
00322 {
00323     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
00324 }
00325 static void FUNCC(h264_h_loop_filter_chroma422_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
00326 {
00327     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
00328 }
Generated on Fri Feb 1 2013 14:34:36 for FFmpeg by doxygen 1.7.1