clock.c 12.2 KB
Newer Older
1
/*****************************************************************************
2
 * input_clock.c: Clock/System date convertions, stream management
3
 *****************************************************************************
zorglub's avatar
zorglub committed
4
 * Copyright (C) 1999-2004 VideoLAN
5
 * $Id$
6
7
8
9
10
11
12
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
13
 *
14
15
16
17
18
19
20
21
22
23
24
25
26
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
27
#include <stdlib.h>
28
#include <vlc/vlc.h>
29

Laurent Aimar's avatar
Laurent Aimar committed
30
31
#include <vlc/input.h>
#include "input_internal.h"
32

33
/*
34
 * DISCUSSION : SYNCHRONIZATION METHOD
35
 *
36
37
38
39
40
41
42
 * In some cases we can impose the pace of reading (when reading from a
 * file or a pipe), and for the synchronization we simply sleep() until
 * it is time to deliver the packet to the decoders. When reading from
 * the network, we must be read at the same pace as the server writes,
 * otherwise the kernel's buffer will trash packets. The risk is now to
 * overflow the input buffers in case the server goes too fast, that is
 * why we do these calculations :
43
 *
44
45
46
47
 * We compute a mean for the pcr because we want to eliminate the
 * network jitter and keep the low frequency variations. The mean is
 * in fact a low pass filter and the jitter is a high frequency signal
 * that is why it is eliminated by the filter/average.
48
 *
49
50
51
52
53
54
55
 * The low frequency variations enable us to synchronize the client clock
 * with the server clock because they represent the time variation between
 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
 * the presentation dates for the audio and video frames. With those dates
 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
 * as it is sent by the server and so we keep the synchronization between
 * the server and the client.
56
 *
57
58
 * It is a very important matter if you want to avoid underflow or overflow
 * in all the FIFOs, but it may be not enough.
59
60
 */

61
62
63
64
65
66
/* p_input->i_cr_average : Maximum number of samples used to compute the
 * dynamic average value.
 * We use the following formula :
 * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
 */

Laurent Aimar's avatar
Laurent Aimar committed
67
68

static void ClockNewRef( input_clock_t * p_pgrm,
69
70
                         mtime_t i_clock, mtime_t i_sysdate );

71
/*****************************************************************************
72
 * Constants
73
 *****************************************************************************/
74
75

/* Maximum gap allowed between two CRs. */
gbazin's avatar
   
gbazin committed
76
#define CR_MAX_GAP 2000000
77

78
79
80
81
/* Latency introduced on DVDs with CR == 0 on chapter change - this is from
 * my dice --Meuuh */
#define CR_MEAN_PTS_GAP 300000

82
83
84
/*****************************************************************************
 * ClockToSysdate: converts a movie clock to system date
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
85
86
static mtime_t ClockToSysdate( input_thread_t *p_input,
                               input_clock_t *cl, mtime_t i_clock )
87
88
89
{
    mtime_t     i_sysdate = 0;

Laurent Aimar's avatar
Laurent Aimar committed
90
    if( cl->i_synchro_state == SYNCHRO_OK )
91
    {
Laurent Aimar's avatar
Laurent Aimar committed
92
93
        i_sysdate = (mtime_t)(i_clock - cl->cr_ref)
                        * (mtime_t)p_input->i_rate
94
95
96
                        * (mtime_t)300;
        i_sysdate /= 27;
        i_sysdate /= 1000;
Laurent Aimar's avatar
Laurent Aimar committed
97
        i_sysdate += (mtime_t)cl->sysdate_ref;
98
99
100
101
102
    }

    return( i_sysdate );
}

103
104
105
106
107
/*****************************************************************************
 * ClockCurrent: converts current system date to clock units
 *****************************************************************************
 * Caution : the synchro state must be SYNCHRO_OK for this to operate.
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
108
109
static mtime_t ClockCurrent( input_thread_t *p_input,
                             input_clock_t *cl )
110
{
Laurent Aimar's avatar
Laurent Aimar committed
111
112
113
    return( (mdate() - cl->sysdate_ref) * 27 * INPUT_RATE_DEFAULT
             / p_input->i_rate / 300
             + cl->cr_ref );
114
115
}

116
/*****************************************************************************
117
 * ClockNewRef: writes a new clock reference
118
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
119
static void ClockNewRef( input_clock_t *cl,
120
                         mtime_t i_clock, mtime_t i_sysdate )
121
{
Laurent Aimar's avatar
Laurent Aimar committed
122
123
    cl->cr_ref = i_clock;
    cl->sysdate_ref = i_sysdate ;
124
125
}

126
127
128
129
/*****************************************************************************
 * input_ClockInit: reinitializes the clock reference after a stream
 *                  discontinuity
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
130
void input_ClockInit( input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
131
{
Laurent Aimar's avatar
Laurent Aimar committed
132
133
134
135
136
137
138
139
140
141
142
143
    cl->i_synchro_state = SYNCHRO_START;

    cl->last_cr = 0;
    cl->last_pts = 0;
    cl->cr_ref = 0;
    cl->sysdate_ref = 0;
    cl->delta_cr = 0;
    cl->c_average_count = 0;

    cl->i_cr_average = i_cr_average;

    cl->b_master = b_master;
144
145
}

Laurent Aimar's avatar
Laurent Aimar committed
146
#if 0
147
148
149
150
151
152
/*****************************************************************************
 * input_ClockManageControl: handles the messages from the interface
 *****************************************************************************
 * Returns UNDEF_S if nothing happened, PAUSE_S if the stream was paused
 *****************************************************************************/
int input_ClockManageControl( input_thread_t * p_input,
Laurent Aimar's avatar
Laurent Aimar committed
153
                               input_clock_t *cl, mtime_t i_clock )
154
{
Laurent Aimar's avatar
Laurent Aimar committed
155
#if 0
156
    vlc_value_t val;
157
158
159
160
161
162
163
    int i_return_value = UNDEF_S;

    vlc_mutex_lock( &p_input->stream.stream_lock );

    if( p_input->stream.i_new_status == PAUSE_S )
    {
        int i_old_status;
164

165
166
167
        vlc_mutex_lock( &p_input->stream.control.control_lock );
        i_old_status = p_input->stream.control.i_status;
        p_input->stream.control.i_status = PAUSE_S;
168
        vlc_mutex_unlock( &p_input->stream.control.control_lock );
169

170
171
        vlc_cond_wait( &p_input->stream.stream_wait,
                       &p_input->stream.stream_lock );
172
173
        ClockNewRef( p_pgrm, i_clock, p_pgrm->last_pts > mdate() ?
                                      p_pgrm->last_pts : mdate() );
174
175

        if( p_input->stream.i_new_status == PAUSE_S )
176
        {
177
            /* PAUSE_S undoes the pause state: Return to old state. */
178
            vlc_mutex_lock( &p_input->stream.control.control_lock );
179
            p_input->stream.control.i_status = i_old_status;
180
            vlc_mutex_unlock( &p_input->stream.control.control_lock );
181

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
            p_input->stream.i_new_status = UNDEF_S;
            p_input->stream.i_new_rate = UNDEF_S;
        }

        /* We handle i_new_status != PAUSE_S below... */

        i_return_value = PAUSE_S;
    }

    if( p_input->stream.i_new_status != UNDEF_S )
    {
        vlc_mutex_lock( &p_input->stream.control.control_lock );

        p_input->stream.control.i_status = p_input->stream.i_new_status;

197
        ClockNewRef( p_pgrm, i_clock,
198
199
200
201
202
203
204
205
206
207
208
209
210
211
                     ClockToSysdate( p_input, p_pgrm, i_clock ) );

        if( p_input->stream.control.i_status == PLAYING_S )
        {
            p_input->stream.control.i_rate = DEFAULT_RATE;
            p_input->stream.control.b_mute = 0;
        }
        else
        {
            p_input->stream.control.i_rate = p_input->stream.i_new_rate;
            p_input->stream.control.b_mute = 1;

            /* Feed the audio decoders with a NULL packet to avoid
             * discontinuities. */
212
            input_EscapeAudioDiscontinuity( p_input );
213
214
        }

215
216
217
218
219
220
        val.i_int = p_input->stream.control.i_rate;
        var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );

        val.i_int = p_input->stream.control.i_status;
        var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );

221
222
223
224
225
226
227
228
229
        p_input->stream.i_new_status = UNDEF_S;
        p_input->stream.i_new_rate = UNDEF_S;

        vlc_mutex_unlock( &p_input->stream.control.control_lock );
    }

    vlc_mutex_unlock( &p_input->stream.stream_lock );

    return( i_return_value );
Laurent Aimar's avatar
Laurent Aimar committed
230
231
#endif
    return UNDEF_S;
232
}
Laurent Aimar's avatar
Laurent Aimar committed
233
#endif
234

235
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
236
 * input_ClockSetPCR: manages a clock reference
237
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
238
239
void input_ClockSetPCR( input_thread_t *p_input,
                        input_clock_t *cl, mtime_t i_clock )
240
{
Laurent Aimar's avatar
Laurent Aimar committed
241
242
    if( ( cl->i_synchro_state != SYNCHRO_OK ) ||
        ( i_clock == 0 && cl->last_cr != 0 ) )
243
244
    {
        /* Feed synchro with a new reference point. */
Laurent Aimar's avatar
Laurent Aimar committed
245
246
247
248
        ClockNewRef( cl, i_clock,
                     cl->last_pts + CR_MEAN_PTS_GAP > mdate() ?
                     cl->last_pts + CR_MEAN_PTS_GAP : mdate() );
        cl->i_synchro_state = SYNCHRO_OK;
249

Laurent Aimar's avatar
Laurent Aimar committed
250
        if( p_input->b_can_pace_control && cl->b_master )
251
        {
Laurent Aimar's avatar
Laurent Aimar committed
252
            cl->last_cr = i_clock;
253
254
            if( !p_input->b_out_pace_control )
            {
Laurent Aimar's avatar
Laurent Aimar committed
255
                mtime_t i_wakeup = ClockToSysdate( p_input, cl, i_clock );
256
257
258
259
260
261
                while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
                {
                    msleep( CLOCK_FREQ );
                    if( p_input->b_die ) i_wakeup = mdate();
                }
                mwait( i_wakeup );
262
            }
263
        }
264
265
        else
        {
Laurent Aimar's avatar
Laurent Aimar committed
266
267
268
            cl->last_cr = 0;
            cl->delta_cr = 0;
            cl->c_average_count = 0;
269
        }
270
271
272
    }
    else
    {
Laurent Aimar's avatar
Laurent Aimar committed
273
274
275
        if ( cl->last_cr != 0 &&
               (    (cl->last_cr - i_clock) > CR_MAX_GAP
                 || (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
276
277
278
279
        {
            /* Stream discontinuity, for which we haven't received a
             * warning from the stream control facilities (dd-edited
             * stream ?). */
280
            msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
Laurent Aimar's avatar
Laurent Aimar committed
281
282
283
            input_ClockInit( cl, cl->b_master, cl->i_cr_average );
            /* FIXME needed ? */
#if 0
284
            input_EscapeDiscontinuity( p_input );
Laurent Aimar's avatar
Laurent Aimar committed
285
#endif
286
287
        }

Laurent Aimar's avatar
Laurent Aimar committed
288
        cl->last_cr = i_clock;
289

Laurent Aimar's avatar
Laurent Aimar committed
290
        if( p_input->b_can_pace_control && cl->b_master )
291
292
293
        {
            /* Wait a while before delivering the packets to the decoder.
             * In case of multiple programs, we arbitrarily follow the
Christophe Massiot's avatar
Christophe Massiot committed
294
             * clock of the selected program. */
295
296
            if( !p_input->b_out_pace_control )
            {
Laurent Aimar's avatar
Laurent Aimar committed
297
                mtime_t i_wakeup = ClockToSysdate( p_input, cl, i_clock );
298
299
300
301
302
303
                while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
                {
                    msleep( CLOCK_FREQ );
                    if( p_input->b_die ) i_wakeup = mdate();
                }
                mwait( i_wakeup );
304
            }
Laurent Aimar's avatar
Laurent Aimar committed
305
306
            /* FIXME Not needed anymore ? */
#if 0
307
            /* Now take into account interface changes. */
Laurent Aimar's avatar
Laurent Aimar committed
308
309
            input_ClockManageControl( p_input, cl, i_clock );
#endif
310
311
312
313
        }
        else
        {
            /* Smooth clock reference variations. */
314
            mtime_t i_extrapoled_clock = ClockCurrent( p_input, cl );
315
316

            /* Bresenham algorithm to smooth variations. */
317
318
319
            cl->delta_cr = ( cl->delta_cr * (cl->i_cr_average - 1)
                               + ( i_extrapoled_clock - i_clock ) )
                           / cl->i_cr_average;
320
321
322
323
324
325
326
327
        }
    }
}

/*****************************************************************************
 * input_ClockGetTS: manages a PTS or DTS
 *****************************************************************************/
mtime_t input_ClockGetTS( input_thread_t * p_input,
Laurent Aimar's avatar
Laurent Aimar committed
328
                          input_clock_t *cl, mtime_t i_ts )
329
{
Laurent Aimar's avatar
Laurent Aimar committed
330
    if( cl->i_synchro_state != SYNCHRO_OK )
331
        return 0;
Laurent Aimar's avatar
Laurent Aimar committed
332
333
334

    cl->last_pts = ClockToSysdate( p_input, cl, i_ts + cl->delta_cr );
    return cl->last_pts + p_input->i_pts_delay;
335
336
}