+ vlc_bool_t b_ok = VLC_TRUE;
+ sout_buffer_t *p_data;
+
+ /* Accumulate enough data in the pcr stream (>i_caching_delay) */
+ /* Accumulate enough data in all other stream ( >= length of pcr) */
+ for( i = 0; i < p_mux->i_nb_inputs; i++ )
+ {
+ sout_input_t *p_input = p_mux->pp_inputs[i];
+ ts_stream_t *p_stream = (ts_stream_t*)p_input->p_sys;
+
+ if( ( p_stream == p_pcr_stream && p_stream->i_pes_length <= p_sys->i_caching_delay ) ||
+ p_stream->i_pes_dts + p_stream->i_pes_length < p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
+ {
+ /* Need more data */
+ if( p_input->p_fifo->i_depth <= 1 )
+ {
+ if( p_input->p_fmt->i_cat == AUDIO_ES ||
+ p_input->p_fmt->i_cat == VIDEO_ES )
+ {
+ /* We need more data */
+ return VLC_SUCCESS;
+ }
+ else if( p_input->p_fifo->i_depth <= 0 )
+ {
+ /* spu, only one packet is needed */
+ continue;
+ }
+ }
+ b_ok = VLC_FALSE;
+
+ p_data = sout_FifoGet( p_input->p_fifo );
+ if( p_input->p_fifo->i_depth > 0 )
+ {
+ sout_buffer_t *p_next = sout_FifoShow( p_input->p_fifo );
+
+ p_data->i_length = p_next->i_dts - p_data->i_dts;
+ }
+
+ if( ( p_pcr_stream->i_pes_dts > 0 && p_data->i_dts - 2000000 > p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length ) ||
+ p_data->i_dts < p_stream->i_pes_dts ||
+ ( p_stream->i_pes_dts > 0 && p_data->i_dts - 2000000 > p_stream->i_pes_dts + p_stream->i_pes_length ) )
+ {
+ msg_Warn( p_mux, "packet with too strange dts (dts=%lld,old=%lld,pcr=%lld)",
+ p_data->i_dts,
+ p_stream->i_pes_dts,
+ p_pcr_stream->i_pes_dts );
+ sout_BufferDelete( p_mux->p_sout, p_data );
+
+ BufferChainClean( p_mux->p_sout, &p_stream->chain_pes );
+ p_stream->i_pes_dts = 0;
+ p_stream->i_pes_used = 0;
+ p_stream->i_pes_length = 0;
+
+ BufferChainClean( p_mux->p_sout, &p_pcr_stream->chain_pes );
+ p_pcr_stream->i_pes_dts = 0;
+ p_pcr_stream->i_pes_used = 0;
+ p_pcr_stream->i_pes_length = 0;
+
+ }
+ else
+ {
+ p_stream->i_pes_length += p_data->i_length;
+ if( p_stream->i_pes_dts == 0 )
+ {
+ p_stream->i_pes_dts = p_data->i_dts;
+ }
+
+ /* Convert to pes */
+ E_( EStoPES )( p_mux->p_sout, &p_data, p_data, p_stream->i_stream_id, 1 );
+
+ BufferChainAppend( &p_stream->chain_pes, p_data );
+ }
+ }
+ }
+
+ if( b_ok )
+ {
+ break;
+ }