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 (void)b_force_decoder;
226 /* If we are in sout mode, search for packetizer module */
229 /* Create the decoder configuration structure */
230 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
233 msg_Err( p_input, "could not create packetizer" );
234 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
235 _("VLC could not open the packetizer module.") );
242 /* Create the decoder configuration structure */
243 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
246 msg_Err( p_input, "could not create decoder" );
247 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
248 _("VLC could not open the decoder module.") );
253 if( !p_dec->p_module )
255 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
257 DeleteDecoder( p_dec );
258 vlc_object_release( p_dec );
262 p_dec->p_owner->p_clock = p_clock;
264 if( fmt->i_cat == AUDIO_ES )
265 i_priority = VLC_THREAD_PRIORITY_AUDIO;
267 i_priority = VLC_THREAD_PRIORITY_VIDEO;
269 /* Spawn the decoder thread */
270 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
271 i_priority, false ) )
273 msg_Err( p_dec, "cannot spawn decoder thread" );
274 module_unneed( p_dec, p_dec->p_module );
275 DeleteDecoder( p_dec );
276 vlc_object_release( p_dec );
284 * Kills a decoder thread and waits until it's finished
286 * \param p_input the input thread
287 * \param p_es the es descriptor
290 void input_DecoderDelete( decoder_t *p_dec )
292 decoder_owner_sys_t *p_owner = p_dec->p_owner;
294 vlc_object_kill( p_dec );
296 /* Make sure we aren't paused anymore */
297 vlc_mutex_lock( &p_owner->lock );
298 if( p_owner->b_paused || p_owner->b_buffering )
300 p_owner->b_paused = false;
301 p_owner->b_buffering = false;
302 vlc_cond_signal( &p_owner->wait );
304 vlc_mutex_unlock( &p_owner->lock );
306 /* Make sure the thread leaves the function */
307 block_FifoWake( p_owner->p_fifo );
309 vlc_thread_join( p_dec );
311 /* Don't module_unneed() here because of the dll loader that wants
312 * close() in the same thread than open()/decode() */
315 if( p_dec->p_owner->cc.b_supported )
318 for( i = 0; i < 4; i++ )
319 input_DecoderSetCcState( p_dec, false, i );
322 /* Delete decoder configuration */
323 DeleteDecoder( p_dec );
325 /* Delete the decoder */
326 vlc_object_release( p_dec );
330 * Put a block_t in the decoder's fifo.
332 * \param p_dec the decoder object
333 * \param p_block the data block
335 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
337 decoder_owner_sys_t *p_owner = p_dec->p_owner;
339 if( p_owner->p_input->p->b_out_pace_control )
342 while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
343 block_FifoCount( p_owner->p_fifo ) > 10 )
348 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
350 /* FIXME: ideally we would check the time amount of data
351 * in the fifo instead of its size. */
352 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
353 "consumed quickly enough), resetting fifo!" );
354 block_FifoEmpty( p_owner->p_fifo );
357 block_FifoPut( p_owner->p_fifo, p_block );
360 bool input_DecoderIsEmpty( decoder_t * p_dec )
362 assert( !p_dec->p_owner->b_buffering );
364 /* FIXME that's not really true */
365 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
368 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
370 decoder_owner_sys_t *p_owner = p_dec->p_owner;
373 vlc_mutex_lock( &p_owner->lock );
374 for( i = 0; i < 4; i++ )
375 pb_present[i] = p_owner->cc.pb_present[i];
376 vlc_mutex_unlock( &p_owner->lock );
378 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
380 decoder_owner_sys_t *p_owner = p_dec->p_owner;
382 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
384 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
389 static const vlc_fourcc_t fcc[4] = {
390 VLC_FOURCC('c', 'c', '1', ' '),
391 VLC_FOURCC('c', 'c', '2', ' '),
392 VLC_FOURCC('c', 'c', '3', ' '),
393 VLC_FOURCC('c', 'c', '4', ' '),
398 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
399 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
402 msg_Err( p_dec, "could not create decoder" );
403 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
404 _("VLC could not open the decoder module.") );
407 else if( !p_cc->p_module )
409 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
410 DeleteDecoder( p_cc );
411 vlc_object_release( p_cc );
414 p_cc->p_owner->p_clock = p_owner->p_clock;
416 vlc_mutex_lock( &p_owner->lock );
417 p_owner->cc.pp_decoder[i_channel] = p_cc;
418 vlc_mutex_unlock( &p_owner->lock );
424 vlc_mutex_lock( &p_owner->lock );
425 p_cc = p_owner->cc.pp_decoder[i_channel];
426 p_owner->cc.pp_decoder[i_channel] = NULL;
427 vlc_mutex_unlock( &p_owner->lock );
431 vlc_object_kill( p_cc );
432 module_unneed( p_cc, p_cc->p_module );
433 DeleteDecoder( p_cc );
434 vlc_object_release( p_cc );
439 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
441 decoder_owner_sys_t *p_owner = p_dec->p_owner;
444 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
447 vlc_mutex_lock( &p_owner->lock );
448 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
449 vlc_mutex_unlock( &p_owner->lock );
453 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
455 decoder_owner_sys_t *p_owner = p_dec->p_owner;
457 vlc_mutex_lock( &p_owner->lock );
459 assert( !p_owner->b_paused || !b_paused );
461 p_owner->b_paused = b_paused;
462 p_owner->pause.i_date = i_date;
463 vlc_cond_signal( &p_owner->wait );
465 DecoderOutputChangePause( p_dec, b_paused, i_date );
467 vlc_mutex_unlock( &p_owner->lock );
470 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
472 decoder_owner_sys_t *p_owner = p_dec->p_owner;
474 vlc_mutex_lock( &p_owner->lock );
475 p_owner->i_ts_delay = i_delay;
476 vlc_mutex_unlock( &p_owner->lock );
479 void input_DecoderStartBuffering( decoder_t *p_dec )
481 decoder_owner_sys_t *p_owner = p_dec->p_owner;
483 vlc_mutex_lock( &p_owner->lock );
485 DecoderFlush( p_dec );
487 p_owner->buffer.b_first = true;
488 p_owner->buffer.b_full = false;
489 p_owner->buffer.i_count = 0;
491 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
493 p_owner->buffer.p_picture = NULL;
494 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
496 p_owner->buffer.p_subpic = NULL;
497 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
499 p_owner->buffer.p_audio = NULL;
500 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
502 p_owner->b_buffering = true;
504 vlc_cond_signal( &p_owner->wait );
506 vlc_mutex_unlock( &p_owner->lock );
509 void input_DecoderStopBuffering( decoder_t *p_dec )
511 decoder_owner_sys_t *p_owner = p_dec->p_owner;
513 vlc_mutex_lock( &p_owner->lock );
515 p_owner->b_buffering = false;
517 vlc_cond_signal( &p_owner->wait );
519 vlc_mutex_unlock( &p_owner->lock );
522 void input_DecoderWaitBuffering( decoder_t *p_dec )
524 decoder_owner_sys_t *p_owner = p_dec->p_owner;
526 vlc_mutex_lock( &p_owner->lock );
528 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
530 block_FifoWake( p_owner->p_fifo );
531 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
534 vlc_mutex_unlock( &p_owner->lock );
537 /*****************************************************************************
539 *****************************************************************************/
542 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
544 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
545 "VLC probably does not support this sound or video format.",
547 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
548 _("VLC does not support the audio or video format \"%4.4s\". "
549 "Unfortunately there is no way for you to fix this."), (char*)&codec );
554 * Create a decoder object
556 * \param p_input the input thread
557 * \param p_es the es descriptor
558 * \param i_object_type Object type as define in include/vlc_objects.h
559 * \return the decoder object
561 static decoder_t * CreateDecoder( input_thread_t *p_input,
562 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
565 decoder_owner_sys_t *p_owner;
568 p_dec = vlc_object_create( p_input, i_object_type );
572 p_dec->pf_decode_audio = NULL;
573 p_dec->pf_decode_video = NULL;
574 p_dec->pf_decode_sub = NULL;
575 p_dec->pf_get_cc = NULL;
576 p_dec->pf_packetize = NULL;
578 /* Initialize the decoder fifo */
579 p_dec->p_module = NULL;
581 memset( &null_es_format, 0, sizeof(es_format_t) );
582 es_format_Copy( &p_dec->fmt_in, fmt );
583 es_format_Copy( &p_dec->fmt_out, &null_es_format );
585 /* Allocate our private structure for the decoder */
586 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
587 if( p_dec->p_owner == NULL )
589 vlc_object_release( p_dec );
592 p_dec->p_owner->i_preroll_end = -1;
593 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
594 p_dec->p_owner->p_input = p_input;
595 p_dec->p_owner->p_aout = NULL;
596 p_dec->p_owner->p_aout_input = NULL;
597 p_dec->p_owner->p_vout = NULL;
598 p_dec->p_owner->p_spu_vout = NULL;
599 p_dec->p_owner->i_spu_channel = 0;
600 p_dec->p_owner->i_spu_order = 0;
601 p_dec->p_owner->p_sout = p_sout;
602 p_dec->p_owner->p_sout_input = NULL;
603 p_dec->p_owner->p_packetizer = NULL;
606 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
608 free( p_dec->p_owner );
609 vlc_object_release( p_dec );
613 /* Set buffers allocation callbacks for the decoders */
614 p_dec->pf_aout_buffer_new = aout_new_buffer;
615 p_dec->pf_aout_buffer_del = aout_del_buffer;
616 p_dec->pf_vout_buffer_new = vout_new_buffer;
617 p_dec->pf_vout_buffer_del = vout_del_buffer;
618 p_dec->pf_picture_link = vout_link_picture;
619 p_dec->pf_picture_unlink = vout_unlink_picture;
620 p_dec->pf_spu_buffer_new = spu_new_buffer;
621 p_dec->pf_spu_buffer_del = spu_del_buffer;
623 vlc_object_attach( p_dec, p_input );
625 /* Find a suitable decoder/packetizer module */
626 if( i_object_type == VLC_OBJECT_DECODER )
627 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
629 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
631 /* Check if decoder requires already packetized data */
632 if( i_object_type == VLC_OBJECT_DECODER &&
633 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
635 p_dec->p_owner->p_packetizer =
636 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
637 if( p_dec->p_owner->p_packetizer )
639 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
642 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
645 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
647 p_dec->p_owner->p_packetizer->p_module =
648 module_need( p_dec->p_owner->p_packetizer,
649 "packetizer", "$packetizer", 0 );
651 if( !p_dec->p_owner->p_packetizer->p_module )
653 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
654 vlc_object_detach( p_dec->p_owner->p_packetizer );
655 vlc_object_release( p_dec->p_owner->p_packetizer );
660 /* Copy ourself the input replay gain */
661 if( fmt->i_cat == AUDIO_ES )
663 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
665 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
667 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
668 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
670 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
672 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
673 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
679 vlc_mutex_init( &p_owner->lock );
680 vlc_cond_init( &p_owner->wait );
682 p_owner->b_paused = false;
683 p_owner->pause.i_date = 0;
685 p_owner->b_buffering = false;
686 p_owner->buffer.b_first = true;
687 p_owner->buffer.b_full = false;
688 p_owner->buffer.i_count = 0;
689 p_owner->buffer.p_picture = NULL;
690 p_owner->buffer.p_subpic = NULL;
691 p_owner->buffer.p_audio = NULL;
693 p_owner->b_flushing = false;
696 p_owner->cc.b_supported = false;
697 if( i_object_type == VLC_OBJECT_DECODER )
699 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
700 p_owner->cc.b_supported = true;
701 if( p_dec->pf_get_cc )
702 p_owner->cc.b_supported = true;
705 for( i = 0; i < 4; i++ )
707 p_owner->cc.pb_present[i] = false;
708 p_owner->cc.pp_decoder[i] = NULL;
710 p_owner->i_ts_delay = 0;
715 * The decoding main loop
717 * \param p_dec the decoder
719 static void *DecoderThread( vlc_object_t *p_this )
721 decoder_t *p_dec = (decoder_t *)p_this;
722 decoder_owner_sys_t *p_owner = p_dec->p_owner;
724 int canc = vlc_savecancel();
726 /* The decoder's main loop */
727 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
729 block_t *p_block = block_FifoGet( p_owner->p_fifo );
731 DecoderSignalBuffering( p_dec, p_block == NULL );
733 if( p_block && DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
737 while( vlc_object_alive( p_dec ) )
739 block_t *p_block = block_FifoGet( p_owner->p_fifo );
741 DecoderSignalBuffering( p_dec, p_block == NULL );
743 /* Trash all received PES packets */
745 block_Release( p_block );
747 DecoderSignalBuffering( p_dec, true );
749 /* We do it here because of the dll loader that wants close() in the
750 * same thread than open()/decode() */
751 module_unneed( p_dec, p_dec->p_module );
752 vlc_restorecancel( canc );
756 static void DecoderFlush( decoder_t *p_dec )
758 decoder_owner_sys_t *p_owner = p_dec->p_owner;
761 vlc_assert_locked( &p_owner->lock );
764 block_FifoEmpty( p_owner->p_fifo );
766 /* Monitor for flush end */
767 p_owner->b_flushing = true;
768 vlc_cond_signal( &p_owner->wait );
770 /* Send a special block */
771 p_null = block_New( p_dec, 128 );
774 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
775 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
776 if( !p_dec->fmt_in.b_packetized )
777 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
778 memset( p_null->p_buffer, 0, p_null->i_buffer );
780 input_DecoderDecode( p_dec, p_null );
783 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
784 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
787 static void DecoderSignalFlushed( decoder_t *p_dec )
789 decoder_owner_sys_t *p_owner = p_dec->p_owner;
791 vlc_mutex_lock( &p_owner->lock );
793 if( p_owner->b_flushing )
795 p_owner->b_flushing = false;
796 vlc_cond_signal( &p_owner->wait );
799 vlc_mutex_unlock( &p_owner->lock );
802 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
804 decoder_owner_sys_t *p_owner = p_dec->p_owner;
806 vlc_mutex_lock( &p_owner->lock );
808 if( p_owner->b_buffering )
811 p_owner->buffer.b_full = true;
812 vlc_cond_signal( &p_owner->wait );
815 vlc_mutex_unlock( &p_owner->lock );
818 static bool DecoderIsFlushing( decoder_t *p_dec )
820 decoder_owner_sys_t *p_owner = p_dec->p_owner;
823 vlc_mutex_lock( &p_owner->lock );
825 b_flushing = p_owner->b_flushing;
827 vlc_mutex_unlock( &p_owner->lock );
832 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
834 decoder_owner_sys_t *p_owner = p_dec->p_owner;
836 vlc_assert_locked( &p_owner->lock );
838 while( !p_owner->b_flushing )
840 if( p_owner->b_paused && p_owner->b_buffering && !p_owner->buffer.b_full )
843 if( !p_owner->b_paused && ( !p_owner->b_buffering || !p_owner->buffer.b_full ) )
846 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
850 *pb_reject = p_owner->b_flushing;
853 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
855 decoder_owner_sys_t *p_owner = p_dec->p_owner;
857 vlc_assert_locked( &p_owner->lock );
859 /* XXX only audio and video output have to be paused.
860 * - for sout it is useless
861 * - for subs, it is done by the vout
863 if( p_dec->fmt_in.i_cat == AUDIO_ES )
865 if( p_owner->p_aout && p_owner->p_aout_input )
866 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
869 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
871 if( p_owner->p_vout )
872 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
875 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
877 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
878 *pi_preroll = INT64_MAX;
879 else if( p->i_pts > 0 )
880 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
881 else if( p->i_dts > 0 )
882 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
885 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
887 mtime_t current_date = mdate();
889 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
890 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
892 /* ETSI EN 300 472 Annex A : do not take into account the PTS
893 * for teletext streams. */
894 return current_date + 400000 + i_ts_delay;
899 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
900 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
902 decoder_owner_sys_t *p_owner = p_dec->p_owner;
903 input_clock_t *p_clock = p_owner->p_clock;
906 vlc_assert_locked( &p_owner->lock );
908 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
909 const mtime_t i_es_delay = p_owner->i_ts_delay;
913 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
916 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
917 *pi_ts0 + i_es_delay );
918 if( pi_ts1 && *pi_ts1 > 0 )
919 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
920 *pi_ts1 + i_es_delay );
922 /* Do not create ephemere data because of rounding errors */
923 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
927 i_rate = input_clock_GetRate( p_clock );
930 *pi_duration = ( *pi_duration * i_rate +
931 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
938 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
939 if( pi_ts1 && *pi_ts1 <= 0 )
945 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
946 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
950 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
951 int *pi_played_sum, int *pi_lost_sum )
953 decoder_owner_sys_t *p_owner = p_dec->p_owner;
954 aout_instance_t *p_aout = p_owner->p_aout;
955 aout_input_t *p_aout_input = p_owner->p_aout_input;
958 if( p_audio->start_date <= 0 )
960 msg_Warn( p_dec, "non-dated audio buffer received" );
962 aout_BufferFree( p_audio );
967 vlc_mutex_lock( &p_owner->lock );
969 if( p_owner->b_buffering || p_owner->buffer.p_audio )
971 p_audio->p_next = NULL;
973 *p_owner->buffer.pp_audio_next = p_audio;
974 p_owner->buffer.pp_audio_next = &p_audio->p_next;
976 p_owner->buffer.i_count++;
977 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
978 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
980 p_owner->buffer.b_full = true;
981 vlc_cond_signal( &p_owner->wait );
987 bool b_has_more = false;
989 DecoderWaitUnblock( p_dec, &b_reject );
991 if( p_owner->b_buffering )
993 vlc_mutex_unlock( &p_owner->lock );
998 if( p_owner->buffer.p_audio )
1000 p_audio = p_owner->buffer.p_audio;
1002 p_owner->buffer.p_audio = p_audio->p_next;
1004 b_has_more = p_owner->buffer.p_audio != NULL;
1006 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1010 int i_rate = INPUT_RATE_DEFAULT;
1013 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1014 &i_rate, &i_delay, false );
1016 vlc_mutex_unlock( &p_owner->lock );
1019 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1021 if( !p_aout || !p_aout_input ||
1022 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1023 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1024 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1029 /* Wait if we are too early
1030 * FIXME that's plain ugly to do it here */
1031 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1033 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1034 *pi_played_sum += 1;
1035 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1039 if( p_audio->start_date <= 0 )
1041 msg_Warn( p_dec, "non-dated audio buffer received" );
1043 else if( p_audio->start_date > i_max_date )
1045 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1046 p_audio->start_date - mdate() );
1049 aout_BufferFree( p_audio );
1055 vlc_mutex_lock( &p_owner->lock );
1056 if( !p_owner->buffer.p_audio )
1058 vlc_mutex_unlock( &p_owner->lock );
1064 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1066 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1067 input_thread_t *p_input = p_owner->p_input;
1068 aout_buffer_t *p_aout_buf;
1073 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1075 aout_instance_t *p_aout = p_owner->p_aout;
1076 aout_input_t *p_aout_input = p_owner->p_aout_input;
1082 /* It prevent freezing VLC in case of broken decoder */
1083 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1085 block_Release( p_block );
1090 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1092 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1096 if( p_owner->i_preroll_end > 0 )
1098 msg_Dbg( p_dec, "End of audio preroll" );
1099 if( p_owner->p_aout && p_owner->p_aout_input )
1100 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1102 p_owner->i_preroll_end = -1;
1105 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1108 /* Update ugly stat */
1109 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1111 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1113 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1115 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1117 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1120 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1123 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1125 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1131 assert( p_dec_cc->pf_get_cc != NULL );
1133 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1134 if( !p_owner->cc.b_supported )
1137 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1141 vlc_mutex_lock( &p_owner->lock );
1142 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1144 p_owner->cc.pb_present[i] |= pb_present[i];
1145 if( p_owner->cc.pp_decoder[i] )
1149 for( i = 0; i < 4; i++ )
1151 if( !p_owner->cc.pp_decoder[i] )
1154 if( i_cc_decoder > 1 )
1155 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1157 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1160 vlc_mutex_unlock( &p_owner->lock );
1162 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
1164 vlc_mutex_lock( &p_vout->picture_lock );
1166 if( p_pic->i_status == READY_PICTURE )
1168 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
1171 else if( p_pic->i_refcount > 0 )
1173 p_pic->i_status = DISPLAYED_PICTURE;
1177 p_pic->i_status = DESTROYED_PICTURE;
1178 picture_CleanupQuant( p_pic );
1179 p_vout->i_heap_size--;
1182 vlc_mutex_unlock( &p_vout->picture_lock );
1184 static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
1187 vlc_mutex_lock( &p_vout->picture_lock );
1188 for( i = 0; i < p_vout->render.i_pictures; i++ )
1190 picture_t *p_pic = p_vout->render.pp_picture[i];
1192 if( p_pic->i_status == READY_PICTURE ||
1193 p_pic->i_status == DISPLAYED_PICTURE )
1195 /* We cannot change picture status if it is in READY_PICTURE state,
1196 * Just make sure they won't be displayed */
1197 if( p_pic->date > i_max_date )
1198 p_pic->date = i_max_date;
1201 vlc_mutex_unlock( &p_vout->picture_lock );
1204 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1205 int *pi_played_sum, int *pi_lost_sum )
1207 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1208 vout_thread_t *p_vout = p_owner->p_vout;
1210 if( p_picture->date <= 0 )
1212 msg_Warn( p_vout, "non-dated video buffer received" );
1214 VoutDisplayedPicture( p_vout, p_picture );
1219 vlc_mutex_lock( &p_owner->lock );
1221 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1223 p_picture->p_next = NULL;
1225 *p_owner->buffer.pp_picture_next = p_picture;
1226 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1228 p_owner->buffer.i_count++;
1229 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1230 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1232 p_owner->buffer.b_full = true;
1233 vlc_cond_signal( &p_owner->wait );
1239 bool b_has_more = false;
1242 DecoderWaitUnblock( p_dec, &b_reject );
1244 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1246 vlc_mutex_unlock( &p_owner->lock );
1249 bool b_buffering_first = p_owner->b_buffering;
1252 if( p_owner->buffer.p_picture )
1254 p_picture = p_owner->buffer.p_picture;
1256 p_owner->buffer.p_picture = p_picture->p_next;
1258 b_has_more = p_owner->buffer.p_picture != NULL;
1260 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1264 int i_rate = INPUT_RATE_DEFAULT;
1267 if( b_buffering_first )
1269 assert( p_owner->buffer.b_first );
1270 assert( !p_owner->buffer.i_count );
1271 msg_Dbg( p_dec, "Received first picture" );
1272 p_owner->buffer.b_first = false;
1273 p_picture->date = mdate();
1274 p_picture->b_force = true;
1276 if( p_owner->p_clock )
1277 i_rate = input_clock_GetRate( p_owner->p_clock );
1281 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1282 &i_rate, &i_delay, false );
1285 vlc_mutex_unlock( &p_owner->lock );
1288 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1290 if( p_picture->date <= 0 || p_picture->date >= i_max_date )
1295 if( i_rate != p_owner->i_last_rate )
1297 /* Be sure to not display old picture after our own */
1298 VoutFlushPicture( p_vout, p_picture->date );
1299 p_owner->i_last_rate = i_rate;
1302 vout_DatePicture( p_vout, p_picture, p_picture->date );
1304 vout_DisplayPicture( p_vout, p_picture );
1308 if( p_picture->date <= 0 )
1310 msg_Warn( p_vout, "non-dated video buffer received" );
1314 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1315 p_picture->date - mdate() );
1318 VoutDisplayedPicture( p_vout, p_picture );
1322 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1324 *pi_played_sum += i_tmp_display;
1325 *pi_lost_sum += i_tmp_lost;
1327 if( !b_has_more || b_buffering_first )
1330 vlc_mutex_lock( &p_owner->lock );
1331 if( !p_owner->buffer.p_picture )
1333 vlc_mutex_unlock( &p_owner->lock );
1339 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1341 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1342 input_thread_t *p_input = p_owner->p_input;
1346 int i_displayed = 0;
1348 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1350 vout_thread_t *p_vout = p_owner->p_vout;
1353 /* It prevent freezing VLC in case of broken decoder */
1354 VoutDisplayedPicture( p_vout, p_pic );
1356 block_Release( p_block );
1362 if( p_pic->date < p_owner->i_preroll_end )
1364 VoutDisplayedPicture( p_vout, p_pic );
1368 if( p_owner->i_preroll_end > 0 )
1370 msg_Dbg( p_dec, "End of video preroll" );
1372 VoutFlushPicture( p_vout, 1 );
1374 p_owner->i_preroll_end = -1;
1377 if( p_dec->pf_get_cc &&
1378 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1379 DecoderGetCc( p_dec, p_dec );
1381 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1383 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1385 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1387 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1389 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1392 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1395 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1399 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1402 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1403 vout_thread_t *p_vout = p_owner->p_spu_vout;
1406 if( p_subpic->i_start <= 0 )
1408 msg_Warn( p_dec, "non-dated spu buffer received" );
1409 subpicture_Delete( p_subpic );
1414 vlc_mutex_lock( &p_owner->lock );
1416 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1418 p_subpic->p_next = NULL;
1420 *p_owner->buffer.pp_subpic_next = p_subpic;
1421 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1423 p_owner->buffer.i_count++;
1424 /* XXX it is important to be full after the first one */
1425 if( p_owner->buffer.i_count > 0 )
1427 p_owner->buffer.b_full = true;
1428 vlc_cond_signal( &p_owner->wait );
1434 bool b_has_more = false;
1436 DecoderWaitUnblock( p_dec, &b_reject );
1438 if( p_owner->b_buffering )
1440 vlc_mutex_unlock( &p_owner->lock );
1445 if( p_owner->buffer.p_subpic )
1447 p_subpic = p_owner->buffer.p_subpic;
1449 p_owner->buffer.p_subpic = p_subpic->p_next;
1451 b_has_more = p_owner->buffer.p_subpic != NULL;
1453 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1457 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1458 NULL, NULL, b_telx );
1460 vlc_mutex_unlock( &p_owner->lock );
1463 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1465 subpicture_Delete( p_subpic );
1469 vlc_mutex_lock( &p_owner->lock );
1470 if( !p_owner->buffer.p_subpic )
1472 vlc_mutex_unlock( &p_owner->lock );
1478 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1481 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1483 assert( p_owner->p_clock );
1485 vlc_mutex_lock( &p_owner->lock );
1488 DecoderWaitUnblock( p_dec, &b_reject );
1490 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1491 &p_sout_block->i_rate, NULL, b_telx );
1493 vlc_mutex_unlock( &p_owner->lock );
1495 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1499 static void DecoderFlushBuffering( decoder_t *p_dec )
1501 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1503 vlc_assert_locked( &p_owner->lock );
1505 while( p_owner->buffer.p_picture )
1507 picture_t *p_picture = p_owner->buffer.p_picture;
1509 p_owner->buffer.p_picture = p_picture->p_next;
1510 p_owner->buffer.i_count--;
1512 if( p_owner->p_vout )
1513 VoutDisplayedPicture( p_owner->p_vout, p_picture );
1515 if( !p_owner->buffer.p_picture )
1516 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1518 while( p_owner->buffer.p_audio )
1520 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1522 p_owner->buffer.p_audio = p_audio->p_next;
1523 p_owner->buffer.i_count--;
1525 aout_BufferFree( p_audio );
1527 if( !p_owner->buffer.p_audio )
1528 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1530 while( p_owner->buffer.p_subpic )
1532 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1534 p_owner->buffer.p_subpic = p_subpic->p_next;
1535 p_owner->buffer.i_count--;
1537 subpicture_Delete( p_subpic );
1539 if( !p_owner->buffer.p_subpic )
1540 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1544 /* This function process a block for sout
1546 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1548 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1549 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1550 block_t *p_sout_block;
1552 while( ( p_sout_block =
1553 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1555 if( !p_owner->p_sout_input )
1557 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1559 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1560 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1561 if( p_dec->fmt_in.psz_language )
1563 if( p_owner->sout.psz_language )
1564 free( p_owner->sout.psz_language );
1565 p_owner->sout.psz_language =
1566 strdup( p_dec->fmt_in.psz_language );
1569 p_owner->p_sout_input =
1570 sout_InputNew( p_owner->p_sout,
1573 if( p_owner->p_sout_input == NULL )
1575 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1576 (char *)&p_owner->sout.i_codec );
1577 p_dec->b_error = true;
1579 while( p_sout_block )
1581 block_t *p_next = p_sout_block->p_next;
1582 block_Release( p_sout_block );
1583 p_sout_block = p_next;
1589 while( p_sout_block )
1591 block_t *p_next = p_sout_block->p_next;
1593 p_sout_block->p_next = NULL;
1595 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1597 p_sout_block = p_next;
1600 /* For now it's enough, as only sout impact on this flag */
1601 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1602 p_owner->p_input->p->b_out_pace_control )
1604 msg_Dbg( p_dec, "switching to sync mode" );
1605 p_owner->p_input->p->b_out_pace_control = false;
1607 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1608 !p_owner->p_input->p->b_out_pace_control )
1610 msg_Dbg( p_dec, "switching to async mode" );
1611 p_owner->p_input->p->b_out_pace_control = true;
1616 /* This function process a video block
1618 static void DecoderProcessVideo( 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 );
1635 if( p_packetizer->pf_get_cc )
1636 DecoderGetCc( p_dec, p_packetizer );
1638 while( p_packetized_block )
1640 block_t *p_next = p_packetized_block->p_next;
1641 p_packetized_block->p_next = NULL;
1643 DecoderDecodeVideo( p_dec, p_packetized_block );
1645 p_packetized_block = p_next;
1651 DecoderDecodeVideo( p_dec, p_block );
1654 if( b_flush && p_owner->p_vout )
1655 VoutFlushPicture( p_owner->p_vout, 1 );
1658 /* This function process a audio block
1660 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1662 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1664 if( p_owner->p_packetizer )
1666 block_t *p_packetized_block;
1667 decoder_t *p_packetizer = p_owner->p_packetizer;
1669 while( (p_packetized_block =
1670 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1672 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1674 es_format_Clean( &p_dec->fmt_in );
1675 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1678 while( p_packetized_block )
1680 block_t *p_next = p_packetized_block->p_next;
1681 p_packetized_block->p_next = NULL;
1683 DecoderDecodeAudio( p_dec, p_packetized_block );
1685 p_packetized_block = p_next;
1691 DecoderDecodeAudio( p_dec, p_block );
1694 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1695 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1698 /* This function process a subtitle block
1700 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1702 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1703 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1705 input_thread_t *p_input = p_owner->p_input;
1706 vout_thread_t *p_vout;
1707 subpicture_t *p_spu;
1709 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1711 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1712 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1713 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1715 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1716 if( p_vout && p_owner->p_spu_vout == p_vout )
1718 /* Preroll does not work very well with subtitle */
1719 if( p_spu->i_start > 0 &&
1720 p_spu->i_start < p_owner->i_preroll_end &&
1721 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1723 subpicture_Delete( p_spu );
1727 DecoderPlaySpu( p_dec, p_spu, b_telx );
1732 subpicture_Delete( p_spu );
1735 vlc_object_release( p_vout );
1738 if( b_flush && p_owner->p_spu_vout )
1740 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1742 if( p_vout && p_owner->p_spu_vout == p_vout )
1743 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1744 p_owner->i_spu_channel );
1747 vlc_object_release( p_vout );
1754 * \param p_dec the decoder object
1755 * \param p_block the block to decode
1756 * \return VLC_SUCCESS or an error code
1758 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1760 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1761 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1763 if( p_block && p_block->i_buffer <= 0 )
1765 assert( !b_flush_request );
1766 block_Release( p_block );
1771 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1774 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1776 DecoderProcessSout( p_dec, p_block );
1781 bool b_flush = false;
1785 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1786 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1788 b_flush = !b_flushing && b_flush_request;
1790 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1793 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1795 DecoderProcessAudio( p_dec, p_block, b_flush );
1797 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1799 DecoderProcessVideo( p_dec, p_block, b_flush );
1801 else if( p_dec->fmt_in.i_cat == SPU_ES )
1803 DecoderProcessSpu( p_dec, p_block, b_flush );
1807 msg_Err( p_dec, "unknown ES format" );
1808 p_dec->b_error = true;
1813 if( b_flush_request )
1815 vlc_mutex_lock( &p_owner->lock );
1816 DecoderFlushBuffering( p_dec );
1817 vlc_mutex_unlock( &p_owner->lock );
1819 DecoderSignalFlushed( p_dec );
1822 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1826 * Destroys a decoder object
1828 * \param p_dec the decoder object
1831 static void DeleteDecoder( decoder_t * p_dec )
1833 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1835 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1836 (char*)&p_dec->fmt_in.i_codec,
1837 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1839 /* Free all packets still in the decoder fifo. */
1840 block_FifoEmpty( p_owner->p_fifo );
1841 block_FifoRelease( p_owner->p_fifo );
1844 vlc_mutex_lock( &p_owner->lock );
1845 DecoderFlushBuffering( p_dec );
1846 vlc_mutex_unlock( &p_owner->lock );
1849 if( p_owner->p_aout_input )
1850 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1851 if( p_owner->p_aout )
1853 vlc_object_release( p_owner->p_aout );
1854 p_owner->p_aout = NULL;
1856 if( p_owner->p_vout )
1858 /* Hack to make sure all the the pictures are freed by the decoder */
1859 for( int i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1861 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1863 if( p_pic->i_status == RESERVED_PICTURE )
1864 vout_DestroyPicture( p_owner->p_vout, p_pic );
1865 if( p_pic->i_refcount > 0 )
1866 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1869 /* We are about to die. Reattach video output to p_vlc. */
1870 vout_Request( p_dec, p_owner->p_vout, NULL );
1871 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1875 if( p_owner->p_sout_input )
1877 sout_InputDelete( p_owner->p_sout_input );
1878 es_format_Clean( &p_owner->sout );
1882 if( p_dec->fmt_in.i_cat == SPU_ES )
1884 vout_thread_t *p_vout;
1886 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1887 if( p_vout && p_owner->p_spu_vout == p_vout )
1889 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1890 p_owner->i_spu_channel );
1891 vlc_object_release( p_vout );
1895 es_format_Clean( &p_dec->fmt_in );
1896 es_format_Clean( &p_dec->fmt_out );
1898 if( p_owner->p_packetizer )
1900 module_unneed( p_owner->p_packetizer,
1901 p_owner->p_packetizer->p_module );
1902 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1903 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1904 vlc_object_detach( p_owner->p_packetizer );
1905 vlc_object_release( p_owner->p_packetizer );
1908 vlc_cond_destroy( &p_owner->wait );
1909 vlc_mutex_destroy( &p_owner->lock );
1911 vlc_object_detach( p_dec );
1916 /*****************************************************************************
1917 * Buffers allocation callbacks for the decoders
1918 *****************************************************************************/
1919 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1921 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1922 aout_buffer_t *p_buffer;
1924 if( p_owner->p_aout_input != NULL &&
1925 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1926 p_dec->fmt_out.audio.i_original_channels !=
1927 p_owner->audio.i_original_channels ||
1928 p_dec->fmt_out.audio.i_bytes_per_frame !=
1929 p_owner->audio.i_bytes_per_frame ) )
1931 aout_input_t *p_aout_input = p_owner->p_aout_input;
1933 /* Parameters changed, restart the aout */
1934 vlc_mutex_lock( &p_owner->lock );
1936 DecoderFlushBuffering( p_dec );
1938 p_owner->p_aout_input = NULL;
1939 aout_DecDelete( p_owner->p_aout, p_aout_input );
1941 vlc_mutex_unlock( &p_owner->lock );
1944 if( p_owner->p_aout_input == NULL )
1946 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1947 audio_sample_format_t format;
1948 aout_input_t *p_aout_input;
1949 aout_instance_t *p_aout;
1951 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1952 p_owner->audio = p_dec->fmt_out.audio;
1954 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1955 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1956 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1958 if ( i_force_dolby == 1 )
1960 format.i_original_channels = format.i_original_channels |
1961 AOUT_CHAN_DOLBYSTEREO;
1963 else /* i_force_dolby == 2 */
1965 format.i_original_channels = format.i_original_channels &
1966 ~AOUT_CHAN_DOLBYSTEREO;
1970 p_aout = p_owner->p_aout;
1971 p_aout_input = aout_DecNew( p_dec, &p_aout,
1972 &format, &p_dec->fmt_out.audio_replay_gain );
1974 vlc_mutex_lock( &p_owner->lock );
1975 p_owner->p_aout = p_aout;
1976 p_owner->p_aout_input = p_aout_input;
1977 vlc_mutex_unlock( &p_owner->lock );
1979 if( p_owner->p_aout_input == NULL )
1981 msg_Err( p_dec, "failed to create audio output" );
1982 p_dec->b_error = true;
1985 p_dec->fmt_out.audio.i_bytes_per_frame =
1986 p_owner->audio.i_bytes_per_frame;
1989 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1994 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1996 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1998 aout_DecDeleteBuffer( p_owner->p_aout,
1999 p_owner->p_aout_input, p_buffer );
2003 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2005 static picture_t *vout_new_buffer( decoder_t *p_dec )
2007 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2009 if( p_owner->p_vout == NULL ||
2010 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2011 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2012 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2013 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2015 vout_thread_t *p_vout;
2017 if( !p_dec->fmt_out.video.i_width ||
2018 !p_dec->fmt_out.video.i_height )
2020 /* Can't create a new vout without display size */
2024 if( !p_dec->fmt_out.video.i_visible_width ||
2025 !p_dec->fmt_out.video.i_visible_height )
2027 if( p_dec->fmt_in.video.i_visible_width &&
2028 p_dec->fmt_in.video.i_visible_height )
2030 p_dec->fmt_out.video.i_visible_width =
2031 p_dec->fmt_in.video.i_visible_width;
2032 p_dec->fmt_out.video.i_visible_height =
2033 p_dec->fmt_in.video.i_visible_height;
2037 p_dec->fmt_out.video.i_visible_width =
2038 p_dec->fmt_out.video.i_width;
2039 p_dec->fmt_out.video.i_visible_height =
2040 p_dec->fmt_out.video.i_height;
2044 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2045 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2047 p_dec->fmt_out.video.i_visible_height = 1080;
2048 p_dec->fmt_out.video.i_sar_num *= 135;
2049 p_dec->fmt_out.video.i_sar_den *= 136;
2050 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2053 if( !p_dec->fmt_out.video.i_sar_num ||
2054 !p_dec->fmt_out.video.i_sar_den )
2056 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2057 p_dec->fmt_out.video.i_visible_height;
2059 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2060 p_dec->fmt_out.video.i_visible_width;
2063 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2064 &p_dec->fmt_out.video.i_sar_den,
2065 p_dec->fmt_out.video.i_sar_num,
2066 p_dec->fmt_out.video.i_sar_den, 50000 );
2068 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2069 p_owner->video = p_dec->fmt_out.video;
2071 vlc_mutex_lock( &p_owner->lock );
2073 DecoderFlushBuffering( p_dec );
2075 p_vout = p_owner->p_vout;
2076 p_owner->p_vout = NULL;
2077 vlc_mutex_unlock( &p_owner->lock );
2079 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2081 vlc_mutex_lock( &p_owner->lock );
2082 p_owner->p_vout = p_vout;
2083 vlc_mutex_unlock( &p_owner->lock );
2085 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2086 if( p_vout == NULL )
2088 msg_Err( p_dec, "failed to create video output" );
2089 p_dec->b_error = true;
2093 if( p_owner->video.i_rmask )
2094 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2095 if( p_owner->video.i_gmask )
2096 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2097 if( p_owner->video.i_bmask )
2098 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2101 /* Get a new picture
2105 picture_t *p_picture;
2106 int i_pic, i_ready_pic;
2108 if( p_dec->b_die || p_dec->b_error )
2111 /* The video filter chain required that there is always 1 free buffer
2112 * that it will use as temporary one. It will release the temporary
2113 * buffer once its work is done, so this check is safe even if we don't
2114 * lock around both count() and create().
2116 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2118 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2123 if( DecoderIsFlushing( p_dec ) )
2127 DecoderSignalBuffering( p_dec, true );
2129 /* Check the decoder doesn't leak pictures */
2130 for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
2132 const picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
2134 if( p_pic->i_status == READY_PICTURE )
2137 /* If we have at least 2 ready pictures, wait for the vout thread to
2139 if( i_ready_pic >= 2 )
2145 if( p_pic->i_status == DISPLAYED_PICTURE )
2147 /* If at least one displayed picture is not referenced
2148 * let vout free it */
2149 if( p_pic->i_refcount == 0 )
2154 if( i_pic == p_owner->p_vout->render.i_pictures )
2156 /* Too many pictures are still referenced, there is probably a bug
2157 * with the decoder */
2158 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
2160 /* Just free all the pictures */
2161 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
2163 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
2165 if( p_pic->i_status == RESERVED_PICTURE )
2166 vout_DestroyPicture( p_owner->p_vout, p_pic );
2167 if( p_pic->i_refcount > 0 )
2168 vout_UnlinkPicture( p_owner->p_vout, p_pic );
2172 msleep( VOUT_OUTMEM_SLEEP );
2176 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2178 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
2181 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2183 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2186 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2188 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2191 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2193 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2194 vout_thread_t *p_vout = NULL;
2195 subpicture_t *p_subpic;
2196 int i_attempts = 30;
2198 while( i_attempts-- )
2200 if( p_dec->b_die || p_dec->b_error )
2203 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2207 msleep( VOUT_DISPLAY_DELAY );
2212 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2216 if( p_owner->p_spu_vout != p_vout )
2218 vlc_mutex_lock( &p_owner->lock );
2220 DecoderFlushBuffering( p_dec );
2222 vlc_mutex_unlock( &p_owner->lock );
2224 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2225 &p_owner->i_spu_channel );
2226 p_owner->i_spu_order = 0;
2227 p_owner->p_spu_vout = p_vout;
2230 p_subpic = subpicture_New();
2233 p_subpic->i_channel = p_owner->i_spu_channel;
2234 p_subpic->i_order = p_owner->i_spu_order++;
2235 p_subpic->b_subtitle = true;
2238 vlc_object_release( p_vout );
2243 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2245 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2246 vout_thread_t *p_vout = NULL;
2248 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2249 if( !p_vout || p_owner->p_spu_vout != p_vout )
2252 vlc_object_release( p_vout );
2253 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2257 subpicture_Delete( p_subpic );
2259 vlc_object_release( p_vout );