1 /*****************************************************************************
2 * fixed.c: Fixed-point audio format conversions
3 *****************************************************************************
4 * Copyright (C) 2002, 2006-2009 the VideoLAN team
7 * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8 * Marc Ariberti <marcari@videolan.org>
9 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
39 static int CreateTo( vlc_object_t * );
40 static int CreateFrom( vlc_object_t * );
42 /*****************************************************************************
44 *****************************************************************************/
46 set_description( N_("Fixed point audio format conversions") )
47 set_callbacks( CreateTo, NULL )
48 set_capability( "audio filter", 15 )
50 set_callbacks( CreateFrom, NULL )
51 set_capability( "audio filter", 10 )
54 /*** Conversion from FI32 to audio output ***/
55 static block_t *Do_F32ToS16( filter_t *, block_t * );
57 static int CreateFrom( vlc_object_t *p_this )
59 filter_t * p_filter = (filter_t *)p_this;
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 ) )
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 )
71 p_filter->pf_audio_filter = Do_F32ToS16;
86 /*****************************************************************************
88 *****************************************************************************/
90 /*****************************************************************************
91 * support routines borrowed from mpg321 (file: mad.c), which is distributed
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).
98 * It also incorporates some source from mad, written by Robert Leslie
99 *****************************************************************************/
101 /* The following two routines and data structure are from the ever-brilliant
105 #define VLC_F_FRACBITS 28
107 # if VLC_F_FRACBITS == 28
108 # define VLC_F(x) ((vlc_fixed_t) (x##L))
111 # define VLC_F_ONE VLC_F(0x10000000)
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)
119 sample += (1L << (VLC_F_FRACBITS - 16));
122 if (sample >= VLC_F_ONE)
123 sample = VLC_F_ONE - 1;
124 else if (sample < -VLC_F_ONE)
128 return (sample >> (VLC_F_FRACBITS + 1 - 16));
131 static block_t *Do_F32ToS16( filter_t * p_filter, block_t * p_in_buf )
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;
137 for ( i = p_in_buf->i_nb_samples
138 * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
141 *p_out++ = s24_to_s16_pcm(*p_in++);
143 p_in_buf->i_buffer /= 2;
147 /*** Conversions from decoders to FI32 */
148 static block_t *Do_FL32ToF32( filter_t *, block_t * );
149 static block_t *Do_S32ToF32( filter_t *, block_t * );
150 static block_t *Do_S16ToF32( filter_t *, block_t * );
151 static block_t *Do_U8ToF32( filter_t *, block_t * );
153 static int CreateTo( vlc_object_t *p_this )
155 filter_t * p_filter = (filter_t *)p_this;
157 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32
158 || !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio,
159 &p_filter->fmt_out.audio ) )
162 switch( p_filter->fmt_in.audio.i_format )
165 p_filter->pf_audio_filter = Do_FL32ToF32;
169 p_filter->pf_audio_filter = Do_S32ToF32;
173 p_filter->pf_audio_filter = Do_S16ToF32;
177 p_filter->pf_audio_filter = Do_U8ToF32;
187 /*****************************************************************************
189 *****************************************************************************/
190 static block_t *Do_S16ToF32( filter_t * p_filter, block_t * p_in_buf )
193 p_out_buf = filter_NewAudioBuffer( p_filter, p_in_buf->i_buffer * 2 );
197 int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
198 const int16_t * p_in = (int16_t *)p_in_buf->p_buffer;
199 vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
203 *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
207 p_out_buf->i_pts = p_in_buf->i_pts;
208 p_out_buf->i_length = p_in_buf->i_length;
209 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
211 block_Release( p_in_buf );
216 /*****************************************************************************
218 *****************************************************************************/
219 static block_t *Do_U8ToF32( filter_t * p_filter, block_t * p_in_buf )
222 p_out_buf = filter_NewAudioBuffer( p_filter, 4 * p_in_buf->i_buffer );
226 int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
228 uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer;
229 vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
233 *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
236 p_out_buf->i_pts = p_in_buf->i_pts;
237 p_out_buf->i_length = p_in_buf->i_length;
238 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
240 block_Release( p_in_buf );
244 static block_t *Do_FL32ToF32( filter_t * p_filter, block_t * p_in_buf )
246 unsigned count = p_in_buf->i_nb_samples
247 * aout_FormatNbChannels( &p_filter->fmt_in.audio );
248 union dw *restrict p = (union dw *)p_in_buf->p_buffer, *end = p + count;
249 const float one = FIXED32_ONE;
259 static block_t *Do_S32ToF32( filter_t * p_filter, block_t * p_in_buf )
261 unsigned count = p_in_buf->i_nb_samples
262 * aout_FormatNbChannels( &p_filter->fmt_in.audio );
263 int32_t *restrict p = (int32_t *)p_in_buf->p_buffer, *end = p + count;