nengel@2: /* nengel@2: * H.264 IDCT nengel@2: * Copyright (c) 2004 Michael Niedermayer nengel@2: * nengel@2: * This file is part of FFmpeg. nengel@2: * nengel@2: * FFmpeg is free software; you can redistribute it and/or nengel@2: * modify it under the terms of the GNU Lesser General Public nengel@2: * License as published by the Free Software Foundation; either nengel@2: * version 2.1 of the License, or (at your option) any later version. nengel@2: * nengel@2: * FFmpeg is distributed in the hope that it will be useful, nengel@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of nengel@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nengel@2: * Lesser General Public License for more details. nengel@2: * nengel@2: * You should have received a copy of the GNU Lesser General Public nengel@2: * License along with FFmpeg; if not, write to the Free Software nengel@2: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA nengel@2: */ nengel@2: nengel@2: /** nengel@2: * @file nengel@2: * H.264 IDCT. nengel@2: * @author Michael Niedermayer nengel@2: */ nengel@2: nengel@2: #include "dsputil.h" nengel@2: #include "h264_data.h" nengel@2: nengel@2: static av_always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){ nengel@2: int i; nengel@2: uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; nengel@2: nengel@2: block[0] += 1<<(shift-1); nengel@2: nengel@2: for(i=0; i<4; i++){ nengel@2: const int z0= block[0 + block_stride*i] + block[2 + block_stride*i]; nengel@2: const int z1= block[0 + block_stride*i] - block[2 + block_stride*i]; nengel@2: const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i]; nengel@2: const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1); nengel@2: nengel@2: block[0 + block_stride*i]= z0 + z3; nengel@2: block[1 + block_stride*i]= z1 + z2; nengel@2: block[2 + block_stride*i]= z1 - z2; nengel@2: block[3 + block_stride*i]= z0 - z3; nengel@2: } nengel@2: nengel@2: for(i=0; i<4; i++){ nengel@2: const int z0= block[i + block_stride*0] + block[i + block_stride*2]; nengel@2: const int z1= block[i + block_stride*0] - block[i + block_stride*2]; nengel@2: const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3]; nengel@2: const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1); nengel@2: nengel@2: dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ]; nengel@2: dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ]; nengel@2: dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ]; nengel@2: dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ]; nengel@2: } nengel@2: } nengel@2: nengel@2: void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){ nengel@2: idct_internal(dst, block, stride, 4, 6, 1); nengel@2: } nengel@2: nengel@2: void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){ nengel@2: idct_internal(dst, block, stride, 8, 3, 1); nengel@2: } nengel@2: nengel@2: void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){ nengel@2: idct_internal(dst, block, stride, 8, 3, 0); nengel@2: } nengel@2: nengel@2: void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride){ nengel@2: int i; nengel@2: uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; nengel@2: nengel@2: block[0] += 32; nengel@2: nengel@2: for( i = 0; i < 8; i++ ) nengel@2: { nengel@2: const int a0 = block[0+i*8] + block[4+i*8]; nengel@2: const int a2 = block[0+i*8] - block[4+i*8]; nengel@2: const int a4 = (block[2+i*8]>>1) - block[6+i*8]; nengel@2: const int a6 = (block[6+i*8]>>1) + block[2+i*8]; nengel@2: nengel@2: const int b0 = a0 + a6; nengel@2: const int b2 = a2 + a4; nengel@2: const int b4 = a2 - a4; nengel@2: const int b6 = a0 - a6; nengel@2: nengel@2: const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1); nengel@2: const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1); nengel@2: const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1); nengel@2: const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1); nengel@2: nengel@2: const int b1 = (a7>>2) + a1; nengel@2: const int b3 = a3 + (a5>>2); nengel@2: const int b5 = (a3>>2) - a5; nengel@2: const int b7 = a7 - (a1>>2); nengel@2: nengel@2: block[0+i*8] = b0 + b7; nengel@2: block[7+i*8] = b0 - b7; nengel@2: block[1+i*8] = b2 + b5; nengel@2: block[6+i*8] = b2 - b5; nengel@2: block[2+i*8] = b4 + b3; nengel@2: block[5+i*8] = b4 - b3; nengel@2: block[3+i*8] = b6 + b1; nengel@2: block[4+i*8] = b6 - b1; nengel@2: } nengel@2: for( i = 0; i < 8; i++ ) nengel@2: { nengel@2: const int a0 = block[i+0*8] + block[i+4*8]; nengel@2: const int a2 = block[i+0*8] - block[i+4*8]; nengel@2: const int a4 = (block[i+2*8]>>1) - block[i+6*8]; nengel@2: const int a6 = (block[i+6*8]>>1) + block[i+2*8]; nengel@2: nengel@2: const int b0 = a0 + a6; nengel@2: const int b2 = a2 + a4; nengel@2: const int b4 = a2 - a4; nengel@2: const int b6 = a0 - a6; nengel@2: nengel@2: const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1); nengel@2: const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1); nengel@2: const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1); nengel@2: const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1); nengel@2: nengel@2: const int b1 = (a7>>2) + a1; nengel@2: const int b3 = a3 + (a5>>2); nengel@2: const int b5 = (a3>>2) - a5; nengel@2: const int b7 = a7 - (a1>>2); nengel@2: nengel@2: dst[i + 0*stride] = cm[ dst[i + 0*stride] + ((b0 + b7) >> 6) ]; nengel@2: dst[i + 1*stride] = cm[ dst[i + 1*stride] + ((b2 + b5) >> 6) ]; nengel@2: dst[i + 2*stride] = cm[ dst[i + 2*stride] + ((b4 + b3) >> 6) ]; nengel@2: dst[i + 3*stride] = cm[ dst[i + 3*stride] + ((b6 + b1) >> 6) ]; nengel@2: dst[i + 4*stride] = cm[ dst[i + 4*stride] + ((b6 - b1) >> 6) ]; nengel@2: dst[i + 5*stride] = cm[ dst[i + 5*stride] + ((b4 - b3) >> 6) ]; nengel@2: dst[i + 6*stride] = cm[ dst[i + 6*stride] + ((b2 - b5) >> 6) ]; nengel@2: dst[i + 7*stride] = cm[ dst[i + 7*stride] + ((b0 - b7) >> 6) ]; nengel@2: } nengel@2: } nengel@2: nengel@2: // assumes all AC coefs are 0 nengel@2: void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){ nengel@2: int i, j; nengel@2: uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; nengel@2: int dc = (block[0] + 32) >> 6; nengel@2: for( j = 0; j < 4; j++ ) nengel@2: { nengel@2: for( i = 0; i < 4; i++ ) nengel@2: dst[i] = cm[ dst[i] + dc ]; nengel@2: dst += stride; nengel@2: } nengel@2: } nengel@2: nengel@2: void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){ nengel@2: int i, j; nengel@2: uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; nengel@2: int dc = (block[0] + 32) >> 6; nengel@2: for( j = 0; j < 8; j++ ) nengel@2: { nengel@2: for( i = 0; i < 8; i++ ) nengel@2: dst[i] = cm[ dst[i] + dc ]; nengel@2: dst += stride; nengel@2: } nengel@2: } nengel@2: nengel@2: void ff_h264_idct_add16_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ nengel@2: int i; nengel@2: for(i=0; i<16; i++){ nengel@2: int nnz = nnzc[ scan8[i] ]; nengel@2: if(nnz){ nengel@2: if(nnz==1 && block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride); nengel@2: else idct_internal (dst + block_offset[i], block + i*16, stride, 4, 6, 1); nengel@2: } nengel@2: } nengel@2: } nengel@2: nengel@2: void ff_h264_idct_add16intra_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ nengel@2: int i; nengel@2: for(i=0; i<16; i++){ nengel@2: if(nnzc[ scan8[i] ]) idct_internal (dst + block_offset[i], block + i*16, stride, 4, 6, 1); nengel@2: else if(block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride); nengel@2: } nengel@2: } nengel@2: nengel@2: void ff_h264_idct8_add4_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ nengel@2: int i; nengel@2: for(i=0; i<16; i+=4){ nengel@2: int nnz = nnzc[ scan8[i] ]; nengel@2: if(nnz){ nengel@2: if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_c(dst + block_offset[i], block + i*16, stride); nengel@2: else ff_h264_idct8_add_c (dst + block_offset[i], block + i*16, stride); nengel@2: } nengel@2: } nengel@2: } nengel@2: nengel@2: void ff_h264_idct_add8_c(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){ nengel@2: int i; nengel@2: for(i=16; i<16+8; i++){ nengel@2: if(nnzc[ scan8[i] ]) nengel@2: ff_h264_idct_add_c (dest[(i&4)>>2] + block_offset[i], block + i*16, stride); nengel@2: else if(block[i*16]) nengel@2: ff_h264_idct_dc_add_c(dest[(i&4)>>2] + block_offset[i], block + i*16, stride); nengel@2: } nengel@2: } nengel@2: nengel@2: /** nengel@2: * IDCT transforms the 16 dc values and dequantizes them. nengel@2: * @param qp quantization parameter nengel@2: */ nengel@2: void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qmul){ nengel@2: #define stride 16 nengel@2: int i; nengel@2: int temp[16]; //FIXME check if this is a good idea nengel@2: static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride}; nengel@2: static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride}; nengel@2: nengel@2: //return; nengel@2: for(i=0; i<4; i++){ nengel@2: const int offset= y_offset[i]; nengel@2: const int z0= block[offset+stride*0] + block[offset+stride*4]; nengel@2: const int z1= block[offset+stride*0] - block[offset+stride*4]; nengel@2: const int z2= block[offset+stride*1] - block[offset+stride*5]; nengel@2: const int z3= block[offset+stride*1] + block[offset+stride*5]; nengel@2: nengel@2: temp[4*i+0]= z0+z3; nengel@2: temp[4*i+1]= z1+z2; nengel@2: temp[4*i+2]= z1-z2; nengel@2: temp[4*i+3]= z0-z3; nengel@2: } nengel@2: nengel@2: for(i=0; i<4; i++){ nengel@2: const int offset= x_offset[i]; nengel@2: const int z0= temp[4*0+i] + temp[4*2+i]; nengel@2: const int z1= temp[4*0+i] - temp[4*2+i]; nengel@2: const int z2= temp[4*1+i] - temp[4*3+i]; nengel@2: const int z3= temp[4*1+i] + temp[4*3+i]; nengel@2: nengel@2: block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual nengel@2: block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8)); nengel@2: block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8)); nengel@2: block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8)); nengel@2: } nengel@2: } nengel@2: nengel@2: #undef xStride nengel@2: #undef stride nengel@2: nengel@2: void chroma_dc_dequant_idct_c(DCTELEM *block, int qmul){ nengel@2: const int stride= 16*2; nengel@2: const int xStride= 16; nengel@2: int a,b,c,d,e; nengel@2: nengel@2: a= block[stride*0 + xStride*0]; nengel@2: b= block[stride*0 + xStride*1]; nengel@2: c= block[stride*1 + xStride*0]; nengel@2: d= block[stride*1 + xStride*1]; nengel@2: nengel@2: e= a-b; nengel@2: a= a+b; nengel@2: b= c-d; nengel@2: c= c+d; nengel@2: nengel@2: block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7; nengel@2: block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7; nengel@2: block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7; nengel@2: block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7; nengel@2: }