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