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_S16ToF32( filter_t *, block_t * );
142 static block_t *Do_U8ToF32( filter_t *, block_t * );
144 static int CreateTo( vlc_object_t *p_this )
146 filter_t * p_filter = (filter_t *)p_this;
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 ) )
153 switch( p_filter->fmt_in.audio.i_format )
156 p_filter->pf_audio_filter = Do_S16ToF32;
160 p_filter->pf_audio_filter = Do_U8ToF32;
170 /*****************************************************************************
172 *****************************************************************************/
173 static block_t *Do_S16ToF32( filter_t * p_filter, block_t * p_in_buf )
176 p_out_buf = filter_NewAudioBuffer( p_filter, p_in_buf->i_buffer * 2 );
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;
186 *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
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;
194 block_Release( p_in_buf );
199 /*****************************************************************************
201 *****************************************************************************/
202 static block_t *Do_U8ToF32( filter_t * p_filter, block_t * p_in_buf )
205 p_out_buf = filter_NewAudioBuffer( p_filter, 4 * p_in_buf->i_buffer );
209 int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
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;
216 *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
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;
223 block_Release( p_in_buf );