Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264idct_internal.h @ 9abd87bb

History | View | Annotate | Download (10.6 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 "dsputil.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
#define pixel  uint8_t
46
#define dctcoef DCTELEM
47
#define INIT_CLIP uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
48
#define CLIP(a) cm[a]
49
#define FUNCC(a) a ## _c
50

    
51
static av_always_inline void FUNCC(idct_internal)(uint8_t *p_dst, DCTELEM *p_block, int stride, int block_stride, int shift, int add){
52
    int i;
53
    INIT_CLIP
54
    pixel *dst = (pixel*)p_dst;
55
    dctcoef *block = (dctcoef*)p_block;
56
    stride >>= sizeof(pixel)-1;
57

    
58
    block[0] += 1<<(shift-1);
59

    
60
    for(i=0; i<4; i++){
61
        const int z0=  block[i + block_stride*0]     +  block[i + block_stride*2];
62
        const int z1=  block[i + block_stride*0]     -  block[i + block_stride*2];
63
        const int z2= (block[i + block_stride*1]>>1) -  block[i + block_stride*3];
64
        const int z3=  block[i + block_stride*1]     + (block[i + block_stride*3]>>1);
65

    
66
        block[i + block_stride*0]= z0 + z3;
67
        block[i + block_stride*1]= z1 + z2;
68
        block[i + block_stride*2]= z1 - z2;
69
        block[i + block_stride*3]= z0 - z3;
70
    }
71

    
72
    for(i=0; i<4; i++){
73
        const int z0=  block[0 + block_stride*i]     +  block[2 + block_stride*i];
74
        const int z1=  block[0 + block_stride*i]     -  block[2 + block_stride*i];
75
        const int z2= (block[1 + block_stride*i]>>1) -  block[3 + block_stride*i];
76
        const int z3=  block[1 + block_stride*i]     + (block[3 + block_stride*i]>>1);
77

    
78
        dst[i + 0*stride]= CLIP(add*dst[i + 0*stride] + ((z0 + z3) >> shift));
79
        dst[i + 1*stride]= CLIP(add*dst[i + 1*stride] + ((z1 + z2) >> shift));
80
        dst[i + 2*stride]= CLIP(add*dst[i + 2*stride] + ((z1 - z2) >> shift));
81
        dst[i + 3*stride]= CLIP(add*dst[i + 3*stride] + ((z0 - z3) >> shift));
82
    }
83
}
84

    
85
void FUNCC(ff_h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride){
86
    FUNCC(idct_internal)(dst, block, stride, 4, 6, 1);
87
}
88

    
89
void FUNCC(ff_h264_lowres_idct_add)(uint8_t *dst, int stride, DCTELEM *block){
90
    FUNCC(idct_internal)(dst, block, stride, 8, 3, 1);
91
}
92

    
93
void FUNCC(ff_h264_lowres_idct_put)(uint8_t *dst, int stride, DCTELEM *block){
94
    FUNCC(idct_internal)(dst, block, stride, 8, 3, 0);
95
}
96

    
97
void FUNCC(ff_h264_idct8_add)(uint8_t *p_dst, DCTELEM *p_block, int stride){
98
    int i;
99
    INIT_CLIP
100
    pixel *dst = (pixel*)p_dst;
101
    dctcoef *block = (dctcoef*)p_block;
102
    stride >>= sizeof(pixel)-1;
103

    
104
    block[0] += 32;
105

    
106
    for( i = 0; i < 8; i++ )
107
    {
108
        const int a0 =  block[i+0*8] + block[i+4*8];
109
        const int a2 =  block[i+0*8] - block[i+4*8];
110
        const int a4 = (block[i+2*8]>>1) - block[i+6*8];
111
        const int a6 = (block[i+6*8]>>1) + block[i+2*8];
112

    
113
        const int b0 = a0 + a6;
114
        const int b2 = a2 + a4;
115
        const int b4 = a2 - a4;
116
        const int b6 = a0 - a6;
117

    
118
        const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
119
        const int a3 =  block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
120
        const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
121
        const int a7 =  block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
122

    
123
        const int b1 = (a7>>2) + a1;
124
        const int b3 =  a3 + (a5>>2);
125
        const int b5 = (a3>>2) - a5;
126
        const int b7 =  a7 - (a1>>2);
127

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

    
144
        const int b0 = a0 + a6;
145
        const int b2 = a2 + a4;
146
        const int b4 = a2 - a4;
147
        const int b6 = a0 - a6;
148

    
149
        const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
150
        const int a3 =  block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
151
        const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
152
        const int a7 =  block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
153

    
154
        const int b1 = (a7>>2) + a1;
155
        const int b3 =  a3 + (a5>>2);
156
        const int b5 = (a3>>2) - a5;
157
        const int b7 =  a7 - (a1>>2);
158

    
159
        dst[i + 0*stride] = CLIP( dst[i + 0*stride] + ((b0 + b7) >> 6) );
160
        dst[i + 1*stride] = CLIP( dst[i + 1*stride] + ((b2 + b5) >> 6) );
161
        dst[i + 2*stride] = CLIP( dst[i + 2*stride] + ((b4 + b3) >> 6) );
162
        dst[i + 3*stride] = CLIP( dst[i + 3*stride] + ((b6 + b1) >> 6) );
163
        dst[i + 4*stride] = CLIP( dst[i + 4*stride] + ((b6 - b1) >> 6) );
164
        dst[i + 5*stride] = CLIP( dst[i + 5*stride] + ((b4 - b3) >> 6) );
165
        dst[i + 6*stride] = CLIP( dst[i + 6*stride] + ((b2 - b5) >> 6) );
166
        dst[i + 7*stride] = CLIP( dst[i + 7*stride] + ((b0 - b7) >> 6) );
167
    }
168
}
169

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

    
185
void FUNCC(ff_h264_idct8_dc_add)(uint8_t *p_dst, DCTELEM *block, int stride){
186
    int i, j;
187
    int dc = (((dctcoef*)block)[0] + 32) >> 6;
188
    INIT_CLIP
189
    pixel *dst = (pixel*)p_dst;
190
    stride >>= sizeof(pixel)-1;
191
    for( j = 0; j < 8; j++ )
192
    {
193
        for( i = 0; i < 8; i++ )
194
            dst[i] = CLIP( dst[i] + dc );
195
        dst += stride;
196
    }
197
}
198

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

    
210
void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
211
    int i;
212
    for(i=0; i<16; i++){
213
        if(nnzc[ scan8[i] ])             FUNCC(idct_internal      )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
214
        else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
215
    }
216
}
217

    
218
void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
219
    int i;
220
    for(i=0; i<16; i+=4){
221
        int nnz = nnzc[ scan8[i] ];
222
        if(nnz){
223
            if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
224
            else                                  FUNCC(ff_h264_idct8_add   )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
225
        }
226
    }
227
}
228

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

    
250
    for(i=0; i<4; i++){
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];
255

    
256
        temp[4*i+0]= z0+z3;
257
        temp[4*i+1]= z0-z3;
258
        temp[4*i+2]= z1-z2;
259
        temp[4*i+3]= z1+z2;
260
    }
261

    
262
    for(i=0; i<4; i++){
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];
268

    
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));
273
    }
274
#undef stride
275
}
276

    
277
void FUNCC(ff_h264_chroma_dc_dequant_idct)(DCTELEM *p_block, int qmul){
278
    const int stride= 16*2;
279
    const int xStride= 16;
280
    int a,b,c,d,e;
281
    dctcoef *block = (dctcoef*)p_block;
282

    
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];
287

    
288
    e= a-b;
289
    a= a+b;
290
    b= c-d;
291
    c= c+d;
292

    
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;
297
}