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;
150 decoder_t *pp_decoder[4];
157 #define DECODER_MAX_BUFFERING_COUNT (4)
158 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
159 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
161 /*****************************************************************************
163 *****************************************************************************/
164 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
166 if( !p_decoder->pf_aout_buffer_new )
168 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
170 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
172 if( !p_decoder->pf_aout_buffer_del )
174 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
177 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
179 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
181 msg_Warn( p_decoder, "can't get output subpicture" );
184 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
186 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
189 /* decoder_GetInputAttachments:
191 int decoder_GetInputAttachments( decoder_t *p_dec,
192 input_attachment_t ***ppp_attachment,
195 if( !p_dec->pf_get_attachments )
198 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
200 /* decoder_GetDisplayDate:
202 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
204 if( !p_dec->pf_get_display_date )
207 return p_dec->pf_get_display_date( p_dec, i_ts );
209 /* decoder_GetDisplayRate:
211 int decoder_GetDisplayRate( decoder_t *p_dec )
213 if( !p_dec->pf_get_display_rate )
214 return INPUT_RATE_DEFAULT;
216 return p_dec->pf_get_display_rate( p_dec );
220 * Spawns a new decoder thread
222 * \param p_input the input thread
223 * \param p_es the es descriptor
224 * \return the spawned decoder object
226 decoder_t *input_DecoderNew( input_thread_t *p_input,
227 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
229 decoder_t *p_dec = NULL;
233 /* If we are in sout mode, search for packetizer module */
236 /* Create the decoder configuration structure */
237 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
240 msg_Err( p_input, "could not create packetizer" );
241 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
242 _("VLC could not open the packetizer module.") );
249 /* Create the decoder configuration structure */
250 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
253 msg_Err( p_input, "could not create decoder" );
254 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
255 _("VLC could not open the decoder module.") );
260 if( !p_dec->p_module )
262 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
264 DeleteDecoder( p_dec );
265 vlc_object_release( p_dec );
269 p_dec->p_owner->p_clock = p_clock;
271 if( fmt->i_cat == AUDIO_ES )
272 i_priority = VLC_THREAD_PRIORITY_AUDIO;
274 i_priority = VLC_THREAD_PRIORITY_VIDEO;
276 /* Spawn the decoder thread */
277 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
278 i_priority, false ) )
280 msg_Err( p_dec, "cannot spawn decoder thread" );
281 module_unneed( p_dec, p_dec->p_module );
282 DeleteDecoder( p_dec );
283 vlc_object_release( p_dec );
291 * Kills a decoder thread and waits until it's finished
293 * \param p_input the input thread
294 * \param p_es the es descriptor
297 void input_DecoderDelete( decoder_t *p_dec )
299 decoder_owner_sys_t *p_owner = p_dec->p_owner;
301 vlc_object_kill( p_dec );
303 /* Make sure we aren't paused anymore */
304 vlc_mutex_lock( &p_owner->lock );
305 if( p_owner->b_paused || p_owner->b_buffering )
307 p_owner->b_paused = false;
308 p_owner->b_buffering = false;
309 vlc_cond_signal( &p_owner->wait );
311 vlc_mutex_unlock( &p_owner->lock );
313 vlc_thread_join( p_dec );
314 module_unneed( p_dec, p_dec->p_module );
317 if( p_dec->p_owner->cc.b_supported )
320 for( i = 0; i < 4; i++ )
321 input_DecoderSetCcState( p_dec, false, i );
324 /* Delete decoder configuration */
325 DeleteDecoder( p_dec );
327 /* Delete the decoder */
328 vlc_object_release( p_dec );
332 * Put a block_t in the decoder's fifo.
333 * Thread-safe w.r.t. the decoder. May be a cancellation point.
335 * \param p_dec the decoder object
336 * \param p_block the data block
338 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
340 decoder_owner_sys_t *p_owner = p_dec->p_owner;
342 if( p_owner->p_input->p->b_out_pace_control )
344 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
346 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
348 /* FIXME: ideally we would check the time amount of data
349 * in the FIFO instead of its size. */
350 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
351 "consumed quickly enough), resetting fifo!" );
352 block_FifoEmpty( p_owner->p_fifo );
355 block_FifoPut( p_owner->p_fifo, p_block );
358 bool input_DecoderIsEmpty( decoder_t * p_dec )
360 assert( !p_dec->p_owner->b_buffering );
362 /* FIXME that's not really true */
363 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
366 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
368 decoder_owner_sys_t *p_owner = p_dec->p_owner;
371 vlc_mutex_lock( &p_owner->lock );
372 for( i = 0; i < 4; i++ )
373 pb_present[i] = p_owner->cc.pb_present[i];
374 vlc_mutex_unlock( &p_owner->lock );
376 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
378 decoder_owner_sys_t *p_owner = p_dec->p_owner;
380 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
382 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
387 static const vlc_fourcc_t fcc[4] = {
388 VLC_FOURCC('c', 'c', '1', ' '),
389 VLC_FOURCC('c', 'c', '2', ' '),
390 VLC_FOURCC('c', 'c', '3', ' '),
391 VLC_FOURCC('c', 'c', '4', ' '),
396 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
397 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
400 msg_Err( p_dec, "could not create decoder" );
401 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
402 _("VLC could not open the decoder module.") );
405 else if( !p_cc->p_module )
407 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
408 DeleteDecoder( p_cc );
409 vlc_object_release( p_cc );
412 p_cc->p_owner->p_clock = p_owner->p_clock;
414 vlc_mutex_lock( &p_owner->lock );
415 p_owner->cc.pp_decoder[i_channel] = p_cc;
416 vlc_mutex_unlock( &p_owner->lock );
422 vlc_mutex_lock( &p_owner->lock );
423 p_cc = p_owner->cc.pp_decoder[i_channel];
424 p_owner->cc.pp_decoder[i_channel] = NULL;
425 vlc_mutex_unlock( &p_owner->lock );
429 vlc_object_kill( p_cc );
430 module_unneed( p_cc, p_cc->p_module );
431 DeleteDecoder( p_cc );
432 vlc_object_release( p_cc );
437 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
439 decoder_owner_sys_t *p_owner = p_dec->p_owner;
442 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
445 vlc_mutex_lock( &p_owner->lock );
446 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
447 vlc_mutex_unlock( &p_owner->lock );
451 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
453 decoder_owner_sys_t *p_owner = p_dec->p_owner;
455 vlc_mutex_lock( &p_owner->lock );
457 assert( !p_owner->b_paused || !b_paused );
459 p_owner->b_paused = b_paused;
460 p_owner->pause.i_date = i_date;
461 p_owner->pause.i_ignore = 0;
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_first = true;
487 p_owner->buffer.b_full = false;
488 p_owner->buffer.i_count = 0;
490 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
492 p_owner->buffer.p_picture = NULL;
493 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
495 p_owner->buffer.p_subpic = NULL;
496 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
498 p_owner->buffer.p_audio = NULL;
499 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
501 p_owner->b_buffering = true;
503 vlc_cond_signal( &p_owner->wait );
505 vlc_mutex_unlock( &p_owner->lock );
508 void input_DecoderStopBuffering( decoder_t *p_dec )
510 decoder_owner_sys_t *p_owner = p_dec->p_owner;
512 vlc_mutex_lock( &p_owner->lock );
514 p_owner->b_buffering = false;
516 vlc_cond_signal( &p_owner->wait );
518 vlc_mutex_unlock( &p_owner->lock );
521 void input_DecoderWaitBuffering( decoder_t *p_dec )
523 decoder_owner_sys_t *p_owner = p_dec->p_owner;
525 vlc_mutex_lock( &p_owner->lock );
527 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
529 block_FifoWake( p_owner->p_fifo );
530 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
533 vlc_mutex_unlock( &p_owner->lock );
535 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
537 decoder_owner_sys_t *p_owner = p_dec->p_owner;
541 vlc_mutex_lock( &p_owner->lock );
542 if( p_dec->fmt_in.i_cat == VIDEO_ES )
544 if( p_owner->b_paused && p_owner->p_vout )
546 vout_NextPicture( p_owner->p_vout, pi_duration );
547 p_owner->pause.i_ignore++;
548 vlc_cond_signal( &p_owner->wait );
553 /* TODO subtitle should not be flushed */
554 DecoderFlush( p_dec );
556 vlc_mutex_unlock( &p_owner->lock );
559 /*****************************************************************************
561 *****************************************************************************/
562 static int DecoderGetInputAttachments( decoder_t *p_dec,
563 input_attachment_t ***ppp_attachment,
566 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
567 ppp_attachment, pi_attachment );
569 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
571 decoder_owner_sys_t *p_owner = p_dec->p_owner;
573 vlc_mutex_lock( &p_owner->lock );
574 if( p_owner->b_buffering || p_owner->b_paused )
576 vlc_mutex_unlock( &p_owner->lock );
578 if( !p_owner->p_clock || !i_ts )
581 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
583 static int DecoderGetDisplayRate( decoder_t *p_dec )
585 decoder_owner_sys_t *p_owner = p_dec->p_owner;
587 if( !p_owner->p_clock )
588 return INPUT_RATE_DEFAULT;
589 return input_clock_GetRate( p_owner->p_clock );
593 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
595 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
596 "VLC probably does not support this sound or video format.",
598 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
599 _("VLC does not support the audio or video format \"%4.4s\". "
600 "Unfortunately there is no way for you to fix this."), (char*)&codec );
605 * Create a decoder object
607 * \param p_input the input thread
608 * \param p_es the es descriptor
609 * \param i_object_type Object type as define in include/vlc_objects.h
610 * \return the decoder object
612 static decoder_t * CreateDecoder( input_thread_t *p_input,
613 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
616 decoder_owner_sys_t *p_owner;
619 p_dec = vlc_object_create( p_input, i_object_type );
623 p_dec->pf_decode_audio = NULL;
624 p_dec->pf_decode_video = NULL;
625 p_dec->pf_decode_sub = NULL;
626 p_dec->pf_get_cc = NULL;
627 p_dec->pf_packetize = NULL;
629 /* Initialize the decoder fifo */
630 p_dec->p_module = NULL;
632 memset( &null_es_format, 0, sizeof(es_format_t) );
633 es_format_Copy( &p_dec->fmt_in, fmt );
634 es_format_Copy( &p_dec->fmt_out, &null_es_format );
636 /* Allocate our private structure for the decoder */
637 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
638 if( p_dec->p_owner == NULL )
640 vlc_object_release( p_dec );
643 p_dec->p_owner->i_preroll_end = -1;
644 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
645 p_dec->p_owner->p_input = p_input;
646 p_dec->p_owner->p_aout = NULL;
647 p_dec->p_owner->p_aout_input = NULL;
648 p_dec->p_owner->p_vout = NULL;
649 p_dec->p_owner->p_spu_vout = NULL;
650 p_dec->p_owner->i_spu_channel = 0;
651 p_dec->p_owner->i_spu_order = 0;
652 p_dec->p_owner->p_sout = p_sout;
653 p_dec->p_owner->p_sout_input = NULL;
654 p_dec->p_owner->p_packetizer = NULL;
657 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
659 free( p_dec->p_owner );
660 vlc_object_release( p_dec );
664 /* Set buffers allocation callbacks for the decoders */
665 p_dec->pf_aout_buffer_new = aout_new_buffer;
666 p_dec->pf_aout_buffer_del = aout_del_buffer;
667 p_dec->pf_vout_buffer_new = vout_new_buffer;
668 p_dec->pf_vout_buffer_del = vout_del_buffer;
669 p_dec->pf_picture_link = vout_link_picture;
670 p_dec->pf_picture_unlink = vout_unlink_picture;
671 p_dec->pf_spu_buffer_new = spu_new_buffer;
672 p_dec->pf_spu_buffer_del = spu_del_buffer;
674 p_dec->pf_get_attachments = DecoderGetInputAttachments;
675 p_dec->pf_get_display_date = DecoderGetDisplayDate;
676 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
678 vlc_object_attach( p_dec, p_input );
680 /* Find a suitable decoder/packetizer module */
681 if( i_object_type == VLC_OBJECT_DECODER )
682 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
684 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
686 /* Check if decoder requires already packetized data */
687 if( i_object_type == VLC_OBJECT_DECODER &&
688 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
690 p_dec->p_owner->p_packetizer =
691 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
692 if( p_dec->p_owner->p_packetizer )
694 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
697 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
700 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
702 p_dec->p_owner->p_packetizer->p_module =
703 module_need( p_dec->p_owner->p_packetizer,
704 "packetizer", "$packetizer", 0 );
706 if( !p_dec->p_owner->p_packetizer->p_module )
708 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
709 vlc_object_detach( p_dec->p_owner->p_packetizer );
710 vlc_object_release( p_dec->p_owner->p_packetizer );
715 /* Copy ourself the input replay gain */
716 if( fmt->i_cat == AUDIO_ES )
718 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
720 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
722 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
723 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
725 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
727 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
728 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
734 vlc_mutex_init( &p_owner->lock );
735 vlc_cond_init( &p_owner->wait );
737 p_owner->b_paused = false;
738 p_owner->pause.i_date = 0;
739 p_owner->pause.i_ignore = 0;
741 p_owner->b_buffering = false;
742 p_owner->buffer.b_first = true;
743 p_owner->buffer.b_full = false;
744 p_owner->buffer.i_count = 0;
745 p_owner->buffer.p_picture = NULL;
746 p_owner->buffer.p_subpic = NULL;
747 p_owner->buffer.p_audio = NULL;
749 p_owner->b_flushing = false;
752 p_owner->cc.b_supported = false;
753 if( i_object_type == VLC_OBJECT_DECODER )
755 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
756 p_owner->cc.b_supported = true;
757 if( p_dec->pf_get_cc )
758 p_owner->cc.b_supported = true;
761 for( i = 0; i < 4; i++ )
763 p_owner->cc.pb_present[i] = false;
764 p_owner->cc.pp_decoder[i] = NULL;
766 p_owner->i_ts_delay = 0;
771 * The decoding main loop
773 * \param p_dec the decoder
775 static void *DecoderThread( vlc_object_t *p_this )
777 decoder_t *p_dec = (decoder_t *)p_this;
778 decoder_owner_sys_t *p_owner = p_dec->p_owner;
780 /* The decoder's main loop */
783 block_t *p_block = block_FifoGet( p_owner->p_fifo );
784 /* Make sure there is no cancellation point other than this one^^.
785 * If you need one, be sure to push cleanup of p_block. */
786 DecoderSignalBuffering( p_dec, p_block == NULL );
791 { /* Trash all received PES packets */
792 block_Release( p_block );
794 else if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
801 DecoderSignalBuffering( p_dec, true );
805 static void DecoderFlush( decoder_t *p_dec )
807 decoder_owner_sys_t *p_owner = p_dec->p_owner;
810 vlc_assert_locked( &p_owner->lock );
813 block_FifoEmpty( p_owner->p_fifo );
815 /* Monitor for flush end */
816 p_owner->b_flushing = true;
817 vlc_cond_signal( &p_owner->wait );
819 /* Send a special block */
820 p_null = block_New( p_dec, 128 );
823 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
824 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
825 if( !p_dec->fmt_in.b_packetized )
826 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
827 memset( p_null->p_buffer, 0, p_null->i_buffer );
829 input_DecoderDecode( p_dec, p_null );
832 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
833 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
836 static void DecoderSignalFlushed( decoder_t *p_dec )
838 decoder_owner_sys_t *p_owner = p_dec->p_owner;
840 vlc_mutex_lock( &p_owner->lock );
842 if( p_owner->b_flushing )
844 p_owner->b_flushing = false;
845 vlc_cond_signal( &p_owner->wait );
848 vlc_mutex_unlock( &p_owner->lock );
851 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
853 decoder_owner_sys_t *p_owner = p_dec->p_owner;
855 vlc_mutex_lock( &p_owner->lock );
857 if( p_owner->b_buffering )
860 p_owner->buffer.b_full = true;
861 vlc_cond_signal( &p_owner->wait );
864 vlc_mutex_unlock( &p_owner->lock );
867 static bool DecoderIsFlushing( decoder_t *p_dec )
869 decoder_owner_sys_t *p_owner = p_dec->p_owner;
872 vlc_mutex_lock( &p_owner->lock );
874 b_flushing = p_owner->b_flushing;
876 vlc_mutex_unlock( &p_owner->lock );
881 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
883 decoder_owner_sys_t *p_owner = p_dec->p_owner;
885 vlc_assert_locked( &p_owner->lock );
887 while( !p_owner->b_flushing )
889 if( p_owner->b_paused )
891 if( p_owner->b_buffering && !p_owner->buffer.b_full )
893 if( p_owner->pause.i_ignore > 0 )
895 p_owner->pause.i_ignore--;
901 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
904 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
908 *pb_reject = p_owner->b_flushing;
911 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
913 decoder_owner_sys_t *p_owner = p_dec->p_owner;
915 vlc_assert_locked( &p_owner->lock );
917 /* XXX only audio and video output have to be paused.
918 * - for sout it is useless
919 * - for subs, it is done by the vout
921 if( p_dec->fmt_in.i_cat == AUDIO_ES )
923 if( p_owner->p_aout && p_owner->p_aout_input )
924 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
927 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
929 if( p_owner->p_vout )
930 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
933 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
935 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
936 *pi_preroll = INT64_MAX;
937 else if( p->i_pts > 0 )
938 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
939 else if( p->i_dts > 0 )
940 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
943 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
945 mtime_t current_date = mdate();
947 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
948 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
950 /* ETSI EN 300 472 Annex A : do not take into account the PTS
951 * for teletext streams. */
952 return current_date + 400000 + i_ts_delay;
957 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
958 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
960 decoder_owner_sys_t *p_owner = p_dec->p_owner;
961 input_clock_t *p_clock = p_owner->p_clock;
964 vlc_assert_locked( &p_owner->lock );
966 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
967 const mtime_t i_es_delay = p_owner->i_ts_delay;
971 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
974 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
975 *pi_ts0 + i_es_delay );
976 if( pi_ts1 && *pi_ts1 > 0 )
977 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
978 *pi_ts1 + i_es_delay );
980 /* Do not create ephemere data because of rounding errors */
981 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
985 i_rate = input_clock_GetRate( p_clock );
988 *pi_duration = ( *pi_duration * i_rate +
989 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
996 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
997 if( pi_ts1 && *pi_ts1 <= 0 )
1003 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1004 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1008 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1009 int *pi_played_sum, int *pi_lost_sum )
1011 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1012 aout_instance_t *p_aout = p_owner->p_aout;
1013 aout_input_t *p_aout_input = p_owner->p_aout_input;
1016 if( p_audio->start_date <= 0 )
1018 msg_Warn( p_dec, "non-dated audio buffer received" );
1020 aout_BufferFree( p_audio );
1025 vlc_mutex_lock( &p_owner->lock );
1027 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1029 p_audio->p_next = NULL;
1031 *p_owner->buffer.pp_audio_next = p_audio;
1032 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1034 p_owner->buffer.i_count++;
1035 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1036 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1038 p_owner->buffer.b_full = true;
1039 vlc_cond_signal( &p_owner->wait );
1045 bool b_has_more = false;
1047 DecoderWaitUnblock( p_dec, &b_reject );
1049 if( p_owner->b_buffering )
1051 vlc_mutex_unlock( &p_owner->lock );
1056 if( p_owner->buffer.p_audio )
1058 p_audio = p_owner->buffer.p_audio;
1060 p_owner->buffer.p_audio = p_audio->p_next;
1061 p_owner->buffer.i_count--;
1063 b_has_more = p_owner->buffer.p_audio != NULL;
1065 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1069 int i_rate = INPUT_RATE_DEFAULT;
1072 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1073 &i_rate, &i_delay, false );
1075 vlc_mutex_unlock( &p_owner->lock );
1078 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1080 if( !p_aout || !p_aout_input ||
1081 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1082 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1083 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1088 /* Wait if we are too early
1089 * FIXME that's plain ugly to do it here */
1090 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1092 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1093 *pi_played_sum += 1;
1094 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1098 if( p_audio->start_date <= 0 )
1100 msg_Warn( p_dec, "non-dated audio buffer received" );
1102 else if( p_audio->start_date > i_max_date )
1104 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1105 p_audio->start_date - mdate() );
1108 aout_BufferFree( p_audio );
1114 vlc_mutex_lock( &p_owner->lock );
1115 if( !p_owner->buffer.p_audio )
1117 vlc_mutex_unlock( &p_owner->lock );
1123 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1125 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1126 input_thread_t *p_input = p_owner->p_input;
1127 aout_buffer_t *p_aout_buf;
1132 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1134 aout_instance_t *p_aout = p_owner->p_aout;
1135 aout_input_t *p_aout_input = p_owner->p_aout_input;
1141 /* It prevent freezing VLC in case of broken decoder */
1142 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1144 block_Release( p_block );
1149 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1151 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1155 if( p_owner->i_preroll_end > 0 )
1157 msg_Dbg( p_dec, "End of audio preroll" );
1158 if( p_owner->p_aout && p_owner->p_aout_input )
1159 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1161 p_owner->i_preroll_end = -1;
1164 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1167 /* Update ugly stat */
1168 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1170 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1172 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1174 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1176 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1179 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1182 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1184 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1190 assert( p_dec_cc->pf_get_cc != NULL );
1192 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1193 if( !p_owner->cc.b_supported )
1196 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1200 vlc_mutex_lock( &p_owner->lock );
1201 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1203 p_owner->cc.pb_present[i] |= pb_present[i];
1204 if( p_owner->cc.pp_decoder[i] )
1208 for( i = 0; i < 4; i++ )
1210 if( !p_owner->cc.pp_decoder[i] )
1213 if( i_cc_decoder > 1 )
1214 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1216 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1219 vlc_mutex_unlock( &p_owner->lock );
1222 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1223 int *pi_played_sum, int *pi_lost_sum )
1225 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1226 vout_thread_t *p_vout = p_owner->p_vout;
1227 bool b_first_buffered;
1229 if( p_picture->date <= 0 )
1231 msg_Warn( p_vout, "non-dated video buffer received" );
1233 vout_DropPicture( p_vout, p_picture );
1238 vlc_mutex_lock( &p_owner->lock );
1240 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1242 p_picture->p_next = NULL;
1244 *p_owner->buffer.pp_picture_next = p_picture;
1245 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1247 p_owner->buffer.i_count++;
1248 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1249 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1251 p_owner->buffer.b_full = true;
1252 vlc_cond_signal( &p_owner->wait );
1255 b_first_buffered = p_owner->buffer.p_picture != NULL;
1257 for( ;; b_first_buffered = false )
1259 bool b_has_more = false;
1263 DecoderWaitUnblock( p_dec, &b_reject );
1265 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1267 vlc_mutex_unlock( &p_owner->lock );
1270 bool b_buffering_first = p_owner->b_buffering;
1273 if( p_owner->buffer.p_picture )
1275 p_picture = p_owner->buffer.p_picture;
1277 p_owner->buffer.p_picture = p_picture->p_next;
1278 p_owner->buffer.i_count--;
1280 b_has_more = p_owner->buffer.p_picture != NULL;
1282 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1286 int i_rate = INPUT_RATE_DEFAULT;
1289 if( b_buffering_first )
1291 assert( p_owner->buffer.b_first );
1292 assert( !p_owner->buffer.i_count );
1293 msg_Dbg( p_dec, "Received first picture" );
1294 p_owner->buffer.b_first = false;
1295 p_picture->b_force = true;
1297 if( p_owner->p_clock )
1298 i_rate = input_clock_GetRate( p_owner->p_clock );
1300 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1301 &i_rate, &i_delay, false );
1303 vlc_mutex_unlock( &p_owner->lock );
1306 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1308 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1313 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1315 /* Be sure to not display old picture after our own */
1316 vout_Flush( p_vout, p_picture->date );
1317 p_owner->i_last_rate = i_rate;
1319 vout_DisplayPicture( p_vout, p_picture );
1323 if( p_picture->date <= 0 )
1325 msg_Warn( p_vout, "non-dated video buffer received" );
1329 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1330 p_picture->date - mdate() );
1333 vout_DropPicture( p_vout, p_picture );
1337 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1339 *pi_played_sum += i_tmp_display;
1340 *pi_lost_sum += i_tmp_lost;
1342 if( !b_has_more || b_buffering_first )
1345 vlc_mutex_lock( &p_owner->lock );
1346 if( !p_owner->buffer.p_picture )
1348 vlc_mutex_unlock( &p_owner->lock );
1354 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1356 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1357 input_thread_t *p_input = p_owner->p_input;
1361 int i_displayed = 0;
1363 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1365 vout_thread_t *p_vout = p_owner->p_vout;
1368 /* It prevent freezing VLC in case of broken decoder */
1369 vout_DropPicture( p_vout, p_pic );
1371 block_Release( p_block );
1377 if( p_pic->date < p_owner->i_preroll_end )
1379 vout_DropPicture( p_vout, p_pic );
1383 if( p_owner->i_preroll_end > 0 )
1385 msg_Dbg( p_dec, "End of video preroll" );
1387 vout_Flush( p_vout, 1 );
1389 p_owner->i_preroll_end = -1;
1392 if( p_dec->pf_get_cc &&
1393 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1394 DecoderGetCc( p_dec, p_dec );
1396 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1398 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1400 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1402 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1404 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1407 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1410 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1414 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1417 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1418 vout_thread_t *p_vout = p_owner->p_spu_vout;
1421 if( p_subpic->i_start <= 0 )
1423 msg_Warn( p_dec, "non-dated spu buffer received" );
1424 subpicture_Delete( p_subpic );
1429 vlc_mutex_lock( &p_owner->lock );
1431 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1433 p_subpic->p_next = NULL;
1435 *p_owner->buffer.pp_subpic_next = p_subpic;
1436 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1438 p_owner->buffer.i_count++;
1439 /* XXX it is important to be full after the first one */
1440 if( p_owner->buffer.i_count > 0 )
1442 p_owner->buffer.b_full = true;
1443 vlc_cond_signal( &p_owner->wait );
1449 bool b_has_more = false;
1451 DecoderWaitUnblock( p_dec, &b_reject );
1453 if( p_owner->b_buffering )
1455 vlc_mutex_unlock( &p_owner->lock );
1460 if( p_owner->buffer.p_subpic )
1462 p_subpic = p_owner->buffer.p_subpic;
1464 p_owner->buffer.p_subpic = p_subpic->p_next;
1465 p_owner->buffer.i_count--;
1467 b_has_more = p_owner->buffer.p_subpic != NULL;
1469 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1473 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1474 NULL, NULL, b_telx );
1476 vlc_mutex_unlock( &p_owner->lock );
1479 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1481 subpicture_Delete( p_subpic );
1485 vlc_mutex_lock( &p_owner->lock );
1486 if( !p_owner->buffer.p_subpic )
1488 vlc_mutex_unlock( &p_owner->lock );
1494 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1497 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1499 assert( p_owner->p_clock );
1501 vlc_mutex_lock( &p_owner->lock );
1504 DecoderWaitUnblock( p_dec, &b_reject );
1506 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1507 &p_sout_block->i_rate, NULL, b_telx );
1509 vlc_mutex_unlock( &p_owner->lock );
1511 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1515 static void DecoderFlushBuffering( decoder_t *p_dec )
1517 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1519 vlc_assert_locked( &p_owner->lock );
1521 while( p_owner->buffer.p_picture )
1523 picture_t *p_picture = p_owner->buffer.p_picture;
1525 p_owner->buffer.p_picture = p_picture->p_next;
1526 p_owner->buffer.i_count--;
1528 if( p_owner->p_vout )
1529 vout_DropPicture( p_owner->p_vout, p_picture );
1531 if( !p_owner->buffer.p_picture )
1532 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1534 while( p_owner->buffer.p_audio )
1536 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1538 p_owner->buffer.p_audio = p_audio->p_next;
1539 p_owner->buffer.i_count--;
1541 aout_BufferFree( p_audio );
1543 if( !p_owner->buffer.p_audio )
1544 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1546 while( p_owner->buffer.p_subpic )
1548 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1550 p_owner->buffer.p_subpic = p_subpic->p_next;
1551 p_owner->buffer.i_count--;
1553 subpicture_Delete( p_subpic );
1555 if( !p_owner->buffer.p_subpic )
1556 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1560 /* This function process a block for sout
1562 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1564 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1565 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1566 block_t *p_sout_block;
1568 while( ( p_sout_block =
1569 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1571 if( !p_owner->p_sout_input )
1573 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1575 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1576 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1577 if( p_dec->fmt_in.psz_language )
1579 if( p_owner->sout.psz_language )
1580 free( p_owner->sout.psz_language );
1581 p_owner->sout.psz_language =
1582 strdup( p_dec->fmt_in.psz_language );
1585 p_owner->p_sout_input =
1586 sout_InputNew( p_owner->p_sout,
1589 if( p_owner->p_sout_input == NULL )
1591 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1592 (char *)&p_owner->sout.i_codec );
1593 p_dec->b_error = true;
1595 while( p_sout_block )
1597 block_t *p_next = p_sout_block->p_next;
1598 block_Release( p_sout_block );
1599 p_sout_block = p_next;
1605 while( p_sout_block )
1607 block_t *p_next = p_sout_block->p_next;
1609 p_sout_block->p_next = NULL;
1611 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1613 p_sout_block = p_next;
1616 /* For now it's enough, as only sout impact on this flag */
1617 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1618 p_owner->p_input->p->b_out_pace_control )
1620 msg_Dbg( p_dec, "switching to sync mode" );
1621 p_owner->p_input->p->b_out_pace_control = false;
1623 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1624 !p_owner->p_input->p->b_out_pace_control )
1626 msg_Dbg( p_dec, "switching to async mode" );
1627 p_owner->p_input->p->b_out_pace_control = true;
1632 /* This function process a video block
1634 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1636 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1638 if( p_owner->p_packetizer )
1640 block_t *p_packetized_block;
1641 decoder_t *p_packetizer = p_owner->p_packetizer;
1643 while( (p_packetized_block =
1644 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1646 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1648 es_format_Clean( &p_dec->fmt_in );
1649 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1651 if( p_packetizer->pf_get_cc )
1652 DecoderGetCc( p_dec, p_packetizer );
1654 while( p_packetized_block )
1656 block_t *p_next = p_packetized_block->p_next;
1657 p_packetized_block->p_next = NULL;
1659 DecoderDecodeVideo( p_dec, p_packetized_block );
1661 p_packetized_block = p_next;
1667 DecoderDecodeVideo( p_dec, p_block );
1670 if( b_flush && p_owner->p_vout )
1671 vout_Flush( p_owner->p_vout, 1 );
1674 /* This function process a audio block
1676 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1678 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1680 if( p_owner->p_packetizer )
1682 block_t *p_packetized_block;
1683 decoder_t *p_packetizer = p_owner->p_packetizer;
1685 while( (p_packetized_block =
1686 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1688 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1690 es_format_Clean( &p_dec->fmt_in );
1691 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1694 while( p_packetized_block )
1696 block_t *p_next = p_packetized_block->p_next;
1697 p_packetized_block->p_next = NULL;
1699 DecoderDecodeAudio( p_dec, p_packetized_block );
1701 p_packetized_block = p_next;
1707 DecoderDecodeAudio( p_dec, p_block );
1710 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1711 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1714 /* This function process a subtitle block
1716 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1718 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1719 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1721 input_thread_t *p_input = p_owner->p_input;
1722 vout_thread_t *p_vout;
1723 subpicture_t *p_spu;
1725 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1727 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1728 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1729 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1731 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1732 if( p_vout && p_owner->p_spu_vout == p_vout )
1734 /* Preroll does not work very well with subtitle */
1735 if( p_spu->i_start > 0 &&
1736 p_spu->i_start < p_owner->i_preroll_end &&
1737 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1739 subpicture_Delete( p_spu );
1743 DecoderPlaySpu( p_dec, p_spu, b_telx );
1748 subpicture_Delete( p_spu );
1751 vlc_object_release( p_vout );
1754 if( b_flush && p_owner->p_spu_vout )
1756 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1758 if( p_vout && p_owner->p_spu_vout == p_vout )
1759 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1760 p_owner->i_spu_channel );
1763 vlc_object_release( p_vout );
1770 * \param p_dec the decoder object
1771 * \param p_block the block to decode
1772 * \return VLC_SUCCESS or an error code
1774 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1776 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1777 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1779 if( p_block && p_block->i_buffer <= 0 )
1781 assert( !b_flush_request );
1782 block_Release( p_block );
1786 int canc = vlc_savecancel ();
1788 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1791 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1793 DecoderProcessSout( p_dec, p_block );
1798 bool b_flush = false;
1802 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1803 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1805 b_flush = !b_flushing && b_flush_request;
1807 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1810 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1812 DecoderProcessAudio( p_dec, p_block, b_flush );
1814 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1816 DecoderProcessVideo( p_dec, p_block, b_flush );
1818 else if( p_dec->fmt_in.i_cat == SPU_ES )
1820 DecoderProcessSpu( p_dec, p_block, b_flush );
1824 msg_Err( p_dec, "unknown ES format" );
1825 p_dec->b_error = true;
1830 if( b_flush_request )
1832 vlc_mutex_lock( &p_owner->lock );
1833 DecoderFlushBuffering( p_dec );
1834 vlc_mutex_unlock( &p_owner->lock );
1836 DecoderSignalFlushed( p_dec );
1838 vlc_restorecancel(canc);
1840 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1844 * Destroys a decoder object
1846 * \param p_dec the decoder object
1849 static void DeleteDecoder( decoder_t * p_dec )
1851 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1853 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1854 (char*)&p_dec->fmt_in.i_codec,
1855 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1857 /* Free all packets still in the decoder fifo. */
1858 block_FifoEmpty( p_owner->p_fifo );
1859 block_FifoRelease( p_owner->p_fifo );
1862 vlc_mutex_lock( &p_owner->lock );
1863 DecoderFlushBuffering( p_dec );
1864 vlc_mutex_unlock( &p_owner->lock );
1867 if( p_owner->p_aout_input )
1868 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1869 if( p_owner->p_aout )
1871 vlc_object_release( p_owner->p_aout );
1872 p_owner->p_aout = NULL;
1874 if( p_owner->p_vout )
1876 /* Hack to make sure all the the pictures are freed by the decoder */
1877 vout_FixLeaks( p_owner->p_vout, true );
1879 /* We are about to die. Reattach video output to p_vlc. */
1880 vout_Request( p_dec, p_owner->p_vout, NULL );
1881 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1885 if( p_owner->p_sout_input )
1887 sout_InputDelete( p_owner->p_sout_input );
1888 es_format_Clean( &p_owner->sout );
1892 if( p_dec->fmt_in.i_cat == SPU_ES )
1894 vout_thread_t *p_vout;
1896 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1897 if( p_vout && p_owner->p_spu_vout == p_vout )
1899 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1900 p_owner->i_spu_channel );
1901 vlc_object_release( p_vout );
1905 es_format_Clean( &p_dec->fmt_in );
1906 es_format_Clean( &p_dec->fmt_out );
1908 if( p_owner->p_packetizer )
1910 module_unneed( p_owner->p_packetizer,
1911 p_owner->p_packetizer->p_module );
1912 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1913 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1914 vlc_object_detach( p_owner->p_packetizer );
1915 vlc_object_release( p_owner->p_packetizer );
1918 vlc_cond_destroy( &p_owner->wait );
1919 vlc_mutex_destroy( &p_owner->lock );
1921 vlc_object_detach( p_dec );
1926 /*****************************************************************************
1927 * Buffers allocation callbacks for the decoders
1928 *****************************************************************************/
1929 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1931 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1932 aout_buffer_t *p_buffer;
1934 if( p_owner->p_aout_input != NULL &&
1935 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1936 p_dec->fmt_out.audio.i_original_channels !=
1937 p_owner->audio.i_original_channels ||
1938 p_dec->fmt_out.audio.i_bytes_per_frame !=
1939 p_owner->audio.i_bytes_per_frame ) )
1941 aout_input_t *p_aout_input = p_owner->p_aout_input;
1943 /* Parameters changed, restart the aout */
1944 vlc_mutex_lock( &p_owner->lock );
1946 DecoderFlushBuffering( p_dec );
1948 p_owner->p_aout_input = NULL;
1949 aout_DecDelete( p_owner->p_aout, p_aout_input );
1951 vlc_mutex_unlock( &p_owner->lock );
1954 if( p_owner->p_aout_input == NULL )
1956 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1957 audio_sample_format_t format;
1958 aout_input_t *p_aout_input;
1959 aout_instance_t *p_aout;
1961 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1962 p_owner->audio = p_dec->fmt_out.audio;
1964 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1965 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1966 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1968 if ( i_force_dolby == 1 )
1970 format.i_original_channels = format.i_original_channels |
1971 AOUT_CHAN_DOLBYSTEREO;
1973 else /* i_force_dolby == 2 */
1975 format.i_original_channels = format.i_original_channels &
1976 ~AOUT_CHAN_DOLBYSTEREO;
1980 p_aout = p_owner->p_aout;
1981 p_aout_input = aout_DecNew( p_dec, &p_aout,
1982 &format, &p_dec->fmt_out.audio_replay_gain );
1984 vlc_mutex_lock( &p_owner->lock );
1985 p_owner->p_aout = p_aout;
1986 p_owner->p_aout_input = p_aout_input;
1987 vlc_mutex_unlock( &p_owner->lock );
1989 if( p_owner->p_aout_input == NULL )
1991 msg_Err( p_dec, "failed to create audio output" );
1992 p_dec->b_error = true;
1995 p_dec->fmt_out.audio.i_bytes_per_frame =
1996 p_owner->audio.i_bytes_per_frame;
1999 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2004 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2006 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2008 aout_DecDeleteBuffer( p_owner->p_aout,
2009 p_owner->p_aout_input, p_buffer );
2013 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2015 static picture_t *vout_new_buffer( decoder_t *p_dec )
2017 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2019 if( p_owner->p_vout == NULL ||
2020 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2021 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2022 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2023 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2025 vout_thread_t *p_vout;
2027 if( !p_dec->fmt_out.video.i_width ||
2028 !p_dec->fmt_out.video.i_height )
2030 /* Can't create a new vout without display size */
2034 if( !p_dec->fmt_out.video.i_visible_width ||
2035 !p_dec->fmt_out.video.i_visible_height )
2037 if( p_dec->fmt_in.video.i_visible_width &&
2038 p_dec->fmt_in.video.i_visible_height )
2040 p_dec->fmt_out.video.i_visible_width =
2041 p_dec->fmt_in.video.i_visible_width;
2042 p_dec->fmt_out.video.i_visible_height =
2043 p_dec->fmt_in.video.i_visible_height;
2047 p_dec->fmt_out.video.i_visible_width =
2048 p_dec->fmt_out.video.i_width;
2049 p_dec->fmt_out.video.i_visible_height =
2050 p_dec->fmt_out.video.i_height;
2054 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2055 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2057 p_dec->fmt_out.video.i_visible_height = 1080;
2058 p_dec->fmt_out.video.i_sar_num *= 135;
2059 p_dec->fmt_out.video.i_sar_den *= 136;
2060 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2063 if( !p_dec->fmt_out.video.i_sar_num ||
2064 !p_dec->fmt_out.video.i_sar_den )
2066 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2067 p_dec->fmt_out.video.i_visible_height;
2069 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2070 p_dec->fmt_out.video.i_visible_width;
2073 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2074 &p_dec->fmt_out.video.i_sar_den,
2075 p_dec->fmt_out.video.i_sar_num,
2076 p_dec->fmt_out.video.i_sar_den, 50000 );
2078 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2079 p_owner->video = p_dec->fmt_out.video;
2081 vlc_mutex_lock( &p_owner->lock );
2083 DecoderFlushBuffering( p_dec );
2085 p_vout = p_owner->p_vout;
2086 p_owner->p_vout = NULL;
2087 vlc_mutex_unlock( &p_owner->lock );
2089 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2091 vlc_mutex_lock( &p_owner->lock );
2092 p_owner->p_vout = p_vout;
2093 vlc_mutex_unlock( &p_owner->lock );
2095 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2096 if( p_vout == NULL )
2098 msg_Err( p_dec, "failed to create video output" );
2099 p_dec->b_error = true;
2103 if( p_owner->video.i_rmask )
2104 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2105 if( p_owner->video.i_gmask )
2106 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2107 if( p_owner->video.i_bmask )
2108 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2111 /* Get a new picture
2115 picture_t *p_picture;
2117 if( p_dec->b_die || p_dec->b_error )
2120 /* The video filter chain required that there is always 1 free buffer
2121 * that it will use as temporary one. It will release the temporary
2122 * buffer once its work is done, so this check is safe even if we don't
2123 * lock around both count() and create().
2125 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2127 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2132 if( DecoderIsFlushing( p_dec ) )
2136 DecoderSignalBuffering( p_dec, true );
2138 /* Check the decoder doesn't leak pictures */
2139 vout_FixLeaks( p_owner->p_vout, false );
2141 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2142 msleep( VOUT_OUTMEM_SLEEP );
2146 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2148 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2151 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2153 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2156 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2158 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2161 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2163 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2164 vout_thread_t *p_vout = NULL;
2165 subpicture_t *p_subpic;
2166 int i_attempts = 30;
2168 while( i_attempts-- )
2170 if( p_dec->b_die || p_dec->b_error )
2173 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2177 msleep( VOUT_DISPLAY_DELAY );
2182 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2186 if( p_owner->p_spu_vout != p_vout )
2188 vlc_mutex_lock( &p_owner->lock );
2190 DecoderFlushBuffering( p_dec );
2192 vlc_mutex_unlock( &p_owner->lock );
2194 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2195 &p_owner->i_spu_channel );
2196 p_owner->i_spu_order = 0;
2197 p_owner->p_spu_vout = p_vout;
2200 p_subpic = subpicture_New();
2203 p_subpic->i_channel = p_owner->i_spu_channel;
2204 p_subpic->i_order = p_owner->i_spu_order++;
2205 p_subpic->b_subtitle = true;
2208 vlc_object_release( p_vout );
2213 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2215 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2216 vout_thread_t *p_vout = NULL;
2218 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2219 if( !p_vout || p_owner->p_spu_vout != p_vout )
2222 vlc_object_release( p_vout );
2223 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2227 subpicture_Delete( p_subpic );
2229 vlc_object_release( p_vout );