]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/dolby.c
Cosmetic (no functionnal change).
[vlc] / modules / audio_filter / channel_mixer / dolby.c
1 /*****************************************************************************
2  * dolby.c : simple decoder for dolby surround encoded streams
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Boris Dorès <babal@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 51
21  * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
43                         aout_buffer_t * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 vlc_module_begin ()
49     set_description( N_("Simple decoder for Dolby Surround encoded streams") )
50     set_shortname( N_("Dolby Surround decoder") )
51     set_category( CAT_INPUT )
52     set_subcategory( SUBCAT_INPUT_ACODEC )
53     set_capability( "audio filter", 5 )
54     set_callbacks( Create, Destroy )
55 vlc_module_end ()
56
57 /*****************************************************************************
58  * Internal data structures
59  *****************************************************************************/
60 struct aout_filter_sys_t
61 {
62     int i_left;
63     int i_center;
64     int i_right;
65     int i_rear_left;
66     int i_rear_center;
67     int i_rear_right;
68 };
69
70 /* our internal channel order (WG-4 order) */
71 static const uint32_t pi_channels[] =
72 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
73   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
74   AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
75
76 /*****************************************************************************
77  * Create: allocate headphone downmixer
78  *****************************************************************************/
79 static int Create( vlc_object_t *p_this )
80 {
81     int i = 0;
82     int i_offset = 0;
83     aout_filter_t * p_filter = (aout_filter_t *)p_this;
84     aout_filter_sys_t *p_sys;
85
86     /* Validate audio filter format */
87     if ( p_filter->input.i_physical_channels != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
88        || ! ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
89        || aout_FormatNbChannels( &p_filter->output ) <= 2
90        || ( p_filter->input.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO )
91           != ( p_filter->output.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO ) )
92     {
93         return VLC_EGENERIC;
94     }
95
96     if ( p_filter->input.i_rate != p_filter->output.i_rate )
97     {
98         return VLC_EGENERIC;
99     }
100
101     if ( p_filter->input.i_format != VLC_CODEC_FL32
102           || p_filter->output.i_format != VLC_CODEC_FL32 )
103     {
104         return VLC_EGENERIC;
105     }
106
107     /* Allocate the memory needed to store the module's structure */
108     p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
109     if( p_sys == NULL )
110         return VLC_ENOMEM;
111     p_sys->i_left = -1;
112     p_sys->i_center = -1;
113     p_sys->i_right = -1;
114     p_sys->i_rear_left = -1;
115     p_sys->i_rear_center = -1;
116     p_sys->i_rear_right = -1;
117
118     while ( pi_channels[i] )
119     {
120         if ( p_filter->output.i_physical_channels & pi_channels[i] )
121         {
122             switch ( pi_channels[i] )
123             {
124                 case AOUT_CHAN_LEFT:
125                     p_sys->i_left = i_offset;
126                     break;
127                 case AOUT_CHAN_CENTER:
128                     p_sys->i_center = i_offset;
129                     break;
130                 case AOUT_CHAN_RIGHT:
131                     p_sys->i_right = i_offset;
132                     break;
133                 case AOUT_CHAN_REARLEFT:
134                     p_sys->i_rear_left = i_offset;
135                     break;
136                 case AOUT_CHAN_REARCENTER:
137                     p_sys->i_rear_center = i_offset;
138                     break;
139                 case AOUT_CHAN_REARRIGHT:
140                     p_sys->i_rear_right = i_offset;
141                     break;
142             }
143             ++i_offset;
144         }
145         ++i;
146     }
147
148     p_filter->pf_do_work = DoWork;
149     p_filter->b_in_place = 0;
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Destroy: deallocate resources associated with headphone downmixer
156  *****************************************************************************/
157 static void Destroy( vlc_object_t *p_this )
158 {
159     aout_filter_t * p_filter = (aout_filter_t *)p_this;
160
161     if ( p_filter->p_sys != NULL )
162     {
163         free ( p_filter->p_sys );
164         p_filter->p_sys = NULL;
165     }
166 }
167
168 /*****************************************************************************
169  * DoWork: convert a buffer
170  *****************************************************************************/
171 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
172                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
173 {
174     VLC_UNUSED(p_aout);
175     float * p_in = (float*) p_in_buf->p_buffer;
176     float * p_out = (float*) p_out_buf->p_buffer;
177     size_t i_nb_samples = p_in_buf->i_nb_samples;
178     size_t i_nb_channels = aout_FormatNbChannels( &p_filter->output );
179
180     p_out_buf->i_nb_samples = i_nb_samples;
181     p_out_buf->i_nb_bytes = sizeof(float) * i_nb_samples
182                             * aout_FormatNbChannels( &p_filter->output );
183     memset ( p_out , 0 , p_out_buf->i_nb_bytes );
184
185     if ( p_filter->p_sys != NULL )
186     {
187         struct aout_filter_sys_t * p_sys = p_filter->p_sys;
188         size_t i_nb_rear = 0;
189         size_t i;
190
191         if ( p_sys->i_rear_left >= 0 )
192         {
193             ++i_nb_rear;
194         }
195         if ( p_sys->i_rear_center >= 0 )
196         {
197             ++i_nb_rear;
198         }
199         if ( p_sys->i_rear_right >= 0 )
200         {
201             ++i_nb_rear;
202         }
203
204         for ( i = 0; i < i_nb_samples; ++i )
205         {
206             float f_left = p_in[ i * 2 ];
207             float f_right = p_in[ i * 2 + 1 ];
208             float f_rear = ( f_left - f_right ) / i_nb_rear;
209
210             if ( p_sys->i_center >= 0 )
211             {
212                 float f_center = f_left + f_right;
213                 f_left -= f_center / 2;
214                 f_right -= f_center / 2;
215
216                 p_out[ i * i_nb_channels + p_sys->i_center ] = f_center;
217             }
218
219             if ( p_sys->i_left >= 0 )
220             {
221                 p_out[ i * i_nb_channels + p_sys->i_left ] = f_left;
222             }
223             if ( p_sys->i_right >= 0 )
224             {
225                 p_out[ i * i_nb_channels + p_sys->i_right ] = f_right;
226             }
227             if ( p_sys->i_rear_left >= 0 )
228             {
229                 p_out[ i * i_nb_channels + p_sys->i_rear_left ] = f_rear;
230             }
231             if ( p_sys->i_rear_center >= 0 )
232             {
233                 p_out[ i * i_nb_channels + p_sys->i_rear_center ] = f_rear;
234             }
235             if ( p_sys->i_rear_right >= 0 )
236             {
237                 p_out[ i * i_nb_channels + p_sys->i_rear_right ] = f_rear;
238             }
239         }
240     }
241 }