Libav
|
00001 /* 00002 * H.264 IDCT 00003 * Copyright (c) 2004 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 "dsputil.h" 00029 00030 static av_always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){ 00031 int i; 00032 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; 00033 00034 block[0] += 1<<(shift-1); 00035 00036 for(i=0; i<4; i++){ 00037 const int z0= block[0 + block_stride*i] + block[2 + block_stride*i]; 00038 const int z1= block[0 + block_stride*i] - block[2 + block_stride*i]; 00039 const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i]; 00040 const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1); 00041 00042 block[0 + block_stride*i]= z0 + z3; 00043 block[1 + block_stride*i]= z1 + z2; 00044 block[2 + block_stride*i]= z1 - z2; 00045 block[3 + block_stride*i]= z0 - z3; 00046 } 00047 00048 for(i=0; i<4; i++){ 00049 const int z0= block[i + block_stride*0] + block[i + block_stride*2]; 00050 const int z1= block[i + block_stride*0] - block[i + block_stride*2]; 00051 const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3]; 00052 const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1); 00053 00054 dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ]; 00055 dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ]; 00056 dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ]; 00057 dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ]; 00058 } 00059 } 00060 00061 void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){ 00062 idct_internal(dst, block, stride, 4, 6, 1); 00063 } 00064 00065 void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){ 00066 idct_internal(dst, block, stride, 8, 3, 1); 00067 } 00068 00069 void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){ 00070 idct_internal(dst, block, stride, 8, 3, 0); 00071 } 00072 00073 void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride){ 00074 int i; 00075 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; 00076 00077 block[0] += 32; 00078 00079 for( i = 0; i < 8; i++ ) 00080 { 00081 const int a0 = block[0+i*8] + block[4+i*8]; 00082 const int a2 = block[0+i*8] - block[4+i*8]; 00083 const int a4 = (block[2+i*8]>>1) - block[6+i*8]; 00084 const int a6 = (block[6+i*8]>>1) + block[2+i*8]; 00085 00086 const int b0 = a0 + a6; 00087 const int b2 = a2 + a4; 00088 const int b4 = a2 - a4; 00089 const int b6 = a0 - a6; 00090 00091 const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1); 00092 const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1); 00093 const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1); 00094 const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1); 00095 00096 const int b1 = (a7>>2) + a1; 00097 const int b3 = a3 + (a5>>2); 00098 const int b5 = (a3>>2) - a5; 00099 const int b7 = a7 - (a1>>2); 00100 00101 block[0+i*8] = b0 + b7; 00102 block[7+i*8] = b0 - b7; 00103 block[1+i*8] = b2 + b5; 00104 block[6+i*8] = b2 - b5; 00105 block[2+i*8] = b4 + b3; 00106 block[5+i*8] = b4 - b3; 00107 block[3+i*8] = b6 + b1; 00108 block[4+i*8] = b6 - b1; 00109 } 00110 for( i = 0; i < 8; i++ ) 00111 { 00112 const int a0 = block[i+0*8] + block[i+4*8]; 00113 const int a2 = block[i+0*8] - block[i+4*8]; 00114 const int a4 = (block[i+2*8]>>1) - block[i+6*8]; 00115 const int a6 = (block[i+6*8]>>1) + block[i+2*8]; 00116 00117 const int b0 = a0 + a6; 00118 const int b2 = a2 + a4; 00119 const int b4 = a2 - a4; 00120 const int b6 = a0 - a6; 00121 00122 const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1); 00123 const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1); 00124 const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1); 00125 const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1); 00126 00127 const int b1 = (a7>>2) + a1; 00128 const int b3 = a3 + (a5>>2); 00129 const int b5 = (a3>>2) - a5; 00130 const int b7 = a7 - (a1>>2); 00131 00132 dst[i + 0*stride] = cm[ dst[i + 0*stride] + ((b0 + b7) >> 6) ]; 00133 dst[i + 1*stride] = cm[ dst[i + 1*stride] + ((b2 + b5) >> 6) ]; 00134 dst[i + 2*stride] = cm[ dst[i + 2*stride] + ((b4 + b3) >> 6) ]; 00135 dst[i + 3*stride] = cm[ dst[i + 3*stride] + ((b6 + b1) >> 6) ]; 00136 dst[i + 4*stride] = cm[ dst[i + 4*stride] + ((b6 - b1) >> 6) ]; 00137 dst[i + 5*stride] = cm[ dst[i + 5*stride] + ((b4 - b3) >> 6) ]; 00138 dst[i + 6*stride] = cm[ dst[i + 6*stride] + ((b2 - b5) >> 6) ]; 00139 dst[i + 7*stride] = cm[ dst[i + 7*stride] + ((b0 - b7) >> 6) ]; 00140 } 00141 } 00142 00143 // assumes all AC coefs are 0 00144 void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){ 00145 int i, j; 00146 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; 00147 int dc = (block[0] + 32) >> 6; 00148 for( j = 0; j < 4; j++ ) 00149 { 00150 for( i = 0; i < 4; i++ ) 00151 dst[i] = cm[ dst[i] + dc ]; 00152 dst += stride; 00153 } 00154 } 00155 00156 void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){ 00157 int i, j; 00158 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; 00159 int dc = (block[0] + 32) >> 6; 00160 for( j = 0; j < 8; j++ ) 00161 { 00162 for( i = 0; i < 8; i++ ) 00163 dst[i] = cm[ dst[i] + dc ]; 00164 dst += stride; 00165 } 00166 } 00167 00168 //FIXME this table is a duplicate from h264data.h, and will be removed once the tables from, h264 have been split 00169 static const uint8_t scan8[16 + 2*4]={ 00170 4+1*8, 5+1*8, 4+2*8, 5+2*8, 00171 6+1*8, 7+1*8, 6+2*8, 7+2*8, 00172 4+3*8, 5+3*8, 4+4*8, 5+4*8, 00173 6+3*8, 7+3*8, 6+4*8, 7+4*8, 00174 1+1*8, 2+1*8, 00175 1+2*8, 2+2*8, 00176 1+4*8, 2+4*8, 00177 1+5*8, 2+5*8, 00178 }; 00179 00180 void ff_h264_idct_add16_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ 00181 int i; 00182 for(i=0; i<16; i++){ 00183 int nnz = nnzc[ scan8[i] ]; 00184 if(nnz){ 00185 if(nnz==1 && block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride); 00186 else idct_internal (dst + block_offset[i], block + i*16, stride, 4, 6, 1); 00187 } 00188 } 00189 } 00190 00191 void ff_h264_idct_add16intra_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ 00192 int i; 00193 for(i=0; i<16; i++){ 00194 if(nnzc[ scan8[i] ]) idct_internal (dst + block_offset[i], block + i*16, stride, 4, 6, 1); 00195 else if(block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride); 00196 } 00197 } 00198 00199 void ff_h264_idct8_add4_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ 00200 int i; 00201 for(i=0; i<16; i+=4){ 00202 int nnz = nnzc[ scan8[i] ]; 00203 if(nnz){ 00204 if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_c(dst + block_offset[i], block + i*16, stride); 00205 else ff_h264_idct8_add_c (dst + block_offset[i], block + i*16, stride); 00206 } 00207 } 00208 } 00209 00210 void ff_h264_idct_add8_c(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ 00211 int i; 00212 for(i=16; i<16+8; i++){ 00213 if(nnzc[ scan8[i] ]) 00214 ff_h264_idct_add_c (dest[(i&4)>>2] + block_offset[i], block + i*16, stride); 00215 else if(block[i*16]) 00216 ff_h264_idct_dc_add_c(dest[(i&4)>>2] + block_offset[i], block + i*16, stride); 00217 } 00218 }