Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264idct_internal.h @ 7bc8032b

History | View | Annotate | Download (9.77 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
static av_always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){
46
    int i;
47
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
48

    
49
    block[0] += 1<<(shift-1);
50

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

    
57
        block[i + block_stride*0]= z0 + z3;
58
        block[i + block_stride*1]= z1 + z2;
59
        block[i + block_stride*2]= z1 - z2;
60
        block[i + block_stride*3]= z0 - z3;
61
    }
62

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

    
69
        dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ];
70
        dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ];
71
        dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ];
72
        dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ];
73
    }
74
}
75

    
76
void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){
77
    idct_internal(dst, block, stride, 4, 6, 1);
78
}
79

    
80
void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){
81
    idct_internal(dst, block, stride, 8, 3, 1);
82
}
83

    
84
void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){
85
    idct_internal(dst, block, stride, 8, 3, 0);
86
}
87

    
88
void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride){
89
    int i;
90
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
91

    
92
    block[0] += 32;
93

    
94
    for( i = 0; i < 8; i++ )
95
    {
96
        const int a0 =  block[i+0*8] + block[i+4*8];
97
        const int a2 =  block[i+0*8] - block[i+4*8];
98
        const int a4 = (block[i+2*8]>>1) - block[i+6*8];
99
        const int a6 = (block[i+6*8]>>1) + block[i+2*8];
100

    
101
        const int b0 = a0 + a6;
102
        const int b2 = a2 + a4;
103
        const int b4 = a2 - a4;
104
        const int b6 = a0 - a6;
105

    
106
        const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
107
        const int a3 =  block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
108
        const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
109
        const int a7 =  block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
110

    
111
        const int b1 = (a7>>2) + a1;
112
        const int b3 =  a3 + (a5>>2);
113
        const int b5 = (a3>>2) - a5;
114
        const int b7 =  a7 - (a1>>2);
115

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

    
132
        const int b0 = a0 + a6;
133
        const int b2 = a2 + a4;
134
        const int b4 = a2 - a4;
135
        const int b6 = a0 - a6;
136

    
137
        const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
138
        const int a3 =  block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
139
        const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
140
        const int a7 =  block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
141

    
142
        const int b1 = (a7>>2) + a1;
143
        const int b3 =  a3 + (a5>>2);
144
        const int b5 = (a3>>2) - a5;
145
        const int b7 =  a7 - (a1>>2);
146

    
147
        dst[i + 0*stride] = cm[ dst[i + 0*stride] + ((b0 + b7) >> 6) ];
148
        dst[i + 1*stride] = cm[ dst[i + 1*stride] + ((b2 + b5) >> 6) ];
149
        dst[i + 2*stride] = cm[ dst[i + 2*stride] + ((b4 + b3) >> 6) ];
150
        dst[i + 3*stride] = cm[ dst[i + 3*stride] + ((b6 + b1) >> 6) ];
151
        dst[i + 4*stride] = cm[ dst[i + 4*stride] + ((b6 - b1) >> 6) ];
152
        dst[i + 5*stride] = cm[ dst[i + 5*stride] + ((b4 - b3) >> 6) ];
153
        dst[i + 6*stride] = cm[ dst[i + 6*stride] + ((b2 - b5) >> 6) ];
154
        dst[i + 7*stride] = cm[ dst[i + 7*stride] + ((b0 - b7) >> 6) ];
155
    }
156
}
157

    
158
// assumes all AC coefs are 0
159
void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
160
    int i, j;
161
    int dc = (block[0] + 32) >> 6;
162
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + dc;
163
    for( j = 0; j < 4; j++ )
164
    {
165
        for( i = 0; i < 4; i++ )
166
            dst[i] = cm[ dst[i] ];
167
        dst += stride;
168
    }
169
}
170

    
171
void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
172
    int i, j;
173
    int dc = (block[0] + 32) >> 6;
174
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + dc;
175
    for( j = 0; j < 8; j++ )
176
    {
177
        for( i = 0; i < 8; i++ )
178
            dst[i] = cm[ dst[i] ];
179
        dst += stride;
180
    }
181
}
182

    
183
void ff_h264_idct_add16_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
184
    int i;
185
    for(i=0; i<16; i++){
186
        int nnz = nnzc[ scan8[i] ];
187
        if(nnz){
188
            if(nnz==1 && block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride);
189
            else                      idct_internal        (dst + block_offset[i], block + i*16, stride, 4, 6, 1);
190
        }
191
    }
192
}
193

    
194
void ff_h264_idct_add16intra_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
195
    int i;
196
    for(i=0; i<16; i++){
197
        if(nnzc[ scan8[i] ]) idct_internal        (dst + block_offset[i], block + i*16, stride, 4, 6, 1);
198
        else if(block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride);
199
    }
200
}
201

    
202
void ff_h264_idct8_add4_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
203
    int i;
204
    for(i=0; i<16; i+=4){
205
        int nnz = nnzc[ scan8[i] ];
206
        if(nnz){
207
            if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_c(dst + block_offset[i], block + i*16, stride);
208
            else                      ff_h264_idct8_add_c   (dst + block_offset[i], block + i*16, stride);
209
        }
210
    }
211
}
212

    
213
void ff_h264_idct_add8_c(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
214
    int i;
215
    for(i=16; i<16+8; i++){
216
        if(nnzc[ scan8[i] ])
217
            ff_h264_idct_add_c   (dest[(i&4)>>2] + block_offset[i], block + i*16, stride);
218
        else if(block[i*16])
219
            ff_h264_idct_dc_add_c(dest[(i&4)>>2] + block_offset[i], block + i*16, stride);
220
    }
221
}
222
/**
223
 * IDCT transforms the 16 dc values and dequantizes them.
224
 * @param qp quantization parameter
225
 */
226
void ff_h264_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qmul){
227
#define stride 16
228
    int i;
229
    int temp[16];
230
    static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
231

    
232
    for(i=0; i<4; i++){
233
        const int z0= input[4*i+0] + input[4*i+1];
234
        const int z1= input[4*i+0] - input[4*i+1];
235
        const int z2= input[4*i+2] - input[4*i+3];
236
        const int z3= input[4*i+2] + input[4*i+3];
237

    
238
        temp[4*i+0]= z0+z3;
239
        temp[4*i+1]= z0-z3;
240
        temp[4*i+2]= z1-z2;
241
        temp[4*i+3]= z1+z2;
242
    }
243

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

    
251
        output[stride* 0+offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
252
        output[stride* 1+offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
253
        output[stride* 4+offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
254
        output[stride* 5+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
255
    }
256
#undef stride
257
}
258

    
259
void ff_h264_chroma_dc_dequant_idct_c(DCTELEM *block, int qmul){
260
    const int stride= 16*2;
261
    const int xStride= 16;
262
    int a,b,c,d,e;
263

    
264
    a= block[stride*0 + xStride*0];
265
    b= block[stride*0 + xStride*1];
266
    c= block[stride*1 + xStride*0];
267
    d= block[stride*1 + xStride*1];
268

    
269
    e= a-b;
270
    a= a+b;
271
    b= c-d;
272
    c= c+d;
273

    
274
    block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
275
    block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
276
    block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
277
    block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
278
}