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 * );
59 static void DecoderSignalBuffering( decoder_t *, bool );
60 static void DecoderFlushBuffering( decoder_t * );
62 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
64 /* Buffers allocation callbacks for the decoders */
65 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
66 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
68 static picture_t *vout_new_buffer( decoder_t * );
69 static void vout_del_buffer( decoder_t *, picture_t * );
70 static void vout_link_picture( decoder_t *, picture_t * );
71 static void vout_unlink_picture( decoder_t *, picture_t * );
73 static subpicture_t *spu_new_buffer( decoder_t * );
74 static void spu_del_buffer( decoder_t *, subpicture_t * );
76 static es_format_t null_es_format;
78 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 -- */
130 picture_t *p_picture;
131 picture_t **pp_picture_next;
133 subpicture_t *p_subpic;
134 subpicture_t **pp_subpic_next;
136 aout_buffer_t *p_audio;
137 aout_buffer_t **pp_audio_next;
148 decoder_t *pp_decoder[4];
155 #define DECODER_MAX_BUFFERING_COUNT (4)
156 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
157 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
159 /*****************************************************************************
161 *****************************************************************************/
163 /* decoder_GetInputAttachment:
165 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
166 const char *psz_name )
168 input_attachment_t *p_attachment;
169 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
173 /* decoder_GetInputAttachments:
175 int decoder_GetInputAttachments( decoder_t *p_dec,
176 input_attachment_t ***ppp_attachment,
179 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
180 ppp_attachment, pi_attachment );
182 /* decoder_GetDisplayDate:
184 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
186 decoder_owner_sys_t *p_owner = p_dec->p_owner;
188 vlc_mutex_lock( &p_owner->lock );
189 if( p_owner->b_buffering )
191 vlc_mutex_unlock( &p_owner->lock );
193 if( !p_owner->p_clock || !i_ts )
196 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
198 /* decoder_GetDisplayRate:
200 int decoder_GetDisplayRate( decoder_t *p_dec )
202 decoder_owner_sys_t *p_owner = p_dec->p_owner;
204 if( !p_owner->p_clock )
205 return INPUT_RATE_DEFAULT;
206 return input_clock_GetRate( p_owner->p_clock );
210 * Spawns a new decoder thread
212 * \param p_input the input thread
213 * \param p_es the es descriptor
214 * \return the spawned decoder object
216 decoder_t *input_DecoderNew( input_thread_t *p_input,
217 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
219 decoder_t *p_dec = NULL;
223 (void)b_force_decoder;
225 /* If we are in sout mode, search for packetizer module */
228 /* Create the decoder configuration structure */
229 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
232 msg_Err( p_input, "could not create packetizer" );
233 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
234 _("VLC could not open the packetizer module.") );
241 /* Create the decoder configuration structure */
242 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
245 msg_Err( p_input, "could not create decoder" );
246 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
247 _("VLC could not open the decoder module.") );
252 if( !p_dec->p_module )
254 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
256 DeleteDecoder( p_dec );
257 vlc_object_release( p_dec );
261 p_dec->p_owner->p_clock = p_clock;
263 if( fmt->i_cat == AUDIO_ES )
264 i_priority = VLC_THREAD_PRIORITY_AUDIO;
266 i_priority = VLC_THREAD_PRIORITY_VIDEO;
268 /* Spawn the decoder thread */
269 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
270 i_priority, false ) )
272 msg_Err( p_dec, "cannot spawn decoder thread" );
273 module_unneed( p_dec, p_dec->p_module );
274 DeleteDecoder( p_dec );
275 vlc_object_release( p_dec );
283 * Kills a decoder thread and waits until it's finished
285 * \param p_input the input thread
286 * \param p_es the es descriptor
289 void input_DecoderDelete( decoder_t *p_dec )
291 decoder_owner_sys_t *p_owner = p_dec->p_owner;
293 vlc_object_kill( p_dec );
295 /* Make sure we aren't paused anymore */
296 vlc_mutex_lock( &p_owner->lock );
297 if( p_owner->b_paused || p_owner->b_buffering )
299 p_owner->b_paused = false;
300 p_owner->b_buffering = false;
301 vlc_cond_signal( &p_owner->wait );
303 vlc_mutex_unlock( &p_owner->lock );
305 /* Make sure the thread leaves the function */
306 block_FifoWake( p_owner->p_fifo );
308 vlc_thread_join( p_dec );
310 /* Don't module_unneed() here because of the dll loader that wants
311 * close() in the same thread than open()/decode() */
314 if( p_dec->p_owner->cc.b_supported )
317 for( i = 0; i < 4; i++ )
318 input_DecoderSetCcState( p_dec, false, i );
321 /* Delete decoder configuration */
322 DeleteDecoder( p_dec );
324 /* Delete the decoder */
325 vlc_object_release( p_dec );
329 * Put a block_t in the decoder's fifo.
331 * \param p_dec the decoder object
332 * \param p_block the data block
334 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
338 if( p_owner->p_input->p->b_out_pace_control )
341 while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
342 block_FifoCount( p_owner->p_fifo ) > 10 )
347 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
349 /* FIXME: ideally we would check the time amount of data
350 * in the fifo instead of its size. */
351 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
352 "consumed quickly enough), resetting fifo!" );
353 block_FifoEmpty( p_owner->p_fifo );
356 block_FifoPut( p_owner->p_fifo, p_block );
359 bool input_DecoderIsEmpty( decoder_t * p_dec )
361 assert( !p_dec->p_owner->b_buffering );
363 /* FIXME that's not really true */
364 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
367 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
369 decoder_owner_sys_t *p_owner = p_dec->p_owner;
372 vlc_mutex_lock( &p_owner->lock );
373 for( i = 0; i < 4; i++ )
374 pb_present[i] = p_owner->cc.pb_present[i];
375 vlc_mutex_unlock( &p_owner->lock );
377 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
379 decoder_owner_sys_t *p_owner = p_dec->p_owner;
381 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
383 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
388 static const vlc_fourcc_t fcc[4] = {
389 VLC_FOURCC('c', 'c', '1', ' '),
390 VLC_FOURCC('c', 'c', '2', ' '),
391 VLC_FOURCC('c', 'c', '3', ' '),
392 VLC_FOURCC('c', 'c', '4', ' '),
397 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
398 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
401 msg_Err( p_dec, "could not create decoder" );
402 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
403 _("VLC could not open the decoder module.") );
406 else if( !p_cc->p_module )
408 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
409 DeleteDecoder( p_cc );
410 vlc_object_release( p_cc );
413 p_cc->p_owner->p_clock = p_owner->p_clock;
415 vlc_mutex_lock( &p_owner->lock );
416 p_owner->cc.pp_decoder[i_channel] = p_cc;
417 vlc_mutex_unlock( &p_owner->lock );
423 vlc_mutex_lock( &p_owner->lock );
424 p_cc = p_owner->cc.pp_decoder[i_channel];
425 p_owner->cc.pp_decoder[i_channel] = NULL;
426 vlc_mutex_unlock( &p_owner->lock );
430 vlc_object_kill( p_cc );
431 module_unneed( p_cc, p_cc->p_module );
432 DeleteDecoder( p_cc );
433 vlc_object_release( p_cc );
438 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
440 decoder_owner_sys_t *p_owner = p_dec->p_owner;
443 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
446 vlc_mutex_lock( &p_owner->lock );
447 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
448 vlc_mutex_unlock( &p_owner->lock );
452 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
454 decoder_owner_sys_t *p_owner = p_dec->p_owner;
456 vlc_mutex_lock( &p_owner->lock );
458 assert( !p_owner->b_paused || !b_paused );
460 p_owner->b_paused = b_paused;
461 p_owner->pause.i_date = i_date;
462 vlc_cond_signal( &p_owner->wait );
464 DecoderOutputChangePause( p_dec, b_paused, i_date );
466 vlc_mutex_unlock( &p_owner->lock );
469 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
471 decoder_owner_sys_t *p_owner = p_dec->p_owner;
473 vlc_mutex_lock( &p_owner->lock );
474 p_owner->i_ts_delay = i_delay;
475 vlc_mutex_unlock( &p_owner->lock );
478 void input_DecoderStartBuffering( decoder_t *p_dec )
480 decoder_owner_sys_t *p_owner = p_dec->p_owner;
482 vlc_mutex_lock( &p_owner->lock );
484 DecoderFlush( p_dec );
486 p_owner->buffer.b_full = false;
487 p_owner->buffer.i_count = 0;
489 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
491 p_owner->buffer.p_picture = NULL;
492 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
494 p_owner->buffer.p_subpic = NULL;
495 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
497 p_owner->buffer.p_audio = NULL;
498 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
500 p_owner->b_buffering = true;
502 vlc_cond_signal( &p_owner->wait );
504 vlc_mutex_unlock( &p_owner->lock );
507 void input_DecoderStopBuffering( decoder_t *p_dec )
509 decoder_owner_sys_t *p_owner = p_dec->p_owner;
511 vlc_mutex_lock( &p_owner->lock );
513 p_owner->b_buffering = false;
515 vlc_cond_signal( &p_owner->wait );
517 vlc_mutex_unlock( &p_owner->lock );
520 void input_DecoderWaitBuffering( decoder_t *p_dec )
522 decoder_owner_sys_t *p_owner = p_dec->p_owner;
524 vlc_mutex_lock( &p_owner->lock );
526 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
528 block_FifoWake( p_owner->p_fifo );
529 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
532 vlc_mutex_unlock( &p_owner->lock );
535 /*****************************************************************************
537 *****************************************************************************/
540 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
542 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
543 "VLC probably does not support this sound or video format.",
545 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
546 _("VLC does not support the audio or video format \"%4.4s\". "
547 "Unfortunately there is no way for you to fix this."), (char*)&codec );
552 * Create a decoder object
554 * \param p_input the input thread
555 * \param p_es the es descriptor
556 * \param i_object_type Object type as define in include/vlc_objects.h
557 * \return the decoder object
559 static decoder_t * CreateDecoder( input_thread_t *p_input,
560 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
563 decoder_owner_sys_t *p_owner;
566 p_dec = vlc_object_create( p_input, i_object_type );
570 p_dec->pf_decode_audio = NULL;
571 p_dec->pf_decode_video = NULL;
572 p_dec->pf_decode_sub = NULL;
573 p_dec->pf_get_cc = NULL;
574 p_dec->pf_packetize = NULL;
576 /* Initialize the decoder fifo */
577 p_dec->p_module = NULL;
579 memset( &null_es_format, 0, sizeof(es_format_t) );
580 es_format_Copy( &p_dec->fmt_in, fmt );
581 es_format_Copy( &p_dec->fmt_out, &null_es_format );
583 /* Allocate our private structure for the decoder */
584 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
585 if( p_dec->p_owner == NULL )
587 vlc_object_release( p_dec );
590 p_dec->p_owner->i_preroll_end = -1;
591 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
592 p_dec->p_owner->p_input = p_input;
593 p_dec->p_owner->p_aout = NULL;
594 p_dec->p_owner->p_aout_input = NULL;
595 p_dec->p_owner->p_vout = NULL;
596 p_dec->p_owner->p_spu_vout = NULL;
597 p_dec->p_owner->i_spu_channel = 0;
598 p_dec->p_owner->i_spu_order = 0;
599 p_dec->p_owner->p_sout = p_sout;
600 p_dec->p_owner->p_sout_input = NULL;
601 p_dec->p_owner->p_packetizer = NULL;
604 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
606 free( p_dec->p_owner );
607 vlc_object_release( p_dec );
611 /* Set buffers allocation callbacks for the decoders */
612 p_dec->pf_aout_buffer_new = aout_new_buffer;
613 p_dec->pf_aout_buffer_del = aout_del_buffer;
614 p_dec->pf_vout_buffer_new = vout_new_buffer;
615 p_dec->pf_vout_buffer_del = vout_del_buffer;
616 p_dec->pf_picture_link = vout_link_picture;
617 p_dec->pf_picture_unlink = vout_unlink_picture;
618 p_dec->pf_spu_buffer_new = spu_new_buffer;
619 p_dec->pf_spu_buffer_del = spu_del_buffer;
621 vlc_object_attach( p_dec, p_input );
623 /* Find a suitable decoder/packetizer module */
624 if( i_object_type == VLC_OBJECT_DECODER )
625 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
627 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
629 /* Check if decoder requires already packetized data */
630 if( i_object_type == VLC_OBJECT_DECODER &&
631 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
633 p_dec->p_owner->p_packetizer =
634 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
635 if( p_dec->p_owner->p_packetizer )
637 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
640 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
643 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
645 p_dec->p_owner->p_packetizer->p_module =
646 module_need( p_dec->p_owner->p_packetizer,
647 "packetizer", "$packetizer", 0 );
649 if( !p_dec->p_owner->p_packetizer->p_module )
651 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
652 vlc_object_detach( p_dec->p_owner->p_packetizer );
653 vlc_object_release( p_dec->p_owner->p_packetizer );
658 /* Copy ourself the input replay gain */
659 if( fmt->i_cat == AUDIO_ES )
661 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
663 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
665 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
666 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
668 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
670 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
671 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
677 vlc_mutex_init( &p_owner->lock );
678 vlc_cond_init( &p_owner->wait );
680 p_owner->b_paused = false;
681 p_owner->pause.i_date = 0;
683 p_owner->b_buffering = false;
684 p_owner->buffer.b_full = false;
685 p_owner->buffer.i_count = 0;
686 p_owner->buffer.p_picture = NULL;
687 p_owner->buffer.p_subpic = NULL;
688 p_owner->buffer.p_audio = NULL;
690 p_owner->b_flushing = false;
693 p_owner->cc.b_supported = false;
694 if( i_object_type == VLC_OBJECT_DECODER )
696 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
697 p_owner->cc.b_supported = true;
698 if( p_dec->pf_get_cc )
699 p_owner->cc.b_supported = true;
702 for( i = 0; i < 4; i++ )
704 p_owner->cc.pb_present[i] = false;
705 p_owner->cc.pp_decoder[i] = NULL;
707 p_owner->i_ts_delay = 0;
712 * The decoding main loop
714 * \param p_dec the decoder
716 static void *DecoderThread( vlc_object_t *p_this )
718 decoder_t *p_dec = (decoder_t *)p_this;
719 decoder_owner_sys_t *p_owner = p_dec->p_owner;
721 int canc = vlc_savecancel();
723 /* The decoder's main loop */
724 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
726 block_t *p_block = block_FifoGet( p_owner->p_fifo );
728 DecoderSignalBuffering( p_dec, p_block == NULL );
730 if( p_block && DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
734 while( vlc_object_alive( p_dec ) )
736 block_t *p_block = block_FifoGet( p_owner->p_fifo );
738 DecoderSignalBuffering( p_dec, p_block == NULL );
740 /* Trash all received PES packets */
742 block_Release( p_block );
744 DecoderSignalBuffering( p_dec, true );
746 /* We do it here because of the dll loader that wants close() in the
747 * same thread than open()/decode() */
748 module_unneed( p_dec, p_dec->p_module );
749 vlc_restorecancel( canc );
753 static void DecoderFlush( decoder_t *p_dec )
755 decoder_owner_sys_t *p_owner = p_dec->p_owner;
758 vlc_assert_locked( &p_owner->lock );
761 block_FifoEmpty( p_owner->p_fifo );
763 /* Monitor for flush end */
764 p_owner->b_flushing = true;
765 vlc_cond_signal( &p_owner->wait );
767 /* Send a special block */
768 p_null = block_New( p_dec, 128 );
771 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
772 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
773 if( !p_dec->fmt_in.b_packetized )
774 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
775 memset( p_null->p_buffer, 0, p_null->i_buffer );
777 input_DecoderDecode( p_dec, p_null );
780 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
781 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
784 static void DecoderSignalFlushed( decoder_t *p_dec )
786 decoder_owner_sys_t *p_owner = p_dec->p_owner;
788 vlc_mutex_lock( &p_owner->lock );
790 if( p_owner->b_flushing )
792 p_owner->b_flushing = false;
793 vlc_cond_signal( &p_owner->wait );
796 vlc_mutex_unlock( &p_owner->lock );
799 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
801 decoder_owner_sys_t *p_owner = p_dec->p_owner;
803 vlc_mutex_lock( &p_owner->lock );
805 if( p_owner->b_buffering )
808 p_owner->buffer.b_full = true;
809 vlc_cond_signal( &p_owner->wait );
812 vlc_mutex_unlock( &p_owner->lock );
815 static bool DecoderIsFlushing( decoder_t *p_dec )
817 decoder_owner_sys_t *p_owner = p_dec->p_owner;
820 vlc_mutex_lock( &p_owner->lock );
822 b_flushing = p_owner->b_flushing;
824 vlc_mutex_unlock( &p_owner->lock );
829 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
831 decoder_owner_sys_t *p_owner = p_dec->p_owner;
833 vlc_assert_locked( &p_owner->lock );
835 while( !p_owner->b_flushing )
837 if( !p_owner->b_paused && ( !p_owner->b_buffering || !p_owner->buffer.b_full ) )
839 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
843 *pb_reject = p_owner->b_flushing;
846 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
848 decoder_owner_sys_t *p_owner = p_dec->p_owner;
850 vlc_assert_locked( &p_owner->lock );
852 /* XXX only audio and video output have to be paused.
853 * - for sout it is useless
854 * - for subs, it is done by the vout
856 if( p_dec->fmt_in.i_cat == AUDIO_ES )
858 if( p_owner->p_aout && p_owner->p_aout_input )
859 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
862 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
864 if( p_owner->p_vout )
865 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
868 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
870 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
871 *pi_preroll = INT64_MAX;
872 else if( p->i_pts > 0 )
873 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
874 else if( p->i_dts > 0 )
875 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
878 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
880 mtime_t current_date = mdate();
882 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
883 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
885 /* ETSI EN 300 472 Annex A : do not take into account the PTS
886 * for teletext streams. */
887 return current_date + 400000 + i_ts_delay;
892 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
893 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
895 decoder_owner_sys_t *p_owner = p_dec->p_owner;
896 input_clock_t *p_clock = p_owner->p_clock;
899 vlc_assert_locked( &p_owner->lock );
901 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
902 const mtime_t i_es_delay = p_owner->i_ts_delay;
906 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
909 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
910 *pi_ts0 + i_es_delay );
911 if( pi_ts1 && *pi_ts1 > 0 )
912 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
913 *pi_ts1 + i_es_delay );
915 /* Do not create ephemere data because of rounding errors */
916 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
920 i_rate = input_clock_GetRate( p_clock );
923 *pi_duration = ( *pi_duration * i_rate +
924 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
931 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
932 if( pi_ts1 && *pi_ts1 <= 0 )
938 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
939 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
943 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
944 int *pi_played_sum, int *pi_lost_sum )
946 decoder_owner_sys_t *p_owner = p_dec->p_owner;
947 aout_instance_t *p_aout = p_owner->p_aout;
948 aout_input_t *p_aout_input = p_owner->p_aout_input;
951 if( p_audio->start_date <= 0 )
953 msg_Warn( p_dec, "non-dated audio buffer received" );
955 aout_BufferFree( p_audio );
960 vlc_mutex_lock( &p_owner->lock );
962 if( p_owner->b_buffering || p_owner->buffer.p_audio )
964 p_audio->p_next = NULL;
966 *p_owner->buffer.pp_audio_next = p_audio;
967 p_owner->buffer.pp_audio_next = &p_audio->p_next;
969 p_owner->buffer.i_count++;
970 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
971 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
973 p_owner->buffer.b_full = true;
974 vlc_cond_signal( &p_owner->wait );
980 bool b_has_more = false;
982 DecoderWaitUnblock( p_dec, &b_reject );
984 if( p_owner->b_buffering )
986 vlc_mutex_unlock( &p_owner->lock );
991 if( p_owner->buffer.p_audio )
993 p_audio = p_owner->buffer.p_audio;
995 p_owner->buffer.p_audio = p_audio->p_next;
997 b_has_more = p_owner->buffer.p_audio != NULL;
999 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1003 int i_rate = INPUT_RATE_DEFAULT;
1006 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1007 &i_rate, &i_delay, false );
1009 vlc_mutex_unlock( &p_owner->lock );
1012 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1014 if( !p_aout || !p_aout_input ||
1015 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1016 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1017 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1022 /* Wait if we are too early
1023 * FIXME that's plain ugly to do it here */
1024 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1026 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1027 *pi_played_sum += 1;
1028 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1032 if( p_audio->start_date <= 0 )
1034 msg_Warn( p_dec, "non-dated audio buffer received" );
1036 else if( p_audio->start_date > i_max_date )
1038 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1039 p_audio->start_date - mdate() );
1042 aout_BufferFree( p_audio );
1047 vlc_mutex_lock( &p_owner->lock );
1051 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1053 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1054 input_thread_t *p_input = p_owner->p_input;
1055 aout_buffer_t *p_aout_buf;
1060 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1062 aout_instance_t *p_aout = p_owner->p_aout;
1063 aout_input_t *p_aout_input = p_owner->p_aout_input;
1069 /* It prevent freezing VLC in case of broken decoder */
1070 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1072 block_Release( p_block );
1077 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1079 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1083 if( p_owner->i_preroll_end > 0 )
1085 msg_Dbg( p_dec, "End of audio preroll" );
1086 if( p_owner->p_aout && p_owner->p_aout_input )
1087 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1089 p_owner->i_preroll_end = -1;
1092 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1095 /* Update ugly stat */
1096 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1098 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1100 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1102 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1104 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1107 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1110 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1112 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1118 assert( p_dec_cc->pf_get_cc != NULL );
1120 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1121 if( !p_owner->cc.b_supported )
1124 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1128 vlc_mutex_lock( &p_owner->lock );
1129 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1131 p_owner->cc.pb_present[i] |= pb_present[i];
1132 if( p_owner->cc.pp_decoder[i] )
1136 for( i = 0; i < 4; i++ )
1138 if( !p_owner->cc.pp_decoder[i] )
1141 if( i_cc_decoder > 1 )
1142 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1144 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1147 vlc_mutex_unlock( &p_owner->lock );
1149 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
1151 vlc_mutex_lock( &p_vout->picture_lock );
1153 if( p_pic->i_status == READY_PICTURE )
1155 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
1158 else if( p_pic->i_refcount > 0 )
1160 p_pic->i_status = DISPLAYED_PICTURE;
1164 p_pic->i_status = DESTROYED_PICTURE;
1165 picture_CleanupQuant( p_pic );
1166 p_vout->i_heap_size--;
1169 vlc_mutex_unlock( &p_vout->picture_lock );
1171 static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
1174 vlc_mutex_lock( &p_vout->picture_lock );
1175 for( i = 0; i < p_vout->render.i_pictures; i++ )
1177 picture_t *p_pic = p_vout->render.pp_picture[i];
1179 if( p_pic->i_status == READY_PICTURE ||
1180 p_pic->i_status == DISPLAYED_PICTURE )
1182 /* We cannot change picture status if it is in READY_PICTURE state,
1183 * Just make sure they won't be displayed */
1184 if( p_pic->date > i_max_date )
1185 p_pic->date = i_max_date;
1188 vlc_mutex_unlock( &p_vout->picture_lock );
1191 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1192 int *pi_played_sum, int *pi_lost_sum )
1194 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1195 vout_thread_t *p_vout = p_owner->p_vout;
1197 if( p_picture->date <= 0 )
1199 msg_Warn( p_vout, "non-dated video buffer received" );
1201 VoutDisplayedPicture( p_vout, p_picture );
1206 vlc_mutex_lock( &p_owner->lock );
1208 if( p_owner->b_buffering || p_owner->buffer.p_picture )
1210 p_picture->p_next = NULL;
1212 *p_owner->buffer.pp_picture_next = p_picture;
1213 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1215 p_owner->buffer.i_count++;
1216 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1217 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1219 p_owner->buffer.b_full = true;
1220 vlc_cond_signal( &p_owner->wait );
1226 bool b_has_more = false;
1229 DecoderWaitUnblock( p_dec, &b_reject );
1231 if( p_owner->b_buffering )
1233 vlc_mutex_unlock( &p_owner->lock );
1238 if( p_owner->buffer.p_picture )
1240 p_picture = p_owner->buffer.p_picture;
1242 p_owner->buffer.p_picture = p_picture->p_next;
1244 b_has_more = p_owner->buffer.p_picture != NULL;
1246 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1250 int i_rate = INPUT_RATE_DEFAULT;
1253 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1254 &i_rate, &i_delay, false );
1256 vlc_mutex_unlock( &p_owner->lock );
1259 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1261 if( p_picture->date <= 0 || p_picture->date >= i_max_date )
1266 if( i_rate != p_owner->i_last_rate )
1268 /* Be sure to not display old picture after our own */
1269 VoutFlushPicture( p_vout, p_picture->date );
1270 p_owner->i_last_rate = i_rate;
1273 vout_DatePicture( p_vout, p_picture, p_picture->date );
1275 vout_DisplayPicture( p_vout, p_picture );
1279 if( p_picture->date <= 0 )
1281 msg_Warn( p_vout, "non-dated video buffer received" );
1285 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1286 p_picture->date - mdate() );
1289 VoutDisplayedPicture( p_vout, p_picture );
1293 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1295 *pi_played_sum += i_tmp_display;
1296 *pi_lost_sum += i_tmp_lost;
1300 vlc_mutex_lock( &p_owner->lock );
1304 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1306 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1307 input_thread_t *p_input = p_owner->p_input;
1311 int i_displayed = 0;
1313 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1315 vout_thread_t *p_vout = p_owner->p_vout;
1318 /* It prevent freezing VLC in case of broken decoder */
1319 VoutDisplayedPicture( p_vout, p_pic );
1321 block_Release( p_block );
1327 if( p_pic->date < p_owner->i_preroll_end )
1329 VoutDisplayedPicture( p_vout, p_pic );
1333 if( p_owner->i_preroll_end > 0 )
1335 msg_Dbg( p_dec, "End of video preroll" );
1337 VoutFlushPicture( p_vout, 1 );
1339 p_owner->i_preroll_end = -1;
1342 if( p_dec->pf_get_cc &&
1343 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1344 DecoderGetCc( p_dec, p_dec );
1346 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1348 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1350 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1352 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1354 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1357 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1360 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1364 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1367 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1368 vout_thread_t *p_vout = p_owner->p_spu_vout;
1371 if( p_subpic->i_start <= 0 )
1373 msg_Warn( p_dec, "non-dated spu buffer received" );
1374 subpicture_Delete( p_subpic );
1379 vlc_mutex_lock( &p_owner->lock );
1381 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1383 p_subpic->p_next = NULL;
1385 *p_owner->buffer.pp_subpic_next = p_subpic;
1386 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1388 p_owner->buffer.i_count++;
1389 /* XXX it is important to be full after the first one */
1390 if( p_owner->buffer.i_count > 0 )
1392 p_owner->buffer.b_full = true;
1393 vlc_cond_signal( &p_owner->wait );
1399 bool b_has_more = false;
1401 DecoderWaitUnblock( p_dec, &b_reject );
1403 if( p_owner->b_buffering )
1405 vlc_mutex_unlock( &p_owner->lock );
1410 if( p_owner->buffer.p_subpic )
1412 p_subpic = p_owner->buffer.p_subpic;
1414 p_owner->buffer.p_subpic = p_subpic->p_next;
1416 b_has_more = p_owner->buffer.p_subpic != NULL;
1418 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1422 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1423 NULL, NULL, b_telx );
1425 vlc_mutex_unlock( &p_owner->lock );
1428 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1430 subpicture_Delete( p_subpic );
1434 vlc_mutex_lock( &p_owner->lock );
1438 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1441 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1443 assert( p_owner->p_clock );
1445 vlc_mutex_lock( &p_owner->lock );
1448 DecoderWaitUnblock( p_dec, &b_reject );
1450 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1451 &p_sout_block->i_rate, NULL, b_telx );
1453 vlc_mutex_unlock( &p_owner->lock );
1455 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1459 static void DecoderFlushBuffering( decoder_t *p_dec )
1461 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1463 vlc_assert_locked( &p_owner->lock );
1465 while( p_owner->buffer.p_picture )
1467 picture_t *p_picture = p_owner->buffer.p_picture;
1469 p_owner->buffer.p_picture = p_picture->p_next;
1470 p_owner->buffer.i_count--;
1472 if( p_owner->p_vout )
1473 VoutDisplayedPicture( p_owner->p_vout, p_picture );
1475 if( !p_owner->buffer.p_picture )
1476 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1478 while( p_owner->buffer.p_audio )
1480 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1482 p_owner->buffer.p_audio = p_audio->p_next;
1483 p_owner->buffer.i_count--;
1485 aout_BufferFree( p_audio );
1487 if( !p_owner->buffer.p_audio )
1488 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1490 while( p_owner->buffer.p_subpic )
1492 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1494 p_owner->buffer.p_subpic = p_subpic->p_next;
1495 p_owner->buffer.i_count--;
1497 subpicture_Delete( p_subpic );
1499 if( !p_owner->buffer.p_subpic )
1500 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1504 /* This function process a block for sout
1506 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1508 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1509 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1510 block_t *p_sout_block;
1512 while( ( p_sout_block =
1513 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1515 if( !p_owner->p_sout_input )
1517 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1519 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1520 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1521 if( p_dec->fmt_in.psz_language )
1523 if( p_owner->sout.psz_language )
1524 free( p_owner->sout.psz_language );
1525 p_owner->sout.psz_language =
1526 strdup( p_dec->fmt_in.psz_language );
1529 p_owner->p_sout_input =
1530 sout_InputNew( p_owner->p_sout,
1533 if( p_owner->p_sout_input == NULL )
1535 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1536 (char *)&p_owner->sout.i_codec );
1537 p_dec->b_error = true;
1539 while( p_sout_block )
1541 block_t *p_next = p_sout_block->p_next;
1542 block_Release( p_sout_block );
1543 p_sout_block = p_next;
1549 while( p_sout_block )
1551 block_t *p_next = p_sout_block->p_next;
1553 p_sout_block->p_next = NULL;
1555 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1557 p_sout_block = p_next;
1560 /* For now it's enough, as only sout impact on this flag */
1561 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1562 p_owner->p_input->p->b_out_pace_control )
1564 msg_Dbg( p_dec, "switching to sync mode" );
1565 p_owner->p_input->p->b_out_pace_control = false;
1567 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1568 !p_owner->p_input->p->b_out_pace_control )
1570 msg_Dbg( p_dec, "switching to async mode" );
1571 p_owner->p_input->p->b_out_pace_control = true;
1576 /* This function process a video block
1578 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1580 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1582 if( p_owner->p_packetizer )
1584 block_t *p_packetized_block;
1585 decoder_t *p_packetizer = p_owner->p_packetizer;
1587 while( (p_packetized_block =
1588 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1590 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1592 es_format_Clean( &p_dec->fmt_in );
1593 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1595 if( p_packetizer->pf_get_cc )
1596 DecoderGetCc( p_dec, p_packetizer );
1598 while( p_packetized_block )
1600 block_t *p_next = p_packetized_block->p_next;
1601 p_packetized_block->p_next = NULL;
1603 DecoderDecodeVideo( p_dec, p_packetized_block );
1605 p_packetized_block = p_next;
1611 DecoderDecodeVideo( p_dec, p_block );
1614 if( b_flush && p_owner->p_vout )
1615 VoutFlushPicture( p_owner->p_vout, 1 );
1618 /* This function process a audio block
1620 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1622 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1624 if( p_owner->p_packetizer )
1626 block_t *p_packetized_block;
1627 decoder_t *p_packetizer = p_owner->p_packetizer;
1629 while( (p_packetized_block =
1630 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1632 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1634 es_format_Clean( &p_dec->fmt_in );
1635 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1638 while( p_packetized_block )
1640 block_t *p_next = p_packetized_block->p_next;
1641 p_packetized_block->p_next = NULL;
1643 DecoderDecodeAudio( p_dec, p_packetized_block );
1645 p_packetized_block = p_next;
1651 DecoderDecodeAudio( p_dec, p_block );
1654 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1655 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1658 /* This function process a subtitle block
1660 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1662 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1663 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1665 input_thread_t *p_input = p_owner->p_input;
1666 vout_thread_t *p_vout;
1667 subpicture_t *p_spu;
1669 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1671 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1672 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1673 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1675 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1676 if( p_vout && p_owner->p_spu_vout == p_vout )
1678 /* Preroll does not work very well with subtitle */
1679 if( p_spu->i_start > 0 &&
1680 p_spu->i_start < p_owner->i_preroll_end &&
1681 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1683 subpicture_Delete( p_spu );
1687 DecoderPlaySpu( p_dec, p_spu, b_telx );
1692 subpicture_Delete( p_spu );
1695 vlc_object_release( p_vout );
1698 if( b_flush && p_owner->p_spu_vout )
1700 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1702 if( p_vout && p_owner->p_spu_vout == p_vout )
1703 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1704 p_owner->i_spu_channel );
1707 vlc_object_release( p_vout );
1714 * \param p_dec the decoder object
1715 * \param p_block the block to decode
1716 * \return VLC_SUCCESS or an error code
1718 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1720 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1721 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1723 if( p_block && p_block->i_buffer <= 0 )
1725 assert( !b_flush_request );
1726 block_Release( p_block );
1731 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1734 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1736 DecoderProcessSout( p_dec, p_block );
1741 bool b_flush = false;
1745 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1746 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1748 b_flush = !b_flushing && b_flush_request;
1750 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1753 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1755 DecoderProcessAudio( p_dec, p_block, b_flush );
1757 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1759 DecoderProcessVideo( p_dec, p_block, b_flush );
1761 else if( p_dec->fmt_in.i_cat == SPU_ES )
1763 DecoderProcessSpu( p_dec, p_block, b_flush );
1767 msg_Err( p_dec, "unknown ES format" );
1768 p_dec->b_error = true;
1773 if( b_flush_request )
1775 vlc_mutex_lock( &p_owner->lock );
1776 DecoderFlushBuffering( p_dec );
1777 vlc_mutex_unlock( &p_owner->lock );
1779 DecoderSignalFlushed( p_dec );
1782 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1786 * Destroys a decoder object
1788 * \param p_dec the decoder object
1791 static void DeleteDecoder( decoder_t * p_dec )
1793 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1795 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1796 (char*)&p_dec->fmt_in.i_codec,
1797 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1799 /* Free all packets still in the decoder fifo. */
1800 block_FifoEmpty( p_owner->p_fifo );
1801 block_FifoRelease( p_owner->p_fifo );
1804 vlc_mutex_lock( &p_owner->lock );
1805 DecoderFlushBuffering( p_dec );
1806 vlc_mutex_unlock( &p_owner->lock );
1809 if( p_owner->p_aout_input )
1810 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1811 if( p_owner->p_aout )
1813 vlc_object_release( p_owner->p_aout );
1814 p_owner->p_aout = NULL;
1816 if( p_owner->p_vout )
1818 /* Hack to make sure all the the pictures are freed by the decoder */
1819 for( int i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1821 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1823 if( p_pic->i_status == RESERVED_PICTURE )
1824 vout_DestroyPicture( p_owner->p_vout, p_pic );
1825 if( p_pic->i_refcount > 0 )
1826 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1829 /* We are about to die. Reattach video output to p_vlc. */
1830 vout_Request( p_dec, p_owner->p_vout, NULL );
1831 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1835 if( p_owner->p_sout_input )
1837 sout_InputDelete( p_owner->p_sout_input );
1838 es_format_Clean( &p_owner->sout );
1842 if( p_dec->fmt_in.i_cat == SPU_ES )
1844 vout_thread_t *p_vout;
1846 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1847 if( p_vout && p_owner->p_spu_vout == p_vout )
1849 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1850 p_owner->i_spu_channel );
1851 vlc_object_release( p_vout );
1855 es_format_Clean( &p_dec->fmt_in );
1856 es_format_Clean( &p_dec->fmt_out );
1858 if( p_owner->p_packetizer )
1860 module_unneed( p_owner->p_packetizer,
1861 p_owner->p_packetizer->p_module );
1862 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1863 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1864 vlc_object_detach( p_owner->p_packetizer );
1865 vlc_object_release( p_owner->p_packetizer );
1868 vlc_cond_destroy( &p_owner->wait );
1869 vlc_mutex_destroy( &p_owner->lock );
1871 vlc_object_detach( p_dec );
1876 /*****************************************************************************
1877 * Buffers allocation callbacks for the decoders
1878 *****************************************************************************/
1879 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1881 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1882 aout_buffer_t *p_buffer;
1884 if( p_owner->p_aout_input != NULL &&
1885 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1886 p_dec->fmt_out.audio.i_original_channels !=
1887 p_owner->audio.i_original_channels ||
1888 p_dec->fmt_out.audio.i_bytes_per_frame !=
1889 p_owner->audio.i_bytes_per_frame ) )
1891 aout_input_t *p_aout_input = p_owner->p_aout_input;
1893 /* Parameters changed, restart the aout */
1894 vlc_mutex_lock( &p_owner->lock );
1896 DecoderFlushBuffering( p_dec );
1898 p_owner->p_aout_input = NULL;
1899 aout_DecDelete( p_owner->p_aout, p_aout_input );
1901 vlc_mutex_unlock( &p_owner->lock );
1904 if( p_owner->p_aout_input == NULL )
1906 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1907 audio_sample_format_t format;
1908 aout_input_t *p_aout_input;
1909 aout_instance_t *p_aout;
1911 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1912 p_owner->audio = p_dec->fmt_out.audio;
1914 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1915 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1916 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1918 if ( i_force_dolby == 1 )
1920 format.i_original_channels = format.i_original_channels |
1921 AOUT_CHAN_DOLBYSTEREO;
1923 else /* i_force_dolby == 2 */
1925 format.i_original_channels = format.i_original_channels &
1926 ~AOUT_CHAN_DOLBYSTEREO;
1930 p_aout = p_owner->p_aout;
1931 p_aout_input = aout_DecNew( p_dec, &p_aout,
1932 &format, &p_dec->fmt_out.audio_replay_gain );
1934 vlc_mutex_lock( &p_owner->lock );
1935 p_owner->p_aout = p_aout;
1936 p_owner->p_aout_input = p_aout_input;
1937 vlc_mutex_unlock( &p_owner->lock );
1939 if( p_owner->p_aout_input == NULL )
1941 msg_Err( p_dec, "failed to create audio output" );
1942 p_dec->b_error = true;
1945 p_dec->fmt_out.audio.i_bytes_per_frame =
1946 p_owner->audio.i_bytes_per_frame;
1949 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1954 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1956 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1958 aout_DecDeleteBuffer( p_owner->p_aout,
1959 p_owner->p_aout_input, p_buffer );
1963 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1965 static picture_t *vout_new_buffer( decoder_t *p_dec )
1967 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1969 if( p_owner->p_vout == NULL ||
1970 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1971 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1972 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1973 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1975 vout_thread_t *p_vout;
1977 if( !p_dec->fmt_out.video.i_width ||
1978 !p_dec->fmt_out.video.i_height )
1980 /* Can't create a new vout without display size */
1984 if( !p_dec->fmt_out.video.i_visible_width ||
1985 !p_dec->fmt_out.video.i_visible_height )
1987 if( p_dec->fmt_in.video.i_visible_width &&
1988 p_dec->fmt_in.video.i_visible_height )
1990 p_dec->fmt_out.video.i_visible_width =
1991 p_dec->fmt_in.video.i_visible_width;
1992 p_dec->fmt_out.video.i_visible_height =
1993 p_dec->fmt_in.video.i_visible_height;
1997 p_dec->fmt_out.video.i_visible_width =
1998 p_dec->fmt_out.video.i_width;
1999 p_dec->fmt_out.video.i_visible_height =
2000 p_dec->fmt_out.video.i_height;
2004 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2005 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2007 p_dec->fmt_out.video.i_visible_height = 1080;
2008 p_dec->fmt_out.video.i_sar_num *= 135;
2009 p_dec->fmt_out.video.i_sar_den *= 136;
2010 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2013 if( !p_dec->fmt_out.video.i_sar_num ||
2014 !p_dec->fmt_out.video.i_sar_den )
2016 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2017 p_dec->fmt_out.video.i_visible_height;
2019 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2020 p_dec->fmt_out.video.i_visible_width;
2023 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2024 &p_dec->fmt_out.video.i_sar_den,
2025 p_dec->fmt_out.video.i_sar_num,
2026 p_dec->fmt_out.video.i_sar_den, 50000 );
2028 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2029 p_owner->video = p_dec->fmt_out.video;
2031 vlc_mutex_lock( &p_owner->lock );
2033 DecoderFlushBuffering( p_dec );
2035 p_vout = p_owner->p_vout;
2036 p_owner->p_vout = NULL;
2037 vlc_mutex_unlock( &p_owner->lock );
2039 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2041 vlc_mutex_lock( &p_owner->lock );
2042 p_owner->p_vout = p_vout;
2043 vlc_mutex_unlock( &p_owner->lock );
2045 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2046 if( p_vout == NULL )
2048 msg_Err( p_dec, "failed to create video output" );
2049 p_dec->b_error = true;
2053 if( p_owner->video.i_rmask )
2054 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2055 if( p_owner->video.i_gmask )
2056 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2057 if( p_owner->video.i_bmask )
2058 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2061 /* Get a new picture
2065 picture_t *p_picture;
2066 int i_pic, i_ready_pic;
2068 if( p_dec->b_die || p_dec->b_error )
2071 /* The video filter chain required that there is always 1 free buffer
2072 * that it will use as temporary one. It will release the temporary
2073 * buffer once its work is done, so this check is safe even if we don't
2074 * lock around both count() and create().
2076 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2078 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2083 if( DecoderIsFlushing( p_dec ) )
2087 DecoderSignalBuffering( p_dec, true );
2089 /* Check the decoder doesn't leak pictures */
2090 for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
2092 const picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
2094 if( p_pic->i_status == READY_PICTURE )
2097 /* If we have at least 2 ready pictures, wait for the vout thread to
2099 if( i_ready_pic >= 2 )
2105 if( p_pic->i_status == DISPLAYED_PICTURE )
2107 /* If at least one displayed picture is not referenced
2108 * let vout free it */
2109 if( p_pic->i_refcount == 0 )
2114 if( i_pic == p_owner->p_vout->render.i_pictures )
2116 /* Too many pictures are still referenced, there is probably a bug
2117 * with the decoder */
2118 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
2120 /* Just free all the pictures */
2121 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
2123 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
2125 if( p_pic->i_status == RESERVED_PICTURE )
2126 vout_DestroyPicture( p_owner->p_vout, p_pic );
2127 if( p_pic->i_refcount > 0 )
2128 vout_UnlinkPicture( p_owner->p_vout, p_pic );
2132 msleep( VOUT_OUTMEM_SLEEP );
2136 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2138 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
2141 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2143 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2146 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2148 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2151 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2153 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2154 vout_thread_t *p_vout = NULL;
2155 subpicture_t *p_subpic;
2156 int i_attempts = 30;
2158 while( i_attempts-- )
2160 if( p_dec->b_die || p_dec->b_error )
2163 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2167 msleep( VOUT_DISPLAY_DELAY );
2172 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2176 if( p_owner->p_spu_vout != p_vout )
2178 vlc_mutex_lock( &p_owner->lock );
2180 DecoderFlushBuffering( p_dec );
2182 vlc_mutex_unlock( &p_owner->lock );
2184 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2185 &p_owner->i_spu_channel );
2186 p_owner->i_spu_order = 0;
2187 p_owner->p_spu_vout = p_vout;
2190 p_subpic = subpicture_New();
2193 p_subpic->i_channel = p_owner->i_spu_channel;
2194 p_subpic->i_order = p_owner->i_spu_order++;
2195 p_subpic->b_subtitle = true;
2198 vlc_object_release( p_vout );
2203 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2205 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2206 vout_thread_t *p_vout = NULL;
2208 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2209 if( !p_vout || p_owner->p_spu_vout != p_vout )
2212 vlc_object_release( p_vout );
2213 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2217 subpicture_Delete( p_subpic );
2219 vlc_object_release( p_vout );