1 /******************************************************************************
2 * audio_output.c : audio output thread
4 ******************************************************************************/
8 * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
10 * - Faire les optimisations dans les fonctions threads :
11 * = Stocker les "petits calculs" dans des variables au lieu de les refaire
13 * = Utiliser des tables pour les gros calculs
14 * - Faire une structure différente pour intf/adec fifo
15 * - Rajouter des vlc_cond_signal ?
19 /******************************************************************************
21 ******************************************************************************/
24 #include <sys/soundcard.h>
25 #include <stdio.h> /* "intf_msg.h" */
26 #include <stdlib.h> /* calloc(), malloc(), free() */
30 #include "mtime.h" /* mtime_t, mdate(), msleep() */
31 #include "vlc_thread.h"
33 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
35 #include "audio_output.h"
36 #include "audio_dsp.h"
39 /******************************************************************************
41 ******************************************************************************/
44 static int aout_SpawnThread( aout_thread_t * p_aout );
46 /* Creating as much aout_Thread functions as configurations is one solution,
47 * examining the different cases in the Thread loop of an unique function is
48 * another. I chose the first solution. */
49 void aout_Thread_S8_Mono ( aout_thread_t * p_aout );
50 void aout_Thread_U8_Mono ( aout_thread_t * p_aout );
51 void aout_Thread_S16_Mono ( aout_thread_t * p_aout );
52 void aout_Thread_U16_Mono ( aout_thread_t * p_aout );
53 void aout_Thread_S8_Stereo ( aout_thread_t * p_aout );
54 void aout_Thread_U8_Stereo ( aout_thread_t * p_aout );
55 void aout_Thread_S16_Stereo ( aout_thread_t * p_aout );
56 void aout_Thread_U16_Stereo ( aout_thread_t * p_aout );
58 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
59 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo/*, mtime_t aout_date*/ );
61 /******************************************************************************
62 * aout_CreateThread: initialize audio thread
63 ******************************************************************************/
64 aout_thread_t *aout_CreateThread( int *pi_status )
66 aout_thread_t * p_aout; /* thread descriptor */
67 int i_status; /* thread status */
69 /* Allocate descriptor */
70 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
76 //???? kludge to initialize some audio parameters - place this section somewhere
78 p_aout->dsp.i_format = AOUT_DEFAULT_FORMAT;
79 p_aout->dsp.psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
80 p_aout->dsp.b_stereo = main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
81 p_aout->dsp.l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
87 if ( aout_dspOpen( &p_aout->dsp ) )
92 if ( aout_dspReset( &p_aout->dsp ) )
94 aout_dspClose( &p_aout->dsp );
98 if ( aout_dspSetFormat( &p_aout->dsp ) )
100 aout_dspClose( &p_aout->dsp );
104 if ( aout_dspSetChannels( &p_aout->dsp ) )
106 aout_dspClose( &p_aout->dsp );
110 if ( aout_dspSetRate( &p_aout->dsp ) )
112 aout_dspClose( &p_aout->dsp );
116 intf_DbgMsg("aout debug: audio device (%s) opened (format=%i, stereo=%i, rate=%li)\n",
117 p_aout->dsp.psz_device,
118 p_aout->dsp.i_format,
119 p_aout->dsp.b_stereo, p_aout->dsp.l_rate);
121 //?? maybe it would be cleaner to change SpawnThread prototype
122 //?? see vout to handle status correctly - however, it is not critical since
123 //?? this thread is only called in main is all calls are blocking
124 if( aout_SpawnThread( p_aout ) )
126 aout_dspClose( &p_aout->dsp );
134 /******************************************************************************
136 ******************************************************************************/
137 static int aout_SpawnThread( aout_thread_t * p_aout )
141 s64 s64_numerator, s64_denominator;
142 void * aout_thread = NULL;
144 intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
146 /* We want the audio output thread to live */
149 /* Initialize the fifos lock */
150 vlc_mutex_init( &p_aout->fifos_lock );
151 /* Initialize audio fifos : set all fifos as empty and initialize locks */
152 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
154 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
155 vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
156 vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
159 /* Compute the size (in audio units) of the audio output buffer. Although
160 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
161 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
162 p_aout->l_units = (long)( ((s64)p_aout->dsp.l_rate * AOUT_BUFFER_DURATION) / 1000000 );
164 /* Make aout_thread point to the right thread function, and compute the
165 * byte size of the audio output buffer */
166 switch ( p_aout->dsp.b_stereo )
168 /* Audio output is mono */
170 switch ( p_aout->dsp.i_format )
173 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
174 aout_thread = (void *)aout_Thread_U8_Mono;
178 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
179 aout_thread = (void *)aout_Thread_S8_Mono;
184 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
185 aout_thread = (void *)aout_Thread_U16_Mono;
190 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
191 aout_thread = (void *)aout_Thread_S16_Mono;
195 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
196 p_aout->dsp.i_format);
201 /* Audio output is stereo */
203 switch ( p_aout->dsp.i_format )
206 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
207 aout_thread = (void *)aout_Thread_U8_Stereo;
211 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
212 aout_thread = (void *)aout_Thread_S8_Stereo;
217 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
218 aout_thread = (void *)aout_Thread_U16_Stereo;
223 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
224 aout_thread = (void *)aout_Thread_S16_Stereo;
228 intf_ErrMsg("aout error: unknown audio output format (%i)\n",
229 p_aout->dsp.i_format);
235 intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
236 p_aout->dsp.b_stereo + 1);
240 /* Allocate the memory needed by the audio output buffers, and set to zero
241 * the s32 buffer's memory */
242 if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
244 intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
247 if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << p_aout->dsp.b_stereo)) == NULL )
249 intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
250 free( p_aout->buffer );
254 /* Initialize the incremental structure that is used to work out the date
255 * of the first audio unit in the output buffer */
256 s64_numerator = (s64)p_aout->l_units * 1000000;
257 s64_denominator = (s64)p_aout->dsp.l_rate;
259 p_aout->date_increment.l_remainder = -(long)s64_denominator;
261 p_aout->date_increment.l_euclidean_integer = 0;
262 while ( s64_numerator >= s64_denominator )
264 p_aout->date_increment.l_euclidean_integer++;
265 s64_numerator -= s64_denominator;
268 p_aout->date_increment.l_euclidean_remainder = (long)s64_numerator;
270 p_aout->date_increment.l_euclidean_denominator = (long)s64_denominator;
272 /* Before launching the thread, we try to predict the date of the first
273 * audio unit in the first output buffer */
274 p_aout->date = mdate();
276 /* Launch the thread */
277 if ( vlc_thread_create( &p_aout->thread_id, "audio output", (vlc_thread_func_t)aout_thread, p_aout ) )
279 intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
280 free( p_aout->buffer );
281 free( p_aout->s32_buffer );
285 intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
289 /******************************************************************************
291 ******************************************************************************/
292 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
294 //???? pi_status is not handled correctly: check vout how to do!
296 intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
298 /* Ask thread to kill itself and wait until it's done */
300 vlc_thread_join( p_aout->thread_id ); // only if pi_status is NULL
302 /* Free the allocated memory */
303 free( p_aout->buffer );
304 free( p_aout->s32_buffer );
306 /* Free the structure */
307 aout_dspClose( &p_aout->dsp );
308 intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->dsp.psz_device);
312 /******************************************************************************
314 ******************************************************************************/
315 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
319 /* Take the fifos lock */
320 vlc_mutex_lock( &p_aout->fifos_lock );
322 /* Looking for a free fifo structure */
323 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
325 if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
330 if ( i_fifo == AOUT_MAX_FIFOS )
332 intf_ErrMsg("aout error: no empty fifo available\n");
333 vlc_mutex_unlock( &p_aout->fifos_lock );
337 /* Initialize the new fifo structure */
338 switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
340 case AOUT_INTF_MONO_FIFO:
341 case AOUT_INTF_STEREO_FIFO:
342 p_aout->fifo[i_fifo].b_die = 0;
344 p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
345 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
347 p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
349 p_aout->fifo[i_fifo].l_unit = 0;
350 InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->dsp.l_rate );
351 p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
354 case AOUT_ADEC_MONO_FIFO:
355 case AOUT_ADEC_STEREO_FIFO:
356 p_aout->fifo[i_fifo].b_die = 0;
358 p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
359 p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
361 p_aout->fifo[i_fifo].l_frame_size = p_fifo->l_frame_size;
362 /* Allocate the memory needed to store the audio frames. As the
363 * fifo is a rotative fifo, we must be able to find out whether the
364 * fifo is full or empty, that's why we must in fact allocate memory
365 * for (AOUT_FIFO_SIZE+1) audio frames. */
366 if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*p_fifo->l_frame_size )) == NULL )
368 intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
369 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
370 vlc_mutex_unlock( &p_aout->fifos_lock );
374 /* Allocate the memory needed to store the dates of the frames */
375 if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
377 intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
378 free( p_aout->fifo[i_fifo].buffer );
379 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
380 vlc_mutex_unlock( &p_aout->fifos_lock );
384 /* Set the fifo's buffer as empty (the first frame that is to be
385 * played is also the first frame that is not to be played) */
386 p_aout->fifo[i_fifo].l_start_frame = 0;
387 /* p_aout->fifo[i_fifo].l_next_frame = 0; */
388 p_aout->fifo[i_fifo].l_end_frame = 0;
390 /* Waiting for the audio decoder to compute enough frames to work
391 * out the fifo's current rate (as soon as the decoder has decoded
392 * enough frames, the members of the fifo structure that are not
393 * initialized now will be calculated) */
394 p_aout->fifo[i_fifo].l_unit = 0; /* !! */
395 p_aout->fifo[i_fifo].b_start_frame = 0;
396 p_aout->fifo[i_fifo].b_next_frame = 0;
400 intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
401 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
402 vlc_mutex_unlock( &p_aout->fifos_lock );
406 /* Release the fifos lock */
407 vlc_mutex_unlock( &p_aout->fifos_lock );
409 /* Return the pointer to the fifo structure */
410 intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
411 return( &p_aout->fifo[i_fifo] );
414 /******************************************************************************
416 ******************************************************************************/
417 void aout_DestroyFifo( aout_fifo_t * p_fifo )
419 intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
423 /* Here are the local macros */
425 #define UPDATE_INCREMENT( increment, integer ) \
426 if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 ) \
428 (integer) += (increment).l_euclidean_integer + 1; \
429 (increment).l_remainder -= (increment).l_euclidean_denominator; \
433 (integer) += (increment).l_euclidean_integer; \
436 /* Following functions are local */
438 /******************************************************************************
439 * InitializeIncrement
440 ******************************************************************************/
441 static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
443 p_increment->l_remainder = -l_denominator;
445 p_increment->l_euclidean_integer = 0;
446 while ( l_numerator >= l_denominator )
448 p_increment->l_euclidean_integer++;
449 l_numerator -= l_denominator;
452 p_increment->l_euclidean_remainder = l_numerator;
454 p_increment->l_euclidean_denominator = l_denominator;
457 /******************************************************************************
459 ******************************************************************************/
460 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo/*, mtime_t aout_date*/ )
462 long l_units, l_rate;
464 /* We take the lock */
465 vlc_mutex_lock( &p_fifo->data_lock );
467 /* Are we looking for a dated start frame ? */
468 if ( !p_fifo->b_start_frame )
470 while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
472 if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
474 p_fifo->b_start_frame = 1;
475 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
478 p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
480 if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
482 vlc_mutex_unlock( &p_fifo->data_lock );
487 if ( aout_date < p_fifo->date[p_fifo->l_start_frame] )
489 fprintf(stderr, "+");
490 vlc_mutex_unlock( &p_fifo->data_lock );
494 /* We are looking for the next dated frame */
495 while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
497 if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
500 if ( aout_date < p_fifo->date[p_fifo->l_next_frame] )
504 if ( p_fifo->date[p_fifo->l_start_frame] >= p_fifo->date[p_fifo->l_next_frame] )
506 fprintf( stderr, "aout debug: %lli >= %lli\n", p_fifo->date[p_fifo->l_start_frame], p_fifo->date[p_fifo->l_next_frame] );
507 p_fifo->date[p_fifo->l_start_frame] = p_fifo->date[p_fifo->l_next_frame] - ((1000000 * ((mtime_t)(p_fifo->l_frame_size * ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE))) >> p_fifo->b_stereo) / ((mtime_t)p_fifo->l_rate));
510 else if ( (p_fifo->date[p_fifo->l_next_frame] - p_fifo->date[p_fifo->l_start_frame]) > 1000000 )
512 fprintf( stderr, "aout debug: (%lli - %lli) > 1000000\n", p_fifo->date[p_fifo->l_next_frame], p_fifo->date[p_fifo->l_start_frame] );
513 p_fifo->date[p_fifo->l_start_frame] = p_fifo->date[p_fifo->l_next_frame] - ((1000000 * ((mtime_t)(p_fifo->l_frame_size * ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE))) >> p_fifo->b_stereo) / ((mtime_t)p_fifo->l_rate));
516 p_fifo->b_next_frame = 1;
523 fprintf(stderr, "-");
524 p_fifo->l_start_frame = p_fifo->l_next_frame;
528 p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
530 if ( p_fifo->l_next_frame == p_fifo->l_end_frame )
532 if ( (((p_fifo->l_end_frame + 1) - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) == 0 )
534 p_fifo->l_start_frame = 0;
535 p_fifo->b_start_frame = 0;
536 /* p_fifo->l_next_frame = 0; */
537 /* p_fifo->b_next_frame = 0; */
538 p_fifo->l_end_frame = 0;
540 vlc_mutex_unlock( &p_fifo->data_lock );
544 l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) * (p_fifo->l_frame_size >> p_fifo->b_stereo);
546 l_rate = (long)( ((mtime_t)l_units * 1000000)
547 / (p_fifo->date[p_fifo->l_next_frame] - p_fifo->date[p_fifo->l_start_frame]) );
548 // fprintf( stderr, "aout debug: l_rate == %li\n", l_rate );
550 InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->dsp.l_rate );
552 p_fifo->l_units = (((l_units - (p_fifo->l_unit -
553 (p_fifo->l_start_frame * (p_fifo->l_frame_size >> p_fifo->b_stereo))))
554 * p_aout->dsp.l_rate) / l_rate) + 1;
556 /* We release the lock before leaving */
557 vlc_mutex_unlock( &p_fifo->data_lock );
561 void aout_Thread_S8_Mono( aout_thread_t * p_aout )
565 void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
569 void aout_Thread_U8_Mono( aout_thread_t * p_aout )
573 void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
577 void aout_Thread_S16_Mono( aout_thread_t * p_aout )
581 void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
585 long l_buffer, l_buffer_limit;
587 intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
589 /* As the s32_buffer was created with calloc(), we don't have to set this
590 * memory to zero and we can immediately jump into the thread's loop */
591 while ( !p_aout->b_die )
593 vlc_mutex_lock( &p_aout->fifos_lock );
594 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
596 switch ( p_aout->fifo[i_fifo].i_type )
598 case AOUT_EMPTY_FIFO:
601 case AOUT_INTF_MONO_FIFO:
602 if ( p_aout->fifo[i_fifo].b_die )
604 free( p_aout->fifo[i_fifo].buffer ); /* !! */
605 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
606 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
610 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
613 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
615 p_aout->s32_buffer[l_buffer++] +=
616 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
617 p_aout->s32_buffer[l_buffer++] +=
618 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
619 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
621 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
626 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
628 p_aout->s32_buffer[l_buffer++] +=
629 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
630 p_aout->s32_buffer[l_buffer++] +=
631 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
632 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
634 free( p_aout->fifo[i_fifo].buffer ); /* !! */
635 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
636 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
640 case AOUT_INTF_STEREO_FIFO:
641 if ( p_aout->fifo[i_fifo].b_die )
643 free( p_aout->fifo[i_fifo].buffer ); /* !! */
644 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
645 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
649 if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
652 while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->dsp.b_stereo == 1 */
654 p_aout->s32_buffer[l_buffer++] +=
655 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
656 p_aout->s32_buffer[l_buffer++] +=
657 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
658 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
660 p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
665 while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->dsp.b_stereo */
667 p_aout->s32_buffer[l_buffer++] +=
668 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
669 p_aout->s32_buffer[l_buffer++] +=
670 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
671 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
673 free( p_aout->fifo[i_fifo].buffer ); /* !! */
674 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
675 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
679 case AOUT_ADEC_MONO_FIFO:
680 if ( p_aout->fifo[i_fifo].b_die )
682 free( p_aout->fifo[i_fifo].buffer );
683 free( p_aout->fifo[i_fifo].date );
684 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
685 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
689 l_units = p_aout->l_units;
691 while ( l_units > 0 )
693 if ( !p_aout->fifo[i_fifo].b_next_frame )
695 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo]/*, p_aout->date + (mtime_t)((l_buffer >> p_aout->dsp.b_stereo) / p_aout->dsp.l_rate)*/) )
701 if ( p_aout->fifo[i_fifo].l_units > l_units )
703 l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
704 while ( l_buffer < l_buffer_limit )
706 p_aout->s32_buffer[l_buffer++] +=
707 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
708 p_aout->s32_buffer[l_buffer++] +=
709 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
711 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
712 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
713 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
715 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
716 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
719 p_aout->fifo[i_fifo].l_units -= l_units;
724 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
725 /* p_aout->dsp.b_stereo == 1 */
726 while ( l_buffer < l_buffer_limit )
728 p_aout->s32_buffer[l_buffer++] +=
729 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
730 p_aout->s32_buffer[l_buffer++] +=
731 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
733 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
734 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
735 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
737 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
738 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
741 l_units -= p_aout->fifo[i_fifo].l_units;
743 vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
744 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
745 vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
746 vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
748 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
749 p_aout->fifo[i_fifo].l_next_frame += 1;
750 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
751 p_aout->fifo[i_fifo].b_next_frame = 0;
756 case AOUT_ADEC_STEREO_FIFO:
757 if ( p_aout->fifo[i_fifo].b_die )
759 free( p_aout->fifo[i_fifo].buffer );
760 free( p_aout->fifo[i_fifo].date );
761 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
762 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
766 l_units = p_aout->l_units;
768 while ( l_units > 0 )
770 if ( !p_aout->fifo[i_fifo].b_next_frame )
772 if ( NextFrame(p_aout, &p_aout->fifo[i_fifo]/*, p_aout->date + (mtime_t)((l_buffer >> p_aout->dsp.b_stereo) / p_aout->dsp.l_rate)*/) )
778 if ( p_aout->fifo[i_fifo].l_units > l_units )
780 l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
781 while ( l_buffer < l_buffer_limit )
783 p_aout->s32_buffer[l_buffer++] +=
784 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
785 p_aout->s32_buffer[l_buffer++] +=
786 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
788 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
789 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
790 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
792 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
793 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
796 p_aout->fifo[i_fifo].l_units -= l_units;
801 l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
802 /* p_aout->dsp.b_stereo == 1 */
803 while ( l_buffer < l_buffer_limit )
805 p_aout->s32_buffer[l_buffer++] +=
806 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
807 p_aout->s32_buffer[l_buffer++] +=
808 (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
810 UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
811 if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
812 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
814 p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
815 ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
818 l_units -= p_aout->fifo[i_fifo].l_units;
820 vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
821 p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
822 vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
823 vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
825 /* p_aout->fifo[i_fifo].b_start_frame = 1; */
826 p_aout->fifo[i_fifo].l_next_frame += 1;
827 p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
828 p_aout->fifo[i_fifo].b_next_frame = 0;
834 intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
838 vlc_mutex_unlock( &p_aout->fifos_lock );
840 l_buffer_limit = p_aout->l_units << 1; /* p_aout->dsp.b_stereo == 1 */
842 for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
844 ((s16 *)p_aout->buffer)[l_buffer] = (s16)( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
845 p_aout->s32_buffer[l_buffer] = 0;
848 aout_dspGetBufInfo( &p_aout->dsp );
849 if ( p_aout->dsp.buf_info.fragments == p_aout->dsp.buf_info.fragstotal ) /* ?? */
851 aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
852 p_aout->date = mdate();
854 else if ( p_aout->dsp.buf_info.bytes >=
855 ((p_aout->dsp.buf_info.fragsize * p_aout->dsp.buf_info.fragstotal) -
856 (sizeof(s16) * l_buffer_limit)) )
858 aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
862 aout_dspPlaySamples( &p_aout->dsp, (byte_t *)p_aout->buffer, sizeof(s16) * l_buffer_limit );
863 msleep( p_aout->date_increment.l_euclidean_integer );
865 UPDATE_INCREMENT( p_aout->date_increment, p_aout->date )
868 vlc_mutex_lock( &p_aout->fifos_lock );
869 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
871 switch ( p_aout->fifo[i_fifo].i_type )
873 case AOUT_EMPTY_FIFO:
876 case AOUT_INTF_MONO_FIFO:
877 case AOUT_INTF_STEREO_FIFO:
878 free( p_aout->fifo[i_fifo].buffer ); /* !! */
879 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
880 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
883 case AOUT_ADEC_MONO_FIFO:
884 case AOUT_ADEC_STEREO_FIFO:
885 free( p_aout->fifo[i_fifo].buffer );
886 free( p_aout->fifo[i_fifo].date );
887 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
888 intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
895 vlc_mutex_unlock( &p_aout->fifos_lock );
898 void aout_Thread_U16_Mono( aout_thread_t * p_aout )
902 void aout_Thread_U16_Stereo( aout_thread_t * p_aout )