]> git.sesse.net Git - ffmpeg/blob - libavcodec/simple_idct_template.c
Merge commit '17d57848fc14e82f76a65ffb25c90f2f011dc4a0'
[ffmpeg] / libavcodec / simple_idct_template.c
1 /*
2  * Simple IDCT
3  *
4  * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * simpleidct in C.
26  */
27
28 /*
29   based upon some outcommented c code from mpeg2dec (idct_mmx.c
30   written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
31  */
32
33 #include "bit_depth_template.c"
34
35 #undef W1
36 #undef W2
37 #undef W3
38 #undef W4
39 #undef W5
40 #undef W6
41 #undef W7
42 #undef ROW_SHIFT
43 #undef COL_SHIFT
44 #undef DC_SHIFT
45 #undef MUL
46 #undef MAC
47
48 #if BIT_DEPTH == 8
49
50 #define W1  22725  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
51 #define W2  21407  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
52 #define W3  19266  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
53 #define W4  16383  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
54 #define W5  12873  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
55 #define W6  8867   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
56 #define W7  4520   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
57
58 #define ROW_SHIFT 11
59 #define COL_SHIFT 20
60 #define DC_SHIFT 3
61
62 #define MUL(a, b)    MUL16(a, b)
63 #define MAC(a, b, c) MAC16(a, b, c)
64
65 #elif BIT_DEPTH == 10 || BIT_DEPTH == 12
66
67 #define W1 90901
68 #define W2 85627
69 #define W3 77062
70 #define W4 65535
71 #define W5 51491
72 #define W6 35468
73 #define W7 18081
74
75 #if BIT_DEPTH == 10
76 #define ROW_SHIFT 15
77 #define COL_SHIFT 20
78 #define DC_SHIFT 1
79 #else
80 #define ROW_SHIFT 17
81 #define COL_SHIFT 18
82 #define DC_SHIFT -1
83 #endif
84
85 #define MUL(a, b)    ((a) * (b))
86 #define MAC(a, b, c) ((a) += (b) * (c))
87
88 #else
89
90 #error "Unsupported bitdepth"
91
92 #endif
93
94 static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)
95 {
96     int a0, a1, a2, a3, b0, b1, b2, b3;
97
98 #if HAVE_FAST_64BIT
99 #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
100     if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
101         uint64_t temp;
102         if (DC_SHIFT - extra_shift > 0) {
103             temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
104         } else {
105             temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
106         }
107         temp += temp << 16;
108         temp += temp << 32;
109         ((uint64_t *)row)[0] = temp;
110         ((uint64_t *)row)[1] = temp;
111         return;
112     }
113 #else
114     if (!(((uint32_t*)row)[1] |
115           ((uint32_t*)row)[2] |
116           ((uint32_t*)row)[3] |
117           row[1])) {
118         uint32_t temp;
119         if (DC_SHIFT - extra_shift > 0) {
120             temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
121         } else {
122             temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
123         }
124         temp += temp << 16;
125         ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
126             ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
127         return;
128     }
129 #endif
130
131     a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
132     a1 = a0;
133     a2 = a0;
134     a3 = a0;
135
136     a0 += W2 * row[2];
137     a1 += W6 * row[2];
138     a2 -= W6 * row[2];
139     a3 -= W2 * row[2];
140
141     b0 = MUL(W1, row[1]);
142     MAC(b0, W3, row[3]);
143     b1 = MUL(W3, row[1]);
144     MAC(b1, -W7, row[3]);
145     b2 = MUL(W5, row[1]);
146     MAC(b2, -W1, row[3]);
147     b3 = MUL(W7, row[1]);
148     MAC(b3, -W5, row[3]);
149
150     if (AV_RN64A(row + 4)) {
151         a0 +=   W4*row[4] + W6*row[6];
152         a1 += - W4*row[4] - W2*row[6];
153         a2 += - W4*row[4] + W2*row[6];
154         a3 +=   W4*row[4] - W6*row[6];
155
156         MAC(b0,  W5, row[5]);
157         MAC(b0,  W7, row[7]);
158
159         MAC(b1, -W1, row[5]);
160         MAC(b1, -W5, row[7]);
161
162         MAC(b2,  W7, row[5]);
163         MAC(b2,  W3, row[7]);
164
165         MAC(b3,  W3, row[5]);
166         MAC(b3, -W1, row[7]);
167     }
168
169     row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
170     row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
171     row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
172     row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
173     row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
174     row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
175     row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
176     row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
177 }
178
179 #define IDCT_COLS do {                                  \
180         a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
181         a1 = a0;                                        \
182         a2 = a0;                                        \
183         a3 = a0;                                        \
184                                                         \
185         a0 +=  W2*col[8*2];                             \
186         a1 +=  W6*col[8*2];                             \
187         a2 += -W6*col[8*2];                             \
188         a3 += -W2*col[8*2];                             \
189                                                         \
190         b0 = MUL(W1, col[8*1]);                         \
191         b1 = MUL(W3, col[8*1]);                         \
192         b2 = MUL(W5, col[8*1]);                         \
193         b3 = MUL(W7, col[8*1]);                         \
194                                                         \
195         MAC(b0,  W3, col[8*3]);                         \
196         MAC(b1, -W7, col[8*3]);                         \
197         MAC(b2, -W1, col[8*3]);                         \
198         MAC(b3, -W5, col[8*3]);                         \
199                                                         \
200         if (col[8*4]) {                                 \
201             a0 +=  W4*col[8*4];                         \
202             a1 += -W4*col[8*4];                         \
203             a2 += -W4*col[8*4];                         \
204             a3 +=  W4*col[8*4];                         \
205         }                                               \
206                                                         \
207         if (col[8*5]) {                                 \
208             MAC(b0,  W5, col[8*5]);                     \
209             MAC(b1, -W1, col[8*5]);                     \
210             MAC(b2,  W7, col[8*5]);                     \
211             MAC(b3,  W3, col[8*5]);                     \
212         }                                               \
213                                                         \
214         if (col[8*6]) {                                 \
215             a0 +=  W6*col[8*6];                         \
216             a1 += -W2*col[8*6];                         \
217             a2 +=  W2*col[8*6];                         \
218             a3 += -W6*col[8*6];                         \
219         }                                               \
220                                                         \
221         if (col[8*7]) {                                 \
222             MAC(b0,  W7, col[8*7]);                     \
223             MAC(b1, -W5, col[8*7]);                     \
224             MAC(b2,  W3, col[8*7]);                     \
225             MAC(b3, -W1, col[8*7]);                     \
226         }                                               \
227     } while (0)
228
229 static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
230                                           int16_t *col)
231 {
232     int a0, a1, a2, a3, b0, b1, b2, b3;
233
234     IDCT_COLS;
235
236     dest[0] = av_clip_pixel((a0 + b0) >> COL_SHIFT);
237     dest += line_size;
238     dest[0] = av_clip_pixel((a1 + b1) >> COL_SHIFT);
239     dest += line_size;
240     dest[0] = av_clip_pixel((a2 + b2) >> COL_SHIFT);
241     dest += line_size;
242     dest[0] = av_clip_pixel((a3 + b3) >> COL_SHIFT);
243     dest += line_size;
244     dest[0] = av_clip_pixel((a3 - b3) >> COL_SHIFT);
245     dest += line_size;
246     dest[0] = av_clip_pixel((a2 - b2) >> COL_SHIFT);
247     dest += line_size;
248     dest[0] = av_clip_pixel((a1 - b1) >> COL_SHIFT);
249     dest += line_size;
250     dest[0] = av_clip_pixel((a0 - b0) >> COL_SHIFT);
251 }
252
253 static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
254                                           int16_t *col)
255 {
256     int a0, a1, a2, a3, b0, b1, b2, b3;
257
258     IDCT_COLS;
259
260     dest[0] = av_clip_pixel(dest[0] + ((a0 + b0) >> COL_SHIFT));
261     dest += line_size;
262     dest[0] = av_clip_pixel(dest[0] + ((a1 + b1) >> COL_SHIFT));
263     dest += line_size;
264     dest[0] = av_clip_pixel(dest[0] + ((a2 + b2) >> COL_SHIFT));
265     dest += line_size;
266     dest[0] = av_clip_pixel(dest[0] + ((a3 + b3) >> COL_SHIFT));
267     dest += line_size;
268     dest[0] = av_clip_pixel(dest[0] + ((a3 - b3) >> COL_SHIFT));
269     dest += line_size;
270     dest[0] = av_clip_pixel(dest[0] + ((a2 - b2) >> COL_SHIFT));
271     dest += line_size;
272     dest[0] = av_clip_pixel(dest[0] + ((a1 - b1) >> COL_SHIFT));
273     dest += line_size;
274     dest[0] = av_clip_pixel(dest[0] + ((a0 - b0) >> COL_SHIFT));
275 }
276
277 static inline void FUNC(idctSparseCol)(int16_t *col)
278 {
279     int a0, a1, a2, a3, b0, b1, b2, b3;
280
281     IDCT_COLS;
282
283     col[0 ] = ((a0 + b0) >> COL_SHIFT);
284     col[8 ] = ((a1 + b1) >> COL_SHIFT);
285     col[16] = ((a2 + b2) >> COL_SHIFT);
286     col[24] = ((a3 + b3) >> COL_SHIFT);
287     col[32] = ((a3 - b3) >> COL_SHIFT);
288     col[40] = ((a2 - b2) >> COL_SHIFT);
289     col[48] = ((a1 - b1) >> COL_SHIFT);
290     col[56] = ((a0 - b0) >> COL_SHIFT);
291 }
292
293 void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, int16_t *block)
294 {
295     pixel *dest = (pixel *)dest_;
296     int i;
297
298     line_size /= sizeof(pixel);
299
300     for (i = 0; i < 8; i++)
301         FUNC(idctRowCondDC)(block + i*8, 0);
302
303     for (i = 0; i < 8; i++)
304         FUNC(idctSparseColPut)(dest + i, line_size, block + i);
305 }
306
307 void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, int16_t *block)
308 {
309     pixel *dest = (pixel *)dest_;
310     int i;
311
312     line_size /= sizeof(pixel);
313
314     for (i = 0; i < 8; i++)
315         FUNC(idctRowCondDC)(block + i*8, 0);
316
317     for (i = 0; i < 8; i++)
318         FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
319 }
320
321 void FUNC(ff_simple_idct)(int16_t *block)
322 {
323     int i;
324
325     for (i = 0; i < 8; i++)
326         FUNC(idctRowCondDC)(block + i*8, 0);
327
328     for (i = 0; i < 8; i++)
329         FUNC(idctSparseCol)(block + i);
330 }