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"
50 #include "../video_output/vout_control.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 -- */
132 picture_t *p_picture;
133 picture_t **pp_picture_next;
135 subpicture_t *p_subpic;
136 subpicture_t **pp_subpic_next;
138 aout_buffer_t *p_audio;
139 aout_buffer_t **pp_audio_next;
142 block_t **pp_block_next;
153 decoder_t *pp_decoder[4];
160 #define DECODER_MAX_BUFFERING_COUNT (4)
161 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
162 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
164 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
165 * a bogus PTS and won't be displayed */
166 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
169 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
172 /*****************************************************************************
174 *****************************************************************************/
175 picture_t *decoder_NewPicture( decoder_t *p_decoder )
177 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
179 msg_Warn( p_decoder, "can't get output picture" );
182 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
184 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
186 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
188 p_decoder->pf_picture_link( p_decoder, p_picture );
190 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
192 p_decoder->pf_picture_unlink( p_decoder, p_picture );
195 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
197 if( !p_decoder->pf_aout_buffer_new )
199 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
201 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
203 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
206 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
208 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
210 msg_Warn( p_decoder, "can't get output subpicture" );
213 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
215 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
218 /* decoder_GetInputAttachments:
220 int decoder_GetInputAttachments( decoder_t *p_dec,
221 input_attachment_t ***ppp_attachment,
224 if( !p_dec->pf_get_attachments )
227 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
229 /* decoder_GetDisplayDate:
231 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
233 if( !p_dec->pf_get_display_date )
236 return p_dec->pf_get_display_date( p_dec, i_ts );
238 /* decoder_GetDisplayRate:
240 int decoder_GetDisplayRate( decoder_t *p_dec )
242 if( !p_dec->pf_get_display_rate )
243 return INPUT_RATE_DEFAULT;
245 return p_dec->pf_get_display_rate( p_dec );
249 * Spawns a new decoder thread
251 * \param p_input the input thread
252 * \param p_es the es descriptor
253 * \return the spawned decoder object
255 decoder_t *input_DecoderNew( input_thread_t *p_input,
256 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
258 decoder_t *p_dec = NULL;
262 /* If we are in sout mode, search for packetizer module */
265 /* Create the decoder configuration structure */
266 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
269 msg_Err( p_input, "could not create packetizer" );
270 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
271 _("VLC could not open the packetizer module.") );
278 /* Create the decoder configuration structure */
279 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
282 msg_Err( p_input, "could not create decoder" );
283 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
284 _("VLC could not open the decoder module.") );
289 if( !p_dec->p_module )
291 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
293 DeleteDecoder( p_dec );
294 vlc_object_release( p_dec );
298 p_dec->p_owner->p_clock = p_clock;
300 if( fmt->i_cat == AUDIO_ES )
301 i_priority = VLC_THREAD_PRIORITY_AUDIO;
303 i_priority = VLC_THREAD_PRIORITY_VIDEO;
305 /* Spawn the decoder thread */
306 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
307 i_priority, false ) )
309 msg_Err( p_dec, "cannot spawn decoder thread" );
310 module_unneed( p_dec, p_dec->p_module );
311 DeleteDecoder( p_dec );
312 vlc_object_release( p_dec );
320 * Kills a decoder thread and waits until it's finished
322 * \param p_input the input thread
323 * \param p_es the es descriptor
326 void input_DecoderDelete( decoder_t *p_dec )
328 decoder_owner_sys_t *p_owner = p_dec->p_owner;
330 vlc_object_kill( p_dec );
332 /* Make sure we aren't paused anymore */
333 vlc_mutex_lock( &p_owner->lock );
334 if( p_owner->b_paused || p_owner->b_buffering )
336 p_owner->b_paused = false;
337 p_owner->b_buffering = false;
338 vlc_cond_signal( &p_owner->wait );
340 vlc_mutex_unlock( &p_owner->lock );
342 vlc_thread_join( p_dec );
343 module_unneed( p_dec, p_dec->p_module );
346 if( p_dec->p_owner->cc.b_supported )
349 for( i = 0; i < 4; i++ )
350 input_DecoderSetCcState( p_dec, false, i );
353 /* Delete decoder configuration */
354 DeleteDecoder( p_dec );
356 /* Delete the decoder */
357 vlc_object_release( p_dec );
361 * Put a block_t in the decoder's fifo.
362 * Thread-safe w.r.t. the decoder. May be a cancellation point.
364 * \param p_dec the decoder object
365 * \param p_block the data block
367 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
369 decoder_owner_sys_t *p_owner = p_dec->p_owner;
371 if( p_owner->p_input->p->b_out_pace_control )
373 /* The fifo is not consummed when buffering and so will
375 * There is no need to lock as b_buffering is never modify
376 * inside decoder thread. */
377 if( !p_owner->b_buffering )
378 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
380 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
382 /* FIXME: ideally we would check the time amount of data
383 * in the FIFO instead of its size. */
384 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
385 "consumed quickly enough), resetting fifo!" );
386 block_FifoEmpty( p_owner->p_fifo );
389 block_FifoPut( p_owner->p_fifo, p_block );
392 bool input_DecoderIsEmpty( decoder_t * p_dec )
394 assert( !p_dec->p_owner->b_buffering );
396 /* FIXME that's not really true */
397 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
400 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
402 decoder_owner_sys_t *p_owner = p_dec->p_owner;
405 vlc_mutex_lock( &p_owner->lock );
406 for( i = 0; i < 4; i++ )
407 pb_present[i] = p_owner->cc.pb_present[i];
408 vlc_mutex_unlock( &p_owner->lock );
410 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
412 decoder_owner_sys_t *p_owner = p_dec->p_owner;
414 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
416 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
421 static const vlc_fourcc_t fcc[4] = {
422 VLC_FOURCC('c', 'c', '1', ' '),
423 VLC_FOURCC('c', 'c', '2', ' '),
424 VLC_FOURCC('c', 'c', '3', ' '),
425 VLC_FOURCC('c', 'c', '4', ' '),
430 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
431 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
434 msg_Err( p_dec, "could not create decoder" );
435 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
436 _("VLC could not open the decoder module.") );
439 else if( !p_cc->p_module )
441 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
442 DeleteDecoder( p_cc );
443 vlc_object_release( p_cc );
446 p_cc->p_owner->p_clock = p_owner->p_clock;
448 vlc_mutex_lock( &p_owner->lock );
449 p_owner->cc.pp_decoder[i_channel] = p_cc;
450 vlc_mutex_unlock( &p_owner->lock );
456 vlc_mutex_lock( &p_owner->lock );
457 p_cc = p_owner->cc.pp_decoder[i_channel];
458 p_owner->cc.pp_decoder[i_channel] = NULL;
459 vlc_mutex_unlock( &p_owner->lock );
463 vlc_object_kill( p_cc );
464 module_unneed( p_cc, p_cc->p_module );
465 DeleteDecoder( p_cc );
466 vlc_object_release( p_cc );
471 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
473 decoder_owner_sys_t *p_owner = p_dec->p_owner;
476 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
479 vlc_mutex_lock( &p_owner->lock );
480 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
481 vlc_mutex_unlock( &p_owner->lock );
485 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
487 decoder_owner_sys_t *p_owner = p_dec->p_owner;
489 vlc_mutex_lock( &p_owner->lock );
491 assert( !p_owner->b_paused || !b_paused );
493 p_owner->b_paused = b_paused;
494 p_owner->pause.i_date = i_date;
495 p_owner->pause.i_ignore = 0;
496 vlc_cond_signal( &p_owner->wait );
498 DecoderOutputChangePause( p_dec, b_paused, i_date );
500 vlc_mutex_unlock( &p_owner->lock );
503 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
505 decoder_owner_sys_t *p_owner = p_dec->p_owner;
507 vlc_mutex_lock( &p_owner->lock );
508 p_owner->i_ts_delay = i_delay;
509 vlc_mutex_unlock( &p_owner->lock );
512 void input_DecoderStartBuffering( decoder_t *p_dec )
514 decoder_owner_sys_t *p_owner = p_dec->p_owner;
516 vlc_mutex_lock( &p_owner->lock );
518 DecoderFlush( p_dec );
520 p_owner->buffer.b_first = true;
521 p_owner->buffer.b_full = false;
522 p_owner->buffer.i_count = 0;
524 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
525 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
527 p_owner->buffer.p_picture = NULL;
528 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
530 p_owner->buffer.p_subpic = NULL;
531 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
533 p_owner->buffer.p_audio = NULL;
534 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
536 p_owner->buffer.p_block = NULL;
537 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
540 p_owner->b_buffering = true;
542 vlc_cond_signal( &p_owner->wait );
544 vlc_mutex_unlock( &p_owner->lock );
547 void input_DecoderStopBuffering( decoder_t *p_dec )
549 decoder_owner_sys_t *p_owner = p_dec->p_owner;
551 vlc_mutex_lock( &p_owner->lock );
553 p_owner->b_buffering = false;
555 vlc_cond_signal( &p_owner->wait );
557 vlc_mutex_unlock( &p_owner->lock );
560 void input_DecoderWaitBuffering( decoder_t *p_dec )
562 decoder_owner_sys_t *p_owner = p_dec->p_owner;
564 vlc_mutex_lock( &p_owner->lock );
566 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
568 block_FifoWake( p_owner->p_fifo );
569 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
572 vlc_mutex_unlock( &p_owner->lock );
574 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
576 decoder_owner_sys_t *p_owner = p_dec->p_owner;
580 vlc_mutex_lock( &p_owner->lock );
581 if( p_dec->fmt_in.i_cat == VIDEO_ES )
583 if( p_owner->b_paused && p_owner->p_vout )
585 vout_NextPicture( p_owner->p_vout, pi_duration );
586 p_owner->pause.i_ignore++;
587 vlc_cond_signal( &p_owner->wait );
592 /* TODO subtitle should not be flushed */
593 DecoderFlush( p_dec );
595 vlc_mutex_unlock( &p_owner->lock );
598 /*****************************************************************************
600 *****************************************************************************/
601 static int DecoderGetInputAttachments( decoder_t *p_dec,
602 input_attachment_t ***ppp_attachment,
605 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
606 ppp_attachment, pi_attachment );
608 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
610 decoder_owner_sys_t *p_owner = p_dec->p_owner;
612 vlc_mutex_lock( &p_owner->lock );
613 if( p_owner->b_buffering || p_owner->b_paused )
615 vlc_mutex_unlock( &p_owner->lock );
617 if( !p_owner->p_clock || !i_ts )
620 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
622 static int DecoderGetDisplayRate( decoder_t *p_dec )
624 decoder_owner_sys_t *p_owner = p_dec->p_owner;
626 if( !p_owner->p_clock )
627 return INPUT_RATE_DEFAULT;
628 return input_clock_GetRate( p_owner->p_clock );
632 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
634 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
635 "VLC probably does not support this sound or video format.",
637 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
638 _("VLC does not support the audio or video format \"%4.4s\". "
639 "Unfortunately there is no way for you to fix this."), (char*)&codec );
644 * Create a decoder object
646 * \param p_input the input thread
647 * \param p_es the es descriptor
648 * \param i_object_type Object type as define in include/vlc_objects.h
649 * \return the decoder object
651 static decoder_t * CreateDecoder( input_thread_t *p_input,
652 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
655 decoder_owner_sys_t *p_owner;
658 p_dec = vlc_object_create( p_input, i_object_type );
662 p_dec->pf_decode_audio = NULL;
663 p_dec->pf_decode_video = NULL;
664 p_dec->pf_decode_sub = NULL;
665 p_dec->pf_get_cc = NULL;
666 p_dec->pf_packetize = NULL;
668 /* Initialize the decoder fifo */
669 p_dec->p_module = NULL;
671 memset( &null_es_format, 0, sizeof(es_format_t) );
672 es_format_Copy( &p_dec->fmt_in, fmt );
673 es_format_Copy( &p_dec->fmt_out, &null_es_format );
675 /* Allocate our private structure for the decoder */
676 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
677 if( p_dec->p_owner == NULL )
679 vlc_object_release( p_dec );
682 p_dec->p_owner->i_preroll_end = -1;
683 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
684 p_dec->p_owner->p_input = p_input;
685 p_dec->p_owner->p_aout = NULL;
686 p_dec->p_owner->p_aout_input = NULL;
687 p_dec->p_owner->p_vout = NULL;
688 p_dec->p_owner->p_spu_vout = NULL;
689 p_dec->p_owner->i_spu_channel = 0;
690 p_dec->p_owner->i_spu_order = 0;
691 p_dec->p_owner->p_sout = p_sout;
692 p_dec->p_owner->p_sout_input = NULL;
693 p_dec->p_owner->p_packetizer = NULL;
696 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
698 free( p_dec->p_owner );
699 vlc_object_release( p_dec );
703 /* Set buffers allocation callbacks for the decoders */
704 p_dec->pf_aout_buffer_new = aout_new_buffer;
705 p_dec->pf_aout_buffer_del = aout_del_buffer;
706 p_dec->pf_vout_buffer_new = vout_new_buffer;
707 p_dec->pf_vout_buffer_del = vout_del_buffer;
708 p_dec->pf_picture_link = vout_link_picture;
709 p_dec->pf_picture_unlink = vout_unlink_picture;
710 p_dec->pf_spu_buffer_new = spu_new_buffer;
711 p_dec->pf_spu_buffer_del = spu_del_buffer;
713 p_dec->pf_get_attachments = DecoderGetInputAttachments;
714 p_dec->pf_get_display_date = DecoderGetDisplayDate;
715 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
717 vlc_object_attach( p_dec, p_input );
719 /* Find a suitable decoder/packetizer module */
720 if( i_object_type == VLC_OBJECT_DECODER )
721 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
723 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
725 /* Check if decoder requires already packetized data */
726 if( i_object_type == VLC_OBJECT_DECODER &&
727 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
729 p_dec->p_owner->p_packetizer =
730 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
731 if( p_dec->p_owner->p_packetizer )
733 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
736 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
739 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
741 p_dec->p_owner->p_packetizer->p_module =
742 module_need( p_dec->p_owner->p_packetizer,
743 "packetizer", "$packetizer", 0 );
745 if( !p_dec->p_owner->p_packetizer->p_module )
747 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
748 vlc_object_detach( p_dec->p_owner->p_packetizer );
749 vlc_object_release( p_dec->p_owner->p_packetizer );
754 /* Copy ourself the input replay gain */
755 if( fmt->i_cat == AUDIO_ES )
757 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
759 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
761 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
762 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
764 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
766 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
767 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
773 vlc_mutex_init( &p_owner->lock );
774 vlc_cond_init( &p_owner->wait );
776 p_owner->b_paused = false;
777 p_owner->pause.i_date = 0;
778 p_owner->pause.i_ignore = 0;
780 p_owner->b_buffering = false;
781 p_owner->buffer.b_first = true;
782 p_owner->buffer.b_full = false;
783 p_owner->buffer.i_count = 0;
784 p_owner->buffer.p_picture = NULL;
785 p_owner->buffer.p_subpic = NULL;
786 p_owner->buffer.p_audio = NULL;
787 p_owner->buffer.p_block = NULL;
789 p_owner->b_flushing = false;
792 p_owner->cc.b_supported = false;
793 if( i_object_type == VLC_OBJECT_DECODER )
795 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
796 p_owner->cc.b_supported = true;
797 if( p_dec->pf_get_cc )
798 p_owner->cc.b_supported = true;
801 for( i = 0; i < 4; i++ )
803 p_owner->cc.pb_present[i] = false;
804 p_owner->cc.pp_decoder[i] = NULL;
806 p_owner->i_ts_delay = 0;
811 * The decoding main loop
813 * \param p_dec the decoder
815 static void *DecoderThread( vlc_object_t *p_this )
817 decoder_t *p_dec = (decoder_t *)p_this;
818 decoder_owner_sys_t *p_owner = p_dec->p_owner;
820 /* The decoder's main loop */
823 block_t *p_block = block_FifoGet( p_owner->p_fifo );
824 /* Make sure there is no cancellation point other than this one^^.
825 * If you need one, be sure to push cleanup of p_block. */
826 DecoderSignalBuffering( p_dec, p_block == NULL );
831 block_Release( p_block );
833 DecoderProcess( p_dec, p_block );
837 DecoderSignalBuffering( p_dec, true );
841 static void DecoderFlush( decoder_t *p_dec )
843 decoder_owner_sys_t *p_owner = p_dec->p_owner;
846 vlc_assert_locked( &p_owner->lock );
849 block_FifoEmpty( p_owner->p_fifo );
851 /* Monitor for flush end */
852 p_owner->b_flushing = true;
853 vlc_cond_signal( &p_owner->wait );
855 /* Send a special block */
856 p_null = block_New( p_dec, 128 );
859 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
860 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
861 if( !p_dec->fmt_in.b_packetized )
862 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
863 memset( p_null->p_buffer, 0, p_null->i_buffer );
865 input_DecoderDecode( p_dec, p_null );
868 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
869 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
872 static void DecoderSignalFlushed( decoder_t *p_dec )
874 decoder_owner_sys_t *p_owner = p_dec->p_owner;
876 vlc_mutex_lock( &p_owner->lock );
878 if( p_owner->b_flushing )
880 p_owner->b_flushing = false;
881 vlc_cond_signal( &p_owner->wait );
884 vlc_mutex_unlock( &p_owner->lock );
887 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
889 decoder_owner_sys_t *p_owner = p_dec->p_owner;
891 vlc_mutex_lock( &p_owner->lock );
893 if( p_owner->b_buffering )
896 p_owner->buffer.b_full = true;
897 vlc_cond_signal( &p_owner->wait );
900 vlc_mutex_unlock( &p_owner->lock );
903 static bool DecoderIsFlushing( decoder_t *p_dec )
905 decoder_owner_sys_t *p_owner = p_dec->p_owner;
908 vlc_mutex_lock( &p_owner->lock );
910 b_flushing = p_owner->b_flushing;
912 vlc_mutex_unlock( &p_owner->lock );
917 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
919 decoder_owner_sys_t *p_owner = p_dec->p_owner;
921 vlc_assert_locked( &p_owner->lock );
923 while( !p_owner->b_flushing )
925 if( p_owner->b_paused )
927 if( p_owner->b_buffering && !p_owner->buffer.b_full )
929 if( p_owner->pause.i_ignore > 0 )
931 p_owner->pause.i_ignore--;
937 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
940 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
944 *pb_reject = p_owner->b_flushing;
947 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
949 decoder_owner_sys_t *p_owner = p_dec->p_owner;
951 vlc_assert_locked( &p_owner->lock );
953 /* XXX only audio and video output have to be paused.
954 * - for sout it is useless
955 * - for subs, it is done by the vout
957 if( p_dec->fmt_in.i_cat == AUDIO_ES )
959 if( p_owner->p_aout && p_owner->p_aout_input )
960 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
963 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
965 if( p_owner->p_vout )
966 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
969 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
971 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
972 *pi_preroll = INT64_MAX;
973 else if( p->i_pts > 0 )
974 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
975 else if( p->i_dts > 0 )
976 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
979 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
981 mtime_t current_date = mdate();
983 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
984 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
986 /* ETSI EN 300 472 Annex A : do not take into account the PTS
987 * for teletext streams. */
988 return current_date + 400000 + i_ts_delay;
993 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
994 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
996 decoder_owner_sys_t *p_owner = p_dec->p_owner;
997 input_clock_t *p_clock = p_owner->p_clock;
1000 vlc_assert_locked( &p_owner->lock );
1002 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
1003 const mtime_t i_es_delay = p_owner->i_ts_delay;
1007 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1010 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1011 *pi_ts0 + i_es_delay );
1012 if( pi_ts1 && *pi_ts1 > 0 )
1013 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1014 *pi_ts1 + i_es_delay );
1016 /* Do not create ephemere data because of rounding errors */
1017 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1021 i_rate = input_clock_GetRate( p_clock );
1024 *pi_duration = ( *pi_duration * i_rate +
1025 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1032 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1033 if( pi_ts1 && *pi_ts1 <= 0 )
1039 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1040 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1044 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1045 int *pi_played_sum, int *pi_lost_sum )
1047 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1048 aout_instance_t *p_aout = p_owner->p_aout;
1049 aout_input_t *p_aout_input = p_owner->p_aout_input;
1052 if( p_audio->start_date <= 0 )
1054 msg_Warn( p_dec, "non-dated audio buffer received" );
1056 aout_BufferFree( p_audio );
1061 vlc_mutex_lock( &p_owner->lock );
1063 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1065 p_audio->p_next = NULL;
1067 *p_owner->buffer.pp_audio_next = p_audio;
1068 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1070 p_owner->buffer.i_count++;
1071 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1072 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1074 p_owner->buffer.b_full = true;
1075 vlc_cond_signal( &p_owner->wait );
1081 bool b_has_more = false;
1083 DecoderWaitUnblock( p_dec, &b_reject );
1085 if( p_owner->b_buffering )
1087 vlc_mutex_unlock( &p_owner->lock );
1092 if( p_owner->buffer.p_audio )
1094 p_audio = p_owner->buffer.p_audio;
1096 p_owner->buffer.p_audio = p_audio->p_next;
1097 p_owner->buffer.i_count--;
1099 b_has_more = p_owner->buffer.p_audio != NULL;
1101 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1105 int i_rate = INPUT_RATE_DEFAULT;
1108 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1109 &i_rate, &i_delay, false );
1111 vlc_mutex_unlock( &p_owner->lock );
1114 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1116 if( !p_aout || !p_aout_input ||
1117 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1118 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1119 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1124 /* Wait if we are too early
1125 * FIXME that's plain ugly to do it here */
1126 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1128 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1129 *pi_played_sum += 1;
1130 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1134 if( p_audio->start_date <= 0 )
1136 msg_Warn( p_dec, "non-dated audio buffer received" );
1138 else if( p_audio->start_date > i_max_date )
1140 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1141 p_audio->start_date - mdate() );
1144 aout_BufferFree( p_audio );
1150 vlc_mutex_lock( &p_owner->lock );
1151 if( !p_owner->buffer.p_audio )
1153 vlc_mutex_unlock( &p_owner->lock );
1159 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1161 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1162 input_thread_t *p_input = p_owner->p_input;
1163 aout_buffer_t *p_aout_buf;
1168 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1170 aout_instance_t *p_aout = p_owner->p_aout;
1171 aout_input_t *p_aout_input = p_owner->p_aout_input;
1177 /* It prevent freezing VLC in case of broken decoder */
1178 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1180 block_Release( p_block );
1185 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1187 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1191 if( p_owner->i_preroll_end > 0 )
1193 msg_Dbg( p_dec, "End of audio preroll" );
1194 if( p_owner->p_aout && p_owner->p_aout_input )
1195 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1197 p_owner->i_preroll_end = -1;
1200 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1203 /* Update ugly stat */
1204 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1206 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1208 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1210 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1212 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1215 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1218 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1220 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1226 assert( p_dec_cc->pf_get_cc != NULL );
1228 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1229 if( !p_owner->cc.b_supported )
1232 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1236 vlc_mutex_lock( &p_owner->lock );
1237 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1239 p_owner->cc.pb_present[i] |= pb_present[i];
1240 if( p_owner->cc.pp_decoder[i] )
1244 for( i = 0; i < 4; i++ )
1246 if( !p_owner->cc.pp_decoder[i] )
1249 if( i_cc_decoder > 1 )
1250 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1252 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1255 vlc_mutex_unlock( &p_owner->lock );
1258 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1259 int *pi_played_sum, int *pi_lost_sum )
1261 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1262 vout_thread_t *p_vout = p_owner->p_vout;
1263 bool b_first_buffered;
1265 if( p_picture->date <= 0 )
1267 msg_Warn( p_vout, "non-dated video buffer received" );
1269 vout_DropPicture( p_vout, p_picture );
1274 vlc_mutex_lock( &p_owner->lock );
1276 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1278 p_picture->p_next = NULL;
1280 *p_owner->buffer.pp_picture_next = p_picture;
1281 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1283 p_owner->buffer.i_count++;
1284 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1285 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1287 p_owner->buffer.b_full = true;
1288 vlc_cond_signal( &p_owner->wait );
1291 b_first_buffered = p_owner->buffer.p_picture != NULL;
1293 for( ;; b_first_buffered = false )
1295 bool b_has_more = false;
1299 DecoderWaitUnblock( p_dec, &b_reject );
1301 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1303 vlc_mutex_unlock( &p_owner->lock );
1306 bool b_buffering_first = p_owner->b_buffering;
1309 if( p_owner->buffer.p_picture )
1311 p_picture = p_owner->buffer.p_picture;
1313 p_owner->buffer.p_picture = p_picture->p_next;
1314 p_owner->buffer.i_count--;
1316 b_has_more = p_owner->buffer.p_picture != NULL;
1318 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1322 int i_rate = INPUT_RATE_DEFAULT;
1325 if( b_buffering_first )
1327 assert( p_owner->buffer.b_first );
1328 assert( !p_owner->buffer.i_count );
1329 msg_Dbg( p_dec, "Received first picture" );
1330 p_owner->buffer.b_first = false;
1331 p_picture->b_force = true;
1333 if( p_owner->p_clock )
1334 i_rate = input_clock_GetRate( p_owner->p_clock );
1336 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1337 &i_rate, &i_delay, false );
1339 vlc_mutex_unlock( &p_owner->lock );
1342 const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1344 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1349 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1351 /* Be sure to not display old picture after our own */
1352 vout_Flush( p_vout, p_picture->date );
1353 p_owner->i_last_rate = i_rate;
1355 vout_DisplayPicture( p_vout, p_picture );
1359 if( p_picture->date <= 0 )
1361 msg_Warn( p_vout, "non-dated video buffer received" );
1365 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1366 p_picture->date - mdate() );
1369 vout_DropPicture( p_vout, p_picture );
1373 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1375 *pi_played_sum += i_tmp_display;
1376 *pi_lost_sum += i_tmp_lost;
1378 if( !b_has_more || b_buffering_first )
1381 vlc_mutex_lock( &p_owner->lock );
1382 if( !p_owner->buffer.p_picture )
1384 vlc_mutex_unlock( &p_owner->lock );
1390 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1392 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1393 input_thread_t *p_input = p_owner->p_input;
1397 int i_displayed = 0;
1399 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1401 vout_thread_t *p_vout = p_owner->p_vout;
1404 /* It prevent freezing VLC in case of broken decoder */
1405 vout_DropPicture( p_vout, p_pic );
1407 block_Release( p_block );
1413 if( p_pic->date < p_owner->i_preroll_end )
1415 vout_DropPicture( p_vout, p_pic );
1419 if( p_owner->i_preroll_end > 0 )
1421 msg_Dbg( p_dec, "End of video preroll" );
1423 vout_Flush( p_vout, 1 );
1425 p_owner->i_preroll_end = -1;
1428 if( p_dec->pf_get_cc &&
1429 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1430 DecoderGetCc( p_dec, p_dec );
1432 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1434 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1436 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1438 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1440 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1443 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1446 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1450 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1453 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1454 vout_thread_t *p_vout = p_owner->p_spu_vout;
1457 if( p_subpic->i_start <= 0 )
1459 msg_Warn( p_dec, "non-dated spu buffer received" );
1460 subpicture_Delete( p_subpic );
1465 vlc_mutex_lock( &p_owner->lock );
1467 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1469 p_subpic->p_next = NULL;
1471 *p_owner->buffer.pp_subpic_next = p_subpic;
1472 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1474 p_owner->buffer.i_count++;
1475 /* XXX it is important to be full after the first one */
1476 if( p_owner->buffer.i_count > 0 )
1478 p_owner->buffer.b_full = true;
1479 vlc_cond_signal( &p_owner->wait );
1485 bool b_has_more = false;
1487 DecoderWaitUnblock( p_dec, &b_reject );
1489 if( p_owner->b_buffering )
1491 vlc_mutex_unlock( &p_owner->lock );
1496 if( p_owner->buffer.p_subpic )
1498 p_subpic = p_owner->buffer.p_subpic;
1500 p_owner->buffer.p_subpic = p_subpic->p_next;
1501 p_owner->buffer.i_count--;
1503 b_has_more = p_owner->buffer.p_subpic != NULL;
1505 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1509 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1510 NULL, NULL, b_telx );
1512 vlc_mutex_unlock( &p_owner->lock );
1515 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1517 subpicture_Delete( p_subpic );
1521 vlc_mutex_lock( &p_owner->lock );
1522 if( !p_owner->buffer.p_subpic )
1524 vlc_mutex_unlock( &p_owner->lock );
1530 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1533 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1535 assert( p_owner->p_clock );
1536 assert( !p_sout_block->p_next );
1538 vlc_mutex_lock( &p_owner->lock );
1540 if( p_owner->b_buffering || p_owner->buffer.p_block )
1542 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1544 p_owner->buffer.i_count++;
1545 /* XXX it is important to be full after the first one */
1546 if( p_owner->buffer.i_count > 0 )
1548 p_owner->buffer.b_full = true;
1549 vlc_cond_signal( &p_owner->wait );
1555 bool b_has_more = false;
1557 DecoderWaitUnblock( p_dec, &b_reject );
1559 if( p_owner->b_buffering )
1561 vlc_mutex_unlock( &p_owner->lock );
1566 if( p_owner->buffer.p_block )
1568 p_sout_block = p_owner->buffer.p_block;
1570 p_owner->buffer.p_block = p_sout_block->p_next;
1571 p_owner->buffer.i_count--;
1573 b_has_more = p_owner->buffer.p_block != NULL;
1575 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1577 p_sout_block->p_next = NULL;
1579 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1580 &p_sout_block->i_length,
1581 &p_sout_block->i_rate, NULL, b_telx );
1583 vlc_mutex_unlock( &p_owner->lock );
1586 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1588 block_Release( p_sout_block );
1592 vlc_mutex_lock( &p_owner->lock );
1593 if( !p_owner->buffer.p_block )
1595 vlc_mutex_unlock( &p_owner->lock );
1602 static void DecoderFlushBuffering( decoder_t *p_dec )
1604 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1606 vlc_assert_locked( &p_owner->lock );
1608 while( p_owner->buffer.p_picture )
1610 picture_t *p_picture = p_owner->buffer.p_picture;
1612 p_owner->buffer.p_picture = p_picture->p_next;
1613 p_owner->buffer.i_count--;
1615 if( p_owner->p_vout )
1616 vout_DropPicture( p_owner->p_vout, p_picture );
1618 if( !p_owner->buffer.p_picture )
1619 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1621 while( p_owner->buffer.p_audio )
1623 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1625 p_owner->buffer.p_audio = p_audio->p_next;
1626 p_owner->buffer.i_count--;
1628 aout_BufferFree( p_audio );
1630 if( !p_owner->buffer.p_audio )
1631 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1633 while( p_owner->buffer.p_subpic )
1635 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1637 p_owner->buffer.p_subpic = p_subpic->p_next;
1638 p_owner->buffer.i_count--;
1640 subpicture_Delete( p_subpic );
1642 if( !p_owner->buffer.p_subpic )
1643 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1645 if( p_owner->buffer.p_block )
1647 block_ChainRelease( p_owner->buffer.p_block );
1649 p_owner->buffer.i_count = 0;
1650 p_owner->buffer.p_block = NULL;
1651 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1655 /* This function process a block for sout
1657 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1659 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1660 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1661 block_t *p_sout_block;
1663 while( ( p_sout_block =
1664 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1666 if( !p_owner->p_sout_input )
1668 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1670 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1671 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1672 if( p_dec->fmt_in.psz_language )
1674 if( p_owner->sout.psz_language )
1675 free( p_owner->sout.psz_language );
1676 p_owner->sout.psz_language =
1677 strdup( p_dec->fmt_in.psz_language );
1680 p_owner->p_sout_input =
1681 sout_InputNew( p_owner->p_sout,
1684 if( p_owner->p_sout_input == NULL )
1686 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1687 (char *)&p_owner->sout.i_codec );
1688 p_dec->b_error = true;
1690 while( p_sout_block )
1692 block_t *p_next = p_sout_block->p_next;
1693 block_Release( p_sout_block );
1694 p_sout_block = p_next;
1700 while( p_sout_block )
1702 block_t *p_next = p_sout_block->p_next;
1704 p_sout_block->p_next = NULL;
1706 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1708 p_sout_block = p_next;
1711 /* For now it's enough, as only sout impact on this flag */
1712 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1713 p_owner->p_input->p->b_out_pace_control )
1715 msg_Dbg( p_dec, "switching to sync mode" );
1716 p_owner->p_input->p->b_out_pace_control = false;
1718 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1719 !p_owner->p_input->p->b_out_pace_control )
1721 msg_Dbg( p_dec, "switching to async mode" );
1722 p_owner->p_input->p->b_out_pace_control = true;
1727 /* This function process a video block
1729 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1731 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1733 if( p_owner->p_packetizer )
1735 block_t *p_packetized_block;
1736 decoder_t *p_packetizer = p_owner->p_packetizer;
1738 while( (p_packetized_block =
1739 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1741 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1743 es_format_Clean( &p_dec->fmt_in );
1744 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1746 if( p_packetizer->pf_get_cc )
1747 DecoderGetCc( p_dec, p_packetizer );
1749 while( p_packetized_block )
1751 block_t *p_next = p_packetized_block->p_next;
1752 p_packetized_block->p_next = NULL;
1754 DecoderDecodeVideo( p_dec, p_packetized_block );
1756 p_packetized_block = p_next;
1762 DecoderDecodeVideo( p_dec, p_block );
1765 if( b_flush && p_owner->p_vout )
1766 vout_Flush( p_owner->p_vout, 1 );
1769 /* This function process a audio block
1771 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1773 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1775 if( p_owner->p_packetizer )
1777 block_t *p_packetized_block;
1778 decoder_t *p_packetizer = p_owner->p_packetizer;
1780 while( (p_packetized_block =
1781 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1783 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1785 es_format_Clean( &p_dec->fmt_in );
1786 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1789 while( p_packetized_block )
1791 block_t *p_next = p_packetized_block->p_next;
1792 p_packetized_block->p_next = NULL;
1794 DecoderDecodeAudio( p_dec, p_packetized_block );
1796 p_packetized_block = p_next;
1802 DecoderDecodeAudio( p_dec, p_block );
1805 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1806 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1809 /* This function process a subtitle block
1811 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1813 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1814 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1816 input_thread_t *p_input = p_owner->p_input;
1817 vout_thread_t *p_vout;
1818 subpicture_t *p_spu;
1820 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1822 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1823 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1824 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1826 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1827 if( p_vout && p_owner->p_spu_vout == p_vout )
1829 /* Preroll does not work very well with subtitle */
1830 if( p_spu->i_start > 0 &&
1831 p_spu->i_start < p_owner->i_preroll_end &&
1832 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1834 subpicture_Delete( p_spu );
1838 DecoderPlaySpu( p_dec, p_spu, b_telx );
1843 subpicture_Delete( p_spu );
1846 vlc_object_release( p_vout );
1849 if( b_flush && p_owner->p_spu_vout )
1851 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1853 if( p_vout && p_owner->p_spu_vout == p_vout )
1854 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1855 p_owner->i_spu_channel );
1858 vlc_object_release( p_vout );
1865 * \param p_dec the decoder object
1866 * \param p_block the block to decode
1867 * \return VLC_SUCCESS or an error code
1869 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1871 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1872 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1874 if( p_block && p_block->i_buffer <= 0 )
1876 assert( !b_flush_request );
1877 block_Release( p_block );
1881 int canc = vlc_savecancel();
1883 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1886 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1888 DecoderProcessSout( p_dec, p_block );
1893 bool b_flush = false;
1897 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1898 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1900 b_flush = !b_flushing && b_flush_request;
1902 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1905 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1907 DecoderProcessAudio( p_dec, p_block, b_flush );
1909 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1911 DecoderProcessVideo( p_dec, p_block, b_flush );
1913 else if( p_dec->fmt_in.i_cat == SPU_ES )
1915 DecoderProcessSpu( p_dec, p_block, b_flush );
1919 msg_Err( p_dec, "unknown ES format" );
1920 p_dec->b_error = true;
1925 if( b_flush_request )
1927 vlc_mutex_lock( &p_owner->lock );
1928 DecoderFlushBuffering( p_dec );
1929 vlc_mutex_unlock( &p_owner->lock );
1931 DecoderSignalFlushed( p_dec );
1933 vlc_restorecancel( canc );
1935 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1939 * Destroys a decoder object
1941 * \param p_dec the decoder object
1944 static void DeleteDecoder( decoder_t * p_dec )
1946 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1948 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1949 (char*)&p_dec->fmt_in.i_codec,
1950 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1952 /* Free all packets still in the decoder fifo. */
1953 block_FifoEmpty( p_owner->p_fifo );
1954 block_FifoRelease( p_owner->p_fifo );
1957 vlc_mutex_lock( &p_owner->lock );
1958 DecoderFlushBuffering( p_dec );
1959 vlc_mutex_unlock( &p_owner->lock );
1962 if( p_owner->p_aout_input )
1963 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1964 if( p_owner->p_aout )
1966 vlc_object_release( p_owner->p_aout );
1967 p_owner->p_aout = NULL;
1969 if( p_owner->p_vout )
1971 /* Hack to make sure all the the pictures are freed by the decoder */
1972 vout_FixLeaks( p_owner->p_vout, true );
1974 /* We are about to die. Reattach video output to p_vlc. */
1975 vout_Request( p_dec, p_owner->p_vout, NULL );
1976 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1980 if( p_owner->p_sout_input )
1982 sout_InputDelete( p_owner->p_sout_input );
1983 es_format_Clean( &p_owner->sout );
1987 if( p_dec->fmt_in.i_cat == SPU_ES )
1989 vout_thread_t *p_vout;
1991 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1992 if( p_vout && p_owner->p_spu_vout == p_vout )
1994 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1995 p_owner->i_spu_channel );
1996 vlc_object_release( p_vout );
2000 es_format_Clean( &p_dec->fmt_in );
2001 es_format_Clean( &p_dec->fmt_out );
2003 if( p_owner->p_packetizer )
2005 module_unneed( p_owner->p_packetizer,
2006 p_owner->p_packetizer->p_module );
2007 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2008 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2009 vlc_object_detach( p_owner->p_packetizer );
2010 vlc_object_release( p_owner->p_packetizer );
2013 vlc_cond_destroy( &p_owner->wait );
2014 vlc_mutex_destroy( &p_owner->lock );
2016 vlc_object_detach( p_dec );
2021 /*****************************************************************************
2022 * Buffers allocation callbacks for the decoders
2023 *****************************************************************************/
2024 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2026 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2027 aout_buffer_t *p_buffer;
2029 if( p_owner->p_aout_input != NULL &&
2030 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2031 p_dec->fmt_out.audio.i_original_channels !=
2032 p_owner->audio.i_original_channels ||
2033 p_dec->fmt_out.audio.i_bytes_per_frame !=
2034 p_owner->audio.i_bytes_per_frame ) )
2036 aout_input_t *p_aout_input = p_owner->p_aout_input;
2038 /* Parameters changed, restart the aout */
2039 vlc_mutex_lock( &p_owner->lock );
2041 DecoderFlushBuffering( p_dec );
2043 p_owner->p_aout_input = NULL;
2044 aout_DecDelete( p_owner->p_aout, p_aout_input );
2046 vlc_mutex_unlock( &p_owner->lock );
2049 if( p_owner->p_aout_input == NULL )
2051 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2052 audio_sample_format_t format;
2053 aout_input_t *p_aout_input;
2054 aout_instance_t *p_aout;
2056 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2057 p_owner->audio = p_dec->fmt_out.audio;
2059 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2060 if( i_force_dolby &&
2061 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2062 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2064 if( i_force_dolby == 1 )
2066 format.i_original_channels = format.i_original_channels |
2067 AOUT_CHAN_DOLBYSTEREO;
2069 else /* i_force_dolby == 2 */
2071 format.i_original_channels = format.i_original_channels &
2072 ~AOUT_CHAN_DOLBYSTEREO;
2076 p_aout = p_owner->p_aout;
2077 p_aout_input = aout_DecNew( p_dec, &p_aout,
2078 &format, &p_dec->fmt_out.audio_replay_gain );
2080 vlc_mutex_lock( &p_owner->lock );
2081 p_owner->p_aout = p_aout;
2082 p_owner->p_aout_input = p_aout_input;
2083 vlc_mutex_unlock( &p_owner->lock );
2085 if( p_owner->p_aout_input == NULL )
2087 msg_Err( p_dec, "failed to create audio output" );
2088 p_dec->b_error = true;
2091 p_dec->fmt_out.audio.i_bytes_per_frame =
2092 p_owner->audio.i_bytes_per_frame;
2095 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2100 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2102 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2104 aout_DecDeleteBuffer( p_owner->p_aout,
2105 p_owner->p_aout_input, p_buffer );
2109 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2111 static picture_t *vout_new_buffer( decoder_t *p_dec )
2113 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2115 if( p_owner->p_vout == NULL ||
2116 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2117 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2118 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2119 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2121 vout_thread_t *p_vout;
2123 if( !p_dec->fmt_out.video.i_width ||
2124 !p_dec->fmt_out.video.i_height )
2126 /* Can't create a new vout without display size */
2130 if( !p_dec->fmt_out.video.i_visible_width ||
2131 !p_dec->fmt_out.video.i_visible_height )
2133 if( p_dec->fmt_in.video.i_visible_width &&
2134 p_dec->fmt_in.video.i_visible_height )
2136 p_dec->fmt_out.video.i_visible_width =
2137 p_dec->fmt_in.video.i_visible_width;
2138 p_dec->fmt_out.video.i_visible_height =
2139 p_dec->fmt_in.video.i_visible_height;
2143 p_dec->fmt_out.video.i_visible_width =
2144 p_dec->fmt_out.video.i_width;
2145 p_dec->fmt_out.video.i_visible_height =
2146 p_dec->fmt_out.video.i_height;
2150 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2151 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2153 p_dec->fmt_out.video.i_visible_height = 1080;
2154 p_dec->fmt_out.video.i_sar_num *= 135;
2155 p_dec->fmt_out.video.i_sar_den *= 136;
2156 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2159 if( !p_dec->fmt_out.video.i_sar_num ||
2160 !p_dec->fmt_out.video.i_sar_den )
2162 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2163 p_dec->fmt_out.video.i_visible_height;
2165 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2166 p_dec->fmt_out.video.i_visible_width;
2169 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2170 &p_dec->fmt_out.video.i_sar_den,
2171 p_dec->fmt_out.video.i_sar_num,
2172 p_dec->fmt_out.video.i_sar_den, 50000 );
2174 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2175 p_owner->video = p_dec->fmt_out.video;
2177 vlc_mutex_lock( &p_owner->lock );
2179 DecoderFlushBuffering( p_dec );
2181 p_vout = p_owner->p_vout;
2182 p_owner->p_vout = NULL;
2183 vlc_mutex_unlock( &p_owner->lock );
2185 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2187 vlc_mutex_lock( &p_owner->lock );
2188 p_owner->p_vout = p_vout;
2189 vlc_mutex_unlock( &p_owner->lock );
2191 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2192 if( p_vout == NULL )
2194 msg_Err( p_dec, "failed to create video output" );
2195 p_dec->b_error = true;
2199 if( p_owner->video.i_rmask )
2200 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2201 if( p_owner->video.i_gmask )
2202 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2203 if( p_owner->video.i_bmask )
2204 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2207 /* Get a new picture
2211 picture_t *p_picture;
2213 if( p_dec->b_die || p_dec->b_error )
2216 /* The video filter chain required that there is always 1 free buffer
2217 * that it will use as temporary one. It will release the temporary
2218 * buffer once its work is done, so this check is safe even if we don't
2219 * lock around both count() and create().
2221 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2223 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2228 if( DecoderIsFlushing( p_dec ) )
2232 DecoderSignalBuffering( p_dec, true );
2234 /* Check the decoder doesn't leak pictures */
2235 vout_FixLeaks( p_owner->p_vout, false );
2237 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2238 msleep( VOUT_OUTMEM_SLEEP );
2242 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2244 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2247 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2249 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2252 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2254 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2257 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2259 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2260 vout_thread_t *p_vout = NULL;
2261 subpicture_t *p_subpic;
2262 int i_attempts = 30;
2264 while( i_attempts-- )
2266 if( p_dec->b_die || p_dec->b_error )
2269 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2273 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2278 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2282 if( p_owner->p_spu_vout != p_vout )
2284 vlc_mutex_lock( &p_owner->lock );
2286 DecoderFlushBuffering( p_dec );
2288 vlc_mutex_unlock( &p_owner->lock );
2290 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2291 &p_owner->i_spu_channel );
2292 p_owner->i_spu_order = 0;
2293 p_owner->p_spu_vout = p_vout;
2296 p_subpic = subpicture_New();
2299 p_subpic->i_channel = p_owner->i_spu_channel;
2300 p_subpic->i_order = p_owner->i_spu_order++;
2301 p_subpic->b_subtitle = true;
2304 vlc_object_release( p_vout );
2309 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2311 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2312 vout_thread_t *p_vout = NULL;
2314 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2315 if( !p_vout || p_owner->p_spu_vout != p_vout )
2318 vlc_object_release( p_vout );
2319 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2323 subpicture_Delete( p_subpic );
2325 vlc_object_release( p_vout );