]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_output.c
- �a compile sous FreeBSD (mais �a ne tourne pas)
[vlc] / src / video_output / video_output.c
index 376652aef3d6bb0a2bc358e449aade2f36226f2b..0341868ebe59e6d2912b21df1e9231847be3a63d 100644 (file)
-/*******************************************************************************
+/*****************************************************************************
  * video_output.c : video output thread
- * (c)1999 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-1307, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
  * Preamble
- *******************************************************************************/
-#include <errno.h> 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-#include <X11/extensions/XShm.h>
+ *****************************************************************************/
+#include <errno.h>                                                 /* ENOMEM */
+#include <stdlib.h>                                                /* free() */
+#include <stdio.h>                                              /* sprintf() */
+#include <string.h>                                            /* strerror() */
 
 #include "common.h"
 #include "config.h"
 #include "mtime.h"
-#include "vlc_thread.h"
-#include "thread.h"
-
+#include "threads.h"
+#include "plugins.h"
 #include "video.h"
-#include "video_graphics.h"
 #include "video_output.h"
-#include "video_x11.h"
+#include "video_text.h"
+#include "video_yuv.h"
 
 #include "intf_msg.h"
+#include "main.h"
 
-/*
+/*****************************************************************************
  * Local prototypes
- */
-static int      CheckConfiguration  ( video_cfg_t *p_cfg );
-static int      InitThread          ( vout_thread_t *p_vout );
-static void     RunThread           ( vout_thread_t *p_vout );
-static void     ErrorThread         ( vout_thread_t *p_vout );
-static void     EndThread           ( vout_thread_t *p_vout );
-
-static picture_t *  FindPicture     ( vout_thread_t *p_vout );
-static void         EmptyPicture    ( vout_thread_t *p_vout, picture_t *p_pic );
-static mtime_t      FirstPass       ( vout_thread_t *p_vout, mtime_t current_date );
-static void         SecondPass      ( vout_thread_t *p_vout, mtime_t display_date );
-static void         SortPictures    ( vout_thread_t *p_vout, picture_t **pp_picture );
-static void         RenderPicture   ( vout_thread_t *p_vout, picture_t *p_pic );
-static void         ClipPicture     ( int *pi_ofs, int i_size, int *pi_pic_ofs, int *pi_pic_size,
-                                      int i_ratio, int i_placement );
-
-/*******************************************************************************
+ *****************************************************************************/
+static int      BinaryLog         ( u32 i );
+static void     MaskToShift       ( int *pi_left, int *pi_right, u32 i_mask );
+static int      InitThread        ( vout_thread_t *p_vout );
+static void     RunThread         ( vout_thread_t *p_vout );
+static void     ErrorThread       ( vout_thread_t *p_vout );
+static void     EndThread         ( vout_thread_t *p_vout );
+static void     DestroyThread     ( vout_thread_t *p_vout, int i_status );
+static void     Print             ( vout_thread_t *p_vout, int i_x, int i_y,
+                                    int i_h_align, int i_v_align,
+                                    unsigned char *psz_text );
+static void     SetBufferArea     ( vout_thread_t *p_vout, int i_x, int i_y,
+                                    int i_w, int i_h );
+static void     SetBufferPicture  ( vout_thread_t *p_vout, picture_t *p_pic );
+static void     RenderPicture     ( vout_thread_t *p_vout, picture_t *p_pic );
+static void     RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
+static void     RenderSubPicture  ( vout_thread_t *p_vout,
+                                    subpicture_t *p_subpic );
+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,
+                                    int i_h_align, int i_v_align );
+static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
+                                    u16 *green, u16 *blue, u16 *transp );
+
+/*****************************************************************************
  * vout_CreateThread: creates a new video output thread
- *******************************************************************************
+ *****************************************************************************
  * This function creates a new video output thread, and returns a pointer
  * to its description. On error, it returns NULL.
- * Following configuration properties are used:
- *  VIDEO_CFG_SIZE      video heap maximal size
- *  VIDEO_CFG_WIDTH     window width
- *  VIDEO_CFG_HEIGHT    window height 
- * Using X11 display method (the only one supported yet):
- *  VIDEO_CFG_DISPLAY   display used
- *  VIDEO_CFG_TITLE     window title
- *  VIDEO_CFG_SHM_EXT   try to use XShm extension
  * If pi_status is NULL, then the function will block until the thread is ready.
- * If not, pi_error will be updated using one of the THREAD_* constants.
- *******************************************************************************/
-vout_thread_t *vout_CreateThread( video_cfg_t *p_cfg, int *pi_status )
+ * 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, int i_method )
 {
-    vout_thread_t * p_vout;                               /* thread descriptor */
-    int             i_status;                                 /* thread status */
+    vout_thread_t * p_vout;                             /* thread descriptor */
+    int             i_status;                               /* thread status */
+    int             i_index;               /* index for array initialization */
+    char *          psz_method;
 
-    /*
-     * Check configuration 
-     */
-    if( CheckConfiguration( p_cfg ) )
+    /* 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) );
         return( NULL );
     }
 
-    /* Allocate descriptor and initialize flags */
-    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
-    if( p_vout == NULL )                                              /* error */
+    /* Request an interface plugin */
+    psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
+    p_vout->p_vout_plugin = RequestPlugin( "vout", psz_method );
+
+    if( !p_vout->p_vout_plugin )
     {
+        intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
+        free( p_vout );
         return( NULL );
     }
-    if( vout_X11AllocOutputMethod( p_vout, p_cfg ) )
+
+    /* Get plugins */
+    p_vout->p_sys_create =  GetPluginFunction( p_vout->p_vout_plugin, "vout_SysCreate" );
+    p_vout->p_sys_init =    GetPluginFunction( p_vout->p_vout_plugin, "vout_SysInit" );
+    p_vout->p_sys_end =     GetPluginFunction( p_vout->p_vout_plugin, "vout_SysEnd" );
+    p_vout->p_sys_destroy = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysDestroy" );
+    p_vout->p_sys_manage =  GetPluginFunction( p_vout->p_vout_plugin, "vout_SysManage" );
+    p_vout->p_sys_display = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysDisplay" );
+
+    /* Initialize thread properties - thread id and locks will be initialized
+     * later */
+    p_vout->b_die               = 0;
+    p_vout->b_error             = 0;
+    p_vout->b_active            = 0;
+    p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
+    *p_vout->pi_status          = THREAD_CREATE;
+
+    /* Initialize some fields used by the system-dependant method - these fields will
+     * probably be modified by the method, and are only preferences */
+    p_vout->i_changes           = 0;
+    p_vout->i_width             = i_width;
+    p_vout->i_height            = i_height;
+    p_vout->i_bytes_per_line    = i_width * 2;
+    p_vout->i_screen_depth      = 15;
+    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_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 );
+
+    /* Initialize idle screen */
+    p_vout->last_display_date   = mdate();
+    p_vout->last_display_date   = 0;
+    p_vout->last_idle_date      = 0;
+
+#ifdef STATS
+    /* Initialize statistics fields */
+    p_vout->render_time         = 0;
+    p_vout->c_fps_samples       = 0;
+#endif
+
+    /* Initialize buffer index */
+    p_vout->i_buffer_index      = 0;
+
+    /* Initialize pictures and subpictures - translation tables and functions
+     * will be initialized later in InitThread */
+    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
     {
-        free( p_vout );
-        return( NULL );        
+        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
+        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
+        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;
 
-    /* Copy configuration */
-    p_vout->i_max_pictures = p_cfg->i_size;    
-    p_vout->i_width = p_cfg->i_width;
-    p_vout->i_height = p_cfg->i_height;
-   
-    /* Set status */
-    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
-    *p_vout->pi_status = THREAD_CREATE;
-    
-    /* Initialize flags */
-    p_vout->b_die = 0;
-    p_vout->b_error = 0;    
-    p_vout->b_active = 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( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
+    {
+        TrashPlugin( p_vout->p_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,
+                p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
+                p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
+
+    /* Calculate shifts from system-updated masks */
+    MaskToShift( &p_vout->i_red_lshift,   &p_vout->i_red_rshift,   p_vout->i_red_mask );
+    MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
+    MaskToShift( &p_vout->i_blue_lshift,  &p_vout->i_blue_rshift,  p_vout->i_blue_mask );
+
+    /* Set some useful colors */
+    p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
+    p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
+    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 system method since
+     * they may be dependant on screen depth and other thread properties */
+    p_vout->p_default_font      = vout_LoadFont( VOUT_DEFAULT_FONT );
+    if( p_vout->p_default_font == NULL )
+    {
+        p_vout->p_sys_destroy( p_vout );
+        TrashPlugin( p_vout->p_vout_plugin );
+        free( p_vout );
+        return( NULL );
+    }
+    p_vout->p_large_font        = vout_LoadFont( VOUT_LARGE_FONT );
+    if( p_vout->p_large_font == NULL )
+    {
+        vout_UnloadFont( p_vout->p_default_font );
+        p_vout->p_sys_destroy( p_vout );
+        TrashPlugin( p_vout->p_vout_plugin );
+        free( p_vout );
+        return( NULL );
+    }
 
     /* Create thread and set locks */
-    vlc_mutex_init( &p_vout->streams_lock );
-    vlc_mutex_init( &p_vout->pictures_lock );
-    if( vlc_thread_create( &p_vout->thread_id, "video output", (vlc_thread_func)RunThread, (void *) p_vout) )
+    vlc_mutex_init( &p_vout->picture_lock );
+    vlc_mutex_init( &p_vout->subpicture_lock );
+    vlc_mutex_init( &p_vout->change_lock );
+    vlc_mutex_lock( &p_vout->change_lock );
+    if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
     {
-        intf_ErrMsg("vout error: %s\n", strerror(ENOMEM));
-        intf_DbgMsg("failed\n");        
-        vout_X11FreeOutputMethod( p_vout );        
+        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
+        vout_UnloadFont( p_vout->p_default_font );
+        vout_UnloadFont( p_vout->p_large_font );
+        p_vout->p_sys_destroy( p_vout );
+        TrashPlugin( p_vout->p_vout_plugin );
         free( p_vout );
         return( NULL );
-    }   
+    }
+
+    intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
+             p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
 
     /* If status is NULL, wait until the thread is created */
     if( pi_status == NULL )
     {
         do
-        {            
+        {
             msleep( THREAD_SLEEP );
-        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR) 
+        }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
                 && (i_status != THREAD_FATAL) );
         if( i_status != THREAD_READY )
         {
-            intf_DbgMsg("failed\n");            
-            return( NULL );            
-        }        
+            return( NULL );
+        }
     }
-
-    intf_DbgMsg("succeeded -> %p\n", p_vout);    
     return( p_vout );
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * vout_DestroyThread: destroys a previously created thread
- *******************************************************************************
- * Destroy a terminated thread. 
+ *****************************************************************************
+ * Destroy a terminated thread.
  * The function will request a destruction of the specified thread. If pi_error
- * is NULL, it will return once the thread is destroyed. Else, it will be 
+ * is NULL, it will return once the thread is destroyed. Else, it will be
  * update using one of the THREAD_* constants.
- *******************************************************************************/
+ *****************************************************************************/
 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
-{  
-    int     i_status;                                         /* thread status */
+{
+    int     i_status;                                       /* thread status */
 
     /* Set status */
+    intf_DbgMsg("\n");
     p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
     *p_vout->pi_status = THREAD_DESTROY;
-     
+
     /* Request thread destruction */
     p_vout->b_die = 1;
 
     /* If status is NULL, wait until thread has been destroyed */
-    if( pi_status )
+    if( pi_status == NULL )
     {
         do
         {
             msleep( THREAD_SLEEP );
-        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR) 
-                && (i_status != THREAD_FATAL) );   
+        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
+                && (i_status != THREAD_FATAL) );
     }
-    
-    intf_DbgMsg("%p -> succeeded\n", p_vout);
 }
 
-/*******************************************************************************
- * vout_DisplayPicture: add a picture to a thread
- *******************************************************************************
- * The picture will be placed in an empty place of the video heap of the thread.
- * If this is impossible, NULL will be returned. Else, the original picture
- * is destroyed.
- *******************************************************************************/
-picture_t * vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
+/*****************************************************************************
+ * vout_DisplaySubPicture: display a subpicture unit
+ *****************************************************************************
+ * Remove the reservation flag of an subpicture, which will cause it to be ready
+ * for display. The picture does not need to be locked, since it is ignored by
+ * the output thread if is reserved.
+ *****************************************************************************/
+void  vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
 {
-    picture_t * p_newpic;                                 /* new picture index */
-
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Copy picture information */
-        video_CopyPictureDescriptor( p_newpic, p_pic );
-        p_newpic->p_data = p_pic->p_data;
-        free( p_pic );
-
-        /* Update heap size and stats */
-        p_vout->i_pictures++;
-#ifdef STATS
-        p_vout->c_pictures++;
-        p_vout->p_stream[ p_newpic->i_stream ].c_pictures++;
+#ifdef DEBUG_VIDEO
+    char        psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
+    char        psz_end_date[MSTRTIME_MAX_SIZE];   /* buffer for date string */
 #endif
+
+#ifdef DEBUG
+    /* Check if status is valid */
+    if( p_subpic->i_status != RESERVED_SUBPICTURE )
+    {
+        intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
+                    p_subpic->i_status );
     }
+#endif
 
-    /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
+    /* Remove reservation flag */
+    p_subpic->i_status = READY_SUBPICTURE;
+
+#ifdef DEBUG_VIDEO
+    /* Send subpicture informations */
+    intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
+                p_subpic, p_subpic->i_type,
+                mstrtime( psz_begin_date, p_subpic->begin_date ),
+                mstrtime( psz_end_date, p_subpic->end_date ) );
+#endif
 }
 
-/*******************************************************************************
- * vout_DisplayPictureCopy: copy a picture to a thread
- *******************************************************************************
- * The picture will be copied in an empty place of the video heap of the thread.
- * If this is impossible, NULL will be returned. The picture used is the copy
- * of the picture passed as argument, which remains usable.
- * The picture data are copied only if the original picture owns its own data.
- * The link reference counter is set to 0.
- *******************************************************************************/
-picture_t * vout_DisplayPictureCopy( vout_thread_t *p_vout, picture_t *p_pic )
+/*****************************************************************************
+ * vout_CreateSubPicture: allocate an subpicture in the video output heap.
+ *****************************************************************************
+ * This function create a reserved subpicture in the video output heap.
+ * A null pointer is returned if the function fails. This method provides an
+ * already allocated zone of memory in the spu data fields. It needs locking
+ * since several pictures can be created by several producers threads.
+ *****************************************************************************/
+subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
+                                     int i_size )
 {
-    picture_t * p_newpic;                                 /* new picture index */
-
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Copy picture information */
-        video_CopyPictureDescriptor( p_newpic, p_pic );
-
-        /* If source picture owns its data, make a copy */
-        if( p_pic->i_flags & OWNER_PICTURE )
-        {      
-            p_newpic->p_data = malloc( p_pic->i_height * p_pic->i_bytes_per_line );
-            if( p_newpic->p_data == NULL )                            /* error */
-            {
-                intf_ErrMsg("vout error: %s\n", strerror(ENOMEM) );
+    int                 i_subpic;                        /* subpicture index */
+    subpicture_t *      p_free_subpic = NULL;       /* first free subpicture */
+    subpicture_t *      p_destroyed_subpic = NULL; /* first destroyed subpic */
+
+    /* Get lock */
+    vlc_mutex_lock( &p_vout->subpicture_lock );
 
-                /* Restore type and flags */
-                p_newpic->i_type = EMPTY_PICTURE;
-                p_newpic->i_flags = 0;
-                vlc_mutex_unlock( &p_vout->pictures_lock );            
-                return( NULL );
+    /*
+     * 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;
+#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];
             }
-            memcpy( p_newpic->p_data, p_pic->p_data,
-                    p_pic->i_height * p_pic->i_bytes_per_line );
         }
-        /* If source picture does not owns its data, copy the reference */
-        else              
+        else if( (p_free_subpic == NULL) &&
+                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
         {
-            p_newpic->p_data = p_pic->p_data;
+            /* Subpicture is empty and ready for allocation */
+            p_free_subpic = &p_vout->p_subpicture[i_subpic];
         }
-        p_newpic->i_refcount = 0;
-            
-        /* Update heap size and stats */
-        p_vout->i_pictures++;
-#ifdef STATS
-        p_vout->c_pictures++;
-        p_vout->p_stream[ p_newpic->i_stream ].c_pictures++;
-#endif
     }
 
-    /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
-}
+    /* 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 */
+        free( p_destroyed_subpic->p_data );
+        p_free_subpic = p_destroyed_subpic;
+    }
 
-/*******************************************************************************
- * vout_DisplayPictureReplicate: copy a picture to a thread
- *******************************************************************************
- * The picture will be copied in an empty place of the video heap of the thread.
- * If this is impossible, NULL will be returned. The picture used is the copy
- * of the picture passed as argument, which remains usable.
- * The picture data are a reference to original picture (the picture in the heap
- * does not owns its data). Link reference counter is set to 0.
- *******************************************************************************/
-picture_t * vout_DisplayPictureReplicate( vout_thread_t *p_vout, picture_t *p_pic )
-{
-    picture_t * p_newpic;                            /* new picture descrpitor */
-
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Copy picture information */
-        video_CopyPictureDescriptor( p_newpic, p_pic );   
-        p_newpic->i_flags &= ~OWNER_PICTURE;
-        p_newpic->p_data = p_pic->p_data;
-        p_newpic->i_refcount = 0;
-            
-        /* Update heap size and stats */
-        p_vout->i_pictures++;
-#ifdef STATS
-        p_vout->c_pictures++;
-        p_vout->p_stream[ p_newpic->i_stream ].c_pictures++;
+    /*
+     * Prepare subpicture
+     */
+    if( p_free_subpic != NULL )
+    {
+        /* Allocate memory */
+        switch( i_type )
+        {
+        case TEXT_SUBPICTURE:                             /* text subpicture */
+            p_free_subpic->p_data = malloc( i_size + 1 );
+            break;
+#ifdef DEBUG
+        default:
+            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
+            p_free_subpic->p_data   =  NULL;
+            break;
+#endif
+        }
+
+        if( p_free_subpic->p_data != NULL )
+        {                    /* 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;
+            p_free_subpic->i_x                         = 0;
+            p_free_subpic->i_y                         = 0;
+            p_free_subpic->i_width                     = 0;
+            p_free_subpic->i_height                    = 0;
+            p_free_subpic->i_horizontal_align          = CENTER_RALIGN;
+            p_free_subpic->i_vertical_align            = CENTER_RALIGN;
+        }
+        else
+        {
+            /* Memory allocation failed : set subpicture as empty */
+            p_free_subpic->i_type   =  EMPTY_SUBPICTURE;
+            p_free_subpic->i_status =  FREE_SUBPICTURE;
+            p_free_subpic =            NULL;
+            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
+        }
+
+#ifdef DEBUG_VIDEO
+        intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
 #endif
+        vlc_mutex_unlock( &p_vout->subpicture_lock );
+        return( p_free_subpic );
     }
 
-   /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
+    /* No free or destroyed subpicture could be found */
+    intf_DbgMsg( "warning: heap is full\n" );
+    vlc_mutex_unlock( &p_vout->subpicture_lock );
+    return( NULL );
 }
 
-/*******************************************************************************
- * vout_DisplayReservedPicture: add a reserved picture to a thread
- *******************************************************************************
- * The picture will be placed in an empty place of the video heap of the thread.
- * If the picture has been declared on reservation as permanent, it remains
- * usable. Else, it will be destroyed by this call.
- * This function can't fail, since it is the purpose of picture reservation to
- * provide such a robust mechanism.
- *******************************************************************************/
-picture_t * vout_DisplayReservedPicture( vout_thread_t *p_vout, picture_t *p_pic )
+/*****************************************************************************
+ * vout_DestroySubPicture: remove a subpicture from the heap
+ *****************************************************************************
+ * This function frees a previously reserved subpicture.
+ * It is meant to be used when the construction of a picture aborted.
+ * This function does not need locking since reserved subpictures are ignored
+ * by the output thread.
+ *****************************************************************************/
+void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
 {
-    vlc_mutex_lock( &p_vout->pictures_lock );
+#ifdef DEBUG
+   /* Check if status is valid */
+   if( p_subpic->i_status != RESERVED_SUBPICTURE )
+   {
+       intf_DbgMsg("error: subpicture %p has invalid status %d\n",
+                   p_subpic, p_subpic->i_status );
+   }
+#endif
 
-    /* Remove reservation flag */
-    p_pic->i_flags &= ~RESERVED_PICTURE;
+    p_subpic->i_status = DESTROYED_SUBPICTURE;
 
-#ifdef STATS
-    /* Update stats */
-    p_vout->c_pictures++;
-    p_vout->p_stream[ p_pic->i_stream ].c_pictures++;
+#ifdef DEBUG_VIDEO
+    intf_DbgMsg("subpicture %p\n", p_subpic);
 #endif
-
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_pic );
 }
 
-/*******************************************************************************
- * vout_CreateReservedPicture: reserve a place for a new picture in heap
- *******************************************************************************
- * This function create a reserved image in the video output heap. 
- * A null pointer is returned if the function fails. 
- * Following configuration properties are used:
- *  VIDEO_CFG_WIDTH     picture width (required)
- *  VIDEO_CFG_HEIGHT    picture height (required)
- *  VIDEO_CFG_TYPE      picture type (required)
- *  VIDEO_CFG_FLAGS     flags
- *  VIDEO_CFG_BPP       padded bpp (required for pixel pictures)
- *  VIDEO_CFG_POSITION  position in output window
- *  VIDEO_CFG_ALIGN     base position in output window
- *  VIDEO_CFG_RATIO     display ratio
- *  VIDEO_CFG_LEVEL     overlay hierarchical level  
- *  VIDEO_CFG_REFCOUNT  links reference counter
- *  VIDEO_CFG_STREAM    video stream id
- *  VIDEO_CFG_DATE      display date
- *  VIDEO_CFG_DURATION  duration for overlay pictures
- *  VIDEO_CFG_PIXEL     pixel value for mask pictures
- *  VIDEO_CFG_DATA      picture data (required if not owner and non blank)
- * Pictures created using this function are always reserved, even if they did
- * not had the RESERVED_PICTURE flag set.
- * This function is very similar to the video_CreatePicture function.
- *******************************************************************************/
-picture_t *vout_CreateReservedPicture( vout_thread_t *p_vout, video_cfg_t *p_cfg )
+/*****************************************************************************
+ * vout_DisplayPicture: display a picture
+ *****************************************************************************
+ * Remove the reservation flag of a picture, which will cause it to be ready for
+ * display. The picture won't be displayed until vout_DatePicture has been
+ * called.
+ *****************************************************************************/
+void  vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    picture_t * p_newpic;                                 /* new picture index */
-
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Create new picture */
-        if( video_CreatePictureBody( p_newpic, p_cfg ) )
-        {   
-            intf_ErrMsg("vout error: %s\n", strerror(ENOMEM));             
-            /* Error: restore type and flags */
-            p_newpic->i_type = EMPTY_PICTURE;
-            p_newpic->i_flags = 0;
-            vlc_mutex_unlock( &p_vout->pictures_lock );
-            return( NULL );
-        }
-        p_newpic->i_flags |= RESERVED_PICTURE;
-
-        /* Update heap size, release lock and return */
-        p_vout->i_pictures++;
+    vlc_mutex_lock( &p_vout->picture_lock );
+    switch( p_pic->i_status )
+    {
+    case RESERVED_PICTURE:
+        p_pic->i_status = RESERVED_DISP_PICTURE;
+        break;
+    case RESERVED_DATED_PICTURE:
+        p_pic->i_status = READY_PICTURE;
+        break;
+#ifdef DEBUG
+    default:
+        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
+        break;
+#endif
     }
 
-    /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
+#ifdef DEBUG_VIDEO
+    intf_DbgMsg("picture %p\n", p_pic);
+#endif
+    vlc_mutex_unlock( &p_vout->picture_lock );
 }
 
-/*******************************************************************************
- * vout_ReservePicture: reserve a place for a picture in heap
- *******************************************************************************
- * This function transforms an existing picture in a reserved picture in a
- * video heap. The picture will be placed in an empty place of the video heap 
- * of the thread. If this is impossible, NULL will be returned. Else, the 
- * original picture is destroyed.
- *******************************************************************************/
-picture_t *vout_ReservePicture( vout_thread_t *p_vout, picture_t *p_pic )
+/*****************************************************************************
+ * vout_DatePicture: date a picture
+ *****************************************************************************
+ * Remove the reservation flag of a picture, which will cause it to be ready for
+ * display. The picture won't be displayed until vout_DisplayPicture has been
+ * called.
+ *****************************************************************************/
+void  vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
 {
-    picture_t * p_newpic;                                 /* new picture index */
-
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Copy picture information */
-        video_CopyPictureDescriptor( p_newpic, p_pic );
-        p_newpic->p_data = p_pic->p_data;
-        p_newpic->i_flags |= RESERVED_PICTURE;
-        free( p_pic );           
+#ifdef DEBUG_VIDEO
+    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
+#endif
 
-        /* Update heap size */
-        p_vout->i_pictures++;
+    vlc_mutex_lock( &p_vout->picture_lock );
+    p_pic->date = date;
+    switch( p_pic->i_status )
+    {
+    case RESERVED_PICTURE:
+        p_pic->i_status = RESERVED_DATED_PICTURE;
+        break;
+    case RESERVED_DISP_PICTURE:
+        p_pic->i_status = READY_PICTURE;
+        break;
+#ifdef DEBUG
+    default:
+        intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
+        break;
+#endif
     }
 
-    /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
+#ifdef DEBUG_VIDEO
+    intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
+#endif
+    vlc_mutex_unlock( &p_vout->picture_lock );
 }
 
-/*******************************************************************************
- * vout_ReservePictureCopy: reserve a place for a picture in heap
- *******************************************************************************
- * This function returns a pointer to a picture which can be processed. The
- * returned picture is a copy of the one passed as parameter.
- * This function returns NULL if the reservation fails.
- *******************************************************************************/
-picture_t *vout_ReservePictureCopy( vout_thread_t *p_vout, picture_t *p_pic )
+/*****************************************************************************
+ * vout_CreatePicture: allocate a picture in the video output heap.
+ *****************************************************************************
+ * This function create a reserved image in the video output heap.
+ * A null pointer is returned if the function fails. This method provides an
+ * already allocated zone of memory in the picture data fields. It needs locking
+ * 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 )
 {
-    picture_t * p_newpic;                                 /* new picture index */
-
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Copy picture information */
-        video_CopyPictureDescriptor( p_newpic, p_pic );
-
-        /* If source picture owns its data, make a copy */
-        if( p_pic->i_flags & OWNER_PICTURE )
-        {              
-            p_newpic->p_data = malloc( p_pic->i_height * p_pic->i_bytes_per_line );
-            if( p_newpic->p_data == NULL )                            /* error */
+    int         i_picture;                                  /* picture index */
+    int         i_chroma_width = 0;                          /* chroma width */
+    picture_t * p_free_picture = NULL;                 /* first free picture */
+    picture_t * p_destroyed_picture = NULL;       /* first destroyed picture */
+
+    /* Get lock */
+    vlc_mutex_lock( &p_vout->picture_lock );
+
+    /*
+     * Look for an empty place
+     */
+    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
+             * 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) )
             {
-                intf_ErrMsg("vout error: %s\n", strerror(ENOMEM));                    
-                /* Restore type and flags */
-                p_newpic->i_type = EMPTY_PICTURE;
-                p_newpic->i_flags = 0;
-                vlc_mutex_unlock( &p_vout->pictures_lock );   
-                return( NULL );
+                /* 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->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];
             }
-            memcpy( p_newpic->p_data, p_pic->p_data,
-                    p_pic->i_height * p_pic->i_bytes_per_line );
-        }   
-        /* If source picture does not owns its data, copy the reference */
-        else              
+        }
+        else if( (p_free_picture == NULL) &&
+                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
         {
-            p_newpic->p_data = p_pic->p_data;
+            /* Picture is empty and ready for allocation */
+            p_free_picture = &p_vout->p_picture[i_picture];
         }
-        p_newpic->i_refcount = 0;
-        p_newpic->i_flags |= RESERVED_PICTURE;
-
-        /* Update heap size */
-        p_vout->i_pictures++;
     }
 
-    /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
-}
+    /* 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 */
+        free( p_destroyed_picture->p_data );
+        p_free_picture = p_destroyed_picture;
+    }
 
-/*******************************************************************************
- * vout_ReservePictureReplicate: reserve a place for a picture in heap
- *******************************************************************************
- * This function returns a pointer to a picture which can be processed. The
- * returned picture is a copy of the one passed as parameter.
- * This function returns NULL if the reservation fails.
- *******************************************************************************/
-picture_t *vout_ReservePictureReplicate( vout_thread_t *p_vout, picture_t *p_pic )
-{
-    picture_t * p_newpic;                                 /* new picture index */
+    /*
+     * Prepare picture
+     */
+    if( p_free_picture != NULL )
+    {
+        /* Allocate memory */
+        switch( i_type )
+        {
+        case YUV_420_PICTURE:        /* YUV 420: 1,1/4,1/4 samples per pixel */
+            i_chroma_width = i_width / 2;
+            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
+            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
+            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
+            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
+            break;
+        case YUV_422_PICTURE:        /* YUV 422: 1,1/2,1/2 samples per pixel */
+            i_chroma_width = i_width / 2;
+            p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
+            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
+            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
+            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
+            break;
+        case YUV_444_PICTURE:            /* YUV 444: 1,1,1 samples per pixel */
+            i_chroma_width = i_width;
+            p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
+            p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
+            p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
+            p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
+            break;
+#ifdef DEBUG
+        default:
+            intf_DbgMsg("error: unknown picture type %d\n", i_type );
+            p_free_picture->p_data   =  NULL;
+            break;
+#endif
+        }
 
-    p_newpic = FindPicture( p_vout );
-    if( p_newpic != NULL )
-    {        
-        /* Copy picture information */
-        video_CopyPictureDescriptor( p_newpic, p_pic );
-        p_newpic->p_data = p_pic->p_data;
-        p_newpic->i_refcount = 0;
-        p_newpic->i_flags &= ~OWNER_PICTURE;
-        p_newpic->i_flags |= RESERVED_PICTURE;
+        if( p_free_picture->p_data != NULL )
+        {
+            /* Copy picture informations, set some default values */
+            p_free_picture->i_type                      = i_type;
+            p_free_picture->i_status                    = RESERVED_PICTURE;
+            p_free_picture->i_matrix_coefficients       = 1;
+            p_free_picture->i_width                     = i_width;
+            p_free_picture->i_height                    = i_height;
+            p_free_picture->i_chroma_width              = i_chroma_width;
+            p_free_picture->i_display_horizontal_offset = 0;
+            p_free_picture->i_display_vertical_offset   = 0;
+            p_free_picture->i_display_width             = i_width;
+            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
+        {
+            /* Memory allocation failed : set picture as empty */
+            p_free_picture->i_type   =  EMPTY_PICTURE;
+            p_free_picture->i_status =  FREE_PICTURE;
+            p_free_picture =            NULL;
+            intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
+        }
 
-        /* Update heap size */
-        p_vout->i_pictures++;
+#ifdef DEBUG_VIDEO
+        intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
+#endif
+        vlc_mutex_unlock( &p_vout->picture_lock );
+        return( p_free_picture );
     }
 
-    /* Release lock and return */
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-    return( p_newpic );
+    /* No free or destroyed picture could be found */
+    intf_DbgMsg( "warning: heap is full\n" );
+    vlc_mutex_unlock( &p_vout->picture_lock );
+    return( NULL );
 }
 
-/*******************************************************************************
- * vout_RemovePicture: remove a permanent or reserved picture from the heap
- *******************************************************************************
+/*****************************************************************************
+ * vout_DestroyPicture: remove a permanent or reserved picture from the heap
+ *****************************************************************************
  * This function frees a previously reserved picture or a permanent
- * picture. The picture data is destroyed if required. 
- *******************************************************************************/
-void vout_RemovePicture( vout_thread_t *p_vout, picture_t *p_pic )
+ * 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 !
+ *****************************************************************************/
+void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    vlc_mutex_lock( &p_vout->pictures_lock );
+   vlc_mutex_lock( &p_vout->picture_lock );
 
-    /* Mark picture for destruction */
-    p_pic->i_flags |= DESTROY_PICTURE;
-    /* Since permanent pictures can normally not be destroyed by the vout thread,   
-     * the permanent flag needs to be removed */
-    p_pic->i_flags &= ~PERMANENT_PICTURE;
-    intf_DbgMsg("%p -> picture %p removing requested\n", p_vout, p_pic );
+#ifdef DEBUG
+   /* Check if picture status is valid */
+   if( (p_pic->i_status != RESERVED_PICTURE) &&
+       (p_pic->i_status != RESERVED_DATED_PICTURE) &&
+       (p_pic->i_status != RESERVED_DISP_PICTURE) )
+   {
+       intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
+   }
+#endif
 
-    vlc_mutex_unlock( &p_vout->pictures_lock );
-}
+   p_pic->i_status = DESTROYED_PICTURE;
+   p_vout->i_pictures--;
 
-/*******************************************************************************
- * vout_RefreshPermanentPicture: 
- *******************************************************************************
- * Set the DISPLAYED_PICTURE flag of a permanent picture to 0, allowing to
- * display it again if it not overlaid. The display date is also updated.
- *******************************************************************************/
-void vout_RefreshPermanentPicture( vout_thread_t *p_vout, picture_t *p_pic, 
-                                   mtime_t display_date )
-{
-    vlc_mutex_lock( &p_vout->pictures_lock );
-    p_pic->i_flags &= ~DISPLAYED_PICTURE;
-    p_pic->date = display_date;
-    vlc_mutex_lock( &p_vout->pictures_lock );
+#ifdef DEBUG_VIDEO
+   intf_DbgMsg("picture %p\n", p_pic);
+#endif
+   vlc_mutex_unlock( &p_vout->picture_lock );
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * vout_LinkPicture: increment reference counter of a picture
- *******************************************************************************
+ *****************************************************************************
  * This function increment the reference counter of a picture in the video
- * heap.
- *******************************************************************************/
+ * heap. It needs a lock since several producer threads can access the picture.
+ *****************************************************************************/
 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    vlc_mutex_lock( &p_vout->pictures_lock );
+    vlc_mutex_lock( &p_vout->picture_lock );
     p_pic->i_refcount++;
-    vlc_mutex_unlock( &p_vout->pictures_lock );
+
+#ifdef DEBUG_VIDEO
+    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
+#endif
+
+    vlc_mutex_unlock( &p_vout->picture_lock );
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * vout_UnlinkPicture: decrement reference counter of a picture
- *******************************************************************************
+ *****************************************************************************
  * This function decrement the reference counter of a picture in the video heap.
- *******************************************************************************/
+ *****************************************************************************/
 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    vlc_mutex_lock( &p_vout->pictures_lock );
+    vlc_mutex_lock( &p_vout->picture_lock );
     p_pic->i_refcount--;
-#ifdef DEBUG
+
+#ifdef DEBUG_VIDEO
     if( p_pic->i_refcount < 0 )
     {
-        intf_DbgMsg("%p -> picture %p i_refcount < 0\n", p_vout, p_pic);
+        intf_DbgMsg("error: refcount < 0\n");
+        p_pic->i_refcount = 0;
     }
 #endif
-    vlc_mutex_unlock( &p_vout->pictures_lock );    
-}
-
-/*******************************************************************************
- * vout_CreateStream: get a new stream id
- *******************************************************************************
- * Stream ids are used to create list of pictures in a video output thread.
- * The function returns an unused stream id, or a negative number on error.
- * Stream id 0 is never returned, since it is not a stream descriptor but a set 
- * of independant images.
- *******************************************************************************/
-int vout_CreateStream( vout_thread_t *p_vout )
-{
-    int     i_index;                                           /* stream index */
 
-    /* Find free (inactive) stream id */
-    vlc_mutex_lock( &p_vout->streams_lock );                   /* get lock */
-    for( i_index = 1; i_index < VOUT_MAX_STREAMS; i_index++ )  /* find free id */
+    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
     {
-        if( p_vout->p_stream[i_index].i_status == VOUT_INACTIVE_STREAM )
-        {
-            /* Initialize stream */
-            p_vout->p_stream[i_index].i_status = VOUT_ACTIVE_STREAM; 
-#ifdef STATS
-            p_vout->p_stream[i_index].c_pictures = 0;
-            p_vout->p_stream[i_index].c_rendered_pictures = 0;
-#endif
-            
-            /* Return stream id */
-            intf_DbgMsg("%p -> stream %i created\n", p_vout, i_index);
-            vlc_mutex_unlock( &p_vout->streams_lock );     /* release lock */
-            return( i_index );                                    /* return id */
-        }
+        p_pic->i_status = DESTROYED_PICTURE;
+        p_vout->i_pictures--;
     }
-   
-    /* Failure: all streams id are already active */
-    intf_DbgMsg("%p -> failed\n", p_vout);    
-    vlc_mutex_unlock( &p_vout->streams_lock );    
-    return( -1 );
+
+#ifdef DEBUG_VIDEO
+    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
+#endif
+
+    vlc_mutex_unlock( &p_vout->picture_lock );
 }
 
-/*******************************************************************************
- * vout_EndStream: free a previously allocated stream
- *******************************************************************************
- * This function must be called once a stream is no more used. It can be called
- * even if there are remaining pictures in the video heap, since the stream will
- * only be destroyed once all pictures of the stream have been displayed.
- *******************************************************************************/
-void vout_EndStream( vout_thread_t *p_vout, int i_stream )
+/*****************************************************************************
+ * vout_SetBuffers: set buffers adresses
+ *****************************************************************************
+ * This function is called by system drivers to set buffers video memory
+ * adresses.
+ *****************************************************************************/
+void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
 {
-    vlc_mutex_lock( &p_vout->streams_lock );                   /* get lock */
-    p_vout->p_stream[i_stream].i_status = VOUT_ENDING_STREAM;   /* mark stream */
-    vlc_mutex_unlock( &p_vout->streams_lock );             /* release lock */
-    intf_DbgMsg("%p -> stream %d\n", p_vout, i_stream);
+    /* No picture previously */
+    p_vout->p_buffer[0].i_pic_x =         0;
+    p_vout->p_buffer[0].i_pic_y =         0;
+    p_vout->p_buffer[0].i_pic_width =     0;
+    p_vout->p_buffer[0].i_pic_height =    0;
+    p_vout->p_buffer[1].i_pic_x =         0;
+    p_vout->p_buffer[1].i_pic_y =         0;
+    p_vout->p_buffer[1].i_pic_width =     0;
+    p_vout->p_buffer[1].i_pic_height =    0;
+
+    /* The first area covers all the screen */
+    p_vout->p_buffer[0].i_areas =                 1;
+    p_vout->p_buffer[0].pi_area_begin[0] =        0;
+    p_vout->p_buffer[0].pi_area_end[0] =          p_vout->i_height - 1;
+    p_vout->p_buffer[1].i_areas =                 1;
+    p_vout->p_buffer[1].pi_area_begin[0] =        0;
+    p_vout->p_buffer[1].pi_area_end[0] =          p_vout->i_height - 1;
+
+    /* Set adresses */
+    p_vout->p_buffer[0].p_data = p_buf1;
+    p_vout->p_buffer[1].p_data = p_buf2;
 }
 
-/*******************************************************************************
- * vout_DestroyStream: free a previously allocated stream
- *******************************************************************************
- * This function must be called once a stream is no more used. It can be called
- * even if there are remaining pictures in the video heap, since all pictures
- * will be removed before the stream is destroyed.
- *******************************************************************************/
-void vout_DestroyStream( vout_thread_t *p_vout, int i_stream )
+/*****************************************************************************
+ * vout_Pixel2RGB: return red, green and blue from pixel value
+ *****************************************************************************
+ * Return color values, in 0-255 range, of the decomposition of a pixel. This
+ * is a slow routine and should only be used for initialization phase.
+ *****************************************************************************/
+void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
 {
-    vlc_mutex_lock( &p_vout->streams_lock );                   /* get lock */
-    p_vout->p_stream[i_stream].i_status = VOUT_DESTROYED_STREAM;/* mark stream */
-    vlc_mutex_unlock( &p_vout->streams_lock );             /* release lock */
-    intf_DbgMsg("%p -> stream %d\n", p_vout, i_stream);
+    *pi_red =   i_pixel & p_vout->i_red_mask;
+    *pi_green = i_pixel & p_vout->i_green_mask;
+    *pi_blue =  i_pixel & p_vout->i_blue_mask;
 }
 
-/* following functions are debugging functions */
+/* following functions are local */
 
-/*******************************************************************************
- * vout_PrintHeap: display heap state (debugging function)
- *******************************************************************************
- * This functions, which is only defined if DEBUG is defined, can be used 
- * for debugging purposes. It prints on debug stream the current state of the   
- * heap. The second parameter is used to identify the output.
- *******************************************************************************/
-#ifdef DEBUG
-void vout_PrintHeap( vout_thread_t *p_vout, char *psz_str )
+/*****************************************************************************
+ * BinaryLog: computes the base 2 log of a binary value
+ *****************************************************************************
+ * This functions is used by MaskToShift, to get a bit index from a binary
+ * value.
+ *****************************************************************************/
+static int BinaryLog(u32 i)
 {
-    int         i_picture;                                    /* picture index */
-
-    intf_Msg("vout: --- thread %p heap status (%s) ---\n", p_vout, psz_str );
+    int i_log;
 
-    /* Browse all pictures in heap */
-    for( i_picture = 0; i_picture < p_vout->i_max_pictures; i_picture++ )
+    i_log = 0;
+    if (i & 0xffff0000)
     {
-        if( p_vout->p_picture[i_picture].i_type != EMPTY_PICTURE )
-        {
-            video_PrintPicture( &p_vout->p_picture[i_picture], "vout: ..." );
-        }
-    }   
+        i_log = 16;
+    }
+    if (i & 0xff00ff00)
+    {
+        i_log += 8;
+    }
+    if (i & 0xf0f0f0f0)
+    {
+        i_log += 4;
+    }
+    if (i & 0xcccccccc)
+    {
+        i_log += 2;
+    }
+    if (i & 0xaaaaaaaa)
+    {
+        i_log++;
+    }
+    if (i != ((u32)1 << i_log))
+    {
+        intf_ErrMsg("internal error: binary log overflow\n");
+    }
 
-    intf_Msg("vout: --- end ---\n");
+    return( i_log );
 }
-#endif
 
-/* following functions are local */
-
-/*******************************************************************************
- * CheckConfiguration: check vout_CreateThread() configuration
- *******************************************************************************
- * Set default parameters where required. In DEBUG mode, check if configuration
- * is valid.
- *******************************************************************************/
-static int CheckConfiguration( video_cfg_t *p_cfg )
+/*****************************************************************************
+ * MaskToShift: transform a color mask into right and left shifts
+ *****************************************************************************
+ * This function is used for obtaining color shifts from masks.
+ *****************************************************************************/
+static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
 {
-    /* Heap size */
-    if( !( p_cfg->i_properties & VIDEO_CFG_SIZE ) )
-    {
-        p_cfg->i_size = VOUT_HEAP_SIZE;
-    }
+    u32 i_low, i_high;                 /* lower hand higher bits of the mask */
 
-    return( 0 );
+    /* Get bits */
+    i_low =  i_mask & (- i_mask);                   /* lower bit of the mask */
+    i_high = i_mask + i_low;                       /* higher bit of the mask */
+
+    /* Transform bits into an index */
+    i_low =  BinaryLog (i_low);
+    i_high = BinaryLog (i_high);
+
+    /* Update pointers and return */
+    *pi_left =   i_low;
+    *pi_right = (8 - i_high + i_low);
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * InitThread: initialize video output thread
- *******************************************************************************
+ *****************************************************************************
  * This function is called from RunThread and performs the second step of the
  * initialization. It returns 0 on success. Note that the thread's flag are not
  * modified inside this function.
- *******************************************************************************/
+ *****************************************************************************/
 static int InitThread( vout_thread_t *p_vout )
 {
-    int     i_index;                                          /* generic index */    
-
     /* Update status */
-    *p_vout->pi_status = THREAD_START;    
-    
-    /* Allocate video heap */
-    p_vout->p_picture = 
-        (picture_t *) malloc( sizeof(picture_t) * p_vout->i_max_pictures );
-    if( !p_vout->p_picture )                                          /* error */
-    {        
-        intf_ErrMsg("vout error: %s\n", strerror(ENOMEM));
-        intf_DbgMsg("%p -> failed\n", p_vout);
-        *p_vout->pi_status = THREAD_ERROR;        
-        return( 1 );
-    }
-
-    /* Initialize pictures */    
-    for( i_index = 0; i_index < p_vout->i_max_pictures; i_index++)
-    {
-        p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
-        p_vout->p_picture[i_index].i_flags = 0;
-    }
+    intf_DbgMsg("\n");
+    *p_vout->pi_status = THREAD_START;
 
-    /* Initialize video streams - note that stream 0 is always active */
-    p_vout->p_stream[0].i_status = VOUT_ACTIVE_STREAM; 
-#ifdef STATS
-    p_vout->p_stream[0].c_pictures = 0;
-    p_vout->p_stream[0].c_rendered_pictures = 0;
-#endif
-    for( i_index = 1; i_index < VOUT_MAX_STREAMS; i_index++ )
+   /* Initialize output method - this function issues its own error messages */
+    if( p_vout->p_sys_init( p_vout ) )
     {
-        p_vout->p_stream[i_index].i_status = VOUT_INACTIVE_STREAM;
+        return( 1 );
     }
-    /* Initialize other properties */
-    p_vout->i_pictures = 0;
-#ifdef STATS
-    p_vout->c_loops = 0;
-    p_vout->c_idle_loops = 0;
-    p_vout->c_pictures = 0;
-    p_vout->c_rendered_pictures = 0;
-#endif
 
-    /* Initialize output method - width, height, screen depth and bytes per 
-     * pixel are initialized by this call. */
-    if( vout_X11CreateOutputMethod( p_vout ) )                        /* error */
+    /* Initialize convertion tables and functions */
+    if( vout_InitYUV( p_vout ) )
     {
-        free( p_vout->p_picture );
-        *p_vout->pi_status = THREAD_ERROR;        
+        intf_ErrMsg("error: can't allocate YUV translation tables\n");
         return( 1 );
-    } 
+    }
 
     /* Mark thread as running and return */
-    *p_vout->pi_status = THREAD_READY;    
-    intf_DbgMsg("%p -> succeeded\n", p_vout);    
-    return(0);    
+    p_vout->b_active =          1;
+    *p_vout->pi_status =        THREAD_READY;
+    intf_DbgMsg("thread ready\n");
+    return( 0 );
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * RunThread: video output thread
- *******************************************************************************
+ *****************************************************************************
  * Video output thread. This function does only returns when the thread is
  * terminated. It handles the pictures arriving in the video heap and the
  * display device events.
- *******************************************************************************/
+ *****************************************************************************/
 static void RunThread( vout_thread_t *p_vout)
 {
-    int             i_stream;                                  /* stream index */
-    int             i_picture;                                /* picture index */
-    int             i_active_streams;              /* number of active streams */
-    int             i_err;                                       /* error code */
-    mtime_t         display_date;                              /* display date */    
-    mtime_t         current_date;                              /* current date */
-    picture_t *     pp_sorted_picture[VOUT_MAX_PICTURES];   /* sorted pictures */
-#ifdef VOUT_DEBUG
-    char            sz_date[MSTRTIME_MAX_SIZE];                 /* date buffer */
-#endif
+    /* XXX?? welcome to gore land */
+    static int i_trash_count = 0;
+    static mtime_t last_display_date = 0;
 
-    /* 
-     * Initialize thread and free configuration 
+    int             i_index;                                /* index in heap */
+    mtime_t         current_date;                            /* current date */
+    mtime_t         display_date;                            /* display date */
+    boolean_t       b_display;                               /* display flag */
+    picture_t *     p_pic;                                /* picture pointer */
+    subpicture_t *  p_subpic;                          /* subpicture pointer */
+
+    /*
+     * Initialize thread
      */
     p_vout->b_error = InitThread( p_vout );
     if( p_vout->b_error )
     {
-        free( p_vout );                                  /* destroy descriptor */
-        return;        
-    }    
+        DestroyThread( p_vout, THREAD_ERROR );
+        return;
+    }
+    intf_DbgMsg("\n");
 
     /*
      * Main loop - it is not executed if an error occured during
@@ -764,873 +933,1138 @@ static void RunThread( vout_thread_t *p_vout)
      */
     while( (!p_vout->b_die) && (!p_vout->b_error) )
     {
-        /* Get locks on pictures and streams */
-        vlc_mutex_lock( &p_vout->streams_lock );
-        vlc_mutex_lock( &p_vout->pictures_lock );
-        
-        /* Initialise streams: clear images from all streams */
-        for( i_stream = 0; i_stream < VOUT_MAX_STREAMS; i_stream++ )
+        /* Initialize loop variables */
+        p_pic =         NULL;
+        p_subpic =      NULL;
+        display_date =  0;
+        current_date =  mdate();
+
+        /*
+         * 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++ )
         {
-            p_vout->p_stream[i_stream].p_next_picture = NULL;
+            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;
+            }
         }
 
-        /* First pass: a set of pictures is selected - all unselected pictures
-         * which should be removed are removed, and a display date is computed.
-         * If a stream has no next_picture after this step, then the heap does
-         * not include any picture from that stream. */
-        current_date = mdate();
-        display_date = FirstPass( p_vout, current_date );
-
-        /* Stream management: streams which are in ENDING or DESTROYED state and
-         * which have no more pictures are destroyed */
-        for( i_active_streams = i_stream = 1; 
-             i_stream < VOUT_MAX_STREAMS; 
-             i_stream++ )
-        {    
-            switch( p_vout->p_stream[i_stream].i_status )
+        if( p_pic )
+        {
+#ifdef STATS
+            /* Computes FPS rate */
+            p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
+#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 )
             {
-            case VOUT_ACTIVE_STREAM:                          /* active stream */
-                i_active_streams++;
-                break;
+                /* Picture is late: it will be destroyed and the thread will sleep and
+                 * go to next picture */
 
-            case VOUT_ENDING_STREAM:            /* ending or destroyed streams */
-            case VOUT_DESTROYED_STREAM:
-                /* Those streams can be destroyed if there are no more picture
-                 * remaining. This should always be the case for destroyed
-                 * streams, except if it includes permanent pictures */
-                if( p_vout->p_stream[i_stream].p_next_picture == NULL )
+                vlc_mutex_lock( &p_vout->picture_lock );
+                if( p_pic->i_refcount )
                 {
-                    p_vout->p_stream[i_stream].i_status = VOUT_INACTIVE_STREAM;
-#ifdef STATS                   
-                    intf_DbgMsg("%p -> stream %d destroyed %d pictures, %d rendered pictures\n", 
-                                p_vout, i_stream, p_vout->p_stream[i_stream].c_pictures, 
-                                p_vout->p_stream[i_stream].c_rendered_pictures );
-#else
-                    intf_DbgMsg("%p -> stream %d destroyed\n", p_vout, i_stream );
-#endif
+                    p_pic->i_status = DISPLAYED_PICTURE;
                 }
-                /* If the stream can't be destroyed, it means it is still 
-                 * active - in that case, the next image pointer needs to be
-                 * cleard */
                 else
                 {
-                    i_active_streams++;
-                    p_vout->p_stream[i_stream].p_next_picture = NULL;
+                    p_pic->i_status = DESTROYED_PICTURE;
+                    p_vout->i_pictures--;
                 }
-                break;
+                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;
+                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 );
             }
         }
-      
-        /* From now until next loop, only next_picture field in streams
-         * will be used, and selected pictures structures won't modified.
-         * Therefore, locks can be released */
-        vlc_mutex_unlock( &p_vout->pictures_lock );
-        vlc_mutex_unlock( &p_vout->streams_lock );
-
-        /* If there are some pictures to display, then continue */
-        if( display_date != LAST_MDATE )
+        /*
+         * 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
+         */
+        /* XXX?? */
+
+        /*
+         * Perform rendering, sleep and display rendered picture
+         */
+        if( p_pic )                        /* picture and perhaps subpicture */
         {
-            /* Increase display_date if it is too low */
-            if( display_date < current_date + VOUT_DISPLAY_DELAY )
+            b_display = p_vout->b_active;
+            p_vout->last_display_date = display_date;
+
+            if( b_display )
             {
-                intf_Msg("vout: late picture(s) detected\n");
-                display_date = current_date + VOUT_DISPLAY_DELAY;
+                /* Set picture dimensions and clear buffer */
+                SetBufferPicture( p_vout, p_pic );
+
+                /* Render picture and informations */
+                RenderPicture( p_vout, p_pic );
+                if( p_vout->b_info )
+                {
+                    RenderPictureInfo( p_vout, p_pic );
+                    RenderInfo( p_vout );
+                }
             }
-#ifdef VOUT_DEBUG
-            intf_DbgMsg("%p -> display_date is %s\n", p_vout, 
-                        mstrtime( sz_date, display_date ));
-#endif
 
-            /* Second pass: a maximum of one picture per stream is selected
-             * (except for stream 0), and some previously selected pictures may
-             * be removed */
-            SecondPass( p_vout, display_date );
-#ifdef VOUT_DEBUG
-            vout_PrintHeap( p_vout, "ready for rendering" );
-#endif
-            /* Rendering: sort pictures and render them */
-            SortPictures( p_vout, pp_sorted_picture );
-            for( i_picture = 0; pp_sorted_picture[i_picture] != NULL; i_picture++ )
+            /* Remove picture from heap */
+            vlc_mutex_lock( &p_vout->picture_lock );
+            if( p_pic->i_refcount )
             {
-                /* Render picture */
-                RenderPicture( p_vout, pp_sorted_picture[i_picture] );             
+                p_pic->i_status = DISPLAYED_PICTURE;
             }
-            
-            /* Handle output method events - this function can return a negative
-             * value, which means a fatal error and the end of the display loop 
-             * (i.e. the X11 window has been destroyed), a positive one, meaning
-             * a modification occured in the pictures and they do have need to 
-             * be displayed, or 0 */
-            i_err = vout_X11ManageOutputMethod( p_vout );
-            if( !i_err )
+            else
             {
-                /* Sleep and display */
-                mwait( display_date );
-                vout_X11DisplayOutput( p_vout );
+                p_pic->i_status = DESTROYED_PICTURE;
+                p_vout->i_pictures--;
             }
-            else if( i_err < 0 )
-            {                
-                /* An 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;
+            vlc_mutex_unlock( &p_vout->picture_lock );
+
+            /* Render interface and subpicture */
+            if( b_display && p_vout->b_interface )
+            {
+                RenderInterface( p_vout );
             }
-        }        
-        /* If there is nothing to display, just handle events and sleep a while, 
-         * hopping there will be something to do later */
-        else    
-        {
-            if( vout_X11ManageOutputMethod( p_vout ) < 0)
+            if( p_subpic )
             {
-                p_vout->b_error = 1;
-                
+                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 );
             }
-            else
-            {   
-                msleep( VOUT_IDLE_SLEEP );
-            }            
-#ifdef STATS
-            /* Update counters */
-            p_vout->c_idle_loops++;
-#endif
-        }        
 
-#ifdef STATS
-        /* Update counters */
-        p_vout->c_loops++;
-#endif
-    } 
+        }
+        else if( p_subpic )                              /* subpicture alone */
+        {
+            b_display = p_vout->b_active;
+            p_vout->last_display_date = display_date;
 
-    /*
-     * Error loop
-     */
-    if( p_vout->b_error )
-    {
-        ErrorThread( p_vout );        
-    }
+            if( b_display )
+            {
+                /* Clear buffer */
+                SetBufferPicture( p_vout, NULL );
 
-    /* End of thread */
-    EndThread( p_vout );
-}
+                /* 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 );
+            }
 
-/*******************************************************************************
- * ErrorThread: RunThread() error loop
- *******************************************************************************
- * This function is called when an error occured during thread main's loop. The
- * thread can still receive feed, but must be ready to terminate as soon as
+            /* 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 /* && XXX?? intf_change */ )
+            {
+                /* Interface has changed, so a new rendering is required - force
+                 * it by setting last idle date to 0 */
+                p_vout->last_idle_date = 0;
+            }
+
+            /* Render idle screen and update idle date, then render interface if
+             * required */
+            b_display = RenderIdle( p_vout );
+            if( b_display )
+            {
+                p_vout->last_idle_date = current_date;
+                if( p_vout->b_interface )
+                {
+                    RenderInterface( p_vout );
+                }
+            }
+
+        }
+        else
+        {
+            b_display = 0;
+        }
+
+        /*
+         * Sleep, wake up and display rendered picture
+         */
+
+#ifdef STATS
+        /* Store render time */
+        p_vout->render_time = mdate() - current_date;
+#endif
+
+        /* Give back change lock */
+        vlc_mutex_unlock( &p_vout->change_lock );
+
+        /* Sleep a while or until a given date */
+        if( display_date != 0 )
+        {
+            mwait( display_date );
+        }
+        else
+        {
+            msleep( VOUT_IDLE_SLEEP );
+        }
+
+        /* On awakening, take back lock and send immediately picture to display,
+         * then swap buffers */
+        vlc_mutex_lock( &p_vout->change_lock );
+#ifdef DEBUG_VIDEO
+        intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
+                     p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
+#endif
+        if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
+        {
+            p_vout->p_sys_display( p_vout );
+            p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
+        }
+
+        /*
+         * Check events and manage thread
+         */
+        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;
+        }
+    }
+
+    /*
+     * Error loop - wait until the thread destruction is requested
+     */
+    if( p_vout->b_error )
+    {
+        ErrorThread( p_vout );
+    }
+
+    /* End of thread */
+    EndThread( p_vout );
+    DestroyThread( p_vout, THREAD_OVER );
+    intf_DbgMsg( "thread end\n" );
+}
+
+/*****************************************************************************
+ * ErrorThread: RunThread() error loop
+ *****************************************************************************
+ * This function is called when an error occured during thread main's loop. The
+ * thread can still receive feed, but must be ready to terminate as soon as
  * possible.
- *******************************************************************************/
+ *****************************************************************************/
 static void ErrorThread( vout_thread_t *p_vout )
 {
-    int i_picture;                                            /* picture index */    
-    
     /* Wait until a `die' order */
+    intf_DbgMsg("\n");
     while( !p_vout->b_die )
     {
-        /* Get lock on pictures */
-        vlc_mutex_lock( &p_vout->pictures_lock );     
-
-        /* Try to remove all pictures - only removable pictures will be
-         * removed */
-        for( i_picture = 0; i_picture < p_vout->i_max_pictures; i_picture++ )
-        {   
-            if( p_vout->p_picture[i_picture].i_type != EMPTY_PICTURE )
-            {
-                EmptyPicture( p_vout, &p_vout->p_picture[i_picture] );
-            }
-        }
-        
-        /* Release locks on pictures */
-        vlc_mutex_unlock( &p_vout->pictures_lock );
-
         /* Sleep a while */
-        msleep( VOUT_IDLE_SLEEP );                
+        msleep( VOUT_IDLE_SLEEP );
     }
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * EndThread: thread destruction
- *******************************************************************************
- * This function is called when the thread ends after a sucessfull 
- * initialization.
- *******************************************************************************/
+ *****************************************************************************
+ * This function is called when the thread ends after a sucessfull
+ * initialization. It frees all ressources allocated by InitThread.
+ *****************************************************************************/
 static void EndThread( vout_thread_t *p_vout )
 {
-    int *   pi_status;                                        /* thread status */
-#ifdef DEBUG
-    int     i_stream;                                    /* video stream index */
-#endif
-        
+    int     i_index;                                        /* index in heap */
+
     /* Store status */
-    pi_status = p_vout->pi_status;    
-    *pi_status = THREAD_END;    
-    
-#ifdef DEBUG
-    /* Check for remaining pictures or video streams */
-    if( p_vout->i_pictures )
-    {
-        intf_DbgMsg("%p -> remaining pictures\n", p_vout);        
-    }   
-    for( i_stream = 1; 
-         (i_stream < VOUT_MAX_STREAMS) && (p_vout->p_stream[i_stream].i_status == VOUT_INACTIVE_STREAM);
-         i_stream++ )
-    {
-        ;        
-    }    
-    if( i_stream != VOUT_MAX_STREAMS )
-    {
-        intf_DbgMsg("%p -> remaining video streams\n", p_vout);        
-    }    
-#endif
+    intf_DbgMsg("\n");
+    *p_vout->pi_status = THREAD_END;
 
-    /* Destroy thread structures allocated by InitThread */
-    vout_X11DestroyOutputMethod( p_vout );            /* destroy output method */
-    free( p_vout->p_picture );                                    /* free heap */
-    free( 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 )
+        {
+            free( p_vout->p_picture[i_index].p_data );
+        }
+        if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
+        {
+            free( p_vout->p_subpicture[i_index].p_data );
+        }
+    }
 
-    /* Update status */
-    *pi_status = THREAD_OVER;    
-    intf_DbgMsg("%p\n", p_vout);
+    /* Destroy translation tables */
+    vout_EndYUV( p_vout );
+    p_vout->p_sys_end( p_vout );
 }
 
-/*******************************************************************************
- * FindPicture: find an empty picture in a video heap
- *******************************************************************************
- * This function is used by most of the vout_*Picture*() functions. It locks the
- * video heap, look for an empty picture in it and return a pointer to this
- * picture, or NULL if the heap is full.
- * Not that the heap is not unlocked when this function returns, and it needs 
- * to be donne by the calling function.
- *******************************************************************************/
-static picture_t *FindPicture( vout_thread_t *p_vout )
+/*****************************************************************************
+ * DestroyThread: thread destruction
+ *****************************************************************************
+ * This function is called when the thread ends. It frees all ressources
+ * allocated by CreateThread. Status is available at this stage.
+ *****************************************************************************/
+static void DestroyThread( vout_thread_t *p_vout, int i_status )
 {
-    int         i_picture;                                    /* picture index */
+    int *pi_status;                                         /* status adress */
 
-    /* Get lock */
-    vlc_mutex_lock( &p_vout->pictures_lock );
+    /* Store status adress */
+    intf_DbgMsg("\n");
+    pi_status = p_vout->pi_status;
 
-    /* Look for an empty place */
-    for( i_picture = 0; i_picture < p_vout->i_max_pictures; i_picture++ )
-    {
-        /* An empty place has been found: return */
-        if( p_vout->p_picture[i_picture].i_type == EMPTY_PICTURE )
-        {
-            return( &p_vout->p_picture[i_picture] );
-        }
-    }
+    /* Destroy thread structures allocated by Create and InitThread */
+    vout_UnloadFont( p_vout->p_default_font );
+    vout_UnloadFont( p_vout->p_large_font );
+    p_vout->p_sys_destroy( p_vout );
 
-    /* Nothing has been found */
-    return( NULL );    
+    /* Close plugin */
+    TrashPlugin( p_vout->p_vout_plugin );
+
+    /* Free structure */
+    free( p_vout );
+    *pi_status = i_status;
 }
 
-/*******************************************************************************
- * EmptyPicture: empty a picture without destroying its descriptor
- *******************************************************************************
- * When a picture is no more used in video heap or must be destroyed, this
- * function is called to destroy associated data and set picture type to empty.
- * Only non permanent and unlinked pictures can be destroyed. 
- * Only non-empty pictures should be sent to this function.
- * All picture flags are cleared, and the heap size is decreased.
- *******************************************************************************
- * Messages type: vout, major code 15
- *******************************************************************************/
-static void EmptyPicture( vout_thread_t *p_vout, picture_t *p_pic )
-{    
-#ifdef DEBUG
-    /* Check if picture is non-empty */
-    if( p_pic->i_type == EMPTY_PICTURE )
+/*****************************************************************************
+ * Print: print simple text on a picture
+ *****************************************************************************
+ * This function will print a simple text on the picture. It is designed to
+ * print debugging or general informations.
+ *****************************************************************************/
+void Print( vout_thread_t *p_vout, int i_x, int i_y, int i_h_align, int i_v_align, unsigned char *psz_text )
+{
+    int                 i_text_height;                  /* total text height */
+    int                 i_text_width;                    /* total text width */
+
+    /* Update upper left coordinates according to alignment */
+    vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
+    if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
     {
-        intf_DbgMsg("%p -> trying to remove empty picture %p\n",
-                    p_vout, p_pic);
-        return;
+        /* Set area and print text */
+        SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
+        vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
+                    i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
+                    p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
+                    p_vout->i_white_pixel, 0, 0,
+                    0, psz_text );
     }
-#endif
+}
+
+/*****************************************************************************
+ * SetBufferArea: activate an area in current buffer
+ *****************************************************************************
+ * This function is called when something is rendered on the current buffer.
+ * It set the area as active and prepare it to be cleared on next rendering.
+ * Pay attention to the fact that in this functions, i_h is in fact the end y
+ * coordinate of the new area.
+ *****************************************************************************/
+static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
+{
+    vout_buffer_t *     p_buffer;                          /* current buffer */
+    int                 i_area_begin, i_area_end; /* area vertical extension */
+    int                 i_area, i_area_copy;                   /* area index */
+    int                 i_area_shift;            /* shift distance for areas */
 
-    /* Mark as ready for destruction */
-    p_pic->i_flags |= DESTROY_PICTURE;
-    p_pic->i_flags &= ~DISPLAY_PICTURE;
+    /* Choose buffer and modify h to end of area position */
+    p_buffer =  &p_vout->p_buffer[ p_vout->i_buffer_index ];
+    i_h +=      i_y - 1;
 
-    /* If picture is no more referenced and is not permanent, destroy */
-    if( (p_pic->i_refcount == 0) && !(p_pic->i_flags & PERMANENT_PICTURE) )
+    /*
+     * Remove part of the area which is inside the picture - this is done
+     * by calling again SetBufferArea with the correct areas dimensions.
+     */
+    if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
     {
-        /* If picture owns its data, free them */
-        if( p_pic->i_flags & OWNER_PICTURE )
+        i_area_begin =  p_buffer->i_pic_y;
+        i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
+
+        if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
+            ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
+            ((i_y <  i_area_begin) && (i_h > i_area_end)) )
         {
-            free( p_pic->p_data );
-        }           
-        p_pic->i_type = EMPTY_PICTURE;
-        p_pic->i_flags = 0;  
-        p_vout->i_pictures--;
-        intf_DbgMsg("%p -> picture %p removed\n", p_vout, p_pic);
+            /* Keep the stripe above the picture, if any */
+            if( i_y < i_area_begin )
+            {
+                SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
+            }
+            /* Keep the stripe below the picture, if any */
+            if( i_h > i_area_end )
+            {
+                SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
+            }
+            return;
+        }
     }
-}
 
-/*******************************************************************************
- * FirstPass: first pass of the vout thread on pictures
- *******************************************************************************
- * A set of pictures is selected according to the current date and their display
- * date. Some unselected and already displayed pictures are removed, and the
- * next display date is computed. 
- * The next_picture fields of all the streams are updated. Note that streams
- * are initialized before this function is called.
- *******************************************************************************
- * Messages type: vout, major code 18
- *******************************************************************************/
-static mtime_t FirstPass( vout_thread_t *p_vout, mtime_t current_date )
-{
-    mtime_t         display_date;                              /* display date */
-    int             i_picture;                                /* picture index */
-    picture_t *     p_picture;                   /* picture pointer (shortcut) */
-    /* Set up date */
-    display_date = LAST_MDATE;
-        
-    /* Browse all pictures */
-    for( i_picture = 0; i_picture < p_vout->i_max_pictures; i_picture++ )
-    {
-        p_picture = &p_vout->p_picture[i_picture];
-
-        /* Empty pictures are never selected */
-        if( p_picture->i_type == EMPTY_PICTURE )
-        {
-            ;
-        }               
-        /* Destroyed pictures are never selected, they can be emptied if needed */
-        else if( p_picture->i_flags & DESTROY_PICTURE )
+    /* Skip some extensions until interesting areas */
+    for( i_area = 0;
+         (i_area < p_buffer->i_areas) &&
+             (p_buffer->pi_area_end[i_area] + 1 <= i_y);
+         i_area++ )
+    {
+        ;
+    }
+
+    if( i_area == p_buffer->i_areas )
+    {
+        /* New area is below all existing ones: just add it at the end of the
+         * array, if possible - else, append it to the last one */
+        if( i_area < VOUT_MAX_AREAS )
         {
-            EmptyPicture( p_vout, p_picture );
-        }  
-#ifdef DEBUG
-        /* Pictures in an inactive stream are submitted to a deletion attempt,
-         * and nether selected - this case should never happen */
-        else if( p_vout->p_stream[ p_picture->i_stream ].i_status == VOUT_INACTIVE_STREAM )
-        {   
-            intf_DbgMsg("%p -> picture %p belongs to an inactive stream\n", 
-                        p_vout, p_picture);
-            EmptyPicture( p_vout, p_picture );
-        }  
-#endif
-        /* Pictures in a destroyed stream are submitted to a deletion attempt,
-         * and nether selected */
-        else if( p_vout->p_stream[ p_picture->i_stream ].i_status == VOUT_DESTROYED_STREAM )
-        {   
-            EmptyPicture( p_vout, p_picture );
+            p_buffer->pi_area_begin[i_area] = i_y;
+            p_buffer->pi_area_end[i_area] = i_h;
+            p_buffer->i_areas++;
         }
-        /* Reserved pictures are never selected */
-        else if( p_picture->i_flags & RESERVED_PICTURE )
+        else
         {
-            p_picture->i_flags &= ~DISPLAY_PICTURE;
+#ifdef DEBUG_VIDEO
+            intf_DbgMsg("areas overflow\n");
+#endif
+            p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
         }
-        /* Overlay pictures */
-        else if( p_picture->i_flags & OVERLAY_PICTURE )
+    }
+    else
+    {
+        i_area_begin =  p_buffer->pi_area_begin[i_area];
+        i_area_end =    p_buffer->pi_area_end[i_area];
+
+        if( i_y < i_area_begin )
         {
-            /* A picture is outdated if it is not permanent and if it's maximal
-             * date is passed */
-            if( !(p_picture->i_flags & PERMANENT_PICTURE ) 
-                && (p_picture->date + p_picture->duration < current_date) )
+            if( i_h >= i_area_begin - 1 )
             {
-                p_picture->i_flags &= ~DISPLAY_PICTURE;      
-                EmptyPicture( p_vout, p_picture );
+                /* Extend area above */
+                p_buffer->pi_area_begin[i_area] = i_y;
             }
-            /* Else if picture is in stream 0, it will always be selected */
-            else if( p_picture->i_stream == 0 )
+            else
             {
-                p_picture->i_flags |= DISPLAY_PICTURE;   
-                /* Update display_date if picture has never been displayed */
-                if( !(p_picture->i_flags & DISPLAYED_PICTURE) && (p_picture->date < display_date) )
+                /* Create a new area above : merge last area if overflow, then
+                 * move all old areas down */
+                if( p_buffer->i_areas == VOUT_MAX_AREAS )
                 {
-                    display_date = p_picture->date;
-                }    
-            }
-            /* The picture can be considered as a regular picture, because it
-             * has never been displayed */
-            else if( !(p_picture->i_flags & DISPLAYED_PICTURE) )
-            {                
-                /* Select picture if can be updated */
-                if( (p_vout->p_stream[ p_picture->i_stream].p_next_picture == NULL)
-                    || (p_vout->p_stream[p_picture->i_stream].p_next_picture->date > p_picture->date))
+#ifdef DEBUG_VIDEO
+                    intf_DbgMsg("areas overflow\n");
+#endif
+                    p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
+                }
+                else
                 {
-                    p_picture->i_flags |= DISPLAY_PICTURE;   
-                    p_vout->p_stream[p_picture->i_stream].p_next_picture = p_picture;
-                    /* Update display_date */
-                    if( p_picture->date < display_date )
-                    {
-                        display_date = p_picture->date;
-                    }
+                    p_buffer->i_areas++;
                 }
-            }
-            /* In other cases (overlay pictures which have already been displayed), 
-             * the picture is always selected */
-            else
-            {
-                p_picture->i_flags |= DISPLAY_PICTURE;
-                /* The stream is only updated if there is no picture in that stream */
-                if( p_vout->p_stream[ p_picture->i_stream].p_next_picture == NULL )
+                for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
                 {
-                    p_vout->p_stream[p_picture->i_stream].p_next_picture = p_picture;
+                    p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
+                    p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy - 1];
                 }
+                p_buffer->pi_area_begin[i_area] = i_y;
+                p_buffer->pi_area_end[i_area] = i_h;
+                return;
             }
         }
-        /* Non overlay pictures are deleted if they have been displayed, and
-         * selected if there date is valid */
-        else
+        if( i_h > i_area_end )
         {
-            /* Remove already displayed pictures */
-            if( p_picture->i_flags & DISPLAYED_PICTURE )
+            /* Find further areas which can be merged with the new one */
+            for( i_area_copy = i_area + 1;
+                 (i_area_copy < p_buffer->i_areas) &&
+                     (p_buffer->pi_area_begin[i_area] <= i_h);
+                 i_area_copy++ )
             {
-                EmptyPicture( p_vout, p_picture );
+                ;
             }
-            /* Remove passed pictures, The picture is marked as displayed in case it 
-             * could not be removed */
-            else if( p_picture->date < current_date )
+            i_area_copy--;
+
+            if( i_area_copy != i_area )
             {
-                intf_DbgMsg("%p -> picture %p detected late\n", 
-                            p_vout, p_picture );
-                p_picture->i_flags |= DISPLAYED_PICTURE;
-                EmptyPicture( p_vout, p_picture );
+                /* Merge with last possible areas */
+                p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
+
+                /* Shift lower areas upward */
+                i_area_shift = i_area_copy - i_area;
+                p_buffer->i_areas -= i_area_shift;
+                for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
+                {
+                    p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
+                    p_buffer->pi_area_end[i_area_copy] =   p_buffer->pi_area_end[i_area_copy + i_area_shift];
+                }
             }
-            /* Update streams for other pictures */
             else
             {
-                p_picture->i_flags |= DISPLAY_PICTURE;
-                /* Update 'next picture' field in stream descriptor */
-                if( (p_vout->p_stream[ p_picture->i_stream].p_next_picture == NULL)
-                    || (p_vout->p_stream[p_picture->i_stream].p_next_picture->date > p_picture->date))
-                {
-                   p_vout->p_stream[p_picture->i_stream].p_next_picture = p_picture;
-                   /* Update display_date */
-                   if( p_picture->date < display_date )
-                   {
-                       display_date = p_picture->date;
-                   }
-                }
+                /* Extend area below */
+                p_buffer->pi_area_end[i_area] = i_h;
             }
         }
     }
-
-    return( display_date );
 }
 
-/*******************************************************************************
- * SecondPass: second pass of the vout thread on pictures
- *******************************************************************************
- * Select only one picture per stream other than stream 0, and remove pictures
- * which should have been displayed.... but arrived too late. Note that nothing
- * is locked when this function is called, and therefore pictures should not
- * be removed here.
- * Only previously selected pictures are processed.
- *******************************************************************************
- * Messages type: vout, major code 19
- *******************************************************************************/
-static void SecondPass( vout_thread_t *p_vout, mtime_t display_date )
+/*****************************************************************************
+ * SetBufferPicture: clear buffer and set picture area
+ *****************************************************************************
+ * This function is called before any rendering. It clears the current
+ * rendering buffer and set the new picture area. If the picture pointer is
+ * NULL, then no picture area is defined. Floating operations are avoided since
+ * some MMX calculations may follow.
+ *****************************************************************************/
+static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    int         i_picture;                                    /* picture index */
-    picture_t * p_picture;                       /* picture pointer (shortcut) */
+    vout_buffer_t *     p_buffer;                          /* current buffer */
+    int                 i_pic_x, i_pic_y;                /* picture position */
+    int                 i_pic_width, i_pic_height;     /* picture dimensions */
+    int                 i_old_pic_y, i_old_pic_height;   /* old picture area */
+    int                 i_vout_width, i_vout_height;   /* display dimensions */
+    int                 i_area;                                /* area index */
+    int                 i_data_index;                     /* area data index */
+    int                 i_data_size;   /* area data size, in 256 bytes blocs */
+    u64 *               p_data;                   /* area data, for clearing */
+    byte_t *            p_data8;           /* area data, for clearing (slow) */
+
+    /* Choose buffer and set display dimensions */
+    p_buffer =          &p_vout->p_buffer[ p_vout->i_buffer_index ];
+    i_vout_width =      p_vout->i_width;
+    i_vout_height =     p_vout->i_height;
 
-    for( i_picture = 0; i_picture < p_vout->i_max_pictures; i_picture++ )
+    /*
+     * Computes new picture size
+     */
+    if( p_pic != NULL )
     {
-        p_picture = &p_vout->p_picture[i_picture];
-        
-        /* Process only previously selected pictures */
-        if( p_picture->i_flags & DISPLAY_PICTURE )
+        /* Try horizontal scaling first - width must be a mutiple of 16 */
+        i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
+                       i_vout_width : p_pic->i_width) & ~0xf;
+        switch( p_pic->i_aspect_ratio )
         {
-            /* Deselect picture if it is too youg */
-            if( p_picture->date > display_date + VOUT_DISPLAY_TOLERANCE )
-            {
-                p_picture->i_flags &= ~DISPLAY_PICTURE;
-            }
-            /* Pictures in stream 0 are selected depending only of their date */
-            else if( p_picture->i_stream == 0 )
-            {
-                /* Overlay pictures are not selected if they are 
-                 * outdated */
-                if( p_picture->i_flags & OVERLAY_PICTURE )
-                {
-                    if( !(p_picture->i_flags & PERMANENT_PICTURE)
-                    && (display_date > p_picture->date + p_picture->duration) )
-                    {
-                        p_picture->i_flags |= DESTROY_PICTURE;
-                        p_picture->i_flags &= ~DISPLAY_PICTURE;
-                    }
-                }
-                /* Regular pictures are selected if they are not too late */
-                else if( p_picture->date < display_date - VOUT_DISPLAY_TOLERANCE )
-                {
-                    intf_DbgMsg("%p -> picture %p detected late\n",
-                                p_vout, p_picture );
-                    p_picture->i_flags |= DISPLAYED_PICTURE | DESTROY_PICTURE;
-                    p_picture->i_flags &= ~DISPLAY_PICTURE;
-                }
-            }
-            /* Overlay pictures which have been displayed have special 
-             * processing */
-            else if( (p_picture->i_flags & OVERLAY_PICTURE) 
-                     && (p_picture->i_flags & DISPLAYED_PICTURE) )
-            {
-                /* If the stream is not empty, or the picture has been 
-                 * outdated, de select */
-                if( (p_vout->p_stream[p_picture->i_stream].p_next_picture != NULL)
-                    || (!(p_picture->i_flags & PERMANENT_PICTURE)
-                        && (display_date > p_picture->date + p_picture->duration) ) )
-                {
-                    p_picture->i_flags |= DESTROY_PICTURE;
-                    p_picture->i_flags &= ~DISPLAY_PICTURE;
-                }
-            }
-            /* Other pictures are 'regular' pictures */
-            else 
-            {
-                /* If the stream is empty, always select */
-                if( p_vout->p_stream[p_picture->i_stream].p_next_picture == NULL)
-                {
-                    p_vout->p_stream[p_picture->i_stream].p_next_picture = p_picture;
-                }
-                /* Else, remove and mark as displayed (skip) if the picture is too old */
-                else if( p_picture->date < display_date - VOUT_DISPLAY_TOLERANCE )
-                {
-                    intf_DbgMsg("%p -> picture %p detected late\n",
-                                p_vout, p_picture );
-                    p_picture->i_flags |= DISPLAYED_PICTURE | DESTROY_PICTURE;
-                    p_picture->i_flags &= ~DISPLAY_PICTURE;
-                }
-                /* Else, select if the picture is younger than the current selected one */
-                else if( p_picture->date 
-                          < p_vout->p_stream[p_picture->i_stream].p_next_picture->date )
-                {
-                    /* Deselect the previous picture */
-                    p_vout->p_stream[p_picture->i_stream].p_next_picture->i_flags 
-                        &= ~DISPLAY_PICTURE; 
-                    /* Select the current one */
-                    p_vout->p_stream[p_picture->i_stream].p_next_picture = p_picture;     
-                }
-                /* Else, select if the current selected one is an already displayed overlay */
-                else if( (p_vout->p_stream[p_picture->i_stream].p_next_picture->i_flags 
-                          & ( OVERLAY_PICTURE | DISPLAYED_PICTURE )) 
-                         == (OVERLAY_PICTURE | DISPLAYED_PICTURE) ) 
+        case AR_3_4_PICTURE:
+            i_pic_height = i_pic_width * 3 / 4;
+            break;
+        case AR_16_9_PICTURE:
+            i_pic_height = i_pic_width * 9 / 16;
+            break;
+        case AR_221_1_PICTURE:
+            i_pic_height = i_pic_width * 100 / 221;
+            break;
+        case AR_SQUARE_PICTURE:
+        default:
+            i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
+            break;
+        }
 
-                {
-                    /* Deselect and remove the previous picture */
-                    p_picture->i_flags |= DESTROY_PICTURE;
-                    p_picture->i_flags &= ~DISPLAY_PICTURE;
-                    /* Select the current one */
-                    p_vout->p_stream[p_picture->i_stream].p_next_picture = p_picture;     
-                }
-                /* Else, deselect the picture */
-                else
-                {
-                    p_picture->i_flags &= ~DISPLAY_PICTURE;
-                }
+        /* If picture dimensions using horizontal scaling are too large, use
+         * vertical scaling. Since width must be a multiple of 16, height is
+         * adjusted again after. */
+        if( i_pic_height > i_vout_height )
+        {
+            i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
+                i_vout_height : p_pic->i_height;
+            switch( p_pic->i_aspect_ratio )
+            {
+            case AR_3_4_PICTURE:
+                i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
+                i_pic_height = i_pic_width * 3 / 4;
+                break;
+            case AR_16_9_PICTURE:
+                i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
+                i_pic_height = i_pic_width * 9 / 16;
+                break;
+            case AR_221_1_PICTURE:
+                i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
+                i_pic_height = i_pic_width * 100 / 221;
+                break;
+            case AR_SQUARE_PICTURE:
+            default:
+                i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
+                i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
+                break;
             }
         }
+
+        /* Set picture position */
+        i_pic_x = (p_vout->i_width - i_pic_width) / 2;
+        i_pic_y = (p_vout->i_height - i_pic_height) / 2;
+    }
+    else
+    {
+        /* No picture: size is 0 */
+        i_pic_x =       0;
+        i_pic_y =       0;
+        i_pic_width =   0;
+        i_pic_height =  0;
     }
-}
 
-/*******************************************************************************
- * SortPictures: sort pictures ready for display by hierarchical level
- *******************************************************************************
- * Build an array of pictures in rendering order, starting from minimal to
- * level to maximal level. A NULL pointer always ends the array, which size is
- * limited to VOUT_MAX_PICTURES.
- *******************************************************************************
- * Messages type: vout, major code 26
- *******************************************************************************/
-static void SortPictures( vout_thread_t *p_vout, picture_t **pp_picture )
-{
-    int             i_picture;                /* picture index in sorted array */
-    int             i_heap_picture;                   /* picture index in heap */
-    picture_t *     p_previous_picture;                /* first shift register */
-    picture_t *     p_current_picture;                /* second shift register */
-
-    /* Initialize array */
-    pp_picture[0] = NULL;
-
-    /* Copy and sort pictures */
-    for( i_heap_picture = 0; i_heap_picture < p_vout->i_max_pictures ; i_heap_picture++ )
-    {
-        /* Sort only selected pictures */
-        if( p_vout->p_picture[ i_heap_picture ].i_flags & DISPLAY_PICTURE )
-        {            
-            /* Find picture position */
-            for( i_picture = 0; (pp_picture[i_picture] != NULL) 
-                     && (pp_picture[i_picture]->i_level 
-                         <= p_vout->p_picture[ i_heap_picture ].i_level);
-                 i_picture++ )
-            {
-                ;
-            }
-            p_current_picture = &p_vout->p_picture[ i_heap_picture ];
+    /*
+     * Set new picture size - if is 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) )
+    {
+        i_old_pic_y =            p_buffer->i_pic_y;
+        i_old_pic_height =       p_buffer->i_pic_height;
+        p_buffer->i_pic_x =      i_pic_x;
+        p_buffer->i_pic_y =      i_pic_y;
+        p_buffer->i_pic_width =  i_pic_width;
+        p_buffer->i_pic_height = i_pic_height;
+        SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
+    }
+    else
+    {
+        p_buffer->i_pic_x =      i_pic_x;
+        p_buffer->i_pic_y =      i_pic_y;
+        p_buffer->i_pic_width =  i_pic_width;
+        p_buffer->i_pic_height = i_pic_height;
+    }
 
-            /* Insert picture and shift end of array */
-            for( ; p_previous_picture != NULL; i_picture++ )
-            {
-                p_previous_picture = p_current_picture;
-                p_current_picture = pp_picture[i_picture];
-                if( i_picture == VOUT_MAX_PICTURES - 1 )
-                {
-                    p_previous_picture = NULL;
-                }
-                pp_picture[i_picture] = p_previous_picture; 
-            }
+    /*
+     * Clear areas
+     */
+    for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
+    {
+#ifdef DEBUG_VIDEO
+        intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
+                    p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
+#endif
+        i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
+        p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
+        for( i_data_index = i_data_size / 256; i_data_index-- ; )
+        {
+            /* Clear 256 bytes block */
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+            *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;  *p_data++ = 0;
+        }
+        for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
+        {
+            /* Clear remaining 16 bytes blocks */
+            *p_data++ = 0;  *p_data++ = 0;
+        }
+        p_data8 = (byte_t *)p_data;
+        for( i_data_index = i_data_size % 16; i_data_index--; )
+        {
+            /* Clear remaining bytes */
+            *p_data8++ = 0;
         }
     }
 
+    /*
+     * Clear areas array
+     */
+    p_buffer->i_areas = 0;
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * RenderPicture: render a picture
- *******************************************************************************
+ *****************************************************************************
  * This function convert a picture from a video heap to a pixel-encoded image
  * and copy it to the current rendering buffer. No lock is required, since the
  * rendered picture has been determined as existant, and will only be destroyed
- * by the vout_Thread() later.
- *******************************************************************************
- * Messages types: vout, major code 27
- *******************************************************************************/
+ * by the vout thread later.
+ *****************************************************************************/
 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    int     i_x, i_y;                                   /* position in display */
-    int     i_pic_x;                            /* x offset in clipped picture */
-    int     i_pic_y;                            /* y offset in clipped picture */
-    int     i_height, i_width;                   /* clipped picture dimensions */
-    int     i_line_width;                                        /* line width */
-    void    (*RenderLine)(vout_thread_t *p_vout, picture_t *p_pic, /* renderer */
-                          int i_x, int i_y, int i_pic_x, int i_pic_y, int i_width,
-                          int i_line_width, int i_ratio );
-
-    /* Update counters - this is done now since for blank pictures, the function 
-     * may never reach it's regular end */
-    p_pic->i_flags |= DISPLAYED_PICTURE;
-#ifdef STATS
-    p_vout->c_rendered_pictures++;
-    p_vout->p_stream[ p_pic->i_stream ].c_rendered_pictures++;
-#endif                
-    
-    /* Computes position and size of the picture zone to render */
-    i_x = p_pic->i_x;
-    i_y = p_pic->i_y;
-    i_width = p_pic->i_width;
-    i_height = p_pic->i_height;
-    ClipPicture( &i_x, p_vout->i_width, &i_pic_x, &i_width, p_pic->i_h_ratio, p_pic->i_h_align );
-    ClipPicture( &i_y, p_vout->i_height, &i_pic_y, &i_height, p_pic->i_v_ratio, p_pic->i_v_align );
-#ifdef VOUT_DEBUG
-   intf_DbgMsg("%p -> picture %p, (%d-%d:%d-%d, %dx%d)\n", 
-               p_vout, p_pic, i_x, i_pic_x, i_y, i_pic_y, i_width, i_height );
+#ifdef DEBUG_VIDEO
+    char                psz_date[MSTRTIME_MAX_SIZE];         /* picture date */
+    mtime_t             render_time;               /* picture rendering time */
+#endif
+    vout_buffer_t *     p_buffer;                        /* rendering buffer */
+    byte_t *            p_pic_data;                /* convertion destination */
+
+    /* 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;
+#ifdef DEBUG_VIDEO
+    render_time = mdate();
+#endif
 
+    /*
+     * Choose appropriate rendering function and render picture
+     */
+    switch( p_pic->i_type )
+    {
+    case YUV_420_PICTURE:
+        p_vout->yuv.p_Convert420( p_vout, p_pic_data,
+                                  p_pic->p_y, p_pic->p_u, p_pic->p_v,
+                                  p_pic->i_width, p_pic->i_height,
+                                  p_buffer->i_pic_width, p_buffer->i_pic_height,
+                                  p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
+                                  p_pic->i_matrix_coefficients );
+        break;
+    case YUV_422_PICTURE:
+        p_vout->yuv.p_Convert422( p_vout, p_pic_data,
+                                  p_pic->p_y, p_pic->p_u, p_pic->p_v,
+                                  p_pic->i_width, p_pic->i_height,
+                                  p_buffer->i_pic_width, p_buffer->i_pic_height,
+                                  p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
+                                  p_pic->i_matrix_coefficients );
+        break;
+    case YUV_444_PICTURE:
+        p_vout->yuv.p_Convert444( p_vout, p_pic_data,
+                                  p_pic->p_y, p_pic->p_u, p_pic->p_v,
+                                  p_pic->i_width, p_pic->i_height,
+                                  p_buffer->i_pic_width, p_buffer->i_pic_height,
+                                  p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
+                                  p_pic->i_matrix_coefficients );
+        break;
+#ifdef DEBUG
+    default:
+        intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
+        break;
 #endif
+    }
 
-    /* If there is noting to display, returns immediately */
-    if( (i_pic_x >= i_width) || (i_pic_y >= i_height) )
+#ifdef DEBUG_VIDEO
+    /* Print picture date and rendering time */
+    intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
+                p_vout->i_buffer_index, (long) (mdate() - render_time),
+                mstrtime( psz_date, p_pic->date ));
+#endif
+}
+
+/*****************************************************************************
+ * RenderPictureInfo: print additionnal informations on a picture
+ *****************************************************************************
+ * This function will print informations such as fps and other picture
+ * dependant informations.
+ *****************************************************************************/
+static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
+{
+#if defined(STATS) || defined(DEBUG)
+    char        psz_buffer[256];                            /* string buffer */
+#endif
+
+#ifdef STATS
+    /*
+     * Print FPS rate in upper right corner
+     */
+    if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
     {
-        return;
+        sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
+                 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
+                   p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
+        Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
     }
-    
-    /* Determine method used to render line. This function is choosed here to
-     * avoid multiple conditions tests later */
-    switch( p_pic->i_type )
+
+    /*
+     * Print frames count and loop time in upper left corner
+     */
+    sprintf( psz_buffer, "%ld frames   rendering: %ld us",
+             (long) p_vout->c_fps_samples, (long) p_vout->render_time );
+    Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
+#endif
+
+#ifdef DEBUG
+    /*
+     * Print picture information in lower right corner
+     */
+    sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
+             (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
+             ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
+              ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
+             p_pic->i_width, p_pic->i_height,
+             p_pic->i_display_width, p_pic->i_display_height,
+             p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
+             (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
+             ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
+              ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
+               ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
+             p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
+             p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
+             p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
+             p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
+    Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
+#endif
+}
+
+/*****************************************************************************
+ * RenderIdle: render idle picture
+ *****************************************************************************
+ * This function will print something on the screen. It will return 0 if
+ * nothing has been rendered, or 1 if something has been changed on the screen.
+ * Note that if you absolutely want something to be printed, you will have
+ * to force it by setting the last idle date to 0.
+ * Unlike other rendering functions, this one calls the SetBufferPicture
+ * function when needed.
+ *****************************************************************************/
+static int RenderIdle( vout_thread_t *p_vout )
+{
+    int         i_x = 0, i_y = 0;                           /* text position */
+    int         i_width, i_height;                              /* text size */
+    mtime_t     current_date;                                /* current date */
+    const char *psz_text = "waiting for stream ...";      /* text to display */
+
+
+    current_date = mdate();
+    if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
+        (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
     {
-    case RGB_BLANK_PICTURE:         /* picture is blank, RGB encoded (no data) */
-    case PIXEL_BLANK_PICTURE:     /* picture is blank, pixel encoded (no data) */
-        /* Blank pictures are displayed immediately - dimensions are real ones,
-         * and should be recalculated */
-        switch( p_pic->i_h_ratio )                   
+        SetBufferPicture( p_vout, NULL );
+        vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
+                       &i_width, &i_height );
+        if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
         {
-        case DISPLAY_RATIO_HALF:                              /* 1:2 half size */
-            i_width = (i_width - i_pic_x) / 2;
-            break;     
-        case DISPLAY_RATIO_NORMAL:                          /* 1:1 normal size */
-            i_width -= i_pic_x;
-            break;
-        case DISPLAY_RATIO_DOUBLE:                          /* 2:1 double size */   
-            i_width = (i_width - i_pic_x) * 2;
-            break;
-            /* ?? add others display ratio */
+            vout_Print( p_vout->p_large_font,
+                        p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
+                        i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
+                        p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
+                        p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
+                        WIDE_TEXT | OUTLINED_TEXT, psz_text );
+            SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
         }
-        switch( p_pic->i_v_ratio )                   
+        return( 1 );
+    }
+    return( 0 );
+}
+
+/*****************************************************************************
+ * RenderInfo: render additionnal informations
+ *****************************************************************************
+ * This function render informations which do not depend of the current picture
+ * rendered.
+ *****************************************************************************/
+static void RenderInfo( vout_thread_t *p_vout )
+{
+#ifdef DEBUG
+    char        psz_buffer[256];                            /* string buffer */
+    int         i_ready_pic = 0;                           /* ready pictures */
+    int         i_reserved_pic = 0;                     /* reserved pictures */
+    int         i_picture;                                  /* picture index */
+#endif
+
+#ifdef DEBUG
+    /*
+     * Print thread state in lower left corner
+     */
+    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
+    {
+        switch( p_vout->p_picture[i_picture].i_status )
         {
-        case DISPLAY_RATIO_HALF:                              /* 1:2 half size */
-            i_height = (i_height - i_pic_y) / 2;
-            break;     
-        case DISPLAY_RATIO_NORMAL:                          /* 1:1 normal size */
-            i_height -= i_pic_y;
+        case RESERVED_PICTURE:
+        case RESERVED_DATED_PICTURE:
+        case RESERVED_DISP_PICTURE:
+            i_reserved_pic++;
             break;
-        case DISPLAY_RATIO_DOUBLE:                          /* 2:1 double size */   
-            i_height = (i_height - i_pic_y) * 2;
+        case READY_PICTURE:
+            i_ready_pic++;
             break;
-            /* ?? add others display ratio */
         }
-        if( p_pic->i_type == RGB_BLANK_PICTURE )          /* RGB blank picture */
-        {            
-            p_vout->RenderRGBBlank( p_vout, p_pic->pixel, i_x, i_y, i_width, i_height );
+    }
+    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
+}
+
+/*****************************************************************************
+ * RenderSubPicture: render a subpicture
+ *****************************************************************************
+ * This function render a sub picture unit.
+ *****************************************************************************/
+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 )
+    {
+    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;
         }
-        else                                            /* pixel blank picture */
+
+        /* 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 ) )
         {
-            p_vout->RenderPixelBlank( p_vout, p_pic->pixel, i_x, i_y, i_width, i_height );
-        }        
-        return;
-        break;
-    case RGB_PICTURE:                        /* picture is 24 bits rgb encoded */
-        RenderLine = p_vout->RenderRGBLine;
-        break;
-    case PIXEL_PICTURE:                            /* picture is pixel encoded */
-        RenderLine = p_vout->RenderPixelLine;
-        break;
-    case RGB_MASK_PICTURE:                      /* picture is a 1 rgb bpp mask */
-        RenderLine = p_vout->RenderRGBMaskLine;
-        break;
-    case PIXEL_MASK_PICTURE:                  /* picture is a 1 pixel bpp mask */
-        RenderLine = p_vout->RenderPixelMaskLine;
+            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;
-        /* ?? add YUV types */
+
 #ifdef DEBUG
-    default:                      /* internal error, which should never happen */
-        intf_DbgMsg("%p -> unknown type for picture %p\n", p_vout, p_pic);
-        break;  
-#endif  
+    default:
+        intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
+#endif
     }
+}
 
-    /* For non blank pictures, loop on lines */
-    for( ; i_pic_y < i_height; i_pic_y++ )
+/*****************************************************************************
+ * RenderInterface: render the interface
+ *****************************************************************************
+ * This function render the interface, if any.
+ *****************************************************************************/
+static void RenderInterface( vout_thread_t *p_vout )
+{
+    int         i_height, i_text_height;            /* total and text height */
+    int         i_width_1, i_width_2;                          /* text width */
+    int         i_byte;                                        /* byte index */
+    const char *psz_text_1 = "[1-9] Channel   [i]nfo   [c]olor     [g/G]amma";
+    const char *psz_text_2 = "[+/-] Volume    [m]ute   [s]caling   [Q]uit";
+
+    /* Get text size */
+    vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
+    vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
+    i_height += i_text_height;
+
+    /* Render background */
+    for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
+         i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
+         i_byte++ )
     {
-        /* First step: check if line has to be rendered. This is not obvious since
-         * if display ratio is less than 1:1, some of the lines don't need to
-         * be displayed, and therefore do not need to be rendered. */
-        switch( p_pic->i_v_ratio )
-        {
-        case DISPLAY_RATIO_HALF:                              /* 1:2 half size */
-            /* Only even lines are rendered, and copied once */
-            i_line_width = i_pic_y % 2;
-            break;
+        /* XXX?? noooo ! */
+        p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
+    }
 
-        case DISPLAY_RATIO_NORMAL:                          /* 1:1 normal size */
-            /* All lines are rendered and copied once */
-            i_line_width = 1;
-            break;
+    /* Render text, if not larger than screen */
+    if( i_width_1 < p_vout->i_width )
+    {
+        vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
+                    (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
+                    p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
+                    p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
+                    OUTLINED_TEXT, psz_text_1 );
+    }
+    if( i_width_2 < p_vout->i_width )
+    {
+        vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
+                    (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
+                    p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
+                    p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
+                    OUTLINED_TEXT, psz_text_2 );
+    }
 
-        case DISPLAY_RATIO_DOUBLE:                          /* 2:1 double size */
-            /* All lines are rendered and copied twice */
-            i_line_width = 2;
-            break;
-        }
+    /* Activate modified area */
+    SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
+}
 
-        if( i_line_width )
-        {
-            /* Computed line width can be reduced if it would cause to render 
-             * outside the display */
-            if( i_y + i_line_width > p_vout->i_height )
-            {
-                i_line_width = p_vout->i_height - i_y;
-            }
+/*****************************************************************************
+ * 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;
 
-            /* Second step: render line. Since direct access to the rendering 
-             * buffer is required, functions in charge of rendering the line
-             * are part of the display driver. Because this step is critical
-             * and require high optimization, different methods are used 
-             * depending of the horizontal display ratio, the image type and 
-             * the screen depth. */
-            RenderLine( p_vout, p_pic, i_x, i_y, i_pic_x, i_pic_y,
-                        i_width, i_line_width, p_pic->i_h_ratio );
-            
-            /* Increment display line index */
-            i_y += i_line_width;
+    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;
 }
 
-/*******************************************************************************
- * ClipPicture: clip a picture in display window
- *******************************************************************************
- * This function computes picture placement in display window and rendering
- * zone, according to wished picture placement and display ratio. It must be
- * called twice, once and with the x coordinates and once with the y
- * coordinates. 
- * The pi_pic_ofs parameter is only written, but pi_ofs and pi_pic_size must
- * be valid arguments. Note that *pi_pic_size is still the rendering zone size,
- * starting from picture offset 0 and not the size starting from *pi_pic_ofs
- * (same thing for i_size).
- *******************************************************************************
- * Messages types: vout, major code 28
- *******************************************************************************/
-static void ClipPicture( int *pi_ofs, int i_size, int *pi_pic_ofs, int *pi_pic_size,
-                         int i_ratio, int i_placement )
+/*****************************************************************************
+ * Manage: manage thread
+ *****************************************************************************
+ * This function will handle changes in thread configuration.
+ *****************************************************************************/
+static int Manage( vout_thread_t *p_vout )
 {
-    int i_ofs;                                   /* temporary picture position */
-
-    /* Computes base picture position */
-    switch( i_placement )
+#ifdef DEBUG_VIDEO
+    if( p_vout->i_changes )
     {
-    case -1:                                               /* left/top aligned */
-        i_ofs = *pi_ofs;
-        break;
-    case 0:                                                        /* centered */
-        i_ofs = *pi_ofs + (i_size - *pi_pic_size) / 2;
-        break;
-    case 1:                                            /* right/bottom aligned */
-        i_ofs = *pi_ofs + i_size - *pi_pic_size;
-        break;
-#ifdef DEBUG
-    default:                      /* internal error, which should never happen */
-        intf_DbgMsg("invalid placement\n");
-        break;
-#endif
+        intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
+                    p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
     }
+#endif
 
-    /* Computes base rendering position and update picture position - i_ofs is
-     * the picture position, and can be negative */
-    if( i_ofs < 0 )                       /* picture starts outside the screen */
+    /* On gamma or grayscale change, rebuild tables */
+    if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
+                             VOUT_YUV_CHANGE) )
     {
-        switch( i_ratio )                   
+        if( vout_ResetYUV( p_vout ) )
         {
-        case DISPLAY_RATIO_HALF:                              /* 1:2 half size */
-            *pi_pic_ofs = - *pi_ofs * 2;
-            break;     
-        case DISPLAY_RATIO_NORMAL:                          /* 1:1 normal size */
-            *pi_pic_ofs = -*pi_ofs;
-            break;
-        case DISPLAY_RATIO_DOUBLE:                          /* 2:1 double size */
-            *pi_pic_ofs = -CEIL(*pi_ofs, 2);
-            break;
-#ifdef DEBUG
-        default:                  /* internal error, which should never happen */
-            intf_DbgMsg("unsupported ratio\n");
-            break;  
-#endif  
+            intf_ErrMsg("error: can't rebuild convertion tables\n");
+            return( 1 );
         }
-        *pi_ofs = 0;
     }
-    else                                   /* picture starts inside the screen */
+
+    /* Clear changes flags which does not need management or have been
+     * handled */
+    p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
+                           VOUT_YUV_CHANGE   | VOUT_INFO_CHANGE |
+                           VOUT_INTF_CHANGE  | VOUT_SCALE_CHANGE );
+
+    /* Detect unauthorized changes */
+    if( p_vout->i_changes )
     {
-        *pi_pic_ofs = 0;
-        *pi_ofs = i_ofs;
+        /* 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 );
     }
 
-    /* Computes rendering size - i_ofs is the picture position, and can be 
-     * negative, *pi_ofs is the real picture position, and is always positive */
-    switch( i_ratio )                   
+    return( 0 );
+}
+
+/*****************************************************************************
+ * Align: align a subpicture in the screen
+ *****************************************************************************
+ * This function is used for rendering text or subpictures. It returns non 0
+ * it the final aera is not fully included in display area. Return coordinates
+ * are absolute.
+ *****************************************************************************/
+static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
+                   int i_width, int i_height, int i_h_align, int i_v_align )
+{
+    /* Align horizontally */
+    switch( i_h_align )
     {
-    case DISPLAY_RATIO_HALF:                                  /* 1:2 half size */
-        if( i_ofs + CEIL(*pi_pic_size, 2) > i_size )
-        {
-            *pi_pic_size = ( i_size - i_ofs ) * 2;
-        }
-        break;     
-    case DISPLAY_RATIO_NORMAL:                              /* 1:1 normal size */
-        if( i_ofs + *pi_pic_size > i_size )
-        {
-            *pi_pic_size = i_size - i_ofs;
-        }
+    case CENTER_ALIGN:
+        *pi_x -= i_width / 2;
         break;
-    case DISPLAY_RATIO_DOUBLE:                              /* 2:1 double size */
-        if( *pi_ofs + *pi_pic_size * 2 > i_size )
-        {
-            *pi_pic_size = ( i_size - i_ofs ) / 2;
-        }
+    case CENTER_RALIGN:
+        *pi_x += (p_vout->i_width - i_width) / 2;
         break;
-#ifdef DEBUG
-    default:                  /* internal error, which should never happen */
-        intf_DbgMsg("unsupported ratio\n");
-        break;  
-#endif  
-    }     
+    case RIGHT_ALIGN:
+        *pi_x -= i_width;
+        break;
+    case RIGHT_RALIGN:
+        *pi_x += p_vout->i_width - i_width;
+        break;
+    }
+
+    /* Align vertically */
+    switch( i_v_align )
+    {
+    case CENTER_ALIGN:
+        *pi_y -= i_height / 2;
+        break;
+    case CENTER_RALIGN:
+        *pi_y += (p_vout->i_height - i_height) / 2;
+        break;
+    case BOTTOM_ALIGN:
+        *pi_y -= i_height;
+        break;
+    case BOTTOM_RALIGN:
+        *pi_y += p_vout->i_height - i_height;
+        break;
+    case SUBTITLE_RALIGN:
+        *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
+                  p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
+        break;
+    }
+
+    /* Return non 0 if clipping failed */
+    return( (*pi_x < 0) || (*pi_y < 0) ||
+            (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
+}
+
+/*****************************************************************************
+ * SetPalette: sets an 8 bpp palette
+ *****************************************************************************
+ * This function is just a prototype that does nothing. Architectures that
+ * support palette allocation should override it.
+ *****************************************************************************/
+static void     SetPalette        ( p_vout_thread_t p_vout, u16 *red,
+                                    u16 *green, u16 *blue, u16 *transp )
+{
+    intf_ErrMsg( "SetPalette: method does not support palette changing\n" );
 }