dct32.c
Go to the documentation of this file.
1 /*
2  * Template for the Discrete Cosine Transform for 32 samples
3  * Copyright (c) 2001, 2002 Fabrice Bellard
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 #include "dct32.h"
23 #include "mathops.h"
24 
25 #if DCT32_FLOAT
26 # define dct32 ff_dct32_float
27 # define FIXHR(x) ((float)(x))
28 # define MULH3(x, y, s) ((s)*(y)*(x))
29 # define INTFLOAT float
30 #else
31 # define dct32 ff_dct32_fixed
32 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
33 # define MULH3(x, y, s) MULH((s)*(x), y)
34 # define INTFLOAT int
35 #endif
36 
37 
38 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
39 
40 /* cos(i*pi/64) */
41 
42 #define COS0_0 FIXHR(0.50060299823519630134/2)
43 #define COS0_1 FIXHR(0.50547095989754365998/2)
44 #define COS0_2 FIXHR(0.51544730992262454697/2)
45 #define COS0_3 FIXHR(0.53104259108978417447/2)
46 #define COS0_4 FIXHR(0.55310389603444452782/2)
47 #define COS0_5 FIXHR(0.58293496820613387367/2)
48 #define COS0_6 FIXHR(0.62250412303566481615/2)
49 #define COS0_7 FIXHR(0.67480834145500574602/2)
50 #define COS0_8 FIXHR(0.74453627100229844977/2)
51 #define COS0_9 FIXHR(0.83934964541552703873/2)
52 #define COS0_10 FIXHR(0.97256823786196069369/2)
53 #define COS0_11 FIXHR(1.16943993343288495515/4)
54 #define COS0_12 FIXHR(1.48416461631416627724/4)
55 #define COS0_13 FIXHR(2.05778100995341155085/8)
56 #define COS0_14 FIXHR(3.40760841846871878570/8)
57 #define COS0_15 FIXHR(10.19000812354805681150/32)
58 
59 #define COS1_0 FIXHR(0.50241928618815570551/2)
60 #define COS1_1 FIXHR(0.52249861493968888062/2)
61 #define COS1_2 FIXHR(0.56694403481635770368/2)
62 #define COS1_3 FIXHR(0.64682178335999012954/2)
63 #define COS1_4 FIXHR(0.78815462345125022473/2)
64 #define COS1_5 FIXHR(1.06067768599034747134/4)
65 #define COS1_6 FIXHR(1.72244709823833392782/4)
66 #define COS1_7 FIXHR(5.10114861868916385802/16)
67 
68 #define COS2_0 FIXHR(0.50979557910415916894/2)
69 #define COS2_1 FIXHR(0.60134488693504528054/2)
70 #define COS2_2 FIXHR(0.89997622313641570463/2)
71 #define COS2_3 FIXHR(2.56291544774150617881/8)
72 
73 #define COS3_0 FIXHR(0.54119610014619698439/2)
74 #define COS3_1 FIXHR(1.30656296487637652785/4)
75 
76 #define COS4_0 FIXHR(0.70710678118654752439/2)
77 
78 /* butterfly operator */
79 #define BF(a, b, c, s)\
80 {\
81  tmp0 = val##a + val##b;\
82  tmp1 = val##a - val##b;\
83  val##a = tmp0;\
84  val##b = MULH3(tmp1, c, 1<<(s));\
85 }
86 
87 #define BF0(a, b, c, s)\
88 {\
89  tmp0 = tab[a] + tab[b];\
90  tmp1 = tab[a] - tab[b];\
91  val##a = tmp0;\
92  val##b = MULH3(tmp1, c, 1<<(s));\
93 }
94 
95 #define BF1(a, b, c, d)\
96 {\
97  BF(a, b, COS4_0, 1);\
98  BF(c, d,-COS4_0, 1);\
99  val##c += val##d;\
100 }
101 
102 #define BF2(a, b, c, d)\
103 {\
104  BF(a, b, COS4_0, 1);\
105  BF(c, d,-COS4_0, 1);\
106  val##c += val##d;\
107  val##a += val##c;\
108  val##c += val##b;\
109  val##b += val##d;\
110 }
111 
112 #define ADD(a, b) val##a += val##b
113 
114 /* DCT32 without 1/sqrt(2) coef zero scaling. */
115 void dct32(INTFLOAT *out, const INTFLOAT *tab)
116 {
117  INTFLOAT tmp0, tmp1;
118 
119  INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,
120  val8 , val9 , val10, val11, val12, val13, val14, val15,
121  val16, val17, val18, val19, val20, val21, val22, val23,
122  val24, val25, val26, val27, val28, val29, val30, val31;
123 
124  /* pass 1 */
125  BF0( 0, 31, COS0_0 , 1);
126  BF0(15, 16, COS0_15, 5);
127  /* pass 2 */
128  BF( 0, 15, COS1_0 , 1);
129  BF(16, 31,-COS1_0 , 1);
130  /* pass 1 */
131  BF0( 7, 24, COS0_7 , 1);
132  BF0( 8, 23, COS0_8 , 1);
133  /* pass 2 */
134  BF( 7, 8, COS1_7 , 4);
135  BF(23, 24,-COS1_7 , 4);
136  /* pass 3 */
137  BF( 0, 7, COS2_0 , 1);
138  BF( 8, 15,-COS2_0 , 1);
139  BF(16, 23, COS2_0 , 1);
140  BF(24, 31,-COS2_0 , 1);
141  /* pass 1 */
142  BF0( 3, 28, COS0_3 , 1);
143  BF0(12, 19, COS0_12, 2);
144  /* pass 2 */
145  BF( 3, 12, COS1_3 , 1);
146  BF(19, 28,-COS1_3 , 1);
147  /* pass 1 */
148  BF0( 4, 27, COS0_4 , 1);
149  BF0(11, 20, COS0_11, 2);
150  /* pass 2 */
151  BF( 4, 11, COS1_4 , 1);
152  BF(20, 27,-COS1_4 , 1);
153  /* pass 3 */
154  BF( 3, 4, COS2_3 , 3);
155  BF(11, 12,-COS2_3 , 3);
156  BF(19, 20, COS2_3 , 3);
157  BF(27, 28,-COS2_3 , 3);
158  /* pass 4 */
159  BF( 0, 3, COS3_0 , 1);
160  BF( 4, 7,-COS3_0 , 1);
161  BF( 8, 11, COS3_0 , 1);
162  BF(12, 15,-COS3_0 , 1);
163  BF(16, 19, COS3_0 , 1);
164  BF(20, 23,-COS3_0 , 1);
165  BF(24, 27, COS3_0 , 1);
166  BF(28, 31,-COS3_0 , 1);
167 
168 
169 
170  /* pass 1 */
171  BF0( 1, 30, COS0_1 , 1);
172  BF0(14, 17, COS0_14, 3);
173  /* pass 2 */
174  BF( 1, 14, COS1_1 , 1);
175  BF(17, 30,-COS1_1 , 1);
176  /* pass 1 */
177  BF0( 6, 25, COS0_6 , 1);
178  BF0( 9, 22, COS0_9 , 1);
179  /* pass 2 */
180  BF( 6, 9, COS1_6 , 2);
181  BF(22, 25,-COS1_6 , 2);
182  /* pass 3 */
183  BF( 1, 6, COS2_1 , 1);
184  BF( 9, 14,-COS2_1 , 1);
185  BF(17, 22, COS2_1 , 1);
186  BF(25, 30,-COS2_1 , 1);
187 
188  /* pass 1 */
189  BF0( 2, 29, COS0_2 , 1);
190  BF0(13, 18, COS0_13, 3);
191  /* pass 2 */
192  BF( 2, 13, COS1_2 , 1);
193  BF(18, 29,-COS1_2 , 1);
194  /* pass 1 */
195  BF0( 5, 26, COS0_5 , 1);
196  BF0(10, 21, COS0_10, 1);
197  /* pass 2 */
198  BF( 5, 10, COS1_5 , 2);
199  BF(21, 26,-COS1_5 , 2);
200  /* pass 3 */
201  BF( 2, 5, COS2_2 , 1);
202  BF(10, 13,-COS2_2 , 1);
203  BF(18, 21, COS2_2 , 1);
204  BF(26, 29,-COS2_2 , 1);
205  /* pass 4 */
206  BF( 1, 2, COS3_1 , 2);
207  BF( 5, 6,-COS3_1 , 2);
208  BF( 9, 10, COS3_1 , 2);
209  BF(13, 14,-COS3_1 , 2);
210  BF(17, 18, COS3_1 , 2);
211  BF(21, 22,-COS3_1 , 2);
212  BF(25, 26, COS3_1 , 2);
213  BF(29, 30,-COS3_1 , 2);
214 
215  /* pass 5 */
216  BF1( 0, 1, 2, 3);
217  BF2( 4, 5, 6, 7);
218  BF1( 8, 9, 10, 11);
219  BF2(12, 13, 14, 15);
220  BF1(16, 17, 18, 19);
221  BF2(20, 21, 22, 23);
222  BF1(24, 25, 26, 27);
223  BF2(28, 29, 30, 31);
224 
225  /* pass 6 */
226 
227  ADD( 8, 12);
228  ADD(12, 10);
229  ADD(10, 14);
230  ADD(14, 9);
231  ADD( 9, 13);
232  ADD(13, 11);
233  ADD(11, 15);
234 
235  out[ 0] = val0;
236  out[16] = val1;
237  out[ 8] = val2;
238  out[24] = val3;
239  out[ 4] = val4;
240  out[20] = val5;
241  out[12] = val6;
242  out[28] = val7;
243  out[ 2] = val8;
244  out[18] = val9;
245  out[10] = val10;
246  out[26] = val11;
247  out[ 6] = val12;
248  out[22] = val13;
249  out[14] = val14;
250  out[30] = val15;
251 
252  ADD(24, 28);
253  ADD(28, 26);
254  ADD(26, 30);
255  ADD(30, 25);
256  ADD(25, 29);
257  ADD(29, 27);
258  ADD(27, 31);
259 
260  out[ 1] = val16 + val24;
261  out[17] = val17 + val25;
262  out[ 9] = val18 + val26;
263  out[25] = val19 + val27;
264  out[ 5] = val20 + val28;
265  out[21] = val21 + val29;
266  out[13] = val22 + val30;
267  out[29] = val23 + val31;
268  out[ 3] = val24 + val20;
269  out[19] = val25 + val21;
270  out[11] = val26 + val22;
271  out[27] = val27 + val23;
272  out[ 7] = val28 + val18;
273  out[23] = val29 + val19;
274  out[15] = val30 + val17;
275  out[31] = val31;
276 }
#define COS2_2
Definition: dct32.c:70
#define COS0_12
Definition: dct32.c:54
#define COS0_11
Definition: dct32.c:53
#define COS1_3
Definition: dct32.c:62
#define COS2_3
Definition: dct32.c:71
#define COS0_8
Definition: dct32.c:50
#define COS0_14
Definition: dct32.c:56
#define COS1_7
Definition: dct32.c:66
#define dct32
Definition: dct32.c:31
#define COS0_3
Definition: dct32.c:45
#define COS3_1
Definition: dct32.c:74
#define COS1_5
Definition: dct32.c:64
#define COS1_1
Definition: dct32.c:60
#define BF1(a, b, c, d)
Definition: dct32.c:95
#define COS1_0
Definition: dct32.c:59
#define COS0_7
Definition: dct32.c:49
#define BF2(a, b, c, d)
Definition: dct32.c:102
#define INTFLOAT
Definition: dct32.c:34
#define COS2_1
Definition: dct32.c:69
#define COS0_9
Definition: dct32.c:51
#define COS1_2
Definition: dct32.c:61
#define COS0_10
Definition: dct32.c:52
#define BF(a, b, c, s)
Definition: dct32.c:79
#define COS0_1
Definition: dct32.c:43
#define COS1_4
Definition: dct32.c:63
#define COS0_0
Definition: dct32.c:42
#define COS0_2
Definition: dct32.c:44
#define COS0_5
Definition: dct32.c:47
#define COS0_13
Definition: dct32.c:55
#define BF0(a, b, c, s)
Definition: dct32.c:87
#define COS0_4
Definition: dct32.c:46
#define COS0_6
Definition: dct32.c:48
#define COS0_15
Definition: dct32.c:57
static const struct twinvq_data tab
#define COS2_0
Definition: dct32.c:68
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
#define COS3_0
Definition: dct32.c:73
#define ADD(a, b)
Definition: dct32.c:112
#define COS1_6
Definition: dct32.c:65