]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
aout_filter_t.(in|out)put -> aout_filter_t.fmt_(in|out).audio
[vlc] / modules / audio_filter / channel_mixer / simple.c
1 /*****************************************************************************
2  * simple.c : simple channel mixer plug-in
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 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  Create    ( vlc_object_t * );
42 static int  OpenFilter( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_description( N_("Audio filter for simple channel mixing") )
46     set_capability( "audio filter", 10 )
47     set_category( CAT_AUDIO )
48     set_subcategory( SUBCAT_AUDIO_MISC )
49     set_callbacks( Create, NULL )
50
51     add_submodule ()
52     set_description( N_("audio filter for simple channel mixing") )
53     set_capability( "audio filter2", 10 )
54     set_callbacks( OpenFilter, NULL )
55 vlc_module_end ()
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 #define AOUT_CHANS_STEREO_FRONT  ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
61 #define AOUT_CHANS_STEREO_REAR   ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT )
62 #define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT )
63
64 #define AOUT_CHANS_2_0          AOUT_CHANS_STEREO_FRONT
65 #define AOUT_CHANS_3_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER )
66 #define AOUT_CHANS_4_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR )
67 #define AOUT_CHANS_4_0_MIDDLE   ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_MIDDLE )
68 #define AOUT_CHANS_4_CENTER_REAR (AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER)
69 #define AOUT_CHANS_5_0          ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER )
70 #define AOUT_CHANS_5_0_MIDDLE   ( AOUT_CHANS_4_0_MIDDLE | AOUT_CHAN_CENTER )
71 #define AOUT_CHANS_6_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE )
72 #define AOUT_CHANS_7_0          ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER )
73
74 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
75
76 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
77                         aout_buffer_t * );
78
79 static block_t *Filter( filter_t *, block_t * );
80
81 /*****************************************************************************
82  * Create: allocate trivial channel mixer
83  *****************************************************************************/
84 static int Create( vlc_object_t *p_this )
85 {
86     aout_filter_t * p_filter = (aout_filter_t *)p_this;
87
88     if( !IsSupported( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
89         return -1;
90
91     p_filter->pf_do_work = DoWork;
92     p_filter->b_in_place = 0;
93
94     return 0;
95 }
96
97 /*****************************************************************************
98  * DoWork: convert a buffer
99  *****************************************************************************/
100 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
101                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
102 {
103     VLC_UNUSED(p_aout);
104     const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
105
106     const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
107     const bool b_input_5_0 = !b_input_7_0 &&
108                              ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
109                                (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
110     const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
111                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
112     const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
113                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
114
115     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
116     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
117     float *p_dest = (float *)p_out_buf->p_buffer;
118     const float *p_src = (const float *)p_in_buf->p_buffer;
119     int i;
120
121     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
122     p_out_buf->i_buffer = p_in_buf->i_buffer * i_output_nb / i_input_nb;
123
124     if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
125     {
126         if( b_input_7_0 )
127         for( i = p_in_buf->i_nb_samples; i--; )
128         {
129             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
130             p_dest++;
131             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
132             p_dest++;
133
134             p_src += 7;
135
136             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
137         }
138         else if( b_input_5_0 )
139         for( i = p_in_buf->i_nb_samples; i--; )
140         {
141             *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
142             p_dest++;
143             *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
144             p_dest++;
145
146             p_src += 5;
147
148             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
149         }
150         else if( b_input_3_0 )
151         for( i = p_in_buf->i_nb_samples; i--; )
152         {
153             *p_dest = p_src[2] + 0.5 * p_src[0];
154             p_dest++;
155             *p_dest = p_src[2] + 0.5 * p_src[1];
156             p_dest++;
157
158             p_src += 3;
159
160             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
161         }
162         else if (b_input_4_center_rear)
163         for( i = p_in_buf->i_nb_samples; i--; )
164         {
165           *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
166           p_dest++;
167           *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
168           p_dest++;
169           p_src += 4;
170         }
171     }
172     else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
173     {
174         if( b_input_7_0 )
175         for( i = p_in_buf->i_nb_samples; i--; )
176         {
177             *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;
178             p_dest++;
179
180             p_src += 7;
181
182             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
183         }
184         else if( b_input_5_0 )
185         for( i = p_in_buf->i_nb_samples; i--; )
186         {
187             *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
188             p_dest++;
189
190             p_src += 5;
191
192             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
193         }
194         else if( b_input_3_0 )
195         for( i = p_in_buf->i_nb_samples; i--; )
196         {
197             *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
198             p_dest++;
199
200             p_src += 3;
201
202             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
203         }
204         else
205         for( i = p_in_buf->i_nb_samples; i--; )
206         {
207             *p_dest = p_src[0] / 2 + p_src[1] / 2;
208             p_dest++;
209
210             p_src += 2;
211         }
212     }
213     else
214     {
215         assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 );
216         assert( b_input_7_0 || b_input_5_0 );
217
218         if( b_input_7_0 )
219         for( i = p_in_buf->i_nb_samples; i--; )
220         {
221             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
222             p_dest++;
223             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
224             p_dest++;
225             *p_dest = p_src[2] / 6 +  p_src[4];
226             p_dest++;
227             *p_dest = p_src[3] / 6 +  p_src[5];
228             p_dest++;
229
230             p_src += 7;
231
232             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
233         }
234         else
235         for( i = p_in_buf->i_nb_samples; i--; )
236         {
237             *p_dest = p_src[4] + 0.5 * p_src[0];
238             p_dest++;
239             *p_dest = p_src[4] + 0.5 * p_src[1];
240             p_dest++;
241             *p_dest = p_src[2];
242             p_dest++;
243             *p_dest = p_src[3];
244             p_dest++;
245
246             p_src += 5;
247
248             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
249         }
250     }
251 }
252
253 /*****************************************************************************
254  * OpenFilter:
255  *****************************************************************************/
256 static int OpenFilter( vlc_object_t *p_this )
257 {
258     filter_t *p_filter = (filter_t *)p_this;
259
260     audio_format_t fmt_in  = p_filter->fmt_in.audio;
261     audio_format_t fmt_out = p_filter->fmt_out.audio;
262
263     fmt_in.i_format = p_filter->fmt_in.i_codec;
264     fmt_out.i_format = p_filter->fmt_out.i_codec;
265
266     if( !IsSupported( &fmt_in, &fmt_out ) )
267         return -1;
268
269     p_filter->pf_audio_filter = Filter;
270
271     return 0;
272 }
273
274 /*****************************************************************************
275  * Filter:
276  *****************************************************************************/
277 static block_t *Filter( filter_t *p_filter, block_t *p_block )
278 {
279     aout_filter_t aout_filter;
280     aout_buffer_t in_buf, out_buf;
281     block_t *p_out;
282     int i_out_size;
283
284     if( !p_block || !p_block->i_nb_samples )
285     {
286         if( p_block )
287             block_Release( p_block );
288         return NULL;
289     }
290
291     i_out_size = p_block->i_nb_samples *
292       p_filter->fmt_out.audio.i_bitspersample *
293         p_filter->fmt_out.audio.i_channels / 8;
294
295     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
296     if( !p_out )
297     {
298         msg_Warn( p_filter, "can't get output buffer" );
299         block_Release( p_block );
300         return NULL;
301     }
302
303     p_out->i_nb_samples = p_block->i_nb_samples;
304     p_out->i_dts = p_block->i_dts;
305     p_out->i_pts = p_block->i_pts;
306     p_out->i_length = p_block->i_length;
307
308     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
309     aout_filter.fmt_in.audio = p_filter->fmt_in.audio;
310     aout_filter.fmt_in.audio.i_format = p_filter->fmt_in.i_codec;
311     aout_filter.fmt_out.audio = p_filter->fmt_out.audio;
312     aout_filter.fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
313
314     in_buf.p_buffer = p_block->p_buffer;
315     in_buf.i_buffer = p_block->i_buffer;
316     in_buf.i_nb_samples = p_block->i_nb_samples;
317     out_buf.p_buffer = p_out->p_buffer;
318     out_buf.i_buffer = p_out->i_buffer;
319     out_buf.i_nb_samples = p_out->i_nb_samples;
320
321     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
322
323     block_Release( p_block );
324
325     p_out->i_buffer = out_buf.i_buffer;
326     p_out->i_nb_samples = out_buf.i_nb_samples;
327
328     return p_out;
329 }
330
331 /*****************************************************************************
332  * Helpers:
333  *****************************************************************************/
334 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
335 {
336     if( p_input->i_format != VLC_CODEC_FL32 ||
337           p_input->i_format != p_output->i_format ||
338           p_input->i_rate != p_output->i_rate )
339         return false;
340
341     if( p_input->i_physical_channels == p_output->i_physical_channels &&
342         p_input->i_original_channels == p_output->i_original_channels )
343     {
344         return false;
345     }
346
347     /* Only conversion to Mono, Stereo and 4.0 right now */
348     if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
349         p_output->i_physical_channels != AOUT_CHANS_2_0 &&
350         p_output->i_physical_channels != AOUT_CHANS_4_0 )
351     {
352         return false;
353     }
354
355     /* Only from 7/7.1/5/5.1/3/3.1/2.0
356      * XXX 5.X rear and middle are handled the same way */
357     if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
358         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
359         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
360         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
361         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
362          p_input->i_physical_channels != AOUT_CHANS_2_0 )
363     {
364         return false;
365     }
366
367     /* Only if we downmix */
368     if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
369         return false;
370
371     return true;
372 }
373