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"
48 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
49 static void DeleteDecoder( decoder_t * );
51 static void* DecoderThread( vlc_object_t * );
52 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
54 /* Buffers allocation callbacks for the decoders */
55 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
56 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
58 static picture_t *vout_new_buffer( decoder_t * );
59 static void vout_del_buffer( decoder_t *, picture_t * );
60 static void vout_link_picture( decoder_t *, picture_t * );
61 static void vout_unlink_picture( decoder_t *, picture_t * );
63 static subpicture_t *spu_new_buffer( decoder_t * );
64 static void spu_del_buffer( decoder_t *, subpicture_t * );
66 static es_format_t null_es_format;
68 struct decoder_owner_sys_t
72 int64_t i_preroll_end;
74 input_thread_t *p_input;
75 input_clock_t *p_clock;
77 aout_instance_t *p_aout;
78 aout_input_t *p_aout_input;
80 vout_thread_t *p_vout;
82 vout_thread_t *p_spu_vout;
86 sout_instance_t *p_sout;
87 sout_packetizer_input_t *p_sout_input;
89 /* Some decoders require already packetized data (ie. not truncated) */
90 decoder_t *p_packetizer;
92 /* Current format in use by the output */
103 bool pb_cc_present[4];
108 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
110 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
111 "VLC probably does not support this sound or video format.",
113 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
114 _("VLC does not support the audio or video format \"%4.4s\". "
115 "Unfortunately there is no way for you to fix this."), (char*)&codec );
118 /* decoder_GetInputAttachment:
120 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
121 const char *psz_name )
123 input_attachment_t *p_attachment;
124 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
128 /* decoder_GetInputAttachments:
130 int decoder_GetInputAttachments( decoder_t *p_dec,
131 input_attachment_t ***ppp_attachment,
134 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
135 ppp_attachment, pi_attachment );
137 /* decoder_GetDisplayDate:
139 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
146 * Spawns a new decoder thread
148 * \param p_input the input thread
149 * \param p_es the es descriptor
150 * \return the spawned decoder object
152 decoder_t *input_DecoderNew( input_thread_t *p_input,
153 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
155 decoder_t *p_dec = NULL;
159 (void)b_force_decoder;
161 /* If we are in sout mode, search for packetizer module */
164 /* Create the decoder configuration structure */
165 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
168 msg_Err( p_input, "could not create packetizer" );
169 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
170 _("VLC could not open the packetizer module.") );
177 /* Create the decoder configuration structure */
178 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
181 msg_Err( p_input, "could not create decoder" );
182 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
183 _("VLC could not open the decoder module.") );
188 if( !p_dec->p_module )
190 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
192 DeleteDecoder( p_dec );
193 vlc_object_release( p_dec );
197 p_dec->p_owner->p_clock = p_clock;
199 if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
201 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
202 p_dec->p_owner->b_own_thread = false;
206 var_Get( p_input, "minimize-threads", &val );
207 p_dec->p_owner->b_own_thread = !val.b_bool;
210 if( p_dec->p_owner->b_own_thread )
213 if( fmt->i_cat == AUDIO_ES )
214 i_priority = VLC_THREAD_PRIORITY_AUDIO;
216 i_priority = VLC_THREAD_PRIORITY_VIDEO;
218 /* Spawn the decoder thread */
219 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
220 i_priority, false ) )
222 msg_Err( p_dec, "cannot spawn decoder thread" );
223 module_unneed( p_dec, p_dec->p_module );
224 DeleteDecoder( p_dec );
225 vlc_object_release( p_dec );
234 * Kills a decoder thread and waits until it's finished
236 * \param p_input the input thread
237 * \param p_es the es descriptor
240 void input_DecoderDelete( decoder_t *p_dec )
242 vlc_object_kill( p_dec );
244 if( p_dec->p_owner->b_own_thread )
246 /* Make sure the thread leaves the function by
247 * sending it an empty block. */
248 block_t *p_block = block_New( p_dec, 0 );
249 input_DecoderDecode( p_dec, p_block );
251 vlc_thread_join( p_dec );
253 /* Don't module_unneed() here because of the dll loader that wants
254 * close() in the same thread than open()/decode() */
259 input_DecoderDecode( p_dec, NULL );
261 module_unneed( p_dec, p_dec->p_module );
265 if( p_dec->p_owner->b_cc_supported )
268 for( i = 0; i < 4; i++ )
269 input_DecoderSetCcState( p_dec, false, i );
272 /* Delete decoder configuration */
273 DeleteDecoder( p_dec );
275 /* Delete the decoder */
276 vlc_object_release( p_dec );
280 * Put a block_t in the decoder's fifo.
282 * \param p_dec the decoder object
283 * \param p_block the data block
285 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
287 if( p_dec->p_owner->b_own_thread )
289 if( p_dec->p_owner->p_input->p->b_out_pace_control )
292 while( !p_dec->b_die && !p_dec->b_error &&
293 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
298 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
300 /* FIXME: ideally we would check the time amount of data
301 * in the fifo instead of its size. */
302 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
303 "consumed quickly enough), resetting fifo!" );
304 block_FifoEmpty( p_dec->p_owner->p_fifo );
307 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
311 if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
314 block_Release( p_block );
318 DecoderDecode( p_dec, p_block );
323 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
328 if( p_dec->p_owner->b_own_thread && b_flush )
329 block_FifoEmpty( p_dec->p_owner->p_fifo );
331 /* Send a special block */
332 p_null = block_New( p_dec, 128 );
333 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
334 if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
335 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
336 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
337 if( p_dec->p_owner->p_packetizer && b_flush )
338 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
339 memset( p_null->p_buffer, 0, p_null->i_buffer );
341 input_DecoderDecode( p_dec, p_null );
344 bool input_DecoderEmpty( decoder_t * p_dec )
346 if( p_dec->p_owner->b_own_thread &&
347 block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
354 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
358 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
359 for( i = 0; i < 4; i++ )
360 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
361 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
363 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
365 decoder_owner_sys_t *p_owner = p_dec->p_owner;
367 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
369 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
374 static const vlc_fourcc_t fcc[4] = {
375 VLC_FOURCC('c', 'c', '1', ' '),
376 VLC_FOURCC('c', 'c', '2', ' '),
377 VLC_FOURCC('c', 'c', '3', ' '),
378 VLC_FOURCC('c', 'c', '4', ' '),
383 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
384 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_dec->p_owner->p_sout );
387 msg_Err( p_dec, "could not create decoder" );
388 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
389 _("VLC could not open the decoder module.") );
392 else if( !p_cc->p_module )
394 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
395 DeleteDecoder( p_cc );
396 vlc_object_release( p_cc );
400 vlc_mutex_lock( &p_owner->lock_cc );
401 p_dec->p_owner->pp_cc[i_channel] = p_cc;
402 vlc_mutex_unlock( &p_owner->lock_cc );
408 vlc_mutex_lock( &p_owner->lock_cc );
409 p_cc = p_dec->p_owner->pp_cc[i_channel];
410 p_dec->p_owner->pp_cc[i_channel] = NULL;
411 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
415 vlc_object_kill( p_cc );
416 module_unneed( p_cc, p_cc->p_module );
417 DeleteDecoder( p_cc );
418 vlc_object_release( p_cc );
423 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
425 decoder_owner_sys_t *p_owner = p_dec->p_owner;
428 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
431 vlc_mutex_lock( &p_owner->lock_cc );
432 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
433 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
438 * Create a decoder object
440 * \param p_input the input thread
441 * \param p_es the es descriptor
442 * \param i_object_type Object type as define in include/vlc_objects.h
443 * \return the decoder object
445 static decoder_t * CreateDecoder( input_thread_t *p_input,
446 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
449 decoder_owner_sys_t *p_owner;
452 p_dec = vlc_object_create( p_input, i_object_type );
456 p_dec->pf_decode_audio = NULL;
457 p_dec->pf_decode_video = NULL;
458 p_dec->pf_decode_sub = NULL;
459 p_dec->pf_get_cc = NULL;
460 p_dec->pf_packetize = NULL;
462 /* Initialize the decoder fifo */
463 p_dec->p_module = NULL;
465 memset( &null_es_format, 0, sizeof(es_format_t) );
466 es_format_Copy( &p_dec->fmt_in, fmt );
467 es_format_Copy( &p_dec->fmt_out, &null_es_format );
469 /* Allocate our private structure for the decoder */
470 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
471 if( p_dec->p_owner == NULL )
473 vlc_object_release( p_dec );
476 p_dec->p_owner->b_own_thread = true;
477 p_dec->p_owner->i_preroll_end = -1;
478 p_dec->p_owner->p_input = p_input;
479 p_dec->p_owner->p_aout = NULL;
480 p_dec->p_owner->p_aout_input = NULL;
481 p_dec->p_owner->p_vout = NULL;
482 p_dec->p_owner->p_spu_vout = NULL;
483 p_dec->p_owner->i_spu_channel = 0;
484 p_dec->p_owner->i_spu_order = 0;
485 p_dec->p_owner->p_sout = p_sout;
486 p_dec->p_owner->p_sout_input = NULL;
487 p_dec->p_owner->p_packetizer = NULL;
490 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
492 free( p_dec->p_owner );
493 vlc_object_release( p_dec );
497 /* Set buffers allocation callbacks for the decoders */
498 p_dec->pf_aout_buffer_new = aout_new_buffer;
499 p_dec->pf_aout_buffer_del = aout_del_buffer;
500 p_dec->pf_vout_buffer_new = vout_new_buffer;
501 p_dec->pf_vout_buffer_del = vout_del_buffer;
502 p_dec->pf_picture_link = vout_link_picture;
503 p_dec->pf_picture_unlink = vout_unlink_picture;
504 p_dec->pf_spu_buffer_new = spu_new_buffer;
505 p_dec->pf_spu_buffer_del = spu_del_buffer;
507 vlc_object_attach( p_dec, p_input );
509 /* Find a suitable decoder/packetizer module */
510 if( i_object_type == VLC_OBJECT_DECODER )
511 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
513 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
515 /* Check if decoder requires already packetized data */
516 if( i_object_type == VLC_OBJECT_DECODER &&
517 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
519 p_dec->p_owner->p_packetizer =
520 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
521 if( p_dec->p_owner->p_packetizer )
523 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
526 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
529 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
531 p_dec->p_owner->p_packetizer->p_module =
532 module_need( p_dec->p_owner->p_packetizer,
533 "packetizer", "$packetizer", 0 );
535 if( !p_dec->p_owner->p_packetizer->p_module )
537 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
538 vlc_object_detach( p_dec->p_owner->p_packetizer );
539 vlc_object_release( p_dec->p_owner->p_packetizer );
544 /* Copy ourself the input replay gain */
545 if( fmt->i_cat == AUDIO_ES )
547 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
549 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
551 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
552 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
554 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
556 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
557 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
562 p_owner->b_cc_supported = false;
563 if( i_object_type == VLC_OBJECT_DECODER )
565 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
566 p_owner->b_cc_supported = true;
567 if( p_dec->pf_get_cc )
568 p_owner->b_cc_supported = true;
571 vlc_mutex_init( &p_owner->lock_cc );
572 for( i = 0; i < 4; i++ )
574 p_owner->pb_cc_present[i] = false;
575 p_owner->pp_cc[i] = NULL;
581 * The decoding main loop
583 * \param p_dec the decoder
585 static void* DecoderThread( vlc_object_t *p_this )
587 decoder_t * p_dec = (decoder_t *)p_this;
589 int canc = vlc_savecancel();
591 /* The decoder's main loop */
592 while( !p_dec->b_die && !p_dec->b_error )
594 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
599 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
605 while( !p_dec->b_die )
607 /* Trash all received PES packets */
608 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
610 block_Release( p_block );
613 /* We do it here because of the dll loader that wants close() in the
614 * same thread than open()/decode() */
615 module_unneed( p_dec, p_dec->p_module );
616 vlc_restorecancel( canc );
620 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
622 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
623 *pi_preroll = INT64_MAX;
624 else if( p->i_pts > 0 )
625 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
626 else if( p->i_dts > 0 )
627 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
629 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
631 input_thread_t *p_input = p_dec->p_owner->p_input;
632 const int i_rate = p_block->i_rate;
633 aout_buffer_t *p_aout_buf;
635 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
637 aout_instance_t *p_aout = p_dec->p_owner->p_aout;
638 aout_input_t *p_aout_input = p_dec->p_owner->p_aout_input;
642 /* It prevent freezing VLC in case of broken decoder */
643 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
645 block_Release( p_block );
648 vlc_mutex_lock( &p_input->p->counters.counters_lock );
649 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
650 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
652 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
654 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
658 if( p_dec->p_owner->i_preroll_end > 0 )
660 /* FIXME TODO flush audio output (don't know how to do that) */
661 msg_Dbg( p_dec, "End of audio preroll" );
662 p_dec->p_owner->i_preroll_end = -1;
664 aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate );
667 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
674 assert( p_dec_cc->pf_get_cc != NULL );
676 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
677 if( !p_dec->p_owner->b_cc_supported )
680 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
684 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
685 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
687 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
688 if( p_dec->p_owner->pp_cc[i] )
692 for( i = 0; i < 4; i++ )
694 if( !p_dec->p_owner->pp_cc[i] )
697 if( i_cc_decoder > 1 )
698 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
700 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
703 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
705 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
707 vlc_mutex_lock( &p_vout->picture_lock );
709 if( p_pic->i_status == READY_PICTURE )
711 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
714 else if( p_pic->i_refcount > 0 )
716 p_pic->i_status = DISPLAYED_PICTURE;
720 p_pic->i_status = DESTROYED_PICTURE;
721 picture_CleanupQuant( p_pic );
722 p_vout->i_heap_size--;
725 vlc_mutex_unlock( &p_vout->picture_lock );
727 static void VoutFlushPicture( vout_thread_t *p_vout )
730 vlc_mutex_lock( &p_vout->picture_lock );
731 for( i = 0; i < p_vout->render.i_pictures; i++ )
733 picture_t *p_pic = p_vout->render.pp_picture[i];
735 if( p_pic->i_status == READY_PICTURE ||
736 p_pic->i_status == DISPLAYED_PICTURE )
738 /* We cannot change picture status if it is in READY_PICTURE state,
739 * Just make sure they won't be displayed */
743 vlc_mutex_unlock( &p_vout->picture_lock );
746 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
748 input_thread_t *p_input = p_dec->p_owner->p_input;
749 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
750 input_thread_private_t *p_priv = p_input->p;
752 picture_t *p_old = NULL;
753 picture_t *p_young = NULL;
756 /* Enable with --auto-adjust-pts-delay */
757 if( !p_priv->pts_adjust.b_auto_adjust )
760 for( i = 0; i < I_RENDERPICTURES; i++ )
762 picture_t *p_pic = PP_RENDERPICTURE[i];
764 if( p_pic->i_status != READY_PICTURE )
767 if( !p_old || p_pic->date < p_old->date )
769 if( !p_young || p_pic->date > p_young->date )
773 if( !p_young || !p_old )
776 /* Try to find if we can reduce the pts
777 * This first draft is way to simple, and we can't say if the
778 * algo will converge. It's also full of constants.
779 * But this simple algo allows to reduce the latency
781 * The whole point of this, is to bypass the pts_delay set
782 * by the access but also the delay arbitraly set by
784 * Actually the remote server's muxer may set up a
785 * pts<->dts delay in the muxed stream. That is
786 * why we may end up in having a negative pts_delay,
787 * to compensate that artificial delay. */
788 const mtime_t i_buffer_length = p_young->date - p_old->date;
789 int64_t i_pts_slide = 0;
790 if( i_buffer_length < 10000 )
792 if( p_priv->pts_adjust.i_num_faulty > 10 )
794 i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
795 p_priv->pts_adjust.i_num_faulty = 0;
797 if( p_priv->pts_adjust.b_to_high )
799 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
800 p_priv->pts_adjust.i_num_faulty = 0;
802 p_priv->pts_adjust.i_num_faulty++;
804 else if( i_buffer_length > 100000 )
806 if( p_priv->pts_adjust.i_num_faulty > 25 )
808 i_pts_slide = -i_buffer_length/2;
809 p_priv->pts_adjust.i_num_faulty = 0;
811 if( p_priv->pts_adjust.b_to_high )
813 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
814 p_priv->pts_adjust.i_num_faulty = 0;
816 p_priv->pts_adjust.i_num_faulty++;
818 if( i_pts_slide != 0 )
820 const mtime_t i_pts_delay_org = p_input->i_pts_delay;
822 p_input->i_pts_delay += i_pts_slide;
824 /* Don't play with the pts delay for more than -2<->3sec */
825 if( p_input->i_pts_delay < -2000000 )
826 p_input->i_pts_delay = -2000000;
827 else if( p_input->i_pts_delay > 3000000 )
828 p_input->i_pts_delay = 3000000;
829 i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
831 msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
832 (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
834 vlc_mutex_lock( &p_vout->picture_lock );
835 /* Slide all the picture */
836 for( i = 0; i < I_RENDERPICTURES; i++ )
837 PP_RENDERPICTURE[i]->date += i_pts_slide;
838 /* FIXME: slide aout/spu */
839 vlc_mutex_unlock( &p_vout->picture_lock );
843 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
845 input_thread_t *p_input = p_dec->p_owner->p_input;
848 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
850 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
853 /* It prevent freezing VLC in case of broken decoder */
854 VoutDisplayedPicture( p_vout, p_pic );
856 block_Release( p_block );
860 vlc_mutex_lock( &p_input->p->counters.counters_lock );
861 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
862 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
864 if( p_pic->date < p_dec->p_owner->i_preroll_end )
866 VoutDisplayedPicture( p_vout, p_pic );
870 if( p_dec->p_owner->i_preroll_end > 0 )
872 msg_Dbg( p_dec, "End of video preroll" );
874 VoutFlushPicture( p_vout );
876 p_dec->p_owner->i_preroll_end = -1;
879 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
880 DecoderGetCc( p_dec, p_dec );
882 vout_DatePicture( p_vout, p_pic, p_pic->date );
884 DecoderOptimizePtsDelay( p_dec );
886 vout_DisplayPicture( p_vout, p_pic );
893 * \param p_dec the decoder object
894 * \param p_block the block to decode
895 * \return VLC_SUCCESS or an error code
897 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
899 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
900 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
902 if( p_block && p_block->i_buffer <= 0 )
904 block_Release( p_block );
909 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
911 block_t *p_sout_block;
913 while( ( p_sout_block =
914 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
916 if( !p_dec->p_owner->p_sout_input )
918 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
920 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
921 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
922 if( p_dec->fmt_in.psz_language )
924 if( p_dec->p_owner->sout.psz_language )
925 free( p_dec->p_owner->sout.psz_language );
926 p_dec->p_owner->sout.psz_language =
927 strdup( p_dec->fmt_in.psz_language );
930 p_dec->p_owner->p_sout_input =
931 sout_InputNew( p_dec->p_owner->p_sout,
932 &p_dec->p_owner->sout );
934 if( p_dec->p_owner->p_sout_input == NULL )
936 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
937 (char *)&p_dec->p_owner->sout.i_codec );
938 p_dec->b_error = true;
940 while( p_sout_block )
942 block_t *p_next = p_sout_block->p_next;
943 block_Release( p_sout_block );
944 p_sout_block = p_next;
950 while( p_sout_block )
952 block_t *p_next = p_sout_block->p_next;
954 p_sout_block->p_next = NULL;
955 p_sout_block->i_rate = i_rate;
957 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
960 p_sout_block = p_next;
963 /* For now it's enough, as only sout impact on this flag */
964 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
965 p_dec->p_owner->p_input->p->b_out_pace_control )
967 msg_Dbg( p_dec, "switching to sync mode" );
968 p_dec->p_owner->p_input->p->b_out_pace_control = false;
970 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
971 !p_dec->p_owner->p_input->p->b_out_pace_control )
973 msg_Dbg( p_dec, "switching to async mode" );
974 p_dec->p_owner->p_input->p->b_out_pace_control = true;
980 if( p_dec->fmt_in.i_cat == AUDIO_ES )
983 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
985 if( p_dec->p_owner->p_packetizer )
987 block_t *p_packetized_block;
988 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
990 while( (p_packetized_block =
991 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
993 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
995 es_format_Clean( &p_dec->fmt_in );
996 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
999 while( p_packetized_block )
1001 block_t *p_next = p_packetized_block->p_next;
1002 p_packetized_block->p_next = NULL;
1003 p_packetized_block->i_rate = i_rate;
1005 DecoderDecodeAudio( p_dec, p_packetized_block );
1007 p_packetized_block = p_next;
1013 DecoderDecodeAudio( p_dec, p_block );
1016 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1019 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
1021 if( p_dec->p_owner->p_packetizer )
1023 block_t *p_packetized_block;
1024 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
1026 while( (p_packetized_block =
1027 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1029 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1031 es_format_Clean( &p_dec->fmt_in );
1032 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1034 if( p_packetizer->pf_get_cc )
1035 DecoderGetCc( p_dec, p_packetizer );
1037 while( p_packetized_block )
1039 block_t *p_next = p_packetized_block->p_next;
1040 p_packetized_block->p_next = NULL;
1041 p_packetized_block->i_rate = i_rate;
1043 DecoderDecodeVideo( p_dec, p_packetized_block );
1045 p_packetized_block = p_next;
1051 DecoderDecodeVideo( p_dec, p_block );
1054 else if( p_dec->fmt_in.i_cat == SPU_ES )
1056 input_thread_t *p_input = p_dec->p_owner->p_input;
1057 vout_thread_t *p_vout;
1058 subpicture_t *p_spu;
1059 bool b_flushing = p_dec->p_owner->i_preroll_end == INT64_MAX;
1060 bool b_flush = false;
1064 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
1065 b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1068 if( !b_flushing && b_flush && p_sys->p_spu_vout )
1070 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1072 if( p_vout && p_sys->p_spu_vout == p_vout )
1073 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1074 p_dec->p_owner->i_spu_channel );
1077 vlc_object_release( p_vout );
1080 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1082 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1083 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1084 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1086 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1087 if( p_vout && p_sys->p_spu_vout == p_vout )
1089 /* Prerool does not work very well with subtitle */
1090 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
1091 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
1093 subpicture_Delete( p_spu );
1096 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1100 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1103 vlc_object_release( p_vout );
1108 msg_Err( p_dec, "unknown ES format" );
1112 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1116 * Destroys a decoder object
1118 * \param p_dec the decoder object
1121 static void DeleteDecoder( decoder_t * p_dec )
1123 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1124 (char*)&p_dec->fmt_in.i_codec,
1125 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
1127 /* Free all packets still in the decoder fifo. */
1128 block_FifoEmpty( p_dec->p_owner->p_fifo );
1129 block_FifoRelease( p_dec->p_owner->p_fifo );
1132 if( p_dec->p_owner->p_aout_input )
1133 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
1134 if( p_dec->p_owner->p_aout )
1136 vlc_object_release( p_dec->p_owner->p_aout );
1137 p_dec->p_owner->p_aout = NULL;
1139 if( p_dec->p_owner->p_vout )
1143 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1144 /* Hack to make sure all the the pictures are freed by the decoder */
1145 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1148 if( p_pic->i_status == RESERVED_PICTURE )
1149 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1150 if( p_pic->i_refcount > 0 )
1151 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1155 /* We are about to die. Reattach video output to p_vlc. */
1156 vout_Request( p_dec, p_dec->p_owner->p_vout, NULL );
1157 var_SetBool( p_dec->p_owner->p_input, "intf-change-vout", true );
1161 if( p_dec->p_owner->p_sout_input )
1163 sout_InputDelete( p_dec->p_owner->p_sout_input );
1164 es_format_Clean( &p_dec->p_owner->sout );
1168 if( p_dec->fmt_in.i_cat == SPU_ES )
1170 vout_thread_t *p_vout;
1172 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1175 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1176 p_dec->p_owner->i_spu_channel );
1177 vlc_object_release( p_vout );
1181 es_format_Clean( &p_dec->fmt_in );
1182 es_format_Clean( &p_dec->fmt_out );
1184 if( p_dec->p_owner->p_packetizer )
1186 module_unneed( p_dec->p_owner->p_packetizer,
1187 p_dec->p_owner->p_packetizer->p_module );
1188 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1189 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1190 vlc_object_detach( p_dec->p_owner->p_packetizer );
1191 vlc_object_release( p_dec->p_owner->p_packetizer );
1194 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1196 vlc_object_detach( p_dec );
1198 free( p_dec->p_owner );
1201 /*****************************************************************************
1202 * Buffers allocation callbacks for the decoders
1203 *****************************************************************************/
1204 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1206 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1207 aout_buffer_t *p_buffer;
1209 if( p_sys->p_aout_input != NULL &&
1210 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1211 p_dec->fmt_out.audio.i_original_channels !=
1212 p_sys->audio.i_original_channels ||
1213 p_dec->fmt_out.audio.i_bytes_per_frame !=
1214 p_sys->audio.i_bytes_per_frame ) )
1216 /* Parameters changed, restart the aout */
1217 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1218 p_sys->p_aout_input = NULL;
1221 if( p_sys->p_aout_input == NULL )
1223 audio_sample_format_t format;
1224 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1226 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1227 p_sys->audio = p_dec->fmt_out.audio;
1229 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1230 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1231 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1233 if ( i_force_dolby == 1 )
1235 format.i_original_channels = format.i_original_channels |
1236 AOUT_CHAN_DOLBYSTEREO;
1238 else /* i_force_dolby == 2 */
1240 format.i_original_channels = format.i_original_channels &
1241 ~AOUT_CHAN_DOLBYSTEREO;
1245 p_sys->p_aout_input =
1246 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1247 if( p_sys->p_aout_input == NULL )
1249 msg_Err( p_dec, "failed to create audio output" );
1250 p_dec->b_error = true;
1253 p_dec->fmt_out.audio.i_bytes_per_frame =
1254 p_sys->audio.i_bytes_per_frame;
1257 p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
1262 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1264 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1265 p_dec->p_owner->p_aout_input, p_buffer );
1269 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1271 static picture_t *vout_new_buffer( decoder_t *p_dec )
1273 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1276 if( p_sys->p_vout == NULL ||
1277 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1278 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1279 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1280 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1282 if( !p_dec->fmt_out.video.i_width ||
1283 !p_dec->fmt_out.video.i_height )
1285 /* Can't create a new vout without display size */
1289 if( !p_dec->fmt_out.video.i_visible_width ||
1290 !p_dec->fmt_out.video.i_visible_height )
1292 if( p_dec->fmt_in.video.i_visible_width &&
1293 p_dec->fmt_in.video.i_visible_height )
1295 p_dec->fmt_out.video.i_visible_width =
1296 p_dec->fmt_in.video.i_visible_width;
1297 p_dec->fmt_out.video.i_visible_height =
1298 p_dec->fmt_in.video.i_visible_height;
1302 p_dec->fmt_out.video.i_visible_width =
1303 p_dec->fmt_out.video.i_width;
1304 p_dec->fmt_out.video.i_visible_height =
1305 p_dec->fmt_out.video.i_height;
1309 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1310 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1312 p_dec->fmt_out.video.i_visible_height = 1080;
1313 p_dec->fmt_out.video.i_sar_num *= 135;
1314 p_dec->fmt_out.video.i_sar_den *= 136;
1315 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1318 if( !p_dec->fmt_out.video.i_sar_num ||
1319 !p_dec->fmt_out.video.i_sar_den )
1321 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1322 p_dec->fmt_out.video.i_visible_height;
1324 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1325 p_dec->fmt_out.video.i_visible_width;
1328 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1329 &p_dec->fmt_out.video.i_sar_den,
1330 p_dec->fmt_out.video.i_sar_num,
1331 p_dec->fmt_out.video.i_sar_den, 50000 );
1333 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1334 p_sys->video = p_dec->fmt_out.video;
1336 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1337 &p_dec->fmt_out.video );
1338 var_SetBool( p_sys->p_input, "intf-change-vout", true );
1339 if( p_sys->p_vout == NULL )
1341 msg_Err( p_dec, "failed to create video output" );
1342 p_dec->b_error = true;
1346 if( p_sys->video.i_rmask )
1347 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1348 if( p_sys->video.i_gmask )
1349 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1350 if( p_sys->video.i_bmask )
1351 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1354 /* Get a new picture
1356 for( p_pic = NULL; ; )
1358 int i_pic, i_ready_pic;
1360 if( p_dec->b_die || p_dec->b_error )
1363 /* The video filter chain required that there is always 1 free buffer
1364 * that it will use as temporary one. It will release the temporary
1365 * buffer once its work is done, so this check is safe even if we don't
1366 * lock around both count() and create().
1368 if( vout_CountPictureAvailable( p_sys->p_vout ) >= 2 )
1370 p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 );
1375 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1376 /* Check the decoder doesn't leak pictures */
1377 for( i_pic = 0, i_ready_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures; i_pic++ )
1379 if( p_pic->i_status == READY_PICTURE )
1382 /* If we have at least 2 ready pictures, wait for the vout thread to
1384 if( i_ready_pic >= 2 )
1390 if( p_pic->i_status == DISPLAYED_PICTURE )
1392 /* If at least one displayed picture is not referenced
1393 * let vout free it */
1394 if( p_pic->i_refcount == 0 )
1398 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1400 /* Too many pictures are still referenced, there is probably a bug
1401 * with the decoder */
1402 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1404 /* Just free all the pictures */
1405 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1408 if( p_pic->i_status == RESERVED_PICTURE )
1409 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1410 if( p_pic->i_refcount > 0 )
1411 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1416 msleep( VOUT_OUTMEM_SLEEP );
1422 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1424 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1427 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1429 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1432 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1434 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1437 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1439 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1440 vout_thread_t *p_vout = NULL;
1441 subpicture_t *p_subpic;
1442 int i_attempts = 30;
1444 while( i_attempts-- )
1446 if( p_dec->b_die || p_dec->b_error ) break;
1448 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1451 msleep( VOUT_DISPLAY_DELAY );
1456 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1460 if( p_sys->p_spu_vout != p_vout )
1462 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1463 &p_sys->i_spu_channel );
1464 p_sys->i_spu_order = 0;
1465 p_sys->p_spu_vout = p_vout;
1468 p_subpic = subpicture_New();
1471 p_subpic->i_channel = p_sys->i_spu_channel;
1472 p_subpic->i_order = p_sys->i_spu_order++;
1473 p_subpic->b_subtitle = true;
1476 vlc_object_release( p_vout );
1481 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1483 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1484 vout_thread_t *p_vout = NULL;
1486 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1487 if( !p_vout || p_sys->p_spu_vout != p_vout )
1490 vlc_object_release( p_vout );
1491 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1495 subpicture_Delete( p_subpic );
1497 vlc_object_release( p_vout );