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 *****************************************************************************/
28 #include <math.h> /* sqrt */
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_block.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
46 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
50 static int OpenFilter ( vlc_object_t * );
51 static void CloseFilter( vlc_object_t * );
52 static block_t *Convert( filter_t *, block_t * );
54 /*****************************************************************************
56 *****************************************************************************/
57 #define MODULE_DESCRIPTION N_ ( \
58 "This effect gives you the feeling that you are standing in a room " \
59 "with a complete 7.1 speaker set when using only a headphone, " \
60 "providing a more realistic sound experience. It should also be " \
61 "more comfortable and less tiring when listening to music for " \
62 "long periods of time.\nIt works with any source format from mono " \
65 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
66 #define HEADPHONE_DIM_LONGTEXT N_( \
67 "Distance between front left speaker and listener in meters.")
69 #define HEADPHONE_COMPENSATE_TEXT N_("Compensate delay")
70 #define HEADPHONE_COMPENSATE_LONGTEXT N_( \
71 "The delay which is introduced by the physical algorithm may "\
72 "sometimes be disturbing for the synchronization between lips-movement "\
73 "and speech. In case, turn this on to compensate.")
75 #define HEADPHONE_DOLBY_TEXT N_("No decoding of Dolby Surround")
76 #define HEADPHONE_DOLBY_LONGTEXT N_( \
77 "Dolby Surround encoded streams won't be decoded before being " \
78 "processed by this filter. Enabling this setting is not recommended.")
81 set_description( N_("Headphone virtual spatialization effect") );
82 set_shortname( N_("Headphone effect") );
83 set_help( MODULE_DESCRIPTION );
84 set_category( CAT_AUDIO );
85 set_subcategory( SUBCAT_AUDIO_AFILTER );
87 add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
88 HEADPHONE_DIM_LONGTEXT, false );
89 add_bool( "headphone-compensate", 0, NULL, HEADPHONE_COMPENSATE_TEXT,
90 HEADPHONE_COMPENSATE_LONGTEXT, true );
91 add_bool( "headphone-dolby", 0, NULL, HEADPHONE_DOLBY_TEXT,
92 HEADPHONE_DOLBY_LONGTEXT, true );
94 set_capability( "audio filter", 0 );
95 set_callbacks( Create, Destroy );
96 add_shortcut( "headphone" );
100 set_description( N_("Headphone virtual spatialization effect") );
101 set_capability( "audio filter2", 0 );
102 set_callbacks( OpenFilter, CloseFilter );
106 /*****************************************************************************
107 * Internal data structures
108 *****************************************************************************/
109 struct atomic_operation_t
111 int i_source_channel_offset;
112 int i_dest_channel_offset;
113 unsigned int i_delay;/* in sample unit */
114 double d_amplitude_factor;
117 struct aout_filter_sys_t
119 size_t i_overflow_buffer_size;/* in bytes */
120 uint8_t * p_overflow_buffer;
121 unsigned int i_nb_atomic_operations;
122 struct atomic_operation_t * p_atomic_operations;
127 size_t i_overflow_buffer_size;/* in bytes */
128 uint8_t * p_overflow_buffer;
129 unsigned int i_nb_atomic_operations;
130 struct atomic_operation_t * p_atomic_operations;
133 /*****************************************************************************
134 * Init: initialize internal data structures
135 * and computes the needed atomic operations
136 *****************************************************************************/
137 /* x and z represent the coordinates of the virtual speaker
138 * relatively to the center of the listener's head, measured in meters :
147 * rear left rear right
151 static void ComputeChannelOperations( struct aout_filter_sys_t * p_data
152 , unsigned int i_rate, unsigned int i_next_atomic_operation
153 , int i_source_channel_offset, double d_x, double d_z
154 , double d_compensation_length, double d_channel_amplitude_factor )
156 double d_c = 340; /*sound celerity (unit: m/s)*/
157 double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
160 p_data->p_atomic_operations[i_next_atomic_operation]
161 .i_source_channel_offset = i_source_channel_offset;
162 p_data->p_atomic_operations[i_next_atomic_operation]
163 .i_dest_channel_offset = 0;/* left */
164 p_data->p_atomic_operations[i_next_atomic_operation]
165 .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
166 / d_c * i_rate - d_compensation_delay );
169 p_data->p_atomic_operations[i_next_atomic_operation]
170 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
174 p_data->p_atomic_operations[i_next_atomic_operation]
175 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
179 p_data->p_atomic_operations[i_next_atomic_operation]
180 .d_amplitude_factor = d_channel_amplitude_factor / 2;
184 p_data->p_atomic_operations[i_next_atomic_operation + 1]
185 .i_source_channel_offset = i_source_channel_offset;
186 p_data->p_atomic_operations[i_next_atomic_operation + 1]
187 .i_dest_channel_offset = 1;/* right */
188 p_data->p_atomic_operations[i_next_atomic_operation + 1]
189 .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
190 / d_c * i_rate - d_compensation_delay );
193 p_data->p_atomic_operations[i_next_atomic_operation + 1]
194 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
198 p_data->p_atomic_operations[i_next_atomic_operation + 1]
199 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
203 p_data->p_atomic_operations[i_next_atomic_operation + 1]
204 .d_amplitude_factor = d_channel_amplitude_factor / 2;
208 static int Init( vlc_object_t *p_this, struct aout_filter_sys_t * p_data
209 , unsigned int i_nb_channels, uint32_t i_physical_channels
210 , unsigned int i_rate )
212 double d_x = config_GetInt( p_this, "headphone-dim" );
214 double d_z_rear = -d_x/3;
216 unsigned int i_next_atomic_operation;
217 int i_source_channel_offset;
222 msg_Dbg( p_this, "passing a null pointer as argument" );
226 if( config_GetInt( p_this, "headphone-compensate" ) )
228 /* minimal distance to any speaker */
229 if( i_physical_channels & AOUT_CHAN_REARCENTER )
239 /* Number of elementary operations */
240 p_data->i_nb_atomic_operations = i_nb_channels * 2;
241 if( i_physical_channels & AOUT_CHAN_CENTER )
243 p_data->i_nb_atomic_operations += 2;
245 p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
246 * p_data->i_nb_atomic_operations );
247 if( p_data->p_atomic_operations == NULL )
250 /* For each virtual speaker, computes elementary wave propagation time
252 i_next_atomic_operation = 0;
253 i_source_channel_offset = 0;
254 if( i_physical_channels & AOUT_CHAN_LEFT )
256 ComputeChannelOperations( p_data , i_rate
257 , i_next_atomic_operation , i_source_channel_offset
258 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
259 i_next_atomic_operation += 2;
260 i_source_channel_offset++;
262 if( i_physical_channels & AOUT_CHAN_RIGHT )
264 ComputeChannelOperations( p_data , i_rate
265 , i_next_atomic_operation , i_source_channel_offset
266 , d_x , d_z , d_min , 2.0 / i_nb_channels );
267 i_next_atomic_operation += 2;
268 i_source_channel_offset++;
270 if( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
272 ComputeChannelOperations( p_data , i_rate
273 , i_next_atomic_operation , i_source_channel_offset
274 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
275 i_next_atomic_operation += 2;
276 i_source_channel_offset++;
278 if( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
280 ComputeChannelOperations( p_data , i_rate
281 , i_next_atomic_operation , i_source_channel_offset
282 , d_x , 0 , d_min , 1.5 / i_nb_channels );
283 i_next_atomic_operation += 2;
284 i_source_channel_offset++;
286 if( i_physical_channels & AOUT_CHAN_REARLEFT )
288 ComputeChannelOperations( p_data , i_rate
289 , i_next_atomic_operation , i_source_channel_offset
290 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
291 i_next_atomic_operation += 2;
292 i_source_channel_offset++;
294 if( i_physical_channels & AOUT_CHAN_REARRIGHT )
296 ComputeChannelOperations( p_data , i_rate
297 , i_next_atomic_operation , i_source_channel_offset
298 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
299 i_next_atomic_operation += 2;
300 i_source_channel_offset++;
302 if( i_physical_channels & AOUT_CHAN_REARCENTER )
304 ComputeChannelOperations( p_data , i_rate
305 , i_next_atomic_operation , i_source_channel_offset
306 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
307 i_next_atomic_operation += 2;
308 i_source_channel_offset++;
310 if( i_physical_channels & AOUT_CHAN_CENTER )
312 /* having two center channels increases the spatialization effect */
313 ComputeChannelOperations( p_data , i_rate
314 , i_next_atomic_operation , i_source_channel_offset
315 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
316 i_next_atomic_operation += 2;
317 ComputeChannelOperations( p_data , i_rate
318 , i_next_atomic_operation , i_source_channel_offset
319 , -d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
320 i_next_atomic_operation += 2;
321 i_source_channel_offset++;
323 if( i_physical_channels & AOUT_CHAN_LFE )
325 ComputeChannelOperations( p_data , i_rate
326 , i_next_atomic_operation , i_source_channel_offset
327 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
328 i_next_atomic_operation += 2;
329 i_source_channel_offset++;
332 /* Initialize the overflow buffer
333 * we need it because the process induce a delay in the samples */
334 p_data->i_overflow_buffer_size = 0;
335 for( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
337 if( p_data->i_overflow_buffer_size
338 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float) )
340 p_data->i_overflow_buffer_size
341 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float);
344 p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
345 if( p_data->p_atomic_operations == NULL )
347 memset( p_data->p_overflow_buffer, 0 , p_data->i_overflow_buffer_size );
353 /*****************************************************************************
354 * Create: allocate headphone downmixer
355 *****************************************************************************/
356 static int Create( vlc_object_t *p_this )
358 aout_filter_t * p_filter = (aout_filter_t *)p_this;
361 /* Activate this filter only with stereo devices */
362 if( p_filter->output.i_physical_channels
363 != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
365 msg_Dbg( p_filter, "filter discarded (incompatible format)" );
369 /* Request a specific format if not already compatible */
370 if( p_filter->input.i_original_channels
371 != p_filter->output.i_original_channels )
374 p_filter->input.i_original_channels =
375 p_filter->output.i_original_channels;
377 if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
378 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
381 p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
382 p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
384 if( p_filter->input.i_rate != p_filter->output.i_rate )
387 p_filter->input.i_rate = p_filter->output.i_rate;
389 if( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
390 && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
391 && ! config_GetInt ( p_filter , "headphone-dolby" ) )
394 p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
401 msg_Dbg( p_filter, "requesting specific format" );
405 /* Allocate the memory needed to store the module's structure */
406 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
407 if( p_filter->p_sys == NULL )
409 p_filter->p_sys->i_overflow_buffer_size = 0;
410 p_filter->p_sys->p_overflow_buffer = NULL;
411 p_filter->p_sys->i_nb_atomic_operations = 0;
412 p_filter->p_sys->p_atomic_operations = NULL;
414 if( Init( VLC_OBJECT(p_filter), p_filter->p_sys
415 , aout_FormatNbChannels ( &p_filter->input )
416 , p_filter->input.i_physical_channels
417 , p_filter->input.i_rate ) < 0 )
422 p_filter->pf_do_work = DoWork;
423 p_filter->b_in_place = 0;
428 /*****************************************************************************
429 * Destroy: deallocate resources associated with headphone downmixer
430 *****************************************************************************/
431 static void Destroy( vlc_object_t *p_this )
433 aout_filter_t * p_filter = (aout_filter_t *)p_this;
435 if( p_filter->p_sys != NULL )
437 if( p_filter->p_sys->p_overflow_buffer != NULL )
439 free( p_filter->p_sys->p_overflow_buffer );
441 if( p_filter->p_sys->p_atomic_operations != NULL )
443 free( p_filter->p_sys->p_atomic_operations );
445 free( p_filter->p_sys );
446 p_filter->p_sys = NULL;
450 /*****************************************************************************
451 * DoWork: convert a buffer
452 *****************************************************************************/
453 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
454 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
457 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
458 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
460 float * p_in = (float*) p_in_buf->p_buffer;
462 uint8_t * p_overflow;
465 size_t i_overflow_size; /* in bytes */
466 size_t i_out_size; /* in bytes */
470 int i_source_channel_offset;
471 int i_dest_channel_offset;
472 unsigned int i_delay;
473 double d_amplitude_factor;
475 /* out buffer characterisitcs */
476 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
477 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
478 p_out = p_out_buf->p_buffer;
479 i_out_size = p_out_buf->i_nb_bytes;
481 if( p_filter->p_sys != NULL )
483 /* Slide the overflow buffer */
484 p_overflow = p_filter->p_sys->p_overflow_buffer;
485 i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
487 memset( p_out, 0, i_out_size );
488 if ( i_out_size > i_overflow_size )
489 memcpy( p_out, p_overflow, i_overflow_size );
491 memcpy( p_out, p_overflow, i_out_size );
493 p_slide = p_filter->p_sys->p_overflow_buffer;
494 while( p_slide < p_overflow + i_overflow_size )
496 if( p_slide + i_out_size < p_overflow + i_overflow_size )
498 memset( p_slide, 0, i_out_size );
499 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
500 memcpy( p_slide, p_slide + i_out_size, i_out_size );
502 memcpy( p_slide, p_slide + i_out_size,
503 p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
507 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
509 p_slide += i_out_size;
512 /* apply the atomic operations */
513 for( i = 0; i < p_filter->p_sys->i_nb_atomic_operations; i++ )
515 /* shorter variable names */
516 i_source_channel_offset
517 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
518 i_dest_channel_offset
519 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
520 i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
522 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
524 if( p_out_buf->i_nb_samples > i_delay )
526 /* current buffer coefficients */
527 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
529 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
530 += p_in[ j * i_input_nb + i_source_channel_offset ]
531 * d_amplitude_factor;
534 /* overflow buffer coefficients */
535 for( j = 0; j < i_delay; j++ )
537 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
538 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
539 * i_input_nb + i_source_channel_offset ]
540 * d_amplitude_factor;
545 /* overflow buffer coefficients only */
546 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
548 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
549 * i_output_nb + i_dest_channel_offset ]
550 += p_in[ j * i_input_nb + i_source_channel_offset ]
551 * d_amplitude_factor;
558 memset( p_out, 0, i_out_size );
565 /*****************************************************************************
567 *****************************************************************************/
568 static int OpenFilter( vlc_object_t *p_this )
570 filter_t *p_filter = (filter_t *)p_this;
573 /* Activate this filter only with stereo devices */
574 if( p_filter->fmt_out.audio.i_physical_channels
575 != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
577 msg_Dbg( p_filter, "filter discarded (incompatible format)" );
581 /* Request a specific format if not already compatible */
582 if( p_filter->fmt_in.audio.i_original_channels
583 != p_filter->fmt_out.audio.i_original_channels )
586 p_filter->fmt_in.audio.i_original_channels =
587 p_filter->fmt_out.audio.i_original_channels;
589 if( p_filter->fmt_in.audio.i_format != VLC_FOURCC('f','l','3','2')
590 || p_filter->fmt_out.audio.i_format != VLC_FOURCC('f','l','3','2') )
593 p_filter->fmt_in.audio.i_format = VLC_FOURCC('f','l','3','2');
594 p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
596 if( p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate )
599 p_filter->fmt_in.audio.i_rate = p_filter->fmt_out.audio.i_rate;
601 if( p_filter->fmt_in.audio.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
602 && ( p_filter->fmt_in.audio.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
603 && !config_GetInt( p_filter, "headphone-dolby" ) )
606 p_filter->fmt_in.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
613 msg_Dbg( p_filter, "requesting specific format" );
617 /* Allocate the memory needed to store the module's structure */
618 p_filter->p_sys = malloc( sizeof(struct filter_sys_t) );
619 if( p_filter->p_sys == NULL )
621 p_filter->p_sys->i_overflow_buffer_size = 0;
622 p_filter->p_sys->p_overflow_buffer = NULL;
623 p_filter->p_sys->i_nb_atomic_operations = 0;
624 p_filter->p_sys->p_atomic_operations = NULL;
626 if( Init( VLC_OBJECT(p_filter), (struct aout_filter_sys_t *)p_filter->p_sys
627 , aout_FormatNbChannels ( &(p_filter->fmt_in.audio) )
628 , p_filter->fmt_in.audio.i_physical_channels
629 , p_filter->fmt_in.audio.i_rate ) < 0 )
634 p_filter->pf_audio_filter = Convert;
635 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
640 /*****************************************************************************
641 * CloseFilter : deallocate data structures
642 *****************************************************************************/
643 static void CloseFilter( vlc_object_t *p_this )
645 filter_t *p_filter = (filter_t *)p_this;
647 if( p_filter->p_sys != NULL )
649 if( p_filter->p_sys->p_overflow_buffer != NULL )
651 free ( p_filter->p_sys->p_overflow_buffer );
653 if( p_filter->p_sys->p_atomic_operations != NULL )
655 free ( p_filter->p_sys->p_atomic_operations );
657 free( p_filter->p_sys );
658 p_filter->p_sys = NULL;
662 static block_t *Convert( filter_t *p_filter, block_t *p_block )
664 aout_filter_t aout_filter;
665 aout_buffer_t in_buf, out_buf;
669 if( !p_block || !p_block->i_samples )
671 if( p_block ) p_block->pf_release( p_block );
675 i_out_size = p_block->i_samples *
676 p_filter->fmt_out.audio.i_bitspersample/8 *
677 aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
679 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
682 msg_Warn( p_filter, "can't get output buffer" );
683 if( p_block ) p_block->pf_release( p_block );
687 p_out->i_samples = p_block->i_samples;
688 p_out->i_dts = p_block->i_dts;
689 p_out->i_pts = p_block->i_pts;
690 p_out->i_length = p_block->i_length;
692 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
693 aout_filter.input = p_filter->fmt_in.audio;
694 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
695 aout_filter.output = p_filter->fmt_out.audio;
696 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
697 aout_filter.b_in_place = 0;
699 in_buf.p_buffer = p_block->p_buffer;
700 in_buf.i_nb_bytes = p_block->i_buffer;
701 in_buf.i_nb_samples = p_block->i_samples;
702 out_buf.p_buffer = p_out->p_buffer;
703 out_buf.i_nb_bytes = p_out->i_buffer;
704 out_buf.i_nb_samples = p_out->i_samples;
706 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
708 p_out->i_buffer = out_buf.i_nb_bytes;
709 p_out->i_samples = out_buf.i_nb_samples;
711 if( p_block ) p_block->pf_release( p_block );