1 /*****************************************************************************
2 * fixed32tos16.c : converter from fixed32 to signed 16 bits integer
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: fixed32tos16.c,v 1.11 2003/10/25 00:49:13 sam Exp $
7 * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include "audio_output.h"
32 #include "aout_internal.h"
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Create ( vlc_object_t * );
39 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
42 /*****************************************************************************
44 *****************************************************************************/
46 set_description( _("audio filter for fixed32->s16 conversion") );
47 set_capability( "audio filter", 10 );
48 set_callbacks( Create, NULL );
51 /*****************************************************************************
52 * Create: allocate trivial mixer
53 *****************************************************************************
54 * This function allocates and initializes a Crop vout method.
55 *****************************************************************************/
56 static int Create( vlc_object_t *p_this )
58 aout_filter_t * p_filter = (aout_filter_t *)p_this;
60 if ( p_filter->input.i_format != VLC_FOURCC('f','i','3','2')
61 || p_filter->output.i_format != AOUT_FMT_S16_NE )
66 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
71 p_filter->pf_do_work = DoWork;
72 p_filter->b_in_place = 1;
77 /*****************************************************************************
78 * support routines borrowed from mpg321 (file: mad.c), which is distributed
81 * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
82 * from the smpeg sources, which was written by various people from Loki Software
83 * (http://www.lokigames.com).
85 * It also incorporates some source from mad, written by Robert Leslie
86 *****************************************************************************/
88 /* The following two routines and data structure are from the ever-brilliant
92 #define VLC_F_FRACBITS 28
94 # if VLC_F_FRACBITS == 28
95 # define VLC_F(x) ((vlc_fixed_t) (x##L))
98 # define VLC_F_ONE VLC_F(0x10000000)
100 struct audio_dither {
101 vlc_fixed_t error[3];
105 /********************************************************************
107 * DESCRIPTION: 32-bit pseudo-random number generator
108 ********************************************************************/
109 static inline unsigned long prng(unsigned long state)
111 return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
114 /********************************************************************
115 * NAME: mpg321_s24_to_s16_pcm()
116 * DESCRIPTION: generic linear sample quantize and dither routine
117 ********************************************************************/
118 static inline int16_t mpg321_s24_to_s16_pcm(unsigned int bits, vlc_fixed_t sample,
119 struct audio_dither *dither)
121 unsigned int scalebits;
122 vlc_fixed_t output, mask, random;
130 sample += dither->error[0] - dither->error[1] + dither->error[2];
132 dither->error[2] = dither->error[1];
133 dither->error[1] = dither->error[0] / 2;
136 output = sample + (1L << (VLC_F_FRACBITS + 1 - bits - 1));
138 scalebits = VLC_F_FRACBITS + 1 - bits;
139 mask = (1L << scalebits) - 1;
142 random = prng(dither->random);
143 output += (random & mask) - (dither->random & mask);
145 dither->random = random;
154 else if (output < MIN) {
165 dither->error[0] = sample - output;
168 return output >> scalebits;
171 /*****************************************************************************
172 * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
173 *****************************************************************************/
174 static inline int16_t s24_to_s16_pcm(vlc_fixed_t sample)
177 sample += (1L << (VLC_F_FRACBITS - 16));
180 if (sample >= VLC_F_ONE)
181 sample = VLC_F_ONE - 1;
182 else if (sample < -VLC_F_ONE)
186 return (sample >> (VLC_F_FRACBITS + 1 - 16));
189 /*****************************************************************************
190 * DoWork: convert a buffer
191 *****************************************************************************/
192 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
193 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
196 vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
197 int16_t * p_out = (int16_t *)p_out_buf->p_buffer;
199 static struct audio_dither dither;
202 for ( i = p_in_buf->i_nb_samples
203 * aout_FormatNbChannels( &p_filter->input ) ; i-- ; )
206 /* Accurate scaling */
207 *p_out++ = mpg321_s24_to_s16_pcm(16, *p_in++, &dither);
210 *p_out++ = s24_to_s16_pcm(*p_in++);
212 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
213 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes / 2;