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 /* Make sure the thread leaves the function */
314 block_FifoWake( p_owner->p_fifo );
316 vlc_thread_join( p_dec );
318 /* Don't module_unneed() here because of the dll loader that wants
319 * close() in the same thread than decode() */
322 if( p_dec->p_owner->cc.b_supported )
325 for( i = 0; i < 4; i++ )
326 input_DecoderSetCcState( p_dec, false, i );
329 /* Delete decoder configuration */
330 DeleteDecoder( p_dec );
332 /* Delete the decoder */
333 vlc_object_release( p_dec );
337 * Put a block_t in the decoder's fifo.
338 * Thread-safe w.r.t. the decoder. May be a cancellation point.
340 * \param p_dec the decoder object
341 * \param p_block the data block
343 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
345 decoder_owner_sys_t *p_owner = p_dec->p_owner;
347 if( p_owner->p_input->p->b_out_pace_control )
349 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
351 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
353 /* FIXME: ideally we would check the time amount of data
354 * in the FIFO instead of its size. */
355 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
356 "consumed quickly enough), resetting fifo!" );
357 block_FifoEmpty( p_owner->p_fifo );
360 block_FifoPut( p_owner->p_fifo, p_block );
363 bool input_DecoderIsEmpty( decoder_t * p_dec )
365 assert( !p_dec->p_owner->b_buffering );
367 /* FIXME that's not really true */
368 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
371 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
373 decoder_owner_sys_t *p_owner = p_dec->p_owner;
376 vlc_mutex_lock( &p_owner->lock );
377 for( i = 0; i < 4; i++ )
378 pb_present[i] = p_owner->cc.pb_present[i];
379 vlc_mutex_unlock( &p_owner->lock );
381 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
383 decoder_owner_sys_t *p_owner = p_dec->p_owner;
385 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
387 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
392 static const vlc_fourcc_t fcc[4] = {
393 VLC_FOURCC('c', 'c', '1', ' '),
394 VLC_FOURCC('c', 'c', '2', ' '),
395 VLC_FOURCC('c', 'c', '3', ' '),
396 VLC_FOURCC('c', 'c', '4', ' '),
401 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
402 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
405 msg_Err( p_dec, "could not create decoder" );
406 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
407 _("VLC could not open the decoder module.") );
410 else if( !p_cc->p_module )
412 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
413 DeleteDecoder( p_cc );
414 vlc_object_release( p_cc );
417 p_cc->p_owner->p_clock = p_owner->p_clock;
419 vlc_mutex_lock( &p_owner->lock );
420 p_owner->cc.pp_decoder[i_channel] = p_cc;
421 vlc_mutex_unlock( &p_owner->lock );
427 vlc_mutex_lock( &p_owner->lock );
428 p_cc = p_owner->cc.pp_decoder[i_channel];
429 p_owner->cc.pp_decoder[i_channel] = NULL;
430 vlc_mutex_unlock( &p_owner->lock );
434 vlc_object_kill( p_cc );
435 module_unneed( p_cc, p_cc->p_module );
436 DeleteDecoder( p_cc );
437 vlc_object_release( p_cc );
442 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
444 decoder_owner_sys_t *p_owner = p_dec->p_owner;
447 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
450 vlc_mutex_lock( &p_owner->lock );
451 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
452 vlc_mutex_unlock( &p_owner->lock );
456 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
458 decoder_owner_sys_t *p_owner = p_dec->p_owner;
460 vlc_mutex_lock( &p_owner->lock );
462 assert( !p_owner->b_paused || !b_paused );
464 p_owner->b_paused = b_paused;
465 p_owner->pause.i_date = i_date;
466 p_owner->pause.i_ignore = 0;
467 vlc_cond_signal( &p_owner->wait );
469 DecoderOutputChangePause( p_dec, b_paused, i_date );
471 vlc_mutex_unlock( &p_owner->lock );
474 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
476 decoder_owner_sys_t *p_owner = p_dec->p_owner;
478 vlc_mutex_lock( &p_owner->lock );
479 p_owner->i_ts_delay = i_delay;
480 vlc_mutex_unlock( &p_owner->lock );
483 void input_DecoderStartBuffering( decoder_t *p_dec )
485 decoder_owner_sys_t *p_owner = p_dec->p_owner;
487 vlc_mutex_lock( &p_owner->lock );
489 DecoderFlush( p_dec );
491 p_owner->buffer.b_first = true;
492 p_owner->buffer.b_full = false;
493 p_owner->buffer.i_count = 0;
495 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
497 p_owner->buffer.p_picture = NULL;
498 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
500 p_owner->buffer.p_subpic = NULL;
501 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
503 p_owner->buffer.p_audio = NULL;
504 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
506 p_owner->b_buffering = true;
508 vlc_cond_signal( &p_owner->wait );
510 vlc_mutex_unlock( &p_owner->lock );
513 void input_DecoderStopBuffering( decoder_t *p_dec )
515 decoder_owner_sys_t *p_owner = p_dec->p_owner;
517 vlc_mutex_lock( &p_owner->lock );
519 p_owner->b_buffering = false;
521 vlc_cond_signal( &p_owner->wait );
523 vlc_mutex_unlock( &p_owner->lock );
526 void input_DecoderWaitBuffering( decoder_t *p_dec )
528 decoder_owner_sys_t *p_owner = p_dec->p_owner;
530 vlc_mutex_lock( &p_owner->lock );
532 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
534 block_FifoWake( p_owner->p_fifo );
535 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
538 vlc_mutex_unlock( &p_owner->lock );
540 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
542 decoder_owner_sys_t *p_owner = p_dec->p_owner;
546 vlc_mutex_lock( &p_owner->lock );
547 if( p_dec->fmt_in.i_cat == VIDEO_ES )
549 if( p_owner->b_paused && p_owner->p_vout )
551 vout_NextPicture( p_owner->p_vout, pi_duration );
552 p_owner->pause.i_ignore++;
553 vlc_cond_signal( &p_owner->wait );
558 /* TODO subtitle should not be flushed */
559 DecoderFlush( p_dec );
561 vlc_mutex_unlock( &p_owner->lock );
564 /*****************************************************************************
566 *****************************************************************************/
567 static int DecoderGetInputAttachments( decoder_t *p_dec,
568 input_attachment_t ***ppp_attachment,
571 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
572 ppp_attachment, pi_attachment );
574 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
576 decoder_owner_sys_t *p_owner = p_dec->p_owner;
578 vlc_mutex_lock( &p_owner->lock );
579 if( p_owner->b_buffering || p_owner->b_paused )
581 vlc_mutex_unlock( &p_owner->lock );
583 if( !p_owner->p_clock || !i_ts )
586 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
588 static int DecoderGetDisplayRate( decoder_t *p_dec )
590 decoder_owner_sys_t *p_owner = p_dec->p_owner;
592 if( !p_owner->p_clock )
593 return INPUT_RATE_DEFAULT;
594 return input_clock_GetRate( p_owner->p_clock );
598 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
600 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
601 "VLC probably does not support this sound or video format.",
603 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
604 _("VLC does not support the audio or video format \"%4.4s\". "
605 "Unfortunately there is no way for you to fix this."), (char*)&codec );
610 * Create a decoder object
612 * \param p_input the input thread
613 * \param p_es the es descriptor
614 * \param i_object_type Object type as define in include/vlc_objects.h
615 * \return the decoder object
617 static decoder_t * CreateDecoder( input_thread_t *p_input,
618 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
621 decoder_owner_sys_t *p_owner;
624 p_dec = vlc_object_create( p_input, i_object_type );
628 p_dec->pf_decode_audio = NULL;
629 p_dec->pf_decode_video = NULL;
630 p_dec->pf_decode_sub = NULL;
631 p_dec->pf_get_cc = NULL;
632 p_dec->pf_packetize = NULL;
634 /* Initialize the decoder fifo */
635 p_dec->p_module = NULL;
637 memset( &null_es_format, 0, sizeof(es_format_t) );
638 es_format_Copy( &p_dec->fmt_in, fmt );
639 es_format_Copy( &p_dec->fmt_out, &null_es_format );
641 /* Allocate our private structure for the decoder */
642 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
643 if( p_dec->p_owner == NULL )
645 vlc_object_release( p_dec );
648 p_dec->p_owner->i_preroll_end = -1;
649 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
650 p_dec->p_owner->p_input = p_input;
651 p_dec->p_owner->p_aout = NULL;
652 p_dec->p_owner->p_aout_input = NULL;
653 p_dec->p_owner->p_vout = NULL;
654 p_dec->p_owner->p_spu_vout = NULL;
655 p_dec->p_owner->i_spu_channel = 0;
656 p_dec->p_owner->i_spu_order = 0;
657 p_dec->p_owner->p_sout = p_sout;
658 p_dec->p_owner->p_sout_input = NULL;
659 p_dec->p_owner->p_packetizer = NULL;
662 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
664 free( p_dec->p_owner );
665 vlc_object_release( p_dec );
669 /* Set buffers allocation callbacks for the decoders */
670 p_dec->pf_aout_buffer_new = aout_new_buffer;
671 p_dec->pf_aout_buffer_del = aout_del_buffer;
672 p_dec->pf_vout_buffer_new = vout_new_buffer;
673 p_dec->pf_vout_buffer_del = vout_del_buffer;
674 p_dec->pf_picture_link = vout_link_picture;
675 p_dec->pf_picture_unlink = vout_unlink_picture;
676 p_dec->pf_spu_buffer_new = spu_new_buffer;
677 p_dec->pf_spu_buffer_del = spu_del_buffer;
679 p_dec->pf_get_attachments = DecoderGetInputAttachments;
680 p_dec->pf_get_display_date = DecoderGetDisplayDate;
681 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
683 vlc_object_attach( p_dec, p_input );
685 /* Find a suitable decoder/packetizer module */
686 if( i_object_type == VLC_OBJECT_DECODER )
687 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
689 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
691 /* Check if decoder requires already packetized data */
692 if( i_object_type == VLC_OBJECT_DECODER &&
693 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
695 p_dec->p_owner->p_packetizer =
696 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
697 if( p_dec->p_owner->p_packetizer )
699 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
702 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
705 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
707 p_dec->p_owner->p_packetizer->p_module =
708 module_need( p_dec->p_owner->p_packetizer,
709 "packetizer", "$packetizer", 0 );
711 if( !p_dec->p_owner->p_packetizer->p_module )
713 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
714 vlc_object_detach( p_dec->p_owner->p_packetizer );
715 vlc_object_release( p_dec->p_owner->p_packetizer );
720 /* Copy ourself the input replay gain */
721 if( fmt->i_cat == AUDIO_ES )
723 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
725 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
727 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
728 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
730 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
732 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
733 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
739 vlc_mutex_init( &p_owner->lock );
740 vlc_cond_init( &p_owner->wait );
742 p_owner->b_paused = false;
743 p_owner->pause.i_date = 0;
744 p_owner->pause.i_ignore = 0;
746 p_owner->b_buffering = false;
747 p_owner->buffer.b_first = true;
748 p_owner->buffer.b_full = false;
749 p_owner->buffer.i_count = 0;
750 p_owner->buffer.p_picture = NULL;
751 p_owner->buffer.p_subpic = NULL;
752 p_owner->buffer.p_audio = NULL;
754 p_owner->b_flushing = false;
757 p_owner->cc.b_supported = false;
758 if( i_object_type == VLC_OBJECT_DECODER )
760 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
761 p_owner->cc.b_supported = true;
762 if( p_dec->pf_get_cc )
763 p_owner->cc.b_supported = true;
766 for( i = 0; i < 4; i++ )
768 p_owner->cc.pb_present[i] = false;
769 p_owner->cc.pp_decoder[i] = NULL;
771 p_owner->i_ts_delay = 0;
776 * The decoding main loop
778 * \param p_dec the decoder
780 static void *DecoderThread( vlc_object_t *p_this )
782 decoder_t *p_dec = (decoder_t *)p_this;
783 decoder_owner_sys_t *p_owner = p_dec->p_owner;
785 int canc = vlc_savecancel();
787 /* The decoder's main loop */
788 while( vlc_object_alive( p_dec ) )
790 block_t *p_block = block_FifoGet( p_owner->p_fifo );
792 DecoderSignalBuffering( p_dec, p_block == NULL );
797 { /* Trash all received PES packets */
798 block_Release( p_block );
800 else if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
807 DecoderSignalBuffering( p_dec, true );
809 /* We do it here because of the dll loader that wants close() in the
810 * same thread than decode() */
811 module_unneed( p_dec, p_dec->p_module );
812 vlc_restorecancel( canc );
816 static void DecoderFlush( decoder_t *p_dec )
818 decoder_owner_sys_t *p_owner = p_dec->p_owner;
821 vlc_assert_locked( &p_owner->lock );
824 block_FifoEmpty( p_owner->p_fifo );
826 /* Monitor for flush end */
827 p_owner->b_flushing = true;
828 vlc_cond_signal( &p_owner->wait );
830 /* Send a special block */
831 p_null = block_New( p_dec, 128 );
834 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
835 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
836 if( !p_dec->fmt_in.b_packetized )
837 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
838 memset( p_null->p_buffer, 0, p_null->i_buffer );
840 input_DecoderDecode( p_dec, p_null );
843 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
844 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
847 static void DecoderSignalFlushed( decoder_t *p_dec )
849 decoder_owner_sys_t *p_owner = p_dec->p_owner;
851 vlc_mutex_lock( &p_owner->lock );
853 if( p_owner->b_flushing )
855 p_owner->b_flushing = false;
856 vlc_cond_signal( &p_owner->wait );
859 vlc_mutex_unlock( &p_owner->lock );
862 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
864 decoder_owner_sys_t *p_owner = p_dec->p_owner;
866 vlc_mutex_lock( &p_owner->lock );
868 if( p_owner->b_buffering )
871 p_owner->buffer.b_full = true;
872 vlc_cond_signal( &p_owner->wait );
875 vlc_mutex_unlock( &p_owner->lock );
878 static bool DecoderIsFlushing( decoder_t *p_dec )
880 decoder_owner_sys_t *p_owner = p_dec->p_owner;
883 vlc_mutex_lock( &p_owner->lock );
885 b_flushing = p_owner->b_flushing;
887 vlc_mutex_unlock( &p_owner->lock );
892 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
894 decoder_owner_sys_t *p_owner = p_dec->p_owner;
896 vlc_assert_locked( &p_owner->lock );
898 while( !p_owner->b_flushing )
900 if( p_owner->b_paused )
902 if( p_owner->b_buffering && !p_owner->buffer.b_full )
904 if( p_owner->pause.i_ignore > 0 )
906 p_owner->pause.i_ignore--;
912 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
915 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
919 *pb_reject = p_owner->b_flushing;
922 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
924 decoder_owner_sys_t *p_owner = p_dec->p_owner;
926 vlc_assert_locked( &p_owner->lock );
928 /* XXX only audio and video output have to be paused.
929 * - for sout it is useless
930 * - for subs, it is done by the vout
932 if( p_dec->fmt_in.i_cat == AUDIO_ES )
934 if( p_owner->p_aout && p_owner->p_aout_input )
935 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
938 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
940 if( p_owner->p_vout )
941 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
944 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
946 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
947 *pi_preroll = INT64_MAX;
948 else if( p->i_pts > 0 )
949 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
950 else if( p->i_dts > 0 )
951 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
954 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
956 mtime_t current_date = mdate();
958 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
959 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
961 /* ETSI EN 300 472 Annex A : do not take into account the PTS
962 * for teletext streams. */
963 return current_date + 400000 + i_ts_delay;
968 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
969 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
971 decoder_owner_sys_t *p_owner = p_dec->p_owner;
972 input_clock_t *p_clock = p_owner->p_clock;
975 vlc_assert_locked( &p_owner->lock );
977 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
978 const mtime_t i_es_delay = p_owner->i_ts_delay;
982 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
985 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
986 *pi_ts0 + i_es_delay );
987 if( pi_ts1 && *pi_ts1 > 0 )
988 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
989 *pi_ts1 + i_es_delay );
991 /* Do not create ephemere data because of rounding errors */
992 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
996 i_rate = input_clock_GetRate( p_clock );
999 *pi_duration = ( *pi_duration * i_rate +
1000 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1007 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1008 if( pi_ts1 && *pi_ts1 <= 0 )
1014 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1015 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1019 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1020 int *pi_played_sum, int *pi_lost_sum )
1022 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1023 aout_instance_t *p_aout = p_owner->p_aout;
1024 aout_input_t *p_aout_input = p_owner->p_aout_input;
1027 if( p_audio->start_date <= 0 )
1029 msg_Warn( p_dec, "non-dated audio buffer received" );
1031 aout_BufferFree( p_audio );
1036 vlc_mutex_lock( &p_owner->lock );
1038 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1040 p_audio->p_next = NULL;
1042 *p_owner->buffer.pp_audio_next = p_audio;
1043 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1045 p_owner->buffer.i_count++;
1046 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1047 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1049 p_owner->buffer.b_full = true;
1050 vlc_cond_signal( &p_owner->wait );
1056 bool b_has_more = false;
1058 DecoderWaitUnblock( p_dec, &b_reject );
1060 if( p_owner->b_buffering )
1062 vlc_mutex_unlock( &p_owner->lock );
1067 if( p_owner->buffer.p_audio )
1069 p_audio = p_owner->buffer.p_audio;
1071 p_owner->buffer.p_audio = p_audio->p_next;
1072 p_owner->buffer.i_count--;
1074 b_has_more = p_owner->buffer.p_audio != NULL;
1076 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1080 int i_rate = INPUT_RATE_DEFAULT;
1083 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1084 &i_rate, &i_delay, false );
1086 vlc_mutex_unlock( &p_owner->lock );
1089 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1091 if( !p_aout || !p_aout_input ||
1092 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1093 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1094 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1099 /* Wait if we are too early
1100 * FIXME that's plain ugly to do it here */
1101 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1103 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1104 *pi_played_sum += 1;
1105 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1109 if( p_audio->start_date <= 0 )
1111 msg_Warn( p_dec, "non-dated audio buffer received" );
1113 else if( p_audio->start_date > i_max_date )
1115 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1116 p_audio->start_date - mdate() );
1119 aout_BufferFree( p_audio );
1125 vlc_mutex_lock( &p_owner->lock );
1126 if( !p_owner->buffer.p_audio )
1128 vlc_mutex_unlock( &p_owner->lock );
1134 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1136 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1137 input_thread_t *p_input = p_owner->p_input;
1138 aout_buffer_t *p_aout_buf;
1143 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1145 aout_instance_t *p_aout = p_owner->p_aout;
1146 aout_input_t *p_aout_input = p_owner->p_aout_input;
1152 /* It prevent freezing VLC in case of broken decoder */
1153 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1155 block_Release( p_block );
1160 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1162 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1166 if( p_owner->i_preroll_end > 0 )
1168 msg_Dbg( p_dec, "End of audio preroll" );
1169 if( p_owner->p_aout && p_owner->p_aout_input )
1170 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1172 p_owner->i_preroll_end = -1;
1175 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1178 /* Update ugly stat */
1179 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1181 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1183 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1185 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1187 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1190 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1193 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1195 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1201 assert( p_dec_cc->pf_get_cc != NULL );
1203 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1204 if( !p_owner->cc.b_supported )
1207 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1211 vlc_mutex_lock( &p_owner->lock );
1212 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1214 p_owner->cc.pb_present[i] |= pb_present[i];
1215 if( p_owner->cc.pp_decoder[i] )
1219 for( i = 0; i < 4; i++ )
1221 if( !p_owner->cc.pp_decoder[i] )
1224 if( i_cc_decoder > 1 )
1225 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1227 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1230 vlc_mutex_unlock( &p_owner->lock );
1233 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1234 int *pi_played_sum, int *pi_lost_sum )
1236 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1237 vout_thread_t *p_vout = p_owner->p_vout;
1238 bool b_first_buffered;
1240 if( p_picture->date <= 0 )
1242 msg_Warn( p_vout, "non-dated video buffer received" );
1244 vout_DropPicture( p_vout, p_picture );
1249 vlc_mutex_lock( &p_owner->lock );
1251 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1253 p_picture->p_next = NULL;
1255 *p_owner->buffer.pp_picture_next = p_picture;
1256 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1258 p_owner->buffer.i_count++;
1259 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1260 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1262 p_owner->buffer.b_full = true;
1263 vlc_cond_signal( &p_owner->wait );
1266 b_first_buffered = p_owner->buffer.p_picture != NULL;
1268 for( ;; b_first_buffered = false )
1270 bool b_has_more = false;
1274 DecoderWaitUnblock( p_dec, &b_reject );
1276 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1278 vlc_mutex_unlock( &p_owner->lock );
1281 bool b_buffering_first = p_owner->b_buffering;
1284 if( p_owner->buffer.p_picture )
1286 p_picture = p_owner->buffer.p_picture;
1288 p_owner->buffer.p_picture = p_picture->p_next;
1289 p_owner->buffer.i_count--;
1291 b_has_more = p_owner->buffer.p_picture != NULL;
1293 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1297 int i_rate = INPUT_RATE_DEFAULT;
1300 if( b_buffering_first )
1302 assert( p_owner->buffer.b_first );
1303 assert( !p_owner->buffer.i_count );
1304 msg_Dbg( p_dec, "Received first picture" );
1305 p_owner->buffer.b_first = false;
1306 p_picture->b_force = true;
1308 if( p_owner->p_clock )
1309 i_rate = input_clock_GetRate( p_owner->p_clock );
1311 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1312 &i_rate, &i_delay, false );
1314 vlc_mutex_unlock( &p_owner->lock );
1317 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1319 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1324 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1326 /* Be sure to not display old picture after our own */
1327 vout_Flush( p_vout, p_picture->date );
1328 p_owner->i_last_rate = i_rate;
1330 vout_DisplayPicture( p_vout, p_picture );
1334 if( p_picture->date <= 0 )
1336 msg_Warn( p_vout, "non-dated video buffer received" );
1340 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1341 p_picture->date - mdate() );
1344 vout_DropPicture( p_vout, p_picture );
1348 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1350 *pi_played_sum += i_tmp_display;
1351 *pi_lost_sum += i_tmp_lost;
1353 if( !b_has_more || b_buffering_first )
1356 vlc_mutex_lock( &p_owner->lock );
1357 if( !p_owner->buffer.p_picture )
1359 vlc_mutex_unlock( &p_owner->lock );
1365 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1367 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1368 input_thread_t *p_input = p_owner->p_input;
1372 int i_displayed = 0;
1374 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1376 vout_thread_t *p_vout = p_owner->p_vout;
1379 /* It prevent freezing VLC in case of broken decoder */
1380 vout_DropPicture( p_vout, p_pic );
1382 block_Release( p_block );
1388 if( p_pic->date < p_owner->i_preroll_end )
1390 vout_DropPicture( p_vout, p_pic );
1394 if( p_owner->i_preroll_end > 0 )
1396 msg_Dbg( p_dec, "End of video preroll" );
1398 vout_Flush( p_vout, 1 );
1400 p_owner->i_preroll_end = -1;
1403 if( p_dec->pf_get_cc &&
1404 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1405 DecoderGetCc( p_dec, p_dec );
1407 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1409 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1411 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1413 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1415 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1418 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1421 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1425 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1428 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1429 vout_thread_t *p_vout = p_owner->p_spu_vout;
1432 if( p_subpic->i_start <= 0 )
1434 msg_Warn( p_dec, "non-dated spu buffer received" );
1435 subpicture_Delete( p_subpic );
1440 vlc_mutex_lock( &p_owner->lock );
1442 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1444 p_subpic->p_next = NULL;
1446 *p_owner->buffer.pp_subpic_next = p_subpic;
1447 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1449 p_owner->buffer.i_count++;
1450 /* XXX it is important to be full after the first one */
1451 if( p_owner->buffer.i_count > 0 )
1453 p_owner->buffer.b_full = true;
1454 vlc_cond_signal( &p_owner->wait );
1460 bool b_has_more = false;
1462 DecoderWaitUnblock( p_dec, &b_reject );
1464 if( p_owner->b_buffering )
1466 vlc_mutex_unlock( &p_owner->lock );
1471 if( p_owner->buffer.p_subpic )
1473 p_subpic = p_owner->buffer.p_subpic;
1475 p_owner->buffer.p_subpic = p_subpic->p_next;
1476 p_owner->buffer.i_count--;
1478 b_has_more = p_owner->buffer.p_subpic != NULL;
1480 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1484 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1485 NULL, NULL, b_telx );
1487 vlc_mutex_unlock( &p_owner->lock );
1490 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1492 subpicture_Delete( p_subpic );
1496 vlc_mutex_lock( &p_owner->lock );
1497 if( !p_owner->buffer.p_subpic )
1499 vlc_mutex_unlock( &p_owner->lock );
1505 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1508 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1510 assert( p_owner->p_clock );
1512 vlc_mutex_lock( &p_owner->lock );
1515 DecoderWaitUnblock( p_dec, &b_reject );
1517 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1518 &p_sout_block->i_rate, NULL, b_telx );
1520 vlc_mutex_unlock( &p_owner->lock );
1522 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1526 static void DecoderFlushBuffering( decoder_t *p_dec )
1528 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1530 vlc_assert_locked( &p_owner->lock );
1532 while( p_owner->buffer.p_picture )
1534 picture_t *p_picture = p_owner->buffer.p_picture;
1536 p_owner->buffer.p_picture = p_picture->p_next;
1537 p_owner->buffer.i_count--;
1539 if( p_owner->p_vout )
1540 vout_DropPicture( p_owner->p_vout, p_picture );
1542 if( !p_owner->buffer.p_picture )
1543 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1545 while( p_owner->buffer.p_audio )
1547 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1549 p_owner->buffer.p_audio = p_audio->p_next;
1550 p_owner->buffer.i_count--;
1552 aout_BufferFree( p_audio );
1554 if( !p_owner->buffer.p_audio )
1555 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1557 while( p_owner->buffer.p_subpic )
1559 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1561 p_owner->buffer.p_subpic = p_subpic->p_next;
1562 p_owner->buffer.i_count--;
1564 subpicture_Delete( p_subpic );
1566 if( !p_owner->buffer.p_subpic )
1567 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1571 /* This function process a block for sout
1573 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1575 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1576 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1577 block_t *p_sout_block;
1579 while( ( p_sout_block =
1580 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1582 if( !p_owner->p_sout_input )
1584 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1586 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1587 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1588 if( p_dec->fmt_in.psz_language )
1590 if( p_owner->sout.psz_language )
1591 free( p_owner->sout.psz_language );
1592 p_owner->sout.psz_language =
1593 strdup( p_dec->fmt_in.psz_language );
1596 p_owner->p_sout_input =
1597 sout_InputNew( p_owner->p_sout,
1600 if( p_owner->p_sout_input == NULL )
1602 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1603 (char *)&p_owner->sout.i_codec );
1604 p_dec->b_error = true;
1606 while( p_sout_block )
1608 block_t *p_next = p_sout_block->p_next;
1609 block_Release( p_sout_block );
1610 p_sout_block = p_next;
1616 while( p_sout_block )
1618 block_t *p_next = p_sout_block->p_next;
1620 p_sout_block->p_next = NULL;
1622 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1624 p_sout_block = p_next;
1627 /* For now it's enough, as only sout impact on this flag */
1628 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1629 p_owner->p_input->p->b_out_pace_control )
1631 msg_Dbg( p_dec, "switching to sync mode" );
1632 p_owner->p_input->p->b_out_pace_control = false;
1634 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1635 !p_owner->p_input->p->b_out_pace_control )
1637 msg_Dbg( p_dec, "switching to async mode" );
1638 p_owner->p_input->p->b_out_pace_control = true;
1643 /* This function process a video block
1645 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1647 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1649 if( p_owner->p_packetizer )
1651 block_t *p_packetized_block;
1652 decoder_t *p_packetizer = p_owner->p_packetizer;
1654 while( (p_packetized_block =
1655 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1657 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1659 es_format_Clean( &p_dec->fmt_in );
1660 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1662 if( p_packetizer->pf_get_cc )
1663 DecoderGetCc( p_dec, p_packetizer );
1665 while( p_packetized_block )
1667 block_t *p_next = p_packetized_block->p_next;
1668 p_packetized_block->p_next = NULL;
1670 DecoderDecodeVideo( p_dec, p_packetized_block );
1672 p_packetized_block = p_next;
1678 DecoderDecodeVideo( p_dec, p_block );
1681 if( b_flush && p_owner->p_vout )
1682 vout_Flush( p_owner->p_vout, 1 );
1685 /* This function process a audio block
1687 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1689 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1691 if( p_owner->p_packetizer )
1693 block_t *p_packetized_block;
1694 decoder_t *p_packetizer = p_owner->p_packetizer;
1696 while( (p_packetized_block =
1697 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1699 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1701 es_format_Clean( &p_dec->fmt_in );
1702 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1705 while( p_packetized_block )
1707 block_t *p_next = p_packetized_block->p_next;
1708 p_packetized_block->p_next = NULL;
1710 DecoderDecodeAudio( p_dec, p_packetized_block );
1712 p_packetized_block = p_next;
1718 DecoderDecodeAudio( p_dec, p_block );
1721 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1722 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1725 /* This function process a subtitle block
1727 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1729 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1730 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1732 input_thread_t *p_input = p_owner->p_input;
1733 vout_thread_t *p_vout;
1734 subpicture_t *p_spu;
1736 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1738 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1739 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1740 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1742 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1743 if( p_vout && p_owner->p_spu_vout == p_vout )
1745 /* Preroll does not work very well with subtitle */
1746 if( p_spu->i_start > 0 &&
1747 p_spu->i_start < p_owner->i_preroll_end &&
1748 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1750 subpicture_Delete( p_spu );
1754 DecoderPlaySpu( p_dec, p_spu, b_telx );
1759 subpicture_Delete( p_spu );
1762 vlc_object_release( p_vout );
1765 if( b_flush && p_owner->p_spu_vout )
1767 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1769 if( p_vout && p_owner->p_spu_vout == p_vout )
1770 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1771 p_owner->i_spu_channel );
1774 vlc_object_release( p_vout );
1781 * \param p_dec the decoder object
1782 * \param p_block the block to decode
1783 * \return VLC_SUCCESS or an error code
1785 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1787 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1788 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1790 if( p_block && p_block->i_buffer <= 0 )
1792 assert( !b_flush_request );
1793 block_Release( p_block );
1798 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1801 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1803 DecoderProcessSout( p_dec, p_block );
1808 bool b_flush = false;
1812 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1813 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1815 b_flush = !b_flushing && b_flush_request;
1817 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1820 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1822 DecoderProcessAudio( p_dec, p_block, b_flush );
1824 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1826 DecoderProcessVideo( p_dec, p_block, b_flush );
1828 else if( p_dec->fmt_in.i_cat == SPU_ES )
1830 DecoderProcessSpu( p_dec, p_block, b_flush );
1834 msg_Err( p_dec, "unknown ES format" );
1835 p_dec->b_error = true;
1840 if( b_flush_request )
1842 vlc_mutex_lock( &p_owner->lock );
1843 DecoderFlushBuffering( p_dec );
1844 vlc_mutex_unlock( &p_owner->lock );
1846 DecoderSignalFlushed( p_dec );
1849 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1853 * Destroys a decoder object
1855 * \param p_dec the decoder object
1858 static void DeleteDecoder( decoder_t * p_dec )
1860 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1862 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1863 (char*)&p_dec->fmt_in.i_codec,
1864 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1866 /* Free all packets still in the decoder fifo. */
1867 block_FifoEmpty( p_owner->p_fifo );
1868 block_FifoRelease( p_owner->p_fifo );
1871 vlc_mutex_lock( &p_owner->lock );
1872 DecoderFlushBuffering( p_dec );
1873 vlc_mutex_unlock( &p_owner->lock );
1876 if( p_owner->p_aout_input )
1877 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1878 if( p_owner->p_aout )
1880 vlc_object_release( p_owner->p_aout );
1881 p_owner->p_aout = NULL;
1883 if( p_owner->p_vout )
1885 /* Hack to make sure all the the pictures are freed by the decoder */
1886 vout_FixLeaks( p_owner->p_vout, true );
1888 /* We are about to die. Reattach video output to p_vlc. */
1889 vout_Request( p_dec, p_owner->p_vout, NULL );
1890 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1894 if( p_owner->p_sout_input )
1896 sout_InputDelete( p_owner->p_sout_input );
1897 es_format_Clean( &p_owner->sout );
1901 if( p_dec->fmt_in.i_cat == SPU_ES )
1903 vout_thread_t *p_vout;
1905 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1906 if( p_vout && p_owner->p_spu_vout == p_vout )
1908 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1909 p_owner->i_spu_channel );
1910 vlc_object_release( p_vout );
1914 es_format_Clean( &p_dec->fmt_in );
1915 es_format_Clean( &p_dec->fmt_out );
1917 if( p_owner->p_packetizer )
1919 module_unneed( p_owner->p_packetizer,
1920 p_owner->p_packetizer->p_module );
1921 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1922 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1923 vlc_object_detach( p_owner->p_packetizer );
1924 vlc_object_release( p_owner->p_packetizer );
1927 vlc_cond_destroy( &p_owner->wait );
1928 vlc_mutex_destroy( &p_owner->lock );
1930 vlc_object_detach( p_dec );
1935 /*****************************************************************************
1936 * Buffers allocation callbacks for the decoders
1937 *****************************************************************************/
1938 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1940 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1941 aout_buffer_t *p_buffer;
1943 if( p_owner->p_aout_input != NULL &&
1944 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1945 p_dec->fmt_out.audio.i_original_channels !=
1946 p_owner->audio.i_original_channels ||
1947 p_dec->fmt_out.audio.i_bytes_per_frame !=
1948 p_owner->audio.i_bytes_per_frame ) )
1950 aout_input_t *p_aout_input = p_owner->p_aout_input;
1952 /* Parameters changed, restart the aout */
1953 vlc_mutex_lock( &p_owner->lock );
1955 DecoderFlushBuffering( p_dec );
1957 p_owner->p_aout_input = NULL;
1958 aout_DecDelete( p_owner->p_aout, p_aout_input );
1960 vlc_mutex_unlock( &p_owner->lock );
1963 if( p_owner->p_aout_input == NULL )
1965 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1966 audio_sample_format_t format;
1967 aout_input_t *p_aout_input;
1968 aout_instance_t *p_aout;
1970 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1971 p_owner->audio = p_dec->fmt_out.audio;
1973 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1974 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1975 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1977 if ( i_force_dolby == 1 )
1979 format.i_original_channels = format.i_original_channels |
1980 AOUT_CHAN_DOLBYSTEREO;
1982 else /* i_force_dolby == 2 */
1984 format.i_original_channels = format.i_original_channels &
1985 ~AOUT_CHAN_DOLBYSTEREO;
1989 p_aout = p_owner->p_aout;
1990 p_aout_input = aout_DecNew( p_dec, &p_aout,
1991 &format, &p_dec->fmt_out.audio_replay_gain );
1993 vlc_mutex_lock( &p_owner->lock );
1994 p_owner->p_aout = p_aout;
1995 p_owner->p_aout_input = p_aout_input;
1996 vlc_mutex_unlock( &p_owner->lock );
1998 if( p_owner->p_aout_input == NULL )
2000 msg_Err( p_dec, "failed to create audio output" );
2001 p_dec->b_error = true;
2004 p_dec->fmt_out.audio.i_bytes_per_frame =
2005 p_owner->audio.i_bytes_per_frame;
2008 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2013 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2015 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2017 aout_DecDeleteBuffer( p_owner->p_aout,
2018 p_owner->p_aout_input, p_buffer );
2022 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2024 static picture_t *vout_new_buffer( decoder_t *p_dec )
2026 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2028 if( p_owner->p_vout == NULL ||
2029 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2030 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2031 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2032 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2034 vout_thread_t *p_vout;
2036 if( !p_dec->fmt_out.video.i_width ||
2037 !p_dec->fmt_out.video.i_height )
2039 /* Can't create a new vout without display size */
2043 if( !p_dec->fmt_out.video.i_visible_width ||
2044 !p_dec->fmt_out.video.i_visible_height )
2046 if( p_dec->fmt_in.video.i_visible_width &&
2047 p_dec->fmt_in.video.i_visible_height )
2049 p_dec->fmt_out.video.i_visible_width =
2050 p_dec->fmt_in.video.i_visible_width;
2051 p_dec->fmt_out.video.i_visible_height =
2052 p_dec->fmt_in.video.i_visible_height;
2056 p_dec->fmt_out.video.i_visible_width =
2057 p_dec->fmt_out.video.i_width;
2058 p_dec->fmt_out.video.i_visible_height =
2059 p_dec->fmt_out.video.i_height;
2063 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2064 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2066 p_dec->fmt_out.video.i_visible_height = 1080;
2067 p_dec->fmt_out.video.i_sar_num *= 135;
2068 p_dec->fmt_out.video.i_sar_den *= 136;
2069 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2072 if( !p_dec->fmt_out.video.i_sar_num ||
2073 !p_dec->fmt_out.video.i_sar_den )
2075 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2076 p_dec->fmt_out.video.i_visible_height;
2078 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2079 p_dec->fmt_out.video.i_visible_width;
2082 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2083 &p_dec->fmt_out.video.i_sar_den,
2084 p_dec->fmt_out.video.i_sar_num,
2085 p_dec->fmt_out.video.i_sar_den, 50000 );
2087 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2088 p_owner->video = p_dec->fmt_out.video;
2090 vlc_mutex_lock( &p_owner->lock );
2092 DecoderFlushBuffering( p_dec );
2094 p_vout = p_owner->p_vout;
2095 p_owner->p_vout = NULL;
2096 vlc_mutex_unlock( &p_owner->lock );
2098 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2100 vlc_mutex_lock( &p_owner->lock );
2101 p_owner->p_vout = p_vout;
2102 vlc_mutex_unlock( &p_owner->lock );
2104 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2105 if( p_vout == NULL )
2107 msg_Err( p_dec, "failed to create video output" );
2108 p_dec->b_error = true;
2112 if( p_owner->video.i_rmask )
2113 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2114 if( p_owner->video.i_gmask )
2115 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2116 if( p_owner->video.i_bmask )
2117 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2120 /* Get a new picture
2124 picture_t *p_picture;
2126 if( p_dec->b_die || p_dec->b_error )
2129 /* The video filter chain required that there is always 1 free buffer
2130 * that it will use as temporary one. It will release the temporary
2131 * buffer once its work is done, so this check is safe even if we don't
2132 * lock around both count() and create().
2134 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2136 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2141 if( DecoderIsFlushing( p_dec ) )
2145 DecoderSignalBuffering( p_dec, true );
2147 /* Check the decoder doesn't leak pictures */
2148 vout_FixLeaks( p_owner->p_vout, false );
2150 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2151 msleep( VOUT_OUTMEM_SLEEP );
2155 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2157 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2160 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2162 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2165 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2167 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2170 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2172 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2173 vout_thread_t *p_vout = NULL;
2174 subpicture_t *p_subpic;
2175 int i_attempts = 30;
2177 while( i_attempts-- )
2179 if( p_dec->b_die || p_dec->b_error )
2182 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2186 msleep( VOUT_DISPLAY_DELAY );
2191 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2195 if( p_owner->p_spu_vout != p_vout )
2197 vlc_mutex_lock( &p_owner->lock );
2199 DecoderFlushBuffering( p_dec );
2201 vlc_mutex_unlock( &p_owner->lock );
2203 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2204 &p_owner->i_spu_channel );
2205 p_owner->i_spu_order = 0;
2206 p_owner->p_spu_vout = p_vout;
2209 p_subpic = subpicture_New();
2212 p_subpic->i_channel = p_owner->i_spu_channel;
2213 p_subpic->i_order = p_owner->i_spu_order++;
2214 p_subpic->b_subtitle = true;
2217 vlc_object_release( p_vout );
2222 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2224 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2225 vout_thread_t *p_vout = NULL;
2227 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2228 if( !p_vout || p_owner->p_spu_vout != p_vout )
2231 vlc_object_release( p_vout );
2232 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2236 subpicture_Delete( p_subpic );
2238 vlc_object_release( p_vout );