]> git.sesse.net Git - vlc/blob - src/audio_output/aout_pcm.c
432890a27b1d0598a0f2ce5904d2c34404b2fc00
[vlc] / src / audio_output / aout_pcm.c
1 /*****************************************************************************
2  * aout_pcm.c: PCM audio output functions
3  *****************************************************************************
4  * Copyright (C) 1999-2002 VideoLAN
5  * $Id: aout_pcm.c,v 1.6 2002/05/18 17:47:47 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Cyril Deguet <asmax@via.ecp.fr>
9  *
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.
14  * 
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.
19  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>                                           /* "intf_msg.h" */
29 #include <stdlib.h>                            /* calloc(), malloc(), free() */
30 #include <string.h>
31
32 #include <videolan/vlc.h>
33
34 #include "audio_output.h"
35 #include "aout_pcm.h"
36
37 /* Biggest difference allowed between scheduled playing date and actual date 
38    (in microseconds) */
39 #define MAX_DELTA 10000
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static void FillBuffer( aout_thread_t * p_aout, aout_fifo_t * p_fifo );
45 static int  NextFrame ( aout_thread_t * p_aout, aout_fifo_t * p_fifo,
46                         mtime_t aout_date );
47
48  /*****************************************************************************
49  * Functions
50  *****************************************************************************/
51 void aout_PCMThread( aout_thread_t * p_aout )
52 {
53     int i_fifo;
54     int i_buffer, i_buffer_limit, i_units = 0;
55
56 #if defined(WIN32)
57     if( !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) )
58         intf_WarnMsg( 2, "aout warning: couldn't change priority of"
59                       "aout_PCMThread()" );
60 #endif
61
62     /* As the s32_buffer was created with calloc(), we don't have to set this
63      * memory to zero and we can immediately jump into the thread's loop */
64     while ( ! p_aout->b_die )
65     {
66         vlc_mutex_lock( &p_aout->fifos_lock );
67         for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
68         {
69             if( p_aout->fifo[i_fifo].b_die )
70             {
71                 aout_FreeFifo( &p_aout->fifo[i_fifo] );
72             }
73             else
74             {
75                 FillBuffer( p_aout, &p_aout->fifo[i_fifo] );
76             }
77         }
78         vlc_mutex_unlock( &p_aout->fifos_lock );
79
80         i_buffer_limit = p_aout->i_units * p_aout->i_channels;
81
82         switch ( p_aout->i_format )
83         {
84         case AOUT_FMT_U8:
85             for ( i_buffer = 0; i_buffer < i_buffer_limit; i_buffer++ )
86             {
87                 ((u8*)p_aout->buffer)[i_buffer] = (u8)(
88                   (p_aout->s32_buffer[i_buffer] / AOUT_MAX_FIFOS / 256 + 128)
89                      * p_aout->i_volume / 256);
90                  p_aout->s32_buffer[i_buffer] = 0;
91             }
92             break;
93
94         case AOUT_FMT_S8:
95             for ( i_buffer = 0; i_buffer < i_buffer_limit; i_buffer++ )
96             {
97                 ((s8*)p_aout->buffer)[i_buffer] = (s8)(
98                   p_aout->s32_buffer[i_buffer] / AOUT_MAX_FIFOS / 256
99                      * p_aout->i_volume / 256);
100                  p_aout->s32_buffer[i_buffer] = 0;
101             }
102             break;
103
104         case AOUT_FMT_U16_LE:
105         case AOUT_FMT_U16_BE:
106             for ( i_buffer = 0; i_buffer < i_buffer_limit; i_buffer++ )
107             {
108                 ((u16*)p_aout->buffer)[i_buffer] = (u16)(
109                   (p_aout->s32_buffer[i_buffer] / AOUT_MAX_FIFOS + 128)
110                      * p_aout->i_volume / 256);
111                  p_aout->s32_buffer[i_buffer] = 0;
112             }
113             break;
114
115         case AOUT_FMT_S16_LE:
116         case AOUT_FMT_S16_BE:
117             for ( i_buffer = 0; i_buffer < i_buffer_limit; i_buffer++ )
118             {
119                 ((s16*)p_aout->buffer)[i_buffer] = (s16)(
120                   p_aout->s32_buffer[i_buffer] / AOUT_MAX_FIFOS
121                      * p_aout->i_volume / 256);
122                  p_aout->s32_buffer[i_buffer] = 0;
123             }
124             break;
125         }
126
127         switch ( p_aout->i_format )
128         {
129         case AOUT_FMT_U8:
130         case AOUT_FMT_S8:
131             i_units = p_aout->pf_getbufinfo( p_aout, i_buffer_limit );
132
133             p_aout->date = mdate() + ((((mtime_t)((i_units + 4 *
134                 p_aout->i_latency) / p_aout->i_channels)) * 1000000) /
135                 ((mtime_t)p_aout->i_rate)) + p_main->i_desync;
136
137             p_aout->pf_play( p_aout, (byte_t *)p_aout->buffer,
138                              i_buffer_limit );
139             break;
140
141         case AOUT_FMT_U16_LE:
142         case AOUT_FMT_U16_BE:
143         case AOUT_FMT_S16_LE:
144         case AOUT_FMT_S16_BE:
145             i_units = p_aout->pf_getbufinfo( p_aout, i_buffer_limit * 2 ) / 2;
146
147             p_aout->date = mdate() + ((((mtime_t)((i_units + 4 *
148                 p_aout->i_latency) / (2 * p_aout->i_channels))) * 1000000) /
149                 ((mtime_t)p_aout->i_rate)) + p_main->i_desync;
150
151             p_aout->pf_play( p_aout, (byte_t *)p_aout->buffer,
152                              i_buffer_limit * 2 );
153             break;
154         }
155
156         /* Sleep until there is only AOUT_BUFFER_DURATION/2 worth of audio
157          * left to play in the aout plugin, then we can start refill the
158          * plugin's buffer */
159         if( i_units > (i_buffer_limit/2) )
160             msleep( (i_units - i_buffer_limit/2) * AOUT_BUFFER_DURATION
161                     / i_buffer_limit );
162
163     }
164
165     vlc_mutex_lock( &p_aout->fifos_lock );
166     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
167     {
168         aout_FreeFifo( &p_aout->fifo[i_fifo] );
169     }
170     vlc_mutex_unlock( &p_aout->fifos_lock );
171 }
172
173 /* Following functions are local */
174
175 /*****************************************************************************
176  * InitializeIncrement: change i_x/i_y to i_a+i_b/i_c
177  *****************************************************************************/
178 static inline void InitializeIncrement( aout_increment_t * p_inc,
179                                         int i_x, int i_y )
180 {
181     p_inc->i_r = -i_y;
182     p_inc->i_a = 0;
183
184     while ( i_x >= i_y )
185     {
186         p_inc->i_a++;
187         i_x -= i_y;
188     }
189
190     p_inc->i_b = i_x;
191     p_inc->i_c = i_y;
192 }
193
194 /*****************************************************************************
195  * UpdateIncrement
196  *****************************************************************************/
197 static inline void UpdateIncrement( aout_increment_t * p_inc, int * pi_integer )
198 {
199     if( (p_inc->i_r += p_inc->i_b) >= 0 )
200     {
201         *pi_integer += p_inc->i_a + 1;
202         p_inc->i_r -= p_inc->i_c;
203     }
204     else
205     {
206         *pi_integer += p_inc->i_a;
207     }
208 }
209
210 /*****************************************************************************
211  * FillBuffer: Read data from decoder fifo, and put it in S32_buffer
212  *****************************************************************************/
213 static void FillBuffer( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
214 {
215     int i_buffer = 0;
216     int i_buffer_limit, i_units;
217
218     switch ( p_fifo->i_format )
219     {
220     case AOUT_FIFO_PCM:
221
222         i_units = p_aout->i_units;
223
224         /* While there are empty frames in the buffer, fill them */
225         while ( i_units > 0 )
226         {
227             /* If there is no next frame, wait for one */
228             if( !p_fifo->b_next_frame )
229             {
230                 if( NextFrame(p_aout, p_fifo, p_aout->date + 
231                         ((((mtime_t)(i_buffer >> 1)) * 1000000) / 
232                         ((mtime_t)p_aout->i_rate))) )
233                 {
234                     break;
235                 }
236             }
237
238             i_buffer_limit = p_aout->i_units * p_aout->i_channels;
239
240             while ( i_buffer < i_buffer_limit )
241             {
242                 /* FIXME: make this more generic */
243                 if( p_aout->i_channels == 2 )
244                 {
245                     p_aout->s32_buffer[i_buffer++] +=
246                         (s32)( ((s16 *)p_fifo->buffer)[2*p_fifo->i_unit] );
247                     p_aout->s32_buffer[i_buffer++] +=
248                         (s32)( ((s16 *)p_fifo->buffer)[2*p_fifo->i_unit+1] );
249                 }
250                 else if( p_aout->i_channels == 1 )
251                 {
252                     p_aout->s32_buffer[i_buffer++] +=
253                         (s32)( ((s16 *)p_fifo->buffer)[p_fifo->i_unit] );
254                 }
255                 else
256                 {
257                     intf_ErrMsg( "aout error: unsupported number of channels" );
258                 }
259
260                 UpdateIncrement(&p_fifo->unit_increment, &p_fifo->i_unit);
261
262                 if( p_fifo->i_unit >= p_fifo->i_unit_limit )
263                 {
264                     p_fifo->i_unit -= p_fifo->i_unit_limit;
265                 }
266             }
267  
268             if( p_fifo->i_units > i_units )
269             {
270                 p_fifo->i_units -= i_units;
271                 break;
272             }
273             else
274             {
275                 i_units -= p_fifo->i_units;
276
277                 vlc_mutex_lock( &p_fifo->data_lock );
278                 p_fifo->i_start_frame = p_fifo->i_next_frame;
279              /* p_fifo->b_start_frame = 1; */
280                 p_fifo->i_next_frame = AOUT_FIFO_INC( p_fifo->i_next_frame );
281                 p_fifo->b_next_frame = 0;
282                 vlc_cond_signal( &p_fifo->data_wait );
283                 vlc_mutex_unlock( &p_fifo->data_lock );
284
285             }
286         }
287         break;
288
289     default:
290         break;
291     }
292 }
293
294 static int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo,
295                       mtime_t aout_date )
296 {
297     int i_units, i_units_dist, i_rate;
298     u64 i_delta;    
299
300     /* We take the lock */
301     vlc_mutex_lock( &p_fifo->data_lock );
302     if ( p_fifo->b_die )
303     {
304         vlc_mutex_unlock( &p_fifo->data_lock );
305         return( -1 );
306     }
307
308     /* Are we looking for a dated start frame ? */
309     if ( !p_fifo->b_start_frame )
310     {
311         while ( p_fifo->i_start_frame != p_fifo->i_end_frame )
312         {
313             if ( p_fifo->date[p_fifo->i_start_frame] != LAST_MDATE )
314             {
315                 p_fifo->b_start_frame = 1;
316                 p_fifo->i_next_frame = AOUT_FIFO_INC( p_fifo->i_start_frame );
317                 p_fifo->i_unit = p_fifo->i_start_frame * 
318                         (p_fifo->i_frame_size / p_fifo->i_channels);
319                 break;
320             }
321             p_fifo->i_start_frame = AOUT_FIFO_INC( p_fifo->i_start_frame );
322         }
323
324         if ( p_fifo->i_start_frame == p_fifo->i_end_frame )
325         {
326             vlc_mutex_unlock( &p_fifo->data_lock );
327             return( -1 );
328         }
329     }
330
331     /* We are looking for the next dated frame */
332     /* FIXME : is the output fifo full ?? -- pretty unlikely given its size */
333     while ( !p_fifo->b_next_frame )
334     {
335         while ( p_fifo->i_next_frame != p_fifo->i_end_frame )
336         {
337             if ( p_fifo->date[p_fifo->i_next_frame] != LAST_MDATE )
338             {
339                 p_fifo->b_next_frame = 1;
340                 break;
341             }
342             p_fifo->i_next_frame = AOUT_FIFO_INC( p_fifo->i_next_frame );
343         }
344
345         while ( p_fifo->i_next_frame == p_fifo->i_end_frame )
346         {
347             vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
348             if ( p_fifo->b_die )
349             {
350                 vlc_mutex_unlock( &p_fifo->data_lock );
351                 return( -1 );
352             }
353         }
354     }
355
356     i_units = ((p_fifo->i_next_frame - p_fifo->i_start_frame) & AOUT_FIFO_SIZE)
357                * (p_fifo->i_frame_size / p_fifo->i_channels);
358
359     i_delta = aout_date - p_fifo->date[p_fifo->i_start_frame];
360
361     /* Resample if delta is too long */
362     if( abs(i_delta) > MAX_DELTA )
363     {
364         i_rate = p_fifo->i_rate + (i_delta / 256);
365     }
366     else
367     {
368         i_rate = p_fifo->i_rate;
369     }
370
371     InitializeIncrement( &p_fifo->unit_increment, i_rate, p_aout->i_rate );
372
373     /* Calculate how many units we're going to write */
374     i_units_dist = p_fifo->i_unit - p_fifo->i_start_frame
375                                 * (p_fifo->i_frame_size / p_fifo->i_channels);
376
377     /* Manage the fifo wrapping */
378     if( i_units_dist > p_fifo->i_unit_limit / 2 )
379     {
380         i_units -= (i_units_dist - p_fifo->i_unit_limit);
381     }
382     else if( i_units_dist < - p_fifo->i_unit_limit / 2 )
383     {
384         i_units -= (i_units_dist + p_fifo->i_unit_limit);
385     }
386     else
387     {
388         i_units -= i_units_dist;
389     }
390
391     p_fifo->i_units = 1 + ( i_units * p_aout->i_rate / i_rate );
392
393     /* We release the lock before leaving */
394     vlc_mutex_unlock( &p_fifo->data_lock );
395     return( 0 );
396 }
397