#endif
#include <assert.h>
-#include <vlc/vlc.h>
+#include <vlc_common.h>
#include <vlc_block.h>
#include <vlc_vout.h>
p_dec = vlc_object_create( p_input, i_object_type );
if( p_dec == NULL )
- {
- msg_Err( p_input, "out of memory" );
return NULL;
- }
p_dec->pf_decode_audio = 0;
p_dec->pf_decode_video = 0;
p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
if( p_dec->p_owner == NULL )
{
- msg_Err( p_dec, "out of memory" );
+ vlc_object_release( p_dec );
return NULL;
}
p_dec->p_owner->b_own_thread = true;
/* decoder fifo */
if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
{
- msg_Err( p_dec, "out of memory" );
+ free( p_dec->p_owner );
+ vlc_object_release( p_dec );
return NULL;
}
p_owner->b_cc_supported = true;
}
- vlc_mutex_init( p_dec, &p_owner->lock_cc );
+ vlc_mutex_init( &p_owner->lock_cc );
for( i = 0; i < 4; i++ )
{
p_owner->pb_cc_present[i] = false;
}
vlc_mutex_unlock( &p_vout->picture_lock );
}
+
+
+static void optimize_video_pts( decoder_t *p_dec )
+{
+ picture_t * oldest_pict = NULL;
+ picture_t * youngest_pict = NULL;
+ int i;
+
+ input_thread_t * p_input = p_dec->p_owner->p_input;
+ vout_thread_t * p_vout = p_dec->p_owner->p_vout;
+ input_thread_private_t * p_priv = p_input->p;
+
+ /* Enable with --auto-adjust-pts-delay */
+ if( !p_priv->pts_adjust.auto_adjust ) return;
+
+ for( i = 0; i < I_RENDERPICTURES; i++ )
+ {
+ picture_t * pic = PP_RENDERPICTURE[i];
+ if( pic->i_status != READY_PICTURE )
+ continue;
+
+ if( !oldest_pict || pic->date < oldest_pict->date )
+ oldest_pict = pic;
+ if( !youngest_pict || pic->date > youngest_pict->date )
+ youngest_pict = pic;
+ }
+
+ if( !youngest_pict || !oldest_pict )
+ return;
+
+ /* Try to find if we can reduce the pts
+ * This first draft is way to simple, and we can't say if the
+ * algo will converge. It's also full of constants.
+ * But this simple algo allows to reduce the latency
+ * to the minimum.
+ * The whole point of this, is to bypass the pts_delay set
+ * by the access but also the delay arbitraly set by
+ * the remote server.
+ * Actually the remote server's muxer may set up a
+ * pts<->dts delay in the muxed stream. That is
+ * why we may end up in having a negative pts_delay,
+ * to compensate that artificial delay. */
+ mtime_t buffer_size = youngest_pict->date - oldest_pict->date;
+ int64_t pts_slide = 0;
+ if( buffer_size < 10000 )
+ {
+ if( p_priv->pts_adjust.i_num_faulty > 10 )
+ {
+ pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
+ p_priv->pts_adjust.i_num_faulty = 0;
+ }
+ if( p_priv->pts_adjust.to_high )
+ {
+ p_priv->pts_adjust.to_high = !p_priv->pts_adjust.to_high;
+ p_priv->pts_adjust.i_num_faulty = 0;
+ }
+ p_priv->pts_adjust.i_num_faulty++;
+ }
+ else if( buffer_size > 100000 )
+ {
+ if( p_priv->pts_adjust.i_num_faulty > 25 )
+ {
+ pts_slide = -buffer_size/2;
+ p_priv->pts_adjust.i_num_faulty = 0;
+ }
+ if( p_priv->pts_adjust.to_high )
+ {
+ p_priv->pts_adjust.to_high = !p_priv->pts_adjust.to_high;
+ p_priv->pts_adjust.i_num_faulty = 0;
+ }
+ p_priv->pts_adjust.i_num_faulty++;
+ }
+ if( pts_slide )
+ {
+ mtime_t origi_delay = p_input->i_pts_delay;
+
+ p_input->i_pts_delay += pts_slide;
+
+ /* Don't play with the pts delay for more than -2<->3sec */
+ if( p_input->i_pts_delay < -2000000 )
+ p_input->i_pts_delay = -2000000;
+ else if( p_input->i_pts_delay > 3000000 )
+ p_input->i_pts_delay = 3000000;
+ pts_slide = p_input->i_pts_delay - origi_delay;
+
+ msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
+ (int)pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)buffer_size/1000);
+
+ vlc_mutex_lock( &p_vout->picture_lock );
+ /* Slide all the picture */
+ for( i = 0; i < I_RENDERPICTURES; i++ )
+ PP_RENDERPICTURE[i]->date += pts_slide;
+ /* FIXME: slide aout/spu */
+ vlc_mutex_unlock( &p_vout->picture_lock );
+
+ }
+}
+
static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
{
input_thread_t *p_input = p_dec->p_owner->p_input;
vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
p_pic->date );
+
+ optimize_video_pts( p_dec );
+
vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
}
}
/* Cleanup */
if( p_dec->p_owner->p_aout_input )
aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
-
+ if( p_dec->p_owner->p_aout )
+ {
+ vlc_object_release( p_dec->p_owner->p_aout );
+ p_dec->p_owner->p_aout = NULL;
+ }
if( p_dec->p_owner->p_vout )
{
int i_pic;