]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / audio_filter / channel_mixer / simple.c
1 /*****************************************************************************
2  * simple.c : simple channel mixer plug-in
3  *****************************************************************************
4  * Copyright (C) 2002, 2004, 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_aout.h>
34 #include <vlc_filter.h>
35 #include <vlc_block.h>
36 #include <assert.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  OpenFilter( vlc_object_t * );
42
43 vlc_module_begin ()
44     set_description( N_("Audio filter for simple channel mixing") )
45     set_category( CAT_AUDIO )
46     set_subcategory( SUBCAT_AUDIO_MISC )
47     set_capability( "audio filter", 10 )
48     set_callbacks( OpenFilter, NULL )
49 vlc_module_end ()
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 #define AOUT_CHANS_STEREO_FRONT  ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
55 #define AOUT_CHANS_STEREO_REAR   ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT )
56 #define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT )
57
58 #define AOUT_CHANS_2_0          AOUT_CHANS_STEREO_FRONT
59 #define AOUT_CHANS_3_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER )
60 #define AOUT_CHANS_4_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR )
61 #define AOUT_CHANS_4_0_MIDDLE   ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_MIDDLE )
62 #define AOUT_CHANS_4_CENTER_REAR (AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER)
63 #define AOUT_CHANS_5_0          ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER )
64 #define AOUT_CHANS_5_0_MIDDLE   ( AOUT_CHANS_4_0_MIDDLE | AOUT_CHAN_CENTER )
65 #define AOUT_CHANS_6_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE )
66 #define AOUT_CHANS_7_0          ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER )
67
68 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
69
70 static block_t *Filter( filter_t *, block_t * );
71
72 /*****************************************************************************
73  * DoWork: convert a buffer
74  *****************************************************************************/
75 static void DoWork( filter_t * p_filter,
76                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
77 {
78     const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
79
80     const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
81     const bool b_input_5_0 = !b_input_7_0 &&
82                              ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
83                                (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
84     const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
85                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
86     const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
87                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
88
89     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
90     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
91     float *p_dest = (float *)p_out_buf->p_buffer;
92     const float *p_src = (const float *)p_in_buf->p_buffer;
93     int i;
94
95     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
96     p_out_buf->i_buffer = p_in_buf->i_buffer * i_output_nb / i_input_nb;
97
98     if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
99     {
100         if( b_input_7_0 )
101         for( i = p_in_buf->i_nb_samples; i--; )
102         {
103             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
104             p_dest++;
105             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
106             p_dest++;
107
108             p_src += 7;
109
110             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
111         }
112         else if( b_input_5_0 )
113         for( i = p_in_buf->i_nb_samples; i--; )
114         {
115             *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
116             p_dest++;
117             *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
118             p_dest++;
119
120             p_src += 5;
121
122             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
123         }
124         else if( b_input_3_0 )
125         for( i = p_in_buf->i_nb_samples; i--; )
126         {
127             *p_dest = p_src[2] + 0.5 * p_src[0];
128             p_dest++;
129             *p_dest = p_src[2] + 0.5 * p_src[1];
130             p_dest++;
131
132             p_src += 3;
133
134             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
135         }
136         else if (b_input_4_center_rear)
137         for( i = p_in_buf->i_nb_samples; i--; )
138         {
139           *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
140           p_dest++;
141           *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
142           p_dest++;
143           p_src += 4;
144         }
145     }
146     else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
147     {
148         if( b_input_7_0 )
149         for( i = p_in_buf->i_nb_samples; i--; )
150         {
151             *p_dest = p_src[6] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 8 + p_src[3] / 8 + p_src[4] / 8 + p_src[5] / 8;
152             p_dest++;
153
154             p_src += 7;
155
156             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
157         }
158         else if( b_input_5_0 )
159         for( i = p_in_buf->i_nb_samples; i--; )
160         {
161             *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
162             p_dest++;
163
164             p_src += 5;
165
166             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
167         }
168         else if( b_input_3_0 )
169         for( i = p_in_buf->i_nb_samples; i--; )
170         {
171             *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
172             p_dest++;
173
174             p_src += 3;
175
176             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
177         }
178         else
179         for( i = p_in_buf->i_nb_samples; i--; )
180         {
181             *p_dest = p_src[0] / 2 + p_src[1] / 2;
182             p_dest++;
183
184             p_src += 2;
185         }
186     }
187     else
188     {
189         assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 );
190         assert( b_input_7_0 || b_input_5_0 );
191
192         if( b_input_7_0 )
193         for( i = p_in_buf->i_nb_samples; i--; )
194         {
195             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
196             p_dest++;
197             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
198             p_dest++;
199             *p_dest = p_src[2] / 6 +  p_src[4];
200             p_dest++;
201             *p_dest = p_src[3] / 6 +  p_src[5];
202             p_dest++;
203
204             p_src += 7;
205
206             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
207         }
208         else
209         for( i = p_in_buf->i_nb_samples; i--; )
210         {
211             *p_dest = p_src[4] + 0.5 * p_src[0];
212             p_dest++;
213             *p_dest = p_src[4] + 0.5 * p_src[1];
214             p_dest++;
215             *p_dest = p_src[2];
216             p_dest++;
217             *p_dest = p_src[3];
218             p_dest++;
219
220             p_src += 5;
221
222             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
223         }
224     }
225 }
226
227 /*****************************************************************************
228  * OpenFilter:
229  *****************************************************************************/
230 static int OpenFilter( vlc_object_t *p_this )
231 {
232     filter_t *p_filter = (filter_t *)p_this;
233
234     audio_format_t fmt_in  = p_filter->fmt_in.audio;
235     audio_format_t fmt_out = p_filter->fmt_out.audio;
236
237     fmt_in.i_format = p_filter->fmt_in.i_codec;
238     fmt_out.i_format = p_filter->fmt_out.i_codec;
239
240     if( !IsSupported( &fmt_in, &fmt_out ) )
241         return -1;
242
243     p_filter->pf_audio_filter = Filter;
244
245     return 0;
246 }
247
248 /*****************************************************************************
249  * Filter:
250  *****************************************************************************/
251 static block_t *Filter( filter_t *p_filter, block_t *p_block )
252 {
253     if( !p_block || !p_block->i_nb_samples )
254     {
255         if( p_block )
256             block_Release( p_block );
257         return NULL;
258     }
259
260     size_t i_out_size = p_block->i_nb_samples *
261       p_filter->fmt_out.audio.i_bitspersample *
262         p_filter->fmt_out.audio.i_channels / 8;
263
264     block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
265     if( !p_out )
266     {
267         msg_Warn( p_filter, "can't get output buffer" );
268         block_Release( p_block );
269         return NULL;
270     }
271
272     p_out->i_nb_samples = p_block->i_nb_samples;
273     p_out->i_dts = p_block->i_dts;
274     p_out->i_pts = p_block->i_pts;
275     p_out->i_length = p_block->i_length;
276
277     DoWork( p_filter, p_block, p_out );
278
279     block_Release( p_block );
280
281     return p_out;
282 }
283
284 /*****************************************************************************
285  * Helpers:
286  *****************************************************************************/
287 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
288 {
289     if( p_input->i_format != VLC_CODEC_FL32 ||
290           p_input->i_format != p_output->i_format ||
291           p_input->i_rate != p_output->i_rate )
292         return false;
293
294     if( p_input->i_physical_channels == p_output->i_physical_channels &&
295         p_input->i_original_channels == p_output->i_original_channels )
296     {
297         return false;
298     }
299
300     /* Only conversion to Mono, Stereo and 4.0 right now */
301     if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
302         p_output->i_physical_channels != AOUT_CHANS_2_0 &&
303         p_output->i_physical_channels != AOUT_CHANS_4_0 )
304     {
305         return false;
306     }
307
308     /* Only from 7/7.1/5/5.1/3/3.1/2.0
309      * XXX 5.X rear and middle are handled the same way */
310     if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
311         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
312         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
313         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
314         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
315          p_input->i_physical_channels != AOUT_CHANS_2_0 )
316     {
317         return false;
318     }
319
320     /* Only if we downmix */
321     if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
322         return false;
323
324     return true;
325 }
326