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