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