Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264idct.c @ e39e3aba

History | View | Annotate | Download (9.68 KB)

1
/*
2
 * H.264 IDCT
3
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This file is part of Libav.
6
 *
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.
11
 *
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.
16
 *
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
20
 */
21

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

    
28
#include "dsputil.h"
29

    
30
static av_always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){
31
    int i;
32
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
33

    
34
    block[0] += 1<<(shift-1);
35

    
36
    for(i=0; i<4; i++){
37
        const int z0=  block[i + block_stride*0]     +  block[i + block_stride*2];
38
        const int z1=  block[i + block_stride*0]     -  block[i + block_stride*2];
39
        const int z2= (block[i + block_stride*1]>>1) -  block[i + block_stride*3];
40
        const int z3=  block[i + block_stride*1]     + (block[i + block_stride*3]>>1);
41

    
42
        block[i + block_stride*0]= z0 + z3;
43
        block[i + block_stride*1]= z1 + z2;
44
        block[i + block_stride*2]= z1 - z2;
45
        block[i + block_stride*3]= z0 - z3;
46
    }
47

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

    
54
        dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ];
55
        dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ];
56
        dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ];
57
        dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ];
58
    }
59
}
60

    
61
void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){
62
    idct_internal(dst, block, stride, 4, 6, 1);
63
}
64

    
65
void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){
66
    idct_internal(dst, block, stride, 8, 3, 1);
67
}
68

    
69
void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){
70
    idct_internal(dst, block, stride, 8, 3, 0);
71
}
72

    
73
void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride){
74
    int i;
75
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
76

    
77
    block[0] += 32;
78

    
79
    for( i = 0; i < 8; i++ )
80
    {
81
        const int a0 =  block[i+0*8] + block[i+4*8];
82
        const int a2 =  block[i+0*8] - block[i+4*8];
83
        const int a4 = (block[i+2*8]>>1) - block[i+6*8];
84
        const int a6 = (block[i+6*8]>>1) + block[i+2*8];
85

    
86
        const int b0 = a0 + a6;
87
        const int b2 = a2 + a4;
88
        const int b4 = a2 - a4;
89
        const int b6 = a0 - a6;
90

    
91
        const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
92
        const int a3 =  block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
93
        const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
94
        const int a7 =  block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
95

    
96
        const int b1 = (a7>>2) + a1;
97
        const int b3 =  a3 + (a5>>2);
98
        const int b5 = (a3>>2) - a5;
99
        const int b7 =  a7 - (a1>>2);
100

    
101
        block[i+0*8] = b0 + b7;
102
        block[i+7*8] = b0 - b7;
103
        block[i+1*8] = b2 + b5;
104
        block[i+6*8] = b2 - b5;
105
        block[i+2*8] = b4 + b3;
106
        block[i+5*8] = b4 - b3;
107
        block[i+3*8] = b6 + b1;
108
        block[i+4*8] = b6 - b1;
109
    }
110
    for( i = 0; i < 8; i++ )
111
    {
112
        const int a0 =  block[0+i*8] + block[4+i*8];
113
        const int a2 =  block[0+i*8] - block[4+i*8];
114
        const int a4 = (block[2+i*8]>>1) - block[6+i*8];
115
        const int a6 = (block[6+i*8]>>1) + block[2+i*8];
116

    
117
        const int b0 = a0 + a6;
118
        const int b2 = a2 + a4;
119
        const int b4 = a2 - a4;
120
        const int b6 = a0 - a6;
121

    
122
        const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
123
        const int a3 =  block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
124
        const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
125
        const int a7 =  block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
126

    
127
        const int b1 = (a7>>2) + a1;
128
        const int b3 =  a3 + (a5>>2);
129
        const int b5 = (a3>>2) - a5;
130
        const int b7 =  a7 - (a1>>2);
131

    
132
        dst[i + 0*stride] = cm[ dst[i + 0*stride] + ((b0 + b7) >> 6) ];
133
        dst[i + 1*stride] = cm[ dst[i + 1*stride] + ((b2 + b5) >> 6) ];
134
        dst[i + 2*stride] = cm[ dst[i + 2*stride] + ((b4 + b3) >> 6) ];
135
        dst[i + 3*stride] = cm[ dst[i + 3*stride] + ((b6 + b1) >> 6) ];
136
        dst[i + 4*stride] = cm[ dst[i + 4*stride] + ((b6 - b1) >> 6) ];
137
        dst[i + 5*stride] = cm[ dst[i + 5*stride] + ((b4 - b3) >> 6) ];
138
        dst[i + 6*stride] = cm[ dst[i + 6*stride] + ((b2 - b5) >> 6) ];
139
        dst[i + 7*stride] = cm[ dst[i + 7*stride] + ((b0 - b7) >> 6) ];
140
    }
141
}
142

    
143
// assumes all AC coefs are 0
144
void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
145
    int i, j;
146
    int dc = (block[0] + 32) >> 6;
147
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + dc;
148
    for( j = 0; j < 4; j++ )
149
    {
150
        for( i = 0; i < 4; i++ )
151
            dst[i] = cm[ dst[i] ];
152
        dst += stride;
153
    }
154
}
155

    
156
void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
157
    int i, j;
158
    int dc = (block[0] + 32) >> 6;
159
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + dc;
160
    for( j = 0; j < 8; j++ )
161
    {
162
        for( i = 0; i < 8; i++ )
163
            dst[i] = cm[ dst[i] ];
164
        dst += stride;
165
    }
166
}
167

    
168
//FIXME this table is a duplicate from h264data.h, and will be removed once the tables from, h264 have been split
169
static const uint8_t scan8[16 + 2*4]={
170
 4+1*8, 5+1*8, 4+2*8, 5+2*8,
171
 6+1*8, 7+1*8, 6+2*8, 7+2*8,
172
 4+3*8, 5+3*8, 4+4*8, 5+4*8,
173
 6+3*8, 7+3*8, 6+4*8, 7+4*8,
174
 1+1*8, 2+1*8,
175
 1+2*8, 2+2*8,
176
 1+4*8, 2+4*8,
177
 1+5*8, 2+5*8,
178
};
179

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

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

    
199
void ff_h264_idct8_add4_c(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+=4){
202
        int nnz = nnzc[ scan8[i] ];
203
        if(nnz){
204
            if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_c(dst + block_offset[i], block + i*16, stride);
205
            else                      ff_h264_idct8_add_c   (dst + block_offset[i], block + i*16, stride);
206
        }
207
    }
208
}
209

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

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

    
235
        temp[4*i+0]= z0+z3;
236
        temp[4*i+1]= z0-z3;
237
        temp[4*i+2]= z1-z2;
238
        temp[4*i+3]= z1+z2;
239
    }
240

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

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

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

    
261
    a= block[stride*0 + xStride*0];
262
    b= block[stride*0 + xStride*1];
263
    c= block[stride*1 + xStride*0];
264
    d= block[stride*1 + xStride*1];
265

    
266
    e= a-b;
267
    a= a+b;
268
    b= c-d;
269
    c= c+d;
270

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