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_internal.h"
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void DeleteDecoder( decoder_t * );
55 static void *DecoderThread( vlc_object_t * );
56 static int DecoderProcess( decoder_t *, block_t * );
57 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
58 static void DecoderFlush( decoder_t * );
59 static void DecoderSignalBuffering( decoder_t *, bool );
60 static void DecoderFlushBuffering( decoder_t * );
62 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
64 /* Buffers allocation callbacks for the decoders */
65 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
66 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
68 static picture_t *vout_new_buffer( decoder_t * );
69 static void vout_del_buffer( decoder_t *, picture_t * );
70 static void vout_link_picture( decoder_t *, picture_t * );
71 static void vout_unlink_picture( decoder_t *, picture_t * );
73 static subpicture_t *spu_new_buffer( decoder_t * );
74 static void spu_del_buffer( decoder_t *, subpicture_t * );
76 static es_format_t null_es_format;
78 struct decoder_owner_sys_t
80 int64_t i_preroll_end;
82 input_thread_t *p_input;
83 input_clock_t *p_clock;
86 vout_thread_t *p_spu_vout;
90 sout_instance_t *p_sout;
91 sout_packetizer_input_t *p_sout_input;
93 /* Some decoders require already packetized data (ie. not truncated) */
94 decoder_t *p_packetizer;
96 /* Current format in use by the output */
102 block_fifo_t *p_fifo;
104 /* Lock for communication with decoder thread */
108 /* -- These variables need locking on write(only) -- */
109 aout_instance_t *p_aout;
110 aout_input_t *p_aout_input;
112 vout_thread_t *p_vout;
114 /* -- Theses variables need locking on read *and* write -- */
131 picture_t *p_picture;
132 picture_t **pp_picture_next;
134 subpicture_t *p_subpic;
135 subpicture_t **pp_subpic_next;
137 aout_buffer_t *p_audio;
138 aout_buffer_t **pp_audio_next;
149 decoder_t *pp_decoder[4];
156 #define DECODER_MAX_BUFFERING_COUNT (4)
157 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
158 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
160 /*****************************************************************************
162 *****************************************************************************/
164 /* decoder_GetInputAttachment:
166 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
167 const char *psz_name )
169 input_attachment_t *p_attachment;
170 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
174 /* decoder_GetInputAttachments:
176 int decoder_GetInputAttachments( decoder_t *p_dec,
177 input_attachment_t ***ppp_attachment,
180 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
181 ppp_attachment, pi_attachment );
183 /* decoder_GetDisplayDate:
185 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
187 decoder_owner_sys_t *p_owner = p_dec->p_owner;
189 vlc_mutex_lock( &p_owner->lock );
190 if( p_owner->b_buffering )
192 vlc_mutex_unlock( &p_owner->lock );
194 if( !p_owner->p_clock || !i_ts )
197 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
199 /* decoder_GetDisplayRate:
201 int decoder_GetDisplayRate( decoder_t *p_dec )
203 decoder_owner_sys_t *p_owner = p_dec->p_owner;
205 if( !p_owner->p_clock )
206 return INPUT_RATE_DEFAULT;
207 return input_clock_GetRate( p_owner->p_clock );
211 * Spawns a new decoder thread
213 * \param p_input the input thread
214 * \param p_es the es descriptor
215 * \return the spawned decoder object
217 decoder_t *input_DecoderNew( input_thread_t *p_input,
218 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
220 decoder_t *p_dec = NULL;
224 /* If we are in sout mode, search for packetizer module */
227 /* Create the decoder configuration structure */
228 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
231 msg_Err( p_input, "could not create packetizer" );
232 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
233 _("VLC could not open the packetizer module.") );
240 /* Create the decoder configuration structure */
241 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
244 msg_Err( p_input, "could not create decoder" );
245 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
246 _("VLC could not open the decoder module.") );
251 if( !p_dec->p_module )
253 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
255 DeleteDecoder( p_dec );
256 vlc_object_release( p_dec );
260 p_dec->p_owner->p_clock = p_clock;
262 if( fmt->i_cat == AUDIO_ES )
263 i_priority = VLC_THREAD_PRIORITY_AUDIO;
265 i_priority = VLC_THREAD_PRIORITY_VIDEO;
267 /* Spawn the decoder thread */
268 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
269 i_priority, false ) )
271 msg_Err( p_dec, "cannot spawn decoder thread" );
272 module_unneed( p_dec, p_dec->p_module );
273 DeleteDecoder( p_dec );
274 vlc_object_release( p_dec );
282 * Kills a decoder thread and waits until it's finished
284 * \param p_input the input thread
285 * \param p_es the es descriptor
288 void input_DecoderDelete( decoder_t *p_dec )
290 decoder_owner_sys_t *p_owner = p_dec->p_owner;
292 vlc_object_kill( p_dec );
294 /* Make sure we aren't paused anymore */
295 vlc_mutex_lock( &p_owner->lock );
296 if( p_owner->b_paused || p_owner->b_buffering )
298 p_owner->b_paused = false;
299 p_owner->b_buffering = false;
300 vlc_cond_signal( &p_owner->wait );
302 vlc_mutex_unlock( &p_owner->lock );
304 /* Make sure the thread leaves the function */
305 block_FifoWake( p_owner->p_fifo );
307 vlc_thread_join( p_dec );
309 /* Don't module_unneed() here because of the dll loader that wants
310 * close() in the same thread than open()/decode() */
313 if( p_dec->p_owner->cc.b_supported )
316 for( i = 0; i < 4; i++ )
317 input_DecoderSetCcState( p_dec, false, i );
320 /* Delete decoder configuration */
321 DeleteDecoder( p_dec );
323 /* Delete the decoder */
324 vlc_object_release( p_dec );
328 * Put a block_t in the decoder's fifo.
330 * \param p_dec the decoder object
331 * \param p_block the data block
333 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
335 decoder_owner_sys_t *p_owner = p_dec->p_owner;
337 if( p_owner->p_input->p->b_out_pace_control )
340 while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
341 block_FifoCount( p_owner->p_fifo ) > 10 )
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 vlc_cond_signal( &p_owner->wait );
463 DecoderOutputChangePause( p_dec, b_paused, i_date );
465 vlc_mutex_unlock( &p_owner->lock );
468 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
470 decoder_owner_sys_t *p_owner = p_dec->p_owner;
472 vlc_mutex_lock( &p_owner->lock );
473 p_owner->i_ts_delay = i_delay;
474 vlc_mutex_unlock( &p_owner->lock );
477 void input_DecoderStartBuffering( decoder_t *p_dec )
479 decoder_owner_sys_t *p_owner = p_dec->p_owner;
481 vlc_mutex_lock( &p_owner->lock );
483 DecoderFlush( p_dec );
485 p_owner->buffer.b_first = true;
486 p_owner->buffer.b_full = false;
487 p_owner->buffer.i_count = 0;
489 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
491 p_owner->buffer.p_picture = NULL;
492 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
494 p_owner->buffer.p_subpic = NULL;
495 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
497 p_owner->buffer.p_audio = NULL;
498 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
500 p_owner->b_buffering = true;
502 vlc_cond_signal( &p_owner->wait );
504 vlc_mutex_unlock( &p_owner->lock );
507 void input_DecoderStopBuffering( decoder_t *p_dec )
509 decoder_owner_sys_t *p_owner = p_dec->p_owner;
511 vlc_mutex_lock( &p_owner->lock );
513 p_owner->b_buffering = false;
515 vlc_cond_signal( &p_owner->wait );
517 vlc_mutex_unlock( &p_owner->lock );
520 void input_DecoderWaitBuffering( decoder_t *p_dec )
522 decoder_owner_sys_t *p_owner = p_dec->p_owner;
524 vlc_mutex_lock( &p_owner->lock );
526 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
528 block_FifoWake( p_owner->p_fifo );
529 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
532 vlc_mutex_unlock( &p_owner->lock );
535 /*****************************************************************************
537 *****************************************************************************/
540 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
542 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
543 "VLC probably does not support this sound or video format.",
545 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
546 _("VLC does not support the audio or video format \"%4.4s\". "
547 "Unfortunately there is no way for you to fix this."), (char*)&codec );
552 * Create a decoder object
554 * \param p_input the input thread
555 * \param p_es the es descriptor
556 * \param i_object_type Object type as define in include/vlc_objects.h
557 * \return the decoder object
559 static decoder_t * CreateDecoder( input_thread_t *p_input,
560 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
563 decoder_owner_sys_t *p_owner;
566 p_dec = vlc_object_create( p_input, i_object_type );
570 p_dec->pf_decode_audio = NULL;
571 p_dec->pf_decode_video = NULL;
572 p_dec->pf_decode_sub = NULL;
573 p_dec->pf_get_cc = NULL;
574 p_dec->pf_packetize = NULL;
576 /* Initialize the decoder fifo */
577 p_dec->p_module = NULL;
579 memset( &null_es_format, 0, sizeof(es_format_t) );
580 es_format_Copy( &p_dec->fmt_in, fmt );
581 es_format_Copy( &p_dec->fmt_out, &null_es_format );
583 /* Allocate our private structure for the decoder */
584 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
585 if( p_dec->p_owner == NULL )
587 vlc_object_release( p_dec );
590 p_dec->p_owner->i_preroll_end = -1;
591 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
592 p_dec->p_owner->p_input = p_input;
593 p_dec->p_owner->p_aout = NULL;
594 p_dec->p_owner->p_aout_input = NULL;
595 p_dec->p_owner->p_vout = NULL;
596 p_dec->p_owner->p_spu_vout = NULL;
597 p_dec->p_owner->i_spu_channel = 0;
598 p_dec->p_owner->i_spu_order = 0;
599 p_dec->p_owner->p_sout = p_sout;
600 p_dec->p_owner->p_sout_input = NULL;
601 p_dec->p_owner->p_packetizer = NULL;
604 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
606 free( p_dec->p_owner );
607 vlc_object_release( p_dec );
611 /* Set buffers allocation callbacks for the decoders */
612 p_dec->pf_aout_buffer_new = aout_new_buffer;
613 p_dec->pf_aout_buffer_del = aout_del_buffer;
614 p_dec->pf_vout_buffer_new = vout_new_buffer;
615 p_dec->pf_vout_buffer_del = vout_del_buffer;
616 p_dec->pf_picture_link = vout_link_picture;
617 p_dec->pf_picture_unlink = vout_unlink_picture;
618 p_dec->pf_spu_buffer_new = spu_new_buffer;
619 p_dec->pf_spu_buffer_del = spu_del_buffer;
621 vlc_object_attach( p_dec, p_input );
623 /* Find a suitable decoder/packetizer module */
624 if( i_object_type == VLC_OBJECT_DECODER )
625 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
627 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
629 /* Check if decoder requires already packetized data */
630 if( i_object_type == VLC_OBJECT_DECODER &&
631 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
633 p_dec->p_owner->p_packetizer =
634 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
635 if( p_dec->p_owner->p_packetizer )
637 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
640 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
643 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
645 p_dec->p_owner->p_packetizer->p_module =
646 module_need( p_dec->p_owner->p_packetizer,
647 "packetizer", "$packetizer", 0 );
649 if( !p_dec->p_owner->p_packetizer->p_module )
651 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
652 vlc_object_detach( p_dec->p_owner->p_packetizer );
653 vlc_object_release( p_dec->p_owner->p_packetizer );
658 /* Copy ourself the input replay gain */
659 if( fmt->i_cat == AUDIO_ES )
661 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
663 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
665 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
666 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
668 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
670 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
671 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
677 vlc_mutex_init( &p_owner->lock );
678 vlc_cond_init( &p_owner->wait );
680 p_owner->b_paused = false;
681 p_owner->pause.i_date = 0;
683 p_owner->b_buffering = false;
684 p_owner->buffer.b_first = true;
685 p_owner->buffer.b_full = false;
686 p_owner->buffer.i_count = 0;
687 p_owner->buffer.p_picture = NULL;
688 p_owner->buffer.p_subpic = NULL;
689 p_owner->buffer.p_audio = NULL;
691 p_owner->b_flushing = false;
694 p_owner->cc.b_supported = false;
695 if( i_object_type == VLC_OBJECT_DECODER )
697 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
698 p_owner->cc.b_supported = true;
699 if( p_dec->pf_get_cc )
700 p_owner->cc.b_supported = true;
703 for( i = 0; i < 4; i++ )
705 p_owner->cc.pb_present[i] = false;
706 p_owner->cc.pp_decoder[i] = NULL;
708 p_owner->i_ts_delay = 0;
713 * The decoding main loop
715 * \param p_dec the decoder
717 static void *DecoderThread( vlc_object_t *p_this )
719 decoder_t *p_dec = (decoder_t *)p_this;
720 decoder_owner_sys_t *p_owner = p_dec->p_owner;
722 int canc = vlc_savecancel();
724 /* The decoder's main loop */
725 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
727 block_t *p_block = block_FifoGet( p_owner->p_fifo );
729 DecoderSignalBuffering( p_dec, p_block == NULL );
731 if( p_block && DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
735 while( vlc_object_alive( p_dec ) )
737 block_t *p_block = block_FifoGet( p_owner->p_fifo );
739 DecoderSignalBuffering( p_dec, p_block == NULL );
741 /* Trash all received PES packets */
743 block_Release( p_block );
745 DecoderSignalBuffering( p_dec, true );
747 /* We do it here because of the dll loader that wants close() in the
748 * same thread than open()/decode() */
749 module_unneed( p_dec, p_dec->p_module );
750 vlc_restorecancel( canc );
754 static void DecoderFlush( decoder_t *p_dec )
756 decoder_owner_sys_t *p_owner = p_dec->p_owner;
759 vlc_assert_locked( &p_owner->lock );
762 block_FifoEmpty( p_owner->p_fifo );
764 /* Monitor for flush end */
765 p_owner->b_flushing = true;
766 vlc_cond_signal( &p_owner->wait );
768 /* Send a special block */
769 p_null = block_New( p_dec, 128 );
772 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
773 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
774 if( !p_dec->fmt_in.b_packetized )
775 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
776 memset( p_null->p_buffer, 0, p_null->i_buffer );
778 input_DecoderDecode( p_dec, p_null );
781 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
782 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
785 static void DecoderSignalFlushed( decoder_t *p_dec )
787 decoder_owner_sys_t *p_owner = p_dec->p_owner;
789 vlc_mutex_lock( &p_owner->lock );
791 if( p_owner->b_flushing )
793 p_owner->b_flushing = false;
794 vlc_cond_signal( &p_owner->wait );
797 vlc_mutex_unlock( &p_owner->lock );
800 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
802 decoder_owner_sys_t *p_owner = p_dec->p_owner;
804 vlc_mutex_lock( &p_owner->lock );
806 if( p_owner->b_buffering )
809 p_owner->buffer.b_full = true;
810 vlc_cond_signal( &p_owner->wait );
813 vlc_mutex_unlock( &p_owner->lock );
816 static bool DecoderIsFlushing( decoder_t *p_dec )
818 decoder_owner_sys_t *p_owner = p_dec->p_owner;
821 vlc_mutex_lock( &p_owner->lock );
823 b_flushing = p_owner->b_flushing;
825 vlc_mutex_unlock( &p_owner->lock );
830 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
832 decoder_owner_sys_t *p_owner = p_dec->p_owner;
834 vlc_assert_locked( &p_owner->lock );
836 while( !p_owner->b_flushing )
838 if( p_owner->b_paused && p_owner->b_buffering && !p_owner->buffer.b_full )
841 if( !p_owner->b_paused && ( !p_owner->b_buffering || !p_owner->buffer.b_full ) )
844 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
848 *pb_reject = p_owner->b_flushing;
851 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
853 decoder_owner_sys_t *p_owner = p_dec->p_owner;
855 vlc_assert_locked( &p_owner->lock );
857 /* XXX only audio and video output have to be paused.
858 * - for sout it is useless
859 * - for subs, it is done by the vout
861 if( p_dec->fmt_in.i_cat == AUDIO_ES )
863 if( p_owner->p_aout && p_owner->p_aout_input )
864 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
867 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
869 if( p_owner->p_vout )
870 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
873 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
875 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
876 *pi_preroll = INT64_MAX;
877 else if( p->i_pts > 0 )
878 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
879 else if( p->i_dts > 0 )
880 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
883 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
885 mtime_t current_date = mdate();
887 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
888 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
890 /* ETSI EN 300 472 Annex A : do not take into account the PTS
891 * for teletext streams. */
892 return current_date + 400000 + i_ts_delay;
897 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
898 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
900 decoder_owner_sys_t *p_owner = p_dec->p_owner;
901 input_clock_t *p_clock = p_owner->p_clock;
904 vlc_assert_locked( &p_owner->lock );
906 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
907 const mtime_t i_es_delay = p_owner->i_ts_delay;
911 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
914 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
915 *pi_ts0 + i_es_delay );
916 if( pi_ts1 && *pi_ts1 > 0 )
917 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
918 *pi_ts1 + i_es_delay );
920 /* Do not create ephemere data because of rounding errors */
921 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
925 i_rate = input_clock_GetRate( p_clock );
928 *pi_duration = ( *pi_duration * i_rate +
929 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
936 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
937 if( pi_ts1 && *pi_ts1 <= 0 )
943 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
944 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
948 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
949 int *pi_played_sum, int *pi_lost_sum )
951 decoder_owner_sys_t *p_owner = p_dec->p_owner;
952 aout_instance_t *p_aout = p_owner->p_aout;
953 aout_input_t *p_aout_input = p_owner->p_aout_input;
956 if( p_audio->start_date <= 0 )
958 msg_Warn( p_dec, "non-dated audio buffer received" );
960 aout_BufferFree( p_audio );
965 vlc_mutex_lock( &p_owner->lock );
967 if( p_owner->b_buffering || p_owner->buffer.p_audio )
969 p_audio->p_next = NULL;
971 *p_owner->buffer.pp_audio_next = p_audio;
972 p_owner->buffer.pp_audio_next = &p_audio->p_next;
974 p_owner->buffer.i_count++;
975 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
976 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
978 p_owner->buffer.b_full = true;
979 vlc_cond_signal( &p_owner->wait );
985 bool b_has_more = false;
987 DecoderWaitUnblock( p_dec, &b_reject );
989 if( p_owner->b_buffering )
991 vlc_mutex_unlock( &p_owner->lock );
996 if( p_owner->buffer.p_audio )
998 p_audio = p_owner->buffer.p_audio;
1000 p_owner->buffer.p_audio = p_audio->p_next;
1001 p_owner->buffer.i_count--;
1003 b_has_more = p_owner->buffer.p_audio != NULL;
1005 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1009 int i_rate = INPUT_RATE_DEFAULT;
1012 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1013 &i_rate, &i_delay, false );
1015 vlc_mutex_unlock( &p_owner->lock );
1018 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1020 if( !p_aout || !p_aout_input ||
1021 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1022 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1023 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1028 /* Wait if we are too early
1029 * FIXME that's plain ugly to do it here */
1030 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1032 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1033 *pi_played_sum += 1;
1034 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1038 if( p_audio->start_date <= 0 )
1040 msg_Warn( p_dec, "non-dated audio buffer received" );
1042 else if( p_audio->start_date > i_max_date )
1044 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1045 p_audio->start_date - mdate() );
1048 aout_BufferFree( p_audio );
1054 vlc_mutex_lock( &p_owner->lock );
1055 if( !p_owner->buffer.p_audio )
1057 vlc_mutex_unlock( &p_owner->lock );
1063 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1065 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1066 input_thread_t *p_input = p_owner->p_input;
1067 aout_buffer_t *p_aout_buf;
1072 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1074 aout_instance_t *p_aout = p_owner->p_aout;
1075 aout_input_t *p_aout_input = p_owner->p_aout_input;
1081 /* It prevent freezing VLC in case of broken decoder */
1082 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1084 block_Release( p_block );
1089 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1091 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1095 if( p_owner->i_preroll_end > 0 )
1097 msg_Dbg( p_dec, "End of audio preroll" );
1098 if( p_owner->p_aout && p_owner->p_aout_input )
1099 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1101 p_owner->i_preroll_end = -1;
1104 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1107 /* Update ugly stat */
1108 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1110 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1112 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1114 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1116 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1119 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1122 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1124 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1130 assert( p_dec_cc->pf_get_cc != NULL );
1132 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1133 if( !p_owner->cc.b_supported )
1136 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1140 vlc_mutex_lock( &p_owner->lock );
1141 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1143 p_owner->cc.pb_present[i] |= pb_present[i];
1144 if( p_owner->cc.pp_decoder[i] )
1148 for( i = 0; i < 4; i++ )
1150 if( !p_owner->cc.pp_decoder[i] )
1153 if( i_cc_decoder > 1 )
1154 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1156 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1159 vlc_mutex_unlock( &p_owner->lock );
1162 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1163 int *pi_played_sum, int *pi_lost_sum )
1165 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1166 vout_thread_t *p_vout = p_owner->p_vout;
1168 if( p_picture->date <= 0 )
1170 msg_Warn( p_vout, "non-dated video buffer received" );
1172 vout_DropPicture( p_vout, p_picture );
1177 vlc_mutex_lock( &p_owner->lock );
1179 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1181 p_picture->p_next = NULL;
1183 *p_owner->buffer.pp_picture_next = p_picture;
1184 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1186 p_owner->buffer.i_count++;
1187 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1188 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1190 p_owner->buffer.b_full = true;
1191 vlc_cond_signal( &p_owner->wait );
1197 bool b_has_more = false;
1200 DecoderWaitUnblock( p_dec, &b_reject );
1202 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1204 vlc_mutex_unlock( &p_owner->lock );
1207 bool b_buffering_first = p_owner->b_buffering;
1210 if( p_owner->buffer.p_picture )
1212 p_picture = p_owner->buffer.p_picture;
1214 p_owner->buffer.p_picture = p_picture->p_next;
1215 p_owner->buffer.i_count--;
1217 b_has_more = p_owner->buffer.p_picture != NULL;
1219 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1223 int i_rate = INPUT_RATE_DEFAULT;
1226 if( b_buffering_first )
1228 assert( p_owner->buffer.b_first );
1229 assert( !p_owner->buffer.i_count );
1230 msg_Dbg( p_dec, "Received first picture" );
1231 p_owner->buffer.b_first = false;
1232 p_picture->date = mdate();
1233 p_picture->b_force = true;
1235 if( p_owner->p_clock )
1236 i_rate = input_clock_GetRate( p_owner->p_clock );
1240 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1241 &i_rate, &i_delay, false );
1244 vlc_mutex_unlock( &p_owner->lock );
1247 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1249 if( p_picture->date <= 0 || p_picture->date >= i_max_date )
1254 if( i_rate != p_owner->i_last_rate )
1256 /* Be sure to not display old picture after our own */
1257 vout_Flush( p_vout, p_picture->date );
1258 p_owner->i_last_rate = i_rate;
1261 vout_DisplayPicture( p_vout, p_picture );
1265 if( p_picture->date <= 0 )
1267 msg_Warn( p_vout, "non-dated video buffer received" );
1271 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1272 p_picture->date - mdate() );
1275 vout_DropPicture( p_vout, p_picture );
1279 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1281 *pi_played_sum += i_tmp_display;
1282 *pi_lost_sum += i_tmp_lost;
1284 if( !b_has_more || b_buffering_first )
1287 vlc_mutex_lock( &p_owner->lock );
1288 if( !p_owner->buffer.p_picture )
1290 vlc_mutex_unlock( &p_owner->lock );
1296 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1298 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1299 input_thread_t *p_input = p_owner->p_input;
1303 int i_displayed = 0;
1305 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1307 vout_thread_t *p_vout = p_owner->p_vout;
1310 /* It prevent freezing VLC in case of broken decoder */
1311 vout_DropPicture( p_vout, p_pic );
1313 block_Release( p_block );
1319 if( p_pic->date < p_owner->i_preroll_end )
1321 vout_DropPicture( p_vout, p_pic );
1325 if( p_owner->i_preroll_end > 0 )
1327 msg_Dbg( p_dec, "End of video preroll" );
1329 vout_Flush( p_vout, 1 );
1331 p_owner->i_preroll_end = -1;
1334 if( p_dec->pf_get_cc &&
1335 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1336 DecoderGetCc( p_dec, p_dec );
1338 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1340 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1342 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1344 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1346 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1349 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1352 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1356 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1359 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1360 vout_thread_t *p_vout = p_owner->p_spu_vout;
1363 if( p_subpic->i_start <= 0 )
1365 msg_Warn( p_dec, "non-dated spu buffer received" );
1366 subpicture_Delete( p_subpic );
1371 vlc_mutex_lock( &p_owner->lock );
1373 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1375 p_subpic->p_next = NULL;
1377 *p_owner->buffer.pp_subpic_next = p_subpic;
1378 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1380 p_owner->buffer.i_count++;
1381 /* XXX it is important to be full after the first one */
1382 if( p_owner->buffer.i_count > 0 )
1384 p_owner->buffer.b_full = true;
1385 vlc_cond_signal( &p_owner->wait );
1391 bool b_has_more = false;
1393 DecoderWaitUnblock( p_dec, &b_reject );
1395 if( p_owner->b_buffering )
1397 vlc_mutex_unlock( &p_owner->lock );
1402 if( p_owner->buffer.p_subpic )
1404 p_subpic = p_owner->buffer.p_subpic;
1406 p_owner->buffer.p_subpic = p_subpic->p_next;
1407 p_owner->buffer.i_count--;
1409 b_has_more = p_owner->buffer.p_subpic != NULL;
1411 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1415 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1416 NULL, NULL, b_telx );
1418 vlc_mutex_unlock( &p_owner->lock );
1421 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1423 subpicture_Delete( p_subpic );
1427 vlc_mutex_lock( &p_owner->lock );
1428 if( !p_owner->buffer.p_subpic )
1430 vlc_mutex_unlock( &p_owner->lock );
1436 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1439 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1441 assert( p_owner->p_clock );
1443 vlc_mutex_lock( &p_owner->lock );
1446 DecoderWaitUnblock( p_dec, &b_reject );
1448 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1449 &p_sout_block->i_rate, NULL, b_telx );
1451 vlc_mutex_unlock( &p_owner->lock );
1453 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1457 static void DecoderFlushBuffering( decoder_t *p_dec )
1459 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1461 vlc_assert_locked( &p_owner->lock );
1463 while( p_owner->buffer.p_picture )
1465 picture_t *p_picture = p_owner->buffer.p_picture;
1467 p_owner->buffer.p_picture = p_picture->p_next;
1468 p_owner->buffer.i_count--;
1470 if( p_owner->p_vout )
1471 vout_DropPicture( p_owner->p_vout, p_picture );
1473 if( !p_owner->buffer.p_picture )
1474 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1476 while( p_owner->buffer.p_audio )
1478 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1480 p_owner->buffer.p_audio = p_audio->p_next;
1481 p_owner->buffer.i_count--;
1483 aout_BufferFree( p_audio );
1485 if( !p_owner->buffer.p_audio )
1486 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1488 while( p_owner->buffer.p_subpic )
1490 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1492 p_owner->buffer.p_subpic = p_subpic->p_next;
1493 p_owner->buffer.i_count--;
1495 subpicture_Delete( p_subpic );
1497 if( !p_owner->buffer.p_subpic )
1498 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1502 /* This function process a block for sout
1504 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1506 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1507 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1508 block_t *p_sout_block;
1510 while( ( p_sout_block =
1511 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1513 if( !p_owner->p_sout_input )
1515 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1517 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1518 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1519 if( p_dec->fmt_in.psz_language )
1521 if( p_owner->sout.psz_language )
1522 free( p_owner->sout.psz_language );
1523 p_owner->sout.psz_language =
1524 strdup( p_dec->fmt_in.psz_language );
1527 p_owner->p_sout_input =
1528 sout_InputNew( p_owner->p_sout,
1531 if( p_owner->p_sout_input == NULL )
1533 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1534 (char *)&p_owner->sout.i_codec );
1535 p_dec->b_error = true;
1537 while( p_sout_block )
1539 block_t *p_next = p_sout_block->p_next;
1540 block_Release( p_sout_block );
1541 p_sout_block = p_next;
1547 while( p_sout_block )
1549 block_t *p_next = p_sout_block->p_next;
1551 p_sout_block->p_next = NULL;
1553 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1555 p_sout_block = p_next;
1558 /* For now it's enough, as only sout impact on this flag */
1559 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1560 p_owner->p_input->p->b_out_pace_control )
1562 msg_Dbg( p_dec, "switching to sync mode" );
1563 p_owner->p_input->p->b_out_pace_control = false;
1565 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1566 !p_owner->p_input->p->b_out_pace_control )
1568 msg_Dbg( p_dec, "switching to async mode" );
1569 p_owner->p_input->p->b_out_pace_control = true;
1574 /* This function process a video block
1576 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1578 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1580 if( p_owner->p_packetizer )
1582 block_t *p_packetized_block;
1583 decoder_t *p_packetizer = p_owner->p_packetizer;
1585 while( (p_packetized_block =
1586 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1588 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1590 es_format_Clean( &p_dec->fmt_in );
1591 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1593 if( p_packetizer->pf_get_cc )
1594 DecoderGetCc( p_dec, p_packetizer );
1596 while( p_packetized_block )
1598 block_t *p_next = p_packetized_block->p_next;
1599 p_packetized_block->p_next = NULL;
1601 DecoderDecodeVideo( p_dec, p_packetized_block );
1603 p_packetized_block = p_next;
1609 DecoderDecodeVideo( p_dec, p_block );
1612 if( b_flush && p_owner->p_vout )
1613 vout_Flush( p_owner->p_vout, 1 );
1616 /* This function process a audio block
1618 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1620 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1622 if( p_owner->p_packetizer )
1624 block_t *p_packetized_block;
1625 decoder_t *p_packetizer = p_owner->p_packetizer;
1627 while( (p_packetized_block =
1628 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1630 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1632 es_format_Clean( &p_dec->fmt_in );
1633 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1636 while( p_packetized_block )
1638 block_t *p_next = p_packetized_block->p_next;
1639 p_packetized_block->p_next = NULL;
1641 DecoderDecodeAudio( p_dec, p_packetized_block );
1643 p_packetized_block = p_next;
1649 DecoderDecodeAudio( p_dec, p_block );
1652 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1653 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1656 /* This function process a subtitle block
1658 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1660 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1661 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1663 input_thread_t *p_input = p_owner->p_input;
1664 vout_thread_t *p_vout;
1665 subpicture_t *p_spu;
1667 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1669 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1670 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1671 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1673 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1674 if( p_vout && p_owner->p_spu_vout == p_vout )
1676 /* Preroll does not work very well with subtitle */
1677 if( p_spu->i_start > 0 &&
1678 p_spu->i_start < p_owner->i_preroll_end &&
1679 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1681 subpicture_Delete( p_spu );
1685 DecoderPlaySpu( p_dec, p_spu, b_telx );
1690 subpicture_Delete( p_spu );
1693 vlc_object_release( p_vout );
1696 if( b_flush && p_owner->p_spu_vout )
1698 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1700 if( p_vout && p_owner->p_spu_vout == p_vout )
1701 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1702 p_owner->i_spu_channel );
1705 vlc_object_release( p_vout );
1712 * \param p_dec the decoder object
1713 * \param p_block the block to decode
1714 * \return VLC_SUCCESS or an error code
1716 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1718 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1719 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1721 if( p_block && p_block->i_buffer <= 0 )
1723 assert( !b_flush_request );
1724 block_Release( p_block );
1729 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1732 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1734 DecoderProcessSout( p_dec, p_block );
1739 bool b_flush = false;
1743 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1744 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1746 b_flush = !b_flushing && b_flush_request;
1748 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1751 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1753 DecoderProcessAudio( p_dec, p_block, b_flush );
1755 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1757 DecoderProcessVideo( p_dec, p_block, b_flush );
1759 else if( p_dec->fmt_in.i_cat == SPU_ES )
1761 DecoderProcessSpu( p_dec, p_block, b_flush );
1765 msg_Err( p_dec, "unknown ES format" );
1766 p_dec->b_error = true;
1771 if( b_flush_request )
1773 vlc_mutex_lock( &p_owner->lock );
1774 DecoderFlushBuffering( p_dec );
1775 vlc_mutex_unlock( &p_owner->lock );
1777 DecoderSignalFlushed( p_dec );
1780 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1784 * Destroys a decoder object
1786 * \param p_dec the decoder object
1789 static void DeleteDecoder( decoder_t * p_dec )
1791 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1793 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1794 (char*)&p_dec->fmt_in.i_codec,
1795 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1797 /* Free all packets still in the decoder fifo. */
1798 block_FifoEmpty( p_owner->p_fifo );
1799 block_FifoRelease( p_owner->p_fifo );
1802 vlc_mutex_lock( &p_owner->lock );
1803 DecoderFlushBuffering( p_dec );
1804 vlc_mutex_unlock( &p_owner->lock );
1807 if( p_owner->p_aout_input )
1808 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1809 if( p_owner->p_aout )
1811 vlc_object_release( p_owner->p_aout );
1812 p_owner->p_aout = NULL;
1814 if( p_owner->p_vout )
1816 /* Hack to make sure all the the pictures are freed by the decoder */
1817 vout_FixLeaks( p_owner->p_vout, true );
1819 /* We are about to die. Reattach video output to p_vlc. */
1820 vout_Request( p_dec, p_owner->p_vout, NULL );
1821 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1825 if( p_owner->p_sout_input )
1827 sout_InputDelete( p_owner->p_sout_input );
1828 es_format_Clean( &p_owner->sout );
1832 if( p_dec->fmt_in.i_cat == SPU_ES )
1834 vout_thread_t *p_vout;
1836 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1837 if( p_vout && p_owner->p_spu_vout == p_vout )
1839 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1840 p_owner->i_spu_channel );
1841 vlc_object_release( p_vout );
1845 es_format_Clean( &p_dec->fmt_in );
1846 es_format_Clean( &p_dec->fmt_out );
1848 if( p_owner->p_packetizer )
1850 module_unneed( p_owner->p_packetizer,
1851 p_owner->p_packetizer->p_module );
1852 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1853 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1854 vlc_object_detach( p_owner->p_packetizer );
1855 vlc_object_release( p_owner->p_packetizer );
1858 vlc_cond_destroy( &p_owner->wait );
1859 vlc_mutex_destroy( &p_owner->lock );
1861 vlc_object_detach( p_dec );
1866 /*****************************************************************************
1867 * Buffers allocation callbacks for the decoders
1868 *****************************************************************************/
1869 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1871 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1872 aout_buffer_t *p_buffer;
1874 if( p_owner->p_aout_input != NULL &&
1875 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1876 p_dec->fmt_out.audio.i_original_channels !=
1877 p_owner->audio.i_original_channels ||
1878 p_dec->fmt_out.audio.i_bytes_per_frame !=
1879 p_owner->audio.i_bytes_per_frame ) )
1881 aout_input_t *p_aout_input = p_owner->p_aout_input;
1883 /* Parameters changed, restart the aout */
1884 vlc_mutex_lock( &p_owner->lock );
1886 DecoderFlushBuffering( p_dec );
1888 p_owner->p_aout_input = NULL;
1889 aout_DecDelete( p_owner->p_aout, p_aout_input );
1891 vlc_mutex_unlock( &p_owner->lock );
1894 if( p_owner->p_aout_input == NULL )
1896 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1897 audio_sample_format_t format;
1898 aout_input_t *p_aout_input;
1899 aout_instance_t *p_aout;
1901 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1902 p_owner->audio = p_dec->fmt_out.audio;
1904 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1905 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1906 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1908 if ( i_force_dolby == 1 )
1910 format.i_original_channels = format.i_original_channels |
1911 AOUT_CHAN_DOLBYSTEREO;
1913 else /* i_force_dolby == 2 */
1915 format.i_original_channels = format.i_original_channels &
1916 ~AOUT_CHAN_DOLBYSTEREO;
1920 p_aout = p_owner->p_aout;
1921 p_aout_input = aout_DecNew( p_dec, &p_aout,
1922 &format, &p_dec->fmt_out.audio_replay_gain );
1924 vlc_mutex_lock( &p_owner->lock );
1925 p_owner->p_aout = p_aout;
1926 p_owner->p_aout_input = p_aout_input;
1927 vlc_mutex_unlock( &p_owner->lock );
1929 if( p_owner->p_aout_input == NULL )
1931 msg_Err( p_dec, "failed to create audio output" );
1932 p_dec->b_error = true;
1935 p_dec->fmt_out.audio.i_bytes_per_frame =
1936 p_owner->audio.i_bytes_per_frame;
1939 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1944 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1946 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1948 aout_DecDeleteBuffer( p_owner->p_aout,
1949 p_owner->p_aout_input, p_buffer );
1953 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1955 static picture_t *vout_new_buffer( decoder_t *p_dec )
1957 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1959 if( p_owner->p_vout == NULL ||
1960 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1961 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1962 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1963 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1965 vout_thread_t *p_vout;
1967 if( !p_dec->fmt_out.video.i_width ||
1968 !p_dec->fmt_out.video.i_height )
1970 /* Can't create a new vout without display size */
1974 if( !p_dec->fmt_out.video.i_visible_width ||
1975 !p_dec->fmt_out.video.i_visible_height )
1977 if( p_dec->fmt_in.video.i_visible_width &&
1978 p_dec->fmt_in.video.i_visible_height )
1980 p_dec->fmt_out.video.i_visible_width =
1981 p_dec->fmt_in.video.i_visible_width;
1982 p_dec->fmt_out.video.i_visible_height =
1983 p_dec->fmt_in.video.i_visible_height;
1987 p_dec->fmt_out.video.i_visible_width =
1988 p_dec->fmt_out.video.i_width;
1989 p_dec->fmt_out.video.i_visible_height =
1990 p_dec->fmt_out.video.i_height;
1994 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1995 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1997 p_dec->fmt_out.video.i_visible_height = 1080;
1998 p_dec->fmt_out.video.i_sar_num *= 135;
1999 p_dec->fmt_out.video.i_sar_den *= 136;
2000 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2003 if( !p_dec->fmt_out.video.i_sar_num ||
2004 !p_dec->fmt_out.video.i_sar_den )
2006 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2007 p_dec->fmt_out.video.i_visible_height;
2009 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2010 p_dec->fmt_out.video.i_visible_width;
2013 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2014 &p_dec->fmt_out.video.i_sar_den,
2015 p_dec->fmt_out.video.i_sar_num,
2016 p_dec->fmt_out.video.i_sar_den, 50000 );
2018 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2019 p_owner->video = p_dec->fmt_out.video;
2021 vlc_mutex_lock( &p_owner->lock );
2023 DecoderFlushBuffering( p_dec );
2025 p_vout = p_owner->p_vout;
2026 p_owner->p_vout = NULL;
2027 vlc_mutex_unlock( &p_owner->lock );
2029 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2031 vlc_mutex_lock( &p_owner->lock );
2032 p_owner->p_vout = p_vout;
2033 vlc_mutex_unlock( &p_owner->lock );
2035 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2036 if( p_vout == NULL )
2038 msg_Err( p_dec, "failed to create video output" );
2039 p_dec->b_error = true;
2043 if( p_owner->video.i_rmask )
2044 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2045 if( p_owner->video.i_gmask )
2046 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2047 if( p_owner->video.i_bmask )
2048 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2051 /* Get a new picture
2055 picture_t *p_picture;
2057 if( p_dec->b_die || p_dec->b_error )
2060 /* The video filter chain required that there is always 1 free buffer
2061 * that it will use as temporary one. It will release the temporary
2062 * buffer once its work is done, so this check is safe even if we don't
2063 * lock around both count() and create().
2065 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2067 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2072 if( DecoderIsFlushing( p_dec ) )
2076 DecoderSignalBuffering( p_dec, true );
2078 /* Check the decoder doesn't leak pictures */
2079 vout_FixLeaks( p_owner->p_vout, false );
2081 msleep( VOUT_OUTMEM_SLEEP );
2085 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2087 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2090 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2092 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2095 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2097 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2100 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2102 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2103 vout_thread_t *p_vout = NULL;
2104 subpicture_t *p_subpic;
2105 int i_attempts = 30;
2107 while( i_attempts-- )
2109 if( p_dec->b_die || p_dec->b_error )
2112 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2116 msleep( VOUT_DISPLAY_DELAY );
2121 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2125 if( p_owner->p_spu_vout != p_vout )
2127 vlc_mutex_lock( &p_owner->lock );
2129 DecoderFlushBuffering( p_dec );
2131 vlc_mutex_unlock( &p_owner->lock );
2133 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2134 &p_owner->i_spu_channel );
2135 p_owner->i_spu_order = 0;
2136 p_owner->p_spu_vout = p_vout;
2139 p_subpic = subpicture_New();
2142 p_subpic->i_channel = p_owner->i_spu_channel;
2143 p_subpic->i_order = p_owner->i_spu_order++;
2144 p_subpic->b_subtitle = true;
2147 vlc_object_release( p_vout );
2152 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2154 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2155 vout_thread_t *p_vout = NULL;
2157 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2158 if( !p_vout || p_owner->p_spu_vout != p_vout )
2161 vlc_object_release( p_vout );
2162 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2166 subpicture_Delete( p_subpic );
2168 vlc_object_release( p_vout );