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