1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_block.h>
40 #include <vlc_codec.h>
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
47 #include "input_clock.h"
48 #include "input_decoder.h"
50 #include "../video_output/vout_internal.h"
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void DeleteDecoder( decoder_t * );
55 static void *DecoderThread( vlc_object_t * );
56 static int DecoderProcess( decoder_t *, block_t * );
57 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
58 static void DecoderFlush( decoder_t * );
60 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
62 /* Buffers allocation callbacks for the decoders */
63 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
64 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
66 static picture_t *vout_new_buffer( decoder_t * );
67 static void vout_del_buffer( decoder_t *, picture_t * );
68 static void vout_link_picture( decoder_t *, picture_t * );
69 static void vout_unlink_picture( decoder_t *, picture_t * );
71 static subpicture_t *spu_new_buffer( decoder_t * );
72 static void spu_del_buffer( decoder_t *, subpicture_t * );
74 static es_format_t null_es_format;
76 struct decoder_owner_sys_t
80 int64_t i_preroll_end;
82 input_thread_t *p_input;
83 input_clock_t *p_clock;
86 vout_thread_t *p_spu_vout;
90 sout_instance_t *p_sout;
91 sout_packetizer_input_t *p_sout_input;
93 /* Some decoders require already packetized data (ie. not truncated) */
94 decoder_t *p_packetizer;
96 /* Current format in use by the output */
102 block_fifo_t *p_fifo;
104 /* Lock for communication with decoder thread */
108 /* -- These variables need locking on write(only) -- */
109 aout_instance_t *p_aout;
110 aout_input_t *p_aout_input;
112 vout_thread_t *p_vout;
114 /* -- Theses variables need locking on read *and* write -- */
118 mtime_t i_pause_date;
128 decoder_t *pp_decoder[4];
135 /*****************************************************************************
137 *****************************************************************************/
139 /* decoder_GetInputAttachment:
141 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
142 const char *psz_name )
144 input_attachment_t *p_attachment;
145 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
149 /* decoder_GetInputAttachments:
151 int decoder_GetInputAttachments( decoder_t *p_dec,
152 input_attachment_t ***ppp_attachment,
155 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
156 ppp_attachment, pi_attachment );
158 /* decoder_GetDisplayDate:
160 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
162 decoder_owner_sys_t *p_owner = p_dec->p_owner;
164 if( !p_owner->p_clock )
166 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
168 /* decoder_GetDisplayRate:
170 int decoder_GetDisplayRate( decoder_t *p_dec )
172 decoder_owner_sys_t *p_owner = p_dec->p_owner;
174 if( !p_owner->p_clock )
175 return INPUT_RATE_DEFAULT;
176 return input_clock_GetRate( p_owner->p_clock );
180 * Spawns a new decoder thread
182 * \param p_input the input thread
183 * \param p_es the es descriptor
184 * \return the spawned decoder object
186 decoder_t *input_DecoderNew( input_thread_t *p_input,
187 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
189 decoder_t *p_dec = NULL;
193 (void)b_force_decoder;
195 /* If we are in sout mode, search for packetizer module */
198 /* Create the decoder configuration structure */
199 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
202 msg_Err( p_input, "could not create packetizer" );
203 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
204 _("VLC could not open the packetizer module.") );
211 /* Create the decoder configuration structure */
212 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
215 msg_Err( p_input, "could not create decoder" );
216 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
217 _("VLC could not open the decoder module.") );
222 if( !p_dec->p_module )
224 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
226 DeleteDecoder( p_dec );
227 vlc_object_release( p_dec );
231 p_dec->p_owner->p_clock = p_clock;
233 if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
235 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
236 p_dec->p_owner->b_own_thread = false;
240 var_Get( p_input, "minimize-threads", &val );
241 p_dec->p_owner->b_own_thread = !val.b_bool;
244 if( p_dec->p_owner->b_own_thread )
247 if( fmt->i_cat == AUDIO_ES )
248 i_priority = VLC_THREAD_PRIORITY_AUDIO;
250 i_priority = VLC_THREAD_PRIORITY_VIDEO;
252 /* Spawn the decoder thread */
253 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
254 i_priority, false ) )
256 msg_Err( p_dec, "cannot spawn decoder thread" );
257 module_unneed( p_dec, p_dec->p_module );
258 DeleteDecoder( p_dec );
259 vlc_object_release( p_dec );
268 * Kills a decoder thread and waits until it's finished
270 * \param p_input the input thread
271 * \param p_es the es descriptor
274 void input_DecoderDelete( decoder_t *p_dec )
276 decoder_owner_sys_t *p_owner = p_dec->p_owner;
278 vlc_object_kill( p_dec );
280 if( p_owner->b_own_thread )
282 /* Make sure we aren't paused anymore */
283 vlc_mutex_lock( &p_owner->lock );
284 if( p_owner->b_paused )
286 p_owner->b_paused = false;
287 vlc_cond_signal( &p_owner->wait );
289 vlc_mutex_unlock( &p_owner->lock );
291 /* Make sure the thread leaves the function */
292 block_FifoWake( p_owner->p_fifo );
294 vlc_thread_join( p_dec );
296 /* Don't module_unneed() here because of the dll loader that wants
297 * close() in the same thread than open()/decode() */
302 input_DecoderDecode( p_dec, NULL );
304 module_unneed( p_dec, p_dec->p_module );
308 if( p_dec->p_owner->cc.b_supported )
311 for( i = 0; i < 4; i++ )
312 input_DecoderSetCcState( p_dec, false, i );
315 /* Delete decoder configuration */
316 DeleteDecoder( p_dec );
318 /* Delete the decoder */
319 vlc_object_release( p_dec );
323 * Put a block_t in the decoder's fifo.
325 * \param p_dec the decoder object
326 * \param p_block the data block
328 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
330 decoder_owner_sys_t *p_owner = p_dec->p_owner;
332 if( p_owner->b_own_thread )
334 if( p_owner->p_input->p->b_out_pace_control )
337 while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
338 block_FifoCount( p_owner->p_fifo ) > 10 )
343 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
345 /* FIXME: ideally we would check the time amount of data
346 * in the fifo instead of its size. */
347 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
348 "consumed quickly enough), resetting fifo!" );
349 block_FifoEmpty( p_owner->p_fifo );
352 block_FifoPut( p_owner->p_fifo, p_block );
356 if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
359 block_Release( p_block );
363 DecoderProcess( p_dec, p_block );
368 bool input_DecoderIsEmpty( decoder_t * p_dec )
370 if( p_dec->p_owner->b_own_thread &&
371 block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
378 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
380 decoder_owner_sys_t *p_owner = p_dec->p_owner;
383 vlc_mutex_lock( &p_owner->lock );
384 for( i = 0; i < 4; i++ )
385 pb_present[i] = p_owner->cc.pb_present[i];
386 vlc_mutex_unlock( &p_owner->lock );
388 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
390 decoder_owner_sys_t *p_owner = p_dec->p_owner;
392 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
394 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
399 static const vlc_fourcc_t fcc[4] = {
400 VLC_FOURCC('c', 'c', '1', ' '),
401 VLC_FOURCC('c', 'c', '2', ' '),
402 VLC_FOURCC('c', 'c', '3', ' '),
403 VLC_FOURCC('c', 'c', '4', ' '),
408 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
409 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
412 msg_Err( p_dec, "could not create decoder" );
413 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
414 _("VLC could not open the decoder module.") );
417 else if( !p_cc->p_module )
419 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
420 DeleteDecoder( p_cc );
421 vlc_object_release( p_cc );
424 p_cc->p_owner->p_clock = p_owner->p_clock;
426 vlc_mutex_lock( &p_owner->lock );
427 p_owner->cc.pp_decoder[i_channel] = p_cc;
428 vlc_mutex_unlock( &p_owner->lock );
434 vlc_mutex_lock( &p_owner->lock );
435 p_cc = p_owner->cc.pp_decoder[i_channel];
436 p_owner->cc.pp_decoder[i_channel] = NULL;
437 vlc_mutex_unlock( &p_owner->lock );
441 vlc_object_kill( p_cc );
442 module_unneed( p_cc, p_cc->p_module );
443 DeleteDecoder( p_cc );
444 vlc_object_release( p_cc );
449 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
451 decoder_owner_sys_t *p_owner = p_dec->p_owner;
454 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
457 vlc_mutex_lock( &p_owner->lock );
458 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
459 vlc_mutex_unlock( &p_owner->lock );
463 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
465 decoder_owner_sys_t *p_owner = p_dec->p_owner;
467 vlc_mutex_lock( &p_owner->lock );
469 assert( !p_owner->b_paused || !b_paused );
470 p_owner->b_paused = b_paused;
471 p_owner->i_pause_date = i_date;
472 if( p_owner->b_own_thread )
473 vlc_cond_signal( &p_owner->wait );
475 DecoderOutputChangePause( p_dec, b_paused, i_date );
476 vlc_mutex_unlock( &p_owner->lock );
479 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
481 decoder_owner_sys_t *p_owner = p_dec->p_owner;
483 vlc_mutex_lock( &p_owner->lock );
484 p_owner->i_ts_delay = i_delay;
485 vlc_mutex_unlock( &p_owner->lock );
488 void input_DecoderStartBuffering( decoder_t *p_dec )
490 decoder_owner_sys_t *p_owner = p_dec->p_owner;
492 vlc_mutex_lock( &p_owner->lock );
494 DecoderFlush( p_dec );
496 vlc_mutex_unlock( &p_owner->lock );
499 void input_DecoderStopBuffering( decoder_t *p_dec )
503 /*****************************************************************************
505 *****************************************************************************/
508 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
510 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
511 "VLC probably does not support this sound or video format.",
513 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
514 _("VLC does not support the audio or video format \"%4.4s\". "
515 "Unfortunately there is no way for you to fix this."), (char*)&codec );
520 * Create a decoder object
522 * \param p_input the input thread
523 * \param p_es the es descriptor
524 * \param i_object_type Object type as define in include/vlc_objects.h
525 * \return the decoder object
527 static decoder_t * CreateDecoder( input_thread_t *p_input,
528 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
531 decoder_owner_sys_t *p_owner;
534 p_dec = vlc_object_create( p_input, i_object_type );
538 p_dec->pf_decode_audio = NULL;
539 p_dec->pf_decode_video = NULL;
540 p_dec->pf_decode_sub = NULL;
541 p_dec->pf_get_cc = NULL;
542 p_dec->pf_packetize = NULL;
544 /* Initialize the decoder fifo */
545 p_dec->p_module = NULL;
547 memset( &null_es_format, 0, sizeof(es_format_t) );
548 es_format_Copy( &p_dec->fmt_in, fmt );
549 es_format_Copy( &p_dec->fmt_out, &null_es_format );
551 /* Allocate our private structure for the decoder */
552 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
553 if( p_dec->p_owner == NULL )
555 vlc_object_release( p_dec );
558 p_dec->p_owner->b_own_thread = true;
559 p_dec->p_owner->i_preroll_end = -1;
560 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
561 p_dec->p_owner->p_input = p_input;
562 p_dec->p_owner->p_aout = NULL;
563 p_dec->p_owner->p_aout_input = NULL;
564 p_dec->p_owner->p_vout = NULL;
565 p_dec->p_owner->p_spu_vout = NULL;
566 p_dec->p_owner->i_spu_channel = 0;
567 p_dec->p_owner->i_spu_order = 0;
568 p_dec->p_owner->p_sout = p_sout;
569 p_dec->p_owner->p_sout_input = NULL;
570 p_dec->p_owner->p_packetizer = NULL;
573 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
575 free( p_dec->p_owner );
576 vlc_object_release( p_dec );
580 /* Set buffers allocation callbacks for the decoders */
581 p_dec->pf_aout_buffer_new = aout_new_buffer;
582 p_dec->pf_aout_buffer_del = aout_del_buffer;
583 p_dec->pf_vout_buffer_new = vout_new_buffer;
584 p_dec->pf_vout_buffer_del = vout_del_buffer;
585 p_dec->pf_picture_link = vout_link_picture;
586 p_dec->pf_picture_unlink = vout_unlink_picture;
587 p_dec->pf_spu_buffer_new = spu_new_buffer;
588 p_dec->pf_spu_buffer_del = spu_del_buffer;
590 vlc_object_attach( p_dec, p_input );
592 /* Find a suitable decoder/packetizer module */
593 if( i_object_type == VLC_OBJECT_DECODER )
594 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
596 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
598 /* Check if decoder requires already packetized data */
599 if( i_object_type == VLC_OBJECT_DECODER &&
600 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
602 p_dec->p_owner->p_packetizer =
603 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
604 if( p_dec->p_owner->p_packetizer )
606 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
609 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
612 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
614 p_dec->p_owner->p_packetizer->p_module =
615 module_need( p_dec->p_owner->p_packetizer,
616 "packetizer", "$packetizer", 0 );
618 if( !p_dec->p_owner->p_packetizer->p_module )
620 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
621 vlc_object_detach( p_dec->p_owner->p_packetizer );
622 vlc_object_release( p_dec->p_owner->p_packetizer );
627 /* Copy ourself the input replay gain */
628 if( fmt->i_cat == AUDIO_ES )
630 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
632 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
634 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
635 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
637 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
639 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
640 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
645 p_owner->cc.b_supported = false;
646 if( i_object_type == VLC_OBJECT_DECODER )
648 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
649 p_owner->cc.b_supported = true;
650 if( p_dec->pf_get_cc )
651 p_owner->cc.b_supported = true;
654 vlc_mutex_init( &p_owner->lock );
655 vlc_cond_init( &p_owner->wait );
656 p_owner->b_paused = false;
657 p_owner->i_pause_date = 0;
658 p_owner->b_flushing = false;
659 for( i = 0; i < 4; i++ )
661 p_owner->cc.pb_present[i] = false;
662 p_owner->cc.pp_decoder[i] = NULL;
664 p_owner->i_ts_delay = 0;
669 * The decoding main loop
671 * \param p_dec the decoder
673 static void *DecoderThread( vlc_object_t *p_this )
675 decoder_t *p_dec = (decoder_t *)p_this;
676 decoder_owner_sys_t *p_owner = p_dec->p_owner;
679 int canc = vlc_savecancel();
681 /* The decoder's main loop */
682 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
684 if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
687 if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
691 while( vlc_object_alive( p_dec ) )
693 /* Trash all received PES packets */
694 p_block = block_FifoGet( p_owner->p_fifo );
696 block_Release( p_block );
699 /* We do it here because of the dll loader that wants close() in the
700 * same thread than open()/decode() */
701 module_unneed( p_dec, p_dec->p_module );
702 vlc_restorecancel( canc );
706 static void DecoderFlush( decoder_t *p_dec )
708 decoder_owner_sys_t *p_owner = p_dec->p_owner;
711 if( p_owner->b_own_thread )
713 vlc_assert_locked( &p_owner->lock );
716 block_FifoEmpty( p_owner->p_fifo );
718 /* Monitor for flush end */
719 p_owner->b_flushing = true;
720 vlc_cond_signal( &p_owner->wait );
723 /* Send a special block */
724 p_null = block_New( p_dec, 128 );
727 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
728 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
729 if( !p_dec->fmt_in.b_packetized )
730 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
731 memset( p_null->p_buffer, 0, p_null->i_buffer );
733 input_DecoderDecode( p_dec, p_null );
736 if( p_owner->b_own_thread )
738 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
739 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
743 static void DecoderWaitUnpause( decoder_t *p_dec, bool *pb_reject )
745 decoder_owner_sys_t *p_owner = p_dec->p_owner;
747 vlc_mutex_lock( &p_owner->lock );
749 while( p_owner->b_paused && !p_owner->b_flushing )
750 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
753 *pb_reject = p_owner->b_flushing;
755 vlc_mutex_unlock( &p_owner->lock );
758 static void DecoderSignalFlushed( decoder_t *p_dec )
760 decoder_owner_sys_t *p_owner = p_dec->p_owner;
762 vlc_mutex_lock( &p_owner->lock );
764 if( p_owner->b_flushing )
766 p_owner->b_flushing = false;
767 vlc_cond_signal( &p_owner->wait );
770 vlc_mutex_unlock( &p_owner->lock );
773 static bool DecoderIsFlushing( decoder_t *p_dec )
775 decoder_owner_sys_t *p_owner = p_dec->p_owner;
778 vlc_mutex_lock( &p_owner->lock );
780 b_flushing = p_owner->b_flushing;
782 vlc_mutex_unlock( &p_owner->lock );
787 static void DecoderGetDelays( decoder_t *p_dec, mtime_t *pi_ts_delay, mtime_t *pi_es_delay )
789 decoder_owner_sys_t *p_owner = p_dec->p_owner;
791 *pi_ts_delay = p_owner->p_input->i_pts_delay;
793 vlc_mutex_lock( &p_owner->lock );
795 *pi_es_delay = p_owner->i_ts_delay;
797 vlc_mutex_unlock( &p_owner->lock );
800 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
802 decoder_owner_sys_t *p_owner = p_dec->p_owner;
804 vlc_assert_locked( &p_owner->lock );
806 /* XXX only audio and video output have to be paused.
807 * - for sout it is useless
808 * - for subs, it is done by the vout
810 if( p_dec->fmt_in.i_cat == AUDIO_ES )
812 if( p_owner->p_aout && p_owner->p_aout_input )
813 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
816 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
818 if( p_owner->p_vout )
819 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
822 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
824 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
825 *pi_preroll = INT64_MAX;
826 else if( p->i_pts > 0 )
827 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
828 else if( p->i_dts > 0 )
829 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
832 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
834 mtime_t current_date = mdate();
836 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
837 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
839 /* ETSI EN 300 472 Annex A : do not take into account the PTS
840 * for teletext streams. */
841 return current_date + 400000 + i_ts_delay;
846 static void DecoderSoutBufferFixTs( block_t *p_block,
847 input_clock_t *p_clock,
848 mtime_t i_ts_delay, mtime_t i_es_delay,
853 if( !p_block->i_dts && !p_block->i_pts )
854 p_block->i_rate = input_clock_GetRate( p_clock );
856 if( p_block->i_dts > 0 )
857 p_block->i_dts = input_clock_GetTS( p_clock, &p_block->i_rate,
858 i_ts_delay, p_block->i_dts + i_es_delay);
860 if( p_block->i_pts > 0 )
861 p_block->i_pts = input_clock_GetTS( p_clock, &p_block->i_rate,
862 i_ts_delay, p_block->i_pts + i_es_delay );
864 if( p_block->i_length > 0 )
865 p_block->i_length = ( p_block->i_length * p_block->i_rate +
866 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
869 p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
871 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer, int *pi_rate,
872 input_clock_t *p_clock,
873 mtime_t i_ts_delay, mtime_t i_es_delay )
875 /* sout display module does not set clock */
879 if( !p_buffer->start_date && !p_buffer->end_date )
880 *pi_rate = input_clock_GetRate( p_clock );
882 if( p_buffer->start_date > 0 )
883 p_buffer->start_date =
884 input_clock_GetTS( p_clock, pi_rate,
885 i_ts_delay, p_buffer->start_date + i_es_delay );
887 if( p_buffer->end_date > 0 )
889 input_clock_GetTS( p_clock, pi_rate,
890 i_ts_delay, p_buffer->end_date + i_es_delay );
892 static void DecoderVoutBufferFixTs( picture_t *p_picture, int *pi_rate,
893 input_clock_t *p_clock,
894 mtime_t i_ts_delay, mtime_t i_es_delay )
896 /* sout display module does not set clock */
900 if( p_picture->date > 0 )
902 input_clock_GetTS( p_clock, pi_rate,
903 i_ts_delay, p_picture->date + i_es_delay );
905 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
906 input_clock_t *p_clock,
907 mtime_t i_ts_delay, mtime_t i_es_delay,
910 bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
912 /* sout display module does not set clock */
916 if( p_subpic->i_start > 0 )
918 input_clock_GetTS( p_clock, NULL,
919 i_ts_delay, p_subpic->i_start + i_es_delay );
921 if( p_subpic->i_stop > 0 )
923 input_clock_GetTS( p_clock, NULL,
924 i_ts_delay, p_subpic->i_stop + i_es_delay );
926 /* Do not create ephemere picture because of rounding errors */
927 if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
931 p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
934 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
936 decoder_owner_sys_t *p_owner = p_dec->p_owner;
937 input_thread_t *p_input = p_owner->p_input;
938 input_clock_t *p_clock = p_owner->p_clock;
939 aout_buffer_t *p_aout_buf;
944 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
946 aout_instance_t *p_aout = p_owner->p_aout;
947 aout_input_t *p_aout_input = p_owner->p_aout_input;
953 /* It prevent freezing VLC in case of broken decoder */
954 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
956 block_Release( p_block );
961 if( p_aout_buf->start_date < p_owner->i_preroll_end )
963 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
967 if( p_owner->i_preroll_end > 0 )
969 msg_Dbg( p_dec, "End of audio preroll" );
970 if( p_owner->p_aout && p_owner->p_aout_input )
971 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
973 p_owner->i_preroll_end = -1;
977 DecoderWaitUnpause( p_dec, &b_reject );
979 int i_rate = INPUT_RATE_DEFAULT;
982 DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
984 DecoderAoutBufferFixTs( p_aout_buf, &i_rate, p_clock, i_ts_delay, i_es_delay );
986 if( !p_clock && p_block && p_block->i_rate > 0 )
987 i_rate = p_block->i_rate;
989 /* FIXME TODO take care of audio-delay for mdate check */
990 const mtime_t i_max_date = mdate() + i_ts_delay +
991 i_es_delay * i_rate / INPUT_RATE_DEFAULT + AOUT_MAX_ADVANCE_TIME;
994 p_aout_buf->start_date > 0 &&
995 p_aout_buf->start_date <= i_max_date &&
996 i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
997 i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
999 /* Wait if we are too early
1000 * FIXME that's plain ugly to do it here */
1001 mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
1003 if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
1005 i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
1009 if( p_aout_buf->start_date <= 0 )
1011 msg_Warn( p_dec, "non-dated audio buffer received" );
1013 else if( p_aout_buf->start_date > i_max_date )
1015 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1016 p_aout_buf->start_date - mdate() );
1019 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1024 /* Update ugly stat */
1025 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1027 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1029 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1031 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1033 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1036 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1039 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1041 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1047 assert( p_dec_cc->pf_get_cc != NULL );
1049 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1050 if( !p_owner->cc.b_supported )
1053 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1057 vlc_mutex_lock( &p_owner->lock );
1058 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1060 p_owner->cc.pb_present[i] |= pb_present[i];
1061 if( p_owner->cc.pp_decoder[i] )
1065 for( i = 0; i < 4; i++ )
1067 if( !p_owner->cc.pp_decoder[i] )
1070 if( i_cc_decoder > 1 )
1071 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1073 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1076 vlc_mutex_unlock( &p_owner->lock );
1078 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
1080 vlc_mutex_lock( &p_vout->picture_lock );
1082 if( p_pic->i_status == READY_PICTURE )
1084 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
1087 else if( p_pic->i_refcount > 0 )
1089 p_pic->i_status = DISPLAYED_PICTURE;
1093 p_pic->i_status = DESTROYED_PICTURE;
1094 picture_CleanupQuant( p_pic );
1095 p_vout->i_heap_size--;
1098 vlc_mutex_unlock( &p_vout->picture_lock );
1100 static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
1103 vlc_mutex_lock( &p_vout->picture_lock );
1104 for( i = 0; i < p_vout->render.i_pictures; i++ )
1106 picture_t *p_pic = p_vout->render.pp_picture[i];
1108 if( p_pic->i_status == READY_PICTURE ||
1109 p_pic->i_status == DISPLAYED_PICTURE )
1111 /* We cannot change picture status if it is in READY_PICTURE state,
1112 * Just make sure they won't be displayed */
1113 if( p_pic->date > i_max_date )
1114 p_pic->date = i_max_date;
1117 vlc_mutex_unlock( &p_vout->picture_lock );
1120 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1122 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1123 input_thread_t *p_input = p_owner->p_input;
1127 int i_displayed = 0;
1129 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1131 vout_thread_t *p_vout = p_owner->p_vout;
1134 /* It prevent freezing VLC in case of broken decoder */
1135 VoutDisplayedPicture( p_vout, p_pic );
1137 block_Release( p_block );
1143 if( p_pic->date < p_owner->i_preroll_end )
1145 VoutDisplayedPicture( p_vout, p_pic );
1149 if( p_owner->i_preroll_end > 0 )
1151 msg_Dbg( p_dec, "End of video preroll" );
1153 VoutFlushPicture( p_vout, 1 );
1155 p_owner->i_preroll_end = -1;
1158 if( p_dec->pf_get_cc &&
1159 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1160 DecoderGetCc( p_dec, p_dec );
1163 DecoderWaitUnpause( p_dec, &b_reject );
1167 DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1168 int i_rate = INPUT_RATE_DEFAULT;
1170 DecoderVoutBufferFixTs( p_pic, &i_rate, p_owner->p_clock, i_ts_delay, i_es_delay );
1173 const mtime_t i_max_date = mdate() + i_ts_delay +
1174 i_es_delay * i_rate / INPUT_RATE_DEFAULT + VOUT_BOGUS_DELAY;
1177 p_pic->date > 0 && p_pic->date < i_max_date )
1179 if( i_rate != p_owner->i_last_rate )
1181 /* Be sure to not display old picture after our own */
1182 VoutFlushPicture( p_vout, p_pic->date );
1183 p_owner->i_last_rate = i_rate;
1186 vout_DatePicture( p_vout, p_pic, p_pic->date );
1188 vout_DisplayPicture( p_vout, p_pic );
1192 if( p_pic->date <= 0 )
1194 msg_Warn( p_vout, "non-dated video buffer received" );
1198 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1199 p_pic->date - mdate() );
1202 VoutDisplayedPicture( p_vout, p_pic );
1206 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1208 i_displayed += i_tmp_display;
1209 i_lost += i_tmp_lost;
1211 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1213 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1215 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1217 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1220 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1223 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1227 /* This function process a block for sout
1229 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1231 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1232 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1233 block_t *p_sout_block;
1235 while( ( p_sout_block =
1236 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1238 if( !p_owner->p_sout_input )
1240 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1242 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1243 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1244 if( p_dec->fmt_in.psz_language )
1246 if( p_owner->sout.psz_language )
1247 free( p_owner->sout.psz_language );
1248 p_owner->sout.psz_language =
1249 strdup( p_dec->fmt_in.psz_language );
1252 p_owner->p_sout_input =
1253 sout_InputNew( p_owner->p_sout,
1256 if( p_owner->p_sout_input == NULL )
1258 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1259 (char *)&p_owner->sout.i_codec );
1260 p_dec->b_error = true;
1262 while( p_sout_block )
1264 block_t *p_next = p_sout_block->p_next;
1265 block_Release( p_sout_block );
1266 p_sout_block = p_next;
1272 while( p_sout_block )
1274 block_t *p_next = p_sout_block->p_next;
1276 p_sout_block->p_next = NULL;
1278 DecoderWaitUnpause( p_dec, NULL );
1282 DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1284 DecoderSoutBufferFixTs( p_sout_block, p_owner->p_clock,
1285 i_ts_delay, i_es_delay, b_telx );
1287 sout_InputSendBuffer( p_owner->p_sout_input,
1290 p_sout_block = p_next;
1293 /* For now it's enough, as only sout impact on this flag */
1294 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1295 p_owner->p_input->p->b_out_pace_control )
1297 msg_Dbg( p_dec, "switching to sync mode" );
1298 p_owner->p_input->p->b_out_pace_control = false;
1300 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1301 !p_owner->p_input->p->b_out_pace_control )
1303 msg_Dbg( p_dec, "switching to async mode" );
1304 p_owner->p_input->p->b_out_pace_control = true;
1309 /* This function process a video block
1311 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1313 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1315 if( p_owner->p_packetizer )
1317 block_t *p_packetized_block;
1318 decoder_t *p_packetizer = p_owner->p_packetizer;
1320 while( (p_packetized_block =
1321 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1323 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1325 es_format_Clean( &p_dec->fmt_in );
1326 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1328 if( p_packetizer->pf_get_cc )
1329 DecoderGetCc( p_dec, p_packetizer );
1331 while( p_packetized_block )
1333 block_t *p_next = p_packetized_block->p_next;
1334 p_packetized_block->p_next = NULL;
1336 DecoderDecodeVideo( p_dec, p_packetized_block );
1338 p_packetized_block = p_next;
1344 DecoderDecodeVideo( p_dec, p_block );
1347 if( b_flush && p_owner->p_vout )
1348 VoutFlushPicture( p_owner->p_vout, 1 );
1351 /* This function process a audio block
1353 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1355 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1357 if( p_owner->p_packetizer )
1359 block_t *p_packetized_block;
1360 decoder_t *p_packetizer = p_owner->p_packetizer;
1362 while( (p_packetized_block =
1363 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1365 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1367 es_format_Clean( &p_dec->fmt_in );
1368 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1371 while( p_packetized_block )
1373 block_t *p_next = p_packetized_block->p_next;
1374 p_packetized_block->p_next = NULL;
1376 DecoderDecodeAudio( p_dec, p_packetized_block );
1378 p_packetized_block = p_next;
1384 DecoderDecodeAudio( p_dec, p_block );
1387 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1388 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1391 /* This function process a subtitle block
1393 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1395 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1396 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1398 input_thread_t *p_input = p_owner->p_input;
1399 vout_thread_t *p_vout;
1400 subpicture_t *p_spu;
1402 if( b_flush && p_owner->p_spu_vout )
1404 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1406 if( p_vout && p_owner->p_spu_vout == p_vout )
1407 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1408 p_owner->i_spu_channel );
1411 vlc_object_release( p_vout );
1414 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1416 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1417 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1418 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1420 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1421 if( p_vout && p_owner->p_spu_vout == p_vout )
1423 /* Preroll does not work very well with subtitle */
1424 if( p_spu->i_start > 0 &&
1425 p_spu->i_start < p_owner->i_preroll_end &&
1426 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1428 subpicture_Delete( p_spu );
1433 DecoderWaitUnpause( p_dec, &b_reject );
1437 DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1439 DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, i_ts_delay, i_es_delay, b_telx );
1441 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1443 subpicture_Delete( p_spu );
1448 subpicture_Delete( p_spu );
1451 vlc_object_release( p_vout );
1458 * \param p_dec the decoder object
1459 * \param p_block the block to decode
1460 * \return VLC_SUCCESS or an error code
1462 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1464 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1465 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1467 if( p_block && p_block->i_buffer <= 0 )
1469 assert( !b_flush_request );
1470 block_Release( p_block );
1475 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1478 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1480 DecoderProcessSout( p_dec, p_block );
1485 bool b_flush = false;
1489 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1490 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1492 b_flush = !b_flushing && b_flush_request;
1494 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1497 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1499 DecoderProcessAudio( p_dec, p_block, b_flush );
1501 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1503 DecoderProcessVideo( p_dec, p_block, b_flush );
1505 else if( p_dec->fmt_in.i_cat == SPU_ES )
1507 DecoderProcessSpu( p_dec, p_block, b_flush );
1511 msg_Err( p_dec, "unknown ES format" );
1512 p_dec->b_error = true;
1517 if( b_flush_request )
1518 DecoderSignalFlushed( p_dec );
1520 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1524 * Destroys a decoder object
1526 * \param p_dec the decoder object
1529 static void DeleteDecoder( decoder_t * p_dec )
1531 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1533 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1534 (char*)&p_dec->fmt_in.i_codec,
1535 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1537 /* Free all packets still in the decoder fifo. */
1538 block_FifoEmpty( p_owner->p_fifo );
1539 block_FifoRelease( p_owner->p_fifo );
1542 if( p_owner->p_aout_input )
1543 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1544 if( p_owner->p_aout )
1546 vlc_object_release( p_owner->p_aout );
1547 p_owner->p_aout = NULL;
1549 if( p_owner->p_vout )
1553 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1554 /* Hack to make sure all the the pictures are freed by the decoder */
1555 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1558 if( p_pic->i_status == RESERVED_PICTURE )
1559 vout_DestroyPicture( p_owner->p_vout, p_pic );
1560 if( p_pic->i_refcount > 0 )
1561 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1565 /* We are about to die. Reattach video output to p_vlc. */
1566 vout_Request( p_dec, p_owner->p_vout, NULL );
1567 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1571 if( p_owner->p_sout_input )
1573 sout_InputDelete( p_owner->p_sout_input );
1574 es_format_Clean( &p_owner->sout );
1578 if( p_dec->fmt_in.i_cat == SPU_ES )
1580 vout_thread_t *p_vout;
1582 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1583 if( p_vout && p_owner->p_spu_vout == p_vout )
1585 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1586 p_owner->i_spu_channel );
1587 vlc_object_release( p_vout );
1591 es_format_Clean( &p_dec->fmt_in );
1592 es_format_Clean( &p_dec->fmt_out );
1594 if( p_owner->p_packetizer )
1596 module_unneed( p_owner->p_packetizer,
1597 p_owner->p_packetizer->p_module );
1598 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1599 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1600 vlc_object_detach( p_owner->p_packetizer );
1601 vlc_object_release( p_owner->p_packetizer );
1604 vlc_cond_destroy( &p_owner->wait );
1605 vlc_mutex_destroy( &p_owner->lock );
1607 vlc_object_detach( p_dec );
1612 /*****************************************************************************
1613 * Buffers allocation callbacks for the decoders
1614 *****************************************************************************/
1615 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1617 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1618 aout_buffer_t *p_buffer;
1620 if( p_owner->p_aout_input != NULL &&
1621 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1622 p_dec->fmt_out.audio.i_original_channels !=
1623 p_owner->audio.i_original_channels ||
1624 p_dec->fmt_out.audio.i_bytes_per_frame !=
1625 p_owner->audio.i_bytes_per_frame ) )
1627 aout_input_t *p_aout_input = p_owner->p_aout_input;
1629 /* Parameters changed, restart the aout */
1630 vlc_mutex_lock( &p_owner->lock );
1632 p_owner->p_aout_input = NULL;
1633 aout_DecDelete( p_owner->p_aout, p_aout_input );
1635 vlc_mutex_unlock( &p_owner->lock );
1638 if( p_owner->p_aout_input == NULL )
1640 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1641 audio_sample_format_t format;
1642 aout_input_t *p_aout_input;
1643 aout_instance_t *p_aout;
1645 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1646 p_owner->audio = p_dec->fmt_out.audio;
1648 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1649 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1650 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1652 if ( i_force_dolby == 1 )
1654 format.i_original_channels = format.i_original_channels |
1655 AOUT_CHAN_DOLBYSTEREO;
1657 else /* i_force_dolby == 2 */
1659 format.i_original_channels = format.i_original_channels &
1660 ~AOUT_CHAN_DOLBYSTEREO;
1664 p_aout = p_owner->p_aout;
1665 p_aout_input = aout_DecNew( p_dec, &p_aout,
1666 &format, &p_dec->fmt_out.audio_replay_gain );
1668 vlc_mutex_lock( &p_owner->lock );
1669 p_owner->p_aout = p_aout;
1670 p_owner->p_aout_input = p_aout_input;
1671 vlc_mutex_unlock( &p_owner->lock );
1673 if( p_owner->p_aout_input == NULL )
1675 msg_Err( p_dec, "failed to create audio output" );
1676 p_dec->b_error = true;
1679 p_dec->fmt_out.audio.i_bytes_per_frame =
1680 p_owner->audio.i_bytes_per_frame;
1683 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1688 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1690 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1692 aout_DecDeleteBuffer( p_owner->p_aout,
1693 p_owner->p_aout_input, p_buffer );
1697 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1699 static picture_t *vout_new_buffer( decoder_t *p_dec )
1701 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1703 if( p_owner->p_vout == NULL ||
1704 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1705 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1706 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1707 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1709 vout_thread_t *p_vout;
1711 if( !p_dec->fmt_out.video.i_width ||
1712 !p_dec->fmt_out.video.i_height )
1714 /* Can't create a new vout without display size */
1718 if( !p_dec->fmt_out.video.i_visible_width ||
1719 !p_dec->fmt_out.video.i_visible_height )
1721 if( p_dec->fmt_in.video.i_visible_width &&
1722 p_dec->fmt_in.video.i_visible_height )
1724 p_dec->fmt_out.video.i_visible_width =
1725 p_dec->fmt_in.video.i_visible_width;
1726 p_dec->fmt_out.video.i_visible_height =
1727 p_dec->fmt_in.video.i_visible_height;
1731 p_dec->fmt_out.video.i_visible_width =
1732 p_dec->fmt_out.video.i_width;
1733 p_dec->fmt_out.video.i_visible_height =
1734 p_dec->fmt_out.video.i_height;
1738 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1739 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1741 p_dec->fmt_out.video.i_visible_height = 1080;
1742 p_dec->fmt_out.video.i_sar_num *= 135;
1743 p_dec->fmt_out.video.i_sar_den *= 136;
1744 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1747 if( !p_dec->fmt_out.video.i_sar_num ||
1748 !p_dec->fmt_out.video.i_sar_den )
1750 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1751 p_dec->fmt_out.video.i_visible_height;
1753 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1754 p_dec->fmt_out.video.i_visible_width;
1757 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1758 &p_dec->fmt_out.video.i_sar_den,
1759 p_dec->fmt_out.video.i_sar_num,
1760 p_dec->fmt_out.video.i_sar_den, 50000 );
1762 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1763 p_owner->video = p_dec->fmt_out.video;
1765 vlc_mutex_lock( &p_owner->lock );
1766 p_vout = p_owner->p_vout;
1767 p_owner->p_vout = NULL;
1768 vlc_mutex_unlock( &p_owner->lock );
1770 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1772 vlc_mutex_lock( &p_owner->lock );
1773 p_owner->p_vout = p_vout;
1774 vlc_mutex_unlock( &p_owner->lock );
1776 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1777 if( p_vout == NULL )
1779 msg_Err( p_dec, "failed to create video output" );
1780 p_dec->b_error = true;
1784 if( p_owner->video.i_rmask )
1785 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1786 if( p_owner->video.i_gmask )
1787 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1788 if( p_owner->video.i_bmask )
1789 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1792 /* Get a new picture
1796 picture_t *p_picture;
1797 int i_pic, i_ready_pic;
1799 if( p_dec->b_die || p_dec->b_error )
1802 /* The video filter chain required that there is always 1 free buffer
1803 * that it will use as temporary one. It will release the temporary
1804 * buffer once its work is done, so this check is safe even if we don't
1805 * lock around both count() and create().
1807 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1809 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1814 if( DecoderIsFlushing( p_dec ) )
1817 /* Check the decoder doesn't leak pictures */
1818 for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1820 const picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1822 if( p_pic->i_status == READY_PICTURE )
1825 /* If we have at least 2 ready pictures, wait for the vout thread to
1827 if( i_ready_pic >= 2 )
1833 if( p_pic->i_status == DISPLAYED_PICTURE )
1835 /* If at least one displayed picture is not referenced
1836 * let vout free it */
1837 if( p_pic->i_refcount == 0 )
1842 if( i_pic == p_owner->p_vout->render.i_pictures )
1844 /* Too many pictures are still referenced, there is probably a bug
1845 * with the decoder */
1846 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1848 /* Just free all the pictures */
1849 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1851 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1853 if( p_pic->i_status == RESERVED_PICTURE )
1854 vout_DestroyPicture( p_owner->p_vout, p_pic );
1855 if( p_pic->i_refcount > 0 )
1856 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1860 msleep( VOUT_OUTMEM_SLEEP );
1864 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1866 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1869 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1871 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1874 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1876 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1879 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1881 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1882 vout_thread_t *p_vout = NULL;
1883 subpicture_t *p_subpic;
1884 int i_attempts = 30;
1886 while( i_attempts-- )
1888 if( p_dec->b_die || p_dec->b_error )
1891 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1895 msleep( VOUT_DISPLAY_DELAY );
1900 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1904 if( p_owner->p_spu_vout != p_vout )
1906 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1907 &p_owner->i_spu_channel );
1908 p_owner->i_spu_order = 0;
1909 p_owner->p_spu_vout = p_vout;
1912 p_subpic = subpicture_New();
1915 p_subpic->i_channel = p_owner->i_spu_channel;
1916 p_subpic->i_order = p_owner->i_spu_order++;
1917 p_subpic->b_subtitle = true;
1920 vlc_object_release( p_vout );
1925 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1927 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1928 vout_thread_t *p_vout = NULL;
1930 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1931 if( !p_vout || p_owner->p_spu_vout != p_vout )
1934 vlc_object_release( p_vout );
1935 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1939 subpicture_Delete( p_subpic );
1941 vlc_object_release( p_vout );