1 /*****************************************************************************
2 * dolby.c : simple decoder for dolby surround encoded streams
3 *****************************************************************************
4 * Copyright (C) 2005, 2006 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 *****************************************************************************/
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Create ( vlc_object_t * );
39 static void Destroy ( vlc_object_t * );
41 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( _("Simple decoder for Dolby Surround encoded streams") );
49 set_shortname( _("Dolby Surround decoder") );
50 set_category( CAT_INPUT );
51 set_subcategory( SUBCAT_INPUT_ACODEC );
52 set_capability( "audio filter", 5 );
53 set_callbacks( Create, Destroy );
56 /*****************************************************************************
57 * Internal data structures
58 *****************************************************************************/
59 struct aout_filter_sys_t
69 /* our internal channel order (WG-4 order) */
70 static const uint32_t pi_channels[] =
71 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
72 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
73 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
75 /*****************************************************************************
76 * Create: allocate headphone downmixer
77 *****************************************************************************/
78 static int Create( vlc_object_t *p_this )
82 aout_filter_t * p_filter = (aout_filter_t *)p_this;
84 /* Validate audio filter format */
85 if ( p_filter->input.i_physical_channels != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
86 || ! ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
87 || aout_FormatNbChannels( &p_filter->output ) <= 2
88 || ( p_filter->input.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO )
89 != ( p_filter->output.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO ) )
94 if ( p_filter->input.i_rate != p_filter->output.i_rate )
99 if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
100 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
105 /* Allocate the memory needed to store the module's structure */
106 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
107 if ( p_filter->p_sys == NULL )
109 msg_Err( p_filter, "out of memory" );
112 p_filter->p_sys->i_left = -1;
113 p_filter->p_sys->i_center = -1;
114 p_filter->p_sys->i_right = -1;
115 p_filter->p_sys->i_rear_left = -1;
116 p_filter->p_sys->i_rear_center = -1;
117 p_filter->p_sys->i_rear_right = -1;
119 while ( pi_channels[i] )
121 if ( p_filter->output.i_physical_channels & pi_channels[i] )
123 switch ( pi_channels[i] )
126 p_filter->p_sys->i_left = i_offset;
128 case AOUT_CHAN_CENTER:
129 p_filter->p_sys->i_center = i_offset;
131 case AOUT_CHAN_RIGHT:
132 p_filter->p_sys->i_right = i_offset;
134 case AOUT_CHAN_REARLEFT:
135 p_filter->p_sys->i_rear_left = i_offset;
137 case AOUT_CHAN_REARCENTER:
138 p_filter->p_sys->i_rear_center = i_offset;
140 case AOUT_CHAN_REARRIGHT:
141 p_filter->p_sys->i_rear_right = i_offset;
149 p_filter->pf_do_work = DoWork;
150 p_filter->b_in_place = 0;
155 /*****************************************************************************
156 * Destroy: deallocate resources associated with headphone downmixer
157 *****************************************************************************/
158 static void Destroy( vlc_object_t *p_this )
160 aout_filter_t * p_filter = (aout_filter_t *)p_this;
162 if ( p_filter->p_sys != NULL )
164 free ( p_filter->p_sys );
165 p_filter->p_sys = NULL;
169 /*****************************************************************************
170 * DoWork: convert a buffer
171 *****************************************************************************/
172 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
173 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
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 );
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 );
185 if ( p_filter->p_sys != NULL )
187 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
188 size_t i_nb_rear = 0;
191 if ( p_sys->i_rear_left >= 0 )
195 if ( p_sys->i_rear_center >= 0 )
199 if ( p_sys->i_rear_right >= 0 )
204 for ( i = 0; i < i_nb_samples; ++i )
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;
210 if ( p_sys->i_center >= 0 )
212 float f_center = f_left + f_right;
213 f_left -= f_center / 2;
214 f_right -= f_center / 2;
216 p_out[ i * i_nb_channels + p_sys->i_center ] = f_center;
219 if ( p_sys->i_left >= 0 )
221 p_out[ i * i_nb_channels + p_sys->i_left ] = f_left;
223 if ( p_sys->i_right >= 0 )
225 p_out[ i * i_nb_channels + p_sys->i_right ] = f_right;
227 if ( p_sys->i_rear_left >= 0 )
229 p_out[ i * i_nb_channels + p_sys->i_rear_left ] = f_rear;
231 if ( p_sys->i_rear_center >= 0 )
233 p_out[ i * i_nb_channels + p_sys->i_rear_center ] = f_rear;
235 if ( p_sys->i_rear_right >= 0 )
237 p_out[ i * i_nb_channels + p_sys->i_rear_right ] = f_rear;