]> git.sesse.net Git - vlc/blobdiff - src/video_parser/vpar_synchro.c
. rajout de l'option -Winline
[vlc] / src / video_parser / vpar_synchro.c
index 7c7c18a8eacd608d9755c9e8f3d7a0aa1a9d0620..fe813eeada747af39072edaab7b500e57867be7a 100644 (file)
@@ -9,29 +9,31 @@
  * 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 GNU
- * General Public License for more details.
+ * 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 Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+#include "defs.h"
+
 #include <stdlib.h>                                                /* free() */
 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
 #include <sys/uio.h>                                            /* "input.h" */
 
 #include "config.h"
 #include "common.h"
-#include "mtime.h"
 #include "threads.h"
+#include "mtime.h"
+#include "plugins.h"
 
 #include "intf_msg.h"
 
  */
 
 #ifdef SAM_SYNCHRO
-/*****************************************************************************
- * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
- *****************************************************************************/
-float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
-{
-
-    tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
-    tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
-                        / ( MAX_COUNT + 1 );
-
-    return tab->deviation;
-}
 
 /*****************************************************************************
  * vpar_SynchroUpdateStructures : Update the synchro structures
  *****************************************************************************/
 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
-                                   int i_coding_type, int dropped )
+                                   int i_coding_type, boolean_t b_kept )
 {
-    float candidate_deviation;
-    float optimal_deviation;
-    float predict;
-    mtime_t i_current_pts;
-    mtime_t i_delay;
-    mtime_t i_displaydate;
-    decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
-
-    /* interpolate the current _decode_ PTS */
-    i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
-                    decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
-                    0;
-    if( !i_current_pts )
+    double          i_can_display;
+    mtime_t         i_pts;
+    pes_packet_t *  p_pes = p_vpar->bit_stream.p_decoder_fifo->buffer[
+                               p_vpar->bit_stream.p_decoder_fifo->i_start ];
+
+    /* try to guess the current DTS and PTS */
+    if( p_pes->b_has_pts )
     {
-        i_current_pts = p_vpar->synchro.i_last_decode_pts
-                       + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
-    }
-    p_vpar->synchro.i_last_decode_pts = i_current_pts;
+        i_pts = p_pes->i_pts;
 
-    /* see if the current image has a pts - if not, set to 0 */
-    p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
-            = i_current_pts;
+        /* if the image is I type, then the presentation timestamp is
+         * the PTS of the PES. Otherwise, we calculate it with the
+         * theorical framerate value */
+        if( i_coding_type == I_CODING_TYPE )
+        {
+            p_vpar->synchro.i_last_pts = p_pes->i_pts;
+        }
+        else
+        {
+            p_vpar->synchro.i_last_pts += p_vpar->synchro.i_theorical_delay;
+        }
 
-    /* update display time */
-    i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
-                    decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
-                    0;
-    if( !i_displaydate || i_coding_type != I_CODING_TYPE )
+        p_pes->b_has_pts = 0;
+    }
+    else
     {
-        if (!p_vpar->synchro.i_images_since_pts )
-            p_vpar->synchro.i_images_since_pts = 10;
-
-        i_displaydate = p_vpar->synchro.i_last_display_pts
-                       + 1000000.0 / (p_vpar->synchro.theorical_fps);
-        //fprintf (stderr, "  ");
+        p_vpar->synchro.i_last_pts += p_vpar->synchro.i_theorical_delay;
+        i_pts = p_vpar->synchro.i_last_pts;
     }
 
-    decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
-
-    /* else fprintf (stderr, "R ");
-    if (dropped) fprintf (stderr, "  "); else fprintf (stderr, "* ");
-    fprintf (stderr, "%i ", i_coding_type);
-    fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
-
-    p_vpar->synchro.i_images_since_pts--;
-    p_vpar->synchro.i_last_display_pts = i_displaydate;
-
-
-
     /* update structures */
     switch(i_coding_type)
     {
         case P_CODING_TYPE:
 
-            p_vpar->synchro.current_p_count++;
-            if( !dropped ) p_vpar->synchro.nondropped_p_count++;
+            p_vpar->synchro.i_P_seen++;
+            if( b_kept ) p_vpar->synchro.i_P_kept++;
             break;
 
         case B_CODING_TYPE:
-            p_vpar->synchro.current_b_count++;
-            if( !dropped ) p_vpar->synchro.nondropped_b_count++;
+            p_vpar->synchro.i_B_seen++;
+            if( b_kept ) p_vpar->synchro.i_B_kept++;
             break;
 
         case I_CODING_TYPE:
 
-            /* update information about images we can decode */
-            if (i_current_pts != p_vpar->synchro.i_last_i_pts)
-            {
-                if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
-                {
-                    p_vpar->synchro.theorical_fps = (p_vpar->synchro.theorical_fps + 1000000.0 * (1 + p_vpar->synchro.current_b_count + p_vpar->synchro.current_p_count) / (i_current_pts - p_vpar->synchro.i_last_i_pts)) / 2;
-                }
-                p_vpar->synchro.i_last_i_pts = i_current_pts;
-            }
-
-            if( !dropped )
+            /* update the last I PTS we have, we need it to
+             * calculate the theorical framerate */
+            if (i_pts != p_vpar->synchro.i_last_seen_I_pts)
             {
-                if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
+                if ( p_vpar->synchro.i_last_seen_I_pts )
                 {
-                    p_vpar->synchro.actual_fps = (p_vpar->synchro.actual_fps + 1000000.0 * (1 + p_vpar->synchro.nondropped_b_count + p_vpar->synchro.nondropped_p_count) / (i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts)) / 2;
+                    p_vpar->synchro.i_theorical_delay =
+                            ( i_pts - p_vpar->synchro.i_last_seen_I_pts )
+                          / ( 1 + p_vpar->synchro.i_B_seen
+                                + p_vpar->synchro.i_P_seen);
                 }
-
+                p_vpar->synchro.i_last_seen_I_pts = i_pts;
             }
 
+            /* now we calculated all statistics, it's time to
+             * decide what we have the time to display */
+            i_can_display = (float)(i_pts - p_vpar->synchro.i_last_kept_I_pts)
+                                / p_vpar->synchro.i_delay;
 
-            /* update all the structures for P images */
-
-            /* period == 1 */
-            optimal_deviation = vpar_SynchroUpdateTab(
-                            &p_vpar->synchro.tab_p[0],
-                            p_vpar->synchro.current_p_count);
-            predict = p_vpar->synchro.tab_p[0].mean;
-
-            /* period == 2 */
-            candidate_deviation = vpar_SynchroUpdateTab(
-                            &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
-                            p_vpar->synchro.current_p_count);
-            if (candidate_deviation < optimal_deviation)
-            {
-                optimal_deviation = candidate_deviation;
-                predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
-            }
-
-            /* period == 3 */
-            candidate_deviation = vpar_SynchroUpdateTab(
-                            &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
-                            p_vpar->synchro.current_p_count);
-            if (candidate_deviation < optimal_deviation)
-            {
-                optimal_deviation = candidate_deviation;
-                predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
-            }
-
-            p_vpar->synchro.p_count_predict = predict;
-            p_vpar->synchro.current_p_count = 0;
-
-
-            /* update all the structures for B images */
-
-            /* period == 1 */
-            optimal_deviation = vpar_SynchroUpdateTab(
-                            &p_vpar->synchro.tab_b[0],
-                            p_vpar->synchro.current_b_count);
-            predict = p_vpar->synchro.tab_b[0].mean;
+            p_vpar->synchro.b_all_I = 0;
+            p_vpar->synchro.b_all_B = 0;
+            p_vpar->synchro.b_all_P = 0;
+            p_vpar->synchro.displayable_p = 0;
+            p_vpar->synchro.displayable_b = 0;
 
-            /* period == 2 */
-            candidate_deviation = vpar_SynchroUpdateTab(
-                            &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
-                            p_vpar->synchro.current_b_count);
-            if (candidate_deviation < optimal_deviation)
+            if( ( p_vpar->synchro.b_all_I = ( i_can_display > 1 ) ) )
             {
-                optimal_deviation = candidate_deviation;
-                predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
-            }
+                i_can_display -= 1;
 
-            /* period == 3 */
-            candidate_deviation = vpar_SynchroUpdateTab(
-                            &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
-                            p_vpar->synchro.current_b_count);
-            if (candidate_deviation < optimal_deviation)
-            {
-                optimal_deviation = candidate_deviation;
-                predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
-            }
-
-            p_vpar->synchro.b_count_predict = predict;
-            p_vpar->synchro.current_b_count = 0;
-
-            /* now we calculated all statistics, it's time to
-             * decide what we have the time to display
-             */
-            i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
-
-            p_vpar->synchro.can_display_i
-                = ( p_vpar->synchro.i_mean_decode_time < i_delay );
-
-            p_vpar->synchro.can_display_p
-                    = ( p_vpar->synchro.i_mean_decode_time
-                    * (1 + p_vpar->synchro.p_count_predict) < i_delay );
+                if( !( p_vpar->synchro.b_all_P
+                        = ( i_can_display > p_vpar->synchro.i_P_seen ) ) )
+                {
+                    p_vpar->synchro.displayable_p = i_can_display;
+                }
+                else
+                {
+                    i_can_display -= p_vpar->synchro.i_P_seen;
 
-            if( !p_vpar->synchro.can_display_p )
-            {
-                p_vpar->synchro.displayable_p
-                    = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
-                if( p_vpar->synchro.displayable_p < 0 )
-                    p_vpar->synchro.displayable_p = 0;
-            }
-            else
-                p_vpar->synchro.displayable_p = 0;
-
-            if( p_vpar->synchro.can_display_p
-                && !(p_vpar->synchro.can_display_b
-                    = ( p_vpar->synchro.i_mean_decode_time
-                    * (1 + p_vpar->synchro.b_count_predict
-                        + p_vpar->synchro.p_count_predict)) < i_delay) )
-            {
-                p_vpar->synchro.displayable_b
-                    = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
-                        - p_vpar->synchro.can_display_p;
+                    if( !( p_vpar->synchro.b_all_B
+                            = ( i_can_display > p_vpar->synchro.i_B_seen ) ) )
+                    {
+                        p_vpar->synchro.displayable_b = i_can_display;
+                    }
+                }
             }
-            else
-                p_vpar->synchro.displayable_b = 0;
 
-#if 0
-            fprintf( stderr,
-                "I %i  P %i (%f)  B %i (%f)\n",
-                p_vpar->synchro.can_display_i,
-                p_vpar->synchro.can_display_p,
-                p_vpar->synchro.displayable_p,
-                p_vpar->synchro.can_display_b,
-                p_vpar->synchro.displayable_b );
+#if 1
+            if( p_vpar->synchro.b_all_I )
+                intf_ErrMsg( " I: 1/1  " );
+            if( p_vpar->synchro.b_all_P )
+                intf_ErrMsg( "P: %i/%i  ", p_vpar->synchro.i_P_seen,
+                                           p_vpar->synchro.i_P_seen );
+            else if( p_vpar->synchro.displayable_p > 0 )
+                intf_ErrMsg( "P: %.2f/%i  ", p_vpar->synchro.displayable_p,
+                                             p_vpar->synchro.i_P_seen );
+            if( p_vpar->synchro.b_all_B )
+                intf_ErrMsg( "B: %i/%i", p_vpar->synchro.i_B_seen,
+                                         p_vpar->synchro.i_B_seen );
+            else if( p_vpar->synchro.displayable_b > 0 )
+                intf_ErrMsg( "B: %.2f/%i", p_vpar->synchro.displayable_b,
+                                           p_vpar->synchro.i_B_seen );
+            intf_ErrMsg( "                    \r" );
 #endif
+            p_vpar->synchro.i_P_seen = 0;
+            p_vpar->synchro.i_B_seen = 0;
 
             /* update some values */
-            if( !dropped )
+            if( b_kept )
             {
-                p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
-                p_vpar->synchro.nondropped_p_count = 0;
-                p_vpar->synchro.nondropped_b_count = 0;
+                p_vpar->synchro.i_last_kept_I_pts = i_pts;
+                p_vpar->synchro.i_P_kept = 0;
+                p_vpar->synchro.i_B_kept = 0;
             }
 
             break;
-
     }
-
-    p_vpar->synchro.modulo++;
-
 }
 
 /*****************************************************************************
@@ -290,45 +195,59 @@ void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
                               int i_structure )
 {
-    mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
+    mtime_t i_delay = p_vpar->synchro.i_last_pts - mdate();
+
+    //return (i_coding_type == I_CODING_TYPE);
 
     switch( i_coding_type )
     {
         case I_CODING_TYPE:
 
-            return( p_vpar->synchro.can_display_i );
+            //intf_ErrMsg( " I %f      %f\nI ", 1000000.0 / p_vpar->synchro.i_theorical_delay, 1000000.0 / p_vpar->synchro.i_delay );
+            return( p_vpar->synchro.b_all_I );
 
         case P_CODING_TYPE:
 
-            if( p_vpar->synchro.can_display_p )
+            //return(1);
+            if( p_vpar->synchro.b_all_P )
+            {
+                //intf_ErrMsg( " p  " );
                 return( 1 );
+            }
 
             if( p_vpar->synchro.displayable_p * i_delay
-                < p_vpar->synchro.i_mean_decode_time )
+                < p_vpar->synchro.i_delay )
             {
-                //fprintf( stderr, "trashed a P\n");
+                //intf_ErrMsg( " -  " );
                 return( 0 );
             }
 
             p_vpar->synchro.displayable_p--;
+            //intf_ErrMsg( " p> " );
             return( 1 );
 
         case B_CODING_TYPE:
 
-            if( p_vpar->synchro.can_display_b )
+            if( p_vpar->synchro.b_all_B )
+            {
+                //intf_ErrMsg( "b " );
                 return( 1 );
+            }
 
-            /* modulo & 0x3 is here to add some randomness */
-            if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
-                * p_vpar->synchro.i_mean_decode_time )
+            if( p_vpar->synchro.displayable_b <= 0 )
             {
-                //fprintf( stderr, "trashed a B\n");
+                //intf_ErrMsg( "  " );
                 return( 0 );
             }
 
-            if( p_vpar->synchro.displayable_b <= 0 )
+            if( i_delay < 0 )
+            {
+                //intf_ErrMsg( "ยท " );
+                p_vpar->synchro.displayable_b -= 0.5;
                 return( 0 );
+            }
 
+            //intf_ErrMsg( "b " );
             p_vpar->synchro.displayable_b--;
             return( 1 );
     }
@@ -343,7 +262,7 @@ boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
                         int i_structure )
 {
-    vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
+    vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
 
 }
 
@@ -353,13 +272,11 @@ void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
                             int i_structure )
 {
-    vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
+    vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
 
-    p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
-    p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
-        = i_coding_type;
+    p_vpar->synchro.i_date_fifo[p_vpar->synchro.i_stop] = mdate();
 
-    p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
+    FIFO_INCREMENT( i_stop );
 
 }
 
@@ -368,21 +285,29 @@ void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
  *****************************************************************************/
 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
 {
-    mtime_t i_decode_time;
-
-    i_decode_time = (mdate() -
-            p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
-        / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
+    if( p_vpar->synchro.i_stop != p_vpar->synchro.i_start )
+    {
+        mtime_t i_delay;
 
-    p_vpar->synchro.i_mean_decode_time =
-        ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
+        i_delay = ( mdate() -
+            p_vpar->synchro.i_date_fifo[p_vpar->synchro.i_start] )
+              / ( (p_vpar->synchro.i_stop - p_vpar->synchro.i_start) & 0x0f );
 
-    /* fprintf (stderr,
-        "decoding time was %lli\n",
-        p_vpar->synchro.i_mean_decode_time); */
+        p_vpar->synchro.i_delay =
+            ( 7 * p_vpar->synchro.i_delay + i_delay ) >> 3;
 
-    p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
+#if 0
+        intf_ErrMsg( "decode %lli (mean %lli, theorical %lli)\n",
+                     i_delay, p_vpar->synchro.i_delay,
+                     p_vpar->synchro.i_theorical_delay );
+#endif
+    }
+    else
+    {
+        intf_ErrMsg( "vpar error: critical ! fifo full\n" );
+    }
 
+    FIFO_INCREMENT( i_start );
 }
 
 /*****************************************************************************
@@ -390,25 +315,27 @@ void vpar_SynchroEnd( vpar_thread_t * p_vpar )
  *****************************************************************************/
 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
 {
-    mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
-
 #if 0
+
+    mtime_t i_displaydate = p_vpar->synchro.i_last_pts;
+
     static mtime_t i_delta = 0;
 
-    fprintf( stderr,
-        "displaying type %i with delay %lli and delta %lli\n",
-        p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
+    intf_ErrMsg( "displaying image with delay %lli and delta %lli\n",
         i_displaydate - mdate(),
         i_displaydate - i_delta );
 
-    fprintf (stderr,
-        "theorical fps: %f - actual fps: %f \n",
-        p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
+    intf_ErrMsg ( "theorical fps: %f - actual fps: %f \n",
+        1000000.0 / p_vpar->synchro.i_theorical_delay, 1000000.0 / p_vpar->synchro.i_delay );
 
     i_delta = i_displaydate;
-#endif
 
     return i_displaydate;
+#else
+
+    return p_vpar->synchro.i_last_pts;
+
+#endif
 }
 
 #endif
@@ -601,9 +528,6 @@ boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
     {
     case I_CODING_TYPE:
 
-//fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
-//         p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
-
         p_vpar->synchro.r_p_average =
             (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
         p_vpar->synchro.r_b_average =
@@ -663,7 +587,6 @@ void vpar_SynchroUpdateLevel()
 
 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
 {
-//fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
     return( p_vpar->synchro.i_current_frame_date );
 }