]> git.sesse.net Git - vlc/blobdiff - src/input/input_clock.c
* Altivec-enabled version of fastmemcpy
[vlc] / src / input / input_clock.c
index bcebd87c7baaf476147a6613362bda571c5f1735..96af2abba727252e972facfab268abcfe5241feb 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * input_clock.c: Clock/System date convertions, stream management
  *****************************************************************************
- * Copyright (C) 1999, 2000 VideoLAN
- * $Id: input_clock.c,v 1.21 2001/07/20 16:20:25 massiot Exp $
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: input_clock.c,v 1.29 2002/03/05 17:46:33 stef Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <string.h>                                    /* memcpy(), memset() */
 #include <sys/types.h>                                              /* off_t */
 
-#include "config.h"
-#include "common.h"
-#include "threads.h"
-#include "mtime.h"
-#include "intf_msg.h"
+#include <videolan/vlc.h>
 
 #include "stream_control.h"
 #include "input_ext-intf.h"
@@ -84,6 +78,8 @@
 /*****************************************************************************
  * 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 )
 {
@@ -93,10 +89,10 @@ static mtime_t ClockToSysdate( input_thread_t * p_input,
     {
         i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref) 
                         * (mtime_t)p_input->stream.control.i_rate
-                        * (mtime_t)300
-                        / (mtime_t)27
-                        / (mtime_t)DEFAULT_RATE
-                        + (mtime_t)p_pgrm->sysdate_ref;
+                        * (mtime_t)300;
+        i_sysdate /= 27;
+        i_sysdate /= 1000;
+        i_sysdate += (mtime_t)p_pgrm->sysdate_ref;
     }
 
     return( i_sysdate );
@@ -122,7 +118,12 @@ static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
                          mtime_t i_clock, mtime_t i_sysdate )
 {
     p_pgrm->cr_ref = i_clock;
-    p_pgrm->sysdate_ref = p_pgrm->last_syscr ? p_pgrm->last_syscr : i_sysdate;
+    /* 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 ;
 }
 
 /*****************************************************************************
@@ -218,6 +219,12 @@ int input_ClockManageControl( input_thread_t * p_input,
 void input_ClockManageRef( input_thread_t * p_input,
                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
 {
+    /* take selected program if none specified */
+    if( !p_pgrm )
+    {
+        p_pgrm = p_input->stream.p_selected_program;
+    }
+
     if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) || ( i_clock == 0 ) )
     {
         /* Feed synchro with a new reference point. */
@@ -225,7 +232,7 @@ void input_ClockManageRef( input_thread_t * p_input,
         p_pgrm->i_synchro_state = SYNCHRO_OK;
 
         if( p_input->stream.b_pace_control
-             && p_input->stream.pp_programs[0] == p_pgrm )
+             && p_input->stream.p_selected_program == p_pgrm )
         {
             p_pgrm->last_cr = i_clock;
             mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
@@ -256,12 +263,16 @@ void input_ClockManageRef( input_thread_t * p_input,
         p_pgrm->last_cr = i_clock;
 
         if( p_input->stream.b_pace_control
-             && p_input->stream.pp_programs[0] == p_pgrm )
+             && 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. */
-            p_pgrm->last_syscr = ClockToSysdate( p_input, p_pgrm, i_clock );
             mwait( p_pgrm->last_syscr );
 
             /* Now take into account interface changes. */
@@ -298,10 +309,17 @@ void input_ClockManageRef( input_thread_t * p_input,
 mtime_t input_ClockGetTS( input_thread_t * p_input,
                           pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
 {
+    /* take selected program if none specified */
+    if( !p_pgrm )
+    {
+        p_pgrm = p_input->stream.p_selected_program;
+    }
+
     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
     {
         return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
-                 + DEFAULT_PTS_DELAY );
+                 + DEFAULT_PTS_DELAY
+                 + (p_main->i_desync > 0 ? p_main->i_desync : 0) );
     }
     else
     {