Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264idct_internal.h @ 8dbe5856

History | View | Annotate | Download (10.5 KB)

1
/*
2
 * H.264 IDCT
3
 * Copyright (c) 2004-2011 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg 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.
11
 *
12
 * FFmpeg 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.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * H.264 IDCT.
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27

    
28
#include "h264_high_depth.h"
29

    
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 + 2*4]={
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
 1+1*8, 2+1*8,
39
 1+2*8, 2+2*8,
40
 1+4*8, 2+4*8,
41
 1+5*8, 2+5*8,
42
};
43
#endif
44

    
45
static av_always_inline void FUNCC(idct_internal)(uint8_t *p_dst, DCTELEM *p_block, int stride, int block_stride, int shift, int add){
46
    int i;
47
    INIT_CLIP
48
    pixel *dst = (pixel*)p_dst;
49
    dctcoef *block = (dctcoef*)p_block;
50
    stride >>= sizeof(pixel)-1;
51

    
52
    block[0] += 1<<(shift-1);
53

    
54
    for(i=0; i<4; i++){
55
        const int z0=  block[i + block_stride*0]     +  block[i + block_stride*2];
56
        const int z1=  block[i + block_stride*0]     -  block[i + block_stride*2];
57
        const int z2= (block[i + block_stride*1]>>1) -  block[i + block_stride*3];
58
        const int z3=  block[i + block_stride*1]     + (block[i + block_stride*3]>>1);
59

    
60
        block[i + block_stride*0]= z0 + z3;
61
        block[i + block_stride*1]= z1 + z2;
62
        block[i + block_stride*2]= z1 - z2;
63
        block[i + block_stride*3]= z0 - z3;
64
    }
65

    
66
    for(i=0; i<4; i++){
67
        const int z0=  block[0 + block_stride*i]     +  block[2 + block_stride*i];
68
        const int z1=  block[0 + block_stride*i]     -  block[2 + block_stride*i];
69
        const int z2= (block[1 + block_stride*i]>>1) -  block[3 + block_stride*i];
70
        const int z3=  block[1 + block_stride*i]     + (block[3 + block_stride*i]>>1);
71

    
72
        dst[i + 0*stride]= CLIP(add*dst[i + 0*stride] + ((z0 + z3) >> shift));
73
        dst[i + 1*stride]= CLIP(add*dst[i + 1*stride] + ((z1 + z2) >> shift));
74
        dst[i + 2*stride]= CLIP(add*dst[i + 2*stride] + ((z1 - z2) >> shift));
75
        dst[i + 3*stride]= CLIP(add*dst[i + 3*stride] + ((z0 - z3) >> shift));
76
    }
77
}
78

    
79
void FUNCC(ff_h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride){
80
    FUNCC(idct_internal)(dst, block, stride, 4, 6, 1);
81
}
82

    
83
void FUNCC(ff_h264_lowres_idct_add)(uint8_t *dst, int stride, DCTELEM *block){
84
    FUNCC(idct_internal)(dst, block, stride, 8, 3, 1);
85
}
86

    
87
void FUNCC(ff_h264_lowres_idct_put)(uint8_t *dst, int stride, DCTELEM *block){
88
    FUNCC(idct_internal)(dst, block, stride, 8, 3, 0);
89
}
90

    
91
void FUNCC(ff_h264_idct8_add)(uint8_t *p_dst, DCTELEM *p_block, int stride){
92
    int i;
93
    INIT_CLIP
94
    pixel *dst = (pixel*)p_dst;
95
    dctcoef *block = (dctcoef*)p_block;
96
    stride >>= sizeof(pixel)-1;
97

    
98
    block[0] += 32;
99

    
100
    for( i = 0; i < 8; i++ )
101
    {
102
        const int a0 =  block[i+0*8] + block[i+4*8];
103
        const int a2 =  block[i+0*8] - block[i+4*8];
104
        const int a4 = (block[i+2*8]>>1) - block[i+6*8];
105
        const int a6 = (block[i+6*8]>>1) + block[i+2*8];
106

    
107
        const int b0 = a0 + a6;
108
        const int b2 = a2 + a4;
109
        const int b4 = a2 - a4;
110
        const int b6 = a0 - a6;
111

    
112
        const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
113
        const int a3 =  block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
114
        const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
115
        const int a7 =  block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
116

    
117
        const int b1 = (a7>>2) + a1;
118
        const int b3 =  a3 + (a5>>2);
119
        const int b5 = (a3>>2) - a5;
120
        const int b7 =  a7 - (a1>>2);
121

    
122
        block[i+0*8] = b0 + b7;
123
        block[i+7*8] = b0 - b7;
124
        block[i+1*8] = b2 + b5;
125
        block[i+6*8] = b2 - b5;
126
        block[i+2*8] = b4 + b3;
127
        block[i+5*8] = b4 - b3;
128
        block[i+3*8] = b6 + b1;
129
        block[i+4*8] = b6 - b1;
130
    }
131
    for( i = 0; i < 8; i++ )
132
    {
133
        const int a0 =  block[0+i*8] + block[4+i*8];
134
        const int a2 =  block[0+i*8] - block[4+i*8];
135
        const int a4 = (block[2+i*8]>>1) - block[6+i*8];
136
        const int a6 = (block[6+i*8]>>1) + block[2+i*8];
137

    
138
        const int b0 = a0 + a6;
139
        const int b2 = a2 + a4;
140
        const int b4 = a2 - a4;
141
        const int b6 = a0 - a6;
142

    
143
        const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
144
        const int a3 =  block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
145
        const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
146
        const int a7 =  block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
147

    
148
        const int b1 = (a7>>2) + a1;
149
        const int b3 =  a3 + (a5>>2);
150
        const int b5 = (a3>>2) - a5;
151
        const int b7 =  a7 - (a1>>2);
152

    
153
        dst[i + 0*stride] = CLIP( dst[i + 0*stride] + ((b0 + b7) >> 6) );
154
        dst[i + 1*stride] = CLIP( dst[i + 1*stride] + ((b2 + b5) >> 6) );
155
        dst[i + 2*stride] = CLIP( dst[i + 2*stride] + ((b4 + b3) >> 6) );
156
        dst[i + 3*stride] = CLIP( dst[i + 3*stride] + ((b6 + b1) >> 6) );
157
        dst[i + 4*stride] = CLIP( dst[i + 4*stride] + ((b6 - b1) >> 6) );
158
        dst[i + 5*stride] = CLIP( dst[i + 5*stride] + ((b4 - b3) >> 6) );
159
        dst[i + 6*stride] = CLIP( dst[i + 6*stride] + ((b2 - b5) >> 6) );
160
        dst[i + 7*stride] = CLIP( dst[i + 7*stride] + ((b0 - b7) >> 6) );
161
    }
162
}
163

    
164
// assumes all AC coefs are 0
165
void FUNCC(ff_h264_idct_dc_add)(uint8_t *p_dst, DCTELEM *block, int stride){
166
    int i, j;
167
    int dc = (((dctcoef*)block)[0] + 32) >> 6;
168
    INIT_CLIP
169
    pixel *dst = (pixel*)p_dst;
170
    stride >>= sizeof(pixel)-1;
171
    for( j = 0; j < 4; j++ )
172
    {
173
        for( i = 0; i < 4; i++ )
174
            dst[i] = CLIP( dst[i] + dc );
175
        dst += stride;
176
    }
177
}
178

    
179
void FUNCC(ff_h264_idct8_dc_add)(uint8_t *p_dst, DCTELEM *block, int stride){
180
    int i, j;
181
    int dc = (((dctcoef*)block)[0] + 32) >> 6;
182
    INIT_CLIP
183
    pixel *dst = (pixel*)p_dst;
184
    stride >>= sizeof(pixel)-1;
185
    for( j = 0; j < 8; j++ )
186
    {
187
        for( i = 0; i < 8; i++ )
188
            dst[i] = CLIP( dst[i] + dc );
189
        dst += stride;
190
    }
191
}
192

    
193
void FUNCC(ff_h264_idct_add16)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
194
    int i;
195
    for(i=0; i<16; i++){
196
        int nnz = nnzc[ scan8[i] ];
197
        if(nnz){
198
            if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
199
            else                                  FUNCC(idct_internal      )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
200
        }
201
    }
202
}
203

    
204
void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
205
    int i;
206
    for(i=0; i<16; i++){
207
        if(nnzc[ scan8[i] ])             FUNCC(idct_internal      )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
208
        else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
209
    }
210
}
211

    
212
void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
213
    int i;
214
    for(i=0; i<16; i+=4){
215
        int nnz = nnzc[ scan8[i] ];
216
        if(nnz){
217
            if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
218
            else                                  FUNCC(ff_h264_idct8_add   )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
219
        }
220
    }
221
}
222

    
223
void FUNCC(ff_h264_idct_add8)(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
224
    int i;
225
    for(i=16; i<16+8; i++){
226
        if(nnzc[ scan8[i] ])
227
            FUNCC(ff_h264_idct_add   )(dest[(i&4)>>2] + block_offset[i], block + i*16*sizeof(pixel), stride);
228
        else if(((dctcoef*)block)[i*16])
229
            FUNCC(ff_h264_idct_dc_add)(dest[(i&4)>>2] + block_offset[i], block + i*16*sizeof(pixel), stride);
230
    }
231
}
232
/**
233
 * IDCT transforms the 16 dc values and dequantizes them.
234
 * @param qp quantization parameter
235
 */
236
void FUNCC(ff_h264_luma_dc_dequant_idct)(DCTELEM *p_output, DCTELEM *p_input, int qmul){
237
#define stride 16
238
    int i;
239
    int temp[16];
240
    static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
241
    dctcoef *input = (dctcoef*)p_input;
242
    dctcoef *output = (dctcoef*)p_output;
243

    
244
    for(i=0; i<4; i++){
245
        const int z0= input[4*i+0] + input[4*i+1];
246
        const int z1= input[4*i+0] - input[4*i+1];
247
        const int z2= input[4*i+2] - input[4*i+3];
248
        const int z3= input[4*i+2] + input[4*i+3];
249

    
250
        temp[4*i+0]= z0+z3;
251
        temp[4*i+1]= z0-z3;
252
        temp[4*i+2]= z1-z2;
253
        temp[4*i+3]= z1+z2;
254
    }
255

    
256
    for(i=0; i<4; i++){
257
        const int offset= x_offset[i];
258
        const int z0= temp[4*0+i] + temp[4*2+i];
259
        const int z1= temp[4*0+i] - temp[4*2+i];
260
        const int z2= temp[4*1+i] - temp[4*3+i];
261
        const int z3= temp[4*1+i] + temp[4*3+i];
262

    
263
        output[stride* 0+offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
264
        output[stride* 1+offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
265
        output[stride* 4+offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
266
        output[stride* 5+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
267
    }
268
#undef stride
269
}
270

    
271
void FUNCC(ff_h264_chroma_dc_dequant_idct)(DCTELEM *p_block, int qmul){
272
    const int stride= 16*2;
273
    const int xStride= 16;
274
    int a,b,c,d,e;
275
    dctcoef *block = (dctcoef*)p_block;
276

    
277
    a= block[stride*0 + xStride*0];
278
    b= block[stride*0 + xStride*1];
279
    c= block[stride*1 + xStride*0];
280
    d= block[stride*1 + xStride*1];
281

    
282
    e= a-b;
283
    a= a+b;
284
    b= c-d;
285
    c= c+d;
286

    
287
    block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
288
    block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
289
    block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
290
    block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
291
}