qt: playlist: use item title if available
[vlc.git] / modules / audio_filter / channel_mixer / headphone.c
1 /*****************************************************************************
2  * headphone.c : headphone virtual spatialization channel mixer module
3  *               -> gives the feeling of a real room with a simple headphone
4  *****************************************************************************
5  * Copyright (C) 2002-2006 the VideoLAN team
6  *
7  * Authors: Boris Dorès <babal@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <math.h>                                        /* sqrt */
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_aout.h>
37 #include <vlc_filter.h>
38 #include <vlc_block.h>
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  OpenFilter ( vlc_object_t * );
44 static void CloseFilter( filter_t * );
45 static block_t *Convert( filter_t *, block_t * );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 #define MODULE_DESCRIPTION N_ ( \
51      "This effect gives you the feeling that you are standing in a room " \
52      "with a complete 7.1 speaker set when using only a headphone, " \
53      "providing a more realistic sound experience. It should also be " \
54      "more comfortable and less tiring when listening to music for " \
55      "long periods of time.\nIt works with any source format from mono " \
56      "to 7.1.")
57
58 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
59 #define HEADPHONE_DIM_LONGTEXT N_( \
60      "Distance between front left speaker and listener in meters.")
61
62 #define HEADPHONE_COMPENSATE_TEXT N_("Compensate delay")
63 #define HEADPHONE_COMPENSATE_LONGTEXT N_( \
64      "The delay which is introduced by the physical algorithm may "\
65      "sometimes be disturbing for the synchronization between lips-movement "\
66      "and speech. In case, turn this on to compensate.")
67
68 #define HEADPHONE_DOLBY_TEXT N_("No decoding of Dolby Surround")
69 #define HEADPHONE_DOLBY_LONGTEXT N_( \
70      "Dolby Surround encoded streams won't be decoded before being " \
71      "processed by this filter. Enabling this setting is not recommended.")
72
73 vlc_module_begin ()
74     set_description( N_("Headphone virtual spatialization effect") )
75     set_shortname( N_("Headphone effect") )
76     set_help( MODULE_DESCRIPTION )
77     set_category( CAT_AUDIO )
78     set_subcategory( SUBCAT_AUDIO_AFILTER )
79
80     add_integer( "headphone-dim", 10, HEADPHONE_DIM_TEXT,
81                  HEADPHONE_DIM_LONGTEXT, false )
82     add_bool( "headphone-compensate", false, HEADPHONE_COMPENSATE_TEXT,
83               HEADPHONE_COMPENSATE_LONGTEXT, true )
84     add_bool( "headphone-dolby", false, HEADPHONE_DOLBY_TEXT,
85               HEADPHONE_DOLBY_LONGTEXT, true )
86
87     set_capability( "audio filter", 0 )
88     set_callback( OpenFilter )
89     add_shortcut( "headphone" )
90 vlc_module_end ()
91
92
93 /*****************************************************************************
94  * Internal data structures
95  *****************************************************************************/
96 struct atomic_operation_t
97 {
98     int i_source_channel_offset;
99     int i_dest_channel_offset;
100     unsigned int i_delay;/* in sample unit */
101     double d_amplitude_factor;
102 };
103
104 typedef struct
105 {
106     size_t i_overflow_buffer_size;/* in bytes */
107     float * p_overflow_buffer;
108     unsigned int i_nb_atomic_operations;
109     struct atomic_operation_t * p_atomic_operations;
110 } filter_sys_t;
111
112 /*****************************************************************************
113  * Init: initialize internal data structures
114  * and computes the needed atomic operations
115  *****************************************************************************/
116 /* x and z represent the coordinates of the virtual speaker
117  *  relatively to the center of the listener's head, measured in meters :
118  *
119  *  left              right
120  *Z
121  *-
122  *a          head
123  *x
124  *i
125  *s
126  *  rear left    rear right
127  *
128  *          x-axis
129  *  */
130 static void ComputeChannelOperations( filter_sys_t * p_data
131         , unsigned int i_rate, unsigned int i_next_atomic_operation
132         , int i_source_channel_offset, double d_x, double d_z
133         , double d_compensation_length, double d_channel_amplitude_factor )
134 {
135     double d_c = 340; /*sound celerity (unit: m/s)*/
136     double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
137
138     /* Left ear */
139     p_data->p_atomic_operations[i_next_atomic_operation]
140         .i_source_channel_offset = i_source_channel_offset;
141     p_data->p_atomic_operations[i_next_atomic_operation]
142         .i_dest_channel_offset = 0;/* left */
143     p_data->p_atomic_operations[i_next_atomic_operation]
144         .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
145                           / d_c * i_rate - d_compensation_delay );
146     if( d_x < 0 )
147     {
148         p_data->p_atomic_operations[i_next_atomic_operation]
149             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
150     }
151     else if( d_x > 0 )
152     {
153         p_data->p_atomic_operations[i_next_atomic_operation]
154             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
155     }
156     else
157     {
158         p_data->p_atomic_operations[i_next_atomic_operation]
159             .d_amplitude_factor = d_channel_amplitude_factor / 2;
160     }
161
162     /* Right ear */
163     p_data->p_atomic_operations[i_next_atomic_operation + 1]
164         .i_source_channel_offset = i_source_channel_offset;
165     p_data->p_atomic_operations[i_next_atomic_operation + 1]
166         .i_dest_channel_offset = 1;/* right */
167     p_data->p_atomic_operations[i_next_atomic_operation + 1]
168         .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
169                           / d_c * i_rate - d_compensation_delay );
170     if( d_x < 0 )
171     {
172         p_data->p_atomic_operations[i_next_atomic_operation + 1]
173             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
174     }
175     else if( d_x > 0 )
176     {
177         p_data->p_atomic_operations[i_next_atomic_operation + 1]
178             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
179     }
180     else
181     {
182         p_data->p_atomic_operations[i_next_atomic_operation + 1]
183             .d_amplitude_factor = d_channel_amplitude_factor / 2;
184     }
185 }
186
187 static int Init( vlc_object_t *p_this, filter_sys_t * p_data
188         , unsigned int i_nb_channels, uint32_t i_physical_channels
189         , unsigned int i_rate )
190 {
191     double d_x = var_InheritInteger( p_this, "headphone-dim" );
192     double d_z = d_x;
193     double d_z_rear = -d_x/3;
194     double d_min = 0;
195     unsigned int i_next_atomic_operation;
196     int i_source_channel_offset;
197     unsigned int i;
198
199     if( var_InheritBool( p_this, "headphone-compensate" ) )
200     {
201         /* minimal distance to any speaker */
202         if( i_physical_channels & AOUT_CHAN_REARCENTER )
203         {
204             d_min = d_z_rear;
205         }
206         else
207         {
208             d_min = d_z;
209         }
210     }
211
212     /* Number of elementary operations */
213     p_data->i_nb_atomic_operations = i_nb_channels * 2;
214     if( i_physical_channels & AOUT_CHAN_CENTER )
215     {
216         p_data->i_nb_atomic_operations += 2;
217     }
218     p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
219             * p_data->i_nb_atomic_operations );
220     if( p_data->p_atomic_operations == NULL )
221         return -1;
222
223     /* For each virtual speaker, computes elementary wave propagation time
224      * to each ear */
225     i_next_atomic_operation = 0;
226     i_source_channel_offset = 0;
227     if( i_physical_channels & AOUT_CHAN_LEFT )
228     {
229         ComputeChannelOperations( p_data , i_rate
230                 , i_next_atomic_operation , i_source_channel_offset
231                 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
232         i_next_atomic_operation += 2;
233         i_source_channel_offset++;
234     }
235     if( i_physical_channels & AOUT_CHAN_RIGHT )
236     {
237         ComputeChannelOperations( p_data , i_rate
238                 , i_next_atomic_operation , i_source_channel_offset
239                 , d_x , d_z , d_min , 2.0 / i_nb_channels );
240         i_next_atomic_operation += 2;
241         i_source_channel_offset++;
242     }
243     if( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
244     {
245         ComputeChannelOperations( p_data , i_rate
246                 , i_next_atomic_operation , i_source_channel_offset
247                 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
248         i_next_atomic_operation += 2;
249         i_source_channel_offset++;
250     }
251     if( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
252     {
253         ComputeChannelOperations( p_data , i_rate
254                 , i_next_atomic_operation , i_source_channel_offset
255                 , d_x , 0 , d_min , 1.5 / i_nb_channels );
256         i_next_atomic_operation += 2;
257         i_source_channel_offset++;
258     }
259     if( i_physical_channels & AOUT_CHAN_REARLEFT )
260     {
261         ComputeChannelOperations( p_data , i_rate
262                 , i_next_atomic_operation , i_source_channel_offset
263                 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
264         i_next_atomic_operation += 2;
265         i_source_channel_offset++;
266     }
267     if( i_physical_channels & AOUT_CHAN_REARRIGHT )
268     {
269         ComputeChannelOperations( p_data , i_rate
270                 , i_next_atomic_operation , i_source_channel_offset
271                 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
272         i_next_atomic_operation += 2;
273         i_source_channel_offset++;
274     }
275     if( i_physical_channels & AOUT_CHAN_REARCENTER )
276     {
277         ComputeChannelOperations( p_data , i_rate
278                 , i_next_atomic_operation , i_source_channel_offset
279                 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
280         i_next_atomic_operation += 2;
281         i_source_channel_offset++;
282     }
283     if( i_physical_channels & AOUT_CHAN_CENTER )
284     {
285         /* having two center channels increases the spatialization effect */
286         ComputeChannelOperations( p_data , i_rate
287                 , i_next_atomic_operation , i_source_channel_offset
288                 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
289         i_next_atomic_operation += 2;
290         ComputeChannelOperations( p_data , i_rate
291                 , i_next_atomic_operation , i_source_channel_offset
292                 , -d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
293         i_next_atomic_operation += 2;
294         i_source_channel_offset++;
295     }
296     if( i_physical_channels & AOUT_CHAN_LFE )
297     {
298         ComputeChannelOperations( p_data , i_rate
299                 , i_next_atomic_operation , i_source_channel_offset
300                 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
301         i_next_atomic_operation += 2;
302         i_source_channel_offset++;
303     }
304
305     /* Initialize the overflow buffer
306      * we need it because the process induce a delay in the samples */
307     p_data->i_overflow_buffer_size = 0;
308     for( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
309     {
310         if( p_data->i_overflow_buffer_size
311                 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float) )
312         {
313             p_data->i_overflow_buffer_size
314                 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float);
315         }
316     }
317     p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
318     if( p_data->p_overflow_buffer == NULL )
319     {
320         free( p_data->p_atomic_operations );
321         return -1;
322     }
323     memset( p_data->p_overflow_buffer, 0 , p_data->i_overflow_buffer_size );
324
325     return 0;
326 }
327
328 /*****************************************************************************
329  * DoWork: convert a buffer
330  *****************************************************************************/
331 static void DoWork( filter_t * p_filter,
332                     block_t * p_in_buf, block_t * p_out_buf )
333 {
334     filter_sys_t *p_sys = p_filter->p_sys;
335     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
336     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
337
338     float * p_in = (float*) p_in_buf->p_buffer;
339     float * p_out;
340     uint8_t * p_overflow;
341     uint8_t * p_end_overflow;
342     uint8_t * p_slide;
343
344     size_t i_overflow_size;     /* in bytes */
345     size_t i_out_size;          /* in bytes */
346
347     unsigned int i, j;
348
349     int i_source_channel_offset;
350     int i_dest_channel_offset;
351     unsigned int i_delay;
352     double d_amplitude_factor;
353
354     p_out = (float *)p_out_buf->p_buffer;
355     i_out_size = p_out_buf->i_buffer;
356
357     /* Slide the overflow buffer */
358     p_overflow = (uint8_t *) p_sys->p_overflow_buffer;
359     i_overflow_size = p_sys->i_overflow_buffer_size;
360     p_end_overflow = p_overflow + i_overflow_size;
361
362     memset( p_out, 0, i_out_size );
363     memcpy( p_out, p_overflow, __MIN( i_out_size, i_overflow_size ) );
364
365     p_slide = (uint8_t *) p_sys->p_overflow_buffer;
366     while( p_slide < p_end_overflow )
367     {
368         size_t i_bytes_copied;
369
370         if( p_slide + i_out_size < p_end_overflow )
371         {
372             memset( p_slide, 0, i_out_size );
373             if( p_slide + 2 * i_out_size < p_end_overflow )
374                 i_bytes_copied = i_out_size;
375             else
376                 i_bytes_copied = p_end_overflow - ( p_slide + i_out_size );
377             memcpy( p_slide, p_slide + i_out_size, i_bytes_copied );
378         }
379         else
380         {
381             i_bytes_copied = p_end_overflow - p_slide;
382             memset( p_slide, 0, i_bytes_copied );
383         }
384         p_slide += i_bytes_copied;
385     }
386
387     /* apply the atomic operations */
388     for( i = 0; i < p_sys->i_nb_atomic_operations; i++ )
389     {
390         /* shorter variable names */
391         i_source_channel_offset
392             = p_sys->p_atomic_operations[i].i_source_channel_offset;
393         i_dest_channel_offset
394             = p_sys->p_atomic_operations[i].i_dest_channel_offset;
395         i_delay = p_sys->p_atomic_operations[i].i_delay;
396         d_amplitude_factor
397             = p_sys->p_atomic_operations[i].d_amplitude_factor;
398
399         if( p_out_buf->i_nb_samples > i_delay )
400         {
401             /* current buffer coefficients */
402             for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
403             {
404                 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
405                     += p_in[ j * i_input_nb + i_source_channel_offset ]
406                        * d_amplitude_factor;
407             }
408
409             /* overflow buffer coefficients */
410             for( j = 0; j < i_delay; j++ )
411             {
412                 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
413                     += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
414                        * i_input_nb + i_source_channel_offset ]
415                        * d_amplitude_factor;
416             }
417         }
418         else
419         {
420             /* overflow buffer coefficients only */
421             for( j = 0; j < p_out_buf->i_nb_samples; j++ )
422             {
423                 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
424                                         * i_output_nb + i_dest_channel_offset ]
425                     += p_in[ j * i_input_nb + i_source_channel_offset ]
426                        * d_amplitude_factor;
427             }
428         }
429     }
430 }
431
432 /*
433  * Audio filter 2
434  */
435 /*****************************************************************************
436  * OpenFilter:
437  *****************************************************************************/
438 static int OpenFilter( vlc_object_t *p_this )
439 {
440     filter_t *p_filter = (filter_t *)p_this;
441     filter_sys_t *p_sys;
442
443     /* Activate this filter only with stereo devices */
444     if( p_filter->fmt_out.audio.i_physical_channels
445             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
446     {
447         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
448         return VLC_EGENERIC;
449     }
450
451     /* Allocate the memory needed to store the module's structure */
452     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
453     if( p_sys == NULL )
454         return VLC_ENOMEM;
455     p_sys->i_overflow_buffer_size = 0;
456     p_sys->p_overflow_buffer = NULL;
457     p_sys->i_nb_atomic_operations = 0;
458     p_sys->p_atomic_operations = NULL;
459
460     if( Init( VLC_OBJECT(p_filter), p_sys
461                 , aout_FormatNbChannels ( &(p_filter->fmt_in.audio) )
462                 , p_filter->fmt_in.audio.i_physical_channels
463                 , p_filter->fmt_in.audio.i_rate ) < 0 )
464     {
465         free( p_sys );
466         return VLC_EGENERIC;
467     }
468
469     /* Request a specific format if not already compatible */
470     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
471     p_filter->fmt_out.audio.i_format = VLC_CODEC_FL32;
472     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
473     p_filter->fmt_in.audio.i_chan_mode =
474                                    p_filter->fmt_out.audio.i_chan_mode;
475     if( p_filter->fmt_in.audio.i_physical_channels == AOUT_CHANS_STEREO
476      && (p_filter->fmt_in.audio.i_chan_mode & AOUT_CHANMODE_DOLBYSTEREO)
477      && !var_InheritBool( p_filter, "headphone-dolby" ) )
478     {
479         p_filter->fmt_in.audio.i_physical_channels = AOUT_CHANS_5_0;
480     }
481
482     static const struct vlc_filter_operations filter_ops =
483     {
484         .filter_audio = Convert, .close = CloseFilter,
485     };
486     p_filter->ops = &filter_ops;
487
488     aout_FormatPrepare(&p_filter->fmt_in.audio);
489     aout_FormatPrepare(&p_filter->fmt_out.audio);
490
491     return VLC_SUCCESS;
492 }
493
494 /*****************************************************************************
495  * CloseFilter : deallocate data structures
496  *****************************************************************************/
497 static void CloseFilter( filter_t *p_filter )
498 {
499     filter_sys_t *p_sys = p_filter->p_sys;
500
501     free( p_sys->p_overflow_buffer );
502     free( p_sys->p_atomic_operations );
503     free( p_sys );
504 }
505
506 static block_t *Convert( filter_t *p_filter, block_t *p_block )
507 {
508     if( !p_block || !p_block->i_nb_samples )
509     {
510         if( p_block )
511             block_Release( p_block );
512         return NULL;
513     }
514
515     size_t i_out_size = p_block->i_buffer *
516         aout_FormatNbChannels( &(p_filter->fmt_out.audio) ) /
517         aout_FormatNbChannels( &(p_filter->fmt_in.audio) );
518
519     block_t *p_out = block_Alloc( i_out_size );
520     if( !p_out )
521     {
522         msg_Warn( p_filter, "can't get output buffer" );
523         block_Release( p_block );
524         return NULL;
525     }
526
527     p_out->i_nb_samples = p_block->i_nb_samples;
528     p_out->i_dts = p_block->i_dts;
529     p_out->i_pts = p_block->i_pts;
530     p_out->i_length = p_block->i_length;
531
532     DoWork( p_filter, p_block, p_out );
533
534     block_Release( p_block );
535     return p_out;
536 }