]> git.sesse.net Git - vlc/blob - modules/audio_filter/format.c
* modules/stream_out/transcode.c, modules/audio_filter/format.c: fixed a bunch of...
[vlc] / modules / audio_filter / format.c
1 /*****************************************************************************
2  * format.c : PCM format converter
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: float32tos16.c 8391 2004-08-06 17:28:36Z sam $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include "vlc_filter.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Open ( vlc_object_t * );
39
40 static block_t *Float32toS16( filter_t *, block_t * );
41 static block_t *Float32toU16( filter_t *, block_t * );
42 static block_t *S16toFloat32( filter_t *, block_t * );
43 static block_t *S16Invert   ( filter_t *, block_t * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 vlc_module_begin();
49     set_description( _("audio filter for PCM format conversion") );
50     set_capability( "audio filter2", 1 );
51     set_callbacks( Open, NULL );
52 vlc_module_end();
53
54 /*****************************************************************************
55  * Open:
56  *****************************************************************************/
57 static int Open( vlc_object_t *p_this )
58 {
59     filter_t *p_filter = (filter_t *)p_this;
60
61     if( p_filter->fmt_in.i_codec == VLC_FOURCC('f','l','3','2') &&
62         p_filter->fmt_out.i_codec == AUDIO_FMT_S16_NE )
63     {
64         p_filter->pf_audio_filter = Float32toS16;
65     }
66     else if( p_filter->fmt_in.i_codec == VLC_FOURCC('f','l','3','2') &&
67              p_filter->fmt_out.i_codec == AUDIO_FMT_U16_NE )
68     {
69         p_filter->pf_audio_filter = Float32toU16;
70     }
71     else if( p_filter->fmt_in.i_codec == AUDIO_FMT_S16_NE &&
72              p_filter->fmt_out.i_codec == VLC_FOURCC('f','l','3','2') )
73     {
74         p_filter->pf_audio_filter = S16toFloat32;
75     }
76     else if( ( p_filter->fmt_in.i_codec == VLC_FOURCC('s','1','6','l') &&
77                p_filter->fmt_out.i_codec == VLC_FOURCC('s','1','6','b') ) ||
78              ( p_filter->fmt_in.i_codec == VLC_FOURCC('s','1','6','b') &&
79                p_filter->fmt_out.i_codec == VLC_FOURCC('s','1','6','l') ) )
80     {
81         p_filter->pf_audio_filter = S16Invert;
82     }
83     else return VLC_EGENERIC;
84     
85     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
86              (char *)&p_filter->fmt_in.i_codec,
87              (char *)&p_filter->fmt_out.i_codec,
88              p_filter->fmt_in.audio.i_bitspersample );
89
90     return VLC_SUCCESS;
91 }
92
93 /*****************************************************************************
94  * Convert a buffer
95  *****************************************************************************/
96 static block_t *Float32toS16( filter_t *p_filter, block_t *p_block )
97 {
98     int i;
99     float *p_in = (float *)p_block->p_buffer;
100     int16_t *p_out = (int16_t *)p_in;
101
102     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
103     {
104 #if 0
105         /* Slow version. */
106         if ( *p_in >= 1.0 ) *p_out = 32767;
107         else if ( *p_in < -1.0 ) *p_out = -32768;
108         else *p_out = *p_in * 32768.0;
109 #else
110         /* This is walken's trick based on IEEE float format. */
111         union { float f; int32_t i; } u;
112         u.f = *p_in + 384.0;
113         if ( u.i > 0x43c07fff ) *p_out = 32767;
114         else if ( u.i < 0x43bf8000 ) *p_out = -32768;
115         else *p_out = u.i - 0x43c00000;
116 #endif
117         p_in++; p_out++;
118     }
119
120     p_block->i_buffer /= 2;
121     return p_block;
122 }
123
124 static block_t *Float32toU16( filter_t *p_filter, block_t *p_block )
125 {
126     int i;
127     float *p_in = (float *)p_block->p_buffer;
128     uint16_t *p_out = (uint16_t *)p_in;
129
130     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
131     {
132         if ( *p_in >= 1.0 ) *p_out = 65535;
133         else if ( *p_in < -1.0 ) *p_out = 0;
134         else *p_out = (uint16_t)(32768 + *p_in * 32768);
135         p_in++; p_out++;
136     }
137
138     p_block->i_buffer /= 2;
139     return p_block;
140 }
141
142 static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
143 {
144     block_t *p_block_out;
145     int16_t *p_in;
146     float *p_out;
147     int i;
148
149     p_block_out =
150         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
151     if( !p_block_out )
152     {
153         msg_Warn( p_filter, "can't get output buffer" );
154         return NULL;
155     }
156
157     p_in = (int16_t *)p_block->p_buffer;
158     p_out = (float *)p_block_out->p_buffer;
159
160     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
161     {
162 #if 0
163         /* Slow version */
164         *p_out = (float)*p_in / 32768.0;
165 #else
166         /* This is walken's trick based on IEEE float format. On my PIII
167          * this takes 16 seconds to perform one billion conversions, instead
168          * of 19 seconds for the above division. */
169         union { float f; int32_t i; } u;
170         u.i = *p_in + 0x43c00000;
171         *p_out = u.f - 384.0;
172 #endif
173
174         p_in++; p_out++;
175     }
176
177     p_block_out->i_samples = p_block->i_samples;
178     p_block_out->i_dts = p_block->i_dts;
179     p_block_out->i_pts = p_block->i_pts;
180     p_block_out->i_length = p_block->i_length;
181     p_block_out->i_rate = p_block->i_rate;
182
183     p_block->pf_release( p_block );
184     return p_block_out;
185 }
186
187 static block_t *S16Invert( filter_t *p_filter, block_t *p_block )
188 {
189     int i;
190     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
191     uint8_t tmp;
192
193     for( i = 0; i < p_block->i_buffer / 2; i++ )
194     {
195         tmp = p_in[0];
196         p_in[0] = p_in[1];
197         p_in[1] = tmp;
198         p_in += 2;
199     }
200
201     return p_block;
202 }