3 * Copyright (c) 2004-2011 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "high_bit_depth.h"
30 #ifndef AVCODEC_H264IDCT_INTERNAL_H
31 #define AVCODEC_H264IDCT_INTERNAL_H
32 //FIXME this table is a duplicate from h264data.h, and will be removed once the tables from, h264 have been split
33 static const uint8_t scan8[16*3]={
34 4+ 1*8, 5+ 1*8, 4+ 2*8, 5+ 2*8,
35 6+ 1*8, 7+ 1*8, 6+ 2*8, 7+ 2*8,
36 4+ 3*8, 5+ 3*8, 4+ 4*8, 5+ 4*8,
37 6+ 3*8, 7+ 3*8, 6+ 4*8, 7+ 4*8,
38 4+ 6*8, 5+ 6*8, 4+ 7*8, 5+ 7*8,
39 6+ 6*8, 7+ 6*8, 6+ 7*8, 7+ 7*8,
40 4+ 8*8, 5+ 8*8, 4+ 9*8, 5+ 9*8,
41 6+ 8*8, 7+ 8*8, 6+ 9*8, 7+ 9*8,
42 4+11*8, 5+11*8, 4+12*8, 5+12*8,
43 6+11*8, 7+11*8, 6+12*8, 7+12*8,
44 4+13*8, 5+13*8, 4+14*8, 5+14*8,
45 6+13*8, 7+13*8, 6+14*8, 7+14*8
49 static av_always_inline void FUNCC(idct_internal)(uint8_t *_dst, DCTELEM *_block, int stride, int block_stride, int shift, int add){
52 pixel *dst = (pixel*)_dst;
53 dctcoef *block = (dctcoef*)_block;
54 stride /= sizeof(pixel);
56 block[0] += 1<<(shift-1);
59 const int z0= block[i + block_stride*0] + block[i + block_stride*2];
60 const int z1= block[i + block_stride*0] - block[i + block_stride*2];
61 const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3];
62 const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1);
64 block[i + block_stride*0]= z0 + z3;
65 block[i + block_stride*1]= z1 + z2;
66 block[i + block_stride*2]= z1 - z2;
67 block[i + block_stride*3]= z0 - z3;
71 const int z0= block[0 + block_stride*i] + block[2 + block_stride*i];
72 const int z1= block[0 + block_stride*i] - block[2 + block_stride*i];
73 const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i];
74 const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1);
76 dst[i + 0*stride]= CLIP(add*dst[i + 0*stride] + ((z0 + z3) >> shift));
77 dst[i + 1*stride]= CLIP(add*dst[i + 1*stride] + ((z1 + z2) >> shift));
78 dst[i + 2*stride]= CLIP(add*dst[i + 2*stride] + ((z1 - z2) >> shift));
79 dst[i + 3*stride]= CLIP(add*dst[i + 3*stride] + ((z0 - z3) >> shift));
83 void FUNCC(ff_h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride){
84 FUNCC(idct_internal)(dst, block, stride, 4, 6, 1);
87 void FUNCC(ff_h264_lowres_idct_add)(uint8_t *dst, int stride, DCTELEM *block){
88 FUNCC(idct_internal)(dst, block, stride, 8, 3, 1);
91 void FUNCC(ff_h264_lowres_idct_put)(uint8_t *dst, int stride, DCTELEM *block){
92 FUNCC(idct_internal)(dst, block, stride, 8, 3, 0);
95 void FUNCC(ff_h264_idct8_add)(uint8_t *_dst, DCTELEM *_block, int stride){
98 pixel *dst = (pixel*)_dst;
99 dctcoef *block = (dctcoef*)_block;
100 stride /= sizeof(pixel);
104 for( i = 0; i < 8; i++ )
106 const int a0 = block[i+0*8] + block[i+4*8];
107 const int a2 = block[i+0*8] - block[i+4*8];
108 const int a4 = (block[i+2*8]>>1) - block[i+6*8];
109 const int a6 = (block[i+6*8]>>1) + block[i+2*8];
111 const int b0 = a0 + a6;
112 const int b2 = a2 + a4;
113 const int b4 = a2 - a4;
114 const int b6 = a0 - a6;
116 const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
117 const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
118 const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
119 const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
121 const int b1 = (a7>>2) + a1;
122 const int b3 = a3 + (a5>>2);
123 const int b5 = (a3>>2) - a5;
124 const int b7 = a7 - (a1>>2);
126 block[i+0*8] = b0 + b7;
127 block[i+7*8] = b0 - b7;
128 block[i+1*8] = b2 + b5;
129 block[i+6*8] = b2 - b5;
130 block[i+2*8] = b4 + b3;
131 block[i+5*8] = b4 - b3;
132 block[i+3*8] = b6 + b1;
133 block[i+4*8] = b6 - b1;
135 for( i = 0; i < 8; i++ )
137 const int a0 = block[0+i*8] + block[4+i*8];
138 const int a2 = block[0+i*8] - block[4+i*8];
139 const int a4 = (block[2+i*8]>>1) - block[6+i*8];
140 const int a6 = (block[6+i*8]>>1) + block[2+i*8];
142 const int b0 = a0 + a6;
143 const int b2 = a2 + a4;
144 const int b4 = a2 - a4;
145 const int b6 = a0 - a6;
147 const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
148 const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
149 const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
150 const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
152 const int b1 = (a7>>2) + a1;
153 const int b3 = a3 + (a5>>2);
154 const int b5 = (a3>>2) - a5;
155 const int b7 = a7 - (a1>>2);
157 dst[i + 0*stride] = CLIP( dst[i + 0*stride] + ((b0 + b7) >> 6) );
158 dst[i + 1*stride] = CLIP( dst[i + 1*stride] + ((b2 + b5) >> 6) );
159 dst[i + 2*stride] = CLIP( dst[i + 2*stride] + ((b4 + b3) >> 6) );
160 dst[i + 3*stride] = CLIP( dst[i + 3*stride] + ((b6 + b1) >> 6) );
161 dst[i + 4*stride] = CLIP( dst[i + 4*stride] + ((b6 - b1) >> 6) );
162 dst[i + 5*stride] = CLIP( dst[i + 5*stride] + ((b4 - b3) >> 6) );
163 dst[i + 6*stride] = CLIP( dst[i + 6*stride] + ((b2 - b5) >> 6) );
164 dst[i + 7*stride] = CLIP( dst[i + 7*stride] + ((b0 - b7) >> 6) );
168 // assumes all AC coefs are 0
169 void FUNCC(ff_h264_idct_dc_add)(uint8_t *_dst, DCTELEM *block, int stride){
171 int dc = (((dctcoef*)block)[0] + 32) >> 6;
173 pixel *dst = (pixel*)_dst;
174 stride /= sizeof(pixel);
175 for( j = 0; j < 4; j++ )
177 for( i = 0; i < 4; i++ )
178 dst[i] = CLIP( dst[i] + dc );
183 void FUNCC(ff_h264_idct8_dc_add)(uint8_t *_dst, DCTELEM *block, int stride){
185 int dc = (((dctcoef*)block)[0] + 32) >> 6;
187 pixel *dst = (pixel*)_dst;
188 stride /= sizeof(pixel);
189 for( j = 0; j < 8; j++ )
191 for( i = 0; i < 8; i++ )
192 dst[i] = CLIP( dst[i] + dc );
197 void FUNCC(ff_h264_idct_add16)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
200 int nnz = nnzc[ scan8[i] ];
202 if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
203 else FUNCC(idct_internal )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
208 void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
211 if(nnzc[ scan8[i] ]) FUNCC(idct_internal )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
212 else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
216 void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
218 for(i=0; i<16; i+=4){
219 int nnz = nnzc[ scan8[i] ];
221 if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
222 else FUNCC(ff_h264_idct8_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
227 void FUNCC(ff_h264_idct_add8)(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
230 for(i=j*16; i<j*16+4; i++){
232 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
233 else if(((dctcoef*)block)[i*16])
234 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
239 * IDCT transforms the 16 dc values and dequantizes them.
240 * @param qmul quantization parameter
242 void FUNCC(ff_h264_luma_dc_dequant_idct)(DCTELEM *_output, DCTELEM *_input, int qmul){
246 static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
247 dctcoef *input = (dctcoef*)_input;
248 dctcoef *output = (dctcoef*)_output;
251 const int z0= input[4*i+0] + input[4*i+1];
252 const int z1= input[4*i+0] - input[4*i+1];
253 const int z2= input[4*i+2] - input[4*i+3];
254 const int z3= input[4*i+2] + input[4*i+3];
263 const int offset= x_offset[i];
264 const int z0= temp[4*0+i] + temp[4*2+i];
265 const int z1= temp[4*0+i] - temp[4*2+i];
266 const int z2= temp[4*1+i] - temp[4*3+i];
267 const int z3= temp[4*1+i] + temp[4*3+i];
269 output[stride* 0+offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
270 output[stride* 1+offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
271 output[stride* 4+offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
272 output[stride* 5+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
277 void FUNCC(ff_h264_chroma_dc_dequant_idct)(DCTELEM *_block, int qmul){
278 const int stride= 16*2;
279 const int xStride= 16;
281 dctcoef *block = (dctcoef*)_block;
283 a= block[stride*0 + xStride*0];
284 b= block[stride*0 + xStride*1];
285 c= block[stride*1 + xStride*0];
286 d= block[stride*1 + xStride*1];
293 block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
294 block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
295 block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
296 block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;