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 "bit_depth_template.c"
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 void FUNCC(ff_h264_idct_add)(uint8_t *_dst, DCTELEM *_block, int stride)
52 pixel *dst = (pixel*)_dst;
53 dctcoef *block = (dctcoef*)_block;
54 stride /= sizeof(pixel);
59 const int z0= block[i + 4*0] + block[i + 4*2];
60 const int z1= block[i + 4*0] - block[i + 4*2];
61 const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
62 const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
64 block[i + 4*0]= z0 + z3;
65 block[i + 4*1]= z1 + z2;
66 block[i + 4*2]= z1 - z2;
67 block[i + 4*3]= z0 - z3;
71 const int z0= block[0 + 4*i] + block[2 + 4*i];
72 const int z1= block[0 + 4*i] - block[2 + 4*i];
73 const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
74 const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
76 dst[i + 0*stride]= av_clip_pixel(dst[i + 0*stride] + ((z0 + z3) >> 6));
77 dst[i + 1*stride]= av_clip_pixel(dst[i + 1*stride] + ((z1 + z2) >> 6));
78 dst[i + 2*stride]= av_clip_pixel(dst[i + 2*stride] + ((z1 - z2) >> 6));
79 dst[i + 3*stride]= av_clip_pixel(dst[i + 3*stride] + ((z0 - z3) >> 6));
83 void FUNCC(ff_h264_idct8_add)(uint8_t *_dst, DCTELEM *_block, int stride){
85 pixel *dst = (pixel*)_dst;
86 dctcoef *block = (dctcoef*)_block;
87 stride /= sizeof(pixel);
91 for( i = 0; i < 8; i++ )
93 const int a0 = block[i+0*8] + block[i+4*8];
94 const int a2 = block[i+0*8] - block[i+4*8];
95 const int a4 = (block[i+2*8]>>1) - block[i+6*8];
96 const int a6 = (block[i+6*8]>>1) + block[i+2*8];
98 const int b0 = a0 + a6;
99 const int b2 = a2 + a4;
100 const int b4 = a2 - a4;
101 const int b6 = a0 - a6;
103 const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
104 const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
105 const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
106 const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
108 const int b1 = (a7>>2) + a1;
109 const int b3 = a3 + (a5>>2);
110 const int b5 = (a3>>2) - a5;
111 const int b7 = a7 - (a1>>2);
113 block[i+0*8] = b0 + b7;
114 block[i+7*8] = b0 - b7;
115 block[i+1*8] = b2 + b5;
116 block[i+6*8] = b2 - b5;
117 block[i+2*8] = b4 + b3;
118 block[i+5*8] = b4 - b3;
119 block[i+3*8] = b6 + b1;
120 block[i+4*8] = b6 - b1;
122 for( i = 0; i < 8; i++ )
124 const int a0 = block[0+i*8] + block[4+i*8];
125 const int a2 = block[0+i*8] - block[4+i*8];
126 const int a4 = (block[2+i*8]>>1) - block[6+i*8];
127 const int a6 = (block[6+i*8]>>1) + block[2+i*8];
129 const int b0 = a0 + a6;
130 const int b2 = a2 + a4;
131 const int b4 = a2 - a4;
132 const int b6 = a0 - a6;
134 const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
135 const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
136 const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
137 const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
139 const int b1 = (a7>>2) + a1;
140 const int b3 = a3 + (a5>>2);
141 const int b5 = (a3>>2) - a5;
142 const int b7 = a7 - (a1>>2);
144 dst[i + 0*stride] = av_clip_pixel( dst[i + 0*stride] + ((b0 + b7) >> 6) );
145 dst[i + 1*stride] = av_clip_pixel( dst[i + 1*stride] + ((b2 + b5) >> 6) );
146 dst[i + 2*stride] = av_clip_pixel( dst[i + 2*stride] + ((b4 + b3) >> 6) );
147 dst[i + 3*stride] = av_clip_pixel( dst[i + 3*stride] + ((b6 + b1) >> 6) );
148 dst[i + 4*stride] = av_clip_pixel( dst[i + 4*stride] + ((b6 - b1) >> 6) );
149 dst[i + 5*stride] = av_clip_pixel( dst[i + 5*stride] + ((b4 - b3) >> 6) );
150 dst[i + 6*stride] = av_clip_pixel( dst[i + 6*stride] + ((b2 - b5) >> 6) );
151 dst[i + 7*stride] = av_clip_pixel( dst[i + 7*stride] + ((b0 - b7) >> 6) );
155 // assumes all AC coefs are 0
156 void FUNCC(ff_h264_idct_dc_add)(uint8_t *_dst, DCTELEM *block, int stride){
158 int dc = (((dctcoef*)block)[0] + 32) >> 6;
159 pixel *dst = (pixel*)_dst;
160 stride /= sizeof(pixel);
161 for( j = 0; j < 4; j++ )
163 for( i = 0; i < 4; i++ )
164 dst[i] = av_clip_pixel( dst[i] + dc );
169 void FUNCC(ff_h264_idct8_dc_add)(uint8_t *_dst, DCTELEM *block, int stride){
171 int dc = (((dctcoef*)block)[0] + 32) >> 6;
172 pixel *dst = (pixel*)_dst;
173 stride /= sizeof(pixel);
174 for( j = 0; j < 8; j++ )
176 for( i = 0; i < 8; i++ )
177 dst[i] = av_clip_pixel( dst[i] + dc );
182 void FUNCC(ff_h264_idct_add16)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
185 int nnz = nnzc[ scan8[i] ];
187 if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
188 else FUNCC(ff_h264_idct_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
193 void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
196 if(nnzc[ scan8[i] ]) FUNCC(ff_h264_idct_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
197 else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
201 void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
203 for(i=0; i<16; i+=4){
204 int nnz = nnzc[ scan8[i] ];
206 if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
207 else FUNCC(ff_h264_idct8_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
212 void FUNCC(ff_h264_idct_add8)(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
215 for(i=j*16; i<j*16+4; i++){
217 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
218 else if(((dctcoef*)block)[i*16])
219 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
224 void FUNCC(ff_h264_idct_add8_422)(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
228 for(i=j*16; i<j*16+4; i++){
230 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
231 else if(((dctcoef*)block)[i*16])
232 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
237 for(i=j*16+4; i<j*16+8; i++){
238 if(nnzc[ scan8[i+4] ])
239 FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i+4], block + i*16*sizeof(pixel), stride);
240 else if(((dctcoef*)block)[i*16])
241 FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i+4], block + i*16*sizeof(pixel), stride);
247 * IDCT transforms the 16 dc values and dequantizes them.
248 * @param qmul quantization parameter
250 void FUNCC(ff_h264_luma_dc_dequant_idct)(DCTELEM *_output, DCTELEM *_input, int qmul){
254 static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
255 dctcoef *input = (dctcoef*)_input;
256 dctcoef *output = (dctcoef*)_output;
259 const int z0= input[4*i+0] + input[4*i+1];
260 const int z1= input[4*i+0] - input[4*i+1];
261 const int z2= input[4*i+2] - input[4*i+3];
262 const int z3= input[4*i+2] + input[4*i+3];
271 const int offset= x_offset[i];
272 const int z0= temp[4*0+i] + temp[4*2+i];
273 const int z1= temp[4*0+i] - temp[4*2+i];
274 const int z2= temp[4*1+i] - temp[4*3+i];
275 const int z3= temp[4*1+i] + temp[4*3+i];
277 output[stride* 0+offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
278 output[stride* 1+offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
279 output[stride* 4+offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
280 output[stride* 5+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
285 void FUNCC(ff_h264_chroma422_dc_dequant_idct)(DCTELEM *_block, int qmul){
286 const int stride= 16*2;
287 const int xStride= 16;
290 static const uint8_t x_offset[2]={0, 16};
291 dctcoef *block = (dctcoef*)_block;
294 temp[2*i+0] = block[stride*i + xStride*0] + block[stride*i + xStride*1];
295 temp[2*i+1] = block[stride*i + xStride*0] - block[stride*i + xStride*1];
299 const int offset= x_offset[i];
300 const int z0= temp[2*0+i] + temp[2*2+i];
301 const int z1= temp[2*0+i] - temp[2*2+i];
302 const int z2= temp[2*1+i] - temp[2*3+i];
303 const int z3= temp[2*1+i] + temp[2*3+i];
305 block[stride*0+offset]= ((z0 + z3)*qmul + 128) >> 8;
306 block[stride*1+offset]= ((z1 + z2)*qmul + 128) >> 8;
307 block[stride*2+offset]= ((z1 - z2)*qmul + 128) >> 8;
308 block[stride*3+offset]= ((z0 - z3)*qmul + 128) >> 8;
312 void FUNCC(ff_h264_chroma_dc_dequant_idct)(DCTELEM *_block, int qmul){
313 const int stride= 16*2;
314 const int xStride= 16;
316 dctcoef *block = (dctcoef*)_block;
318 a= block[stride*0 + xStride*0];
319 b= block[stride*0 + xStride*1];
320 c= block[stride*1 + xStride*0];
321 d= block[stride*1 + xStride*1];
328 block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
329 block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
330 block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
331 block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;