]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
mixer: cleanup packetization
[vlc] / src / audio_output / mixer.c
1 /*****************************************************************************
2  * mixer.c : audio output mixing operations
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
31
32 #include <stddef.h>
33 #include <vlc_common.h>
34 #include <libvlc.h>
35 #include <vlc_modules.h>
36
37 #include <vlc_aout.h>
38 #include "aout_internal.h"
39 /*****************************************************************************
40  * aout_MixerNew: prepare a mixer plug-in
41  *****************************************************************************
42  * Please note that you must hold the mixer lock.
43  *****************************************************************************/
44 int aout_MixerNew( aout_instance_t * p_aout )
45 {
46     assert( !p_aout->p_mixer );
47     vlc_assert_locked( &p_aout->input_fifos_lock );
48
49     aout_mixer_t *p_mixer = vlc_object_create( p_aout, sizeof(*p_mixer) );
50     if( !p_mixer )
51         return VLC_EGENERIC;
52
53     p_mixer->fmt = p_aout->mixer_format;
54     p_mixer->input = &p_aout->p_input->mixer;
55     p_mixer->mix = NULL;
56     p_mixer->sys = NULL;
57
58     p_mixer->module = module_need( p_mixer, "audio mixer", NULL, false );
59     if( !p_mixer->module )
60     {
61         msg_Err( p_mixer, "no suitable audio mixer" );
62         vlc_object_release( p_mixer );
63         return VLC_EGENERIC;
64     }
65
66     /* */
67     p_aout->p_mixer = p_mixer;
68     return VLC_SUCCESS;
69 }
70
71 /*****************************************************************************
72  * aout_MixerDelete: delete the mixer
73  *****************************************************************************
74  * Please note that you must hold the mixer lock.
75  *****************************************************************************/
76 void aout_MixerDelete( aout_instance_t * p_aout )
77 {
78     if( !p_aout->p_mixer )
79         return;
80
81     module_unneed( p_aout->p_mixer, p_aout->p_mixer->module );
82
83     vlc_object_release( p_aout->p_mixer );
84
85     /* */
86     p_aout->p_mixer = NULL;
87 }
88
89 /*****************************************************************************
90  * MixBuffer: try to prepare one output buffer
91  *****************************************************************************
92  * Please note that you must hold the mixer lock.
93  *****************************************************************************/
94 static int MixBuffer( aout_instance_t * p_aout, float volume )
95 {
96     aout_mixer_t *p_mixer = p_aout->p_mixer;
97     aout_mixer_input_t *p_input = p_mixer->input;
98     aout_fifo_t *p_fifo = &p_input->fifo;
99     mtime_t now = mdate();
100
101     aout_lock_input_fifos( p_aout );
102     aout_lock_output_fifo( p_aout );
103
104     /* Retrieve the date of the next buffer. */
105     date_t exact_start_date = p_aout->output.fifo.end_date;
106     mtime_t start_date = date_Get( &exact_start_date );
107
108     if( start_date != 0 && start_date < now )
109     {
110         /* The output is _very_ late. This can only happen if the user
111          * pauses the stream (or if the decoder is buggy, which cannot
112          * happen :). */
113         msg_Warn( p_mixer, "output PTS is out of range (%"PRId64"), clearing out",
114                   mdate() - start_date );
115         aout_FifoSet( &p_aout->output.fifo, 0 );
116         date_Set( &exact_start_date, 0 );
117         start_date = 0;
118     }
119
120     aout_unlock_output_fifo( p_aout );
121
122     /* See if we have enough data to prepare a new buffer for the audio
123      * output. First : start date. */
124     if ( !start_date )
125     {
126         /* Find the latest start date available. */
127         aout_buffer_t *p_buffer;
128         for( ;; )
129         {
130             p_buffer = p_fifo->p_first;
131             if( p_buffer == NULL )
132                 goto giveup;
133             if( p_buffer->i_pts >= now )
134                 break;
135
136             msg_Warn( p_mixer, "input PTS is out of range (%"PRId64"), "
137                       "trashing", now - p_buffer->i_pts );
138             aout_BufferFree( aout_FifoPop( p_fifo ) );
139         }
140
141         date_Set( &exact_start_date, p_buffer->i_pts );
142         start_date = p_buffer->i_pts;
143     }
144
145     date_Increment( &exact_start_date, p_aout->output.i_nb_samples );
146     mtime_t end_date = date_Get( &exact_start_date );
147
148     /* Check that start_date is available. */
149     aout_buffer_t *p_buffer = p_fifo->p_first;
150     mtime_t prev_date;
151
152     for( ;; )
153     {
154         if( p_buffer == NULL )
155             goto giveup;
156
157         /* Check for the continuity of start_date */
158         prev_date = p_buffer->i_pts + p_buffer->i_length;
159         if( prev_date >= start_date - 1 )
160             break;
161         /* We authorize a +-1 because rounding errors get compensated
162          * regularly. */
163         msg_Warn( p_mixer, "the mixer got a packet in the past (%"PRId64")",
164                   start_date - prev_date );
165         aout_BufferFree( aout_FifoPop( p_fifo ) );
166         p_buffer = p_fifo->p_first;
167     }
168
169     /* Check that we have enough samples. */
170     while( prev_date < end_date )
171     {
172         p_buffer = p_buffer->p_next;
173         if( p_buffer == NULL )
174             goto giveup;
175
176         /* Check that all buffers are contiguous. */
177         if( prev_date != p_buffer->i_pts )
178         {
179             msg_Warn( p_mixer,
180                       "buffer hole, dropping packets (%"PRId64")",
181                       p_buffer->i_pts - prev_date );
182
183             aout_buffer_t *p_deleted;
184             while( (p_deleted = p_fifo->p_first) != p_buffer )
185                 aout_BufferFree( aout_FifoPop( p_fifo ) );
186         }
187
188         prev_date = p_buffer->i_pts + p_buffer->i_length;
189     }
190
191     if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
192     {
193         p_buffer = p_fifo->p_first;
194
195         /* Additionally check that p_first_byte_to_mix is well located. */
196         const unsigned framesize = p_mixer->fmt.i_bytes_per_frame;
197         ssize_t delta = (start_date - p_buffer->i_pts)
198                       * p_mixer->fmt.i_rate / CLOCK_FREQ;
199         if( delta != 0 )
200             msg_Warn( p_mixer, "mixer start is not output end (%zd)", delta );
201         if( delta < 0 )
202         {
203             /* Is it really the best way to do it ? */
204             aout_lock_output_fifo( p_aout );
205             aout_FifoSet( &p_aout->output.fifo, 0 );
206             date_Set( &exact_start_date, 0 );
207             aout_unlock_output_fifo( p_aout );
208             goto giveup;
209         }
210         if( delta > 0 )
211         {
212             p_buffer->i_nb_samples -= delta;
213             p_buffer->i_pts += delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
214             p_buffer->i_length -= delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
215             delta *= framesize;
216             p_buffer->p_buffer += delta;
217             p_buffer->i_buffer -= delta;
218         }
219
220         /* Build packet with adequate number of samples */
221         const unsigned samples = p_aout->output.i_nb_samples;
222         unsigned needed = samples * framesize;
223         p_buffer = block_Alloc( needed );
224         if( unlikely(p_buffer == NULL) )
225             /* XXX: should free input buffers */
226             goto giveup;
227         p_buffer->i_nb_samples = samples;
228
229         for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
230         {
231             aout_buffer_t *p_inbuf = p_fifo->p_first;
232             if( unlikely(p_inbuf == NULL) )
233             {
234                 msg_Err( p_mixer, "internal amix error" );
235                 vlc_memset( p_out, 0, needed );
236                 break;
237             }
238
239             const uint8_t *p_in = p_inbuf->p_buffer;
240             size_t avail = p_inbuf->i_nb_samples * framesize;
241             if( avail > needed )
242             {
243                 vlc_memcpy( p_out, p_in, needed );
244                 p_fifo->p_first->p_buffer += needed;
245                 p_fifo->p_first->i_buffer -= needed;
246                 needed /= framesize;
247                 p_fifo->p_first->i_nb_samples -= needed;
248                 p_fifo->p_first->i_pts += needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
249                 p_fifo->p_first->i_length -= needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
250                 break;
251             }
252
253             vlc_memcpy( p_out, p_in, avail );
254             needed -= avail;
255             p_out += avail;
256             /* Next buffer */
257             aout_BufferFree( aout_FifoPop( p_fifo ) );
258         }
259     }
260     else
261         p_buffer = aout_FifoPop( p_fifo );
262
263     p_buffer->i_pts = start_date;
264     p_buffer->i_length = end_date - start_date;
265
266     /* Run the mixer. */
267     p_mixer->mix( p_mixer, p_buffer, volume );
268     aout_unlock_input_fifos( p_aout );
269     aout_OutputPlay( p_aout, p_buffer );
270     return 0;
271
272 giveup:
273     /* Interrupted before the end... We can't run. */
274     aout_unlock_input_fifos( p_aout );
275     return -1;
276 }
277
278 /*****************************************************************************
279  * aout_MixerRun: entry point for the mixer & post-filters processing
280  *****************************************************************************
281  * Please note that you must hold the mixer lock.
282  *****************************************************************************/
283 void aout_MixerRun( aout_instance_t * p_aout, float volume )
284 {
285     while( MixBuffer( p_aout, volume ) != -1 );
286 }