]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/fixed.c
8627deaa49c705f00ce22265f58827339f439d8d
[vlc] / modules / audio_filter / converter / fixed.c
1 /*****************************************************************************
2  * fixed.c: Fixed-point audio format conversions
3  *****************************************************************************
4  * Copyright (C) 2002, 2006-2009 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_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_aout.h>
37 #include <vlc_filter.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create_F32ToS16    ( vlc_object_t * );
43 static block_t *Do_F32ToS16( filter_t *, block_t * );
44
45 static int  Create_S16ToF32    ( vlc_object_t * );
46 static block_t *Do_S16ToF32( filter_t *, block_t * );
47
48 static int  Create_U8ToF32    ( vlc_object_t * );
49 static block_t *Do_U8ToF32( filter_t *, block_t * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 vlc_module_begin ()
55     set_description( N_("Fixed point audio format conversions") )
56     add_submodule ()
57         set_callbacks( Create_F32ToS16, NULL )
58         set_capability( "audio filter2", 10 )
59     add_submodule ()
60         set_callbacks( Create_S16ToF32, NULL )
61         set_capability( "audio filter2", 15 )
62     add_submodule ()
63         set_callbacks( Create_U8ToF32, NULL )
64         set_capability( "audio filter2", 1 )
65 vlc_module_end ()
66
67 /*****************************************************************************
68  * F32 to S16
69  *****************************************************************************/
70 static int Create_F32ToS16( vlc_object_t *p_this )
71 {
72     filter_t * p_filter = (filter_t *)p_this;
73
74     if ( p_filter->fmt_in.audio.i_format != VLC_CODEC_FI32
75           || p_filter->fmt_out.audio.i_format != VLC_CODEC_S16N )
76     {
77         return VLC_EGENERIC;
78     }
79
80     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
81     {
82         return VLC_EGENERIC;
83     }
84
85     p_filter->pf_audio_filter = Do_F32ToS16;
86
87     return VLC_SUCCESS;;
88 }
89
90 /*****************************************************************************
91  * support routines borrowed from mpg321 (file: mad.c), which is distributed
92  * under GPL license
93  *
94  * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
95  * from the smpeg sources, which was written by various people from Loki Software
96  * (http://www.lokigames.com).
97  *
98  * It also incorporates some source from mad, written by Robert Leslie
99  *****************************************************************************/
100
101 /* The following two routines and data structure are from the ever-brilliant
102      Rob Leslie.
103 */
104
105 #define VLC_F_FRACBITS  28
106
107 # if VLC_F_FRACBITS == 28
108 #  define VLC_F(x) ((vlc_fixed_t) (x##L))
109 # endif
110
111 # define VLC_F_ONE VLC_F(0x10000000)
112
113 /*****************************************************************************
114  * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
115  *****************************************************************************/
116 static inline int16_t s24_to_s16_pcm(vlc_fixed_t sample)
117 {
118   /* round */
119   sample += (1L << (VLC_F_FRACBITS - 16));
120
121   /* clip */
122   if (sample >= VLC_F_ONE)
123     sample = VLC_F_ONE - 1;
124   else if (sample < -VLC_F_ONE)
125     sample = -VLC_F_ONE;
126
127   /* quantize */
128   return (sample >> (VLC_F_FRACBITS + 1 - 16));
129 }
130
131 static block_t *Do_F32ToS16( filter_t * p_filter, block_t * p_in_buf )
132 {
133     int i;
134     vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
135     int16_t * p_out = (int16_t *)p_in_buf->p_buffer;
136
137     for ( i = p_in_buf->i_nb_samples
138                * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
139     {
140         /* Fast Scaling */
141         *p_out++ = s24_to_s16_pcm(*p_in++);
142     }
143     p_in_buf->i_buffer /= 2;
144     return p_in_buf;
145 }
146
147
148 /*****************************************************************************
149  * S16 to F32
150  *****************************************************************************/
151 static int Create_S16ToF32( vlc_object_t *p_this )
152 {
153     filter_t * p_filter = (filter_t *)p_this;
154
155     if ( p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32
156           || p_filter->fmt_in.audio.i_format != VLC_CODEC_S16N )
157     {
158         return VLC_EGENERIC;
159     }
160
161     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
162     {
163         return VLC_EGENERIC;
164     }
165
166     p_filter->pf_audio_filter = Do_S16ToF32;
167
168     return VLC_SUCCESS;
169 }
170
171 static block_t *Do_S16ToF32( filter_t * p_filter, block_t * p_in_buf )
172 {
173     block_t *p_out_buf;
174     p_out_buf = filter_NewAudioBuffer( p_filter, p_in_buf->i_buffer * 2 );
175     if( !p_out_buf )
176         goto out;
177
178     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
179     const int16_t * p_in = (int16_t *)p_in_buf->p_buffer;
180     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
181
182     while( i-- )
183     {
184         *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
185         p_in++; p_out++;
186     }
187
188     p_out_buf->i_pts = p_in_buf->i_pts;
189     p_out_buf->i_length = p_in_buf->i_length;
190     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
191 out:
192     block_Release( p_in_buf );
193     return p_out_buf;
194 }
195
196
197 /*****************************************************************************
198  * U8 to F32
199  *****************************************************************************/
200 static int Create_U8ToF32( vlc_object_t *p_this )
201 {
202     filter_t * p_filter = (filter_t *)p_this;
203
204     if ( p_filter->fmt_in.audio.i_format != VLC_CODEC_U8
205           || p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32 )
206     {
207         return VLC_EGENERIC;
208     }
209
210     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
211     {
212         return VLC_EGENERIC;
213     }
214
215     p_filter->pf_audio_filter = Do_U8ToF32;
216
217     return VLC_SUCCESS;
218 }
219
220 static block_t *Do_U8ToF32( filter_t * p_filter, block_t * p_in_buf )
221 {
222     block_t *p_out_buf;
223     p_out_buf = filter_NewAudioBuffer( p_filter, 4 * p_in_buf->i_buffer );
224     if( !p_out_buf )
225         goto out;
226
227     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
228
229     uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer;
230     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
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     p_out_buf->i_pts = p_in_buf->i_pts;
238     p_out_buf->i_length = p_in_buf->i_length;
239     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
240 out:
241     block_Release( p_in_buf );
242     return p_out_buf;
243 }