2011-03-29 18:48:50 +03:00
|
|
|
/*
|
|
|
|
* H.264 IDCT
|
|
|
|
* Copyright (c) 2004-2011 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*
|
2011-05-11 14:56:12 +03:00
|
|
|
* This file is part of FFmpeg.
|
2011-03-29 18:48:50 +03:00
|
|
|
*
|
2011-05-11 14:56:12 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-03-29 18:48:50 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-05-11 14:56:12 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-03-29 18:48:50 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-05-11 14:56:12 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-03-29 18:48:50 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* H.264 IDCT.
|
|
|
|
* @author Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*/
|
|
|
|
|
2011-07-05 18:33:24 +03:00
|
|
|
#include "bit_depth_template.c"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2013-01-19 01:00:18 +03:00
|
|
|
#include "h264.h"
|
2014-01-09 12:27:20 +03:00
|
|
|
#include "h264idct.h"
|
2011-03-29 18:48:50 +03:00
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct_add)(uint8_t *_dst, int16_t *_block, int stride)
|
2011-07-20 11:46:18 +03:00
|
|
|
{
|
2011-03-29 18:48:50 +03:00
|
|
|
int i;
|
2011-03-29 18:48:54 +03:00
|
|
|
pixel *dst = (pixel*)_dst;
|
|
|
|
dctcoef *block = (dctcoef*)_block;
|
2011-05-11 14:56:12 +03:00
|
|
|
stride >>= sizeof(pixel)-1;
|
2011-03-29 18:48:50 +03:00
|
|
|
|
2011-07-20 11:46:18 +03:00
|
|
|
block[0] += 1 << 5;
|
2011-03-29 18:48:50 +03:00
|
|
|
|
|
|
|
for(i=0; i<4; i++){
|
2011-07-20 11:46:18 +03:00
|
|
|
const int z0= block[i + 4*0] + block[i + 4*2];
|
|
|
|
const int z1= block[i + 4*0] - block[i + 4*2];
|
|
|
|
const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
|
|
|
|
const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
|
|
|
|
|
|
|
|
block[i + 4*0]= z0 + z3;
|
|
|
|
block[i + 4*1]= z1 + z2;
|
|
|
|
block[i + 4*2]= z1 - z2;
|
|
|
|
block[i + 4*3]= z0 - z3;
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<4; i++){
|
2011-07-20 11:46:18 +03:00
|
|
|
const int z0= block[0 + 4*i] + block[2 + 4*i];
|
|
|
|
const int z1= block[0 + 4*i] - block[2 + 4*i];
|
|
|
|
const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
|
|
|
|
const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
|
|
|
|
|
2012-03-06 03:01:19 +03:00
|
|
|
dst[i + 0*stride]= av_clip_pixel(dst[i + 0*stride] + ((z0 + z3) >> 6));
|
|
|
|
dst[i + 1*stride]= av_clip_pixel(dst[i + 1*stride] + ((z1 + z2) >> 6));
|
|
|
|
dst[i + 2*stride]= av_clip_pixel(dst[i + 2*stride] + ((z1 - z2) >> 6));
|
|
|
|
dst[i + 3*stride]= av_clip_pixel(dst[i + 3*stride] + ((z0 - z3) >> 6));
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
2013-02-19 08:03:02 +03:00
|
|
|
|
|
|
|
memset(block, 0, 16 * sizeof(dctcoef));
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct8_add)(uint8_t *_dst, int16_t *_block, int stride){
|
2011-03-29 18:48:50 +03:00
|
|
|
int i;
|
2011-03-29 18:48:54 +03:00
|
|
|
pixel *dst = (pixel*)_dst;
|
|
|
|
dctcoef *block = (dctcoef*)_block;
|
2011-05-11 14:56:12 +03:00
|
|
|
stride >>= sizeof(pixel)-1;
|
2011-03-29 18:48:50 +03:00
|
|
|
|
|
|
|
block[0] += 32;
|
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
|
|
|
const int a0 = block[i+0*8] + block[i+4*8];
|
|
|
|
const int a2 = block[i+0*8] - block[i+4*8];
|
|
|
|
const int a4 = (block[i+2*8]>>1) - block[i+6*8];
|
|
|
|
const int a6 = (block[i+6*8]>>1) + block[i+2*8];
|
|
|
|
|
|
|
|
const int b0 = a0 + a6;
|
|
|
|
const int b2 = a2 + a4;
|
|
|
|
const int b4 = a2 - a4;
|
|
|
|
const int b6 = a0 - a6;
|
|
|
|
|
|
|
|
const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
|
|
|
|
const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
|
|
|
|
const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
|
|
|
|
const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
|
|
|
|
|
|
|
|
const int b1 = (a7>>2) + a1;
|
|
|
|
const int b3 = a3 + (a5>>2);
|
|
|
|
const int b5 = (a3>>2) - a5;
|
|
|
|
const int b7 = a7 - (a1>>2);
|
|
|
|
|
|
|
|
block[i+0*8] = b0 + b7;
|
|
|
|
block[i+7*8] = b0 - b7;
|
|
|
|
block[i+1*8] = b2 + b5;
|
|
|
|
block[i+6*8] = b2 - b5;
|
|
|
|
block[i+2*8] = b4 + b3;
|
|
|
|
block[i+5*8] = b4 - b3;
|
|
|
|
block[i+3*8] = b6 + b1;
|
|
|
|
block[i+4*8] = b6 - b1;
|
|
|
|
}
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
|
|
|
const int a0 = block[0+i*8] + block[4+i*8];
|
|
|
|
const int a2 = block[0+i*8] - block[4+i*8];
|
|
|
|
const int a4 = (block[2+i*8]>>1) - block[6+i*8];
|
|
|
|
const int a6 = (block[6+i*8]>>1) + block[2+i*8];
|
|
|
|
|
|
|
|
const int b0 = a0 + a6;
|
|
|
|
const int b2 = a2 + a4;
|
|
|
|
const int b4 = a2 - a4;
|
|
|
|
const int b6 = a0 - a6;
|
|
|
|
|
|
|
|
const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
|
|
|
|
const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
|
|
|
|
const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
|
|
|
|
const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
|
|
|
|
|
|
|
|
const int b1 = (a7>>2) + a1;
|
|
|
|
const int b3 = a3 + (a5>>2);
|
|
|
|
const int b5 = (a3>>2) - a5;
|
|
|
|
const int b7 = a7 - (a1>>2);
|
|
|
|
|
2012-03-06 03:01:19 +03:00
|
|
|
dst[i + 0*stride] = av_clip_pixel( dst[i + 0*stride] + ((b0 + b7) >> 6) );
|
|
|
|
dst[i + 1*stride] = av_clip_pixel( dst[i + 1*stride] + ((b2 + b5) >> 6) );
|
|
|
|
dst[i + 2*stride] = av_clip_pixel( dst[i + 2*stride] + ((b4 + b3) >> 6) );
|
|
|
|
dst[i + 3*stride] = av_clip_pixel( dst[i + 3*stride] + ((b6 + b1) >> 6) );
|
|
|
|
dst[i + 4*stride] = av_clip_pixel( dst[i + 4*stride] + ((b6 - b1) >> 6) );
|
|
|
|
dst[i + 5*stride] = av_clip_pixel( dst[i + 5*stride] + ((b4 - b3) >> 6) );
|
|
|
|
dst[i + 6*stride] = av_clip_pixel( dst[i + 6*stride] + ((b2 - b5) >> 6) );
|
|
|
|
dst[i + 7*stride] = av_clip_pixel( dst[i + 7*stride] + ((b0 - b7) >> 6) );
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
2013-02-19 08:03:02 +03:00
|
|
|
|
|
|
|
memset(block, 0, 64 * sizeof(dctcoef));
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// assumes all AC coefs are 0
|
2013-02-19 08:03:02 +03:00
|
|
|
void FUNCC(ff_h264_idct_dc_add)(uint8_t *_dst, int16_t *_block, int stride){
|
2011-03-29 18:48:50 +03:00
|
|
|
int i, j;
|
2011-03-29 18:48:54 +03:00
|
|
|
pixel *dst = (pixel*)_dst;
|
2013-02-19 08:03:02 +03:00
|
|
|
dctcoef *block = (dctcoef*)_block;
|
|
|
|
int dc = (block[0] + 32) >> 6;
|
2011-03-29 18:48:54 +03:00
|
|
|
stride /= sizeof(pixel);
|
2013-02-19 08:03:02 +03:00
|
|
|
block[0] = 0;
|
2011-03-29 18:48:50 +03:00
|
|
|
for( j = 0; j < 4; j++ )
|
|
|
|
{
|
|
|
|
for( i = 0; i < 4; i++ )
|
2012-03-06 03:01:19 +03:00
|
|
|
dst[i] = av_clip_pixel( dst[i] + dc );
|
2011-03-29 18:48:50 +03:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 08:03:02 +03:00
|
|
|
void FUNCC(ff_h264_idct8_dc_add)(uint8_t *_dst, int16_t *_block, int stride){
|
2011-03-29 18:48:50 +03:00
|
|
|
int i, j;
|
2011-03-29 18:48:54 +03:00
|
|
|
pixel *dst = (pixel*)_dst;
|
2013-02-19 08:03:02 +03:00
|
|
|
dctcoef *block = (dctcoef*)_block;
|
|
|
|
int dc = (block[0] + 32) >> 6;
|
|
|
|
block[0] = 0;
|
2011-03-29 18:48:54 +03:00
|
|
|
stride /= sizeof(pixel);
|
2011-03-29 18:48:50 +03:00
|
|
|
for( j = 0; j < 8; j++ )
|
|
|
|
{
|
|
|
|
for( i = 0; i < 8; i++ )
|
2012-03-06 03:01:19 +03:00
|
|
|
dst[i] = av_clip_pixel( dst[i] + dc );
|
2011-03-29 18:48:50 +03:00
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct_add16)(uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
|
2011-03-29 18:48:50 +03:00
|
|
|
int i;
|
|
|
|
for(i=0; i<16; i++){
|
|
|
|
int nnz = nnzc[ scan8[i] ];
|
|
|
|
if(nnz){
|
2011-03-29 18:48:54 +03:00
|
|
|
if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
|
2011-07-20 11:46:18 +03:00
|
|
|
else FUNCC(ff_h264_idct_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
|
2011-03-29 18:48:50 +03:00
|
|
|
int i;
|
|
|
|
for(i=0; i<16; i++){
|
2011-07-20 11:46:18 +03:00
|
|
|
if(nnzc[ scan8[i] ]) FUNCC(ff_h264_idct_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
|
2011-03-29 18:48:54 +03:00
|
|
|
else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
|
2011-03-29 18:48:50 +03:00
|
|
|
int i;
|
|
|
|
for(i=0; i<16; i+=4){
|
|
|
|
int nnz = nnzc[ scan8[i] ];
|
|
|
|
if(nnz){
|
2011-03-29 18:48:54 +03:00
|
|
|
if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
|
|
|
|
else FUNCC(ff_h264_idct8_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct_add8)(uint8_t **dest, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
|
2011-06-03 11:12:28 +03:00
|
|
|
int i, j;
|
|
|
|
for(j=1; j<3; j++){
|
|
|
|
for(i=j*16; i<j*16+4; i++){
|
|
|
|
if(nnzc[ scan8[i] ])
|
|
|
|
FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
|
|
|
|
else if(((dctcoef*)block)[i*16])
|
|
|
|
FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
|
|
|
|
}
|
2011-03-29 18:48:50 +03:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 01:39:55 +03:00
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_idct_add8_422)(uint8_t **dest, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
|
2011-08-15 01:39:55 +03:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for(j=1; j<3; j++){
|
|
|
|
for(i=j*16; i<j*16+4; i++){
|
|
|
|
if(nnzc[ scan8[i] ])
|
|
|
|
FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
|
|
|
|
else if(((dctcoef*)block)[i*16])
|
|
|
|
FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(j=1; j<3; j++){
|
|
|
|
for(i=j*16+4; i<j*16+8; i++){
|
|
|
|
if(nnzc[ scan8[i+4] ])
|
|
|
|
FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i+4], block + i*16*sizeof(pixel), stride);
|
|
|
|
else if(((dctcoef*)block)[i*16])
|
|
|
|
FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i+4], block + i*16*sizeof(pixel), stride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-29 18:48:50 +03:00
|
|
|
/**
|
|
|
|
* IDCT transforms the 16 dc values and dequantizes them.
|
2011-07-03 20:29:18 +03:00
|
|
|
* @param qmul quantization parameter
|
2011-03-29 18:48:50 +03:00
|
|
|
*/
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_luma_dc_dequant_idct)(int16_t *_output, int16_t *_input, int qmul){
|
2011-03-29 18:48:50 +03:00
|
|
|
#define stride 16
|
|
|
|
int i;
|
|
|
|
int temp[16];
|
|
|
|
static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
|
2011-03-29 18:48:54 +03:00
|
|
|
dctcoef *input = (dctcoef*)_input;
|
|
|
|
dctcoef *output = (dctcoef*)_output;
|
2011-03-29 18:48:50 +03:00
|
|
|
|
|
|
|
for(i=0; i<4; i++){
|
|
|
|
const int z0= input[4*i+0] + input[4*i+1];
|
|
|
|
const int z1= input[4*i+0] - input[4*i+1];
|
|
|
|
const int z2= input[4*i+2] - input[4*i+3];
|
|
|
|
const int z3= input[4*i+2] + input[4*i+3];
|
|
|
|
|
|
|
|
temp[4*i+0]= z0+z3;
|
|
|
|
temp[4*i+1]= z0-z3;
|
|
|
|
temp[4*i+2]= z1-z2;
|
|
|
|
temp[4*i+3]= z1+z2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<4; i++){
|
|
|
|
const int offset= x_offset[i];
|
|
|
|
const int z0= temp[4*0+i] + temp[4*2+i];
|
|
|
|
const int z1= temp[4*0+i] - temp[4*2+i];
|
|
|
|
const int z2= temp[4*1+i] - temp[4*3+i];
|
|
|
|
const int z3= temp[4*1+i] + temp[4*3+i];
|
|
|
|
|
|
|
|
output[stride* 0+offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
|
|
|
|
output[stride* 1+offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
|
|
|
|
output[stride* 4+offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
|
|
|
|
output[stride* 5+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
|
|
|
|
}
|
|
|
|
#undef stride
|
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_chroma422_dc_dequant_idct)(int16_t *_block, int qmul){
|
2011-08-15 01:39:55 +03:00
|
|
|
const int stride= 16*2;
|
|
|
|
const int xStride= 16;
|
|
|
|
int i;
|
|
|
|
int temp[8];
|
|
|
|
static const uint8_t x_offset[2]={0, 16};
|
2011-08-16 18:05:44 +03:00
|
|
|
dctcoef *block = (dctcoef*)_block;
|
2011-08-15 01:39:55 +03:00
|
|
|
|
|
|
|
for(i=0; i<4; i++){
|
|
|
|
temp[2*i+0] = block[stride*i + xStride*0] + block[stride*i + xStride*1];
|
|
|
|
temp[2*i+1] = block[stride*i + xStride*0] - block[stride*i + xStride*1];
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<2; i++){
|
|
|
|
const int offset= x_offset[i];
|
|
|
|
const int z0= temp[2*0+i] + temp[2*2+i];
|
|
|
|
const int z1= temp[2*0+i] - temp[2*2+i];
|
|
|
|
const int z2= temp[2*1+i] - temp[2*3+i];
|
|
|
|
const int z3= temp[2*1+i] + temp[2*3+i];
|
|
|
|
|
|
|
|
block[stride*0+offset]= ((z0 + z3)*qmul + 128) >> 8;
|
|
|
|
block[stride*1+offset]= ((z1 + z2)*qmul + 128) >> 8;
|
|
|
|
block[stride*2+offset]= ((z1 - z2)*qmul + 128) >> 8;
|
|
|
|
block[stride*3+offset]= ((z0 - z3)*qmul + 128) >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
void FUNCC(ff_h264_chroma_dc_dequant_idct)(int16_t *_block, int qmul){
|
2011-03-29 18:48:50 +03:00
|
|
|
const int stride= 16*2;
|
|
|
|
const int xStride= 16;
|
|
|
|
int a,b,c,d,e;
|
2011-03-29 18:48:54 +03:00
|
|
|
dctcoef *block = (dctcoef*)_block;
|
2011-03-29 18:48:50 +03:00
|
|
|
|
|
|
|
a= block[stride*0 + xStride*0];
|
|
|
|
b= block[stride*0 + xStride*1];
|
|
|
|
c= block[stride*1 + xStride*0];
|
|
|
|
d= block[stride*1 + xStride*1];
|
|
|
|
|
|
|
|
e= a-b;
|
|
|
|
a= a+b;
|
|
|
|
b= c-d;
|
|
|
|
c= c+d;
|
|
|
|
|
|
|
|
block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
|
|
|
|
block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
|
|
|
|
block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
|
|
|
|
block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
|
|
|
|
}
|