]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/fixed.c
dbus: adds some playlist locking, finish [22423]
[vlc] / modules / audio_filter / converter / fixed.c
1 /*****************************************************************************
2  * fixed.c: Fixed-point audio format conversions
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 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 #include <vlc/vlc.h>
31 #include <vlc_aout.h>
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int  Create_F32ToS16    ( vlc_object_t * );
37 static void Do_F32ToS16( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
38                          aout_buffer_t * );
39
40 static int  Create_S16ToF32    ( vlc_object_t * );
41 static void Do_S16ToF32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
42                          aout_buffer_t * );
43
44 static int  Create_U8ToF32    ( vlc_object_t * );
45 static void Do_U8ToF32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
46                          aout_buffer_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();
52     set_description( _("Fixed point audio format conversions") );
53     add_submodule();
54         set_callbacks( Create_F32ToS16, NULL );
55         set_capability( "audio filter", 10 );
56     add_submodule();
57         set_callbacks( Create_S16ToF32, NULL );
58         set_capability( "audio filter", 15 );
59     add_submodule();
60         set_callbacks( Create_U8ToF32, NULL );
61         set_capability( "audio filter", 1 );
62 vlc_module_end();
63
64 /*****************************************************************************
65  * F32 to S16
66  *****************************************************************************/
67 static int Create_F32ToS16( vlc_object_t *p_this )
68 {
69     aout_filter_t * p_filter = (aout_filter_t *)p_this;
70
71     if ( p_filter->input.i_format != VLC_FOURCC('f','i','3','2')
72           || p_filter->output.i_format != AOUT_FMT_S16_NE )
73     {
74         return -1;
75     }
76
77     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
78     {
79         return -1;
80     }
81
82     p_filter->pf_do_work = Do_F32ToS16;
83     p_filter->b_in_place = 1;
84
85     return VLC_SUCCESS;;
86 }
87
88 /*****************************************************************************
89  * support routines borrowed from mpg321 (file: mad.c), which is distributed
90  * under GPL license
91  *
92  * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
93  * from the smpeg sources, which was written by various people from Loki Software
94  * (http://www.lokigames.com).
95  *
96  * It also incorporates some source from mad, written by Robert Leslie
97  *****************************************************************************/
98
99 /* The following two routines and data structure are from the ever-brilliant
100      Rob Leslie.
101 */
102
103 #define VLC_F_FRACBITS  28
104
105 # if VLC_F_FRACBITS == 28
106 #  define VLC_F(x) ((vlc_fixed_t) (x##L))
107 # endif
108
109 # define VLC_F_ONE VLC_F(0x10000000)
110
111 /*****************************************************************************
112  * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
113  *****************************************************************************/
114 static inline int16_t s24_to_s16_pcm(vlc_fixed_t sample)
115 {
116   /* round */
117   sample += (1L << (VLC_F_FRACBITS - 16));
118
119   /* clip */
120   if (sample >= VLC_F_ONE)
121     sample = VLC_F_ONE - 1;
122   else if (sample < -VLC_F_ONE)
123     sample = -VLC_F_ONE;
124
125   /* quantize */
126   return (sample >> (VLC_F_FRACBITS + 1 - 16));
127 }
128
129 static void Do_F32ToS16( aout_instance_t * p_aout, aout_filter_t * p_filter,
130                          aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
131 {
132     int i;
133     vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
134     int16_t * p_out = (int16_t *)p_out_buf->p_buffer;
135
136     for ( i = p_in_buf->i_nb_samples
137                * aout_FormatNbChannels( &p_filter->input ) ; i-- ; )
138     {
139         /* Fast Scaling */
140         *p_out++ = s24_to_s16_pcm(*p_in++);
141     }
142     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
143     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes / 2;
144 }
145
146
147 /*****************************************************************************
148  * S16 to F32
149  *****************************************************************************/
150 static int Create_S16ToF32( vlc_object_t *p_this )
151 {
152     aout_filter_t * p_filter = (aout_filter_t *)p_this;
153
154     if ( p_filter->output.i_format != VLC_FOURCC('f','i','3','2')
155           || p_filter->input.i_format != AOUT_FMT_S16_NE )
156     {
157         return -1;
158     }
159
160     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
161     {
162         return -1;
163     }
164
165     p_filter->pf_do_work = Do_S16ToF32;
166     p_filter->b_in_place = 1;
167
168     return 0;
169 }
170
171 static void Do_S16ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
172                          aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
173 {
174     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->input );
175
176     /* We start from the end because b_in_place is true */
177     int16_t * p_in = (int16_t *)p_in_buf->p_buffer + i - 1;
178     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer + i - 1;
179
180     while( i-- )
181     {
182         *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
183         p_in--; p_out--;
184     }
185
186     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
187     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes
188             * sizeof(vlc_fixed_t) / sizeof(int16_t);
189 }
190
191
192 /*****************************************************************************
193  * U8 to F32
194  *****************************************************************************/
195 static int Create_U8ToF32( vlc_object_t *p_this )
196 {
197     aout_filter_t * p_filter = (aout_filter_t *)p_this;
198
199     if ( p_filter->input.i_format != VLC_FOURCC('u','8',' ',' ')
200           || p_filter->output.i_format != VLC_FOURCC('f','i','3','2') )
201     {
202         return -1;
203     }
204
205     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
206     {
207         return -1;
208     }
209
210     p_filter->pf_do_work = Do_U8ToF32;
211     p_filter->b_in_place = VLC_TRUE;
212
213     return 0;
214 }
215
216 static void Do_U8ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
217                         aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
218 {
219     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->input );
220
221     /* We start from the end because b_in_place is true */
222     uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer + i - 1;
223     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer + i - 1;
224
225     while( i-- )
226     {
227         *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
228         p_in--; p_out--;
229     }
230
231     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
232     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * sizeof(vlc_fixed_t);
233 }