]> git.sesse.net Git - vlc/commitdiff
* src/misc/mtime.c, include/mtime.h: new common "date" API for date incrementation...
authorGildas Bazin <gbazin@videolan.org>
Sat, 10 Jul 2004 18:08:09 +0000 (18:08 +0000)
committerGildas Bazin <gbazin@videolan.org>
Sat, 10 Jul 2004 18:08:09 +0000 (18:08 +0000)
  (is going to replace audio_date_t).

include/mtime.h
include/vlc_common.h
src/misc/mtime.c

index 6241b30239059b40b25f1739a94795a09c29674a..582941399477ccf7eac1d3cb71f79e5870e1c109 100644 (file)
@@ -9,7 +9,7 @@
  * Functions prototyped are implemented in interface/mtime.c.
  *****************************************************************************
  * Copyright (C) 1996, 1997, 1998, 1999, 2000 VideoLAN
- * $Id: mtime.h,v 1.14 2003/12/02 01:54:30 rocky Exp $
+ * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *
@@ -59,3 +59,20 @@ VLC_EXPORT( void,    mwait,    ( mtime_t date ) );
 VLC_EXPORT( void,    msleep,   ( mtime_t delay ) );
 VLC_EXPORT( char *,  secstotimestr, ( char *psz_buffer, int secs ) );
 
+/*****************************************************************************
+ * date_t: date incrementation without long-term rounding errors
+ *****************************************************************************/
+struct date_t
+{
+    mtime_t  date;
+    uint32_t i_divider_num;
+    uint32_t i_divider_den;
+    uint32_t i_remainder;
+};
+
+VLC_EXPORT( void,    date_Init,      ( date_t *, uint32_t, uint32_t ) );
+VLC_EXPORT( void,    date_Change,    ( date_t *, uint32_t, uint32_t ) );
+VLC_EXPORT( void,    date_Set,       ( date_t *, mtime_t ) );
+VLC_EXPORT( mtime_t, date_Get,       ( const date_t * ) );
+VLC_EXPORT( void,    date_Move,      ( date_t *, mtime_t ) );
+VLC_EXPORT( mtime_t, date_Increment, ( date_t *, uint32_t ) );
index 5b9df27e952fdddd5a91fe1cd226fa828905499d..8b373085e43e6f0efdf217d950a5d5dcd26b40c3 100644 (file)
@@ -183,6 +183,7 @@ typedef uint32_t vlc_fourcc_t;
 typedef struct libvlc_t libvlc_t;
 typedef struct vlc_t vlc_t;
 typedef struct variable_t variable_t;
+typedef struct date_t date_t;
 
 /* Messages */
 typedef struct msg_bank_t msg_bank_t;
index ea03af0e026fdef1567e9e90035959934537ee60..4f2bcaa75fb40cda7bedcbcbf105f362baa3c3a6 100644 (file)
@@ -3,7 +3,7 @@
  * Functions are prototyped in mtime.h.
  *****************************************************************************
  * Copyright (C) 1998-2004 VideoLAN
- * $Id: mtime.c,v 1.43 2004/01/25 17:16:06 zorglub Exp $
+ * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *
@@ -323,3 +323,94 @@ void msleep( mtime_t delay )
 #endif
 }
 
+/*
+ * Date management (internal and external)
+ */
+
+/**
+ * Initialize a date_t.
+ *
+ * \param date to initialize
+ * \param divider (sample rate) numerator
+ * \param divider (sample rate) denominator
+ */
+
+void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
+{
+    p_date->date = 0;
+    p_date->i_divider_num = i_divider_n;
+    p_date->i_divider_den = i_divider_d;
+    p_date->i_remainder = 0;
+}
+
+/**
+ * Change a date_t.
+ *
+ * \param date to change
+ * \param divider (sample rate) numerator
+ * \param divider (sample rate) denominator
+ */
+
+void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
+{
+    p_date->i_divider_num = i_divider_n;
+    p_date->i_divider_den = i_divider_d;
+}
+
+/**
+ * Set the date value of a date_t.
+ *
+ * \param date to set
+ * \param date value
+ */
+void date_Set( date_t *p_date, mtime_t i_new_date )
+{
+    p_date->date = i_new_date;
+    p_date->i_remainder = 0;
+}
+
+/**
+ * Get the date of a date_t
+ *
+ * \param date to get
+ * \return date value
+ */
+mtime_t date_Get( const date_t *p_date )
+{
+    return p_date->date;
+}
+
+/**
+ * Move forwards or backwards the date of a date_t.
+ *
+ * \param date to move
+ * \param difference value
+ */
+void date_Move( date_t *p_date, mtime_t i_difference )
+{
+    p_date->date += i_difference;
+}
+
+/**
+ * Increment the date and return the result, taking into account
+ * rounding errors.
+ *
+ * \param date to increment
+ * \param incrementation in number of samples
+ * \return date value
+ */
+mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
+{
+    mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
+    p_date->date += i_dividend / p_date->i_divider_num * p_date->i_divider_den;
+    p_date->i_remainder += (int)(i_dividend % p_date->i_divider_num);
+
+    if( p_date->i_remainder >= p_date->i_divider_num )
+    {
+        /* This is Bresenham algorithm. */
+        p_date->date += p_date->i_divider_den;
+        p_date->i_remainder -= p_date->i_divider_num;
+    }
+
+    return p_date->date;
+}