]> git.sesse.net Git - vlc/blob - src/audio_output/aout_common.h
f1e537ab2cb91a4cef620a45e1685bf6459b6a1c
[vlc] / src / audio_output / aout_common.h
1 /*****************************************************************************
2  * aout_common.h: audio output inner functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: aout_common.h,v 1.3 2001/04/29 02:48:51 stef Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /* Creating as many aout_Thread functions as configurations was one solution,
25  * examining the different cases in the Thread loop of an unique function was
26  * another. I chose the first solution. */
27 void aout_U8MonoThread        ( aout_thread_t * p_aout );
28 void aout_U8StereoThread      ( aout_thread_t * p_aout );
29 void aout_S8MonoThread        ( aout_thread_t * p_aout );
30 void aout_S8StereoThread      ( aout_thread_t * p_aout );
31 void aout_U16MonoThread       ( aout_thread_t * p_aout );
32 void aout_U16StereoThread     ( aout_thread_t * p_aout );
33 void aout_S16MonoThread       ( aout_thread_t * p_aout );
34 void aout_S16StereoThread     ( aout_thread_t * p_aout );
35 void aout_SpdifThread         ( aout_thread_t * p_aout );
36
37 #define UPDATE_INCREMENT( increment, integer ) \
38     if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 )\
39     { \
40         (integer) += (increment).l_euclidean_integer + 1; \
41         (increment).l_remainder -= (increment).l_euclidean_denominator; \
42     } \
43     else \
44     { \
45         (integer) += (increment).l_euclidean_integer; \
46     }
47
48 #define FIFO p_aout->fifo[i_fifo]
49
50 /*****************************************************************************
51  * InitializeIncrement
52  *****************************************************************************/
53 static __inline__ void InitializeIncrement( aout_increment_t * p_increment,
54                                             long l_numerator,
55                                             long l_denominator )
56 {
57     p_increment->l_remainder = -l_denominator;
58
59     p_increment->l_euclidean_integer = 0;
60     while ( l_numerator >= l_denominator )
61     {
62         p_increment->l_euclidean_integer++;
63         l_numerator -= l_denominator;
64     }
65
66     p_increment->l_euclidean_remainder = l_numerator;
67
68     p_increment->l_euclidean_denominator = l_denominator;
69 }
70
71 /*****************************************************************************
72  * NextFrame
73  *****************************************************************************/
74 static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo,
75                                  mtime_t aout_date )
76 {
77     long l_units, l_rate;
78
79     /* We take the lock */
80     vlc_mutex_lock( &p_fifo->data_lock );
81
82     /* Are we looking for a dated start frame ? */
83     if ( !p_fifo->b_start_frame )
84     {
85         while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
86         {
87             if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
88             {
89                 p_fifo->b_start_frame = 1;
90                 p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
91                 p_fifo->l_unit = p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->b_stereo));
92                 break;
93             }
94             p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
95         }
96
97         if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
98         {
99             vlc_mutex_unlock( &p_fifo->data_lock );
100             return( -1 );
101         }
102     }
103
104     /* We are looking for the next dated frame */
105     /* FIXME : is the output fifo full ?? */
106     while ( !p_fifo->b_next_frame )
107     {
108         while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
109         {
110             if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
111             {
112                 p_fifo->b_next_frame = 1;
113                 break;
114             }
115             p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
116         }
117
118         while ( p_fifo->l_next_frame == p_fifo->l_end_frame )
119         {
120             vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
121             if ( p_fifo->b_die )
122             {
123                 vlc_mutex_unlock( &p_fifo->data_lock );
124                 return( -1 );
125             }
126         }
127     }
128
129     l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) * (p_fifo->l_frame_size >> (p_fifo->b_stereo));
130
131     l_rate = p_fifo->l_rate + ((aout_date - p_fifo->date[p_fifo->l_start_frame]) / 256);
132     intf_DbgMsg( "aout debug: %lli (%li);", aout_date - p_fifo->date[p_fifo->l_start_frame], l_rate );
133
134     InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->l_rate );
135
136     p_fifo->l_units = (((l_units - (p_fifo->l_unit -
137         (p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->b_stereo)))))
138         * p_aout->l_rate) / l_rate) + 1;
139
140     /* We release the lock before leaving */
141     vlc_mutex_unlock( &p_fifo->data_lock );
142     return( 0 );
143 }
144