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 $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/decoder.h>
33 #include "vlc_filter.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
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 * );
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( _("audio filter for PCM format conversion") );
49 set_capability( "audio filter2", 1 );
50 set_callbacks( Open, NULL );
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open( vlc_object_t *p_this )
58 filter_t *p_filter = (filter_t *)p_this;
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 )
63 p_filter->pf_audio_filter = Float32toS16;
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 )
68 p_filter->pf_audio_filter = Float32toU16;
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') )
73 p_filter->pf_audio_filter = S16toFloat32;
75 else return VLC_EGENERIC;
77 msg_Dbg( 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 );
85 /*****************************************************************************
87 *****************************************************************************/
88 static block_t *Float32toS16( filter_t *p_filter, block_t *p_block )
91 float *p_in = (float *)p_block->p_buffer;
92 int16_t *p_out = (int16_t *)p_in;
94 for( i = p_block->i_buffer/ p_filter->fmt_in.audio.i_bitspersample; i-- ; )
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;
102 /* This is walken's trick based on IEEE float format. */
103 union { float f; int32_t i; } u;
105 if ( u.i > 0x43c07fff ) *p_out = 32767;
106 else if ( u.i < 0x43bf8000 ) *p_out = -32768;
107 else *p_out = u.i - 0x43c00000;
112 p_block->i_buffer /= 2;
116 static block_t *Float32toU16( filter_t *p_filter, block_t *p_block )
119 float *p_in = (float *)p_block->p_buffer;
120 uint16_t *p_out = (uint16_t *)p_in;
122 for( i = p_block->i_buffer/ p_filter->fmt_in.audio.i_bitspersample; i-- ; )
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);
130 p_block->i_buffer /= 2;
134 static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
136 block_t *p_block_out;
142 p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
145 msg_Warn( p_filter, "can't get output buffer" );
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;
152 for( i = p_block->i_buffer/ p_filter->fmt_in.audio.i_bitspersample; i-- ; )
156 *p_out = (float)*p_in / 32768.0;
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;
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;