Commit 958ae35d authored by Laurent Aimar's avatar Laurent Aimar

Reworked the way pictures are handled by the vout core.

As a side effects, it may improve a bit frame stepping.
parent 889ae4fb
/*****************************************************************************
* chrono.h: vout chrono
*****************************************************************************
* Copyright (C) 2009-2010 Laurent Aimar
* $Id$
*
* Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#if defined(__PLUGIN__) || defined(__BUILTIN__) || !defined(__LIBVLC__)
# error This header file can only be included from LibVLC.
#endif
#ifndef _VOUT_CHRONO_H
#define _VOUT_CHRONO_H
typedef struct {
int shift;
mtime_t avg;
mtime_t avg_initial;
int shift_var;
mtime_t var;
mtime_t start;
} vout_chrono_t;
static inline void vout_chrono_Init(vout_chrono_t *chrono, int shift, mtime_t avg_initial)
{
chrono->shift = shift;
chrono->avg_initial =
chrono->avg = avg_initial;
chrono->shift_var = shift+1;
chrono->var = avg_initial / 2;
chrono->start = VLC_TS_INVALID;
}
static inline void vout_chrono_Clean(vout_chrono_t *chrono)
{
VLC_UNUSED(chrono);
}
static inline void vout_chrono_Start(vout_chrono_t *chrono)
{
chrono->start = mdate();
}
static inline mtime_t vout_chrono_GetHigh(vout_chrono_t *chrono)
{
return chrono->avg + 2 * chrono->var;
}
static inline mtime_t vout_chrono_GetLow(vout_chrono_t *chrono)
{
return __MAX(chrono->avg - 2 * chrono->var, 0);
}
static inline void vout_chrono_Stop(vout_chrono_t *chrono)
{
assert(chrono->start != VLC_TS_INVALID);
/* */
const mtime_t duration = mdate() - chrono->start;
const mtime_t var = llabs( duration - chrono->avg );
/* Update average only if the current point is 'valid' */
if( duration < vout_chrono_GetHigh( chrono ) )
chrono->avg = (((1 << chrono->shift) - 1) * chrono->avg + duration) >> chrono->shift;
/* Always update the variance */
chrono->var = (((1 << chrono->shift_var) - 1) * chrono->var + var) >> chrono->shift_var;
/* For assert */
chrono->start = VLC_TS_INVALID;
}
static inline void vout_chrono_Reset(vout_chrono_t *chrono)
{
vout_chrono_t ch = *chrono;
vout_chrono_Clean(chrono);
vout_chrono_Init(chrono, ch.shift, ch.avg_initial);
}
#endif
......@@ -58,7 +58,7 @@ static picture_t *VideoBufferNew(filter_t *filter)
vd->fmt.i_width == fmt->i_width &&
vd->fmt.i_height == fmt->i_height);
picture_pool_t *pool = vout_display_Pool(vd, 1);
picture_pool_t *pool = vout_display_Pool(vd, 3);
if (!pool)
return NULL;
return picture_pool_Get(pool);
......
......@@ -10,6 +10,7 @@
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
* Gildas Bazin <gbazin@videolan.org>
* Laurent Aimar <fenrir _AT_ videolan _DOT_ 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
......@@ -37,20 +38,12 @@
#include <stdlib.h> /* free() */
#include <string.h>
#include <assert.h>
#include <vlc_vout.h>
#include <vlc_filter.h>
#include <vlc_osd.h>
#include <assert.h>
#if defined( __APPLE__ )
/* Include darwin_specific.h here if needed */
#endif
/** FIXME This is quite ugly but needed while we don't have counters
* helpers */
//#include "input/input_internal.h"
#include <libvlc.h>
#include <vlc_input.h>
......@@ -62,7 +55,6 @@
*****************************************************************************/
static int InitThread ( vout_thread_t * );
static void* RunThread ( void * );
static void ErrorThread ( vout_thread_t * );
static void CleanThread ( vout_thread_t * );
static void EndThread ( vout_thread_t * );
......@@ -104,34 +96,19 @@ static void DisplayTitleOnOSD( vout_thread_t *p_vout );
/* Better be in advance when awakening than late... */
#define VOUT_MWAIT_TOLERANCE ((mtime_t)(0.020*CLOCK_FREQ))
/* Minimum number of direct pictures the video output will accept without
* creating additional pictures in system memory */
#ifdef OPTIMIZE_MEMORY
# define VOUT_MIN_DIRECT_PICTURES (VOUT_MAX_PICTURES/2)
#else
# define VOUT_MIN_DIRECT_PICTURES (3*VOUT_MAX_PICTURES/4)
#endif
/*****************************************************************************
* Video Filter2 functions
*****************************************************************************/
static picture_t *video_new_buffer_filter( filter_t *p_filter )
{
vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
p_picture->i_status = READY_PICTURE;
return p_picture;
return picture_pool_Get(p_vout->p->private_pool);
}
static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
{
vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
vlc_mutex_lock( &p_vout->p->picture_lock );
vout_UsePictureLocked( p_vout, p_pic );
vlc_mutex_unlock( &p_vout->p->picture_lock );
picture_Release(p_pic);
}
static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
......@@ -279,9 +256,8 @@ vout_thread_t *vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
*****************************************************************************/
vout_thread_t * vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
{
vout_thread_t * p_vout; /* thread descriptor */
int i_index; /* loop variable */
vlc_value_t text;
vout_thread_t *p_vout; /* thread descriptor */
vlc_value_t text;
config_chain_t *p_cfg;
......@@ -312,18 +288,7 @@ vout_thread_t * vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
return NULL;
}
/* 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->p_picture[i_index].i_status = FREE_PICTURE;
p_vout->p->p_picture[i_index].i_type = EMPTY_PICTURE;
p_vout->p->p_picture[i_index].b_slow = 0;
}
/* Initialize the rendering heap */
I_RENDERPICTURES = 0;
/* */
p_vout->fmt_render = *p_fmt; /* FIXME palette */
p_vout->fmt_in = *p_fmt; /* FIXME palette */
......@@ -332,28 +297,29 @@ vout_thread_t * vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
video_format_FixRgb( &p_vout->fmt_render );
video_format_FixRgb( &p_vout->fmt_in );
p_vout->p->render.i_last_used_pic = -1;
/* Zero the output heap */
I_OUTPUTPICTURES = 0;
/* Initialize misc stuff */
p_vout->p->i_changes = 0;
p_vout->p->b_fullscreen = 0;
p_vout->p->render_time = 10;
p_vout->p->c_fps_samples = 0;
vout_chrono_Init( &p_vout->p->render, 5, 10000 ); /* Arbitrary initial time */
vout_statistic_Init( &p_vout->p->statistic );
p_vout->p->b_filter_change = 0;
p_vout->p->b_paused = false;
p_vout->p->i_pause_date = 0;
p_vout->p->i_par_num =
p_vout->p->i_par_den = 1;
p_vout->p->p_picture_displayed = NULL;
p_vout->p->i_picture_displayed_date = 0;
p_vout->p->b_picture_displayed = false;
p_vout->p->is_late_dropped = var_InheritBool( p_vout, "drop-late-frames" );
p_vout->p->b_picture_empty = false;
p_vout->p->i_picture_qtype = QTYPE_NONE;
p_vout->p->b_picture_interlaced = false;
p_vout->p->displayed.clock = VLC_TS_INVALID;
p_vout->p->displayed.decoded = NULL;
p_vout->p->displayed.timestampX = VLC_TS_INVALID;
p_vout->p->displayed.qtype = QTYPE_NONE;
p_vout->p->displayed.is_interlaced = false;
p_vout->p->step.is_requested = false;
p_vout->p->step.last = VLC_TS_INVALID;
p_vout->p->step.timestamp = VLC_TS_INVALID;
p_vout->p->decoder_fifo = picture_fifo_New();
p_vout->p->decoder_pool = NULL;
vlc_mouse_Init( &p_vout->p->mouse );
......@@ -532,6 +498,12 @@ static void vout_Destructor( vlc_object_t * p_this )
if( p_vout->p->p_spu )
spu_Destroy( p_vout->p->p_spu );
vout_chrono_Clean( &p_vout->p->render );
if( p_vout->p->decoder_fifo )
picture_fifo_Delete( p_vout->p->decoder_fifo );
assert( !p_vout->p->decoder_pool );
/* Destroy the locks */
vlc_cond_destroy( &p_vout->p->change_wait );
vlc_cond_destroy( &p_vout->p->picture_wait );
......@@ -564,19 +536,18 @@ void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
vlc_mutex_lock( &p_vout->p->picture_lock );
p_vout->p->i_picture_displayed_date = 0;
if( p_vout->p->b_paused )
{
const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
{
picture_t *p_pic = PP_RENDERPICTURE[i_index];
if (p_vout->p->step.timestamp > VLC_TS_INVALID)
p_vout->p->step.timestamp += i_duration;
if (!b_paused)
p_vout->p->step.last = p_vout->p->step.timestamp;
picture_fifo_OffsetDate( p_vout->p->decoder_fifo, i_duration );
if (p_vout->p->displayed.decoded)
p_vout->p->displayed.decoded->date += i_duration;
if( p_pic->i_status == READY_PICTURE )
p_pic->date += i_duration;
}
vlc_cond_signal( &p_vout->p->picture_wait );
vlc_mutex_unlock( &p_vout->p->picture_lock );
......@@ -584,6 +555,8 @@ void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
}
else
{
if (b_paused)
p_vout->p->step.last = p_vout->p->step.timestamp;
vlc_mutex_unlock( &p_vout->p->picture_lock );
}
p_vout->p->b_paused = b_paused;
......@@ -598,110 +571,98 @@ void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_l
pi_displayed, pi_lost );
}
void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
static void Flush(vout_thread_t *vout, mtime_t date, bool below)
{
vlc_mutex_lock( &p_vout->p->picture_lock );
p_vout->p->i_picture_displayed_date = 0;
for( int i = 0; i < p_vout->p->render.i_pictures; i++ )
{
picture_t *p_pic = p_vout->p->render.pp_picture[i];
if( p_pic->i_status == READY_PICTURE ||
p_pic->i_status == DISPLAYED_PICTURE )
{
/* We cannot change picture status if it is in READY_PICTURE state,
* Just make sure they won't be displayed */
if( p_pic->date > i_date )
p_pic->date = i_date;
}
vlc_assert_locked(&vout->p->picture_lock);
vout->p->step.timestamp = VLC_TS_INVALID;
vout->p->step.last = VLC_TS_INVALID;
picture_t *last = vout->p->displayed.decoded;
if (last &&
(( below && last->date <= date) ||
(!below && last->date >= date))) {
vout->p->step.is_requested = true;
}
vlc_cond_signal( &p_vout->p->picture_wait );
vlc_mutex_unlock( &p_vout->p->picture_lock );
picture_fifo_Flush( vout->p->decoder_fifo, date, below );
}
void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
void vout_Flush(vout_thread_t *vout, mtime_t date)
{
int i_pic, i_ready_pic;
vlc_mutex_lock(&vout->p->picture_lock);
vlc_mutex_lock( &p_vout->p->picture_lock );
Flush(vout, date, false);
for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->p->render.i_pictures && !b_forced; i_pic++ )
{
const picture_t *p_pic = p_vout->p->render.pp_picture[i_pic];
vlc_cond_signal(&vout->p->picture_wait);
vlc_mutex_unlock(&vout->p->picture_lock);
}
if( p_pic->i_status == READY_PICTURE )
{
i_ready_pic++;
/* If we have at least 2 ready pictures, wait for the vout thread to
* process one */
if( i_ready_pic >= 2 )
break;
static void vout_Reset(vout_thread_t *vout)
{
#warning "TODO reset pause in vout_Reset"
vlc_mutex_lock(&vout->p->picture_lock);
Flush(vout, INT64_MAX, true);
if (vout->p->decoder_pool)
picture_pool_NonEmpty(vout->p->decoder_pool, true);
vlc_cond_signal( &vout->p->picture_wait );
vlc_mutex_unlock(&vout->p->picture_lock);
}
continue;
}
void vout_FixLeaks( vout_thread_t *vout, bool b_forced )
{
#warning "TODO split vout_FixLeaks into vout_FixLeaks and vout_Reset"
if (b_forced) {
vout_Reset(vout);
return;
}
if( p_pic->i_status == DISPLAYED_PICTURE )
{
/* If at least one displayed picture is not referenced
* let vout free it */
if( p_pic->i_refcount == 0 )
break;
}
vlc_mutex_lock(&vout->p->picture_lock);
picture_t *picture = picture_fifo_Peek(vout->p->decoder_fifo);
if (!picture) {
picture = picture_pool_Get(vout->p->decoder_pool);
}
if( i_pic < p_vout->p->render.i_pictures && !b_forced )
{
vlc_mutex_unlock( &p_vout->p->picture_lock );
if (picture) {
picture_Release(picture);
/* Not all pictures has been displayed yet or some are
* free */
vlc_mutex_unlock(&vout->p->picture_lock);
return;
}
/* Too many pictures are still referenced, there is probably a bug
* with the decoder */
if( !b_forced )
msg_Err( p_vout, "pictures leaked, resetting the heap" );
/* Just free all the pictures */
for( i_pic = 0; i_pic < p_vout->p->render.i_pictures; i_pic++ )
{
picture_t *p_pic = p_vout->p->render.pp_picture[i_pic];
/* There is no reason that no pictures are available, force one
* from the pool, becarefull with it though */
msg_Err(vout, "pictures leaked, trying to workaround");
msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
p_pic->i_refcount = 0;
/* */
picture_pool_NonEmpty(vout->p->decoder_pool, false);
switch( p_pic->i_status )
{
case READY_PICTURE:
case DISPLAYED_PICTURE:
case RESERVED_PICTURE:
if( p_pic != p_vout->p->p_picture_displayed )
vout_UsePictureLocked( p_vout, p_pic );
break;
}
}
vlc_cond_signal( &p_vout->p->picture_wait );
vlc_mutex_unlock( &p_vout->p->picture_lock );
vlc_cond_signal(&vout->p->picture_wait);
vlc_mutex_unlock(&vout->p->picture_lock);
}
void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
void vout_NextPicture(vout_thread_t *vout, mtime_t *duration)
{
vlc_mutex_lock( &p_vout->p->picture_lock );
vlc_mutex_lock(&vout->p->picture_lock);
const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
vout->p->b_picture_empty = false;
vout->p->step.is_requested = true;
p_vout->p->b_picture_displayed = false;
p_vout->p->b_picture_empty = false;
if( p_vout->p->p_picture_displayed )
{
p_vout->p->p_picture_displayed->date = 1;
vlc_cond_signal( &p_vout->p->picture_wait );
}
/* FIXME I highly doubt that it can works with only one cond_t FIXME */
vlc_cond_signal(&vout->p->picture_wait);
while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->p->picture_lock );
while (vout->p->step.is_requested && !vout->p->b_picture_empty)
vlc_cond_wait(&vout->p->picture_wait, &vout->p->picture_lock);
*pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
if (vout->p->step.last > VLC_TS_INVALID &&
vout->p->step.timestamp > vout->p->step.last) {
*duration = vout->p->step.timestamp - vout->p->step.last;
vout->p->step.last = vout->p->step.timestamp;
} else {
*duration = 0;
}
/* TODO advance subpicture by the duration ... */
vlc_mutex_unlock( &p_vout->p->picture_lock );
vlc_mutex_unlock(&vout->p->picture_lock);
}
void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
......@@ -738,25 +699,7 @@ static int InitThread( vout_thread_t *p_vout )
if( vout_InitWrapper( p_vout ) )
return VLC_EGENERIC;
p_vout->p->p_picture_displayed = NULL;
if( !I_OUTPUTPICTURES )
{
msg_Err( p_vout, "plugin was unable to allocate at least "
"one direct buffer" );
vout_EndWrapper( p_vout );
return VLC_EGENERIC;
}
if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
{
msg_Err( p_vout, "plugin allocated too many direct buffers, "
"our internal buffers must have overflown." );
vout_EndWrapper( p_vout );
return VLC_EGENERIC;
}
msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
p_vout->p->displayed.decoded = NULL;
assert( p_vout->fmt_out.i_width > 0 && p_vout->fmt_out.i_height > 0 );
if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
......@@ -806,454 +749,369 @@ static int InitThread( vout_thread_t *p_vout )
assert( p_vout->fmt_out.i_width == p_vout->fmt_render.i_width &&
p_vout->fmt_out.i_height == p_vout->fmt_render.i_height &&
p_vout->fmt_out.i_chroma == p_vout->fmt_render.i_chroma );
/* Check whether we managed to create direct buffers similar to
* the render buffers, ie same size and 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 */
for( i = 1; i < VOUT_MAX_PICTURES; i++ )
{
if( p_vout->p->p_picture[ i ].i_type != DIRECT_PICTURE &&
I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
p_vout->p->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->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 );
assert( p_vout->p->decoder_pool );
return VLC_SUCCESS;
}
/*****************************************************************************
* RunThread: video output thread
*****************************************************************************
* Video output thread. This function does only returns when the thread is
* terminated. It handles the pictures arriving in the video heap and the
* display device events.
*****************************************************************************/
static void* RunThread( void *p_this )
static int ThreadDisplayPicture(vout_thread_t *vout,
bool now, mtime_t *deadline)
{
vout_thread_t *p_vout = p_this;
bool b_has_wrapper;
int i_idle_loops = 0; /* loops without displaying a picture */
int i_picture_qtype_last = QTYPE_NONE;
bool b_picture_interlaced_last = false;
mtime_t i_picture_interlaced_last_date;
/*
* Initialize thread
*/
b_has_wrapper = !vout_OpenWrapper( p_vout, p_vout->p->psz_module_name );
vlc_mutex_lock( &p_vout->p->change_lock );
if( b_has_wrapper )
p_vout->p->b_error = InitThread( p_vout );
else
p_vout->p->b_error = true;
/* signal the creation of the vout */
p_vout->p->b_ready = true;
vlc_cond_signal( &p_vout->p->change_wait );
if( p_vout->p->b_error )
goto exit_thread;
/* */
const bool b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
i_picture_interlaced_last_date = mdate();
/*
* Main loop - it is not executed if an error occurred during
* initialization
*/
while( !p_vout->p->b_done && !p_vout->p->b_error )
{
/* Initialize loop variables */
const mtime_t current_date = mdate();
picture_t *p_picture;
picture_t *p_filtered_picture;
mtime_t display_date;
picture_t *p_directbuffer;
int i_index;
if( p_vout->p->b_title_show && p_vout->p->psz_title )
DisplayTitleOnOSD( p_vout );
vlc_mutex_lock( &p_vout->p->picture_lock );
/* Look for the earliest picture but after the last displayed one */
picture_t *p_last = p_vout->p->p_picture_displayed;;
p_picture = NULL;
for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
{
picture_t *p_pic = PP_RENDERPICTURE[i_index];
if( p_pic->i_status != READY_PICTURE )
continue;
if( p_vout->p->b_paused && p_last && p_last->date > 1 )
continue;
if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
{
/* Drop old picture */
vout_UsePictureLocked( p_vout, p_pic );
}
else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
p_pic->date < current_date + p_vout->p->render_time &&
b_drop_late )
{
/* Picture is late: it will be destroyed and the thread
* will directly choose the next picture */
vout_UsePictureLocked( p_vout, p_pic );
vout_statistic_Update( &p_vout->p->statistic, 0, 1 );
msg_Warn( p_vout, "late picture skipped (%"PRId64" > %d)",
current_date - p_pic->date, - p_vout->p->render_time );
}
else if( ( !p_last || p_last->date < p_pic->date ) &&
( p_picture == NULL || p_pic->date < p_picture->date ) )