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