]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/dolby.c
60819f6a3d2afb477b637dc8ea62878fec57fa9f
[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 #include <vlc_filter.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static block_t *DoWork( filter_t *, block_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 converter", 5 )
54     set_callbacks( Create, Destroy )
55 vlc_module_end ()
56
57 /*****************************************************************************
58  * Internal data structures
59  *****************************************************************************/
60 struct 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 /*****************************************************************************
71  * Create: allocate headphone downmixer
72  *****************************************************************************/
73 static int Create( vlc_object_t *p_this )
74 {
75     int i = 0;
76     int i_offset = 0;
77     filter_t * p_filter = (filter_t *)p_this;
78     filter_sys_t *p_sys;
79
80     /* Validate audio filter format */
81     if ( p_filter->fmt_in.audio.i_physical_channels != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
82        || ! ( p_filter->fmt_in.audio.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
83        || aout_FormatNbChannels( &p_filter->fmt_out.audio ) <= 2
84        || ( p_filter->fmt_in.audio.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO )
85           != ( p_filter->fmt_out.audio.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO ) )
86     {
87         return VLC_EGENERIC;
88     }
89
90     if ( p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate )
91     {
92         return VLC_EGENERIC;
93     }
94
95     if ( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32
96           || p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
97     {
98         return VLC_EGENERIC;
99     }
100
101     /* Allocate the memory needed to store the module's structure */
102     p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
103     if( p_sys == NULL )
104         return VLC_ENOMEM;
105     p_sys->i_left = -1;
106     p_sys->i_center = -1;
107     p_sys->i_right = -1;
108     p_sys->i_rear_left = -1;
109     p_sys->i_rear_center = -1;
110     p_sys->i_rear_right = -1;
111
112     while ( pi_vlc_chan_order_wg4[i] )
113     {
114         if ( p_filter->fmt_out.audio.i_physical_channels & pi_vlc_chan_order_wg4[i] )
115         {
116             switch ( pi_vlc_chan_order_wg4[i] )
117             {
118                 case AOUT_CHAN_LEFT:
119                     p_sys->i_left = i_offset;
120                     break;
121                 case AOUT_CHAN_CENTER:
122                     p_sys->i_center = i_offset;
123                     break;
124                 case AOUT_CHAN_RIGHT:
125                     p_sys->i_right = i_offset;
126                     break;
127                 case AOUT_CHAN_REARLEFT:
128                     p_sys->i_rear_left = i_offset;
129                     break;
130                 case AOUT_CHAN_REARCENTER:
131                     p_sys->i_rear_center = i_offset;
132                     break;
133                 case AOUT_CHAN_REARRIGHT:
134                     p_sys->i_rear_right = i_offset;
135                     break;
136             }
137             ++i_offset;
138         }
139         ++i;
140     }
141
142     p_filter->pf_audio_filter = DoWork;
143
144     return VLC_SUCCESS;
145 }
146
147 /*****************************************************************************
148  * Destroy: deallocate resources associated with headphone downmixer
149  *****************************************************************************/
150 static void Destroy( vlc_object_t *p_this )
151 {
152     filter_t * p_filter = (filter_t *)p_this;
153     free( p_filter->p_sys );
154 }
155
156 /*****************************************************************************
157  * DoWork: convert a buffer
158  *****************************************************************************/
159 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
160 {
161     filter_sys_t * p_sys = p_filter->p_sys;
162     float * p_in = (float*) p_in_buf->p_buffer;
163     size_t i_nb_samples = p_in_buf->i_nb_samples;
164     size_t i_nb_channels = aout_FormatNbChannels( &p_filter->fmt_out.audio );
165     size_t i_nb_rear = 0;
166     size_t i;
167     block_t *p_out_buf = block_Alloc(
168                                 sizeof(float) * i_nb_samples * i_nb_channels );
169     if( !p_out_buf )
170         goto out;
171
172     float * p_out = (float*) p_out_buf->p_buffer;
173     p_out_buf->i_nb_samples = i_nb_samples;
174     p_out_buf->i_dts        = p_in_buf->i_dts;
175     p_out_buf->i_pts        = p_in_buf->i_pts;
176     p_out_buf->i_length     = p_in_buf->i_length;
177
178     memset( p_out, 0, p_out_buf->i_buffer );
179
180     if( p_sys->i_rear_left >= 0 )
181     {
182         ++i_nb_rear;
183     }
184     if( p_sys->i_rear_center >= 0 )
185     {
186         ++i_nb_rear;
187     }
188     if( p_sys->i_rear_right >= 0 )
189     {
190         ++i_nb_rear;
191     }
192
193     for( i = 0; i < i_nb_samples; ++i )
194     {
195         float f_left = p_in[ i * 2 ];
196         float f_right = p_in[ i * 2 + 1 ];
197         float f_rear = ( f_left - f_right ) / i_nb_rear;
198
199         if( p_sys->i_center >= 0 )
200         {
201             float f_center = f_left + f_right;
202             f_left -= f_center / 2;
203             f_right -= f_center / 2;
204
205             p_out[ i * i_nb_channels + p_sys->i_center ] = f_center;
206         }
207
208         if( p_sys->i_left >= 0 )
209         {
210             p_out[ i * i_nb_channels + p_sys->i_left ] = f_left;
211         }
212         if( p_sys->i_right >= 0 )
213         {
214             p_out[ i * i_nb_channels + p_sys->i_right ] = f_right;
215         }
216         if( p_sys->i_rear_left >= 0 )
217         {
218             p_out[ i * i_nb_channels + p_sys->i_rear_left ] = f_rear;
219         }
220         if( p_sys->i_rear_center >= 0 )
221         {
222             p_out[ i * i_nb_channels + p_sys->i_rear_center ] = f_rear;
223         }
224         if( p_sys->i_rear_right >= 0 )
225         {
226             p_out[ i * i_nb_channels + p_sys->i_rear_right ] = f_rear;
227         }
228     }
229 out:
230     block_Release( p_in_buf );
231     return p_out_buf;
232 }