]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/fixed.c
Fixed timestamps handling in various audio filters.
[vlc] / modules / audio_filter / converter / fixed.c
1 /*****************************************************************************
2  * fixed.c: Fixed-point audio format conversions
3  *****************************************************************************
4  * Copyright (C) 2002, 2006-2009 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 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_aout.h>
37 #include <vlc_filter.h>
38
39 static int  CreateTo( vlc_object_t * );
40 static int  CreateFrom( vlc_object_t * );
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 vlc_module_begin ()
46     set_description( N_("Fixed point audio format conversions") )
47     set_callbacks( CreateTo, NULL )
48     set_capability( "audio filter", 15 )
49     add_submodule ()
50         set_callbacks( CreateFrom, NULL )
51         set_capability( "audio filter", 10 )
52 vlc_module_end ()
53
54 /*** Conversion from FI32 to audio output ***/
55 static block_t *Do_F32ToS16( filter_t *, block_t * );
56
57 static int CreateFrom( vlc_object_t *p_this )
58 {
59     filter_t * p_filter = (filter_t *)p_this;
60
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 ) )
64         return VLC_EGENERIC;
65
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 )
69     {
70         case VLC_CODEC_S16N:
71             p_filter->pf_audio_filter = Do_F32ToS16;
72             break;
73         default:
74             return VLC_EGENERIC;
75     }
76     return VLC_SUCCESS;
77 }
78
79 union dw
80 {
81     float    f;
82     int32_t  s;
83     uint32_t u;
84 };
85
86 /*****************************************************************************
87  * F32 to S16
88  *****************************************************************************/
89
90 /*****************************************************************************
91  * support routines borrowed from mpg321 (file: mad.c), which is distributed
92  * under GPL license
93  *
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).
97  *
98  * It also incorporates some source from mad, written by Robert Leslie
99  *****************************************************************************/
100
101 /* The following two routines and data structure are from the ever-brilliant
102      Rob Leslie.
103 */
104
105 #define VLC_F_FRACBITS  28
106
107 # if VLC_F_FRACBITS == 28
108 #  define VLC_F(x) ((vlc_fixed_t) (x##L))
109 # endif
110
111 # define VLC_F_ONE VLC_F(0x10000000)
112
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)
117 {
118   /* round */
119   sample += (1L << (VLC_F_FRACBITS - 16));
120
121   /* clip */
122   if (sample >= VLC_F_ONE)
123     sample = VLC_F_ONE - 1;
124   else if (sample < -VLC_F_ONE)
125     sample = -VLC_F_ONE;
126
127   /* quantize */
128   return (sample >> (VLC_F_FRACBITS + 1 - 16));
129 }
130
131 static block_t *Do_F32ToS16( filter_t * p_filter, block_t * p_in_buf )
132 {
133     int i;
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;
136
137     for ( i = p_in_buf->i_nb_samples
138                * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
139     {
140         /* Fast Scaling */
141         *p_out++ = s24_to_s16_pcm(*p_in++);
142     }
143     p_in_buf->i_buffer /= 2;
144     return p_in_buf;
145 }
146
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 * );
152
153 static int CreateTo( vlc_object_t *p_this )
154 {
155     filter_t * p_filter = (filter_t *)p_this;
156
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 ) )
160         return VLC_EGENERIC;
161
162     switch( p_filter->fmt_in.audio.i_format )
163     {
164         case VLC_CODEC_FL32:
165             p_filter->pf_audio_filter = Do_FL32ToF32;
166             break;
167
168         case VLC_CODEC_S32N:
169             p_filter->pf_audio_filter = Do_S32ToF32;
170             break;
171
172         case VLC_CODEC_S16N:
173             p_filter->pf_audio_filter = Do_S16ToF32;
174             break;
175
176         case VLC_CODEC_U8:
177             p_filter->pf_audio_filter = Do_U8ToF32;
178             break;
179
180         default:
181             return VLC_EGENERIC;
182     }
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  * S16 to F32
189  *****************************************************************************/
190 static block_t *Do_S16ToF32( filter_t * p_filter, block_t * p_in_buf )
191 {
192     block_t *p_out_buf;
193     p_out_buf = filter_NewAudioBuffer( p_filter, p_in_buf->i_buffer * 2 );
194     if( !p_out_buf )
195         goto out;
196
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;
200
201     while( i-- )
202     {
203         *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
204         p_in++; p_out++;
205     }
206
207     p_out_buf->i_dts = p_in_buf->i_dts;
208     p_out_buf->i_pts = p_in_buf->i_pts;
209     p_out_buf->i_length = p_in_buf->i_length;
210     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
211 out:
212     block_Release( p_in_buf );
213     return p_out_buf;
214 }
215
216
217 /*****************************************************************************
218  * U8 to F32
219  *****************************************************************************/
220 static block_t *Do_U8ToF32( filter_t * p_filter, block_t * p_in_buf )
221 {
222     block_t *p_out_buf;
223     p_out_buf = filter_NewAudioBuffer( p_filter, 4 * p_in_buf->i_buffer );
224     if( !p_out_buf )
225         goto out;
226
227     int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->fmt_in.audio );
228
229     uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer;
230     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
231
232     while( i-- )
233     {
234         *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
235         p_in++; p_out++;
236     }
237     p_out_buf->i_dts = p_in_buf->i_dts;
238     p_out_buf->i_pts = p_in_buf->i_pts;
239     p_out_buf->i_length = p_in_buf->i_length;
240     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
241 out:
242     block_Release( p_in_buf );
243     return p_out_buf;
244 }
245
246 static block_t *Do_FL32ToF32( filter_t * p_filter, block_t * p_in_buf )
247 {
248     unsigned count = p_in_buf->i_nb_samples
249                    * aout_FormatNbChannels( &p_filter->fmt_in.audio );
250     union dw *restrict p = (union dw *)p_in_buf->p_buffer, *end = p + count;
251     const float one = FIXED32_ONE;
252
253     while (p < end)
254     {
255         p->s = (one * p->f);
256         p++;
257     }
258     return p_in_buf;
259 }
260
261 static block_t *Do_S32ToF32( filter_t * p_filter, block_t * p_in_buf )
262 {
263     unsigned count = p_in_buf->i_nb_samples
264                    * aout_FormatNbChannels( &p_filter->fmt_in.audio );
265     int32_t *restrict p = (int32_t *)p_in_buf->p_buffer, *end = p + count;
266
267     while (p < end)
268     {
269         *p = *p >> 3;
270         p++;
271     }
272     return p_in_buf;
273 }