+ p_owner->b_flushing = false;
+ vlc_cond_signal( &p_owner->wait );
+ }
+
+ vlc_mutex_unlock( &p_owner->lock );
+}
+
+static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+ vlc_mutex_lock( &p_owner->lock );
+
+ if( p_owner->b_buffering )
+ {
+ if( b_full )
+ p_owner->buffer.b_full = true;
+ vlc_cond_signal( &p_owner->wait );
+ }
+
+ vlc_mutex_unlock( &p_owner->lock );
+}
+
+static bool DecoderIsFlushing( decoder_t *p_dec )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+ bool b_flushing;
+
+ vlc_mutex_lock( &p_owner->lock );
+
+ b_flushing = p_owner->b_flushing;
+
+ vlc_mutex_unlock( &p_owner->lock );
+
+ return b_flushing;
+}
+
+static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+ vlc_assert_locked( &p_owner->lock );
+
+ while( !p_owner->b_flushing )
+ {
+ if( p_owner->b_paused )
+ {
+ if( p_owner->b_buffering && !p_owner->buffer.b_full )
+ break;
+ if( p_owner->pause.i_ignore > 0 )
+ {
+ p_owner->pause.i_ignore--;
+ break;
+ }
+ }
+ else
+ {
+ if( !p_owner->b_buffering || !p_owner->buffer.b_full )
+ break;
+ }
+ vlc_cond_wait( &p_owner->wait, &p_owner->lock );
+ }
+
+ if( pb_reject )
+ *pb_reject = p_owner->b_flushing;
+}
+
+static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+ vlc_assert_locked( &p_owner->lock );
+
+ /* XXX only audio and video output have to be paused.
+ * - for sout it is useless
+ * - for subs, it is done by the vout
+ */
+ if( p_dec->fmt_in.i_cat == AUDIO_ES )
+ {
+ if( p_owner->p_aout && p_owner->p_aout_input )
+ aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
+ b_paused, i_date );
+ }
+ else if( p_dec->fmt_in.i_cat == VIDEO_ES )
+ {
+ if( p_owner->p_vout )
+ vout_ChangePause( p_owner->p_vout, b_paused, i_date );
+ }
+}
+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 mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
+{
+ mtime_t current_date = mdate();
+
+ /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
+ if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
+ {
+ /* ETSI EN 300 472 Annex A : do not take into account the PTS
+ * for teletext streams. */
+ return current_date + 400000 + i_ts_delay;
+ }
+ return i_ts;
+}
+
+static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
+ mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+ input_clock_t *p_clock = p_owner->p_clock;
+ int i_rate = 0;
+
+ vlc_assert_locked( &p_owner->lock );
+
+ const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
+ const mtime_t i_es_delay = p_owner->i_ts_delay;
+
+ if( p_clock )
+ {
+ const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
+
+ if( *pi_ts0 > 0 )
+ *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
+ *pi_ts0 + i_es_delay );
+ if( pi_ts1 && *pi_ts1 > 0 )
+ *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
+ *pi_ts1 + i_es_delay );
+
+ /* Do not create ephemere data because of rounding errors */
+ if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
+ *pi_ts1 += 1;
+
+ if( i_rate <= 0 )
+ i_rate = input_clock_GetRate( p_clock );
+
+ if( pi_duration )
+ *pi_duration = ( *pi_duration * i_rate +
+ INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
+
+ if( pi_rate )
+ *pi_rate = i_rate;
+
+ if( b_telx )
+ {
+ *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
+ if( pi_ts1 && *pi_ts1 <= 0 )
+ *pi_ts1 = *pi_ts0;
+ }
+ }
+ if( pi_delay )
+ {
+ const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
+ *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
+ }
+}
+
+static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
+ int *pi_played_sum, int *pi_lost_sum )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+ aout_instance_t *p_aout = p_owner->p_aout;
+ aout_input_t *p_aout_input = p_owner->p_aout_input;
+
+ /* */
+ if( p_audio->start_date <= 0 )
+ {
+ msg_Warn( p_dec, "non-dated audio buffer received" );
+ *pi_lost_sum += 1;
+ aout_BufferFree( p_audio );
+ return;
+ }
+
+ /* */
+ vlc_mutex_lock( &p_owner->lock );
+
+ if( p_owner->b_buffering || p_owner->buffer.p_audio )
+ {
+ p_audio->p_next = NULL;
+
+ *p_owner->buffer.pp_audio_next = p_audio;
+ p_owner->buffer.pp_audio_next = &p_audio->p_next;
+
+ p_owner->buffer.i_count++;
+ if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
+ p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
+ {
+ p_owner->buffer.b_full = true;
+ vlc_cond_signal( &p_owner->wait );
+ }
+ }
+
+ for( ;; )
+ {
+ bool b_has_more = false;
+ bool b_reject;
+ DecoderWaitUnblock( p_dec, &b_reject );
+
+ if( p_owner->b_buffering )
+ {
+ vlc_mutex_unlock( &p_owner->lock );
+ return;
+ }
+
+ /* */
+ if( p_owner->buffer.p_audio )
+ {
+ p_audio = p_owner->buffer.p_audio;
+
+ p_owner->buffer.p_audio = p_audio->p_next;
+ p_owner->buffer.i_count--;
+
+ b_has_more = p_owner->buffer.p_audio != NULL;
+ if( !b_has_more )
+ p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
+ }
+
+ /* */
+ int i_rate = INPUT_RATE_DEFAULT;
+ mtime_t i_delay;
+
+ DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
+ &i_rate, &i_delay, false );
+
+ vlc_mutex_unlock( &p_owner->lock );
+
+ /* */
+ const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
+
+ if( !p_aout || !p_aout_input ||
+ p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
+ i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
+ i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
+ b_reject = true;
+
+ if( !b_reject )
+ {
+ /* Wait if we are too early
+ * FIXME that's plain ugly to do it here */
+ mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
+
+ if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
+ *pi_played_sum += 1;
+ *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
+ }
+ else
+ {
+ if( p_audio->start_date <= 0 )
+ {
+ msg_Warn( p_dec, "non-dated audio buffer received" );
+ }
+ else if( p_audio->start_date > i_max_date )
+ {
+ msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
+ p_audio->start_date - mdate() );
+ }
+ *pi_lost_sum += 1;
+ aout_BufferFree( p_audio );
+ }
+
+ if( !b_has_more )
+ break;
+
+ vlc_mutex_lock( &p_owner->lock );
+ if( !p_owner->buffer.p_audio )
+ {
+ vlc_mutex_unlock( &p_owner->lock );
+ break;
+ }
+ }
+}
+
+static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
+{
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+ input_thread_t *p_input = p_owner->p_input;
+ aout_buffer_t *p_aout_buf;
+ int i_decoded = 0;
+ int i_lost = 0;
+ int i_played = 0;
+
+ while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
+ {
+ aout_instance_t *p_aout = p_owner->p_aout;
+ aout_input_t *p_aout_input = p_owner->p_aout_input;
+ int i_lost = 0;
+ int i_played;