]> git.sesse.net Git - vlc/blobdiff - src/input/input_clock.c
* input/input_dec.c: we automaticaly switch to minimize thread mode
[vlc] / src / input / input_clock.c
index 3479311c4d7827f0ae16b0a0bbeef1a2028ea816..4c249ddafdeff864b303c479d7f98be69a59b3b1 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * input_clock.c: Clock/System date convertions, stream management
  *****************************************************************************
- * Copyright (C) 1999-2001 VideoLAN
- * $Id: input_clock.c,v 1.30 2002/05/14 21:23:44 massiot Exp $
+ * Copyright (C) 1999-2004 VideoLAN
+ * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
@@ -10,7 +10,7 @@
  * 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.
- * 
+ *
  * 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
@@ -25,9 +25,8 @@
  * Preamble
  *****************************************************************************/
 #include <string.h>                                    /* memcpy(), memset() */
-#include <sys/types.h>                                              /* off_t */
 
-#include <videolan/vlc.h>
+#include <vlc/vlc.h>
 
 #include "stream_control.h"
 #include "input_ext-intf.h"
  * in all the FIFOs, but it may be not enough.
  */
 
-/*****************************************************************************
- * Constants
- *****************************************************************************/
-
-/* Maximum number of samples used to compute the dynamic average value.
+/* 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)
  */
-#define CR_MAX_AVERAGE_COUNTER 40
+
+static void ClockNewRef( pgrm_descriptor_t * p_pgrm,
+                         mtime_t i_clock, mtime_t i_sysdate );
+
+/*****************************************************************************
+ * Constants
+ *****************************************************************************/
 
 /* Maximum gap allowed between two CRs. */
-#define CR_MAX_GAP 1000000
+#define CR_MAX_GAP 2000000
+
+/* Latency introduced on DVDs with CR == 0 on chapter change - this is from
+ * my dice --Meuuh */
+#define CR_MEAN_PTS_GAP 300000
 
 /*****************************************************************************
  * ClockToSysdate: converts a movie clock to system date
  *****************************************************************************/
-static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
-                         mtime_t i_clock, mtime_t i_sysdate );
 static mtime_t ClockToSysdate( input_thread_t * p_input,
                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
 {
@@ -87,7 +91,7 @@ static mtime_t ClockToSysdate( input_thread_t * p_input,
 
     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
     {
-        i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref) 
+        i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref)
                         * (mtime_t)p_input->stream.control.i_rate
                         * (mtime_t)300;
         i_sysdate /= 27;
@@ -114,16 +118,11 @@ static mtime_t ClockCurrent( input_thread_t * p_input,
 /*****************************************************************************
  * ClockNewRef: writes a new clock reference
  *****************************************************************************/
-static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
+static void ClockNewRef( pgrm_descriptor_t * p_pgrm,
                          mtime_t i_clock, mtime_t i_sysdate )
 {
     p_pgrm->cr_ref = i_clock;
-    /* this is actually a kludge, but it gives better results when scr
-    * is zero in DVDs: we are 3-4 ms in advance instead of sometimes
-    * 100ms late  */
-    p_pgrm->sysdate_ref = ( p_pgrm->last_syscr && !i_clock )
-                          ? p_pgrm->last_syscr
-                          : i_sysdate ;
+    p_pgrm->sysdate_ref = i_sysdate ;
 }
 
 /*****************************************************************************
@@ -133,7 +132,7 @@ static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
 {
     p_pgrm->last_cr = 0;
-    p_pgrm->last_syscr = 0;
+    p_pgrm->last_pts = 0;
     p_pgrm->cr_ref = 0;
     p_pgrm->sysdate_ref = 0;
     p_pgrm->delta_cr = 0;
@@ -148,6 +147,7 @@ void input_ClockInit( pgrm_descriptor_t * p_pgrm )
 int input_ClockManageControl( input_thread_t * p_input,
                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
 {
+    vlc_value_t val;
     int i_return_value = UNDEF_S;
 
     vlc_mutex_lock( &p_input->stream.stream_lock );
@@ -155,25 +155,29 @@ int input_ClockManageControl( input_thread_t * p_input,
     if( p_input->stream.i_new_status == PAUSE_S )
     {
         int i_old_status;
+
         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;
+        vlc_mutex_unlock( &p_input->stream.control.control_lock );
+
         vlc_cond_wait( &p_input->stream.stream_wait,
                        &p_input->stream.stream_lock );
-        p_pgrm->last_syscr = 0;
-        ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
+        ClockNewRef( p_pgrm, i_clock, p_pgrm->last_pts > mdate() ?
+                                      p_pgrm->last_pts : mdate() );
 
         if( p_input->stream.i_new_status == PAUSE_S )
-        { 
+        {
             /* PAUSE_S undoes the pause state: Return to old state. */
+            vlc_mutex_lock( &p_input->stream.control.control_lock );
             p_input->stream.control.i_status = i_old_status;
+            vlc_mutex_unlock( &p_input->stream.control.control_lock );
+
             p_input->stream.i_new_status = UNDEF_S;
             p_input->stream.i_new_rate = UNDEF_S;
         }
 
         /* We handle i_new_status != PAUSE_S below... */
-        vlc_mutex_unlock( &p_input->stream.control.control_lock );
 
         i_return_value = PAUSE_S;
     }
@@ -184,7 +188,7 @@ int input_ClockManageControl( input_thread_t * p_input,
 
         p_input->stream.control.i_status = p_input->stream.i_new_status;
 
-        ClockNewRef( p_input, p_pgrm, i_clock,
+        ClockNewRef( p_pgrm, i_clock,
                      ClockToSysdate( p_input, p_pgrm, i_clock ) );
 
         if( p_input->stream.control.i_status == PLAYING_S )
@@ -202,6 +206,12 @@ int input_ClockManageControl( input_thread_t * p_input,
             input_EscapeAudioDiscontinuity( p_input );
         }
 
+        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 );
+
         p_input->stream.i_new_status = UNDEF_S;
         p_input->stream.i_new_rate = UNDEF_S;
 
@@ -225,22 +235,27 @@ void input_ClockManageRef( input_thread_t * p_input,
         p_pgrm = p_input->stream.p_selected_program;
     }
 
-    if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) || ( i_clock == 0 ) )
+    if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) ||
+        ( i_clock == 0 && p_pgrm->last_cr != 0 ) )
     {
         /* Feed synchro with a new reference point. */
-        ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
+        ClockNewRef( p_pgrm, i_clock,
+                     p_pgrm->last_pts + CR_MEAN_PTS_GAP > mdate() ?
+                     p_pgrm->last_pts + CR_MEAN_PTS_GAP : mdate() );
         p_pgrm->i_synchro_state = SYNCHRO_OK;
 
         if( p_input->stream.b_pace_control
              && p_input->stream.p_selected_program == p_pgrm )
         {
             p_pgrm->last_cr = i_clock;
-            mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
+            if( !p_input->b_out_pace_control )
+            {
+                mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
+            }
         }
         else
         {
             p_pgrm->last_cr = 0;
-            p_pgrm->last_syscr = 0;
             p_pgrm->delta_cr = 0;
             p_pgrm->c_average_count = 0;
         }
@@ -254,7 +269,7 @@ void input_ClockManageRef( input_thread_t * p_input,
             /* Stream discontinuity, for which we haven't received a
              * warning from the stream control facilities (dd-edited
              * stream ?). */
-            intf_WarnMsg( 1, "Clock gap, unexpected stream discontinuity" );
+            msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
             input_ClockInit( p_pgrm );
             p_pgrm->i_synchro_state = SYNCHRO_START;
             input_EscapeDiscontinuity( p_input );
@@ -265,15 +280,13 @@ void input_ClockManageRef( input_thread_t * p_input,
         if( p_input->stream.b_pace_control
              && p_input->stream.p_selected_program == p_pgrm )
         {
-            /* We remember the last system date to be able to restart
-             * the synchro we statistically better continuity, after 
-             * a zero scr */
-            p_pgrm->last_syscr = ClockToSysdate( p_input, p_pgrm, i_clock );
-            
             /* Wait a while before delivering the packets to the decoder.
              * In case of multiple programs, we arbitrarily follow the
-             * clock of the first program. */
-            mwait( p_pgrm->last_syscr );
+             * clock of the selected program. */
+            if( !p_input->b_out_pace_control )
+            {
+                mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
+            }
 
             /* Now take into account interface changes. */
             input_ClockManageControl( p_input, p_pgrm, i_clock );
@@ -284,12 +297,12 @@ void input_ClockManageRef( input_thread_t * p_input,
             mtime_t     i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
 
             /* Bresenham algorithm to smooth variations. */
-            if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
+            if( p_pgrm->c_average_count == p_input->i_cr_average )
             {
                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
-                                        * (CR_MAX_AVERAGE_COUNTER - 1)
+                                        * (p_input->i_cr_average - 1)
                                       + ( i_extrapoled_clock - i_clock ) )
-                                    / CR_MAX_AVERAGE_COUNTER;
+                                    / p_input->i_cr_average;
             }
             else
             {
@@ -317,9 +330,9 @@ mtime_t input_ClockGetTS( input_thread_t * p_input,
 
     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
     {
-        return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
-                 + DEFAULT_PTS_DELAY
-                 + (p_main->i_desync > 0 ? p_main->i_desync : 0) );
+        p_pgrm->last_pts = ClockToSysdate( p_input, p_pgrm,
+                                           i_ts + p_pgrm->delta_cr );
+        return( p_pgrm->last_pts + p_input->i_pts_delay );
     }
     else
     {