]> git.sesse.net Git - vlc/commitdiff
clock: clean up/simplify + remove 1/90000 reference.
authorLaurent Aimar <fenrir@videolan.org>
Thu, 7 Jun 2007 23:05:16 +0000 (23:05 +0000)
committerLaurent Aimar <fenrir@videolan.org>
Thu, 7 Jun 2007 23:05:16 +0000 (23:05 +0000)
all: improve input rate (no more out of order/dropped pictures on
rate change).

src/input/clock.c
src/input/es_out.c
src/input/input.c
src/input/input_internal.h

index b9a9e844f92dd01d41fb1c9a04796b0ec8032241..9043328827d261b0ffc5b1c69951bf8fe13108a0 100644 (file)
@@ -72,7 +72,7 @@ static void ClockNewRef( input_clock_t * p_pgrm,
  *****************************************************************************/
 
 /* Maximum gap allowed between two CRs. */
-#define CR_MAX_GAP 2000000
+#define CR_MAX_GAP (I64C(2000000)*100/9)
 
 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
  * my dice --Meuuh */
@@ -81,22 +81,13 @@ static void ClockNewRef( input_clock_t * p_pgrm,
 /*****************************************************************************
  * ClockToSysdate: converts a movie clock to system date
  *****************************************************************************/
-static mtime_t ClockToSysdate( input_thread_t *p_input,
-                               input_clock_t *cl, mtime_t i_clock )
+static mtime_t ClockToSysdate( input_clock_t *cl, mtime_t i_clock )
 {
-    mtime_t     i_sysdate = 0;
-
-    if( cl->i_synchro_state == SYNCHRO_OK )
-    {
-        i_sysdate = (mtime_t)(i_clock - cl->cr_ref)
-                        * (mtime_t)p_input->p->i_rate
-                        * (mtime_t)300;
-        i_sysdate /= 27;
-        i_sysdate /= INPUT_RATE_DEFAULT;
-        i_sysdate += (mtime_t)cl->sysdate_ref;
-    }
+    if( cl->i_synchro_state != SYNCHRO_OK )
+        return 0;
 
-    return( i_sysdate );
+    return (i_clock - cl->cr_ref) * cl->i_rate / INPUT_RATE_DEFAULT +
+           cl->sysdate_ref;
 }
 
 /*****************************************************************************
@@ -104,12 +95,10 @@ static mtime_t ClockToSysdate( input_thread_t *p_input,
  *****************************************************************************
  * Caution : the synchro state must be SYNCHRO_OK for this to operate.
  *****************************************************************************/
-static mtime_t ClockCurrent( input_thread_t *p_input,
-                             input_clock_t *cl )
+static mtime_t ClockCurrent( input_clock_t *cl )
 {
-    return( (mdate() - cl->sysdate_ref) * 27 * INPUT_RATE_DEFAULT
-             / p_input->p->i_rate / 300
-             + cl->cr_ref );
+    return (mdate() - cl->sysdate_ref) * INPUT_RATE_DEFAULT / cl->i_rate +
+           cl->cr_ref;
 }
 
 /*****************************************************************************
@@ -126,7 +115,8 @@ static void ClockNewRef( input_clock_t *cl,
  * input_ClockInit: reinitializes the clock reference after a stream
  *                  discontinuity
  *****************************************************************************/
-void input_ClockInit( input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
+void input_ClockInit( input_thread_t *p_input,
+                      input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
 {
     cl->i_synchro_state = SYNCHRO_START;
 
@@ -137,6 +127,7 @@ void input_ClockInit( input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
     cl->sysdate_ref = 0;
     cl->delta_cr = 0;
     cl->i_delta_cr_residue = 0;
+    cl->i_rate = p_input->p->i_rate;
 
     cl->i_cr_average = i_cr_average;
 
@@ -149,86 +140,69 @@ void input_ClockInit( input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
 void input_ClockSetPCR( input_thread_t *p_input,
                         input_clock_t *cl, mtime_t i_clock )
 {
+    const vlc_bool_t b_synchronize = p_input->b_can_pace_control && cl->b_master;
+    const mtime_t i_mdate = mdate();
+
     if( ( cl->i_synchro_state != SYNCHRO_OK ) ||
         ( i_clock == 0 && cl->last_cr != 0 ) )
     {
         /* Feed synchro with a new reference point. */
         ClockNewRef( cl, i_clock,
-                     cl->last_pts + CR_MEAN_PTS_GAP > mdate() ?
-                     cl->last_pts + CR_MEAN_PTS_GAP : mdate() );
+                         __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_mdate ) );
         cl->i_synchro_state = SYNCHRO_OK;
 
-        if( p_input->b_can_pace_control && cl->b_master )
-        {
-            cl->last_cr = i_clock;
-            if( !p_input->p->b_out_pace_control )
-            {
-                mtime_t i_wakeup = ClockToSysdate( p_input, cl, i_clock );
-                while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
-                {
-                    msleep( CLOCK_FREQ );
-                    if( p_input->b_die ) i_wakeup = mdate();
-                }
-                mwait( i_wakeup );
-            }
-        }
-        else
+        if( !b_synchronize )
         {
             cl->last_cr = 0;
-            cl->last_sysdate = 0;
             cl->delta_cr = 0;
             cl->i_delta_cr_residue = 0;
+            cl->last_sysdate = 0;
+            cl->last_update = 0;
         }
     }
-    else
+    else if ( cl->last_cr != 0 &&
+              ( (cl->last_cr - i_clock) > CR_MAX_GAP ||
+                (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
     {
-        if ( cl->last_cr != 0 &&
-               (    (cl->last_cr - i_clock) > CR_MAX_GAP
-                 || (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
-        {
-            /* Stream discontinuity, for which we haven't received a
-             * warning from the stream control facilities (dd-edited
-             * stream ?). */
-            msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
-            input_ClockInit( cl, cl->b_master, cl->i_cr_average );
-        }
+        /* Stream discontinuity, for which we haven't received a
+         * warning from the stream control facilities (dd-edited
+         * stream ?). */
+        msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
+        input_ClockInit( p_input, cl, cl->b_master, cl->i_cr_average );
+    }
 
-        cl->last_cr = i_clock;
+    cl->last_cr = i_clock;
+    cl->last_sysdate = i_mdate;
 
-        if( p_input->b_can_pace_control && cl->b_master )
+    if( b_synchronize )
+    {
+        /* Wait a while before delivering the packets to the decoder.
+         * In case of multiple programs, we arbitrarily follow the
+         * clock of the selected program. */
+        if( !p_input->p->b_out_pace_control )
         {
-            /* Wait a while before delivering the packets to the decoder.
-             * In case of multiple programs, we arbitrarily follow the
-             * clock of the selected program. */
-            if( !p_input->p->b_out_pace_control )
+            mtime_t i_wakeup = ClockToSysdate( cl, i_clock );
+            while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
             {
-                mtime_t i_wakeup = ClockToSysdate( p_input, cl, i_clock );
-                while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
-                {
-                    msleep( CLOCK_FREQ );
-                    if( p_input->b_die ) i_wakeup = mdate();
-                }
-                mwait( i_wakeup );
+                msleep( CLOCK_FREQ );
+                if( p_input->b_die ) i_wakeup = mdate();
             }
+            mwait( i_wakeup );
         }
-        else if ( mdate() - cl->last_sysdate > 200000 )
-        {
-            /* Smooth clock reference variations. */
-            mtime_t i_extrapoled_clock = ClockCurrent( p_input, cl );
-            mtime_t delta_cr;
-
-            /* Bresenham algorithm to smooth variations. */
-            delta_cr = ( cl->delta_cr * (cl->i_cr_average - 1)
-                               + ( i_extrapoled_clock - i_clock )
-                               + cl->i_delta_cr_residue )
-                           / cl->i_cr_average;
-            cl->i_delta_cr_residue = ( cl->delta_cr * (cl->i_cr_average - 1)
-                                       + ( i_extrapoled_clock - i_clock )
-                                       + cl->i_delta_cr_residue )
-                                    % cl->i_cr_average;
-            cl->delta_cr = delta_cr;
-            cl->last_sysdate = mdate();
-        }
+    }
+    else if ( i_mdate - cl->last_update > 200000 )
+    {
+        /* Smooth clock reference variations. */
+        const mtime_t i_extrapoled_clock = ClockCurrent( cl );
+        /* Bresenham algorithm to smooth variations. */
+        const mtime_t i_tmp = cl->delta_cr * (cl->i_cr_average - 1) +
+                              ( i_extrapoled_clock - i_clock ) * 1  +
+                              cl->i_delta_cr_residue;
+
+        cl->i_delta_cr_residue = i_tmp % cl->i_cr_average;
+        cl->delta_cr           = i_tmp / cl->i_cr_average;
+
+        cl->last_update = i_mdate;
     }
 }
 
@@ -241,7 +215,21 @@ mtime_t input_ClockGetTS( input_thread_t * p_input,
     if( cl->i_synchro_state != SYNCHRO_OK )
         return 0;
 
-    cl->last_pts = ClockToSysdate( p_input, cl, i_ts + cl->delta_cr );
+    cl->last_pts = ClockToSysdate( cl, i_ts + cl->delta_cr );
     return cl->last_pts + p_input->i_pts_delay;
 }
 
+/*****************************************************************************
+ * input_ClockSetRate:
+ *****************************************************************************/
+void input_ClockSetRate( input_thread_t *p_input, input_clock_t *cl )
+{
+    if( cl->i_synchro_state == SYNCHRO_OK )
+    {
+        /* Move the reference point */
+        cl->cr_ref = cl->last_cr;
+        cl->sysdate_ref = cl->last_sysdate;
+    }
+    cl->i_rate = p_input->p->i_rate;
+}
+
index 847eb954030fd48805454ce0fb19aff518de77d3..6637221e8403e9014477e937f4ff3340619746a4 100644 (file)
@@ -314,6 +314,14 @@ void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_flush, vlc_bool_t b_a
             input_DecoderDiscontinuity( es->p_dec, b_flush );
     }
 }
+void input_EsOutSetRate( es_out_t *out )
+{
+    es_out_sys_t      *p_sys = out->p_sys;
+    int i;
+
+    for( i = 0; i < p_sys->i_pgrm; i++ )
+        input_ClockSetRate( p_sys->p_input, &p_sys->pgrm[i]->clock );
+}
 
 void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
 {
@@ -507,7 +515,7 @@ static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
     p_pgrm->psz_now_playing = NULL;
     p_pgrm->psz_publisher = NULL;
     p_pgrm->p_epg = NULL;
-    input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
+    input_ClockInit( p_input, &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
 
     /* Append it */
     TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );
@@ -1216,8 +1224,7 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
     else if( p_block->i_dts > 0 )
     {
         p_block->i_dts =
-            input_ClockGetTS( p_input, &p_pgrm->clock,
-                              ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
+            input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_dts ) + i_delay;
     }
     if( p_block->i_pts > 0 && (p_block->i_flags&BLOCK_FLAG_PREROLL) )
     {
@@ -1226,8 +1233,7 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
     else if( p_block->i_pts > 0 )
     {
         p_block->i_pts =
-            input_ClockGetTS( p_input, &p_pgrm->clock,
-                              ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
+            input_ClockGetTS( p_input, &p_pgrm->clock, p_block->i_pts ) + i_delay;
     }
     if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
     {
@@ -1506,8 +1512,7 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
             i_pcr = (int64_t)va_arg( args, int64_t );
             /* search program */
             /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
-            input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
-                               (i_pcr + 11 ) * 9 / 100);
+            input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, i_pcr );
             return VLC_SUCCESS;
         }
 
@@ -1525,8 +1530,7 @@ static int EsOutControl( es_out_t *out, int i_query, va_list args )
                 int64_t i_ts = (int64_t)va_arg( args, int64_t );
                 int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
                 *pi_ts = input_ClockGetTS( p_sys->p_input,
-                                           &p_sys->p_pgrm->clock,
-                                           ( i_ts + 11 ) * 9 / 100 );
+                                           &p_sys->p_pgrm->clock, i_ts );
                 return VLC_SUCCESS;
             }
             return VLC_EGENERIC;
index b046ecfd21c5bad73caf1497fd4d1db164075e91..56953010e217e431ffc3ac17b2f09cf814ffb3bf 100644 (file)
@@ -1672,12 +1672,11 @@ static vlc_bool_t Control( input_thread_t *p_input, int i_type,
 
                 /* We will not send audio data if new rate != default */
                 if( i_rate != INPUT_RATE_DEFAULT && p_input->p->i_rate == INPUT_RATE_DEFAULT )
-                    input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_TRUE );
+                    input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE, VLC_TRUE );
 
                 p_input->p->i_rate  = i_rate;
 
-                /* Reset clock */
-                es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
+                input_EsOutSetRate( p_input->p->p_es_out );
 
                 b_force_update = VLC_TRUE;
             }
index d84ffce325e4fc22398af33840d8f2330a05a8aa..065ef088407c81ac6722b1a1cbc7532f253a743c 100644 (file)
@@ -263,6 +263,7 @@ void       input_EsOutDelete( es_out_t * );
 es_out_id_t *input_EsOutGetFromID( es_out_t *, int i_id );
 void       input_EsOutDiscontinuity( es_out_t *, vlc_bool_t b_flush, vlc_bool_t b_audio );
 void       input_EsOutSetDelay( es_out_t *, int i_cat, int64_t );
+void       input_EsOutSetRate( es_out_t * );
 vlc_bool_t input_EsOutDecodersEmpty( es_out_t * );
 
 /* clock.c */
@@ -282,18 +283,22 @@ typedef struct
     mtime_t                 last_cr; /* reference to detect unexpected stream
                                       * discontinuities                      */
     mtime_t                 last_pts;
+    mtime_t                 last_update;
     int                     i_synchro_state;
 
     vlc_bool_t              b_master;
 
+    int                     i_rate;
+
     /* Config */
     int                     i_cr_average;
     int                     i_delta_cr_residue;
 } input_clock_t;
 
-void input_ClockInit( input_clock_t *, vlc_bool_t b_master, int i_cr_average );
+void    input_ClockInit( input_thread_t *, input_clock_t *, vlc_bool_t b_master, int i_cr_average );
 void    input_ClockSetPCR( input_thread_t *, input_clock_t *, mtime_t );
 mtime_t input_ClockGetTS( input_thread_t *, input_clock_t *, mtime_t );
+void    input_ClockSetRate( input_thread_t *, input_clock_t *cl );
 
 /* Subtitles */
 char **subtitles_Detect( input_thread_t *, char* path, const char *fname );