]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_output.c
Removes trailing spaces. Removes tabs.
[vlc] / src / video_output / video_output.c
index a4f2a769e8a0d71d911f66503afc18fa929ddd45..d7a92f8e8d87bd0b4477aa1a8a1b729257894b7b 100644 (file)
@@ -1,12 +1,15 @@
 /*****************************************************************************
  * video_output.c : video output thread
+ *
  * 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 oppened video output thread.
  *****************************************************************************
- * Copyright (C) 2000 VideoLAN
+ * Copyright (C) 2000-2007 the VideoLAN team
+ * $Id$
  *
- * Authors:
+ * Authors: Vincent Seguin <seguin@via.ecp.fr>
+ *          Gildas Bazin <gbazin@videolan.org>
  *
  * 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
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
+#include <vlc/vlc.h>
 
-#include <errno.h>                                                 /* ENOMEM */
 #include <stdlib.h>                                                /* free() */
-#include <stdio.h>                                              /* sprintf() */
-#include <string.h>                                            /* strerror() */
-
-#include "config.h"
-#include "common.h"
-#include "threads.h"
-#include "mtime.h"
-#include "plugins.h"
-#include "video.h"
-#include "video_output.h"
-#include "video_text.h"
-#include "video_spu.h"
-#include "video_yuv.h"
-
-#include "intf_msg.h"
-#include "main.h"
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-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.
- * If pi_status is NULL, then the function will block until the thread is ready.
- * If not, it will be updated using one of the THREAD_* constants.
- *****************************************************************************/
-vout_thread_t * vout_CreateThread   ( char *psz_display, int i_root_window,
-                          int i_width, int i_height, int *pi_status, int i_method )
-{
-    vout_thread_t * p_vout;                             /* thread descriptor */
-    int             i_status;                               /* thread status */
-    int             i_index;               /* index for array initialization */
-    char *          psz_method;
-
-    /* Allocate descriptor */
-    intf_DbgMsg("\n");
-    p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
-    if( p_vout == NULL )
-    {
-        intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
-        return( NULL );
-    }
-
-    /* Request an interface plugin */
-    psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
-
-    if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
-    {
-        intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
-        free( p_vout );
-        return( NULL );
-    }
+#include <string.h>
 
-    /* Get plugins */
-    p_vout->p_sys_create =  GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
-    p_vout->p_sys_init =    GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
-    p_vout->p_sys_end =     GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
-    p_vout->p_sys_destroy = GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
-    p_vout->p_sys_manage =  GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
-    p_vout->p_sys_display = GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
-
-    /* Initialize thread properties - thread id and locks will be initialized
-     * later */
-    p_vout->b_die               = 0;
-    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;
+#ifdef HAVE_SYS_TIMES_H
+#   include <sys/times.h>
 #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++)
-    {
-        p_vout->p_picture[i_index].i_type   =   EMPTY_PICTURE;
-        p_vout->p_picture[i_index].i_status =   FREE_PICTURE;
-    }
-    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
-    {
-        p_vout->p_subpicture[i_index].i_type  = EMPTY_SUBPICTURE;
-        p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
-    }
-    p_vout->i_pictures = 0;
+#include <vlc_vout.h>
+#include <vlc_playlist.h>
 
-    /* Initialize synchronization informations */
-    p_vout->i_synchro_level     = VOUT_SYNCHRO_LEVEL_START;
+#include <vlc_filter.h>
+#include <vlc_osd.h>
 
-    /* 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->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 );
+#if defined( __APPLE__ )
+/* Include darwin_specific.h here if needed */
+#endif
 
-    /* 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( DATA_PATH "/" VOUT_DEFAULT_FONT );
-    if( p_vout->p_default_font == NULL )
-    {
-        p_vout->p_default_font  = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
-    }
-    if( p_vout->p_default_font == NULL )
-    {
-        p_vout->p_sys_destroy( p_vout );
-        TrashPlugin( p_vout->vout_plugin );
-        free( p_vout );
-        return( NULL );
-    }
-    p_vout->p_large_font        = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
-    if( p_vout->p_large_font == NULL )
-    {
-        p_vout->p_large_font    = vout_LoadFont( "share/" VOUT_LARGE_FONT );
-    }
-    if( p_vout->p_large_font == NULL )
-    {
-        vout_UnloadFont( p_vout->p_default_font );
-        p_vout->p_sys_destroy( p_vout );
-        TrashPlugin( p_vout->vout_plugin );
-        free( p_vout );
-        return( NULL );
-    }
+/** FIXME This is quite ugly but needed while we don't have counters
+ * helpers */
+#include "input/input_internal.h"
 
-    /* Create thread and set locks */
-    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("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->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 );
+#include "modules/modules.h"
 
-    /* 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)
-                && (i_status != THREAD_FATAL) );
-        if( i_status != THREAD_READY )
-        {
-            return( NULL );
-        }
-    }
-    return( p_vout );
-}
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int      InitThread        ( vout_thread_t * );
+static void     RunThread         ( vout_thread_t * );
+static void     ErrorThread       ( vout_thread_t * );
+static void     EndThread         ( vout_thread_t * );
+static void     DestroyThread     ( vout_thread_t * );
+
+static void     AspectRatio       ( int, int *, int * );
+static int      BinaryLog         ( uint32_t );
+static void     MaskToShift       ( int *, int *, uint32_t );
+
+/* Object variables callbacks */
+static int DeinterlaceCallback( vlc_object_t *, char const *,
+                                vlc_value_t, vlc_value_t, void * );
+static int FilterCallback( vlc_object_t *, char const *,
+                           vlc_value_t, vlc_value_t, void * );
+static int VideoFilter2Callback( vlc_object_t *, char const *,
+                                 vlc_value_t, vlc_value_t, void * );
+
+/* From vout_intf.c */
+int vout_Snapshot( vout_thread_t *, picture_t * );
+
+/* Video filter2 parsing */
+static int ParseVideoFilter2Chain( vout_thread_t *, char * );
+static void RemoveVideoFilters2( vout_thread_t *p_vout );
+
+/* Display media title in OSD */
+static void DisplayTitleOnOSD( vout_thread_t *p_vout );
 
 /*****************************************************************************
- * vout_DestroyThread: destroys a previously created 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
- * update using one of the THREAD_* constants.
+ * Video Filter2 functions
  *****************************************************************************/
-void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
+struct filter_owner_sys_t
 {
-    int     i_status;                                       /* thread status */
+    vout_thread_t *p_vout;
+};
 
-    /* Set status */
-    intf_DbgMsg("\n");
-    p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
-    *p_vout->pi_status = THREAD_DESTROY;
+static picture_t *video_new_buffer_filter( filter_t *p_filter )
+{
+    picture_t *p_picture;
+    vout_thread_t *p_vout = p_filter->p_owner->p_vout;
 
-    /* Request thread destruction */
-    p_vout->b_die = 1;
+    p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
 
-    /* If status is NULL, wait until thread has been destroyed */
-    if( pi_status == NULL )
-    {
-        do
-        {
-            msleep( THREAD_SLEEP );
-        }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
-                && (i_status != THREAD_FATAL) );
-    }
+    return p_picture;
 }
 
-/*****************************************************************************
- * 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 )
+static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
 {
-#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
-
-    /* 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_DestroyPicture( p_filter->p_owner->p_vout, p_pic );
 }
 
 /*****************************************************************************
- * vout_CreateSubPicture: allocate an subpicture in the video output heap.
+ * vout_Request: find a video output thread, create one, or destroy one.
  *****************************************************************************
- * 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.
+ * This function looks for a video output thread matching the current
+ * properties. If not found, it spawns a new one.
  *****************************************************************************/
-subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
-                                     int i_size )
+vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
+                               video_format_t *p_fmt )
 {
-    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 );
+    if( !p_fmt )
+    {
+        /* Reattach video output to playlist before bailing out */
+        if( p_vout )
+        {
+            playlist_t  *p_playlist = pl_Yield( p_this );
+            spu_Attach( p_vout->p_spu, p_this, VLC_FALSE );
+            vlc_object_detach( p_vout );
+            vlc_object_attach( p_vout, p_playlist );
+            pl_Release( p_this );
+        }
+        return NULL;
+    }
 
-    /*
-     * Look for an empty place
-     */
-    for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
+    /* If a video output was provided, lock it, otherwise look for one. */
+    if( p_vout )
+    {
+        vlc_object_yield( p_vout );
+    }
+    else
     {
-        if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
+        p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
+
+        if( !p_vout )
         {
-            /* 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) )
+            playlist_t *p_playlist = pl_Yield( p_this );
+            vlc_mutex_lock( &p_playlist->gc_lock );
+            p_vout = vlc_object_find( p_playlist,
+                                      VLC_OBJECT_VOUT, FIND_CHILD );
+            /* only first children of p_input for unused vout */
+            if( p_vout && p_vout->p_parent != (vlc_object_t *)p_playlist )
             {
-                /* 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] );
+                vlc_object_release( p_vout );
+                p_vout = NULL;
             }
-            else if( p_destroyed_subpic == NULL )
-            {
-                /* Memory size do not match, but subpicture index will be kept in
-                 * case no other place are left */
-                p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
-            }
-        }
-        else if( (p_free_subpic == NULL) &&
-                 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
-        {
-            /* Subpicture is empty and ready for allocation */
-            p_free_subpic = &p_vout->p_subpicture[i_subpic];
+            if( p_vout )
+                vlc_object_detach( p_vout );    /* Remove it from the GC */
+            vlc_mutex_unlock( &p_playlist->gc_lock );
+            pl_Release( p_this );
         }
     }
 
-    /* If no free subpicture is available, use a destroyed subpicture */
-    if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
+    /* If we now have a video output, check it has the right properties */
+    if( p_vout )
     {
-        /* 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;
-    }
+        char *psz_filter_chain;
+        vlc_value_t val;
 
-    /*
-     * Prepare subpicture
-     */
-    if( p_free_subpic != NULL )
-    {
-        /* Allocate memory */
-        switch( i_type )
+        /* We don't directly check for the "vout-filter" variable for obvious
+         * performance reasons. */
+        if( p_vout->b_filter_change )
         {
-        case TEXT_SUBPICTURE:                             /* text subpicture */
-            p_free_subpic->p_data = malloc( i_size + 1 );
-            break;
-        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
-            p_free_subpic->p_data = malloc( i_size );
-            break;
-#ifdef DEBUG
-        default:
-            intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
-            p_free_subpic->p_data   =  NULL;
-            break;
-#endif
+            var_Get( p_vout, "vout-filter", &val );
+            psz_filter_chain = val.psz_string;
+
+            if( psz_filter_chain && !*psz_filter_chain )
+            {
+                free( psz_filter_chain );
+                psz_filter_chain = NULL;
+            }
+            if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
+            {
+                free( p_vout->psz_filter_chain );
+                p_vout->psz_filter_chain = NULL;
+            }
+
+            if( !psz_filter_chain && !p_vout->psz_filter_chain )
+            {
+                p_vout->b_filter_change = VLC_FALSE;
+            }
+
+            if( psz_filter_chain ) free( psz_filter_chain );
         }
 
-        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;
+        if( ( p_vout->fmt_render.i_width != p_fmt->i_width ) ||
+            ( p_vout->fmt_render.i_height != p_fmt->i_height ) ||
+            ( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ) ||
+            ( p_vout->fmt_render.i_aspect != p_fmt->i_aspect ) ||
+            p_vout->b_filter_change )
+        {
+            /* We are not interested in this format, close this vout */
+            vlc_object_release( p_vout );
+            vout_Destroy( p_vout );
+            p_vout = NULL;
         }
         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 ) );
+            /* This video output is cool! Hijack it. */
+            spu_Attach( p_vout->p_spu, p_this, VLC_TRUE );
+            vlc_object_attach( p_vout, p_this );
+            if( p_vout->b_title_show )
+                DisplayTitleOnOSD( p_vout );
+            vlc_object_release( p_vout );
         }
+    }
 
-#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 );
+    if( !p_vout )
+    {
+        msg_Dbg( p_this, "no usable vout present, spawning one" );
+
+        p_vout = vout_Create( p_this, p_fmt );
     }
 
-    /* No free or destroyed subpicture could be found */
-    intf_DbgMsg( "warning: heap is full\n" );
-    vlc_mutex_unlock( &p_vout->subpicture_lock );
-    return( NULL );
+    return p_vout;
 }
 
 /*****************************************************************************
- * vout_DestroySubPicture: remove a subpicture from the heap
+ * vout_Create: creates a new video output thread
  *****************************************************************************
- * 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.
+ * This function creates a new video output thread, and returns a pointer
+ * to its description. On error, it returns NULL.
  *****************************************************************************/
-void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
+vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
 {
-#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
+    vout_thread_t  * p_vout;                            /* thread descriptor */
+    input_thread_t * p_input_thread;
+    int              i_index;                               /* loop variable */
+    vlc_value_t      val, text;
 
-    p_subpic->i_status = DESTROYED_SUBPICTURE;
+    unsigned int i_width = p_fmt->i_width;
+    unsigned int i_height = p_fmt->i_height;
+    vlc_fourcc_t i_chroma = p_fmt->i_chroma;
+    unsigned int i_aspect = p_fmt->i_aspect;
 
-#ifdef DEBUG_VIDEO
-    intf_DbgMsg("subpicture %p\n", p_subpic);
-#endif
-}
+    config_chain_t *p_cfg;
+    char *psz_parser;
+    char *psz_name;
 
-/*****************************************************************************
- * 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 )
-{
-    vlc_mutex_lock( &p_vout->picture_lock );
-    switch( p_pic->i_status )
+    /* Allocate descriptor */
+    p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
+    if( p_vout == NULL )
     {
-    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
+        msg_Err( p_parent, "out of memory" );
+        return NULL;
     }
 
-#ifdef DEBUG_VIDEO
-    intf_DbgMsg("picture %p\n", p_pic);
-#endif
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
-
-/*****************************************************************************
- * 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 )
-{
-#ifdef DEBUG_VIDEO
-    char        psz_date[MSTRTIME_MAX_SIZE];                         /* date */
-#endif
-
-    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
+    /* Initialize pictures - translation tables and functions
+     * will be initialized later in InitThread */
+    for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
+    {
+        p_vout->p_picture[i_index].pf_lock = NULL;
+        p_vout->p_picture[i_index].pf_unlock = NULL;
+        p_vout->p_picture[i_index].i_status = FREE_PICTURE;
+        p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
+        p_vout->p_picture[i_index].b_slow   = 0;
+    }
+
+    /* No images in the heap */
+    p_vout->i_heap_size = 0;
+
+    /* Initialize the rendering heap */
+    I_RENDERPICTURES = 0;
+
+    vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
+                 p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
+    p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
+    p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
+
+    p_vout->render.i_width    = i_width;
+    p_vout->render.i_height   = i_height;
+    p_vout->render.i_chroma   = i_chroma;
+    p_vout->render.i_aspect   = i_aspect;
+
+    p_vout->render.i_rmask    = 0;
+    p_vout->render.i_gmask    = 0;
+    p_vout->render.i_bmask    = 0;
+
+    p_vout->render.i_last_used_pic = -1;
+    p_vout->render.b_allow_modify_pics = 1;
+
+    /* Zero the output heap */
+    I_OUTPUTPICTURES = 0;
+    p_vout->output.i_width    = 0;
+    p_vout->output.i_height   = 0;
+    p_vout->output.i_chroma   = 0;
+    p_vout->output.i_aspect   = 0;
+
+    p_vout->output.i_rmask    = 0;
+    p_vout->output.i_gmask    = 0;
+    p_vout->output.i_bmask    = 0;
+
+    /* Initialize misc stuff */
+    p_vout->i_changes    = 0;
+    p_vout->f_gamma      = 0;
+    p_vout->b_grayscale  = 0;
+    p_vout->b_info       = 0;
+    p_vout->b_interface  = 0;
+    p_vout->b_scale      = 1;
+    p_vout->b_fullscreen = 0;
+    p_vout->i_alignment  = 0;
+    p_vout->render_time  = 10;
+    p_vout->c_fps_samples = 0;
+    p_vout->b_filter_change = 0;
+    p_vout->pf_control = 0;
+    p_vout->p_parent_intf = 0;
+    p_vout->i_par_num = p_vout->i_par_den = 1;
+
+    /* Initialize locks */
+    vlc_mutex_init( p_vout, &p_vout->picture_lock );
+    vlc_mutex_init( p_vout, &p_vout->change_lock );
+    vlc_mutex_init( p_vout, &p_vout->vfilter_lock );
+
+    /* Mouse coordinates */
+    var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
+    var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
+    var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
+    var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
+    var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
+
+    /* Initialize subpicture unit */
+    p_vout->p_spu = spu_Create( p_vout );
+    spu_Attach( p_vout->p_spu, p_parent, VLC_TRUE );
+
+    /* Attach the new object now so we can use var inheritance below */
+    vlc_object_attach( p_vout, p_parent );
+
+    spu_Init( p_vout->p_spu );
+
+    /* Take care of some "interface/control" related initialisations */
+    vout_IntfInit( p_vout );
+
+    /* If the parent is not a VOUT object, that means we are at the start of
+     * the video output pipe */
+    if( p_parent->i_object_type != VLC_OBJECT_VOUT )
+    {
+        /* Look for the default filter configuration */
+        var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+        var_Get( p_vout, "vout-filter", &val );
+        p_vout->psz_filter_chain = val.psz_string;
+
+        /* Apply video filter2 objects on the first vout */
+        var_Create( p_vout, "video-filter",
+                    VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+        var_Get( p_vout, "video-filter", &val );
+        ParseVideoFilter2Chain( p_vout, val.psz_string );
+        free( val.psz_string );
     }
+    else
+    {
+        /* continue the parent's filter chain */
+        char *psz_tmp;
 
-#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 );
-}
+        /* Ugly hack to jump to our configuration chain */
+        p_vout->psz_filter_chain
+            = ((vout_thread_t *)p_parent)->psz_filter_chain;
+        p_vout->psz_filter_chain
+            = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->psz_filter_chain );
+        config_ChainDestroy( p_cfg );
+        free( psz_tmp );
 
-/*****************************************************************************
- * 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 )
-{
-    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 */
+        /* Create a video filter2 var ... but don't inherit values */
+        var_Create( p_vout, "video-filter", VLC_VAR_STRING );
+        ParseVideoFilter2Chain( p_vout, NULL );
+    }
 
-    /* Get lock */
-    vlc_mutex_lock( &p_vout->picture_lock );
+    var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
+    p_vout->b_vfilter_change = VLC_TRUE;
+    p_vout->i_vfilters = 0;
 
-    /*
-     * Look for an empty place
-     */
-    for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
+    /* Choose the video output module */
+    if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
     {
-        if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
-        {
-            /* Picture is marked for destruction, but is still allocated - note
-             * that if width and type are the same for two pictures, chroma_width
-             * should also be the same */
-            if( (p_vout->p_picture[i_picture].i_type           == i_type)   &&
-                (p_vout->p_picture[i_picture].i_height         == i_height) &&
-                (p_vout->p_picture[i_picture].i_width          == i_width) )
-            {
-                /* Memory size do match : memory will not be reallocated, and function
-                 * 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];
-            }
-        }
-        else if( (p_free_picture == NULL) &&
-                 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
-        {
-            /* Picture is empty and ready for allocation */
-            p_free_picture = &p_vout->p_picture[i_picture];
-        }
+        var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+        var_Get( p_vout, "vout", &val );
+        psz_parser = val.psz_string;
+    }
+    else
+    {
+        psz_parser = strdup( p_vout->psz_filter_chain );
+    }
+
+    /* Create the vout thread */
+    config_ChainCreate( &psz_name, &p_cfg, psz_parser );
+    free( psz_parser );
+    p_vout->p_cfg = p_cfg;
+    p_vout->p_module = module_Need( p_vout,
+        ( p_vout->psz_filter_chain && *p_vout->psz_filter_chain ) ?
+        "video filter" : "video output", psz_name, 0 );
+    free( psz_name );
+
+    if( p_vout->p_module == NULL )
+    {
+        msg_Err( p_vout, "no suitable vout module" );
+        vlc_object_detach( p_vout );
+        vlc_object_destroy( p_vout );
+        return NULL;
+    }
+
+    /* Create a few object variables for interface interaction */
+    var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
+    text.psz_string = _("Deinterlace");
+    var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
+    val.psz_string = (char *)""; text.psz_string = _("Disable");
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+    val.psz_string = (char *)"discard"; text.psz_string = _("Discard");
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+    val.psz_string = (char *)"blend"; text.psz_string = _("Blend");
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+    val.psz_string = (char *)"mean"; text.psz_string = _("Mean");
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+    val.psz_string = (char *)"bob"; text.psz_string = _("Bob");
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+    val.psz_string = (char *)"linear"; text.psz_string = _("Linear");
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+    val.psz_string = (char *)"x"; text.psz_string = (char *)"X";
+    var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
+
+    if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
+    {
+        var_Set( p_vout, "deinterlace", val );
+        if( val.psz_string ) free( val.psz_string );
+    }
+    var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
+
+    var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    text.psz_string = _("Filters");
+    var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
+    var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
+
+    /* Calculate delay created by internal caching */
+    p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
+                                           VLC_OBJECT_INPUT, FIND_ANYWHERE );
+    if( p_input_thread )
+    {
+        p_vout->i_pts_delay = p_input_thread->i_pts_delay;
+        vlc_object_release( p_input_thread );
+    }
+    else
+    {
+        p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
     }
 
-    /* If no free picture is available, use a destroyed picture */
-    if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
+    if( vlc_thread_create( p_vout, "video output", RunThread,
+                           VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
     {
-        /* 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;
+        msg_Err( p_vout, "out of memory" );
+        module_Unneed( p_vout, p_vout->p_module );
+        vlc_object_detach( p_vout );
+        vlc_object_destroy( p_vout );
+        return NULL;
     }
 
-    /*
-     * Prepare picture
-     */
-    if( p_free_picture != NULL )
+    if( p_vout->b_error )
     {
-        /* 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
-        }
+        msg_Err( p_vout, "video output creation failed" );
 
-        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 ) );
-        }
+        /* Make sure the thread is destroyed */
+        vlc_object_kill( p_vout );
+        vlc_thread_join( p_vout );
 
-#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 );
+        vlc_object_detach( p_vout );
+        vlc_object_destroy( p_vout );
+        return NULL;
     }
 
-    /* No free or destroyed picture could be found */
-    intf_DbgMsg( "warning: heap is full\n" );
-    vlc_mutex_unlock( &p_vout->picture_lock );
-    return( NULL );
+    return p_vout;
 }
 
 /*****************************************************************************
- * vout_DestroyPicture: remove a permanent or reserved picture from the heap
+ * vout_Destroy: destroys a previously created video output
  *****************************************************************************
- * This function frees a previously reserved picture or a permanent
- * picture. It is meant to be used when the construction of a picture aborted.
- * Note that the picture will be destroyed even if it is linked !
+ * 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
+ * update using one of the THREAD_* constants.
  *****************************************************************************/
-void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
+void vout_Destroy( vout_thread_t *p_vout )
 {
-   vlc_mutex_lock( &p_vout->picture_lock );
-
-#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
+    vout_thread_t *p_another_vout;
+    playlist_t *p_playlist = pl_Yield( p_vout );
 
-   p_pic->i_status = DESTROYED_PICTURE;
-   p_vout->i_pictures--;
+    /* Request thread destruction */
+    vlc_object_kill( p_vout );
+    vlc_thread_join( p_vout );
+
+    var_Destroy( p_vout, "intf-change" );
+
+    if( p_vout->psz_filter_chain ) free( p_vout->psz_filter_chain );
+
+    config_ChainDestroy( p_vout->p_cfg );
 
-#ifdef DEBUG_VIDEO
-   intf_DbgMsg("picture %p\n", p_pic);
+    /* Free structure */
+    vlc_object_destroy( p_vout );
+#ifndef __APPLE__
+    /* This is a dirty hack for mostly Linux, where there is no way to get the GUI
+       back if you closed it while playing video. This is solved in Mac OS X,
+       where we have this novelty called menubar, that will always allow you access
+       to the applications main functionality. They should try that on linux sometime */
+    p_another_vout = vlc_object_find( p_playlist,
+                                      VLC_OBJECT_VOUT, FIND_ANYWHERE );
+    if( p_another_vout == NULL )
+    {
+        vlc_value_t val;
+        val.b_bool = VLC_TRUE;
+        var_Set( p_playlist, "intf-show", val );
+    }
+    else
+    {
+        vlc_object_release( p_another_vout );
+    }
 #endif
-   vlc_mutex_unlock( &p_vout->picture_lock );
+    vlc_object_release( p_playlist );
 }
 
 /*****************************************************************************
- * vout_LinkPicture: increment reference counter of a picture
+ * InitThread: initialize video output thread
  *****************************************************************************
- * This function increment the reference counter of a picture in the video
- * heap. It needs a lock since several producer threads can access the picture.
+ * 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.
  *****************************************************************************/
-void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
+static int InitThread( vout_thread_t *p_vout )
 {
-    vlc_mutex_lock( &p_vout->picture_lock );
-    p_pic->i_refcount++;
+    int i, i_aspect_x, i_aspect_y;
 
-#ifdef DEBUG_VIDEO
-    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
-#endif
+    vlc_mutex_lock( &p_vout->change_lock );
 
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
+#ifdef STATS
+    p_vout->c_loops = 0;
+#endif
 
-/*****************************************************************************
- * 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->picture_lock );
-    p_pic->i_refcount--;
+    /* Initialize output method, it allocates direct buffers for us */
+    if( p_vout->pf_init( p_vout ) )
+    {
+        vlc_mutex_unlock( &p_vout->change_lock );
+        return VLC_EGENERIC;
+    }
 
-#ifdef DEBUG_VIDEO
-    if( p_pic->i_refcount < 0 )
+    if( !I_OUTPUTPICTURES )
     {
-        intf_DbgMsg("error: refcount < 0\n");
-        p_pic->i_refcount = 0;
+        msg_Err( p_vout, "plugin was unable to allocate at least "
+                         "one direct buffer" );
+        p_vout->pf_end( p_vout );
+        vlc_mutex_unlock( &p_vout->change_lock );
+        return VLC_EGENERIC;
     }
-#endif
 
-    if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
+    if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
     {
-        p_pic->i_status = DESTROYED_PICTURE;
-        p_vout->i_pictures--;
+        msg_Err( p_vout, "plugin allocated too many direct buffers, "
+                         "our internal buffers must have overflown." );
+        p_vout->pf_end( p_vout );
+        vlc_mutex_unlock( &p_vout->change_lock );
+        return VLC_EGENERIC;
     }
 
-#ifdef DEBUG_VIDEO
-    intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
-#endif
+    msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
 
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
+    AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
 
-/*****************************************************************************
- * 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 )
-{
-    /* 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;
-}
+    msg_Dbg( p_vout, "picture in %ix%i (%i,%i,%ix%i), "
+             "chroma %4.4s, ar %i:%i, sar %i:%i",
+             p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
+             p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
+             p_vout->fmt_render.i_visible_width,
+             p_vout->fmt_render.i_visible_height,
+             (char*)&p_vout->fmt_render.i_chroma,
+             i_aspect_x, i_aspect_y,
+             p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den );
 
-/*****************************************************************************
- * 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 )
-{
-    *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 local */
+    AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
 
-/*****************************************************************************
- * 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_log;
+    msg_Dbg( p_vout, "picture user %ix%i (%i,%i,%ix%i), "
+             "chroma %4.4s, ar %i:%i, sar %i:%i",
+             p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
+             p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
+             p_vout->fmt_in.i_visible_width,
+             p_vout->fmt_in.i_visible_height,
+             (char*)&p_vout->fmt_in.i_chroma,
+             i_aspect_x, i_aspect_y,
+             p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
 
-    i_log = 0;
-    if (i & 0xffff0000)
-    {
-        i_log = 16;
-    }
-    if (i & 0xff00ff00)
-    {
-        i_log += 8;
-    }
-    if (i & 0xf0f0f0f0)
+    if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
     {
-        i_log += 4;
+        p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
+            p_vout->output.i_width;
+        p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
+            p_vout->output.i_height;
+        p_vout->fmt_out.i_x_offset =  p_vout->fmt_out.i_y_offset = 0;
+
+        p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
+        p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
     }
-    if (i & 0xcccccccc)
+    if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
     {
-        i_log += 2;
+        p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
+            p_vout->fmt_out.i_height;
+        p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
+            p_vout->fmt_out.i_width;
     }
-    if (i & 0xaaaaaaaa)
-    {
-        i_log++;
+
+    vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
+                 p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
+
+    AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
+
+    msg_Dbg( p_vout, "picture out %ix%i (%i,%i,%ix%i), "
+             "chroma %4.4s, ar %i:%i, sar %i:%i",
+             p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
+             p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
+             p_vout->fmt_out.i_visible_width,
+             p_vout->fmt_out.i_visible_height,
+             (char*)&p_vout->fmt_out.i_chroma,
+             i_aspect_x, i_aspect_y,
+             p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den );
+
+    /* Calculate shifts from system-updated masks */
+    MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
+                 p_vout->output.i_rmask );
+    MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
+                 p_vout->output.i_gmask );
+    MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
+                 p_vout->output.i_bmask );
+
+    /* Check whether we managed to create direct buffers similar to
+     * the render buffers, ie same size and chroma */
+    if( ( p_vout->output.i_width == p_vout->render.i_width )
+     && ( p_vout->output.i_height == p_vout->render.i_height )
+     && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
+    {
+        /* Cool ! We have direct buffers, we can ask the decoder to
+         * directly decode into them ! Map the first render buffers to
+         * the first direct buffers, but keep the first direct buffer
+         * for memcpy operations */
+        p_vout->b_direct = 1;
+
+        for( i = 1; i < VOUT_MAX_PICTURES; i++ )
+        {
+            if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
+                I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
+                p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
+            {
+                /* We have enough direct buffers so there's no need to
+                 * try to use system memory buffers. */
+                break;
+            }
+            PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
+            I_RENDERPICTURES++;
+        }
+
+        msg_Dbg( p_vout, "direct render, mapping "
+                 "render pictures 0-%i to system pictures 1-%i",
+                 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
     }
-    if (i != ((u32)1 << i_log))
+    else
     {
-        intf_ErrMsg("internal error: binary log overflow\n");
-    }
-
-    return( i_log );
-}
+        /* Rats... Something is wrong here, we could not find an output
+         * plugin able to directly render what we decode. See if we can
+         * find a chroma plugin to do the conversion */
+        p_vout->b_direct = 0;
 
-/*****************************************************************************
- * 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 )
-{
-    u32 i_low, i_high;                 /* lower hand higher bits of the mask */
+        /* Choose the best module */
+        p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 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 */
+        if( p_vout->chroma.p_module == NULL )
+        {
+            msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
+                     (char*)&p_vout->render.i_chroma,
+                     (char*)&p_vout->output.i_chroma );
+            p_vout->pf_end( p_vout );
+            vlc_mutex_unlock( &p_vout->change_lock );
+            return VLC_EGENERIC;
+        }
 
-    /* Transform bits into an index */
-    i_low =  BinaryLog (i_low);
-    i_high = BinaryLog (i_high);
+        msg_Dbg( p_vout, "indirect render, mapping "
+                 "render pictures 0-%i to system pictures %i-%i",
+                 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
+                 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
 
-    /* Update pointers and return */
-    *pi_left =   i_low;
-    *pi_right = (8 - i_high + i_low);
-}
+        /* Append render buffers after the direct buffers */
+        for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
+        {
+            PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
+            I_RENDERPICTURES++;
 
-/*****************************************************************************
- * 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 )
-{
-    /* Update status */
-    intf_DbgMsg("\n");
-    *p_vout->pi_status = THREAD_START;
+            /* Check if we have enough render pictures */
+            if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
+                break;
+        }
+    }
 
-   /* Initialize output method - this function issues its own error messages */
-    if( p_vout->p_sys_init( p_vout ) )
+    /* Link pictures back to their heap */
+    for( i = 0 ; i < I_RENDERPICTURES ; i++ )
     {
-        return( 1 );
+        PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
     }
 
-    /* Initialize convertion tables and functions */
-    if( vout_InitYUV( p_vout ) )
+    for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
     {
-        intf_ErrMsg("error: can't allocate YUV translation tables\n");
-        return( 1 );
+        PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
     }
 
-    /* Mark thread as running and return */
-    p_vout->b_active =          1;
-    *p_vout->pi_status =        THREAD_READY;
-    intf_DbgMsg("thread ready\n");
-    return( 0 );
+/* XXX XXX mark thread ready */
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -922,244 +723,372 @@ static int InitThread( vout_thread_t *p_vout )
  *****************************************************************************/
 static void RunThread( vout_thread_t *p_vout)
 {
-    /* XXX?? welcome to gore land */
-    static int i_trash_count = 0;
-    static mtime_t last_display_date = 0;
-
     int             i_index;                                /* index in heap */
+    int             i_idle_loops = 0;  /* loops without displaying a picture */
     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 */
+
+    picture_t *     p_picture;                            /* picture pointer */
+    picture_t *     p_last_picture = NULL;                   /* last picture */
+    picture_t *     p_directbuffer;              /* direct buffer to display */
+
+    subpicture_t *  p_subpic = NULL;                   /* subpicture pointer */
+
+    input_thread_t *p_input = NULL ;           /* Parent input, if it exists */
+
+    vlc_value_t     val;
+    vlc_bool_t      b_drop_late;
+
+    int             i_displayed = 0, i_lost = 0, i_loops = 0;
 
     /*
      * Initialize thread
      */
     p_vout->b_error = InitThread( p_vout );
+
+    var_Create( p_vout, "drop-late-frames", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
+    var_Get( p_vout, "drop-late-frames", &val );
+    b_drop_late = val.b_bool;
+
+    /* signal the creation of the vout */
+    vlc_thread_ready( p_vout );
+
     if( p_vout->b_error )
     {
-        DestroyThread( p_vout, THREAD_ERROR );
+        /* Destroy thread structures allocated by Create and InitThread */
+        DestroyThread( p_vout );
         return;
     }
-    intf_DbgMsg("\n");
+
+    if( p_vout->b_title_show )
+        DisplayTitleOnOSD( p_vout );
 
     /*
-     * Main loop - it is not executed if an error occured during
+     * Main loop - it is not executed if an error occurred during
      * initialization
      */
     while( (!p_vout->b_die) && (!p_vout->b_error) )
     {
         /* Initialize loop variables */
-        p_pic =         NULL;
-        p_subpic =      NULL;
-        display_date =  0;
-        current_date =  mdate();
+        p_picture = NULL;
+        display_date = 0;
+        current_date = mdate();
+
+        if( p_input && p_input->b_die )
+        {
+            vlc_object_release( p_input );
+            p_input = NULL;
+        }
+
+        i_loops++;
+        if( i_loops % 20 == 0 )
+        {
+            if( !p_input )
+            {
+                p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
+                                           FIND_PARENT );
+            }
+            if( p_input )
+            {
+                vlc_mutex_lock( &p_input->p->counters.counters_lock );
+                stats_UpdateInteger( p_vout, p_input->p->counters.p_lost_pictures,
+                                     i_lost , NULL);
+                stats_UpdateInteger( p_vout,
+                                     p_input->p->counters.p_displayed_pictures,
+                                     i_displayed , NULL);
+                i_displayed = i_lost = 0;
+                vlc_mutex_unlock( &p_input->p->counters.counters_lock );
+            }
+        }
+#if 0
+        p_vout->c_loops++;
+        if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
+        {
+            msg_Dbg( p_vout, "picture heap: %d/%d",
+                     I_RENDERPICTURES, p_vout->i_heap_size );
+        }
+#endif
 
         /*
-         * 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++ )
+         * Find the picture to display (the one with the earliest date).
+         * This operation does not need lock, since only READY_PICTUREs
+         * are handled. */
+        for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
         {
-            if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
-            ( (p_pic == NULL) ||
-              (p_vout->p_picture[i_index].date < display_date) ) )
+            if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
+                && ( (p_picture == NULL) ||
+                     (PP_RENDERPICTURE[i_index]->date < display_date) ) )
             {
-                p_pic = &p_vout->p_picture[i_index];
-                display_date = p_pic->date;
+                p_picture = PP_RENDERPICTURE[i_index];
+                display_date = p_picture->date;
             }
         }
 
-        if( p_pic )
+        if( p_picture )
         {
-#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 )
+            /* If we met the last picture, parse again to see whether there is
+             * a more appropriate one. */
+            if( p_picture == p_last_picture )
             {
-                /* Picture is late: it will be destroyed and the thread
-                 * will sleep and go to next picture */
+                for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
+                {
+                    if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
+                        && (PP_RENDERPICTURE[i_index] != p_last_picture)
+                        && ((p_picture == p_last_picture) ||
+                            (PP_RENDERPICTURE[i_index]->date < display_date)) )
+                    {
+                        p_picture = PP_RENDERPICTURE[i_index];
+                        display_date = p_picture->date;
+                    }
+                }
+            }
 
+            /* If we found better than the last picture, destroy it */
+            if( p_last_picture && p_picture != p_last_picture )
+            {
                 vlc_mutex_lock( &p_vout->picture_lock );
-                if( p_pic->i_refcount )
+                if( p_last_picture->i_refcount )
                 {
-                    p_pic->i_status = DISPLAYED_PICTURE;
+                    p_last_picture->i_status = DISPLAYED_PICTURE;
                 }
                 else
                 {
-                    p_pic->i_status = DESTROYED_PICTURE;
-                    p_vout->i_pictures--;
+                    p_last_picture->i_status = DESTROYED_PICTURE;
+                    p_vout->i_heap_size--;
                 }
-                intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
                 vlc_mutex_unlock( &p_vout->picture_lock );
+                p_last_picture = NULL;
+            }
 
-                /* Update synchronization information as if display delay
-                 * was 0 */
-                Synchronize( p_vout, display_date - current_date );
+            /* Compute FPS rate */
+            p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
+                = display_date;
 
-                p_pic =         NULL;
-                display_date =  0;
-                i_trash_count = 0;
+            if( !p_picture->b_force &&
+                p_picture != p_last_picture &&
+                display_date < current_date + p_vout->render_time &&
+                b_drop_late )
+            {
+                /* Picture is late: it will be destroyed and the thread
+                 * will directly choose the next picture */
+                vlc_mutex_lock( &p_vout->picture_lock );
+                if( p_picture->i_refcount )
+                {
+                    /* Pretend we displayed the picture, but don't destroy
+                     * it since the decoder might still need it. */
+                    p_picture->i_status = DISPLAYED_PICTURE;
+                }
+                else
+                {
+                    /* Destroy the picture without displaying it */
+                    p_picture->i_status = DESTROYED_PICTURE;
+                    p_vout->i_heap_size--;
+                }
+                msg_Warn( p_vout, "late picture skipped ("I64Fd")",
+                                  current_date - display_date );
+                i_lost++;
+                vlc_mutex_unlock( &p_vout->picture_lock );
+
+                continue;
             }
-            else
-#endif
-                if( display_date > current_date + VOUT_DISPLAY_DELAY )
+
+            if( display_date >
+                current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
+            {
+                /* Picture is waaay too early: it will be destroyed */
+                vlc_mutex_lock( &p_vout->picture_lock );
+                if( p_picture->i_refcount )
+                {
+                    /* Pretend we displayed the picture, but don't destroy
+                     * it since the decoder might still need it. */
+                    p_picture->i_status = DISPLAYED_PICTURE;
+                }
+                else
+                {
+                    /* Destroy the picture without displaying it */
+                    p_picture->i_status = DESTROYED_PICTURE;
+                    p_vout->i_heap_size--;
+                }
+                i_lost++;
+                msg_Warn( p_vout, "vout warning: early picture skipped "
+                          "("I64Fd")", display_date - current_date
+                          - p_vout->i_pts_delay );
+                vlc_mutex_unlock( &p_vout->picture_lock );
+
+                continue;
+            }
+
+            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;
+                p_picture    = NULL;
+                display_date = 0;
             }
-            else
+            else if( p_picture == p_last_picture )
             {
-                /* Picture will be displayed, update synchronization
-                 * information */
-                Synchronize( p_vout, display_date - current_date );
+                /* We are asked to repeat the previous picture, but we first
+                 * wait for a couple of idle loops */
+                if( i_idle_loops < 4 )
+                {
+                    p_picture    = NULL;
+                    display_date = 0;
+                }
+                else
+                {
+                    /* We set the display date to something high, otherwise
+                     * we'll have lots of problems with late pictures */
+                    display_date = current_date + p_vout->render_time;
+                }
             }
         }
-        /*
-         * Find the subpictures to display - this operation does not need
-         * lock, since only READY_SUBPICTURE are handled. If no picture
-         * has been selected, display_date will depend on the subpicture.
-         * We get an easily parsable chained list of subpictures which
-         * ends with NULL since p_subpic was initialized to NULL.
-         */
-        for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
+
+        if( p_picture == NULL )
         {
-            if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
-            {
-                p_vout->p_subpicture[i_index].p_next = p_subpic;
-                p_subpic = &p_vout->p_subpicture[i_index];
-            }
+            i_idle_loops++;
         }
 
-        /*
-         * Perform rendering, sleep and display rendered picture
-         */
-        if( p_pic )                        /* picture and perhaps subpicture */
+        /* Video Filter2 stuff */
+        if( p_vout->b_vfilter_change == VLC_TRUE )
         {
-            b_display = p_vout->b_active;
-            p_vout->last_display_date = display_date;
-
-            if( b_display )
+            int i;
+            vlc_mutex_lock( &p_vout->vfilter_lock );
+            RemoveVideoFilters2( p_vout );
+            for( i = 0; i < p_vout->i_vfilters_cfg; i++ )
             {
-                /* Set picture dimensions and clear buffer */
-                SetBufferPicture( p_vout, p_pic );
+                filter_t *p_vfilter =
+                    p_vout->pp_vfilters[p_vout->i_vfilters] =
+                        vlc_object_create( p_vout, VLC_OBJECT_FILTER );
+
+                vlc_object_attach( p_vfilter, p_vout );
+
+                p_vfilter->pf_vout_buffer_new = video_new_buffer_filter;
+                p_vfilter->pf_vout_buffer_del = video_del_buffer_filter;
 
-                /* Render picture and informations */
-                RenderPicture( p_vout, p_pic );
-                if( p_vout->b_info )
+                if( !p_vout->i_vfilters )
                 {
-                    RenderPictureInfo( p_vout, p_pic );
-                    RenderInfo( p_vout );
+                    p_vfilter->fmt_in.video = p_vout->fmt_render;
                 }
-            }
+                else
+                {
+                    p_vfilter->fmt_in.video = (p_vfilter-1)->fmt_out.video;
+                }
+                /* TODO: one day filters in the middle of the chain might
+                 * have a different fmt_out.video than fmt_render ... */
+                p_vfilter->fmt_out.video = p_vout->fmt_render;
 
-            /* Remove picture from heap */
-            vlc_mutex_lock( &p_vout->picture_lock );
-            if( p_pic->i_refcount )
-            {
-                p_pic->i_status = DISPLAYED_PICTURE;
-            }
-            else
-            {
-                p_pic->i_status = DESTROYED_PICTURE;
-                p_vout->i_pictures--;
-            }
-            vlc_mutex_unlock( &p_vout->picture_lock );
+                p_vfilter->p_cfg = p_vout->p_vfilters_cfg[i];
+                p_vfilter->p_module = module_Need( p_vfilter, "video filter2",
+                                                   p_vout->psz_vfilters[i],
+                                                   VLC_TRUE );
 
-            /* Render interface and subpicture */
-            if( b_display && p_vout->b_interface )
-            {
-                RenderInterface( p_vout );
-            }
-            if( p_subpic )
-            {
-                if( b_display )
+                if( p_vfilter->p_module )
                 {
-                    RenderSubPicture( p_vout, p_subpic );
+                    p_vfilter->p_owner =
+                        malloc( sizeof( filter_owner_sys_t ) );
+                    p_vfilter->p_owner->p_vout = p_vout;
+                    p_vout->i_vfilters++;
+                    msg_Dbg( p_vout, "video filter found (%s)",
+                             p_vout->psz_vfilters[i] );
+                }
+                else
+                {
+                    msg_Err( p_vout, "no video filter found (%s)",
+                             p_vout->psz_vfilters[i] );
+                    vlc_object_detach( p_vfilter );
+                    vlc_object_destroy( p_vfilter );
                 }
-
-                /* Remove subpicture from heap */
-                /*vlc_mutex_lock( &p_vout->subpicture_lock );
-                p_subpic->i_status = DESTROYED_SUBPICTURE;
-                vlc_mutex_unlock( &p_vout->subpicture_lock );*/
             }
-
+            p_vout->b_vfilter_change = VLC_FALSE;
+            vlc_mutex_unlock( &p_vout->vfilter_lock );
         }
-#if 0
-        else if( p_subpic )                              /* subpicture alone */
-        {
-            b_display = p_vout->b_active;
-            p_vout->last_display_date = display_date;
 
-            if( b_display )
+        if( p_picture )
+        {
+            int i;
+            for( i = 0; i < p_vout->i_vfilters; i++ )
             {
-                /* Clear buffer */
-                SetBufferPicture( p_vout, NULL );
-
-                /* Render informations, interface and subpicture */
-                if( p_vout->b_info )
+                picture_t *p_old = p_picture;
+                p_picture  = p_vout->pp_vfilters[i]->pf_video_filter(
+                                 p_vout->pp_vfilters[i], p_picture );
+                if( !p_picture )
                 {
-                    RenderInfo( p_vout );
+                    break;
                 }
-                if( p_vout->b_interface )
+                /* FIXME: this is kind of wrong
+                 * if you have 2 or more vfilters and the 2nd breaks,
+                 * on the next loop the 1st one will be applied again */
+
+                /* if p_old and p_picture are the same (ie the filter
+                 * worked on the old picture), then following code is
+                 * still alright since i_status gets changed back to
+                 * the right value */
+                if( p_old->i_refcount )
                 {
-                    RenderInterface( p_vout );
+                    p_old->i_status = DISPLAYED_PICTURE;
                 }
-                RenderSubPicture( p_vout, p_subpic );
+                else
+                {
+                    p_old->i_status = DESTROYED_PICTURE;
+                }
+                p_picture->i_status = READY_PICTURE;
             }
+        }
 
-            /* Remove subpicture from heap */
-            /*vlc_mutex_lock( &p_vout->subpicture_lock );
-            p_subpic->i_status = DESTROYED_SUBPICTURE;
-            vlc_mutex_unlock( &p_vout->subpicture_lock );*/
+        if( p_picture && p_vout->b_snapshot )
+        {
+            p_vout->b_snapshot = VLC_FALSE;
+            vout_Snapshot( p_vout, p_picture );
         }
-#endif
-        else if( p_vout->b_active )        /* idle or interface screen alone */
+
+        /*
+         * Check for subpictures to display
+         */
+        if( display_date > 0 )
         {
-            if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
+            if( !p_input )
             {
-                /* Interface has changed, so a new rendering is required - force
-                 * it by setting last idle date to 0 */
-                p_vout->last_idle_date = 0;
+                p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
+                                           FIND_PARENT );
             }
+            p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date,
+            p_input ? var_GetBool( p_input, "state" ) == PAUSE_S : VLC_FALSE );
+        }
 
-            /* 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 );
-                }
-            }
+        /*
+         * Perform rendering
+         */
+        i_displayed++;
+        p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
 
-        }
-        else
+        /*
+         * Call the plugin-specific rendering method if there is one
+         */
+        if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
         {
-            b_display = 0;
+            /* Render the direct buffer returned by vout_RenderPicture */
+            p_vout->pf_render( p_vout, p_directbuffer );
         }
 
         /*
-         * Sleep, wake up and display rendered picture
+         * Sleep, wake up
          */
-
-#ifdef STATS
-        /* Store render time */
-        p_vout->render_time = mdate() - current_date;
-#endif
+        if( display_date != 0 && p_directbuffer != NULL )
+        {
+            mtime_t current_render_time = mdate() - current_date;
+            /* if render time is very large we don't include it in the mean */
+            if( current_render_time < p_vout->render_time +
+                VOUT_DISPLAY_DELAY )
+            {
+                /* Store render time using a sliding mean weighting to
+                 * current value in a 3 to 1 ratio*/
+                p_vout->render_time *= 3;
+                p_vout->render_time += current_render_time;
+                p_vout->render_time >>= 2;
+            }
+        }
 
         /* Give back change lock */
         vlc_mutex_unlock( &p_vout->change_lock );
@@ -1167,36 +1096,116 @@ last_display_date = display_date;
         /* Sleep a while or until a given date */
         if( display_date != 0 )
         {
-            mwait( display_date );
+            /* If there are filters in the chain, better give them the picture
+             * in advance */
+            if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
+            {
+                mwait( display_date - VOUT_MWAIT_TOLERANCE );
+            }
         }
         else
         {
             msleep( VOUT_IDLE_SLEEP );
         }
 
-        /* On awakening, take back lock and send immediately picture to display,
-         * then swap buffers */
+        /* On awakening, take back lock and send immediately picture
+         * to display. */
         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) )
+
+        /*
+         * Display the previously rendered picture
+         */
+        if( p_picture != NULL && p_directbuffer != NULL )
+        {
+            /* Display the direct buffer returned by vout_RenderPicture */
+            if( p_vout->pf_display )
+            {
+                p_vout->pf_display( p_vout, p_directbuffer );
+            }
+
+            /* Tell the vout this was the last picture and that it does not
+             * need to be forced anymore. */
+            p_last_picture = p_picture;
+            p_last_picture->b_force = 0;
+        }
+
+        if( p_picture != NULL )
         {
-            p_vout->p_sys_display( p_vout );
-            p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
+            /* Reinitialize idle loop count */
+            i_idle_loops = 0;
         }
 
         /*
          * Check events and manage thread
          */
-        if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
+        if( p_vout->pf_manage && p_vout->pf_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. */
+            /* A fatal error occurred, and the thread must terminate
+             * immediately, without displaying anything - setting b_error to 1
+             * causes the immediate end of the main while() loop. */
             p_vout->b_error = 1;
         }
+
+        if( p_vout->i_changes & VOUT_SIZE_CHANGE )
+        {
+            /* this must only happen when the vout plugin is incapable of
+             * rescaling the picture itself. In this case we need to destroy
+             * the current picture buffers and recreate new ones with the right
+             * dimensions */
+            int i;
+
+            p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
+
+            p_vout->pf_end( p_vout );
+            for( i = 0; i < I_OUTPUTPICTURES; i++ )
+                 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
+
+            I_OUTPUTPICTURES = 0;
+            if( p_vout->pf_init( p_vout ) )
+            {
+                msg_Err( p_vout, "cannot resize display" );
+                /* FIXME: pf_end will be called again in EndThread() */
+                p_vout->b_error = 1;
+            }
+
+            /* Need to reinitialise the chroma plugin */
+            if( p_vout->chroma.p_module )
+            {
+                if( p_vout->chroma.p_module->pf_deactivate )
+                    p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
+                p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
+            }
+        }
+
+        if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
+        {
+            /* This happens when the picture buffers need to be recreated.
+             * This is useful on multimonitor displays for instance.
+             *
+             * Warning: This only works when the vout creates only 1 picture
+             * buffer!! */
+            p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
+
+            if( !p_vout->b_direct )
+            {
+                module_Unneed( p_vout, p_vout->chroma.p_module );
+            }
+
+            vlc_mutex_lock( &p_vout->picture_lock );
+
+            p_vout->pf_end( p_vout );
+
+            I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
+
+            p_vout->b_error = InitThread( p_vout );
+
+            vlc_mutex_unlock( &p_vout->picture_lock );
+        }
+    }
+
+    if( p_input )
+    {
+        vlc_object_release( p_input );
     }
 
     /*
@@ -1209,21 +1218,21 @@ last_display_date = display_date;
 
     /* End of thread */
     EndThread( p_vout );
-    DestroyThread( p_vout, THREAD_OVER );
-    intf_DbgMsg( "thread end\n" );
+
+    /* Destroy thread structures allocated by CreateThread */
+    DestroyThread( p_vout );
 }
 
 /*****************************************************************************
  * 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.
+ * This function is called when an error occurred 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 )
 {
     /* Wait until a `die' order */
-    intf_DbgMsg("\n");
     while( !p_vout->b_die )
     {
         /* Sleep a while */
@@ -1235,35 +1244,48 @@ static void ErrorThread( vout_thread_t *p_vout )
  * EndThread: thread destruction
  *****************************************************************************
  * This function is called when the thread ends after a sucessful
- * initialization. It frees all ressources allocated by InitThread.
+ * initialization. It frees all resources allocated by InitThread.
  *****************************************************************************/
 static void EndThread( vout_thread_t *p_vout )
 {
     int     i_index;                                        /* index in heap */
 
-    /* Store status */
-    intf_DbgMsg("\n");
-    *p_vout->pi_status = THREAD_END;
+#ifdef STATS
+    {
+        struct tms cpu_usage;
+        times( &cpu_usage );
+
+        msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
+                 cpu_usage.tms_utime, cpu_usage.tms_stime );
+    }
+#endif
 
-    /* Destroy all remaining pictures and subpictures */
-    for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
+    if( !p_vout->b_direct )
     {
-        if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
-        {
-            free( p_vout->p_picture[i_index].p_data );
-        }
+        module_Unneed( p_vout, p_vout->chroma.p_module );
     }
-    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
+
+    /* Destroy all remaining pictures */
+    for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
     {
-        if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
+        if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
         {
-            free( p_vout->p_subpicture[i_index].p_data );
+            free( p_vout->p_picture[i_index].p_data_orig );
         }
     }
 
+    /* Destroy subpicture unit */
+    spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), VLC_FALSE );
+    spu_Destroy( p_vout->p_spu );
+
+    /* Destroy the video filters2 */
+    RemoveVideoFilters2( p_vout );
+
     /* Destroy translation tables */
-    vout_EndYUV( p_vout );
-    p_vout->p_sys_end( p_vout );
+    p_vout->pf_end( p_vout );
+
+    /* Release the change lock */
+    vlc_mutex_unlock( &p_vout->change_lock );
 }
 
 /*****************************************************************************
@@ -1272,862 +1294,408 @@ static void EndThread( vout_thread_t *p_vout )
  * 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 *pi_status;                                         /* status adress */
-
-    /* Store status adress */
-    intf_DbgMsg("\n");
-    pi_status = p_vout->pi_status;
-
-    /* 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 );
-
-    /* Close plugin */
-    TrashPlugin( p_vout->vout_plugin );
-
-    /* Free structure */
-    free( p_vout );
-    *pi_status = i_status;
-}
-
-/*****************************************************************************
- * 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 )
+static void DestroyThread( vout_thread_t *p_vout )
 {
-    int                 i_text_height;                  /* total text height */
-    int                 i_text_width;                    /* total text width */
+    /* Destroy the locks */
+    vlc_mutex_destroy( &p_vout->picture_lock );
+    vlc_mutex_destroy( &p_vout->change_lock );
+    vlc_mutex_destroy( &p_vout->vfilter_lock );
 
-    /* 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 ) )
+    /* Release the module */
+    if( p_vout && p_vout->p_module )
     {
-        /* 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 );
+        module_Unneed( p_vout, p_vout->p_module );
     }
 }
 
-/*****************************************************************************
- * 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 */
-
-    /* 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;
-
-    /*
-     * 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) )
-    {
-        i_area_begin =  p_buffer->i_pic_y;
-        i_area_end =    i_area_begin + p_buffer->i_pic_height - 1;
+/* following functions are local */
 
-        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)) )
-        {
-            /* 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;
-        }
-    }
+static int ReduceHeight( int i_ratio )
+{
+    int i_dummy = VOUT_ASPECT_FACTOR;
+    int i_pgcd  = 1;
 
-    /* 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_ratio )
     {
-        ;
+        return i_pgcd;
     }
 
-    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 )
-        {
-            p_buffer->pi_area_begin[i_area] = i_y;
-            p_buffer->pi_area_end[i_area] = i_h;
-            p_buffer->i_areas++;
-        }
-        else
-        {
-#ifdef DEBUG_VIDEO
-            intf_DbgMsg("areas overflow\n");
-#endif
-            p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
-        }
-    }
-    else
+    /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
+    while( !(i_ratio & 1) && !(i_dummy & 1) )
     {
-        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 )
-        {
-            if( i_h >= i_area_begin - 1 )
-            {
-                /* Extend area above */
-                p_buffer->pi_area_begin[i_area] = i_y;
-            }
-            else
-            {
-                /* Create a new area above : merge last area if overflow, then
-                 * move all old areas down */
-                if( p_buffer->i_areas == VOUT_MAX_AREAS )
-                {
-#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_buffer->i_areas++;
-                }
-                for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
-                {
-                    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;
-            }
-        }
-        if( i_h > i_area_end )
-        {
-            /* 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++ )
-            {
-                ;
-            }
-            i_area_copy--;
-
-            if( i_area_copy != i_area )
-            {
-                /* 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];
-                }
-            }
-            else
-            {
-                /* Extend area below */
-                p_buffer->pi_area_end[i_area] = i_h;
-            }
-        }
+        i_ratio >>= 1;
+        i_dummy >>= 1;
+        i_pgcd  <<= 1;
     }
-}
-
-/*****************************************************************************
- * 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 )
-{
-    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;
-
-    /*
-     * Computes new picture size
-     */
-    if( p_pic != NULL )
-    {
-        /* 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 )
-        {
-        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;
-        }
-
-        /* 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
+    while( !(i_ratio % 3) && !(i_dummy % 3) )
     {
-        /* No picture: size is 0 */
-        i_pic_x =       0;
-        i_pic_y =       0;
-        i_pic_width =   0;
-        i_pic_height =  0;
+        i_ratio /= 3;
+        i_dummy /= 3;
+        i_pgcd  *= 3;
     }
 
-    /*
-     * 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
+    while( !(i_ratio % 5) && !(i_dummy % 5) )
     {
-        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;
+        i_ratio /= 5;
+        i_dummy /= 5;
+        i_pgcd  *= 5;
     }
 
-    /*
-     * 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;
-        }
-    }
+    return i_pgcd;
+}
 
-    /*
-     * Clear areas array
-     */
-    p_buffer->i_areas = 0;
+static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
+{
+    unsigned int i_pgcd = ReduceHeight( i_aspect );
+    *i_aspect_x = i_aspect / i_pgcd;
+    *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
 }
 
 /*****************************************************************************
- * RenderPicture: render a picture
+ * BinaryLog: computes the base 2 log of a binary value
  *****************************************************************************
- * 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.
+ * This functions is used by MaskToShift, to get a bit index from a binary
+ * value.
  *****************************************************************************/
-static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
+static int BinaryLog( uint32_t i )
 {
-#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
+    int i_log = 0;
 
-    /*
-     * 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( i == 0 ) return -31337;
 
-#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
+    if( i & 0xffff0000 ) 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 += 1;
+
+    return i_log;
 }
 
 /*****************************************************************************
- * RenderPictureInfo: print additionnal informations on a picture
+ * MaskToShift: transform a color mask into right and left shifts
  *****************************************************************************
- * This function will print informations such as fps and other picture
- * dependant informations.
+ * This function is used for obtaining color shifts from masks.
  *****************************************************************************/
-static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
+static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
 {
-#if defined(STATS) || defined(DEBUG)
-    char        psz_buffer[256];                            /* string buffer */
-#endif
+    uint32_t i_low, i_high;            /* lower hand higher bits of the mask */
 
-#ifdef STATS
-    /*
-     * Print FPS rate in upper right corner
-     */
-    if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
+    if( !i_mask )
     {
-        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 );
+        *pi_left = *pi_right = 0;
+        return;
     }
 
-    /*
-     * 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
-}
+    /* Get bits */
+    i_low = i_high = i_mask;
 
-/*****************************************************************************
- * 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 */
+    i_low &= - (int32_t)i_low;          /* lower bit of the mask */
+    i_high += i_low;                    /* higher bit of the mask */
 
+    /* Transform bits into an index. Also deal with i_high overflow, which
+     * is faster than changing the BinaryLog code to handle 64 bit integers. */
+    i_low =  BinaryLog (i_low);
+    i_high = i_high ? BinaryLog (i_high) : 32;
 
-    current_date = mdate();
-    if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
-        (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
-    {
-        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 ) )
-        {
-            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 );
-        }
-        return( 1 );
-    }
-    return( 0 );
+    /* Update pointers and return */
+    *pi_left =   i_low;
+    *pi_right = (8 - i_high + i_low);
 }
 
 /*****************************************************************************
- * RenderInfo: render additionnal informations
- *****************************************************************************
- * This function render informations which do not depend of the current picture
- * rendered.
+ * vout_VarCallback: generic callback for intf variables
  *****************************************************************************/
-static void RenderInfo( vout_thread_t *p_vout )
+int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
+                      vlc_value_t oldval, vlc_value_t newval,
+                      void *p_data )
 {
-#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 RESERVED_PICTURE:
-        case RESERVED_DATED_PICTURE:
-        case RESERVED_DISP_PICTURE:
-            i_reserved_pic++;
-            break;
-        case READY_PICTURE:
-            i_ready_pic++;
-            break;
-        }
-    }
-    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
+    vout_thread_t * p_vout = (vout_thread_t *)p_this;
+    vlc_value_t val;
+    (void)psz_variable; (void)newval; (void)oldval; (void)p_data;
+    val.b_bool = VLC_TRUE;
+    var_Set( p_vout, "intf-change", val );
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * RenderSubPicture: render a subpicture
- *****************************************************************************
- * This function render a sub picture unit.
+ * Helper thread for object variables callbacks.
+ * Only used to avoid deadlocks when using the video embedded mode.
  *****************************************************************************/
-static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
+typedef struct suxor_thread_t
 {
-    p_vout_font_t       p_font;                                 /* text font */
-    int                 i_width, i_height;          /* subpicture dimensions */
-
-    while( p_subpic != NULL )
-    {
-        switch( p_subpic->i_type )
-        {
-        case DVD_SUBPICTURE:                          /* DVD subpicture unit */
-            /* test if the picture really has to be displayed */
-            if( mdate() < p_subpic->begin_date )
-            {
-                break;
-            }
-            if( mdate() > p_subpic->end_date )
-            {
-                /* too late, destroying the subpic */
-                vout_DestroySubPicture( p_vout, p_subpic );
-                printf( "destroying subpicture\n" );
-                break;
-            }
-            vout_RenderSPU( p_subpic->p_data, p_subpic->type.spu.i_offset,
-                            p_subpic->type.spu.i_x1, p_subpic->type.spu.i_y1,
-                            p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
-                            p_vout->i_bytes_per_pixel,
-                            p_vout->i_bytes_per_line );
-            break;
-        case TEXT_SUBPICTURE:                            /* single line text */
-            /* Select default font if not specified */
-            p_font = p_subpic->type.text.p_font;
-            if( p_font == NULL )
-            {
-                p_font = p_vout->p_default_font;
-            }
+    VLC_COMMON_MEMBERS
+    input_thread_t *p_input;
 
-            /* Compute text size (width and height fields are ignored)
-             * and print it */
-            vout_TextSize( p_font, p_subpic->type.text.i_style,
-                           p_subpic->p_data, &i_width, &i_height );
-            if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
-                        i_width, i_height, p_subpic->i_horizontal_align,
-                        p_subpic->i_vertical_align ) )
-            {
-                vout_Print( p_font,
-                            p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
-                            p_subpic->i_x * p_vout->i_bytes_per_pixel +
-                            p_subpic->i_y * p_vout->i_bytes_per_line,
-                            p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
-                            p_subpic->type.text.i_char_color,
-                            p_subpic->type.text.i_border_color,
-                            p_subpic->type.text.i_bg_color,
-                            p_subpic->type.text.i_style, p_subpic->p_data );
-                SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
-                               i_width, i_height );
-            }
-            break;
+} suxor_thread_t;
 
-#ifdef DEBUG
-        default:
-            intf_DbgMsg( "error: unknown subpicture %p type %d\n",
-                         p_subpic, p_subpic->i_type );
-#endif
-        }
+static void SuxorRestartVideoES( suxor_thread_t *p_this )
+{
+    vlc_value_t val;
 
-        p_subpic = p_subpic->p_next;
-    }
-}
+    vlc_thread_ready( p_this );
 
-/*****************************************************************************
- * 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++ )
+    /* Now restart current video stream */
+    var_Get( p_this->p_input, "video-es", &val );
+    if( val.i_int >= 0 )
     {
-        /* XXX?? noooo ! */
-        p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
+        vlc_value_t val_es;
+        val_es.i_int = -VIDEO_ES;
+        var_Set( p_this->p_input, "video-es", val_es );
+        var_Set( p_this->p_input, "video-es", val );
     }
 
-    /* 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 );
-    }
+    vlc_object_release( p_this->p_input );
 
-    /* Activate modified area */
-    SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
+    vlc_object_destroy( p_this );
 }
 
 /*****************************************************************************
- * Synchronize: update synchro level depending of heap state
- *****************************************************************************
- * This function is called during the main vout loop.
+ * object variables callbacks: a bunch of object variables are used by the
+ * interfaces to interact with the vout.
  *****************************************************************************/
-static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
+static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
-    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++;
-    }
+    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    input_thread_t *p_input;
+    vlc_value_t val;
 
-    i_truc *= 2;
+    char *psz_mode = newval.psz_string;
+    char *psz_filter, *psz_deinterlace = NULL;
+    (void)psz_cmd; (void)oldval; (void)p_data;
 
-    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;
+    var_Get( p_vout, "vout-filter", &val );
+    psz_filter = val.psz_string;
+    if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
 
-    }
-    else
+    if( !psz_mode || !*psz_mode )
     {
-        if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
+        if( psz_deinterlace )
         {
-            i_truc = 32;
-            i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
+            char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
+            if( psz_src[0] == ':' ) psz_src++;
+            memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
         }
     }
-
-    if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
-        i_synchro_inc*i_last_synchro_inc < 0 )
+    else if( !psz_deinterlace )
     {
-        i_truc = 32;
+        psz_filter = realloc( psz_filter, strlen( psz_filter ) +
+                              sizeof(":deinterlace") );
+        if( psz_filter && *psz_filter ) strcat( psz_filter, ":" );
+        strcat( psz_filter, "deinterlace" );
     }
 
-    if( i_delay < 6000 )
-    {
-        i_truc = 16;
-        i_synchro_inc -= 2;
-    }
-    else if( i_delay < 70000 )
+    p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
+                                                 FIND_PARENT );
+    if( !p_input ) return VLC_EGENERIC;
+
+    if( psz_mode && *psz_mode )
     {
-        i_truc = 24+(24*i_delay)/70000;
-        if( i_truc < 16 )
-            i_truc = 16;
-        i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
+        /* Modify input as well because the vout might have to be restarted */
+        val.psz_string = psz_mode;
+        var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
+        var_Set( p_input, "deinterlace-mode", val );
     }
-    else if( i_delay > 100000 )
+    vlc_object_release( p_input );
+
+    val.b_bool = VLC_TRUE;
+    var_Set( p_vout, "intf-change", val );
+
+    val.psz_string = psz_filter;
+    var_Set( p_vout, "vout-filter", val );
+    if( psz_filter ) free( psz_filter );
+
+    return VLC_SUCCESS;
+}
+
+static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    input_thread_t *p_input;
+    vlc_value_t val;
+    (void)psz_cmd; (void)oldval; (void)p_data;
+
+    p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
+                                                 FIND_PARENT );
+    if (!p_input)
     {
-        r_synchro_level += 1;
-        if( i_delay > 130000 )
-            r_synchro_level += 1;
+        msg_Err( p_vout, "Input not found" );
+        return( VLC_EGENERIC );
     }
 
-    r_synchro_level += (float)i_synchro_inc / i_truc;
-    p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
+    val.b_bool = VLC_TRUE;
+    var_Set( p_vout, "intf-change", val );
+
+    /* Modify input as well because the vout might have to be restarted */
+    val.psz_string = newval.psz_string;
+    var_Create( p_input, "vout-filter", VLC_VAR_STRING );
+
+    var_Set( p_input, "vout-filter", val );
 
-    if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
+    /* Now restart current video stream */
+    var_Get( p_input, "video-es", &val );
+    if( val.i_int >= 0 )
     {
-        r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
+        suxor_thread_t *p_suxor =
+            vlc_object_create( p_vout, sizeof(suxor_thread_t) );
+        p_suxor->p_input = p_input;
+        p_vout->b_filter_change = VLC_TRUE;
+        vlc_object_yield( p_input );
+        vlc_thread_create( p_suxor, "suxor", SuxorRestartVideoES,
+                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
     }
 
-    //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;
+    vlc_object_release( p_input );
+
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * Manage: manage thread
- *****************************************************************************
- * This function will handle changes in thread configuration.
+ * Video Filter2 stuff
  *****************************************************************************/
-static int Manage( vout_thread_t *p_vout )
+static int ParseVideoFilter2Chain( vout_thread_t *p_vout, char *psz_vfilters )
 {
-#ifdef DEBUG_VIDEO
-    if( p_vout->i_changes )
+    int i;
+    for( i = 0; i < p_vout->i_vfilters_cfg; i++ )
     {
-        intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
-                    p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
+        struct config_chain_t *p_cfg =
+            p_vout->p_vfilters_cfg[p_vout->i_vfilters_cfg];
+        config_ChainDestroy( p_cfg );
+        if( p_vout->psz_vfilters[p_vout->i_vfilters_cfg] )
+        {
+            free( p_vout->psz_vfilters[p_vout->i_vfilters_cfg] );
+            p_vout->psz_vfilters[p_vout->i_vfilters_cfg] = NULL;
+        }
     }
-#endif
-
-    /* On gamma or grayscale change, rebuild tables */
-    if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
-                             VOUT_YUV_CHANGE) )
+    p_vout->i_vfilters_cfg = 0;
+    if( psz_vfilters && *psz_vfilters )
     {
-        if( vout_ResetYUV( p_vout ) )
+        char *psz_parser = psz_vfilters;
+
+        while( psz_parser && *psz_parser )
         {
-            intf_ErrMsg("error: can't rebuild convertion tables\n");
-            return( 1 );
+            psz_parser = config_ChainCreate(
+                            &p_vout->psz_vfilters[p_vout->i_vfilters_cfg],
+                            &p_vout->p_vfilters_cfg[p_vout->i_vfilters_cfg],
+                            psz_parser );
+            msg_Dbg( p_vout, "adding vfilter: %s",
+                     p_vout->psz_vfilters[p_vout->i_vfilters_cfg] );
+            p_vout->i_vfilters_cfg++;
+            if( psz_parser && *psz_parser )
+            {
+                if( p_vout->i_vfilters_cfg == MAX_VFILTERS )
+                {
+                    msg_Warn( p_vout,
+                  "maximum number of video filters reached. \"%s\" discarded",
+                              psz_parser );
+                    break;
+                }
+            }
         }
     }
+    return VLC_SUCCESS;
+}
 
-    /* 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 );
+static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    (void)psz_cmd; (void)oldval; (void)p_data;
 
-    /* Detect unauthorized changes */
-    if( p_vout->i_changes )
-    {
-        /* 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 );
-    }
+    vlc_mutex_lock( &p_vout->vfilter_lock );
+    ParseVideoFilter2Chain( p_vout, newval.psz_string );
+    p_vout->b_vfilter_change = VLC_TRUE;
+    vlc_mutex_unlock( &p_vout->vfilter_lock );
 
-    return( 0 );
+    return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * 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 )
+static void RemoveVideoFilters2( vout_thread_t *p_vout )
 {
-    /* Align horizontally */
-    switch( i_h_align )
+    int i;
+    for( i = 0; i < p_vout->i_vfilters; i++ )
     {
-    case CENTER_ALIGN:
-        *pi_x -= i_width / 2;
-        break;
-    case CENTER_RALIGN:
-        *pi_x += (p_vout->i_width - i_width) / 2;
-        break;
-    case RIGHT_ALIGN:
-        *pi_x -= i_width;
-        break;
-    case RIGHT_RALIGN:
-        *pi_x += p_vout->i_width - i_width;
-        break;
-    }
+        vlc_object_detach( p_vout->pp_vfilters[i] );
+        if( p_vout->pp_vfilters[i]->p_module )
+        {
+            module_Unneed( p_vout->pp_vfilters[i],
+                           p_vout->pp_vfilters[i]->p_module );
+        }
 
-    /* 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;
+        free( p_vout->pp_vfilters[i]->p_owner );
+        vlc_object_destroy( p_vout->pp_vfilters[i] );
     }
-
-    /* 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) );
+    p_vout->i_vfilters = 0;
 }
 
-/*****************************************************************************
- * 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 )
+static void DisplayTitleOnOSD( vout_thread_t *p_vout )
 {
-    intf_ErrMsg( "SetPalette: method does not support palette changing\n" );
-}
+    input_thread_t *p_input;
+    mtime_t i_now, i_stop;
+
+    p_input = (input_thread_t *)vlc_object_find( p_vout,
+              VLC_OBJECT_INPUT, FIND_ANYWHERE );
+    if( p_input )
+    {
+        i_now = mdate();
+        i_stop = i_now + (mtime_t)(p_vout->i_title_timeout * 1000);
+        char *psz_nowplaying =
+            input_item_GetNowPlaying( input_GetItem( p_input ) );
+        char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
+        char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
+        if( EMPTY_STR( psz_name ) )
+            {
+                free( psz_name );
+                psz_name = input_item_GetName( input_GetItem( p_input ) );
+            }
+        if( !EMPTY_STR( psz_nowplaying ) )
+        {
+            vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
+                                   psz_nowplaying, NULL,
+                                   p_vout->i_title_position,
+                                   30 + p_vout->fmt_in.i_width
+                                      - p_vout->fmt_in.i_visible_width
+                                      - p_vout->fmt_in.i_x_offset,
+                                   20 + p_vout->fmt_in.i_y_offset,
+                                   i_now, i_stop );
+        }
+        else if( !EMPTY_STR( psz_artist ) )
+        {
+            char *psz_string = NULL;
 
+            psz_string = malloc( strlen( psz_name ) + strlen( psz_artist ) );
+            if( psz_string )
+            {
+                sprintf( psz_string, "%s - %s", psz_name, psz_artist );
+
+                vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
+                                       psz_string, NULL,
+                                       p_vout->i_title_position,
+                                       30 + p_vout->fmt_in.i_width
+                                          - p_vout->fmt_in.i_visible_width
+                                          - p_vout->fmt_in.i_x_offset,
+                                       20 + p_vout->fmt_in.i_y_offset,
+                                       i_now, i_stop );
+                free( psz_string );
+            }
+        }
+        else
+        {
+            vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
+                                   psz_name, NULL,
+                                   p_vout->i_title_position,
+                                   30 + p_vout->fmt_in.i_width
+                                      - p_vout->fmt_in.i_visible_width
+                                      - p_vout->fmt_in.i_x_offset,
+                                   20 + p_vout->fmt_in.i_y_offset,
+                                   i_now, i_stop );
+        }
+        vlc_object_release( p_input );
+        free( psz_artist );
+        free( psz_name );
+        free( psz_nowplaying );
+    }
+}