]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_output.c
. nouveaux plugins - ne fonctionnent pas encore tous
[vlc] / src / video_output / video_output.c
index 32bf673ed61b143e28eb78075113c8a1d1fa1830..2514f4d7f465e304909e81573d9587c12792d27d 100644 (file)
@@ -1,29 +1,49 @@
 /*****************************************************************************
  * video_output.c : video output thread
- * (c)2000 VideoLAN
- *****************************************************************************
  * This module describes the programming interface for video output threads.
  * It includes functions allowing to open a new thread, send pictures to a
- * thread, and destroy a previously oppenned video output thread.
+ * thread, and destroy a previously oppened video output thread.
+ *****************************************************************************
+ * Copyright (C) 2000 VideoLAN
+ *
+ * Authors:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * 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.
+ *
+ * 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 <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include "defs.h"
+
+#include <errno.h>                                                 /* ENOMEM */
+#include <stdlib.h>                                                /* free() */
+#include <stdio.h>                                              /* sprintf() */
+#include <string.h>                                            /* strerror() */
 
-#include "common.h"
 #include "config.h"
+#include "common.h"
+#include "threads.h"
 #include "mtime.h"
-#include "vlc_thread.h"
+#include "plugins.h"
 #include "video.h"
 #include "video_output.h"
 #include "video_text.h"
-#include "video_sys.h"
+#include "video_spu.h"
 #include "video_yuv.h"
+
 #include "intf_msg.h"
 #include "main.h"
 
@@ -50,6 +70,7 @@ static void     RenderSubPicture  ( vout_thread_t *p_vout,
 static void     RenderInterface   ( vout_thread_t *p_vout );
 static int      RenderIdle        ( vout_thread_t *p_vout );
 static void     RenderInfo        ( vout_thread_t *p_vout );
+static void     Synchronize       ( vout_thread_t *p_vout, s64 i_delay );
 static int      Manage            ( vout_thread_t *p_vout );
 static int      Align             ( vout_thread_t *p_vout, int *pi_x,
                                     int *pi_y, int i_width, int i_height,
@@ -65,22 +86,48 @@ static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
  * If pi_status is NULL, then the function will block until the thread is ready.
  * If not, it will be updated using one of the THREAD_* constants.
  *****************************************************************************/
-vout_thread_t * vout_CreateThread               ( char *psz_display, int i_root_window,
-                                                  int i_width, int i_height, int *pi_status )
+vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
+                          int i_width, int i_height, int *pi_status,
+                          int i_method, void *p_data )
 {
     vout_thread_t * p_vout;                             /* thread descriptor */
     int             i_status;                               /* thread status */
     int             i_index;               /* index for array initialization */
+    char *          psz_method;
 
     /* Allocate descriptor */
     intf_DbgMsg("\n");
     p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
     if( p_vout == NULL )
     {
-        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
+        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
         return( NULL );
     }
 
+    /* Request an interface plugin */
+    psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
+
+    if( RequestPlugin( &p_vout->vout_plugin, psz_method ) < 0 )
+    {
+        intf_ErrMsg( "error: could not open video plugin %s.so\n", psz_method );
+        free( p_vout );
+        return( NULL );
+    }
+
+    /* Get plugins */
+    p_vout->p_sys_create = 
+        GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
+    p_vout->p_sys_init =
+        GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
+    p_vout->p_sys_end =
+        GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
+    p_vout->p_sys_destroy =
+        GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
+    p_vout->p_sys_manage =
+        GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
+    p_vout->p_sys_display =
+        GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
+
     /* Initialize thread properties - thread id and locks will be initialized
      * later */
     p_vout->b_die               = 0;
@@ -99,16 +146,17 @@ vout_thread_t * vout_CreateThread               ( char *psz_display, int i_root_
     p_vout->i_bytes_per_pixel   = 2;
     p_vout->f_gamma             = VOUT_GAMMA;
 
-    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
+    p_vout->b_grayscale         = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
+                                                       VOUT_GRAYSCALE_DEFAULT );
     p_vout->b_info              = 0;
     p_vout->b_interface         = 0;
     p_vout->b_scale             = 0;
 
     p_vout->p_set_palette       = SetPalette;
 
-    intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
-                p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
-                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
+    intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
+                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
+                 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
 
     /* Initialize idle screen */
     p_vout->last_display_date   = mdate();
@@ -130,16 +178,24 @@ vout_thread_t * vout_CreateThread               ( char *psz_display, int i_root_
     {
         p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
         p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
+    }
+    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
+    {
         p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
         p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
     }
+    p_vout->i_pictures = 0;
+
+    /* Initialize synchronization informations */
+    p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
 
     /* Create and initialize system-dependant method - this function issues its
      * own error messages */
-    if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
+    if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
     {
-      free( p_vout );
-      return( NULL );
+        TrashPlugin( p_vout->vout_plugin );
+        free( p_vout );
+        return( NULL );
     }
     intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
                 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
@@ -157,20 +213,30 @@ vout_thread_t * vout_CreateThread               ( char *psz_display, int i_root_
     p_vout->i_gray_pixel  = RGB2PIXEL( p_vout, 128, 128, 128 );
     p_vout->i_blue_pixel  = RGB2PIXEL( p_vout, 0, 0, 50 );
 
-    /* Load fonts - fonts must be initialized after the systme method since
-     * they may be dependant of screen depth and other thread properties */
-    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
+    /* Load fonts - fonts must be initialized after the system method since
+     * they may be dependant on screen depth and other thread properties */
+    p_vout->p_default_font      = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
+    if( p_vout->p_default_font == NULL )
+    {
+        p_vout->p_default_font  = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
+    }
     if( p_vout->p_default_font == NULL )
     {
-        vout_SysDestroy( p_vout );
+        p_vout->p_sys_destroy( p_vout );
+        TrashPlugin( p_vout->vout_plugin );
         free( p_vout );
         return( NULL );
     }
-    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
+    p_vout->p_large_font        = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
+    if( p_vout->p_large_font == NULL )
+    {
+        p_vout->p_large_font    = vout_LoadFont( "share/" VOUT_LARGE_FONT );
+    }
     if( p_vout->p_large_font == NULL )
     {
         vout_UnloadFont( p_vout->p_default_font );
-        vout_SysDestroy( p_vout );
+        p_vout->p_sys_destroy( p_vout );
+        TrashPlugin( p_vout->vout_plugin );
         free( p_vout );
         return( NULL );
     }
@@ -185,7 +251,8 @@ vout_thread_t * vout_CreateThread               ( char *psz_display, int i_root_
         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
         vout_UnloadFont( p_vout->p_default_font );
         vout_UnloadFont( p_vout->p_large_font );
-       vout_SysDestroy( p_vout );
+        p_vout->p_sys_destroy( p_vout );
+        TrashPlugin( p_vout->vout_plugin );
         free( p_vout );
         return( NULL );
     }
@@ -296,36 +363,37 @@ subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
     /*
      * Look for an empty place
      */
-    for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
-    {
-       if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
-       {
-           /* Subpicture is marked for destruction, but is still allocated */
-           if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
-               (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
-           {
-               /* Memory size do match or is smaller : memory will not be reallocated,
-                 * and function can end immediately - this is the best possible case,
-                 * since no memory allocation needs to be done */
-               p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
+    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
+    {
+        if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
+        {
+            /* Subpicture is marked for destruction, but is still allocated */
+            if( (p_vout->p_subpicture[i_subpic].i_type  == i_type)   &&
+                (p_vout->p_subpicture[i_subpic].i_size  >= i_size) )
+            {
+                /* Memory size do match or is smaller : memory will not be
+                 * reallocated, and function can end immediately - this is
+                 * the best possible case, since no memory allocation needs
+                 * to be done */
+                p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
 #ifdef DEBUG_VIDEO
                 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
                             &p_vout->p_subpicture[i_subpic] );
 #endif
-               vlc_mutex_unlock( &p_vout->subpicture_lock );
-               return( &p_vout->p_subpicture[i_subpic] );
-           }
-           else if( p_destroyed_subpic == NULL )
-           {
-               /* Memory size do not match, but subpicture index will be kept in
-                * case no other place are left */
-               p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
-           }   
-       }
+                vlc_mutex_unlock( &p_vout->subpicture_lock );
+                return( &p_vout->p_subpicture[i_subpic] );
+            }
+            else if( p_destroyed_subpic == NULL )
+            {
+                /* Memory size do not match, but subpicture index will be kept in
+                 * case no other place are left */
+                p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
+            }
+        }
         else if( (p_free_subpic == NULL) &&
                  (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
         {
-           /* Subpicture is empty and ready for allocation */
+            /* Subpicture is empty and ready for allocation */
             p_free_subpic = &p_vout->p_subpicture[i_subpic];
         }
     }
@@ -333,8 +401,8 @@ subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
     /* If no free subpicture is available, use a destroyed subpicture */
     if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
     {
-       /* No free subpicture or matching destroyed subpicture has been found, but
-        * a destroyed subpicture is still avalaible */
+        /* No free subpicture or matching destroyed subpicture has been
+         * found, but a destroyed subpicture is still avalaible */
         free( p_destroyed_subpic->p_data );
         p_free_subpic = p_destroyed_subpic;
     }
@@ -350,6 +418,9 @@ subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
         case TEXT_SUBPICTURE:                             /* text subpicture */
             p_free_subpic->p_data = malloc( i_size + 1 );
             break;
+        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
+            p_free_subpic->p_data = malloc( i_size );
+            break;
 #ifdef DEBUG
         default:
             intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
@@ -359,7 +430,7 @@ subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
         }
 
         if( p_free_subpic->p_data != NULL )
-        {                    /* Copy subpicture informations, set some default values */
+        {           /* Copy subpicture informations, set some default values */
             p_free_subpic->i_type                      = i_type;
             p_free_subpic->i_status                    = RESERVED_SUBPICTURE;
             p_free_subpic->i_size                      = i_size;
@@ -494,7 +565,7 @@ void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
  * since several pictures can be created by several producers threads.
  *****************************************************************************/
 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
-                              int i_width, int i_height )
+                               int i_width, int i_height )
 {
     int         i_picture;                                  /* picture index */
     int         i_chroma_width = 0;                          /* chroma width */
@@ -509,37 +580,38 @@ picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
      */
     for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
     {
-       if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
-       {
-           /* Picture is marked for destruction, but is still allocated - note
+        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
+        {
+            /* Picture is marked for destruction, but is still allocated - note
              * that if width and type are the same for two pictures, chroma_width
              * should also be the same */
-           if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
-               (p_vout->p_picture[i_picture].i_height         == i_height) &&
-               (p_vout->p_picture[i_picture].i_width          == i_width) )
-           {
-               /* Memory size do match : memory will not be reallocated, and function
+            if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
+                (p_vout->p_picture[i_picture].i_height         == i_height) &&
+                (p_vout->p_picture[i_picture].i_width          == i_width) )
+            {
+                /* Memory size do match : memory will not be reallocated, and function
                  * can end immediately - this is the best possible case, since no
                  * memory allocation needs to be done */
-               p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
+                p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
+                p_vout->i_pictures++;
 #ifdef DEBUG_VIDEO
                 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
                             &p_vout->p_picture[i_picture] );
 #endif
-               vlc_mutex_unlock( &p_vout->picture_lock );
-               return( &p_vout->p_picture[i_picture] );
-           }
-           else if( p_destroyed_picture == NULL )
-           {
-               /* Memory size do not match, but picture index will be kept in
-                * case no other place are left */
-               p_destroyed_picture = &p_vout->p_picture[i_picture];
-           }   
-       }
+                vlc_mutex_unlock( &p_vout->picture_lock );
+                return( &p_vout->p_picture[i_picture] );
+            }
+            else if( p_destroyed_picture == NULL )
+            {
+                /* Memory size do not match, but picture index will be kept in
+                 * case no other place are left */
+                p_destroyed_picture = &p_vout->p_picture[i_picture];
+            }
+        }
         else if( (p_free_picture == NULL) &&
                  (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
         {
-           /* Picture is empty and ready for allocation */
+            /* Picture is empty and ready for allocation */
             p_free_picture = &p_vout->p_picture[i_picture];
         }
     }
@@ -547,8 +619,8 @@ picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
     /* If no free picture is available, use a destroyed picture */
     if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
     {
-       /* No free picture or matching destroyed picture has been found, but
-        * a destroyed picture is still avalaible */
+        /* No free picture or matching destroyed picture has been found, but
+         * a destroyed picture is still avalaible */
         free( p_destroyed_picture->p_data );
         p_free_picture = p_destroyed_picture;
     }
@@ -605,6 +677,7 @@ picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
             p_free_picture->i_display_height            = i_height;
             p_free_picture->i_aspect_ratio              = AR_SQUARE_PICTURE;
             p_free_picture->i_refcount                  = 0;
+            p_vout->i_pictures++;
         }
         else
         {
@@ -634,11 +707,11 @@ picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
  * This function frees a previously reserved picture or a permanent
  * picture. It is meant to be used when the construction of a picture aborted.
  * Note that the picture will be destroyed even if it is linked !
- * This function does not need locking since reserved pictures are ignored by
- * the output thread.
  *****************************************************************************/
 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
+   vlc_mutex_lock( &p_vout->picture_lock );
+
 #ifdef DEBUG
    /* Check if picture status is valid */
    if( (p_pic->i_status != RESERVED_PICTURE) &&
@@ -649,11 +722,13 @@ void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
    }
 #endif
 
-    p_pic->i_status = DESTROYED_PICTURE;
+   p_pic->i_status = DESTROYED_PICTURE;
+   p_vout->i_pictures--;
 
 #ifdef DEBUG_VIDEO
-    intf_DbgMsg("picture %p\n", p_pic);
+   intf_DbgMsg("picture %p\n", p_pic);
 #endif
+   vlc_mutex_unlock( &p_vout->picture_lock );
 }
 
 /*****************************************************************************
@@ -694,7 +769,8 @@ void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
 
     if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
     {
-       p_pic->i_status = DESTROYED_PICTURE;
+        p_pic->i_status = DESTROYED_PICTURE;
+        p_vout->i_pictures--;
     }
 
 #ifdef DEBUG_VIDEO
@@ -783,7 +859,7 @@ static int BinaryLog(u32 i)
     }
     if (i != ((u32)1 << i_log))
     {
-       intf_ErrMsg("internal error: binary log overflow\n");
+        intf_ErrMsg("internal error: binary log overflow\n");
     }
 
     return( i_log );
@@ -825,7 +901,7 @@ static int InitThread( vout_thread_t *p_vout )
     *p_vout->pi_status = THREAD_START;
 
    /* Initialize output method - this function issues its own error messages */
-    if( vout_SysInit( p_vout ) )
+    if( p_vout->p_sys_init( p_vout ) )
     {
         return( 1 );
     }
@@ -853,6 +929,10 @@ static int InitThread( vout_thread_t *p_vout )
  *****************************************************************************/
 static void RunThread( vout_thread_t *p_vout)
 {
+    /* XXX?? welcome to gore land */
+    static int i_trash_count = 0;
+    static mtime_t last_display_date = 0;
+
     int             i_index;                                /* index in heap */
     mtime_t         current_date;                            /* current date */
     mtime_t         display_date;                            /* display date */
@@ -884,53 +964,90 @@ static void RunThread( vout_thread_t *p_vout)
         current_date =  mdate();
 
         /*
-        * Find the picture to display - this operation does not need lock,
+         * Find the picture to display - this operation does not need lock,
          * since only READY_PICTUREs are handled
          */
         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
-       {
-           if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
-               ( (p_pic == NULL) ||
-                 (p_vout->p_picture[i_index].date < display_date) ) )
-           {
+        {
+            if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
+            ( (p_pic == NULL) ||
+              (p_vout->p_picture[i_index].date < display_date) ) )
+            {
                 p_pic = &p_vout->p_picture[i_index];
                 display_date = p_pic->date;
-           }
-       }
+            }
+        }
 
         if( p_pic )
         {
 #ifdef STATS
             /* Computes FPS rate */
             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
-#endif 
-           if( display_date < current_date )
-           {
-               /* Picture is late: it will be destroyed and the thread will sleep and
-                 * go to next picture */
+#endif
+/* XXX?? */
+i_trash_count++;
+//fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
+last_display_date = display_date;
+#if 1
+            if( display_date < current_date && i_trash_count > 4 )
+            {
+                /* Picture is late: it will be destroyed and the thread
+                 * will sleep and go to next picture */
+
                 vlc_mutex_lock( &p_vout->picture_lock );
-                p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
-               intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
+                if( p_pic->i_refcount )
+                {
+                    p_pic->i_status = DISPLAYED_PICTURE;
+                }
+                else
+                {
+                    p_pic->i_status = DESTROYED_PICTURE;
+                    p_vout->i_pictures--;
+                }
+                intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
                 vlc_mutex_unlock( &p_vout->picture_lock );
+
+                /* Update synchronization information as if display delay
+                 * was 0 */
+                Synchronize( p_vout, display_date - current_date );
+
                 p_pic =         NULL;
                 display_date =  0;
-           }
-           else if( display_date > current_date + VOUT_DISPLAY_DELAY )
-           {
-               /* A picture is ready to be rendered, but its rendering date is
-                * far from the current one so the thread will perform an empty loop
-                * as if no picture were found. The picture state is unchanged */
+                i_trash_count = 0;
+            }
+            else
+#endif
+                if( display_date > current_date + VOUT_DISPLAY_DELAY )
+            {
+                /* A picture is ready to be rendered, but its rendering date
+                 * is far from the current one so the thread will perform an
+                 * empty loop as if no picture were found. The picture state
+                 * is unchanged */
                 p_pic =         NULL;
                 display_date =  0;
-           }
+            }
+            else
+            {
+                /* Picture will be displayed, update synchronization
+                 * information */
+                Synchronize( p_vout, display_date - current_date );
+            }
         }
-
         /*
-         * Find the subpicture to display - this operation does not need lock, since
-         * only READY_SUBPICTURES are handled. If no picture has been selected,
-         * display_date will depend on the subpicture
+         * Find the subpictures to display - this operation does not need
+         * lock, since only READY_SUBPICTURE are handled. If no picture
+         * has been selected, display_date will depend on the subpicture.
+         * We get an easily parsable chained list of subpictures which
+         * ends with NULL since p_subpic was initialized to NULL.
          */
-        //??
+        for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
+        {
+            if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
+            {
+                p_vout->p_subpicture[i_index].p_next = p_subpic;
+                p_subpic = &p_vout->p_subpicture[i_index];
+            }
+        }
 
         /*
          * Perform rendering, sleep and display rendered picture
@@ -952,62 +1069,35 @@ static void RunThread( vout_thread_t *p_vout)
                     RenderPictureInfo( p_vout, p_pic );
                     RenderInfo( p_vout );
                 }
+                if( p_subpic )
+                {
+                    RenderSubPicture( p_vout, p_subpic );
+                }
             }
 
             /* Remove picture from heap */
             vlc_mutex_lock( &p_vout->picture_lock );
-            p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
-            vlc_mutex_unlock( &p_vout->picture_lock );
-
-            /* Render interface and subpicture */
-            if( b_display && p_vout->b_interface )
+            if( p_pic->i_refcount )
             {
-                RenderInterface( p_vout );
+                p_pic->i_status = DISPLAYED_PICTURE;
             }
-            if( p_subpic )
+            else
             {
-                if( b_display )
-                {
-                    RenderSubPicture( p_vout, p_subpic );
-                }
-
-                /* Remove subpicture from heap */
-                vlc_mutex_lock( &p_vout->subpicture_lock );
-                p_subpic->i_status = DESTROYED_SUBPICTURE;
-                vlc_mutex_unlock( &p_vout->subpicture_lock );
+                p_pic->i_status = DESTROYED_PICTURE;
+                p_vout->i_pictures--;
             }
+            vlc_mutex_unlock( &p_vout->picture_lock );
 
-        }
-        else if( p_subpic )                              /* subpicture alone */
-        {
-            b_display = p_vout->b_active;
-            p_vout->last_display_date = display_date;
-
-            if( b_display )
+            /* Render interface and subpicture */
+            if( b_display && p_vout->b_interface )
             {
-                /* Clear buffer */
-                SetBufferPicture( p_vout, NULL );
-
-                /* Render informations, interface and subpicture */
-                if( p_vout->b_info )
-                {
-                    RenderInfo( p_vout );
-                }
-                if( p_vout->b_interface )
-                {
-                    RenderInterface( p_vout );
-                }
-                RenderSubPicture( p_vout, p_subpic );
+                RenderInterface( p_vout );
             }
 
-            /* Remove subpicture from heap */
-            vlc_mutex_lock( &p_vout->subpicture_lock );
-            p_subpic->i_status = DESTROYED_SUBPICTURE;
-            vlc_mutex_unlock( &p_vout->subpicture_lock );
         }
         else if( p_vout->b_active )        /* idle or interface screen alone */
         {
-            if( p_vout->b_interface && 0 /* && ?? intf_change */ )
+            if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
             {
                 /* Interface has changed, so a new rendering is required - force
                  * it by setting last idle date to 0 */
@@ -1063,20 +1153,22 @@ static void RunThread( vout_thread_t *p_vout)
 #endif
         if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
         {
-            vout_SysDisplay( p_vout );
+            p_vout->p_sys_display( p_vout );
+#ifndef SYS_BEOS
             p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
+#endif
         }
 
         /*
          * Check events and manage thread
-        */
-        if( vout_SysManage( p_vout ) | Manage( p_vout ) )
-       {
-           /* A fatal error occured, and the thread must terminate immediately,
-            * without displaying anything - setting b_error to 1 cause the
-            * immediate end of the main while() loop. */
-           p_vout->b_error = 1;
-       }
+         */
+        if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
+        {
+            /* A fatal error occured, and the thread must terminate immediately,
+             * without displaying anything - setting b_error to 1 cause the
+             * immediate end of the main while() loop. */
+            p_vout->b_error = 1;
+        }
     }
 
     /*
@@ -1114,7 +1206,7 @@ static void ErrorThread( vout_thread_t *p_vout )
 /*****************************************************************************
  * EndThread: thread destruction
  *****************************************************************************
- * This function is called when the thread ends after a sucessfull
+ * This function is called when the thread ends after a sucessful
  * initialization. It frees all ressources allocated by InitThread.
  *****************************************************************************/
 static void EndThread( vout_thread_t *p_vout )
@@ -1128,10 +1220,13 @@ static void EndThread( vout_thread_t *p_vout )
     /* Destroy all remaining pictures and subpictures */
     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
     {
-       if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
-       {
+        if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
+        {
             free( p_vout->p_picture[i_index].p_data );
         }
+    }
+    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
+    {
         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
         {
             free( p_vout->p_subpicture[i_index].p_data );
@@ -1140,7 +1235,7 @@ static void EndThread( vout_thread_t *p_vout )
 
     /* Destroy translation tables */
     vout_EndYUV( p_vout );
-    vout_SysEnd( p_vout );
+    p_vout->p_sys_end( p_vout );
 }
 
 /*****************************************************************************
@@ -1160,7 +1255,12 @@ static void DestroyThread( vout_thread_t *p_vout, int i_status )
     /* Destroy thread structures allocated by Create and InitThread */
     vout_UnloadFont( p_vout->p_default_font );
     vout_UnloadFont( p_vout->p_large_font );
-    vout_SysDestroy( p_vout );
+    p_vout->p_sys_destroy( p_vout );
+
+    /* Close plugin */
+    TrashPlugin( p_vout->vout_plugin );
+
+    /* Free structure */
     free( p_vout );
     *pi_status = i_status;
 }
@@ -1429,7 +1529,7 @@ static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
     }
 
     /*
-     * Set new picture size - if is is smaller than the previous one, clear
+     * Set new picture size - if it is smaller than the previous one, clear
      * around it. Since picture are centered, only their size is tested.
      */
     if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
@@ -1512,8 +1612,8 @@ static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
     /* Get and set rendering informations */
     p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
     p_pic_data =        p_buffer->p_data +
-        p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
-        p_buffer->i_pic_y * p_vout->i_bytes_per_line;
+                        p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
+                        p_buffer->i_pic_y * p_vout->i_bytes_per_line;
 #ifdef DEBUG_VIDEO
     render_time = mdate();
 #endif
@@ -1690,8 +1790,8 @@ static void RenderInfo( vout_thread_t *p_vout )
             break;
         }
     }
-    sprintf( psz_buffer, "pic: %d/%d/%d",
-             i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
+    sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
+             p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
     Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
 #endif
 }
@@ -1706,36 +1806,65 @@ static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
     p_vout_font_t       p_font;                                 /* text font */
     int                 i_width, i_height;          /* subpicture dimensions */
 
-    switch( p_subpic->i_type )
+    while( p_subpic != NULL )
     {
-    case TEXT_SUBPICTURE:                                /* single line text */
-        /* Select default font if not specified */
-        p_font = p_subpic->type.text.p_font;
-        if( p_font == NULL )
+        switch( p_subpic->i_type )
         {
-            p_font = p_vout->p_default_font;
-        }
+        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
+            /* test if the picture really has to be displayed */
+            if( mdate() < p_subpic->begin_date )
+            {
+                /* not yet, see you later */
+                break;
+            }
+            if( mdate() > p_subpic->end_date )
+            {
+                /* too late, destroying the subpic */
+                vout_DestroySubPicture( p_vout, p_subpic );
+                break;
+            }
+            vout_RenderSPU( &p_vout->p_buffer[ p_vout->i_buffer_index ],
+                            p_subpic, p_vout->i_bytes_per_pixel,
+                            p_vout->i_bytes_per_line );
+            break;
+        case TEXT_SUBPICTURE:                            /* single line text */
+            /* Select default font if not specified */
+            p_font = p_subpic->type.text.p_font;
+            if( p_font == NULL )
+            {
+                p_font = p_vout->p_default_font;
+            }
 
-        /* Computes text size (width and height fields are ignored) and print it */
-        vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
-        if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
-                    p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
-        {
-            vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
-                        p_subpic->i_x * p_vout->i_bytes_per_pixel +
-                        p_subpic->i_y * p_vout->i_bytes_per_line,
-                        p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
-                        p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
-                        p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
-                        p_subpic->p_data );
-            SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
-        }
-        break;
+            /* Compute text size (width and height fields are ignored)
+             * and print it */
+            vout_TextSize( p_font, p_subpic->type.text.i_style,
+                           p_subpic->p_data, &i_width, &i_height );
+            if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
+                        i_width, i_height, p_subpic->i_horizontal_align,
+                        p_subpic->i_vertical_align ) )
+            {
+                vout_Print( p_font,
+                            p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
+                            p_subpic->i_x * p_vout->i_bytes_per_pixel +
+                            p_subpic->i_y * p_vout->i_bytes_per_line,
+                            p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
+                            p_subpic->type.text.i_char_color,
+                            p_subpic->type.text.i_border_color,
+                            p_subpic->type.text.i_bg_color,
+                            p_subpic->type.text.i_style, p_subpic->p_data );
+                SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
+                               i_width, i_height );
+            }
+            break;
 
 #ifdef DEBUG
-    default:
-        intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
+        default:
+            intf_DbgMsg( "error: unknown subpicture %p type %d\n",
+                         p_subpic, p_subpic->i_type );
 #endif
+        }
+
+        p_subpic = p_subpic->p_next;
     }
 }
 
@@ -1762,7 +1891,7 @@ static void RenderInterface( vout_thread_t *p_vout )
          i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
          i_byte++ )
     {
-        //?? noooo !
+        /* XXX?? noooo ! */
         p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
     }
 
@@ -1788,6 +1917,81 @@ static void RenderInterface( vout_thread_t *p_vout )
     SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
 }
 
+/*****************************************************************************
+ * Synchronize: update synchro level depending of heap state
+ *****************************************************************************
+ * This function is called during the main vout loop.
+ *****************************************************************************/
+static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
+{
+    int i_synchro_inc = 0;
+    /* XXX?? gore following */
+    static int i_panic_count = 0;
+    static int i_last_synchro_inc = 0;
+    static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
+    static int i_truc = 10;
+
+    if( i_delay < 0 )
+    {
+        //fprintf( stderr, "PANIC %d\n", i_panic_count );
+        i_panic_count++;
+    }
+
+    i_truc *= 2;
+
+    if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
+    {
+        i_truc = 40;
+        i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
+
+    }
+    else
+    {
+        if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
+        {
+            i_truc = 32;
+            i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
+        }
+    }
+
+    if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
+        i_synchro_inc*i_last_synchro_inc < 0 )
+    {
+        i_truc = 32;
+    }
+
+    if( i_delay < 6000 )
+    {
+        i_truc = 16;
+        i_synchro_inc -= 2;
+    }
+    else if( i_delay < 70000 )
+    {
+        i_truc = 24+(24*i_delay)/70000;
+        if( i_truc < 16 )
+            i_truc = 16;
+        i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
+    }
+    else if( i_delay > 100000 )
+    {
+        r_synchro_level += 1;
+        if( i_delay > 130000 )
+            r_synchro_level += 1;
+    }
+
+    r_synchro_level += (float)i_synchro_inc / i_truc;
+    p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
+
+    if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
+    {
+        r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
+    }
+
+    //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
+    //        p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
+    i_last_synchro_inc = i_synchro_inc;
+}
+
 /*****************************************************************************
  * Manage: manage thread
  *****************************************************************************
@@ -1823,8 +2027,8 @@ static int Manage( vout_thread_t *p_vout )
     /* Detect unauthorized changes */
     if( p_vout->i_changes )
     {
-        /* Some changes were not acknowledged by vout_SysManage or this function,
-         * it means they should not be authorized */
+        /* Some changes were not acknowledged by p_vout->p_sys_manage or this
+         * function, it means they should not be authorized */
         intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
         return( 1 );
     }