qt: playlist: use item title if available
[vlc.git] / modules / audio_filter / channel_mixer / remap.c
1 /*****************************************************************************
2  * remap.c : simple channel remapper plug-in
3  *****************************************************************************
4  * Copyright (C) 2011 VLC authors and VideoLAN
5  *
6  * Authors: Cheng Sun <chengsun9@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_aout.h>
33 #include <vlc_filter.h>
34 #include <vlc_block.h>
35 #include <assert.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  OpenFilter( vlc_object_t * );
41
42 #define REMAP_CFG "aout-remap-"
43
44 /* wg4 channel indices in the order of channel_name */
45 static const uint8_t channel_wg4idx[] = { 0, 7, 1, 4, 6, 5, 2, 3, 8 };
46
47 static const unsigned channel_idx[]    = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
48
49 static const char *const channel_name[] =
50 {
51     REMAP_CFG "channel-left", REMAP_CFG "channel-center",
52     REMAP_CFG "channel-right", REMAP_CFG "channel-rearleft",
53     REMAP_CFG "channel-rearcenter", REMAP_CFG "channel-rearright",
54     REMAP_CFG "channel-middleleft", REMAP_CFG "channel-middleright",
55     REMAP_CFG "channel-lfe"
56 };
57
58 static const char *const channel_desc[] =
59 {
60     N_( "Left" ), N_( "Center" ), N_( "Right" ),
61     N_( "Rear left" ), N_( "Rear center" ), N_( "Rear right" ),
62     N_( "Side left" ), N_( "Side right" ), N_( "Low-frequency effects" )
63 };
64
65 static const int channel_flag[] =
66 {
67     AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
68     AOUT_CHAN_REARLEFT, AOUT_CHAN_REARCENTER, AOUT_CHAN_REARRIGHT,
69     AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE
70 };
71
72 vlc_module_begin ()
73     set_description( N_("Audio channel remapper") )
74     set_capability( "audio filter", 0 )
75     set_category( CAT_AUDIO )
76     set_subcategory( SUBCAT_AUDIO_AFILTER )
77     set_callback( OpenFilter )
78     set_shortname( "Remap" )
79
80 #define CHANNEL( idx ) \
81     add_integer( channel_name[idx], idx, channel_desc[idx], \
82             channel_desc[idx], false) \
83         change_integer_list( channel_idx, channel_desc )
84     CHANNEL(0) CHANNEL(1) CHANNEL(2)
85     CHANNEL(3) CHANNEL(4) CHANNEL(5)
86     CHANNEL(6) CHANNEL(7) CHANNEL(8)
87 #undef CHANNEL
88
89     add_bool( REMAP_CFG "normalize", true, "Normalize channels",
90             "When mapping more than one channel to a single output channel, "
91             "normalize the output accordingly.", false )
92
93 vlc_module_end ()
94
95 /*****************************************************************************
96  * Local prototypes
97  *****************************************************************************/
98
99 static block_t *Remap( filter_t *, block_t * );
100
101 typedef void (*remap_fun_t)( filter_t *, const void *, void *,
102                              int, unsigned, unsigned);
103
104 typedef struct
105 {
106     remap_fun_t pf_remap;
107     int nb_in_ch[AOUT_CHAN_MAX];
108     int8_t map_ch[AOUT_CHAN_MAX];
109     bool b_normalize;
110 } filter_sys_t;
111
112 static const uint32_t valid_channels[] = {
113 /* list taken from aout_FormatPrintChannels */
114     AOUT_CHAN_LEFT,
115     AOUT_CHAN_RIGHT,
116     AOUT_CHAN_CENTER,
117     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
118     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
119     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER,
120     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
121           | AOUT_CHAN_REARCENTER,
122     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
123           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
124     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
125           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT,
126     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
127           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
128     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
129           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT,
130     AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
131     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE,
132     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
133     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
134           | AOUT_CHAN_LFE,
135     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
136           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE,
137     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
138           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
139     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
140           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
141     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
142           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
143     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
144           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
145     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
146           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
147           | AOUT_CHAN_MIDDLERIGHT,
148     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
149           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
150           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
151 };
152
153 static inline uint32_t CanonicaliseChannels( uint32_t i_physical_channels )
154 {
155     for( unsigned i = 0; i < sizeof( valid_channels )/sizeof( valid_channels[0] ); i++ )
156         if( (i_physical_channels & ~valid_channels[i]) == 0 )
157             return valid_channels[i];
158
159     vlc_assert_unreachable();
160 }
161
162 /*****************************************************************************
163  * Remap*: do remapping
164  *****************************************************************************/
165 #define DEFINE_REMAP( name, type ) \
166 static void RemapCopy##name( filter_t *p_filter, \
167                     const void *p_srcorig, void *p_destorig, \
168                     int i_nb_samples, \
169                     unsigned i_nb_in_channels, unsigned i_nb_out_channels ) \
170 { \
171     filter_sys_t *p_sys = ( filter_sys_t * )p_filter->p_sys; \
172     const type *p_src = p_srcorig; \
173     type *p_dest = p_destorig; \
174  \
175     for( int i = 0; i < i_nb_samples; i++ ) \
176     { \
177         for( uint8_t in_ch = 0; in_ch < i_nb_in_channels; in_ch++ ) \
178         { \
179             int8_t out_ch = p_sys->map_ch[ in_ch ]; \
180             if (out_ch < 0) continue; \
181             memcpy( p_dest + out_ch, \
182                     p_src  + in_ch, \
183                     sizeof( type ) ); \
184         } \
185         p_src  += i_nb_in_channels; \
186         p_dest += i_nb_out_channels; \
187     } \
188 } \
189  \
190 static void RemapAdd##name( filter_t *p_filter, \
191                     const void *p_srcorig, void *p_destorig, \
192                     int i_nb_samples, \
193                     unsigned i_nb_in_channels, unsigned i_nb_out_channels ) \
194 { \
195     filter_sys_t *p_sys = ( filter_sys_t * )p_filter->p_sys; \
196     const type *p_src = p_srcorig; \
197     type *p_dest = p_destorig; \
198  \
199     for( int i = 0; i < i_nb_samples; i++ ) \
200     { \
201         for( uint8_t in_ch = 0; in_ch < i_nb_in_channels; in_ch++ ) \
202         { \
203             int8_t out_ch = p_sys->map_ch[ in_ch ]; \
204             if (out_ch < 0) continue; \
205             if( p_sys->b_normalize ) \
206                 p_dest[ out_ch ] += p_src[ in_ch ] / p_sys->nb_in_ch[ out_ch ]; \
207             else \
208                 p_dest[ out_ch ] += p_src[ in_ch ]; \
209         } \
210         p_src  += i_nb_in_channels; \
211         p_dest += i_nb_out_channels; \
212     } \
213 }
214
215 DEFINE_REMAP( U8,   uint8_t  )
216 DEFINE_REMAP( S16N, int16_t  )
217 DEFINE_REMAP( S32N, int32_t  )
218 DEFINE_REMAP( FL32, float    )
219 DEFINE_REMAP( FL64, double   )
220
221 #undef DEFINE_REMAP
222
223 static inline remap_fun_t GetRemapFun( audio_format_t *p_format, bool b_add )
224 {
225     if( b_add )
226     {
227         switch( p_format->i_format )
228         {
229             case VLC_CODEC_U8:
230                 return RemapAddU8;
231             case VLC_CODEC_S16N:
232                 return RemapAddS16N;
233             case VLC_CODEC_S32N:
234                 return RemapAddS32N;
235             case VLC_CODEC_FL32:
236                 return RemapAddFL32;
237             case VLC_CODEC_FL64:
238                 return RemapAddFL64;
239         }
240     }
241     else
242     {
243         switch( p_format->i_format )
244         {
245             case VLC_CODEC_U8:
246                 return RemapCopyU8;
247             case VLC_CODEC_S16N:
248                 return RemapCopyS16N;
249             case VLC_CODEC_S32N:
250                 return RemapCopyS32N;
251             case VLC_CODEC_FL32:
252                 return RemapCopyFL32;
253             case VLC_CODEC_FL64:
254                 return RemapCopyFL64;
255         }
256     }
257
258     return NULL;
259 }
260
261
262 /*****************************************************************************
263  * OpenFilter:
264  *****************************************************************************/
265 static int OpenFilter( vlc_object_t *p_this )
266 {
267     filter_t *p_filter = (filter_t *)p_this;
268     filter_sys_t *p_sys;
269
270     audio_format_t *audio_in  = &p_filter->fmt_in.audio;
271     audio_format_t *audio_out = &p_filter->fmt_out.audio;
272
273     /* Allocate the memory needed to store the module's structure */
274     p_sys = p_filter->p_sys = vlc_obj_malloc( p_this, sizeof(filter_sys_t) );
275     if( unlikely( p_sys == NULL ) )
276         return VLC_ENOMEM;
277
278     static const char *const options[] = {
279         "channel-left", "channel-center", "channel-right", "channel-rearleft",
280         "channel-rearcenter", "channel-rearright", "channel-middleleft",
281         "channel-middleright", "channel-lfe", "normalize", NULL
282     };
283     config_ChainParse(p_filter, REMAP_CFG, options, p_filter->p_cfg);
284
285     /* get number of and layout of input channels */
286     uint32_t i_output_physical = 0;
287     int8_t pi_map_ch[ AOUT_CHAN_MAX ] = { 0 }; /* which out channel each in channel is mapped to */
288     p_sys->b_normalize = var_InheritBool( p_this, REMAP_CFG "normalize" );
289
290     for( uint8_t in_ch = 0, wg4_i = 0; in_ch < audio_in->i_channels; in_ch++, wg4_i++ )
291     {
292         /* explode in_channels in the right order */
293         while( ( audio_in->i_physical_channels & pi_vlc_chan_order_wg4[ wg4_i ] ) == 0 )
294         {
295             wg4_i++;
296             assert( wg4_i < sizeof( pi_vlc_chan_order_wg4 )/sizeof( pi_vlc_chan_order_wg4[0] ) );
297         }
298         unsigned channel_wg4idx_len = sizeof( channel_wg4idx )/sizeof( channel_wg4idx[0] );
299         uint8_t *pi_chnidx = memchr( channel_wg4idx, wg4_i, channel_wg4idx_len );
300         assert( pi_chnidx != NULL );
301         uint8_t chnidx = pi_chnidx - channel_wg4idx;
302         int64_t val = var_InheritInteger( p_this, channel_name[chnidx] );
303         if (val >= AOUT_CHAN_MAX)
304         {
305             msg_Err( p_filter, "invalid channel index" );
306             return VLC_EGENERIC;
307         }
308         if (val < 0)
309         {
310             pi_map_ch[in_ch] = -1;
311             continue;
312         }
313         uint8_t out_idx = val;
314         pi_map_ch[in_ch] = channel_wg4idx[ out_idx ];
315
316         i_output_physical |= channel_flag[ out_idx ];
317     }
318     i_output_physical = CanonicaliseChannels( i_output_physical );
319
320     unsigned i_channels = vlc_popcount(i_output_physical);
321
322     /* condense out_channels */
323     uint8_t out_ch_sorted[ AOUT_CHAN_MAX ];
324     for( uint8_t i = 0, wg4_i = 0; i < i_channels; i++, wg4_i++ )
325     {
326         while( ( i_output_physical & pi_vlc_chan_order_wg4[ wg4_i ] ) == 0 )
327         {
328             wg4_i++;
329             assert( wg4_i < sizeof( pi_vlc_chan_order_wg4 )/sizeof( pi_vlc_chan_order_wg4[0] ) );
330         }
331         out_ch_sorted[ i ] = wg4_i;
332     }
333     bool b_multiple = false; /* whether we need to add channels (multiple in mapped to an out) */
334     memset( p_sys->nb_in_ch, 0, sizeof( p_sys->nb_in_ch ) );
335     for( uint8_t i = 0; i < audio_in->i_channels; i++ )
336     {
337         int8_t wg4_out_ch = pi_map_ch[i];
338         if (wg4_out_ch < 0)
339         {
340             p_sys->map_ch[i] = -1;
341             continue;
342         }
343         uint8_t *pi_out_ch = memchr( out_ch_sorted, wg4_out_ch, i_channels );
344         assert( pi_out_ch != NULL );
345         p_sys->map_ch[i] = pi_out_ch - out_ch_sorted;
346         if( ++p_sys->nb_in_ch[ p_sys->map_ch[i] ] > 1 )
347             b_multiple = true;
348     }
349
350     p_sys->pf_remap = GetRemapFun( audio_in, b_multiple );
351     if( !p_sys->pf_remap )
352     {
353         msg_Err( p_filter, "Could not decide on %s remap function", b_multiple ? "an add" : "a copy" );
354         return VLC_EGENERIC;
355     }
356
357     audio_out->i_rate = audio_in->i_rate;
358     audio_out->i_format = audio_in->i_format;
359     audio_out->i_physical_channels = i_output_physical;
360     aout_FormatPrepare( audio_out );
361
362     msg_Dbg( p_filter, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
363              "Remap filter",
364              (char *)&audio_in->i_format, (char *)&audio_out->i_format,
365              audio_in->i_rate, audio_out->i_rate,
366              aout_FormatPrintChannels( audio_in ),
367              aout_FormatPrintChannels( audio_out ) );
368
369     static const struct vlc_filter_operations filter_ops =
370     {
371         .filter_audio = Remap,
372     };
373     p_filter->ops = &filter_ops;
374     return VLC_SUCCESS;
375 }
376
377 /*****************************************************************************
378  * Remap:
379  *****************************************************************************/
380 static block_t *Remap( filter_t *p_filter, block_t *p_block )
381 {
382     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
383     if( !p_block || !p_block->i_nb_samples )
384     {
385         if( p_block )
386             block_Release( p_block );
387         return NULL;
388     }
389
390     size_t i_out_size = p_block->i_nb_samples *
391         p_filter->fmt_out.audio.i_bytes_per_frame;
392
393     block_t *p_out = block_Alloc( i_out_size );
394     if( !p_out )
395     {
396         msg_Warn( p_filter, "can't get output buffer" );
397         block_Release( p_block );
398         return NULL;
399     }
400     p_out->i_nb_samples = p_block->i_nb_samples;
401     p_out->i_dts = p_block->i_dts;
402     p_out->i_pts = p_block->i_pts;
403     p_out->i_length = p_block->i_length;
404
405     memset( p_out->p_buffer, 0, i_out_size );
406
407     p_sys->pf_remap( p_filter,
408                 (const void *)p_block->p_buffer, (void *)p_out->p_buffer,
409                 p_block->i_nb_samples,
410                 p_filter->fmt_in.audio.i_channels,
411                 p_filter->fmt_out.audio.i_channels );
412
413     block_Release( p_block );
414
415     return p_out;
416 }