]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/fixed32tos16.c
Improvements to preferences
[vlc] / modules / audio_filter / converter / fixed32tos16.c
1 /*****************************************************************************
2  * fixed32tos16.c : converter from fixed32 to signed 16 bits integer
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include "audio_output.h"
32 #include "aout_internal.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Create    ( vlc_object_t * );
38
39 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
40                         aout_buffer_t * );
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 vlc_module_begin();
46     set_category( CAT_AUDIO );
47     set_subcategory( SUBCAT_AUDIO_MISC );
48     set_description( _("audio filter for fixed32->s16 conversion") );
49     set_capability( "audio filter", 10 );
50     set_callbacks( Create, NULL );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Create: allocate trivial mixer
55  *****************************************************************************
56  * This function allocates and initializes a Crop vout method.
57  *****************************************************************************/
58 static int Create( vlc_object_t *p_this )
59 {
60     aout_filter_t * p_filter = (aout_filter_t *)p_this;
61
62     if ( p_filter->input.i_format != VLC_FOURCC('f','i','3','2')
63           || p_filter->output.i_format != AOUT_FMT_S16_NE )
64     {
65         return -1;
66     }
67
68     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
69     {
70         return -1;
71     }
72
73     p_filter->pf_do_work = DoWork;
74     p_filter->b_in_place = 1;
75
76     return 0;
77 }
78
79 /*****************************************************************************
80  * support routines borrowed from mpg321 (file: mad.c), which is distributed
81  * under GPL license
82  *
83  * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
84  * from the smpeg sources, which was written by various people from Loki Software
85  * (http://www.lokigames.com).
86  *
87  * It also incorporates some source from mad, written by Robert Leslie
88  *****************************************************************************/
89
90 /* The following two routines and data structure are from the ever-brilliant
91      Rob Leslie.
92 */
93
94 #define VLC_F_FRACBITS  28
95
96 # if VLC_F_FRACBITS == 28
97 #  define VLC_F(x) ((vlc_fixed_t) (x##L))
98 # endif
99
100 # define VLC_F_ONE VLC_F(0x10000000)
101
102 struct audio_dither {
103     vlc_fixed_t error[3];
104     vlc_fixed_t random;
105 };
106
107 /********************************************************************
108  * NAME:                prng()
109  * DESCRIPTION: 32-bit pseudo-random number generator
110  ********************************************************************/
111 static inline unsigned long prng(unsigned long state)
112 {
113     return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
114 }
115
116 /********************************************************************
117  * NAME:        mpg321_s24_to_s16_pcm()
118  * DESCRIPTION: generic linear sample quantize and dither routine
119  ********************************************************************/
120 static inline int16_t mpg321_s24_to_s16_pcm(unsigned int bits, vlc_fixed_t sample,
121                                             struct audio_dither *dither)
122 {
123     unsigned int scalebits;
124     vlc_fixed_t output, mask, random;
125
126     enum {
127         MIN = -VLC_F_ONE,
128         MAX = VLC_F_ONE - 1
129     };
130
131     /* noise shape */
132     sample += dither->error[0] - dither->error[1] + dither->error[2];
133
134     dither->error[2] = dither->error[1];
135     dither->error[1] = dither->error[0] / 2;
136
137     /* bias */
138     output = sample + (1L << (VLC_F_FRACBITS + 1 - bits - 1));
139
140     scalebits = VLC_F_FRACBITS + 1 - bits;
141     mask = (1L << scalebits) - 1;
142
143     /* dither */
144     random    = prng(dither->random);
145     output += (random & mask) - (dither->random & mask);
146
147     dither->random = random;
148
149     /* clip */
150     if (output > MAX) {
151         output = MAX;
152
153         if (sample > MAX)
154             sample = MAX;
155     }
156     else if (output < MIN) {
157         output = MIN;
158
159         if (sample < MIN)
160             sample = MIN;
161     }
162
163     /* quantize */
164     output &= ~mask;
165
166     /* error feedback */
167     dither->error[0] = sample - output;
168
169     /* scale */
170     return output >> scalebits;
171 }
172
173 /*****************************************************************************
174  * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
175  *****************************************************************************/
176 static inline int16_t s24_to_s16_pcm(vlc_fixed_t sample)
177 {
178   /* round */
179   sample += (1L << (VLC_F_FRACBITS - 16));
180
181   /* clip */
182   if (sample >= VLC_F_ONE)
183     sample = VLC_F_ONE - 1;
184   else if (sample < -VLC_F_ONE)
185     sample = -VLC_F_ONE;
186
187   /* quantize */
188   return (sample >> (VLC_F_FRACBITS + 1 - 16));
189 }
190
191 /*****************************************************************************
192  * DoWork: convert a buffer
193  *****************************************************************************/
194 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
195                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
196 {
197     int i;
198     vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
199     int16_t * p_out = (int16_t *)p_out_buf->p_buffer;
200 #if 0
201     static struct audio_dither dither;
202 #endif
203
204     for ( i = p_in_buf->i_nb_samples
205                * aout_FormatNbChannels( &p_filter->input ) ; i-- ; )
206     {
207 #if 0
208         /* Accurate scaling */
209         *p_out++ = mpg321_s24_to_s16_pcm(16, *p_in++, &dither);
210 #endif
211         /* Fast Scaling */
212         *p_out++ = s24_to_s16_pcm(*p_in++);
213     }
214     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
215     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes / 2;
216 }