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
8 * Authors: Boris Dorès <babal@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <math.h> /* sqrt */
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_filter.h>
39 #include <vlc_block.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
51 static int OpenFilter ( vlc_object_t * );
52 static void CloseFilter( vlc_object_t * );
53 static block_t *Convert( filter_t *, block_t * );
55 /*****************************************************************************
57 *****************************************************************************/
58 #define MODULE_DESCRIPTION N_ ( \
59 "This effect gives you the feeling that you are standing in a room " \
60 "with a complete 7.1 speaker set when using only a headphone, " \
61 "providing a more realistic sound experience. It should also be " \
62 "more comfortable and less tiring when listening to music for " \
63 "long periods of time.\nIt works with any source format from mono " \
66 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
67 #define HEADPHONE_DIM_LONGTEXT N_( \
68 "Distance between front left speaker and listener in meters.")
70 #define HEADPHONE_COMPENSATE_TEXT N_("Compensate delay")
71 #define HEADPHONE_COMPENSATE_LONGTEXT N_( \
72 "The delay which is introduced by the physical algorithm may "\
73 "sometimes be disturbing for the synchronization between lips-movement "\
74 "and speech. In case, turn this on to compensate.")
76 #define HEADPHONE_DOLBY_TEXT N_("No decoding of Dolby Surround")
77 #define HEADPHONE_DOLBY_LONGTEXT N_( \
78 "Dolby Surround encoded streams won't be decoded before being " \
79 "processed by this filter. Enabling this setting is not recommended.")
82 set_description( N_("Headphone virtual spatialization effect") );
83 set_shortname( N_("Headphone effect") );
84 set_help( MODULE_DESCRIPTION );
85 set_category( CAT_AUDIO );
86 set_subcategory( SUBCAT_AUDIO_AFILTER );
88 add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
89 HEADPHONE_DIM_LONGTEXT, false );
90 add_bool( "headphone-compensate", 0, NULL, HEADPHONE_COMPENSATE_TEXT,
91 HEADPHONE_COMPENSATE_LONGTEXT, true );
92 add_bool( "headphone-dolby", 0, NULL, HEADPHONE_DOLBY_TEXT,
93 HEADPHONE_DOLBY_LONGTEXT, true );
95 set_capability( "audio filter", 0 );
96 set_callbacks( Create, Destroy );
97 add_shortcut( "headphone" );
101 set_description( N_("Headphone virtual spatialization effect") );
102 set_capability( "audio filter2", 0 );
103 set_callbacks( OpenFilter, CloseFilter );
107 /*****************************************************************************
108 * Internal data structures
109 *****************************************************************************/
110 struct atomic_operation_t
112 int i_source_channel_offset;
113 int i_dest_channel_offset;
114 unsigned int i_delay;/* in sample unit */
115 double d_amplitude_factor;
118 struct aout_filter_sys_t
120 size_t i_overflow_buffer_size;/* in bytes */
121 uint8_t * p_overflow_buffer;
122 unsigned int i_nb_atomic_operations;
123 struct atomic_operation_t * p_atomic_operations;
128 size_t i_overflow_buffer_size;/* in bytes */
129 uint8_t * p_overflow_buffer;
130 unsigned int i_nb_atomic_operations;
131 struct atomic_operation_t * p_atomic_operations;
134 /*****************************************************************************
135 * Init: initialize internal data structures
136 * and computes the needed atomic operations
137 *****************************************************************************/
138 /* x and z represent the coordinates of the virtual speaker
139 * relatively to the center of the listener's head, measured in meters :
148 * rear left rear right
152 static void ComputeChannelOperations( struct aout_filter_sys_t * p_data
153 , unsigned int i_rate, unsigned int i_next_atomic_operation
154 , int i_source_channel_offset, double d_x, double d_z
155 , double d_compensation_length, double d_channel_amplitude_factor )
157 double d_c = 340; /*sound celerity (unit: m/s)*/
158 double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
161 p_data->p_atomic_operations[i_next_atomic_operation]
162 .i_source_channel_offset = i_source_channel_offset;
163 p_data->p_atomic_operations[i_next_atomic_operation]
164 .i_dest_channel_offset = 0;/* left */
165 p_data->p_atomic_operations[i_next_atomic_operation]
166 .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
167 / d_c * i_rate - d_compensation_delay );
170 p_data->p_atomic_operations[i_next_atomic_operation]
171 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
175 p_data->p_atomic_operations[i_next_atomic_operation]
176 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
180 p_data->p_atomic_operations[i_next_atomic_operation]
181 .d_amplitude_factor = d_channel_amplitude_factor / 2;
185 p_data->p_atomic_operations[i_next_atomic_operation + 1]
186 .i_source_channel_offset = i_source_channel_offset;
187 p_data->p_atomic_operations[i_next_atomic_operation + 1]
188 .i_dest_channel_offset = 1;/* right */
189 p_data->p_atomic_operations[i_next_atomic_operation + 1]
190 .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
191 / d_c * i_rate - d_compensation_delay );
194 p_data->p_atomic_operations[i_next_atomic_operation + 1]
195 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
199 p_data->p_atomic_operations[i_next_atomic_operation + 1]
200 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
204 p_data->p_atomic_operations[i_next_atomic_operation + 1]
205 .d_amplitude_factor = d_channel_amplitude_factor / 2;
209 static int Init( vlc_object_t *p_this, struct aout_filter_sys_t * p_data
210 , unsigned int i_nb_channels, uint32_t i_physical_channels
211 , unsigned int i_rate )
213 double d_x = config_GetInt( p_this, "headphone-dim" );
215 double d_z_rear = -d_x/3;
217 unsigned int i_next_atomic_operation;
218 int i_source_channel_offset;
223 msg_Dbg( p_this, "passing a null pointer as argument" );
227 if( config_GetInt( p_this, "headphone-compensate" ) )
229 /* minimal distance to any speaker */
230 if( i_physical_channels & AOUT_CHAN_REARCENTER )
240 /* Number of elementary operations */
241 p_data->i_nb_atomic_operations = i_nb_channels * 2;
242 if( i_physical_channels & AOUT_CHAN_CENTER )
244 p_data->i_nb_atomic_operations += 2;
246 p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
247 * p_data->i_nb_atomic_operations );
248 if( p_data->p_atomic_operations == NULL )
251 /* For each virtual speaker, computes elementary wave propagation time
253 i_next_atomic_operation = 0;
254 i_source_channel_offset = 0;
255 if( i_physical_channels & AOUT_CHAN_LEFT )
257 ComputeChannelOperations( p_data , i_rate
258 , i_next_atomic_operation , i_source_channel_offset
259 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
260 i_next_atomic_operation += 2;
261 i_source_channel_offset++;
263 if( i_physical_channels & AOUT_CHAN_RIGHT )
265 ComputeChannelOperations( p_data , i_rate
266 , i_next_atomic_operation , i_source_channel_offset
267 , d_x , d_z , d_min , 2.0 / i_nb_channels );
268 i_next_atomic_operation += 2;
269 i_source_channel_offset++;
271 if( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
273 ComputeChannelOperations( p_data , i_rate
274 , i_next_atomic_operation , i_source_channel_offset
275 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
276 i_next_atomic_operation += 2;
277 i_source_channel_offset++;
279 if( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
281 ComputeChannelOperations( p_data , i_rate
282 , i_next_atomic_operation , i_source_channel_offset
283 , d_x , 0 , d_min , 1.5 / i_nb_channels );
284 i_next_atomic_operation += 2;
285 i_source_channel_offset++;
287 if( i_physical_channels & AOUT_CHAN_REARLEFT )
289 ComputeChannelOperations( p_data , i_rate
290 , i_next_atomic_operation , i_source_channel_offset
291 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
292 i_next_atomic_operation += 2;
293 i_source_channel_offset++;
295 if( i_physical_channels & AOUT_CHAN_REARRIGHT )
297 ComputeChannelOperations( p_data , i_rate
298 , i_next_atomic_operation , i_source_channel_offset
299 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
300 i_next_atomic_operation += 2;
301 i_source_channel_offset++;
303 if( i_physical_channels & AOUT_CHAN_REARCENTER )
305 ComputeChannelOperations( p_data , i_rate
306 , i_next_atomic_operation , i_source_channel_offset
307 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
308 i_next_atomic_operation += 2;
309 i_source_channel_offset++;
311 if( i_physical_channels & AOUT_CHAN_CENTER )
313 /* having two center channels increases the spatialization effect */
314 ComputeChannelOperations( p_data , i_rate
315 , i_next_atomic_operation , i_source_channel_offset
316 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
317 i_next_atomic_operation += 2;
318 ComputeChannelOperations( p_data , i_rate
319 , i_next_atomic_operation , i_source_channel_offset
320 , -d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
321 i_next_atomic_operation += 2;
322 i_source_channel_offset++;
324 if( i_physical_channels & AOUT_CHAN_LFE )
326 ComputeChannelOperations( p_data , i_rate
327 , i_next_atomic_operation , i_source_channel_offset
328 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
329 i_next_atomic_operation += 2;
330 i_source_channel_offset++;
333 /* Initialize the overflow buffer
334 * we need it because the process induce a delay in the samples */
335 p_data->i_overflow_buffer_size = 0;
336 for( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
338 if( p_data->i_overflow_buffer_size
339 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float) )
341 p_data->i_overflow_buffer_size
342 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float);
345 p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
346 if( p_data->p_atomic_operations == NULL )
348 memset( p_data->p_overflow_buffer, 0 , p_data->i_overflow_buffer_size );
354 /*****************************************************************************
355 * Create: allocate headphone downmixer
356 *****************************************************************************/
357 static int Create( vlc_object_t *p_this )
359 aout_filter_t * p_filter = (aout_filter_t *)p_this;
362 /* Activate this filter only with stereo devices */
363 if( p_filter->output.i_physical_channels
364 != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
366 msg_Dbg( p_filter, "filter discarded (incompatible format)" );
370 /* Request a specific format if not already compatible */
371 if( p_filter->input.i_original_channels
372 != p_filter->output.i_original_channels )
375 p_filter->input.i_original_channels =
376 p_filter->output.i_original_channels;
378 if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
379 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
382 p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
383 p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
385 if( p_filter->input.i_rate != p_filter->output.i_rate )
388 p_filter->input.i_rate = p_filter->output.i_rate;
390 if( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
391 && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
392 && ! config_GetInt ( p_filter , "headphone-dolby" ) )
395 p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
402 msg_Dbg( p_filter, "requesting specific format" );
406 /* Allocate the memory needed to store the module's structure */
407 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
408 if( p_filter->p_sys == NULL )
410 p_filter->p_sys->i_overflow_buffer_size = 0;
411 p_filter->p_sys->p_overflow_buffer = NULL;
412 p_filter->p_sys->i_nb_atomic_operations = 0;
413 p_filter->p_sys->p_atomic_operations = NULL;
415 if( Init( VLC_OBJECT(p_filter), p_filter->p_sys
416 , aout_FormatNbChannels ( &p_filter->input )
417 , p_filter->input.i_physical_channels
418 , p_filter->input.i_rate ) < 0 )
423 p_filter->pf_do_work = DoWork;
424 p_filter->b_in_place = 0;
429 /*****************************************************************************
430 * Destroy: deallocate resources associated with headphone downmixer
431 *****************************************************************************/
432 static void Destroy( vlc_object_t *p_this )
434 aout_filter_t * p_filter = (aout_filter_t *)p_this;
436 if( p_filter->p_sys != NULL )
438 if( p_filter->p_sys->p_overflow_buffer != NULL )
440 free( p_filter->p_sys->p_overflow_buffer );
442 if( p_filter->p_sys->p_atomic_operations != NULL )
444 free( p_filter->p_sys->p_atomic_operations );
446 free( p_filter->p_sys );
447 p_filter->p_sys = NULL;
451 /*****************************************************************************
452 * DoWork: convert a buffer
453 *****************************************************************************/
454 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
455 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
458 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
459 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
461 float * p_in = (float*) p_in_buf->p_buffer;
463 uint8_t * p_overflow;
466 size_t i_overflow_size; /* in bytes */
467 size_t i_out_size; /* in bytes */
471 int i_source_channel_offset;
472 int i_dest_channel_offset;
473 unsigned int i_delay;
474 double d_amplitude_factor;
476 /* out buffer characterisitcs */
477 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
478 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
479 p_out = p_out_buf->p_buffer;
480 i_out_size = p_out_buf->i_nb_bytes;
482 if( p_filter->p_sys != NULL )
484 /* Slide the overflow buffer */
485 p_overflow = p_filter->p_sys->p_overflow_buffer;
486 i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
488 memset( p_out, 0, i_out_size );
489 if ( i_out_size > i_overflow_size )
490 memcpy( p_out, p_overflow, i_overflow_size );
492 memcpy( p_out, p_overflow, i_out_size );
494 p_slide = p_filter->p_sys->p_overflow_buffer;
495 while( p_slide < p_overflow + i_overflow_size )
497 if( p_slide + i_out_size < p_overflow + i_overflow_size )
499 memset( p_slide, 0, i_out_size );
500 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
501 memcpy( p_slide, p_slide + i_out_size, i_out_size );
503 memcpy( p_slide, p_slide + i_out_size,
504 p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
508 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
510 p_slide += i_out_size;
513 /* apply the atomic operations */
514 for( i = 0; i < p_filter->p_sys->i_nb_atomic_operations; i++ )
516 /* shorter variable names */
517 i_source_channel_offset
518 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
519 i_dest_channel_offset
520 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
521 i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
523 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
525 if( p_out_buf->i_nb_samples > i_delay )
527 /* current buffer coefficients */
528 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
530 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
531 += p_in[ j * i_input_nb + i_source_channel_offset ]
532 * d_amplitude_factor;
535 /* overflow buffer coefficients */
536 for( j = 0; j < i_delay; j++ )
538 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
539 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
540 * i_input_nb + i_source_channel_offset ]
541 * d_amplitude_factor;
546 /* overflow buffer coefficients only */
547 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
549 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
550 * i_output_nb + i_dest_channel_offset ]
551 += p_in[ j * i_input_nb + i_source_channel_offset ]
552 * d_amplitude_factor;
559 memset( p_out, 0, i_out_size );
566 /*****************************************************************************
568 *****************************************************************************/
569 static int OpenFilter( vlc_object_t *p_this )
571 filter_t *p_filter = (filter_t *)p_this;
574 /* Activate this filter only with stereo devices */
575 if( p_filter->fmt_out.audio.i_physical_channels
576 != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
578 msg_Dbg( p_filter, "filter discarded (incompatible format)" );
582 /* Request a specific format if not already compatible */
583 if( p_filter->fmt_in.audio.i_original_channels
584 != p_filter->fmt_out.audio.i_original_channels )
587 p_filter->fmt_in.audio.i_original_channels =
588 p_filter->fmt_out.audio.i_original_channels;
590 if( p_filter->fmt_in.audio.i_format != VLC_FOURCC('f','l','3','2')
591 || p_filter->fmt_out.audio.i_format != VLC_FOURCC('f','l','3','2') )
594 p_filter->fmt_in.audio.i_format = VLC_FOURCC('f','l','3','2');
595 p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
597 if( p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate )
600 p_filter->fmt_in.audio.i_rate = p_filter->fmt_out.audio.i_rate;
602 if( p_filter->fmt_in.audio.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
603 && ( p_filter->fmt_in.audio.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
604 && !config_GetInt( p_filter, "headphone-dolby" ) )
607 p_filter->fmt_in.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
614 msg_Dbg( p_filter, "requesting specific format" );
618 /* Allocate the memory needed to store the module's structure */
619 p_filter->p_sys = malloc( sizeof(struct filter_sys_t) );
620 if( p_filter->p_sys == NULL )
622 p_filter->p_sys->i_overflow_buffer_size = 0;
623 p_filter->p_sys->p_overflow_buffer = NULL;
624 p_filter->p_sys->i_nb_atomic_operations = 0;
625 p_filter->p_sys->p_atomic_operations = NULL;
627 if( Init( VLC_OBJECT(p_filter), (struct aout_filter_sys_t *)p_filter->p_sys
628 , aout_FormatNbChannels ( &(p_filter->fmt_in.audio) )
629 , p_filter->fmt_in.audio.i_physical_channels
630 , p_filter->fmt_in.audio.i_rate ) < 0 )
635 p_filter->pf_audio_filter = Convert;
636 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
641 /*****************************************************************************
642 * CloseFilter : deallocate data structures
643 *****************************************************************************/
644 static void CloseFilter( vlc_object_t *p_this )
646 filter_t *p_filter = (filter_t *)p_this;
648 if( p_filter->p_sys != NULL )
650 if( p_filter->p_sys->p_overflow_buffer != NULL )
652 free ( p_filter->p_sys->p_overflow_buffer );
654 if( p_filter->p_sys->p_atomic_operations != NULL )
656 free ( p_filter->p_sys->p_atomic_operations );
658 free( p_filter->p_sys );
659 p_filter->p_sys = NULL;
663 static block_t *Convert( filter_t *p_filter, block_t *p_block )
665 aout_filter_t aout_filter;
666 aout_buffer_t in_buf, out_buf;
670 if( !p_block || !p_block->i_samples )
673 block_Release( p_block );
677 i_out_size = p_block->i_samples *
678 p_filter->fmt_out.audio.i_bitspersample/8 *
679 aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
681 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
684 msg_Warn( p_filter, "can't get output buffer" );
685 block_Release( p_block );
689 p_out->i_samples = p_block->i_samples;
690 p_out->i_dts = p_block->i_dts;
691 p_out->i_pts = p_block->i_pts;
692 p_out->i_length = p_block->i_length;
694 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
695 aout_filter.input = p_filter->fmt_in.audio;
696 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
697 aout_filter.output = p_filter->fmt_out.audio;
698 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
699 aout_filter.b_in_place = 0;
701 in_buf.p_buffer = p_block->p_buffer;
702 in_buf.i_nb_bytes = p_block->i_buffer;
703 in_buf.i_nb_samples = p_block->i_samples;
704 out_buf.p_buffer = p_out->p_buffer;
705 out_buf.i_nb_bytes = p_out->i_buffer;
706 out_buf.i_nb_samples = p_out->i_samples;
708 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
710 p_out->i_buffer = out_buf.i_nb_bytes;
711 p_out->i_samples = out_buf.i_nb_samples;
713 block_Release( p_block );