]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/float.c
562fcd3aa872909893bcf0a47d5543bf06ecf07a
[vlc] / modules / audio_filter / converter / float.c
1 /*****************************************************************************
2  * float.c: Floating 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  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Samuel Hocevar <sam@zoy.org>
10  *          Xavier Maillard <zedek@fxgsproject.org>
11  *          Henri Fallon <henri@videolan.org>
12  *          Gildas Bazin <gbazin@netcourrier.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #include <vlc_aout.h>
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  Create_F32ToFL32 ( vlc_object_t * );
49 static void Do_F32ToFL32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
50                            aout_buffer_t * );
51 static void Do_FL32ToF32 ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
52                            aout_buffer_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin ()
58     set_description( N_("Floating-point audio format conversions") )
59     add_submodule ()
60         set_capability( "audio filter", 10 )
61         set_callbacks( Create_F32ToFL32, NULL )
62 vlc_module_end ()
63
64 /*****************************************************************************
65  * Fixed 32 to Float 32 and backwards
66  *****************************************************************************/
67 static int Create_F32ToFL32( vlc_object_t *p_this )
68 {
69     aout_filter_t * p_filter = (aout_filter_t *)p_this;
70
71     if( ( p_filter->fmt_in.audio.i_format != VLC_CODEC_FI32
72            || p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
73       && ( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32
74             || p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32 ) )
75     {
76         return -1;
77     }
78
79     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
80     {
81         return -1;
82     }
83
84     if( p_filter->fmt_in.audio.i_format == VLC_CODEC_FI32 )
85     {
86         p_filter->pf_do_work = Do_F32ToFL32;
87     }
88     else
89     {
90         p_filter->pf_do_work = Do_FL32ToF32;
91     }
92
93     p_filter->b_in_place = 1;
94
95     return 0;
96 }
97
98 static void Do_F32ToFL32( aout_instance_t * p_aout, aout_filter_t * p_filter,
99                           aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
100 {
101     VLC_UNUSED(p_aout);
102     int i;
103     vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
104     float * p_out = (float *)p_out_buf->p_buffer;
105
106     for ( i = p_in_buf->i_nb_samples
107                * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
108     {
109         *p_out++ = (float)*p_in++ / (float)FIXED32_ONE;
110     }
111
112     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
113     p_out_buf->i_buffer = p_in_buf->i_buffer;
114 }
115
116 static void Do_FL32ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
117                           aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
118 {
119     VLC_UNUSED(p_aout);
120     int i;
121     float * p_in = (float *)p_in_buf->p_buffer;
122     vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer;
123
124     for ( i = p_in_buf->i_nb_samples
125                * aout_FormatNbChannels( &p_filter->fmt_in.audio ) ; i-- ; )
126     {
127         *p_out++ = (vlc_fixed_t)( *p_in++ * (float)FIXED32_ONE );
128     }
129
130     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
131     p_out_buf->i_buffer = p_in_buf->i_buffer;
132 }