]> git.sesse.net Git - ffmpeg/blob - libavcodec/simple_idct_template.c
Merge commit 'ce589940c2cac936891e8bba275580d6efc41e8b'
[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 "simple_idct.h"
34
35 #include "bit_depth_template.c"
36
37 #undef W1
38 #undef W2
39 #undef W3
40 #undef W4
41 #undef W5
42 #undef W6
43 #undef W7
44 #undef ROW_SHIFT
45 #undef COL_SHIFT
46 #undef DC_SHIFT
47 #undef MUL
48 #undef MAC
49
50 #if BIT_DEPTH == 8
51
52 #define W1  22725  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
53 #define W2  21407  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
54 #define W3  19266  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
55 #define W4  16383  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
56 #define W5  12873  //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
57 #define W6  8867   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
58 #define W7  4520   //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
59
60 #define ROW_SHIFT 11
61 #define COL_SHIFT 20
62 #define DC_SHIFT 3
63
64 #define MUL(a, b)    MUL16(a, b)
65 #define MAC(a, b, c) MAC16(a, b, c)
66
67 #elif BIT_DEPTH == 10 || BIT_DEPTH == 12
68
69 # if BIT_DEPTH == 10
70 #define W1 22725 // 90901
71 #define W2 21407 //  85627
72 #define W3 19265 //  77062
73 #define W4 16384 //  65535
74 #define W5 12873 //  51491
75 #define W6  8867 //  35468
76 #define W7  4520 //  18081
77
78 #   ifdef EXTRA_SHIFT
79 #define ROW_SHIFT 13
80 #define COL_SHIFT 18
81 #define DC_SHIFT  1
82 #   else
83 #define ROW_SHIFT 12
84 #define COL_SHIFT 19
85 #define DC_SHIFT  2
86 #   endif
87
88 # else
89 #define W1 45451
90 #define W2 42813
91 #define W3 38531
92 #define W4 32767
93 #define W5 25746
94 #define W6 17734
95 #define W7 9041
96
97 #define ROW_SHIFT 16
98 #define COL_SHIFT 17
99 #define DC_SHIFT -1
100 # endif
101
102 #define MUL(a, b)    ((a) * (b))
103 #define MAC(a, b, c) ((a) += (b) * (c))
104
105 #else
106
107 #error "Unsupported bitdepth"
108
109 #endif
110
111 #ifdef EXTRA_SHIFT
112 static inline void FUNC(idctRowCondDC_extrashift)(int16_t *row, int extra_shift)
113 #else
114 static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)
115 #endif
116 {
117     int a0, a1, a2, a3, b0, b1, b2, b3;
118
119 #if HAVE_FAST_64BIT
120 #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
121     if (((AV_RN64A(row) & ~ROW0_MASK) | AV_RN64A(row+4)) == 0) {
122         uint64_t temp;
123         if (DC_SHIFT - extra_shift >= 0) {
124             temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
125         } else {
126             temp = ((row[0] + (1<<(extra_shift - DC_SHIFT-1))) >> (extra_shift - DC_SHIFT)) & 0xffff;
127         }
128         temp += temp * (1 << 16);
129         temp += temp * ((uint64_t) 1 << 32);
130         AV_WN64A(row, temp);
131         AV_WN64A(row+4, temp);
132         return;
133     }
134 #else
135     if (!(AV_RN32A(row+2) |
136           AV_RN32A(row+4) |
137           AV_RN32A(row+6) |
138           row[1])) {
139         uint32_t temp;
140         if (DC_SHIFT - extra_shift >= 0) {
141             temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
142         } else {
143             temp = ((row[0] + (1<<(extra_shift - DC_SHIFT-1))) >> (extra_shift - DC_SHIFT)) & 0xffff;
144         }
145         temp += temp * (1 << 16);
146         AV_WN32A(row, temp);
147         AV_WN32A(row+2, temp);
148         AV_WN32A(row+4, temp);
149         AV_WN32A(row+6, temp);
150         return;
151     }
152 #endif
153
154     a0 = (W4 * row[0]) + (1 << (ROW_SHIFT + extra_shift - 1));
155     a1 = a0;
156     a2 = a0;
157     a3 = a0;
158
159     a0 += W2 * row[2];
160     a1 += W6 * row[2];
161     a2 -= W6 * row[2];
162     a3 -= W2 * row[2];
163
164     b0 = MUL(W1, row[1]);
165     MAC(b0, W3, row[3]);
166     b1 = MUL(W3, row[1]);
167     MAC(b1, -W7, row[3]);
168     b2 = MUL(W5, row[1]);
169     MAC(b2, -W1, row[3]);
170     b3 = MUL(W7, row[1]);
171     MAC(b3, -W5, row[3]);
172
173     if (AV_RN64A(row + 4)) {
174         a0 +=   W4*row[4] + W6*row[6];
175         a1 += - W4*row[4] - W2*row[6];
176         a2 += - W4*row[4] + W2*row[6];
177         a3 +=   W4*row[4] - W6*row[6];
178
179         MAC(b0,  W5, row[5]);
180         MAC(b0,  W7, row[7]);
181
182         MAC(b1, -W1, row[5]);
183         MAC(b1, -W5, row[7]);
184
185         MAC(b2,  W7, row[5]);
186         MAC(b2,  W3, row[7]);
187
188         MAC(b3,  W3, row[5]);
189         MAC(b3, -W1, row[7]);
190     }
191
192     row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
193     row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
194     row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
195     row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
196     row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
197     row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
198     row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
199     row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
200 }
201
202 #define IDCT_COLS do {                                  \
203         a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
204         a1 = a0;                                        \
205         a2 = a0;                                        \
206         a3 = a0;                                        \
207                                                         \
208         a0 +=  W2*col[8*2];                             \
209         a1 +=  W6*col[8*2];                             \
210         a2 += -W6*col[8*2];                             \
211         a3 += -W2*col[8*2];                             \
212                                                         \
213         b0 = MUL(W1, col[8*1]);                         \
214         b1 = MUL(W3, col[8*1]);                         \
215         b2 = MUL(W5, col[8*1]);                         \
216         b3 = MUL(W7, col[8*1]);                         \
217                                                         \
218         MAC(b0,  W3, col[8*3]);                         \
219         MAC(b1, -W7, col[8*3]);                         \
220         MAC(b2, -W1, col[8*3]);                         \
221         MAC(b3, -W5, col[8*3]);                         \
222                                                         \
223         if (col[8*4]) {                                 \
224             a0 +=  W4*col[8*4];                         \
225             a1 += -W4*col[8*4];                         \
226             a2 += -W4*col[8*4];                         \
227             a3 +=  W4*col[8*4];                         \
228         }                                               \
229                                                         \
230         if (col[8*5]) {                                 \
231             MAC(b0,  W5, col[8*5]);                     \
232             MAC(b1, -W1, col[8*5]);                     \
233             MAC(b2,  W7, col[8*5]);                     \
234             MAC(b3,  W3, col[8*5]);                     \
235         }                                               \
236                                                         \
237         if (col[8*6]) {                                 \
238             a0 +=  W6*col[8*6];                         \
239             a1 += -W2*col[8*6];                         \
240             a2 +=  W2*col[8*6];                         \
241             a3 += -W6*col[8*6];                         \
242         }                                               \
243                                                         \
244         if (col[8*7]) {                                 \
245             MAC(b0,  W7, col[8*7]);                     \
246             MAC(b1, -W5, col[8*7]);                     \
247             MAC(b2,  W3, col[8*7]);                     \
248             MAC(b3, -W1, col[8*7]);                     \
249         }                                               \
250     } while (0)
251
252 #ifdef EXTRA_SHIFT
253 static inline void FUNC(idctSparseCol_extrashift)(int16_t *col)
254 #else
255 static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
256                                           int16_t *col)
257 {
258     int a0, a1, a2, a3, b0, b1, b2, b3;
259
260     IDCT_COLS;
261
262     dest[0] = av_clip_pixel((a0 + b0) >> COL_SHIFT);
263     dest += line_size;
264     dest[0] = av_clip_pixel((a1 + b1) >> COL_SHIFT);
265     dest += line_size;
266     dest[0] = av_clip_pixel((a2 + b2) >> COL_SHIFT);
267     dest += line_size;
268     dest[0] = av_clip_pixel((a3 + b3) >> COL_SHIFT);
269     dest += line_size;
270     dest[0] = av_clip_pixel((a3 - b3) >> COL_SHIFT);
271     dest += line_size;
272     dest[0] = av_clip_pixel((a2 - b2) >> COL_SHIFT);
273     dest += line_size;
274     dest[0] = av_clip_pixel((a1 - b1) >> COL_SHIFT);
275     dest += line_size;
276     dest[0] = av_clip_pixel((a0 - b0) >> COL_SHIFT);
277 }
278
279 static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
280                                           int16_t *col)
281 {
282     int a0, a1, a2, a3, b0, b1, b2, b3;
283
284     IDCT_COLS;
285
286     dest[0] = av_clip_pixel(dest[0] + ((a0 + b0) >> COL_SHIFT));
287     dest += line_size;
288     dest[0] = av_clip_pixel(dest[0] + ((a1 + b1) >> COL_SHIFT));
289     dest += line_size;
290     dest[0] = av_clip_pixel(dest[0] + ((a2 + b2) >> COL_SHIFT));
291     dest += line_size;
292     dest[0] = av_clip_pixel(dest[0] + ((a3 + b3) >> COL_SHIFT));
293     dest += line_size;
294     dest[0] = av_clip_pixel(dest[0] + ((a3 - b3) >> COL_SHIFT));
295     dest += line_size;
296     dest[0] = av_clip_pixel(dest[0] + ((a2 - b2) >> COL_SHIFT));
297     dest += line_size;
298     dest[0] = av_clip_pixel(dest[0] + ((a1 - b1) >> COL_SHIFT));
299     dest += line_size;
300     dest[0] = av_clip_pixel(dest[0] + ((a0 - b0) >> COL_SHIFT));
301 }
302
303 static inline void FUNC(idctSparseCol)(int16_t *col)
304 #endif
305 {
306     int a0, a1, a2, a3, b0, b1, b2, b3;
307
308     IDCT_COLS;
309
310     col[0 ] = ((a0 + b0) >> COL_SHIFT);
311     col[8 ] = ((a1 + b1) >> COL_SHIFT);
312     col[16] = ((a2 + b2) >> COL_SHIFT);
313     col[24] = ((a3 + b3) >> COL_SHIFT);
314     col[32] = ((a3 - b3) >> COL_SHIFT);
315     col[40] = ((a2 - b2) >> COL_SHIFT);
316     col[48] = ((a1 - b1) >> COL_SHIFT);
317     col[56] = ((a0 - b0) >> COL_SHIFT);
318 }
319
320 #ifndef EXTRA_SHIFT
321 void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, int16_t *block)
322 {
323     pixel *dest = (pixel *)dest_;
324     int i;
325
326     line_size /= sizeof(pixel);
327
328     for (i = 0; i < 8; i++)
329         FUNC(idctRowCondDC)(block + i*8, 0);
330
331     for (i = 0; i < 8; i++)
332         FUNC(idctSparseColPut)(dest + i, line_size, block + i);
333 }
334
335 void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, int16_t *block)
336 {
337     pixel *dest = (pixel *)dest_;
338     int i;
339
340     line_size /= sizeof(pixel);
341
342     for (i = 0; i < 8; i++)
343         FUNC(idctRowCondDC)(block + i*8, 0);
344
345     for (i = 0; i < 8; i++)
346         FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
347 }
348
349 void FUNC(ff_simple_idct)(int16_t *block)
350 {
351     int i;
352
353     for (i = 0; i < 8; i++)
354         FUNC(idctRowCondDC)(block + i*8, 0);
355
356     for (i = 0; i < 8; i++)
357         FUNC(idctSparseCol)(block + i);
358 }
359 #endif