+ if( p_dec->b_error )
+ {
+ if( p_block )
+ block_Release( p_block );
+ goto flush;
+ }
+
+ if( p_block && p_block->i_buffer <= 0 )
+ {
+ assert( !b_flush_request );
+ block_Release( p_block );
+ return;
+ }
+
+#ifdef ENABLE_SOUT
+ if( p_owner->b_packetizer )
+ {
+ if( p_block )
+ p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
+
+ DecoderProcessSout( p_dec, p_block );
+ }
+ else
+#endif
+ {
+ bool b_flush = false;
+
+ if( p_block )
+ {
+ const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
+ DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
+
+ b_flush = !b_flushing && b_flush_request;
+
+ p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
+ }
+
+ if( p_dec->fmt_out.i_cat == AUDIO_ES )
+ {
+ DecoderProcessAudio( p_dec, p_block, b_flush );
+ }
+ else if( p_dec->fmt_out.i_cat == VIDEO_ES )
+ {
+ DecoderProcessVideo( p_dec, p_block, b_flush );
+ }
+ else if( p_dec->fmt_out.i_cat == SPU_ES )
+ {
+ DecoderProcessSpu( p_dec, p_block, b_flush );
+ }
+ else
+ {
+ msg_Err( p_dec, "unknown ES format" );
+ p_dec->b_error = true;
+ }
+ }
+
+ /* */
+flush:
+ if( b_flush_request )
+ DecoderProcessOnFlush( p_dec );
+}
+
+
+/**
+ * The decoding main loop
+ *
+ * \param p_dec the decoder
+ */
+static void *DecoderThread( void *p_data )
+{
+ decoder_t *p_dec = (decoder_t *)p_data;
+ decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+ /* The decoder's main loop */
+ for( ;; )
+ {
+ block_t *p_block;
+
+ vlc_fifo_Lock( p_owner->p_fifo );
+ vlc_fifo_CleanupPush( p_owner->p_fifo );
+
+ while( vlc_fifo_IsEmpty( p_owner->p_fifo ) )
+ {
+ if( p_owner->b_woken )
+ break;
+ vlc_fifo_Wait( p_owner->p_fifo );
+ /* Make sure there is no cancellation point other than this one^^.
+ * If you need one, be sure to push cleanup of p_block. */
+ }
+
+ p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
+ if( p_block != NULL )
+ vlc_cond_signal( &p_owner->wait_acknowledge );
+
+ p_owner->b_woken = false;
+ vlc_cleanup_run();
+
+ if( p_block == NULL || p_block->i_flags & BLOCK_FLAG_CORE_EOS )
+ DecoderSignalWait( p_dec );
+
+ if( p_block )
+ {
+ int canc = vlc_savecancel();
+
+ if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
+ {
+ /* calling DecoderProcess() with NULL block will make
+ * decoders/packetizers flush their buffers */
+ block_Release( p_block );
+ p_block = NULL;
+ }
+
+ DecoderProcess( p_dec, p_block );
+
+ vlc_restorecancel( canc );
+ }
+ }
+ return NULL;
+}
+
+/**
+ * Create a decoder object
+ *
+ * \param p_input the input thread
+ * \param p_es the es descriptor
+ * \param b_packetizer instead of a decoder
+ * \return the decoder object
+ */
+static decoder_t * CreateDecoder( vlc_object_t *p_parent,
+ input_thread_t *p_input,
+ const es_format_t *fmt, bool b_packetizer,
+ input_resource_t *p_resource,
+ sout_instance_t *p_sout )
+{
+ decoder_t *p_dec;
+ decoder_owner_sys_t *p_owner;
+ es_format_t null_es_format;
+
+ p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
+ if( p_dec == NULL )
+ return NULL;
+
+ p_dec->pf_decode_audio = NULL;
+ p_dec->pf_decode_video = NULL;
+ p_dec->pf_decode_sub = NULL;
+ p_dec->pf_get_cc = NULL;
+ p_dec->pf_packetize = NULL;
+
+ /* Initialize the decoder */
+ p_dec->p_module = NULL;
+
+ memset( &null_es_format, 0, sizeof(es_format_t) );
+ es_format_Copy( &p_dec->fmt_in, fmt );
+ es_format_Copy( &p_dec->fmt_out, &null_es_format );
+
+ p_dec->p_description = NULL;
+
+ /* Allocate our private structure for the decoder */
+ p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
+ if( unlikely(p_owner == NULL) )