]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/fixed.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / audio_filter / converter / fixed.c
1 /*****************************************************************************
2  * fixed.c: Fixed-point audio format conversions
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8  *          Marc Ariberti <marcari@videolan.org>
9  *          Samuel Hocevar <sam@zoy.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc/vlc.h>
35 #include <vlc_aout.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create_F32ToS16    ( vlc_object_t * );
41 static void Do_F32ToS16( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
42                          aout_buffer_t * );
43
44 static int  Create_S16ToF32    ( vlc_object_t * );
45 static void Do_S16ToF32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
46                          aout_buffer_t * );
47
48 static int  Create_U8ToF32    ( vlc_object_t * );
49 static void Do_U8ToF32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
50                          aout_buffer_t * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 vlc_module_begin();
56     set_description( _("Fixed point audio format conversions") );
57     add_submodule();
58         set_callbacks( Create_F32ToS16, NULL );
59         set_capability( "audio filter", 10 );
60     add_submodule();
61         set_callbacks( Create_S16ToF32, NULL );
62         set_capability( "audio filter", 15 );
63     add_submodule();
64         set_callbacks( Create_U8ToF32, NULL );
65         set_capability( "audio filter", 1 );
66 vlc_module_end();
67
68 /*****************************************************************************
69  * F32 to S16
70  *****************************************************************************/
71 static int Create_F32ToS16( vlc_object_t *p_this )
72 {
73     aout_filter_t * p_filter = (aout_filter_t *)p_this;
74
75     if ( p_filter->input.i_format != VLC_FOURCC('f','i','3','2')
76           || p_filter->output.i_format != AOUT_FMT_S16_NE )
77     {
78         return -1;
79     }
80
81     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
82     {
83         return -1;
84     }
85
86     p_filter->pf_do_work = Do_F32ToS16;
87     p_filter->b_in_place = 1;
88
89     return VLC_SUCCESS;;
90 }
91
92 /*****************************************************************************
93  * support routines borrowed from mpg321 (file: mad.c), which is distributed
94  * under GPL license
95  *
96  * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
97  * from the smpeg sources, which was written by various people from Loki Software
98  * (http://www.lokigames.com).
99  *
100  * It also incorporates some source from mad, written by Robert Leslie
101  *****************************************************************************/
102
103 /* The following two routines and data structure are from the ever-brilliant
104      Rob Leslie.
105 */
106
107 #define VLC_F_FRACBITS  28
108
109 # if VLC_F_FRACBITS == 28
110 #  define VLC_F(x) ((vlc_fixed_t) (x##L))
111 # endif
112
113 # define VLC_F_ONE VLC_F(0x10000000)
114
115 /*****************************************************************************
116  * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
117  *****************************************************************************/
118 static inline int16_t s24_to_s16_pcm(vlc_fixed_t sample)
119 {
120   /* round */
121   sample += (1L << (VLC_F_FRACBITS - 16));
122
123   /* clip */
124   if (sample >= VLC_F_ONE)
125     sample = VLC_F_ONE - 1;
126   else if (sample < -VLC_F_ONE)
127     sample = -VLC_F_ONE;
128
129   /* quantize */
130   return (sample >> (VLC_F_FRACBITS + 1 - 16));
131 }
132
133 static void Do_F32ToS16( aout_instance_t * p_aout, aout_filter_t * p_filter,
134                          aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
135 {
136     VLC_UNUSED(p_aout);
137     int i;
138     vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
139     int16_t * p_out = (int16_t *)p_out_buf->p_buffer;
140
141     for ( i = p_in_buf->i_nb_samples
142                * aout_FormatNbChannels( &p_filter->input ) ; i-- ; )
143     {
144         /* Fast Scaling */
145         *p_out++ = s24_to_s16_pcm(*p_in++);
146     }
147     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
148     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes / 2;
149 }
150
151
152 /*****************************************************************************
153  * S16 to F32
154  *****************************************************************************/
155 static int Create_S16ToF32( vlc_object_t *p_this )
156 {
157     aout_filter_t * p_filter = (aout_filter_t *)p_this;
158
159     if ( p_filter->output.i_format != VLC_FOURCC('f','i','3','2')
160           || p_filter->input.i_format != AOUT_FMT_S16_NE )
161     {
162         return -1;
163     }
164
165     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
166     {
167         return -1;
168     }
169
170     p_filter->pf_do_work = Do_S16ToF32;
171     p_filter->b_in_place = 1;
172
173     return 0;
174 }
175
176 static void Do_S16ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
177                          aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
178 {
179     VLC_UNUSED(p_aout);
180     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->input );
181
182     /* We start from the end because b_in_place is true */
183     int16_t * p_in = (int16_t *)p_in_buf->p_buffer + i - 1;
184     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer + i - 1;
185
186     while( i-- )
187     {
188         *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
189         p_in--; p_out--;
190     }
191
192     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
193     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes
194             * sizeof(vlc_fixed_t) / sizeof(int16_t);
195 }
196
197
198 /*****************************************************************************
199  * U8 to F32
200  *****************************************************************************/
201 static int Create_U8ToF32( vlc_object_t *p_this )
202 {
203     aout_filter_t * p_filter = (aout_filter_t *)p_this;
204
205     if ( p_filter->input.i_format != VLC_FOURCC('u','8',' ',' ')
206           || p_filter->output.i_format != VLC_FOURCC('f','i','3','2') )
207     {
208         return -1;
209     }
210
211     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
212     {
213         return -1;
214     }
215
216     p_filter->pf_do_work = Do_U8ToF32;
217     p_filter->b_in_place = true;
218
219     return 0;
220 }
221
222 static void Do_U8ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
223                         aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
224 {
225     VLC_UNUSED(p_aout);
226     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->input );
227
228     /* We start from the end because b_in_place is true */
229     uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer + i - 1;
230     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer + i - 1;
231
232     while( i-- )
233     {
234         *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
235         p_in--; p_out--;
236     }
237
238     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
239     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * sizeof(vlc_fixed_t);
240 }