]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/fixed.c
e982c6d693e3dc84428835b25f5fb38b1443c415
[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_S16ToF32( filter_t *, block_t * );
142 static block_t *Do_U8ToF32( filter_t *, block_t * );
143
144 static int CreateTo( vlc_object_t *p_this )
145 {
146     filter_t * p_filter = (filter_t *)p_this;
147
148     if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32
149      || !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio,
150                             &p_filter->fmt_out.audio ) )
151         return VLC_EGENERIC;
152
153     switch( p_filter->fmt_in.audio.i_format )
154     {
155         case VLC_CODEC_S16N:
156             p_filter->pf_audio_filter = Do_S16ToF32;
157             break;
158
159         case VLC_CODEC_U8:
160             p_filter->pf_audio_filter = Do_U8ToF32;
161             break;
162
163         default:
164             return VLC_EGENERIC;
165     }
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * S16 to F32
172  *****************************************************************************/
173 static block_t *Do_S16ToF32( filter_t * p_filter, block_t * p_in_buf )
174 {
175     block_t *p_out_buf;
176     p_out_buf = filter_NewAudioBuffer( p_filter, p_in_buf->i_buffer * 2 );
177     if( !p_out_buf )
178         goto out;
179
180     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
181     const int16_t * p_in = (int16_t *)p_in_buf->p_buffer;
182     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
183
184     while( i-- )
185     {
186         *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
187         p_in++; p_out++;
188     }
189
190     p_out_buf->i_pts = p_in_buf->i_pts;
191     p_out_buf->i_length = p_in_buf->i_length;
192     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
193 out:
194     block_Release( p_in_buf );
195     return p_out_buf;
196 }
197
198
199 /*****************************************************************************
200  * U8 to F32
201  *****************************************************************************/
202 static block_t *Do_U8ToF32( filter_t * p_filter, block_t * p_in_buf )
203 {
204     block_t *p_out_buf;
205     p_out_buf = filter_NewAudioBuffer( p_filter, 4 * p_in_buf->i_buffer );
206     if( !p_out_buf )
207         goto out;
208
209     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
210
211     uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer;
212     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
213
214     while( i-- )
215     {
216         *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
217         p_in++; p_out++;
218     }
219     p_out_buf->i_pts = p_in_buf->i_pts;
220     p_out_buf->i_length = p_in_buf->i_length;
221     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
222 out:
223     block_Release( p_in_buf );
224     return p_out_buf;
225 }