clock.c 11.7 KB
Newer Older
1
/*****************************************************************************
2
 * input_clock.c: Clock/System date convertions, stream management
3
 *****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
4
 * Copyright (C) 1999-2008 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
Laurent Aimar's avatar
Laurent Aimar committed
8
 *          Laurent Aimar < fenrir _AT_ videolan _DOT_ org >
9 10 11 12 13
 *
 * 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.
14
 *
15 16 17 18 19 20 21
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33 34
#include <vlc_input.h>
#include "input_clock.h"
35

36
/*
37
 * DISCUSSION : SYNCHRONIZATION METHOD
38
 *
39 40 41 42 43 44 45
 * 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 :
46
 *
47 48 49 50
 * 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.
51
 *
52 53 54 55 56 57 58
 * 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.
59
 *
60 61
 * It is a very important matter if you want to avoid underflow or overflow
 * in all the FIFOs, but it may be not enough.
62 63
 */

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

70
/*****************************************************************************
71
 * Constants
72
 *****************************************************************************/
73 74

/* Maximum gap allowed between two CRs. */
75
#define CR_MAX_GAP (INT64_C(2000000)*100/9)
76

77 78
/* Latency introduced on DVDs with CR == 0 on chapter change - this is from
 * my dice --Meuuh */
Laurent Aimar's avatar
Laurent Aimar committed
79
#define CR_MEAN_PTS_GAP (300000)
80

81 82 83
/*****************************************************************************
 * Structures
 *****************************************************************************/
84 85 86 87 88 89 90 91 92 93 94 95

/**
 * This structure holds long term average
 */
typedef struct
{
    mtime_t i_value;
    int     i_residue;

    int     i_count;
    int     i_divider;
} average_t;
96 97
static void    AvgInit( average_t *, int i_divider );
static void    AvgClean( average_t * );
98

99 100 101
static void    AvgReset( average_t * );
static void    AvgUpdate( average_t *, mtime_t i_value );
static mtime_t AvgGet( average_t * );
102

Laurent Aimar's avatar
Laurent Aimar committed
103 104 105 106 107 108 109 110 111 112 113 114 115
/* */
typedef struct
{
    mtime_t i_stream;
    mtime_t i_system;
} clock_point_t;

static inline clock_point_t clock_point_Create( mtime_t i_stream, mtime_t i_system )
{
    clock_point_t p = { .i_stream = i_stream, .i_system = i_system };
    return p;
}

116
/* */
117 118
struct input_clock_t
{
Laurent Aimar's avatar
Laurent Aimar committed
119
    /* Reference point */
Laurent Aimar's avatar
Laurent Aimar committed
120 121
    bool          b_has_reference;
    clock_point_t ref;
Laurent Aimar's avatar
Laurent Aimar committed
122 123 124

    /* Last point
     * It is used to detect unexpected stream discontinuities */
Laurent Aimar's avatar
Laurent Aimar committed
125
    clock_point_t last;
126

127
    /* Maximal timestamp returned by input_clock_GetTS (in system unit) */
128
    mtime_t i_ts_max;
129

Laurent Aimar's avatar
Laurent Aimar committed
130
    /* Clock drift */
131 132
    mtime_t i_next_drift_update;
    average_t drift;
133

Laurent Aimar's avatar
Laurent Aimar committed
134 135 136
    /* Current modifiers */
    bool    b_master;
    int     i_rate;
137 138
};

Laurent Aimar's avatar
Laurent Aimar committed
139
static mtime_t ClockStreamToSystem( input_clock_t *, mtime_t i_stream );
Laurent Aimar's avatar
Laurent Aimar committed
140
static mtime_t ClockSystemToStream( input_clock_t *, mtime_t i_system );
141

142
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
143
 * input_clock_New: create a new clock
144
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
145
input_clock_t *input_clock_New( bool b_master, int i_cr_average, int i_rate )
146
{
147 148 149 150
    input_clock_t *cl = malloc( sizeof(*cl) );
    if( !cl )
        return NULL;

151
    cl->b_has_reference = false;
Laurent Aimar's avatar
Laurent Aimar committed
152
    cl->ref = clock_point_Create( 0, 0 );
153

Laurent Aimar's avatar
Laurent Aimar committed
154
    cl->last = clock_point_Create( 0, 0 );
155 156

    cl->i_ts_max = 0;
157

158
    cl->i_next_drift_update = 0;
159
    AvgInit( &cl->drift, i_cr_average );
160 161

    cl->b_master = b_master;
Laurent Aimar's avatar
Laurent Aimar committed
162 163
    cl->i_rate = i_rate;

164 165 166 167
    return cl;
}

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
168
 * input_clock_Delete: destroy a new clock
169
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
170
void input_clock_Delete( input_clock_t *cl )
171
{
172
    AvgClean( &cl->drift );
173
    free( cl );
174 175 176
}

/*****************************************************************************
177
 * input_clock_Update: manages a clock reference
178 179 180
 *
 *  i_ck_stream: date in stream clock
 *  i_ck_system: date in system clock
181
 *****************************************************************************/
182
void input_clock_Update( input_clock_t *cl,
183
                         vlc_object_t *p_log, bool b_can_pace_control,
Laurent Aimar's avatar
Laurent Aimar committed
184
                         mtime_t i_ck_stream, mtime_t i_ck_system )
185
{
186
    const bool b_synchronize = b_can_pace_control && cl->b_master;
187
    bool b_reset_reference = false;
188

189
    if( ( !cl->b_has_reference ) ||
Laurent Aimar's avatar
Laurent Aimar committed
190
        ( i_ck_stream == 0 && cl->last.i_stream != 0 ) )
191
    {
192 193
        /* */
        b_reset_reference= true;
194
    }
Laurent Aimar's avatar
Laurent Aimar committed
195 196 197
    else if( cl->last.i_stream != 0 &&
             ( (cl->last.i_stream - i_ck_stream) > CR_MAX_GAP ||
               (cl->last.i_stream - i_ck_stream) < -CR_MAX_GAP ) )
198
    {
199 200 201
        /* Stream discontinuity, for which we haven't received a
         * warning from the stream control facilities (dd-edited
         * stream ?). */
202
        msg_Warn( p_log, "clock gap, unexpected stream discontinuity" );
203
        cl->i_ts_max = 0;
204 205

        /* */
206
        msg_Warn( p_log, "feeding synchro with a new reference point trying to recover from clock gap" );
207 208 209 210
        b_reset_reference= true;
    }
    if( b_reset_reference )
    {
211
        cl->i_next_drift_update = 0;
212
        AvgReset( &cl->drift );
213 214

        /* Feed synchro with a new reference point. */
Laurent Aimar's avatar
Laurent Aimar committed
215 216 217
        cl->b_has_reference = true;
        cl->ref = clock_point_Create( i_ck_stream,
                                      __MAX( cl->i_ts_max + CR_MEAN_PTS_GAP, i_ck_system ) );
218
    }
219

220
    if( !b_synchronize && cl->i_next_drift_update < i_ck_system )
221
    {
222
        const mtime_t i_converted = ClockSystemToStream( cl, i_ck_system );
223

224
        AvgUpdate( &cl->drift, i_converted - i_ck_stream );
225

226
        cl->i_next_drift_update = i_ck_system + CLOCK_FREQ/5; /* FIXME why that */
227
    }
Laurent Aimar's avatar
Laurent Aimar committed
228
    cl->last = clock_point_Create( i_ck_stream, i_ck_system );
229 230
}

Laurent Aimar's avatar
Laurent Aimar committed
231
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
232
 * input_clock_Reset:
Laurent Aimar's avatar
Laurent Aimar committed
233
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
234
void input_clock_Reset( input_clock_t *cl )
Laurent Aimar's avatar
Laurent Aimar committed
235
{
236
    cl->b_has_reference = false;
Laurent Aimar's avatar
Laurent Aimar committed
237
    cl->ref = clock_point_Create( 0, 0 );
238
    cl->i_ts_max = 0;
Laurent Aimar's avatar
Laurent Aimar committed
239 240
}

241
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
242
 * input_clock_GetTS: manages a PTS or DTS
243
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
244
mtime_t input_clock_GetTS( input_clock_t *cl,
Laurent Aimar's avatar
Laurent Aimar committed
245
                           mtime_t i_pts_delay, mtime_t i_ts )
246
{
247 248
    mtime_t i_converted_ts;

249
    if( !cl->b_has_reference )
250
        return 0;
251

252
    /* */
253
    i_converted_ts = ClockStreamToSystem( cl, i_ts + AvgGet( &cl->drift ) );
254 255 256 257
    if( i_converted_ts > cl->i_ts_max )
        cl->i_ts_max = i_converted_ts;

    return i_converted_ts + i_pts_delay;
258 259
}

260
/*****************************************************************************
261
 * input_clock_ChangeRate:
262
 *****************************************************************************/
263
void input_clock_ChangeRate( input_clock_t *cl, int i_rate )
264
{
265
    /* Move the reference point */
266
    if( cl->b_has_reference )
Laurent Aimar's avatar
Laurent Aimar committed
267
        cl->ref = cl->last;
268

269
    cl->i_rate = i_rate;
270 271
}

272
/*****************************************************************************
273
 * input_clock_ChangeMaster:
274
 *****************************************************************************/
275
void input_clock_ChangeMaster( input_clock_t *cl, bool b_master )
276 277 278 279
{
    cl->b_master = b_master;
}

280
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
281
 * input_clock_GetWakeup
282
 *****************************************************************************/
283
mtime_t input_clock_GetWakeup( input_clock_t *cl )
284 285
{
    /* Not synchronized, we cannot wait */
286
    if( !cl->b_has_reference )
287 288
        return 0;

289 290
    /* We must not wait if we are not the master clock */
    if( !cl->b_master  )
291 292 293
        return 0;

    /* */
Laurent Aimar's avatar
Laurent Aimar committed
294
    return ClockStreamToSystem( cl, cl->last.i_stream );
295 296
}

Laurent Aimar's avatar
Laurent Aimar committed
297 298 299
/*****************************************************************************
 * ClockStreamToSystem: converts a movie clock to system date
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
300
static mtime_t ClockStreamToSystem( input_clock_t *cl, mtime_t i_stream )
Laurent Aimar's avatar
Laurent Aimar committed
301 302 303 304
{
    if( !cl->b_has_reference )
        return 0;

Laurent Aimar's avatar
Laurent Aimar committed
305
    return ( i_stream - cl->ref.i_stream ) * cl->i_rate / INPUT_RATE_DEFAULT +
Laurent Aimar's avatar
Laurent Aimar committed
306 307 308 309 310 311 312 313 314 315 316 317
           cl->ref.i_system;
}

/*****************************************************************************
 * ClockSystemToStream: converts a system date to movie clock
 *****************************************************************************
 * Caution : a valid reference point is needed for this to operate.
 *****************************************************************************/
static mtime_t ClockSystemToStream( input_clock_t *cl, mtime_t i_system )
{
    assert( cl->b_has_reference );
    return ( i_system - cl->ref.i_system ) * INPUT_RATE_DEFAULT / cl->i_rate +
Laurent Aimar's avatar
Laurent Aimar committed
318
            cl->ref.i_stream;
Laurent Aimar's avatar
Laurent Aimar committed
319 320
}

321 322 323
/*****************************************************************************
 * Long term average helpers
 *****************************************************************************/
324
static void AvgInit( average_t *p_avg, int i_divider )
325 326
{
    p_avg->i_divider = i_divider;
327
    AvgReset( p_avg );
328
}
329
static void AvgClean( average_t *p_avg )
330 331 332
{
    VLC_UNUSED(p_avg);
}
333
static void AvgReset( average_t *p_avg )
334 335 336 337 338
{
    p_avg->i_value = 0;
    p_avg->i_residue = 0;
    p_avg->i_count = 0;
}
339
static void AvgUpdate( average_t *p_avg, mtime_t i_value )
340 341 342 343 344 345 346 347 348 349 350
{
    const int i_f0 = __MIN( p_avg->i_divider - 1, p_avg->i_count );
    const int i_f1 = p_avg->i_divider - i_f0;

    const mtime_t i_tmp = i_f0 * p_avg->i_value + i_f1 * i_value + p_avg->i_residue;

    p_avg->i_value   = i_tmp / p_avg->i_divider;
    p_avg->i_residue = i_tmp % p_avg->i_divider;

    p_avg->i_count++;
}
351
static mtime_t AvgGet( average_t *p_avg )
352 353 354
{
    return p_avg->i_value;
}
Laurent Aimar's avatar
Laurent Aimar committed
355