+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 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 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;
+ }
+
+ vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
+ p_pic->date );
+ vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
+ }
+}
+