1 /*****************************************************************************
2 * dolby.c : simple decoder for dolby surround encoded streams
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Boris Dorès <babal@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include "audio_output.h"
32 #include "aout_internal.h"
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Create ( vlc_object_t * );
38 static void Destroy ( vlc_object_t * );
40 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
43 /*****************************************************************************
45 *****************************************************************************/
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 );
55 /*****************************************************************************
56 * Internal data structures
57 *****************************************************************************/
58 struct aout_filter_sys_t
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 };
74 /*****************************************************************************
75 * Create: allocate headphone downmixer
76 *****************************************************************************/
77 static int Create( vlc_object_t *p_this )
81 aout_filter_t * p_filter = (aout_filter_t *)p_this;
83 /* Validate audio filter format */
84 if ( p_filter->input.i_physical_channels != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
85 || ! ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
86 || aout_FormatNbChannels( &p_filter->output ) <= 2
87 || ( p_filter->input.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO )
88 != ( p_filter->output.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO ) )
93 if ( p_filter->input.i_rate != p_filter->output.i_rate )
98 if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
99 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
104 /* Allocate the memory needed to store the module's structure */
105 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
106 if ( p_filter->p_sys == NULL )
108 msg_Err( p_filter, "Out of memory" );
111 p_filter->p_sys->i_left = -1;
112 p_filter->p_sys->i_center = -1;
113 p_filter->p_sys->i_right = -1;
114 p_filter->p_sys->i_rear_left = -1;
115 p_filter->p_sys->i_rear_center = -1;
116 p_filter->p_sys->i_rear_right = -1;
118 while ( pi_channels[i] )
120 if ( p_filter->output.i_physical_channels & pi_channels[i] )
122 switch ( pi_channels[i] )
125 p_filter->p_sys->i_left = i_offset;
127 case AOUT_CHAN_CENTER:
128 p_filter->p_sys->i_center = i_offset;
130 case AOUT_CHAN_RIGHT:
131 p_filter->p_sys->i_right = i_offset;
133 case AOUT_CHAN_REARLEFT:
134 p_filter->p_sys->i_rear_left = i_offset;
136 case AOUT_CHAN_REARCENTER:
137 p_filter->p_sys->i_rear_center = i_offset;
139 case AOUT_CHAN_REARRIGHT:
140 p_filter->p_sys->i_rear_right = i_offset;
148 p_filter->pf_do_work = DoWork;
149 p_filter->b_in_place = 0;
154 /*****************************************************************************
155 * Destroy: deallocate resources associated with headphone downmixer
156 *****************************************************************************/
157 static void Destroy( vlc_object_t *p_this )
159 aout_filter_t * p_filter = (aout_filter_t *)p_this;
161 if ( p_filter->p_sys != NULL )
163 free ( p_filter->p_sys );
164 p_filter->p_sys = NULL;
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 )
174 float * p_in = (float*) p_in_buf->p_buffer;
175 float * p_out = (float*) p_out_buf->p_buffer;
176 size_t i_nb_samples = p_in_buf->i_nb_samples;
177 size_t i_nb_channels = aout_FormatNbChannels( &p_filter->output );
179 p_out_buf->i_nb_samples = i_nb_samples;
180 p_out_buf->i_nb_bytes = sizeof(float) * i_nb_samples
181 * aout_FormatNbChannels( &p_filter->output );
182 memset ( p_out , 0 , p_out_buf->i_nb_bytes );
184 if ( p_filter->p_sys != NULL )
186 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
187 size_t i_nb_rear = 0;
190 if ( p_sys->i_rear_left >= 0 )
194 if ( p_sys->i_rear_center >= 0 )
198 if ( p_sys->i_rear_right >= 0 )
203 for ( i = 0; i < i_nb_samples; ++i )
205 float f_left = p_in[ i * 2 ];
206 float f_right = p_in[ i * 2 + 1 ];
207 float f_rear = ( f_left - f_right ) / i_nb_rear;
209 if ( p_sys->i_center >= 0 )
211 float f_center = f_left + f_right;
212 f_left -= f_center / 2;
213 f_right -= f_center / 2;
215 p_out[ i * i_nb_channels + p_sys->i_center ] = f_center;
218 if ( p_sys->i_left >= 0 )
220 p_out[ i * i_nb_channels + p_sys->i_left ] = f_left;
222 if ( p_sys->i_right >= 0 )
224 p_out[ i * i_nb_channels + p_sys->i_right ] = f_right;
226 if ( p_sys->i_rear_left >= 0 )
228 p_out[ i * i_nb_channels + p_sys->i_rear_left ] = f_rear;
230 if ( p_sys->i_rear_center >= 0 )
232 p_out[ i * i_nb_channels + p_sys->i_rear_center ] = f_rear;
234 if ( p_sys->i_rear_right >= 0 )
236 p_out[ i * i_nb_channels + p_sys->i_rear_right ] = f_rear;