simple.c 15.6 KB
Newer Older
1
/*****************************************************************************
2
 * simple.c : simple channel mixer plug-in
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002, 2004, 2006-2009 VLC authors and VideoLAN
5
6
7
8
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
9
10
11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12
13
14
15
 * (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
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
16
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software 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
static void CloseFilter( vlc_object_t * );
43

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

52
53
54
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
55
56
57
58
59
struct filter_sys_t
{
    void (*pf_dowork)(filter_t *, block_t *, block_t * );
};

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*****************************************************************************
 * IsSupported: can we downmix?
 *****************************************************************************/
static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
{
    if( p_input->i_format != VLC_CODEC_FL32 ||
        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 )
    {
        return false;
    }

    /* Only conversion to Mono, Stereo, 4.0 and 5.1 */
    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 &&
        p_output->i_physical_channels != AOUT_CHANS_5_1 )
    {
        return false;
    }

    /* Only from 7.x/5.x/4.0/3.x/2.0
     * NB 5.X rear and middle are handled the same way
     * We don't support 2.1 -> 2.0 (trivial can do it)
     * TODO: We don't support any 8.1 input
     * TODO: We don't support any 6.x input
     * TODO: We don't support 4.0 rear and 4.0 middle
     * */
    if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
95
        (p_input->i_physical_channels)                  != AOUT_CHANS_6_1_MIDDLE &&
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
        (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
        (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
        (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
         p_input->i_physical_channels != AOUT_CHANS_2_0 )
    {
        return false;
    }

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

    return true;
}

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

114
115
116
117
118
static void DoWork_7_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
119
120
121
        float ctr = p_src[6] * 0.7071f;
        *p_dest++ = ctr + p_src[0] + p_src[2] / 4 + p_src[4] / 4;
        *p_dest++ = ctr + p_src[1] + p_src[3] / 4 + p_src[5] / 4;
122

123
        p_src += 7;
124

125
126
127
128
        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}

129
130
131
static void DoWork_6_1_to_2_0( filter_t *p_filter, block_t *p_in_buf,
                               block_t *p_out_buf )
{
132
133
134
135
136
    VLC_UNUSED(p_filter);
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
137
138
139
        float ctr = (p_src[2] + p_src[5]) * 0.7071f;
        *p_dest++ = p_src[0] + p_src[3] + ctr;
        *p_dest++ = p_src[1] + p_src[4] + ctr;
140
141
142
143
144
145
146
147

        p_src += 6;

        /* We always have LFE here */
        p_src++;
    }
}

148
static void DoWork_5_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
149
    float *p_dest = (float *)p_out_buf->p_buffer;
150
    const float *p_src = (const float *)p_in_buf->p_buffer;
151
152
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
153
154
        *p_dest++ = p_src[0] + 0.7071f * (p_src[4] + p_src[2]);
        *p_dest++ = p_src[1] + 0.7071f * (p_src[4] + p_src[3]);
155

156
        p_src += 5;
157

158
159
160
161
162
163
164
165
166
        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}

static void DoWork_4_0_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    VLC_UNUSED(p_filter);
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
167
    {
168
169
        *p_dest++ = p_src[2] + p_src[3] + 0.5f * p_src[0];
        *p_dest++ = p_src[2] + p_src[3] + 0.5f * p_src[1];
170
171
172
        p_src += 4;
    }
}
173

174
175
176
177
178
static void DoWork_3_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
179
180
        *p_dest++ = p_src[2] + 0.5f * p_src[0];
        *p_dest++ = p_src[2] + 0.5f * p_src[1];
181

182
        p_src += 3;
183

184
185
186
        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}
187

188
189
190
191
192
193
194
195
196
197
static void DoWork_7_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int 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_src += 7;

        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
198
    }
199
200
201
202
203
204
}

static void DoWork_5_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
205
    {
206
207
        *p_dest++ = 0.7071f * (p_src[0] + p_src[1]) + p_src[4]
                     + 0.5f * (p_src[2] + p_src[3]);
208

209
        p_src += 5;
210

211
212
213
        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}
214

215
216
217
218
219
220
221
222
223
224
225
static void DoWork_4_0_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    VLC_UNUSED(p_filter);
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
        *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4;
        p_src += 4;
    }
}

226
227
228
229
230
231
static void DoWork_3_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
        *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
232

233
        p_src += 3;
234

235
236
237
        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}
238

239
240
241
242
243
244
245
static void DoWork_2_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    VLC_UNUSED(p_filter);
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
        *p_dest++ = p_src[0] / 2 + p_src[1] / 2;
246

247
        p_src += 2;
248
    }
249
250
251
252
253
254
}

static void DoWork_7_x_to_4_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
255
    {
256
257
        *p_dest++ = p_src[6] + 0.5f * p_src[0] + p_src[2] / 6;
        *p_dest++ = p_src[6] + 0.5f * p_src[1] + p_src[3] / 6;
258
259
        *p_dest++ = p_src[2] / 6 +  p_src[4];
        *p_dest++ = p_src[3] / 6 +  p_src[5];
260

261
262
263
264
265
266
267
268
269
270
271
        p_src += 7;

        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}

static void DoWork_5_x_to_4_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
272
273
274
        float ctr = p_src[4] * 0.7071f;
        *p_dest++ = p_src[0] + ctr;
        *p_dest++ = p_src[1] + ctr;
275
276
277
278
279
280
        *p_dest++ = p_src[2];
        *p_dest++ = p_src[3];

        p_src += 5;

        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
281
282
    }
}
283

284
285
286
287
288
289
290
static void DoWork_7_x_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
        *p_dest++ = p_src[0];
        *p_dest++ = p_src[1];
291
292
        *p_dest++ = (p_src[2] + p_src[4]) * 0.5f;
        *p_dest++ = (p_src[3] + p_src[5]) * 0.5f;
293
294
295
296
297
298
299
300
301
302
303
        *p_dest++ = p_src[6];

        p_src += 7;

        if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE &&
            p_filter->fmt_out.audio.i_physical_channels & AOUT_CHAN_LFE )
            *p_dest++ = *p_src++;
        else if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
    }
}

304
static void DoWork_6_1_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
305
    VLC_UNUSED(p_filter);
306
307
308
309
310
311
    float *p_dest = (float *)p_out_buf->p_buffer;
    const float *p_src = (const float *)p_in_buf->p_buffer;
    for( int i = p_in_buf->i_nb_samples; i--; )
    {
        *p_dest++ = p_src[0];
        *p_dest++ = p_src[1];
312
313
        *p_dest++ = (p_src[2] + p_src[4]) * 0.5f;
        *p_dest++ = (p_src[3] + p_src[4]) * 0.5f;
314
315
316
317
318
319
320
321
322
        *p_dest++ = p_src[5];

        p_src += 6;

        /* We always have LFE here */
        *p_dest++ = *p_src++;
    }
}

323

324
325
326
327
328
329
/*****************************************************************************
 * OpenFilter:
 *****************************************************************************/
static int OpenFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
330
    filter_sys_t *p_sys;
331

332
333
    audio_format_t fmt_in  = p_filter->fmt_in.audio;
    audio_format_t fmt_out = p_filter->fmt_out.audio;
334

335
    if( !IsSupported( &fmt_in, &fmt_out ) )
336
337
338
        return VLC_EGENERIC;

    p_filter->p_sys = malloc( sizeof(*p_sys) );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
339
    if( unlikely(!p_filter->p_sys) )
340
        return VLC_ENOMEM;
341
342
343

    p_filter->pf_audio_filter = Filter;

344
345
    const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
    const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
346
347
348
    const bool b_input_6_1 = !b_input_7_0 &&
                             i_input_physical == AOUT_CHANS_6_1_MIDDLE;
    const bool b_input_5_0 = !b_input_7_0 && !b_input_6_1 &&
349
350
351
352
353
354
355
356
357
358
359
                             ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
                               (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
    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 &&
                             (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;

    if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
    {
        if( b_input_7_0 )
            p_filter->p_sys->pf_dowork = DoWork_7_x_to_2_0;
360
361
        else if( b_input_6_1 )
            p_filter->p_sys->pf_dowork = DoWork_6_1_to_2_0;
362
363
364
365
366
367
368
369
370
371
372
373
374
        else if( b_input_5_0 )
            p_filter->p_sys->pf_dowork = DoWork_5_x_to_2_0;
        else if( b_input_4_center_rear )
            p_filter->p_sys->pf_dowork = DoWork_4_0_to_2_0;
        else if( b_input_3_0 )
            p_filter->p_sys->pf_dowork = DoWork_3_x_to_2_0;
    }
    else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
    {
        if( b_input_7_0 )
            p_filter->p_sys->pf_dowork = DoWork_7_x_to_1_0;
        else if( b_input_5_0 )
            p_filter->p_sys->pf_dowork = DoWork_5_x_to_1_0;
375
376
        else if( b_input_4_center_rear )
            p_filter->p_sys->pf_dowork = DoWork_4_0_to_1_0;
377
378
379
380
381
        else if( b_input_3_0 )
            p_filter->p_sys->pf_dowork = DoWork_3_x_to_1_0;
        else
            p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0;
    }
382
    else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0)
383
384
385
386
387
388
    {
        if( b_input_7_0 )
            p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0;
        else
            p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0;
    }
389
390
    else
    {
391
392
393
394
395
        assert( b_input_7_0 || b_input_6_1 );
        if( b_input_7_0 )
            p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x;
        else
            p_filter->p_sys->pf_dowork = DoWork_6_1_to_5_x;
396
    }
397
398

    return VLC_SUCCESS;
399
400
}

401
402
403
404
405
406
407
static void CloseFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *) p_this;
    filter_sys_t *p_sys = p_filter->p_sys;
    free( p_sys );
}

408
409
410
411
412
/*****************************************************************************
 * Filter:
 *****************************************************************************/
static block_t *Filter( filter_t *p_filter, block_t *p_block )
{
413
    filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
414
    if( !p_block || !p_block->i_nb_samples )
415
    {
416
417
        if( p_block )
            block_Release( p_block );
418
419
420
        return NULL;
    }

421
    size_t i_out_size = p_block->i_nb_samples *
422
423
424
      p_filter->fmt_out.audio.i_bitspersample *
        p_filter->fmt_out.audio.i_channels / 8;

425
    block_t *p_out = block_Alloc( i_out_size );
426
427
428
    if( !p_out )
    {
        msg_Warn( p_filter, "can't get output buffer" );
429
        block_Release( p_block );
430
431
432
        return NULL;
    }

433
    p_out->i_nb_samples = p_block->i_nb_samples;
434
435
436
437
    p_out->i_dts = p_block->i_dts;
    p_out->i_pts = p_block->i_pts;
    p_out->i_length = p_block->i_length;

438
439
440
441
442
443
    int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
    int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
    p_out->i_nb_samples = p_block->i_nb_samples;
    p_out->i_buffer = p_block->i_buffer * i_output_nb / i_input_nb;

    p_sys->pf_dowork( p_filter, p_block, p_out );
444

445
    block_Release( p_block );
446
447
448
449

    return p_out;
}