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;
79 /*****************************************************************************
81 *****************************************************************************/
83 /*****************************************************************************
84 * support routines borrowed from mpg321 (file: mad.c), which is distributed
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).
91 * It also incorporates some source from mad, written by Robert Leslie
92 *****************************************************************************/
94 /* The following two routines and data structure are from the ever-brilliant
98 #define VLC_F_FRACBITS 28
100 # if VLC_F_FRACBITS == 28
101 # define VLC_F(x) ((vlc_fixed_t) (x##L))
104 # define VLC_F_ONE VLC_F(0x10000000)
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)
112 sample += (1L << (VLC_F_FRACBITS - 16));
115 if (sample >= VLC_F_ONE)
116 sample = VLC_F_ONE - 1;
117 else if (sample < -VLC_F_ONE)
121 return (sample >> (VLC_F_FRACBITS + 1 - 16));
124 static block_t *Do_F32ToS16( filter_t * p_filter, block_t * p_in_buf )
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;
130 for ( i = p_in_buf->i_nb_samples
131 * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
134 *p_out++ = s24_to_s16_pcm(*p_in++);
136 p_in_buf->i_buffer /= 2;
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 * );
145 static int CreateTo( vlc_object_t *p_this )
147 filter_t * p_filter = (filter_t *)p_this;
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 ) )
154 switch( p_filter->fmt_in.audio.i_format )
157 p_filter->pf_audio_filter = Do_FL32ToF32;
161 p_filter->pf_audio_filter = Do_S16ToF32;
165 p_filter->pf_audio_filter = Do_U8ToF32;
175 /*****************************************************************************
177 *****************************************************************************/
178 static block_t *Do_S16ToF32( filter_t * p_filter, block_t * p_in_buf )
181 p_out_buf = filter_NewAudioBuffer( p_filter, p_in_buf->i_buffer * 2 );
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;
191 *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
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;
199 block_Release( p_in_buf );
204 /*****************************************************************************
206 *****************************************************************************/
207 static block_t *Do_U8ToF32( filter_t * p_filter, block_t * p_in_buf )
210 p_out_buf = filter_NewAudioBuffer( p_filter, 4 * p_in_buf->i_buffer );
214 int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
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;
221 *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
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;
228 block_Release( p_in_buf );
232 static block_t *Do_FL32ToF32( filter_t * p_filter, block_t * p_in_buf )
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;
237 for ( unsigned i = p_in_buf->i_nb_samples
238 * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
240 *p_out++ = (vlc_fixed_t)( *p_in++ * (float)FIXED32_ONE );