+static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
+{
+ if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
+ *pi_preroll = INT64_MAX;
+ else if( p->i_pts > 0 )
+ *pi_preroll = __MIN( *pi_preroll, p->i_pts );
+ else if( p->i_dts > 0 )
+ *pi_preroll = __MIN( *pi_preroll, p->i_dts );
+}
+static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
+{
+ input_thread_t *p_input = p_dec->p_owner->p_input;
+ const int i_rate = p_block->i_rate;
+ aout_buffer_t *p_aout_buf;
+
+ while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
+ {
+ vlc_mutex_lock( &p_input->p->counters.counters_lock );
+ stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
+ vlc_mutex_unlock( &p_input->p->counters.counters_lock );
+
+ if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
+ {
+ aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
+ p_dec->p_owner->p_aout_input, p_aout_buf );
+ continue;
+ }
+
+ if( p_dec->p_owner->i_preroll_end > 0 )
+ {
+ /* FIXME TODO flush audio output (don't know how to do that) */
+ msg_Dbg( p_dec, "End of audio preroll" );
+ p_dec->p_owner->i_preroll_end = -1;
+ }
+ aout_DecPlay( p_dec->p_owner->p_aout,
+ p_dec->p_owner->p_aout_input,
+ p_aout_buf, i_rate );
+ }
+}
+static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
+{
+ block_t *p_cc;
+ bool pb_present[4];
+ int i;
+ int i_cc_decoder;
+
+ assert( p_dec_cc->pf_get_cc != NULL );
+
+ /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
+ if( !p_dec->p_owner->b_cc_supported )
+ return;
+
+ p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
+ if( !p_cc )
+ return;
+
+ vlc_mutex_lock( &p_dec->p_owner->lock_cc );
+ for( i = 0, i_cc_decoder = 0; i < 4; i++ )
+ {
+ p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
+ if( p_dec->p_owner->pp_cc[i] )
+ i_cc_decoder++;
+ }
+
+ for( i = 0; i < 4; i++ )
+ {
+ if( !p_dec->p_owner->pp_cc[i] )
+ continue;
+
+ if( i_cc_decoder > 1 )
+ DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
+ else
+ DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
+ i_cc_decoder--;
+ }
+ vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
+}
+static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
+{
+ vlc_mutex_lock( &p_vout->picture_lock );
+
+ if( p_pic->i_status == READY_PICTURE )
+ {
+ /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
+ p_pic->date = 1;
+ }
+ else if( p_pic->i_refcount > 0 )
+ {
+ p_pic->i_status = DISPLAYED_PICTURE;
+ }
+ else
+ {
+ p_pic->i_status = DESTROYED_PICTURE;
+ p_vout->i_heap_size--;
+ }
+
+ vlc_mutex_unlock( &p_vout->picture_lock );
+}
+static void VoutFlushPicture( vout_thread_t *p_vout )
+{
+ int i;
+ vlc_mutex_lock( &p_vout->picture_lock );
+ for( i = 0; i < p_vout->render.i_pictures; i++ )
+ {
+ picture_t *p_pic = p_vout->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 */
+ p_pic->date = 1;
+ }
+ }
+ 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;
+ picture_t *p_pic;
+
+ while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
+ {
+ vlc_mutex_lock( &p_input->p->counters.counters_lock );
+ stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
+ vlc_mutex_unlock( &p_input->p->counters.counters_lock );
+
+ if( p_pic->date < p_dec->p_owner->i_preroll_end )
+ {
+ VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
+ continue;
+ }
+
+ if( p_dec->p_owner->i_preroll_end > 0 )
+ {
+ msg_Dbg( p_dec, "End of video preroll" );
+ if( p_dec->p_owner->p_vout )
+ VoutFlushPicture( p_dec->p_owner->p_vout );
+ /* */
+ p_dec->p_owner->i_preroll_end = -1;
+ }
+
+ if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
+ DecoderGetCc( p_dec, p_dec );
+
+ 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 );
+ }
+}
+