]> git.sesse.net Git - vlc/blob - modules/audio_filter/format.c
* modules/stream_out/transcode.c:
[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
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( _("audio filter for PCM format conversion") );
49     set_capability( "audio filter2", 1 );
50     set_callbacks( Open, NULL );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Open:
55  *****************************************************************************/
56 static int Open( vlc_object_t *p_this )
57 {
58     filter_t *p_filter = (filter_t *)p_this;
59
60     if( p_filter->fmt_in.i_codec == VLC_FOURCC('f','l','3','2') &&
61         p_filter->fmt_out.i_codec == AUDIO_FMT_S16_NE )
62     {
63         p_filter->pf_audio_filter = Float32toS16;
64     }
65     else if ( p_filter->fmt_in.i_codec == VLC_FOURCC('f','l','3','2') &&
66               p_filter->fmt_out.i_codec == AUDIO_FMT_U16_NE )
67     {
68         p_filter->pf_audio_filter = Float32toU16;
69     }
70     else if ( p_filter->fmt_in.i_codec == AUDIO_FMT_S16_NE &&
71               p_filter->fmt_out.i_codec == VLC_FOURCC('f','l','3','2') )
72     {
73         p_filter->pf_audio_filter = S16toFloat32;
74     }
75     else return VLC_EGENERIC;
76     
77     msg_Err( p_this, "%4.4s->%4.4s, bits per sample: %i",
78              (char *)&p_filter->fmt_in.i_codec,
79              (char *)&p_filter->fmt_out.i_codec,
80              p_filter->fmt_in.audio.i_bitspersample );
81
82     return VLC_SUCCESS;
83 }
84
85 /*****************************************************************************
86  * Convert a buffer
87  *****************************************************************************/
88 static block_t *Float32toS16( filter_t *p_filter, block_t *p_block )
89 {
90     int i;
91     float *p_in = (float *)p_block->p_buffer;
92     int16_t *p_out = (int16_t *)p_in;
93
94     for( i = p_block->i_buffer/ p_filter->fmt_in.audio.i_bitspersample; i-- ; )
95     {
96 #if 0
97         /* Slow version. */
98         if ( *p_in >= 1.0 ) *p_out = 32767;
99         else if ( *p_in < -1.0 ) *p_out = -32768;
100         else *p_out = *p_in * 32768.0;
101 #else
102         /* This is walken's trick based on IEEE float format. */
103         union { float f; int32_t i; } u;
104         u.f = *p_in + 384.0;
105         if ( u.i > 0x43c07fff ) *p_out = 32767;
106         else if ( u.i < 0x43bf8000 ) *p_out = -32768;
107         else *p_out = u.i - 0x43c00000;
108 #endif
109         p_in++; p_out++;
110     }
111
112     p_block->i_buffer /= 2;
113     return p_block;
114 }
115
116 static block_t *Float32toU16( filter_t *p_filter, block_t *p_block )
117 {
118     int i;
119     float *p_in = (float *)p_block->p_buffer;
120     uint16_t *p_out = (uint16_t *)p_in;
121
122     for( i = p_block->i_buffer/ p_filter->fmt_in.audio.i_bitspersample; i-- ; )
123     {
124         if ( *p_in >= 1.0 ) *p_out = 65535;
125         else if ( *p_in < -1.0 ) *p_out = 0;
126         else *p_out = (uint16_t)(32768 + *p_in * 32768);
127         p_in++; p_out++;
128     }
129
130     p_block->i_buffer /= 2;
131     return p_block;
132 }
133
134 static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
135 {
136     block_t *p_block_out;
137     int16_t *p_in;
138     float *p_out;
139     int i;
140
141     p_block_out =
142         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
143     if( !p_block_out )
144     {
145         msg_Warn( p_filter, "can't get output buffer" );
146         return NULL;
147     }
148
149     p_in = (int16_t *)(p_block->p_buffer + p_block->i_buffer) - 1;
150     p_out = (float *)(p_block_out->p_buffer + p_block_out->i_buffer) - 1;
151
152     for( i = p_block->i_buffer/ p_filter->fmt_in.audio.i_bitspersample; i-- ; )
153     {
154 #if 0
155         /* Slow version */
156         *p_out = (float)*p_in / 32768.0;
157 #else
158         /* This is walken's trick based on IEEE float format. On my PIII
159          * this takes 16 seconds to perform one billion conversions, instead
160          * of 19 seconds for the above division. */
161         union { float f; int32_t i; } u;
162         u.i = *p_in + 0x43c00000;
163         *p_out = u.f - 384.0;
164 #endif
165
166         p_in--; p_out--;
167     }
168
169     p_block_out->i_samples = p_block->i_samples;
170     p_block_out->i_dts = p_block->i_dts;
171     p_block_out->i_pts = p_block->i_pts;
172     p_block_out->i_length = p_block->i_length;
173     p_block_out->i_rate = p_block->i_rate;
174
175     return p_block_out;
176 }