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;
76 aout_instance_t *p_aout;
77 aout_input_t *p_aout_input;
79 vout_thread_t *p_vout;
81 vout_thread_t *p_spu_vout;
85 sout_instance_t *p_sout;
86 sout_packetizer_input_t *p_sout_input;
88 /* Some decoders require already packetized data (ie. not truncated) */
89 decoder_t *p_packetizer;
91 /* Current format in use by the output */
102 bool pb_cc_present[4];
107 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
109 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
110 "VLC probably does not support this sound or video format.",
112 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
113 _("VLC does not support the audio or video format \"%4.4s\". "
114 "Unfortunately there is no way for you to fix this."), (char*)&codec );
117 /* decoder_GetInputAttachment:
119 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
120 const char *psz_name )
122 input_attachment_t *p_attachment;
123 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
127 /* decoder_GetInputAttachments:
129 int decoder_GetInputAttachments( decoder_t *p_dec,
130 input_attachment_t ***ppp_attachment,
133 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
134 ppp_attachment, pi_attachment );
136 /* decoder_GetDisplayDate:
138 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
145 * Spawns a new decoder thread
147 * \param p_input the input thread
148 * \param p_es the es descriptor
149 * \return the spawned decoder object
151 decoder_t *input_DecoderNew( input_thread_t *p_input,
152 es_format_t *fmt, sout_instance_t *p_sout )
154 decoder_t *p_dec = NULL;
158 (void)b_force_decoder;
160 /* If we are in sout mode, search for packetizer module */
163 /* Create the decoder configuration structure */
164 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
167 msg_Err( p_input, "could not create packetizer" );
168 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
169 _("VLC could not open the packetizer module.") );
176 /* Create the decoder configuration structure */
177 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
180 msg_Err( p_input, "could not create decoder" );
181 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
182 _("VLC could not open the decoder module.") );
187 if( !p_dec->p_module )
189 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
191 DeleteDecoder( p_dec );
192 vlc_object_release( p_dec );
196 if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
198 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
199 p_dec->p_owner->b_own_thread = false;
203 var_Get( p_input, "minimize-threads", &val );
204 p_dec->p_owner->b_own_thread = !val.b_bool;
207 if( p_dec->p_owner->b_own_thread )
210 if( fmt->i_cat == AUDIO_ES )
211 i_priority = VLC_THREAD_PRIORITY_AUDIO;
213 i_priority = VLC_THREAD_PRIORITY_VIDEO;
215 /* Spawn the decoder thread */
216 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
217 i_priority, false ) )
219 msg_Err( p_dec, "cannot spawn decoder thread" );
220 module_unneed( p_dec, p_dec->p_module );
221 DeleteDecoder( p_dec );
222 vlc_object_release( p_dec );
231 * Kills a decoder thread and waits until it's finished
233 * \param p_input the input thread
234 * \param p_es the es descriptor
237 void input_DecoderDelete( decoder_t *p_dec )
239 vlc_object_kill( p_dec );
241 if( p_dec->p_owner->b_own_thread )
243 /* Make sure the thread leaves the function by
244 * sending it an empty block. */
245 block_t *p_block = block_New( p_dec, 0 );
246 input_DecoderDecode( p_dec, p_block );
248 vlc_thread_join( p_dec );
250 /* Don't module_unneed() here because of the dll loader that wants
251 * close() in the same thread than open()/decode() */
256 input_DecoderDecode( p_dec, NULL );
258 module_unneed( p_dec, p_dec->p_module );
262 if( p_dec->p_owner->b_cc_supported )
265 for( i = 0; i < 4; i++ )
266 input_DecoderSetCcState( p_dec, false, i );
269 /* Delete decoder configuration */
270 DeleteDecoder( p_dec );
272 /* Delete the decoder */
273 vlc_object_release( p_dec );
277 * Put a block_t in the decoder's fifo.
279 * \param p_dec the decoder object
280 * \param p_block the data block
282 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
284 if( p_dec->p_owner->b_own_thread )
286 if( p_dec->p_owner->p_input->p->b_out_pace_control )
289 while( !p_dec->b_die && !p_dec->b_error &&
290 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
295 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
297 /* FIXME: ideally we would check the time amount of data
298 * in the fifo instead of its size. */
299 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
300 "consumed quickly enough), resetting fifo!" );
301 block_FifoEmpty( p_dec->p_owner->p_fifo );
304 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
308 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
310 if( p_block ) block_Release( p_block );
314 DecoderDecode( p_dec, p_block );
319 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
324 if( p_dec->p_owner->b_own_thread && b_flush )
325 block_FifoEmpty( p_dec->p_owner->p_fifo );
327 /* Send a special block */
328 p_null = block_New( p_dec, 128 );
329 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
330 if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
331 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
332 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
333 if( p_dec->p_owner->p_packetizer && b_flush )
334 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
335 memset( p_null->p_buffer, 0, p_null->i_buffer );
337 input_DecoderDecode( p_dec, p_null );
340 bool input_DecoderEmpty( decoder_t * p_dec )
342 if( p_dec->p_owner->b_own_thread
343 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
350 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
354 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
355 for( i = 0; i < 4; i++ )
356 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
357 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
359 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
361 decoder_owner_sys_t *p_owner = p_dec->p_owner;
363 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
365 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
370 static const vlc_fourcc_t fcc[4] = {
371 VLC_FOURCC('c', 'c', '1', ' '),
372 VLC_FOURCC('c', 'c', '2', ' '),
373 VLC_FOURCC('c', 'c', '3', ' '),
374 VLC_FOURCC('c', 'c', '4', ' '),
379 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
380 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_dec->p_owner->p_sout );
383 msg_Err( p_dec, "could not create decoder" );
384 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
385 _("VLC could not open the decoder module.") );
388 else if( !p_cc->p_module )
390 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
391 DeleteDecoder( p_cc );
392 vlc_object_release( p_cc );
396 vlc_mutex_lock( &p_owner->lock_cc );
397 p_dec->p_owner->pp_cc[i_channel] = p_cc;
398 vlc_mutex_unlock( &p_owner->lock_cc );
404 vlc_mutex_lock( &p_owner->lock_cc );
405 p_cc = p_dec->p_owner->pp_cc[i_channel];
406 p_dec->p_owner->pp_cc[i_channel] = NULL;
407 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
411 vlc_object_kill( p_cc );
412 module_unneed( p_cc, p_cc->p_module );
413 DeleteDecoder( p_cc );
414 vlc_object_release( p_cc );
419 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
421 decoder_owner_sys_t *p_owner = p_dec->p_owner;
424 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
427 vlc_mutex_lock( &p_owner->lock_cc );
428 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
429 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
434 * Create a decoder object
436 * \param p_input the input thread
437 * \param p_es the es descriptor
438 * \param i_object_type Object type as define in include/vlc_objects.h
439 * \return the decoder object
441 static decoder_t * CreateDecoder( input_thread_t *p_input,
442 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
445 decoder_owner_sys_t *p_owner;
448 p_dec = vlc_object_create( p_input, i_object_type );
452 p_dec->pf_decode_audio = NULL;
453 p_dec->pf_decode_video = NULL;
454 p_dec->pf_decode_sub = NULL;
455 p_dec->pf_get_cc = NULL;
456 p_dec->pf_packetize = NULL;
458 /* Initialize the decoder fifo */
459 p_dec->p_module = NULL;
461 memset( &null_es_format, 0, sizeof(es_format_t) );
462 es_format_Copy( &p_dec->fmt_in, fmt );
463 es_format_Copy( &p_dec->fmt_out, &null_es_format );
465 /* Allocate our private structure for the decoder */
466 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
467 if( p_dec->p_owner == NULL )
469 vlc_object_release( p_dec );
472 p_dec->p_owner->b_own_thread = true;
473 p_dec->p_owner->i_preroll_end = -1;
474 p_dec->p_owner->p_input = p_input;
475 p_dec->p_owner->p_aout = NULL;
476 p_dec->p_owner->p_aout_input = NULL;
477 p_dec->p_owner->p_vout = NULL;
478 p_dec->p_owner->p_spu_vout = NULL;
479 p_dec->p_owner->i_spu_channel = 0;
480 p_dec->p_owner->i_spu_order = 0;
481 p_dec->p_owner->p_sout = p_sout;
482 p_dec->p_owner->p_sout_input = NULL;
483 p_dec->p_owner->p_packetizer = NULL;
486 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
488 free( p_dec->p_owner );
489 vlc_object_release( p_dec );
493 /* Set buffers allocation callbacks for the decoders */
494 p_dec->pf_aout_buffer_new = aout_new_buffer;
495 p_dec->pf_aout_buffer_del = aout_del_buffer;
496 p_dec->pf_vout_buffer_new = vout_new_buffer;
497 p_dec->pf_vout_buffer_del = vout_del_buffer;
498 p_dec->pf_picture_link = vout_link_picture;
499 p_dec->pf_picture_unlink = vout_unlink_picture;
500 p_dec->pf_spu_buffer_new = spu_new_buffer;
501 p_dec->pf_spu_buffer_del = spu_del_buffer;
503 vlc_object_attach( p_dec, p_input );
505 /* Find a suitable decoder/packetizer module */
506 if( i_object_type == VLC_OBJECT_DECODER )
507 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
509 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
511 /* Check if decoder requires already packetized data */
512 if( i_object_type == VLC_OBJECT_DECODER &&
513 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
515 p_dec->p_owner->p_packetizer =
516 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
517 if( p_dec->p_owner->p_packetizer )
519 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
522 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
525 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
527 p_dec->p_owner->p_packetizer->p_module =
528 module_need( p_dec->p_owner->p_packetizer,
529 "packetizer", "$packetizer", 0 );
531 if( !p_dec->p_owner->p_packetizer->p_module )
533 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
534 vlc_object_detach( p_dec->p_owner->p_packetizer );
535 vlc_object_release( p_dec->p_owner->p_packetizer );
540 /* Copy ourself the input replay gain */
541 if( fmt->i_cat == AUDIO_ES )
543 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
545 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
547 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
548 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
550 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
552 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
553 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
558 p_owner->b_cc_supported = false;
559 if( i_object_type == VLC_OBJECT_DECODER )
561 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
562 p_owner->b_cc_supported = true;
563 if( p_dec->pf_get_cc )
564 p_owner->b_cc_supported = true;
567 vlc_mutex_init( &p_owner->lock_cc );
568 for( i = 0; i < 4; i++ )
570 p_owner->pb_cc_present[i] = false;
571 p_owner->pp_cc[i] = NULL;
577 * The decoding main loop
579 * \param p_dec the decoder
581 static void* DecoderThread( vlc_object_t *p_this )
583 decoder_t * p_dec = (decoder_t *)p_this;
585 int canc = vlc_savecancel ();
587 /* The decoder's main loop */
588 while( !p_dec->b_die && !p_dec->b_error )
590 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
595 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
601 while( !p_dec->b_die )
603 /* Trash all received PES packets */
604 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
605 if( p_block ) block_Release( p_block );
608 /* We do it here because of the dll loader that wants close() in the
609 * same thread than open()/decode() */
610 module_unneed( p_dec, p_dec->p_module );
611 vlc_restorecancel (canc);
615 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
617 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
618 *pi_preroll = INT64_MAX;
619 else if( p->i_pts > 0 )
620 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
621 else if( p->i_dts > 0 )
622 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
624 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
626 input_thread_t *p_input = p_dec->p_owner->p_input;
627 const int i_rate = p_block->i_rate;
628 aout_buffer_t *p_aout_buf;
630 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
632 aout_instance_t *p_aout = p_dec->p_owner->p_aout;
633 aout_input_t *p_aout_input = p_dec->p_owner->p_aout_input;
637 /* It prevent freezing VLC in case of broken decoder */
638 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
640 block_Release( p_block );
643 vlc_mutex_lock( &p_input->p->counters.counters_lock );
644 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
645 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
647 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
649 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
653 if( p_dec->p_owner->i_preroll_end > 0 )
655 /* FIXME TODO flush audio output (don't know how to do that) */
656 msg_Dbg( p_dec, "End of audio preroll" );
657 p_dec->p_owner->i_preroll_end = -1;
659 aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate );
662 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
669 assert( p_dec_cc->pf_get_cc != NULL );
671 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
672 if( !p_dec->p_owner->b_cc_supported )
675 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
679 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
680 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
682 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
683 if( p_dec->p_owner->pp_cc[i] )
687 for( i = 0; i < 4; i++ )
689 if( !p_dec->p_owner->pp_cc[i] )
692 if( i_cc_decoder > 1 )
693 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
695 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
698 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
700 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
702 vlc_mutex_lock( &p_vout->picture_lock );
704 if( p_pic->i_status == READY_PICTURE )
706 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
709 else if( p_pic->i_refcount > 0 )
711 p_pic->i_status = DISPLAYED_PICTURE;
715 p_pic->i_status = DESTROYED_PICTURE;
716 picture_CleanupQuant( p_pic );
717 p_vout->i_heap_size--;
720 vlc_mutex_unlock( &p_vout->picture_lock );
722 static void VoutFlushPicture( vout_thread_t *p_vout )
725 vlc_mutex_lock( &p_vout->picture_lock );
726 for( i = 0; i < p_vout->render.i_pictures; i++ )
728 picture_t *p_pic = p_vout->render.pp_picture[i];
730 if( p_pic->i_status == READY_PICTURE ||
731 p_pic->i_status == DISPLAYED_PICTURE )
733 /* We cannot change picture status if it is in READY_PICTURE state,
734 * Just make sure they won't be displayed */
738 vlc_mutex_unlock( &p_vout->picture_lock );
741 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
743 input_thread_t *p_input = p_dec->p_owner->p_input;
744 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
745 input_thread_private_t *p_priv = p_input->p;
747 picture_t *p_old = NULL;
748 picture_t *p_young = NULL;
751 /* Enable with --auto-adjust-pts-delay */
752 if( !p_priv->pts_adjust.b_auto_adjust )
755 for( i = 0; i < I_RENDERPICTURES; i++ )
757 picture_t *p_pic = PP_RENDERPICTURE[i];
759 if( p_pic->i_status != READY_PICTURE )
762 if( !p_old || p_pic->date < p_old->date )
764 if( !p_young || p_pic->date > p_young->date )
768 if( !p_young || !p_old )
771 /* Try to find if we can reduce the pts
772 * This first draft is way to simple, and we can't say if the
773 * algo will converge. It's also full of constants.
774 * But this simple algo allows to reduce the latency
776 * The whole point of this, is to bypass the pts_delay set
777 * by the access but also the delay arbitraly set by
779 * Actually the remote server's muxer may set up a
780 * pts<->dts delay in the muxed stream. That is
781 * why we may end up in having a negative pts_delay,
782 * to compensate that artificial delay. */
783 const mtime_t i_buffer_length = p_young->date - p_old->date;
784 int64_t i_pts_slide = 0;
785 if( i_buffer_length < 10000 )
787 if( p_priv->pts_adjust.i_num_faulty > 10 )
789 i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
790 p_priv->pts_adjust.i_num_faulty = 0;
792 if( p_priv->pts_adjust.b_to_high )
794 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
795 p_priv->pts_adjust.i_num_faulty = 0;
797 p_priv->pts_adjust.i_num_faulty++;
799 else if( i_buffer_length > 100000 )
801 if( p_priv->pts_adjust.i_num_faulty > 25 )
803 i_pts_slide = -i_buffer_length/2;
804 p_priv->pts_adjust.i_num_faulty = 0;
806 if( p_priv->pts_adjust.b_to_high )
808 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
809 p_priv->pts_adjust.i_num_faulty = 0;
811 p_priv->pts_adjust.i_num_faulty++;
813 if( i_pts_slide != 0 )
815 const mtime_t i_pts_delay_org = p_input->i_pts_delay;
817 p_input->i_pts_delay += i_pts_slide;
819 /* Don't play with the pts delay for more than -2<->3sec */
820 if( p_input->i_pts_delay < -2000000 )
821 p_input->i_pts_delay = -2000000;
822 else if( p_input->i_pts_delay > 3000000 )
823 p_input->i_pts_delay = 3000000;
824 i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
826 msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
827 (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
829 vlc_mutex_lock( &p_vout->picture_lock );
830 /* Slide all the picture */
831 for( i = 0; i < I_RENDERPICTURES; i++ )
832 PP_RENDERPICTURE[i]->date += i_pts_slide;
833 /* FIXME: slide aout/spu */
834 vlc_mutex_unlock( &p_vout->picture_lock );
838 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
840 input_thread_t *p_input = p_dec->p_owner->p_input;
843 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
845 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
848 /* It prevent freezing VLC in case of broken decoder */
849 VoutDisplayedPicture( p_vout, p_pic );
851 block_Release( p_block );
855 vlc_mutex_lock( &p_input->p->counters.counters_lock );
856 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
857 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
859 if( p_pic->date < p_dec->p_owner->i_preroll_end )
861 VoutDisplayedPicture( p_vout, p_pic );
865 if( p_dec->p_owner->i_preroll_end > 0 )
867 msg_Dbg( p_dec, "End of video preroll" );
869 VoutFlushPicture( p_vout );
871 p_dec->p_owner->i_preroll_end = -1;
874 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
875 DecoderGetCc( p_dec, p_dec );
877 vout_DatePicture( p_vout, p_pic, p_pic->date );
879 DecoderOptimizePtsDelay( p_dec );
881 vout_DisplayPicture( p_vout, p_pic );
888 * \param p_dec the decoder object
889 * \param p_block the block to decode
890 * \return VLC_SUCCESS or an error code
892 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
894 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
895 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
897 if( p_block && p_block->i_buffer <= 0 )
899 block_Release( p_block );
904 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
906 block_t *p_sout_block;
908 while( ( p_sout_block =
909 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
911 if( !p_dec->p_owner->p_sout_input )
913 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
915 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
916 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
917 if( p_dec->fmt_in.psz_language )
919 if( p_dec->p_owner->sout.psz_language )
920 free( p_dec->p_owner->sout.psz_language );
921 p_dec->p_owner->sout.psz_language =
922 strdup( p_dec->fmt_in.psz_language );
925 p_dec->p_owner->p_sout_input =
926 sout_InputNew( p_dec->p_owner->p_sout,
927 &p_dec->p_owner->sout );
929 if( p_dec->p_owner->p_sout_input == NULL )
931 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
932 (char *)&p_dec->p_owner->sout.i_codec );
933 p_dec->b_error = true;
935 while( p_sout_block )
937 block_t *p_next = p_sout_block->p_next;
938 block_Release( p_sout_block );
939 p_sout_block = p_next;
945 while( p_sout_block )
947 block_t *p_next = p_sout_block->p_next;
949 p_sout_block->p_next = NULL;
950 p_sout_block->i_rate = i_rate;
952 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
955 p_sout_block = p_next;
958 /* For now it's enough, as only sout impact on this flag */
959 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
960 p_dec->p_owner->p_input->p->b_out_pace_control )
962 msg_Dbg( p_dec, "switching to sync mode" );
963 p_dec->p_owner->p_input->p->b_out_pace_control = false;
965 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
966 !p_dec->p_owner->p_input->p->b_out_pace_control )
968 msg_Dbg( p_dec, "switching to async mode" );
969 p_dec->p_owner->p_input->p->b_out_pace_control = true;
975 if( p_dec->fmt_in.i_cat == AUDIO_ES )
978 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
980 if( p_dec->p_owner->p_packetizer )
982 block_t *p_packetized_block;
983 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
985 while( (p_packetized_block =
986 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
988 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
990 es_format_Clean( &p_dec->fmt_in );
991 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
994 while( p_packetized_block )
996 block_t *p_next = p_packetized_block->p_next;
997 p_packetized_block->p_next = NULL;
998 p_packetized_block->i_rate = i_rate;
1000 DecoderDecodeAudio( p_dec, p_packetized_block );
1002 p_packetized_block = p_next;
1008 DecoderDecodeAudio( p_dec, p_block );
1011 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1014 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
1016 if( p_dec->p_owner->p_packetizer )
1018 block_t *p_packetized_block;
1019 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
1021 while( (p_packetized_block =
1022 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1024 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1026 es_format_Clean( &p_dec->fmt_in );
1027 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1029 if( p_packetizer->pf_get_cc )
1030 DecoderGetCc( p_dec, p_packetizer );
1032 while( p_packetized_block )
1034 block_t *p_next = p_packetized_block->p_next;
1035 p_packetized_block->p_next = NULL;
1036 p_packetized_block->i_rate = i_rate;
1038 DecoderDecodeVideo( p_dec, p_packetized_block );
1040 p_packetized_block = p_next;
1046 DecoderDecodeVideo( p_dec, p_block );
1049 else if( p_dec->fmt_in.i_cat == SPU_ES )
1051 input_thread_t *p_input = p_dec->p_owner->p_input;
1052 vout_thread_t *p_vout;
1053 subpicture_t *p_spu;
1054 bool b_flushing = p_dec->p_owner->i_preroll_end == INT64_MAX;
1055 bool b_flush = false;
1059 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
1060 b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1063 if( !b_flushing && b_flush && p_sys->p_spu_vout )
1065 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1067 if( p_vout && p_sys->p_spu_vout == p_vout )
1068 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1069 p_dec->p_owner->i_spu_channel );
1072 vlc_object_release( p_vout );
1075 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1077 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1078 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1079 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1081 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1082 if( p_vout && p_sys->p_spu_vout == p_vout )
1084 /* Prerool does not work very well with subtitle */
1085 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
1086 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
1088 subpicture_Delete( p_spu );
1091 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1095 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1098 vlc_object_release( p_vout );
1103 msg_Err( p_dec, "unknown ES format" );
1107 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1111 * Destroys a decoder object
1113 * \param p_dec the decoder object
1116 static void DeleteDecoder( decoder_t * p_dec )
1118 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1119 (char*)&p_dec->fmt_in.i_codec,
1120 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
1122 /* Free all packets still in the decoder fifo. */
1123 block_FifoEmpty( p_dec->p_owner->p_fifo );
1124 block_FifoRelease( p_dec->p_owner->p_fifo );
1127 if( p_dec->p_owner->p_aout_input )
1128 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
1129 if( p_dec->p_owner->p_aout )
1131 vlc_object_release( p_dec->p_owner->p_aout );
1132 p_dec->p_owner->p_aout = NULL;
1134 if( p_dec->p_owner->p_vout )
1138 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1139 /* Hack to make sure all the the pictures are freed by the decoder */
1140 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1143 if( p_pic->i_status == RESERVED_PICTURE )
1144 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1145 if( p_pic->i_refcount > 0 )
1146 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1150 /* We are about to die. Reattach video output to p_vlc. */
1151 vout_Request( p_dec, p_dec->p_owner->p_vout, NULL );
1152 var_SetBool( p_dec->p_owner->p_input, "intf-change-vout", true );
1156 if( p_dec->p_owner->p_sout_input )
1158 sout_InputDelete( p_dec->p_owner->p_sout_input );
1159 es_format_Clean( &p_dec->p_owner->sout );
1163 if( p_dec->fmt_in.i_cat == SPU_ES )
1165 vout_thread_t *p_vout;
1167 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1170 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1171 p_dec->p_owner->i_spu_channel );
1172 vlc_object_release( p_vout );
1176 es_format_Clean( &p_dec->fmt_in );
1177 es_format_Clean( &p_dec->fmt_out );
1179 if( p_dec->p_owner->p_packetizer )
1181 module_unneed( p_dec->p_owner->p_packetizer,
1182 p_dec->p_owner->p_packetizer->p_module );
1183 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1184 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1185 vlc_object_detach( p_dec->p_owner->p_packetizer );
1186 vlc_object_release( p_dec->p_owner->p_packetizer );
1189 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1191 vlc_object_detach( p_dec );
1193 free( p_dec->p_owner );
1196 /*****************************************************************************
1197 * Buffers allocation callbacks for the decoders
1198 *****************************************************************************/
1199 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1201 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1202 aout_buffer_t *p_buffer;
1204 if( p_sys->p_aout_input != NULL &&
1205 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1206 p_dec->fmt_out.audio.i_original_channels !=
1207 p_sys->audio.i_original_channels ||
1208 p_dec->fmt_out.audio.i_bytes_per_frame !=
1209 p_sys->audio.i_bytes_per_frame ) )
1211 /* Parameters changed, restart the aout */
1212 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1213 p_sys->p_aout_input = NULL;
1216 if( p_sys->p_aout_input == NULL )
1218 audio_sample_format_t format;
1219 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1221 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1222 p_sys->audio = p_dec->fmt_out.audio;
1224 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1225 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1226 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1228 if ( i_force_dolby == 1 )
1230 format.i_original_channels = format.i_original_channels |
1231 AOUT_CHAN_DOLBYSTEREO;
1233 else /* i_force_dolby == 2 */
1235 format.i_original_channels = format.i_original_channels &
1236 ~AOUT_CHAN_DOLBYSTEREO;
1240 p_sys->p_aout_input =
1241 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1242 if( p_sys->p_aout_input == NULL )
1244 msg_Err( p_dec, "failed to create audio output" );
1245 p_dec->b_error = true;
1248 p_dec->fmt_out.audio.i_bytes_per_frame =
1249 p_sys->audio.i_bytes_per_frame;
1252 p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
1257 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1259 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1260 p_dec->p_owner->p_aout_input, p_buffer );
1264 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1266 static picture_t *vout_new_buffer( decoder_t *p_dec )
1268 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1271 if( p_sys->p_vout == NULL ||
1272 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1273 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1274 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1275 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1277 if( !p_dec->fmt_out.video.i_width ||
1278 !p_dec->fmt_out.video.i_height )
1280 /* Can't create a new vout without display size */
1284 if( !p_dec->fmt_out.video.i_visible_width ||
1285 !p_dec->fmt_out.video.i_visible_height )
1287 if( p_dec->fmt_in.video.i_visible_width &&
1288 p_dec->fmt_in.video.i_visible_height )
1290 p_dec->fmt_out.video.i_visible_width =
1291 p_dec->fmt_in.video.i_visible_width;
1292 p_dec->fmt_out.video.i_visible_height =
1293 p_dec->fmt_in.video.i_visible_height;
1297 p_dec->fmt_out.video.i_visible_width =
1298 p_dec->fmt_out.video.i_width;
1299 p_dec->fmt_out.video.i_visible_height =
1300 p_dec->fmt_out.video.i_height;
1304 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1305 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1307 p_dec->fmt_out.video.i_visible_height = 1080;
1308 p_dec->fmt_out.video.i_sar_num *= 135;
1309 p_dec->fmt_out.video.i_sar_den *= 136;
1310 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1313 if( !p_dec->fmt_out.video.i_sar_num ||
1314 !p_dec->fmt_out.video.i_sar_den )
1316 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1317 p_dec->fmt_out.video.i_visible_height;
1319 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1320 p_dec->fmt_out.video.i_visible_width;
1323 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1324 &p_dec->fmt_out.video.i_sar_den,
1325 p_dec->fmt_out.video.i_sar_num,
1326 p_dec->fmt_out.video.i_sar_den, 50000 );
1328 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1329 p_sys->video = p_dec->fmt_out.video;
1331 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1332 &p_dec->fmt_out.video );
1333 var_SetBool( p_sys->p_input, "intf-change-vout", true );
1334 if( p_sys->p_vout == NULL )
1336 msg_Err( p_dec, "failed to create video output" );
1337 p_dec->b_error = true;
1341 if( p_sys->video.i_rmask )
1342 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1343 if( p_sys->video.i_gmask )
1344 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1345 if( p_sys->video.i_bmask )
1346 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1349 /* Get a new picture
1351 for( p_pic = NULL; ; )
1353 int i_pic, i_ready_pic;
1355 if( p_dec->b_die || p_dec->b_error )
1358 /* The video filter chain required that there is always 1 free buffer
1359 * that it will use as temporary one. It will release the temporary
1360 * buffer once its work is done, so this check is safe even if we don't
1361 * lock around both count() and create().
1363 if( vout_CountPictureAvailable( p_sys->p_vout ) >= 2 )
1365 p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 );
1370 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1371 /* Check the decoder doesn't leak pictures */
1372 for( i_pic = 0, i_ready_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures; i_pic++ )
1374 if( p_pic->i_status == READY_PICTURE )
1377 /* If we have at least 2 ready pictures, wait for the vout thread to
1379 if( i_ready_pic >= 2 )
1385 if( p_pic->i_status == DISPLAYED_PICTURE )
1387 /* If at least one displayed picture is not referenced
1388 * let vout free it */
1389 if( p_pic->i_refcount == 0 )
1393 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1395 /* Too many pictures are still referenced, there is probably a bug
1396 * with the decoder */
1397 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1399 /* Just free all the pictures */
1400 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1403 if( p_pic->i_status == RESERVED_PICTURE )
1404 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1405 if( p_pic->i_refcount > 0 )
1406 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1411 msleep( VOUT_OUTMEM_SLEEP );
1417 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1419 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1422 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1424 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1427 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1429 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1432 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1434 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1435 vout_thread_t *p_vout = NULL;
1436 subpicture_t *p_subpic;
1437 int i_attempts = 30;
1439 while( i_attempts-- )
1441 if( p_dec->b_die || p_dec->b_error ) break;
1443 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1446 msleep( VOUT_DISPLAY_DELAY );
1451 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1455 if( p_sys->p_spu_vout != p_vout )
1457 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1458 &p_sys->i_spu_channel );
1459 p_sys->i_spu_order = 0;
1460 p_sys->p_spu_vout = p_vout;
1463 p_subpic = subpicture_New();
1466 p_subpic->i_channel = p_sys->i_spu_channel;
1467 p_subpic->i_order = p_sys->i_spu_order++;
1468 p_subpic->b_subtitle = true;
1471 vlc_object_release( p_vout );
1476 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1478 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1479 vout_thread_t *p_vout = NULL;
1481 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1482 if( !p_vout || p_sys->p_spu_vout != p_vout )
1485 vlc_object_release( p_vout );
1486 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1490 subpicture_Delete( p_subpic );
1492 vlc_object_release( p_vout );