simple.c 11.5 KB
Newer Older
1
/*****************************************************************************
2
 * simple.c : simple channel mixer plug-in
3
 *****************************************************************************
4
 * Copyright (C) 2002, 2004, 2006-2009 the VideoLAN team
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27
28
29
30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
33
#include <vlc_aout.h>
34
35
#include <vlc_filter.h>
#include <vlc_block.h>
36
#include <assert.h>
37
38

/*****************************************************************************
39
 * Module descriptor
40
 *****************************************************************************/
41
static int  OpenFilter( vlc_object_t * );
42

43
44
45
46
vlc_module_begin ()
    set_description( N_("Audio filter for simple channel mixing") )
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_MISC )
47
    set_capability( "audio filter", 10 )
48
49
    set_callbacks( OpenFilter, NULL )
vlc_module_end ()
50

51
52
53
54
55
56
57
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define AOUT_CHANS_STEREO_FRONT  ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
#define AOUT_CHANS_STEREO_REAR   ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT )
#define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT )

58
#define AOUT_CHANS_2_0          AOUT_CHANS_STEREO_FRONT
59
#define AOUT_CHANS_3_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER )
60
61
#define AOUT_CHANS_4_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR )
#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
64
65
66
#define AOUT_CHANS_5_0          ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER )
#define AOUT_CHANS_5_0_MIDDLE   ( AOUT_CHANS_4_0_MIDDLE | AOUT_CHAN_CENTER )
#define AOUT_CHANS_6_0          ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE )
#define AOUT_CHANS_7_0          ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER )
67
68
69
70
71

static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );

static block_t *Filter( filter_t *, block_t * );

72
73
74
/*****************************************************************************
 * DoWork: convert a buffer
 *****************************************************************************/
75
static void DoWork( filter_t * p_filter,
76
77
                    aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
78
    const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
79
80
81

    const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
    const bool b_input_5_0 = !b_input_7_0 &&
82
83
                             ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
                               (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
84
85
86
    const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
                             (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
    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
90
    int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
    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
94
95
    int i;

    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
102
103
104
105
106
107
108
109
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
            p_dest++;
            *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
            p_dest++;

            p_src += 7;

110
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
111
        }
112
        else if( b_input_5_0 )
113
114
115
116
117
118
119
120
121
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
            p_dest++;
            *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
            p_dest++;

            p_src += 5;

122
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
123
124
125
126
127
128
129
130
131
132
133
        }
        else if( b_input_3_0 )
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[2] + 0.5 * p_src[0];
            p_dest++;
            *p_dest = p_src[2] + 0.5 * p_src[1];
            p_dest++;

            p_src += 3;

134
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
135
        }
136
137
138
139
140
141
142
143
144
        else if (b_input_4_center_rear)
        for( i = p_in_buf->i_nb_samples; i--; )
        {
          *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
          p_dest++;
          *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
          p_dest++;
          p_src += 4;
        }
145
    }
146
    else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
147
    {
148
        if( b_input_7_0 )
149
150
151
152
153
154
155
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *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;
            p_dest++;

            p_src += 7;

156
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
157
        }
158
        else if( b_input_5_0 )
159
160
161
162
163
164
165
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
            p_dest++;

            p_src += 5;

166
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
167
        }
168
169
170
171
172
173
174
175
        else if( b_input_3_0 )
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
            p_dest++;

            p_src += 3;

176
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
177
        }
178
179
180
181
182
183
184
185
186
        else
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[0] / 2 + p_src[1] / 2;
            p_dest++;

            p_src += 2;
        }
    }
187
188
    else
    {
189
        assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 );
190
191
        assert( b_input_7_0 || b_input_5_0 );

192
        if( b_input_7_0 )
193
194
195
196
197
198
199
200
201
202
203
204
205
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
            p_dest++;
            *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
            p_dest++;
            *p_dest = p_src[2] / 6 +  p_src[4];
            p_dest++;
            *p_dest = p_src[3] / 6 +  p_src[5];
            p_dest++;

            p_src += 7;

206
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        }
        else
        for( i = p_in_buf->i_nb_samples; i--; )
        {
            *p_dest = p_src[4] + 0.5 * p_src[0];
            p_dest++;
            *p_dest = p_src[4] + 0.5 * p_src[1];
            p_dest++;
            *p_dest = p_src[2];
            p_dest++;
            *p_dest = p_src[3];
            p_dest++;

            p_src += 5;

222
            if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
223
        }
224
225
    }
}
226
227
228
229
230
231
232
233

/*****************************************************************************
 * OpenFilter:
 *****************************************************************************/
static int OpenFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;

234
235
    audio_format_t fmt_in  = p_filter->fmt_in.audio;
    audio_format_t fmt_out = p_filter->fmt_out.audio;
236

237
238
    fmt_in.i_format = p_filter->fmt_in.i_codec;
    fmt_out.i_format = p_filter->fmt_out.i_codec;
239

240
    if( !IsSupported( &fmt_in, &fmt_out ) )
241
242
243
244
245
246
247
248
249
250
251
252
        return -1;

    p_filter->pf_audio_filter = Filter;

    return 0;
}

/*****************************************************************************
 * Filter:
 *****************************************************************************/
static block_t *Filter( filter_t *p_filter, block_t *p_block )
{
253
    if( !p_block || !p_block->i_nb_samples )
254
    {
255
256
        if( p_block )
            block_Release( p_block );
257
258
259
        return NULL;
    }

260
    size_t i_out_size = p_block->i_nb_samples *
261
262
263
      p_filter->fmt_out.audio.i_bitspersample *
        p_filter->fmt_out.audio.i_channels / 8;

264
    block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
265
266
267
    if( !p_out )
    {
        msg_Warn( p_filter, "can't get output buffer" );
268
        block_Release( p_block );
269
270
271
        return NULL;
    }

272
    p_out->i_nb_samples = p_block->i_nb_samples;
273
274
275
276
    p_out->i_dts = p_block->i_dts;
    p_out->i_pts = p_block->i_pts;
    p_out->i_length = p_block->i_length;

277
    DoWork( p_filter, p_block, p_out );
278

279
    block_Release( p_block );
280
281
282
283

    return p_out;
}

284
285
286
287
288
/*****************************************************************************
 * Helpers:
 *****************************************************************************/
static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
{
289
    if( p_input->i_format != VLC_CODEC_FL32 ||
290
291
292
293
294
295
          p_input->i_format != p_output->i_format ||
          p_input->i_rate != p_output->i_rate )
        return false;

    if( p_input->i_physical_channels == p_output->i_physical_channels &&
        p_input->i_original_channels == p_output->i_original_channels )
296
297
298
299
300
301
302
303
304
305
306
307
    {
        return false;
    }

    /* Only conversion to Mono, Stereo and 4.0 right now */
    if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
        p_output->i_physical_channels != AOUT_CHANS_2_0 &&
        p_output->i_physical_channels != AOUT_CHANS_4_0 )
    {
        return false;
    }

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
311
    if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
        (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
316
317
318
         p_input->i_physical_channels != AOUT_CHANS_2_0 )
    {
        return false;
    }
319
320
321
322
323

    /* Only if we downmix */
    if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
        return false;

324
325
    return true;
}
326