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 );
49 static void DeleteDecoder( decoder_t * );
51 static int DecoderThread( decoder_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;
84 sout_instance_t *p_sout;
85 sout_packetizer_input_t *p_sout_input;
87 /* Some decoders require already packetized data (ie. not truncated) */
88 decoder_t *p_packetizer;
90 /* Current format in use by the output */
101 bool pb_cc_present[4];
106 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
108 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
109 "VLC probably does not support this sound or video format.",
111 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
112 _("VLC does not support the audio or video format \"%4.4s\". "
113 "Unfortunately there is no way for you to fix this."), (char*)&codec );
116 /* decoder_GetInputAttachment:
118 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
119 const char *psz_name )
121 input_attachment_t *p_attachment;
122 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
126 /* decoder_GetInputAttachments:
128 int decoder_GetInputAttachments( decoder_t *p_dec,
129 input_attachment_t ***ppp_attachment,
132 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
133 ppp_attachment, pi_attachment );
135 /* decoder_GetDisplayDate:
137 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
144 * Spawns a new decoder thread
146 * \param p_input the input thread
147 * \param p_es the es descriptor
148 * \return the spawned decoder object
150 decoder_t *input_DecoderNew( input_thread_t *p_input,
151 es_format_t *fmt, bool b_force_decoder )
153 decoder_t *p_dec = NULL;
157 (void)b_force_decoder;
159 /* If we are in sout mode, search for packetizer module */
160 if( p_input->p->p_sout && !b_force_decoder )
162 /* Create the decoder configuration structure */
163 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
166 msg_Err( p_input, "could not create packetizer" );
167 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
168 _("VLC could not open the packetizer module.") );
175 /* Create the decoder configuration structure */
176 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
179 msg_Err( p_input, "could not create decoder" );
180 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
181 _("VLC could not open the decoder module.") );
186 if( !p_dec->p_module )
188 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
190 DeleteDecoder( p_dec );
191 vlc_object_release( p_dec );
195 if( 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 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
331 if( p_dec->p_owner->p_packetizer && b_flush )
332 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
333 memset( p_null->p_buffer, 0, p_null->i_buffer );
335 input_DecoderDecode( p_dec, p_null );
338 bool input_DecoderEmpty( decoder_t * p_dec )
340 if( p_dec->p_owner->b_own_thread
341 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
348 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
352 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
353 for( i = 0; i < 4; i++ )
354 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
355 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
357 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
359 decoder_owner_sys_t *p_owner = p_dec->p_owner;
361 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
363 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
368 static const vlc_fourcc_t fcc[4] = {
369 VLC_FOURCC('c', 'c', '1', ' '),
370 VLC_FOURCC('c', 'c', '2', ' '),
371 VLC_FOURCC('c', 'c', '3', ' '),
372 VLC_FOURCC('c', 'c', '4', ' '),
377 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
378 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
381 msg_Err( p_dec, "could not create decoder" );
382 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
383 _("VLC could not open the decoder module.") );
386 else if( !p_cc->p_module )
388 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
389 DeleteDecoder( p_cc );
390 vlc_object_release( p_cc );
394 vlc_mutex_lock( &p_owner->lock_cc );
395 p_dec->p_owner->pp_cc[i_channel] = p_cc;
396 vlc_mutex_unlock( &p_owner->lock_cc );
402 vlc_mutex_lock( &p_owner->lock_cc );
403 p_cc = p_dec->p_owner->pp_cc[i_channel];
404 p_dec->p_owner->pp_cc[i_channel] = NULL;
405 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
409 vlc_object_kill( p_cc );
410 module_Unneed( p_cc, p_cc->p_module );
411 DeleteDecoder( p_cc );
412 vlc_object_release( p_cc );
417 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
419 decoder_owner_sys_t *p_owner = p_dec->p_owner;
422 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
425 vlc_mutex_lock( &p_owner->lock_cc );
426 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
427 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
432 * Create a decoder object
434 * \param p_input the input thread
435 * \param p_es the es descriptor
436 * \param i_object_type Object type as define in include/vlc_objects.h
437 * \return the decoder object
439 static decoder_t * CreateDecoder( input_thread_t *p_input,
440 es_format_t *fmt, int i_object_type )
443 decoder_owner_sys_t *p_owner;
446 p_dec = vlc_object_create( p_input, i_object_type );
450 p_dec->pf_decode_audio = 0;
451 p_dec->pf_decode_video = 0;
452 p_dec->pf_decode_sub = 0;
453 p_dec->pf_get_cc = 0;
454 p_dec->pf_packetize = 0;
456 /* Initialize the decoder fifo */
457 p_dec->p_module = NULL;
459 memset( &null_es_format, 0, sizeof(es_format_t) );
460 es_format_Copy( &p_dec->fmt_in, fmt );
461 es_format_Copy( &p_dec->fmt_out, &null_es_format );
463 /* Allocate our private structure for the decoder */
464 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
465 if( p_dec->p_owner == NULL )
467 p_dec->p_owner->b_own_thread = true;
468 p_dec->p_owner->i_preroll_end = -1;
469 p_dec->p_owner->p_input = p_input;
470 p_dec->p_owner->p_aout = NULL;
471 p_dec->p_owner->p_aout_input = NULL;
472 p_dec->p_owner->p_vout = NULL;
473 p_dec->p_owner->p_spu_vout = NULL;
474 p_dec->p_owner->i_spu_channel = 0;
475 p_dec->p_owner->p_sout = p_input->p->p_sout;
476 p_dec->p_owner->p_sout_input = NULL;
477 p_dec->p_owner->p_packetizer = NULL;
480 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
483 /* Set buffers allocation callbacks for the decoders */
484 p_dec->pf_aout_buffer_new = aout_new_buffer;
485 p_dec->pf_aout_buffer_del = aout_del_buffer;
486 p_dec->pf_vout_buffer_new = vout_new_buffer;
487 p_dec->pf_vout_buffer_del = vout_del_buffer;
488 p_dec->pf_picture_link = vout_link_picture;
489 p_dec->pf_picture_unlink = vout_unlink_picture;
490 p_dec->pf_spu_buffer_new = spu_new_buffer;
491 p_dec->pf_spu_buffer_del = spu_del_buffer;
493 vlc_object_attach( p_dec, p_input );
495 /* Find a suitable decoder/packetizer module */
496 if( i_object_type == VLC_OBJECT_DECODER )
497 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
499 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
501 /* Check if decoder requires already packetized data */
502 if( i_object_type == VLC_OBJECT_DECODER &&
503 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
505 p_dec->p_owner->p_packetizer =
506 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
507 if( p_dec->p_owner->p_packetizer )
509 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
512 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
515 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
517 p_dec->p_owner->p_packetizer->p_module =
518 module_Need( p_dec->p_owner->p_packetizer,
519 "packetizer", "$packetizer", 0 );
521 if( !p_dec->p_owner->p_packetizer->p_module )
523 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
524 vlc_object_detach( p_dec->p_owner->p_packetizer );
525 vlc_object_release( p_dec->p_owner->p_packetizer );
530 /* Copy ourself the input replay gain */
531 if( fmt->i_cat == AUDIO_ES )
533 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
535 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
537 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
538 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
540 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
542 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
543 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
548 p_owner->b_cc_supported = false;
549 if( i_object_type == VLC_OBJECT_DECODER )
551 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
552 p_owner->b_cc_supported = true;
553 if( p_dec->pf_get_cc )
554 p_owner->b_cc_supported = true;
557 vlc_mutex_init( &p_owner->lock_cc );
558 for( i = 0; i < 4; i++ )
560 p_owner->pb_cc_present[i] = false;
561 p_owner->pp_cc[i] = NULL;
567 * The decoding main loop
569 * \param p_dec the decoder
572 static int DecoderThread( decoder_t * p_dec )
576 /* The decoder's main loop */
577 while( !p_dec->b_die && !p_dec->b_error )
579 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
584 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
590 while( !p_dec->b_die )
592 /* Trash all received PES packets */
593 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
594 if( p_block ) block_Release( p_block );
597 /* We do it here because of the dll loader that wants close() in the
598 * same thread than open()/decode() */
599 module_Unneed( p_dec, p_dec->p_module );
604 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
606 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
607 *pi_preroll = INT64_MAX;
608 else if( p->i_pts > 0 )
609 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
610 else if( p->i_dts > 0 )
611 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
613 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
615 input_thread_t *p_input = p_dec->p_owner->p_input;
616 const int i_rate = p_block->i_rate;
617 aout_buffer_t *p_aout_buf;
619 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
621 vlc_mutex_lock( &p_input->p->counters.counters_lock );
622 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
623 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
625 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
627 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
628 p_dec->p_owner->p_aout_input, p_aout_buf );
632 if( p_dec->p_owner->i_preroll_end > 0 )
634 /* FIXME TODO flush audio output (don't know how to do that) */
635 msg_Dbg( p_dec, "End of audio preroll" );
636 p_dec->p_owner->i_preroll_end = -1;
638 aout_DecPlay( p_dec->p_owner->p_aout,
639 p_dec->p_owner->p_aout_input,
640 p_aout_buf, i_rate );
643 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
650 assert( p_dec_cc->pf_get_cc != NULL );
652 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
653 if( !p_dec->p_owner->b_cc_supported )
656 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
660 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
661 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
663 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
664 if( p_dec->p_owner->pp_cc[i] )
668 for( i = 0; i < 4; i++ )
670 if( !p_dec->p_owner->pp_cc[i] )
673 if( i_cc_decoder > 1 )
674 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
676 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
679 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
681 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
683 vlc_mutex_lock( &p_vout->picture_lock );
685 if( p_pic->i_status == READY_PICTURE )
687 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
690 else if( p_pic->i_refcount > 0 )
692 p_pic->i_status = DISPLAYED_PICTURE;
696 p_pic->i_status = DESTROYED_PICTURE;
697 p_vout->i_heap_size--;
700 vlc_mutex_unlock( &p_vout->picture_lock );
702 static void VoutFlushPicture( vout_thread_t *p_vout )
705 vlc_mutex_lock( &p_vout->picture_lock );
706 for( i = 0; i < p_vout->render.i_pictures; i++ )
708 picture_t *p_pic = p_vout->render.pp_picture[i];
710 if( p_pic->i_status == READY_PICTURE ||
711 p_pic->i_status == DISPLAYED_PICTURE )
713 /* We cannot change picture status if it is in READY_PICTURE state,
714 * Just make sure they won't be displayed */
718 vlc_mutex_unlock( &p_vout->picture_lock );
722 static void optimize_video_pts( decoder_t *p_dec )
724 picture_t * oldest_pict = NULL;
725 picture_t * youngest_pict = NULL;
728 input_thread_t * p_input = p_dec->p_owner->p_input;
729 vout_thread_t * p_vout = p_dec->p_owner->p_vout;
730 input_thread_private_t * p_priv = p_input->p;
732 /* Enable with --auto-adjust-pts-delay */
733 if( !p_priv->pts_adjust.auto_adjust ) return;
735 for( i = 0; i < I_RENDERPICTURES; i++ )
737 picture_t * pic = PP_RENDERPICTURE[i];
738 if( pic->i_status != READY_PICTURE )
741 if( !oldest_pict || pic->date < oldest_pict->date )
743 if( !youngest_pict || pic->date > youngest_pict->date )
747 if( !youngest_pict || !oldest_pict )
750 /* Try to find if we can reduce the pts
751 * This first draft is way to simple, and we can't say if the
752 * algo will converge. It's also full of constants.
753 * But this simple algo allows to reduce the latency
755 * The whole point of this, is to bypass the pts_delay set
756 * by the access but also the delay arbitraly set by
758 * Actually the remote server's muxer may set up a
759 * pts<->dts delay in the muxed stream. That is
760 * why we may end up in having a negative pts_delay,
761 * to compensate that artificial delay. */
762 mtime_t buffer_size = youngest_pict->date - oldest_pict->date;
763 int64_t pts_slide = 0;
764 if( buffer_size < 10000 )
766 if( p_priv->pts_adjust.i_num_faulty > 10 )
768 pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
769 p_priv->pts_adjust.i_num_faulty = 0;
771 if( p_priv->pts_adjust.to_high )
773 p_priv->pts_adjust.to_high = !p_priv->pts_adjust.to_high;
774 p_priv->pts_adjust.i_num_faulty = 0;
776 p_priv->pts_adjust.i_num_faulty++;
778 else if( buffer_size > 100000 )
780 if( p_priv->pts_adjust.i_num_faulty > 25 )
782 pts_slide = -buffer_size/2;
783 p_priv->pts_adjust.i_num_faulty = 0;
785 if( p_priv->pts_adjust.to_high )
787 p_priv->pts_adjust.to_high = !p_priv->pts_adjust.to_high;
788 p_priv->pts_adjust.i_num_faulty = 0;
790 p_priv->pts_adjust.i_num_faulty++;
794 mtime_t origi_delay = p_input->i_pts_delay;
796 p_input->i_pts_delay += pts_slide;
798 /* Don't play with the pts delay for more than -2<->3sec */
799 if( p_input->i_pts_delay < -2000000 )
800 p_input->i_pts_delay = -2000000;
801 else if( p_input->i_pts_delay > 3000000 )
802 p_input->i_pts_delay = 3000000;
803 pts_slide = p_input->i_pts_delay - origi_delay;
805 msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
806 (int)pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)buffer_size/1000);
808 vlc_mutex_lock( &p_vout->picture_lock );
809 /* Slide all the picture */
810 for( i = 0; i < I_RENDERPICTURES; i++ )
811 PP_RENDERPICTURE[i]->date += pts_slide;
812 /* FIXME: slide aout/spu */
813 vlc_mutex_unlock( &p_vout->picture_lock );
818 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
820 input_thread_t *p_input = p_dec->p_owner->p_input;
823 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
825 vlc_mutex_lock( &p_input->p->counters.counters_lock );
826 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
827 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
829 if( p_pic->date < p_dec->p_owner->i_preroll_end )
831 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
835 if( p_dec->p_owner->i_preroll_end > 0 )
837 msg_Dbg( p_dec, "End of video preroll" );
838 if( p_dec->p_owner->p_vout )
839 VoutFlushPicture( p_dec->p_owner->p_vout );
841 p_dec->p_owner->i_preroll_end = -1;
844 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
845 DecoderGetCc( p_dec, p_dec );
847 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
850 optimize_video_pts( p_dec );
852 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
859 * \param p_dec the decoder object
860 * \param p_block the block to decode
861 * \return VLC_SUCCESS or an error code
863 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
865 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
866 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
868 if( p_block && p_block->i_buffer <= 0 )
870 block_Release( p_block );
875 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
877 block_t *p_sout_block;
879 while( ( p_sout_block =
880 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
882 if( !p_dec->p_owner->p_sout_input )
884 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
886 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
887 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
888 if( p_dec->fmt_in.psz_language )
890 if( p_dec->p_owner->sout.psz_language )
891 free( p_dec->p_owner->sout.psz_language );
892 p_dec->p_owner->sout.psz_language =
893 strdup( p_dec->fmt_in.psz_language );
896 p_dec->p_owner->p_sout_input =
897 sout_InputNew( p_dec->p_owner->p_sout,
898 &p_dec->p_owner->sout );
900 if( p_dec->p_owner->p_sout_input == NULL )
902 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
903 (char *)&p_dec->p_owner->sout.i_codec );
904 p_dec->b_error = true;
906 while( p_sout_block )
908 block_t *p_next = p_sout_block->p_next;
909 block_Release( p_sout_block );
910 p_sout_block = p_next;
916 while( p_sout_block )
918 block_t *p_next = p_sout_block->p_next;
920 p_sout_block->p_next = NULL;
921 p_sout_block->i_rate = i_rate;
923 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
926 p_sout_block = p_next;
929 /* For now it's enough, as only sout impact on this flag */
930 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
931 p_dec->p_owner->p_input->p->b_out_pace_control )
933 msg_Dbg( p_dec, "switching to sync mode" );
934 p_dec->p_owner->p_input->p->b_out_pace_control = false;
936 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
937 !p_dec->p_owner->p_input->p->b_out_pace_control )
939 msg_Dbg( p_dec, "switching to async mode" );
940 p_dec->p_owner->p_input->p->b_out_pace_control = true;
946 if( p_dec->fmt_in.i_cat == AUDIO_ES )
949 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
951 if( p_dec->p_owner->p_packetizer )
953 block_t *p_packetized_block;
954 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
956 while( (p_packetized_block =
957 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
959 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
961 es_format_Clean( &p_dec->fmt_in );
962 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
965 while( p_packetized_block )
967 block_t *p_next = p_packetized_block->p_next;
968 p_packetized_block->p_next = NULL;
969 p_packetized_block->i_rate = i_rate;
971 DecoderDecodeAudio( p_dec, p_packetized_block );
973 p_packetized_block = p_next;
979 DecoderDecodeAudio( p_dec, p_block );
982 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
985 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
987 if( p_dec->p_owner->p_packetizer )
989 block_t *p_packetized_block;
990 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
992 while( (p_packetized_block =
993 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
995 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
997 es_format_Clean( &p_dec->fmt_in );
998 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1000 if( p_packetizer->pf_get_cc )
1001 DecoderGetCc( p_dec, p_packetizer );
1003 while( p_packetized_block )
1005 block_t *p_next = p_packetized_block->p_next;
1006 p_packetized_block->p_next = NULL;
1007 p_packetized_block->i_rate = i_rate;
1009 DecoderDecodeVideo( p_dec, p_packetized_block );
1011 p_packetized_block = p_next;
1017 DecoderDecodeVideo( p_dec, p_block );
1020 else if( p_dec->fmt_in.i_cat == SPU_ES )
1022 input_thread_t *p_input = p_dec->p_owner->p_input;
1023 vout_thread_t *p_vout;
1024 subpicture_t *p_spu;
1027 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
1029 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1031 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1032 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1033 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1035 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1036 if( p_vout && p_sys->p_spu_vout == p_vout )
1038 /* Prerool does not work very well with subtitle */
1039 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
1040 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
1041 spu_DestroySubpicture( p_vout->p_spu, p_spu );
1043 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1047 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1050 vlc_object_release( p_vout );
1055 msg_Err( p_dec, "unknown ES format" );
1059 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1063 * Destroys a decoder object
1065 * \param p_dec the decoder object
1068 static void DeleteDecoder( decoder_t * p_dec )
1070 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1071 (char*)&p_dec->fmt_in.i_codec,
1072 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
1074 /* Free all packets still in the decoder fifo. */
1075 block_FifoEmpty( p_dec->p_owner->p_fifo );
1076 block_FifoRelease( p_dec->p_owner->p_fifo );
1079 if( p_dec->p_owner->p_aout_input )
1080 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
1081 if( p_dec->p_owner->p_aout )
1083 vlc_object_release( p_dec->p_owner->p_aout );
1084 p_dec->p_owner->p_aout = NULL;
1086 if( p_dec->p_owner->p_vout )
1090 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1091 /* Hack to make sure all the the pictures are freed by the decoder */
1092 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1095 if( p_pic->i_status == RESERVED_PICTURE )
1096 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1097 if( p_pic->i_refcount > 0 )
1098 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1102 /* We are about to die. Reattach video output to p_vlc. */
1103 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
1107 if( p_dec->p_owner->p_sout_input )
1109 sout_InputDelete( p_dec->p_owner->p_sout_input );
1110 es_format_Clean( &p_dec->p_owner->sout );
1114 if( p_dec->fmt_in.i_cat == SPU_ES )
1116 vout_thread_t *p_vout;
1118 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1121 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1122 p_dec->p_owner->i_spu_channel );
1123 vlc_object_release( p_vout );
1127 es_format_Clean( &p_dec->fmt_in );
1128 es_format_Clean( &p_dec->fmt_out );
1130 if( p_dec->p_owner->p_packetizer )
1132 module_Unneed( p_dec->p_owner->p_packetizer,
1133 p_dec->p_owner->p_packetizer->p_module );
1134 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1135 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1136 vlc_object_detach( p_dec->p_owner->p_packetizer );
1137 vlc_object_release( p_dec->p_owner->p_packetizer );
1140 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1142 vlc_object_detach( p_dec );
1144 free( p_dec->p_owner );
1147 /*****************************************************************************
1148 * Buffers allocation callbacks for the decoders
1149 *****************************************************************************/
1150 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1152 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1153 aout_buffer_t *p_buffer;
1155 if( p_sys->p_aout_input != NULL &&
1156 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1157 p_dec->fmt_out.audio.i_original_channels !=
1158 p_sys->audio.i_original_channels ||
1159 p_dec->fmt_out.audio.i_bytes_per_frame !=
1160 p_sys->audio.i_bytes_per_frame ) )
1162 /* Parameters changed, restart the aout */
1163 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1164 p_sys->p_aout_input = NULL;
1167 if( p_sys->p_aout_input == NULL )
1169 audio_sample_format_t format;
1170 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1172 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1173 p_sys->audio = p_dec->fmt_out.audio;
1175 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1176 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1177 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1179 if ( i_force_dolby == 1 )
1181 format.i_original_channels = format.i_original_channels |
1182 AOUT_CHAN_DOLBYSTEREO;
1184 else /* i_force_dolby == 2 */
1186 format.i_original_channels = format.i_original_channels &
1187 ~AOUT_CHAN_DOLBYSTEREO;
1191 p_sys->p_aout_input =
1192 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1193 if( p_sys->p_aout_input == NULL )
1195 msg_Err( p_dec, "failed to create audio output" );
1196 p_dec->b_error = true;
1199 p_dec->fmt_out.audio.i_bytes_per_frame =
1200 p_sys->audio.i_bytes_per_frame;
1203 p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
1208 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1210 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1211 p_dec->p_owner->p_aout_input, p_buffer );
1214 static picture_t *vout_new_buffer( decoder_t *p_dec )
1216 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1219 if( p_sys->p_vout == NULL ||
1220 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1221 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1222 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1223 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1225 if( !p_dec->fmt_out.video.i_width ||
1226 !p_dec->fmt_out.video.i_height )
1228 /* Can't create a new vout without display size */
1232 if( !p_dec->fmt_out.video.i_visible_width ||
1233 !p_dec->fmt_out.video.i_visible_height )
1235 if( p_dec->fmt_in.video.i_visible_width &&
1236 p_dec->fmt_in.video.i_visible_height )
1238 p_dec->fmt_out.video.i_visible_width =
1239 p_dec->fmt_in.video.i_visible_width;
1240 p_dec->fmt_out.video.i_visible_height =
1241 p_dec->fmt_in.video.i_visible_height;
1245 p_dec->fmt_out.video.i_visible_width =
1246 p_dec->fmt_out.video.i_width;
1247 p_dec->fmt_out.video.i_visible_height =
1248 p_dec->fmt_out.video.i_height;
1252 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1253 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1255 p_dec->fmt_out.video.i_visible_height = 1080;
1256 p_dec->fmt_out.video.i_sar_num *= 135;
1257 p_dec->fmt_out.video.i_sar_den *= 136;
1258 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1261 if( !p_dec->fmt_out.video.i_sar_num ||
1262 !p_dec->fmt_out.video.i_sar_den )
1264 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1265 p_dec->fmt_out.video.i_visible_height;
1267 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1268 p_dec->fmt_out.video.i_visible_width;
1271 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1272 &p_dec->fmt_out.video.i_sar_den,
1273 p_dec->fmt_out.video.i_sar_num,
1274 p_dec->fmt_out.video.i_sar_den, 50000 );
1276 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1277 p_sys->video = p_dec->fmt_out.video;
1279 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1280 &p_dec->fmt_out.video );
1281 if( p_sys->p_vout == NULL )
1283 msg_Err( p_dec, "failed to create video output" );
1284 p_dec->b_error = true;
1288 if( p_sys->video.i_rmask )
1289 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1290 if( p_sys->video.i_gmask )
1291 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1292 if( p_sys->video.i_bmask )
1293 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1296 /* Get a new picture */
1297 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1299 int i_pic, i_ready_pic = 0;
1301 if( p_dec->b_die || p_dec->b_error )
1306 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1307 /* Check the decoder doesn't leak pictures */
1308 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1311 if( p_pic->i_status == READY_PICTURE )
1313 if( i_ready_pic++ > 0 ) break;
1317 if( p_pic->i_status != DISPLAYED_PICTURE &&
1318 p_pic->i_status != RESERVED_PICTURE &&
1319 p_pic->i_status != READY_PICTURE ) break;
1321 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1324 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1326 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1328 /* Just free all the pictures */
1329 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1332 if( p_pic->i_status == RESERVED_PICTURE )
1333 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1334 if( p_pic->i_refcount > 0 )
1335 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1340 msleep( VOUT_OUTMEM_SLEEP );
1346 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1348 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1351 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1353 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1356 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1358 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1361 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1363 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1364 vout_thread_t *p_vout = NULL;
1365 subpicture_t *p_subpic;
1366 int i_attempts = 30;
1368 while( i_attempts-- )
1370 if( p_dec->b_die || p_dec->b_error ) break;
1372 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1375 msleep( VOUT_DISPLAY_DELAY );
1380 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1384 if( p_sys->p_spu_vout != p_vout )
1386 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1387 &p_sys->i_spu_channel );
1388 p_sys->p_spu_vout = p_vout;
1391 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1394 p_subpic->i_channel = p_sys->i_spu_channel;
1397 vlc_object_release( p_vout );
1402 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1404 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1405 vout_thread_t *p_vout = NULL;
1407 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1408 if( !p_vout || p_sys->p_spu_vout != p_vout )
1411 vlc_object_release( p_vout );
1412 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1416 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1418 vlc_object_release( p_vout );