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"
47 #include "input_clock.h"
48 #include "input_decoder.h"
50 #include "../video_output/vout_internal.h"
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void DeleteDecoder( decoder_t * );
55 static void *DecoderThread( vlc_object_t * );
56 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
57 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date );
59 /* Buffers allocation callbacks for the decoders */
60 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
61 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
63 static picture_t *vout_new_buffer( decoder_t * );
64 static void vout_del_buffer( decoder_t *, picture_t * );
65 static void vout_link_picture( decoder_t *, picture_t * );
66 static void vout_unlink_picture( decoder_t *, picture_t * );
68 static subpicture_t *spu_new_buffer( decoder_t * );
69 static void spu_del_buffer( decoder_t *, subpicture_t * );
71 static es_format_t null_es_format;
73 struct decoder_owner_sys_t
77 int64_t i_preroll_end;
79 input_thread_t *p_input;
80 input_clock_t *p_clock;
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 */
100 /* Lock for communication with decoder thread */
104 /* -- These variables need locking on write(only) -- */
105 aout_instance_t *p_aout;
106 aout_input_t *p_aout_input;
108 vout_thread_t *p_vout;
110 /* -- Theses variables need locking on read *and* write -- */
113 mtime_t i_pause_date;
117 bool pb_cc_present[4];
125 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
127 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
128 "VLC probably does not support this sound or video format.",
130 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
131 _("VLC does not support the audio or video format \"%4.4s\". "
132 "Unfortunately there is no way for you to fix this."), (char*)&codec );
135 /* decoder_GetInputAttachment:
137 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
138 const char *psz_name )
140 input_attachment_t *p_attachment;
141 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
145 /* decoder_GetInputAttachments:
147 int decoder_GetInputAttachments( decoder_t *p_dec,
148 input_attachment_t ***ppp_attachment,
151 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
152 ppp_attachment, pi_attachment );
154 /* decoder_GetDisplayDate:
156 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
158 decoder_owner_sys_t *p_owner = p_dec->p_owner;
160 if( !p_owner->p_clock )
162 return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
164 /* decoder_GetDisplayRate:
166 int decoder_GetDisplayRate( decoder_t *p_dec )
168 decoder_owner_sys_t *p_owner = p_dec->p_owner;
170 if( !p_owner->p_clock )
171 return INPUT_RATE_DEFAULT;
172 return input_clock_GetRate( p_owner->p_clock );
176 * Spawns a new decoder thread
178 * \param p_input the input thread
179 * \param p_es the es descriptor
180 * \return the spawned decoder object
182 decoder_t *input_DecoderNew( input_thread_t *p_input,
183 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
185 decoder_t *p_dec = NULL;
189 (void)b_force_decoder;
191 /* If we are in sout mode, search for packetizer module */
194 /* Create the decoder configuration structure */
195 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
198 msg_Err( p_input, "could not create packetizer" );
199 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
200 _("VLC could not open the packetizer module.") );
207 /* Create the decoder configuration structure */
208 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
211 msg_Err( p_input, "could not create decoder" );
212 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
213 _("VLC could not open the decoder module.") );
218 if( !p_dec->p_module )
220 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
222 DeleteDecoder( p_dec );
223 vlc_object_release( p_dec );
227 p_dec->p_owner->p_clock = p_clock;
229 if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
231 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
232 p_dec->p_owner->b_own_thread = false;
236 var_Get( p_input, "minimize-threads", &val );
237 p_dec->p_owner->b_own_thread = !val.b_bool;
240 if( p_dec->p_owner->b_own_thread )
243 if( fmt->i_cat == AUDIO_ES )
244 i_priority = VLC_THREAD_PRIORITY_AUDIO;
246 i_priority = VLC_THREAD_PRIORITY_VIDEO;
248 /* Spawn the decoder thread */
249 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
250 i_priority, false ) )
252 msg_Err( p_dec, "cannot spawn decoder thread" );
253 module_unneed( p_dec, p_dec->p_module );
254 DeleteDecoder( p_dec );
255 vlc_object_release( p_dec );
264 * Kills a decoder thread and waits until it's finished
266 * \param p_input the input thread
267 * \param p_es the es descriptor
270 void input_DecoderDelete( decoder_t *p_dec )
272 decoder_owner_sys_t *p_owner = p_dec->p_owner;
274 vlc_object_kill( p_dec );
276 if( p_owner->b_own_thread )
278 /* Make sure we aren't paused anymore */
279 vlc_mutex_lock( &p_owner->lock );
280 if( p_owner->b_paused )
282 p_owner->b_paused = false;
283 vlc_cond_signal( &p_owner->wait );
285 vlc_mutex_unlock( &p_owner->lock );
287 /* Make sure the thread leaves the function by
288 * sending it an empty block. */
289 block_t *p_block = block_New( p_dec, 0 );
290 input_DecoderDecode( p_dec, p_block );
292 vlc_thread_join( p_dec );
294 /* Don't module_unneed() here because of the dll loader that wants
295 * close() in the same thread than open()/decode() */
300 input_DecoderDecode( p_dec, NULL );
302 module_unneed( p_dec, p_dec->p_module );
306 if( p_dec->p_owner->b_cc_supported )
309 for( i = 0; i < 4; i++ )
310 input_DecoderSetCcState( p_dec, false, i );
313 /* Delete decoder configuration */
314 DeleteDecoder( p_dec );
316 /* Delete the decoder */
317 vlc_object_release( p_dec );
321 * Put a block_t in the decoder's fifo.
323 * \param p_dec the decoder object
324 * \param p_block the data block
326 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
328 decoder_owner_sys_t *p_owner = p_dec->p_owner;
330 if( p_owner->b_own_thread )
332 if( p_owner->p_input->p->b_out_pace_control )
335 while( !p_dec->b_die && !p_dec->b_error &&
336 block_FifoCount( p_owner->p_fifo ) > 10 )
341 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
343 /* FIXME: ideally we would check the time amount of data
344 * in the fifo instead of its size. */
345 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
346 "consumed quickly enough), resetting fifo!" );
347 block_FifoEmpty( p_owner->p_fifo );
350 block_FifoPut( p_owner->p_fifo, p_block );
354 if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
357 block_Release( p_block );
361 DecoderDecode( p_dec, p_block );
366 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
368 decoder_owner_sys_t *p_owner = p_dec->p_owner;
372 if( p_owner->b_own_thread && b_flush )
373 block_FifoEmpty( p_owner->p_fifo );
375 /* Send a special block */
376 p_null = block_New( p_dec, 128 );
377 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
378 if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
379 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
380 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
381 if( p_owner->p_packetizer && b_flush )
382 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
383 memset( p_null->p_buffer, 0, p_null->i_buffer );
385 input_DecoderDecode( p_dec, p_null );
388 bool input_DecoderEmpty( decoder_t * p_dec )
390 if( p_dec->p_owner->b_own_thread &&
391 block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
398 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
400 decoder_owner_sys_t *p_owner = p_dec->p_owner;
403 vlc_mutex_lock( &p_owner->lock );
404 for( i = 0; i < 4; i++ )
405 pb_present[i] = p_owner->pb_cc_present[i];
406 vlc_mutex_unlock( &p_owner->lock );
408 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
410 decoder_owner_sys_t *p_owner = p_dec->p_owner;
412 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
414 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
419 static const vlc_fourcc_t fcc[4] = {
420 VLC_FOURCC('c', 'c', '1', ' '),
421 VLC_FOURCC('c', 'c', '2', ' '),
422 VLC_FOURCC('c', 'c', '3', ' '),
423 VLC_FOURCC('c', 'c', '4', ' '),
428 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
429 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
432 msg_Err( p_dec, "could not create decoder" );
433 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
434 _("VLC could not open the decoder module.") );
437 else if( !p_cc->p_module )
439 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
440 DeleteDecoder( p_cc );
441 vlc_object_release( p_cc );
444 p_cc->p_owner->p_clock = p_owner->p_clock;
446 vlc_mutex_lock( &p_owner->lock );
447 p_owner->pp_cc[i_channel] = p_cc;
448 vlc_mutex_unlock( &p_owner->lock );
454 vlc_mutex_lock( &p_owner->lock );
455 p_cc = p_owner->pp_cc[i_channel];
456 p_owner->pp_cc[i_channel] = NULL;
457 vlc_mutex_unlock( &p_owner->lock );
461 vlc_object_kill( p_cc );
462 module_unneed( p_cc, p_cc->p_module );
463 DeleteDecoder( p_cc );
464 vlc_object_release( p_cc );
469 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
471 decoder_owner_sys_t *p_owner = p_dec->p_owner;
474 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
477 vlc_mutex_lock( &p_owner->lock );
478 *pb_decode = p_owner->pp_cc[i_channel] != NULL;
479 vlc_mutex_unlock( &p_owner->lock );
483 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
485 decoder_owner_sys_t *p_owner = p_dec->p_owner;
487 vlc_mutex_lock( &p_owner->lock );
489 assert( (!p_owner->b_paused) != (!b_paused) );
490 p_owner->b_paused = b_paused;
491 p_owner->i_pause_date = i_date;
492 if( p_owner->b_own_thread )
493 vlc_cond_signal( &p_owner->wait );
495 DecoderOutputChangePause( p_dec, b_paused, i_date );
496 vlc_mutex_unlock( &p_owner->lock );
499 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
501 decoder_owner_sys_t *p_owner = p_dec->p_owner;
503 vlc_mutex_lock( &p_owner->lock );
504 p_owner->i_ts_delay = i_delay;
505 vlc_mutex_unlock( &p_owner->lock );
508 * Create a decoder object
510 * \param p_input the input thread
511 * \param p_es the es descriptor
512 * \param i_object_type Object type as define in include/vlc_objects.h
513 * \return the decoder object
515 static decoder_t * CreateDecoder( input_thread_t *p_input,
516 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
519 decoder_owner_sys_t *p_owner;
522 p_dec = vlc_object_create( p_input, i_object_type );
526 p_dec->pf_decode_audio = NULL;
527 p_dec->pf_decode_video = NULL;
528 p_dec->pf_decode_sub = NULL;
529 p_dec->pf_get_cc = NULL;
530 p_dec->pf_packetize = NULL;
532 /* Initialize the decoder fifo */
533 p_dec->p_module = NULL;
535 memset( &null_es_format, 0, sizeof(es_format_t) );
536 es_format_Copy( &p_dec->fmt_in, fmt );
537 es_format_Copy( &p_dec->fmt_out, &null_es_format );
539 /* Allocate our private structure for the decoder */
540 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
541 if( p_dec->p_owner == NULL )
543 vlc_object_release( p_dec );
546 p_dec->p_owner->b_own_thread = true;
547 p_dec->p_owner->i_preroll_end = -1;
548 p_dec->p_owner->p_input = p_input;
549 p_dec->p_owner->p_aout = NULL;
550 p_dec->p_owner->p_aout_input = NULL;
551 p_dec->p_owner->p_vout = NULL;
552 p_dec->p_owner->p_spu_vout = NULL;
553 p_dec->p_owner->i_spu_channel = 0;
554 p_dec->p_owner->i_spu_order = 0;
555 p_dec->p_owner->p_sout = p_sout;
556 p_dec->p_owner->p_sout_input = NULL;
557 p_dec->p_owner->p_packetizer = NULL;
560 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
562 free( p_dec->p_owner );
563 vlc_object_release( p_dec );
567 /* Set buffers allocation callbacks for the decoders */
568 p_dec->pf_aout_buffer_new = aout_new_buffer;
569 p_dec->pf_aout_buffer_del = aout_del_buffer;
570 p_dec->pf_vout_buffer_new = vout_new_buffer;
571 p_dec->pf_vout_buffer_del = vout_del_buffer;
572 p_dec->pf_picture_link = vout_link_picture;
573 p_dec->pf_picture_unlink = vout_unlink_picture;
574 p_dec->pf_spu_buffer_new = spu_new_buffer;
575 p_dec->pf_spu_buffer_del = spu_del_buffer;
577 vlc_object_attach( p_dec, p_input );
579 /* Find a suitable decoder/packetizer module */
580 if( i_object_type == VLC_OBJECT_DECODER )
581 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
583 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
585 /* Check if decoder requires already packetized data */
586 if( i_object_type == VLC_OBJECT_DECODER &&
587 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
589 p_dec->p_owner->p_packetizer =
590 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
591 if( p_dec->p_owner->p_packetizer )
593 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
596 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
599 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
601 p_dec->p_owner->p_packetizer->p_module =
602 module_need( p_dec->p_owner->p_packetizer,
603 "packetizer", "$packetizer", 0 );
605 if( !p_dec->p_owner->p_packetizer->p_module )
607 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
608 vlc_object_detach( p_dec->p_owner->p_packetizer );
609 vlc_object_release( p_dec->p_owner->p_packetizer );
614 /* Copy ourself the input replay gain */
615 if( fmt->i_cat == AUDIO_ES )
617 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
619 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
621 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
622 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
624 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
626 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
627 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
632 p_owner->b_cc_supported = false;
633 if( i_object_type == VLC_OBJECT_DECODER )
635 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
636 p_owner->b_cc_supported = true;
637 if( p_dec->pf_get_cc )
638 p_owner->b_cc_supported = true;
641 vlc_mutex_init( &p_owner->lock );
642 vlc_cond_init( &p_owner->wait );
643 p_owner->b_paused = false;
644 p_owner->i_pause_date = 0;
645 for( i = 0; i < 4; i++ )
647 p_owner->pb_cc_present[i] = false;
648 p_owner->pp_cc[i] = NULL;
650 p_owner->i_ts_delay = 0;
655 * The decoding main loop
657 * \param p_dec the decoder
659 static void* DecoderThread( vlc_object_t *p_this )
661 decoder_t *p_dec = (decoder_t *)p_this;
662 decoder_owner_sys_t *p_owner = p_dec->p_owner;
665 int canc = vlc_savecancel();
667 /* The decoder's main loop */
668 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
670 if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
676 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
680 while( vlc_object_alive( p_dec ) )
682 /* Trash all received PES packets */
683 p_block = block_FifoGet( p_owner->p_fifo );
685 block_Release( p_block );
688 /* We do it here because of the dll loader that wants close() in the
689 * same thread than open()/decode() */
690 module_unneed( p_dec, p_dec->p_module );
691 vlc_restorecancel( canc );
695 static void DecoderWaitUnpause( decoder_t *p_dec )
697 decoder_owner_sys_t *p_owner = p_dec->p_owner;
699 vlc_mutex_lock( &p_owner->lock );
701 while( p_owner->b_paused )
702 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
704 vlc_mutex_unlock( &p_owner->lock );
707 static mtime_t DecoderGetTotalDelay( decoder_t *p_dec )
709 decoder_owner_sys_t *p_owner = p_dec->p_owner;
711 vlc_mutex_lock( &p_owner->lock );
713 mtime_t i_delay = p_owner->i_ts_delay;
715 vlc_mutex_unlock( &p_owner->lock );
717 return p_owner->p_input->i_pts_delay + i_delay;
720 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
722 decoder_owner_sys_t *p_owner = p_dec->p_owner;
724 vlc_assert_locked( &p_owner->lock );
726 /* XXX only audio and video output have to be paused.
727 * - for sout it is useless
728 * - for subs, it is done by the vout
730 if( p_dec->fmt_in.i_cat == AUDIO_ES )
732 if( p_owner->p_aout && p_owner->p_aout_input )
733 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
736 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
738 if( p_owner->p_vout )
739 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
742 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
744 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
745 *pi_preroll = INT64_MAX;
746 else if( p->i_pts > 0 )
747 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
748 else if( p->i_dts > 0 )
749 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
752 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
754 mtime_t current_date = mdate();
756 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
757 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
759 /* ETSI EN 300 472 Annex A : do not take into account the PTS
760 * for teletext streams. */
761 return current_date + 400000 + i_ts_delay;
766 static void DecoderSoutBufferFixTs( block_t *p_block,
767 input_clock_t *p_clock, mtime_t i_ts_delay,
772 p_block->i_rate = input_clock_GetRate( p_clock );
774 if( p_block->i_dts > 0 )
775 p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
777 if( p_block->i_pts > 0 )
778 p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
780 if( p_block->i_length > 0 )
781 p_block->i_length = ( p_block->i_length * p_block->i_rate +
782 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
785 p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
787 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
788 input_clock_t *p_clock, mtime_t i_ts_delay )
790 /* sout display module does not set clock */
794 if( p_buffer->start_date )
795 p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
797 if( p_buffer->end_date )
798 p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
800 static void DecoderVoutBufferFixTs( picture_t *p_picture,
801 input_clock_t *p_clock, mtime_t i_ts_delay )
803 /* sout display module does not set clock */
807 if( p_picture->date )
808 p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
810 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
811 input_clock_t *p_clock, mtime_t i_ts_delay,
814 bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
816 /* sout display module does not set clock */
820 if( p_subpic->i_start )
821 p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
823 if( p_subpic->i_stop )
824 p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
826 /* Do not create ephemere picture because of rounding errors */
827 if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
831 p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
834 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
836 decoder_owner_sys_t *p_owner = p_dec->p_owner;
837 input_thread_t *p_input = p_owner->p_input;
838 input_clock_t *p_clock = p_owner->p_clock;
839 aout_buffer_t *p_aout_buf;
844 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
846 aout_instance_t *p_aout = p_owner->p_aout;
847 aout_input_t *p_aout_input = p_owner->p_aout_input;
854 /* It prevent freezing VLC in case of broken decoder */
855 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
857 block_Release( p_block );
862 if( p_aout_buf->start_date < p_owner->i_preroll_end )
864 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
868 if( p_owner->i_preroll_end > 0 )
870 /* FIXME TODO flush audio output (don't know how to do that) */
871 msg_Dbg( p_dec, "End of audio preroll" );
872 p_owner->i_preroll_end = -1;
875 DecoderWaitUnpause( p_dec );
877 const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
879 DecoderAoutBufferFixTs( p_aout_buf, p_clock, i_delay );
882 i_rate = input_clock_GetRate( p_clock );
883 else if( p_block && p_block->i_rate > 0 )
884 i_rate = p_block->i_rate;
886 i_rate = INPUT_RATE_DEFAULT;
888 /* FIXME TODO take care of audio-delay for mdate check */
889 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
891 if( p_aout_buf->start_date > 0 &&
892 p_aout_buf->start_date <= i_max_date &&
893 i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
894 i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
896 /* Wait if we are too early
897 * FIXME that's plain ugly to do it here */
898 mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
900 if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
902 i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
906 if( p_aout_buf->start_date <= 0 )
908 msg_Warn( p_dec, "non-dated audio buffer received" );
910 else if( p_aout_buf->start_date > i_max_date )
912 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
913 p_aout_buf->start_date - mdate() );
916 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
921 /* Update ugly stat */
922 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
924 vlc_mutex_lock( &p_input->p->counters.counters_lock);
926 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
928 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
930 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
933 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
936 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
938 decoder_owner_sys_t *p_owner = p_dec->p_owner;
944 assert( p_dec_cc->pf_get_cc != NULL );
946 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
947 if( !p_owner->b_cc_supported )
950 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
954 vlc_mutex_lock( &p_owner->lock );
955 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
957 p_owner->pb_cc_present[i] |= pb_present[i];
958 if( p_owner->pp_cc[i] )
962 for( i = 0; i < 4; i++ )
964 if( !p_owner->pp_cc[i] )
967 if( i_cc_decoder > 1 )
968 DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
970 DecoderDecode( p_owner->pp_cc[i], p_cc );
973 vlc_mutex_unlock( &p_owner->lock );
975 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
977 vlc_mutex_lock( &p_vout->picture_lock );
979 if( p_pic->i_status == READY_PICTURE )
981 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
984 else if( p_pic->i_refcount > 0 )
986 p_pic->i_status = DISPLAYED_PICTURE;
990 p_pic->i_status = DESTROYED_PICTURE;
991 picture_CleanupQuant( p_pic );
992 p_vout->i_heap_size--;
995 vlc_mutex_unlock( &p_vout->picture_lock );
997 static void VoutFlushPicture( vout_thread_t *p_vout )
1000 vlc_mutex_lock( &p_vout->picture_lock );
1001 for( i = 0; i < p_vout->render.i_pictures; i++ )
1003 picture_t *p_pic = p_vout->render.pp_picture[i];
1005 if( p_pic->i_status == READY_PICTURE ||
1006 p_pic->i_status == DISPLAYED_PICTURE )
1008 /* We cannot change picture status if it is in READY_PICTURE state,
1009 * Just make sure they won't be displayed */
1013 vlc_mutex_unlock( &p_vout->picture_lock );
1017 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
1019 input_thread_t *p_input = p_dec->p_owner->p_input;
1020 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
1021 input_thread_private_t *p_priv = p_input->p;
1023 picture_t *p_old = NULL;
1024 picture_t *p_young = NULL;
1027 /* Enable with --auto-adjust-pts-delay */
1028 if( !p_priv->pts_adjust.b_auto_adjust )
1031 for( i = 0; i < I_RENDERPICTURES; i++ )
1033 picture_t *p_pic = PP_RENDERPICTURE[i];
1035 if( p_pic->i_status != READY_PICTURE )
1038 if( !p_old || p_pic->date < p_old->date )
1040 if( !p_young || p_pic->date > p_young->date )
1044 if( !p_young || !p_old )
1047 /* Try to find if we can reduce the pts
1048 * This first draft is way too simple, and we can't say if the
1049 * algo will converge. It's also full of constants.
1050 * But this simple algo allows to reduce the latency
1052 * The whole point of this, is to bypass the pts_delay set
1053 * by the access but also the delay arbitraly set by
1054 * the remote server.
1055 * Actually the remote server's muxer may set up a
1056 * pts<->dts delay in the muxed stream. That is
1057 * why we may end up in having a negative pts_delay,
1058 * to compensate that artificial delay. */
1059 const mtime_t i_buffer_length = p_young->date - p_old->date;
1060 int64_t i_pts_slide = 0;
1061 if( i_buffer_length < 10000 )
1063 if( p_priv->pts_adjust.i_num_faulty > 10 )
1065 i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
1066 p_priv->pts_adjust.i_num_faulty = 0;
1068 if( p_priv->pts_adjust.b_to_high )
1070 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1071 p_priv->pts_adjust.i_num_faulty = 0;
1073 p_priv->pts_adjust.i_num_faulty++;
1075 else if( i_buffer_length > 100000 )
1077 if( p_priv->pts_adjust.i_num_faulty > 25 )
1079 i_pts_slide = -i_buffer_length/2;
1080 p_priv->pts_adjust.i_num_faulty = 0;
1082 if( p_priv->pts_adjust.b_to_high )
1084 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1085 p_priv->pts_adjust.i_num_faulty = 0;
1087 p_priv->pts_adjust.i_num_faulty++;
1089 if( i_pts_slide != 0 )
1091 const mtime_t i_pts_delay_org = p_input->i_pts_delay;
1093 p_input->i_pts_delay += i_pts_slide;
1095 /* Don't play with the pts delay for more than -2<->3sec */
1096 if( p_input->i_pts_delay < -2000000 )
1097 p_input->i_pts_delay = -2000000;
1098 else if( p_input->i_pts_delay > 3000000 )
1099 p_input->i_pts_delay = 3000000;
1100 i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
1102 msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
1103 (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
1105 vlc_mutex_lock( &p_vout->picture_lock );
1106 /* Slide all the picture */
1107 for( i = 0; i < I_RENDERPICTURES; i++ )
1108 PP_RENDERPICTURE[i]->date += i_pts_slide;
1109 /* FIXME: slide aout/spu */
1110 vlc_mutex_unlock( &p_vout->picture_lock );
1115 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1117 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1118 input_thread_t *p_input = p_owner->p_input;
1122 int i_displayed = 0;
1124 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1126 vout_thread_t *p_vout = p_owner->p_vout;
1129 /* It prevent freezing VLC in case of broken decoder */
1130 VoutDisplayedPicture( p_vout, p_pic );
1132 block_Release( p_block );
1138 if( p_pic->date < p_owner->i_preroll_end )
1140 VoutDisplayedPicture( p_vout, p_pic );
1144 if( p_owner->i_preroll_end > 0 )
1146 msg_Dbg( p_dec, "End of video preroll" );
1148 VoutFlushPicture( p_vout );
1150 p_owner->i_preroll_end = -1;
1153 if( p_dec->pf_get_cc &&
1154 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1155 DecoderGetCc( p_dec, p_dec );
1157 DecoderWaitUnpause( p_dec );
1159 const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
1161 DecoderVoutBufferFixTs( p_pic, p_owner->p_clock, i_delay );
1163 /* Video is never delayed so simple */
1164 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1166 if( p_pic->date > 0 && p_pic->date < i_max_date )
1168 vout_DatePicture( p_vout, p_pic, p_pic->date );
1170 /* Re-enable it but do it right this time */
1171 //DecoderOptimizePtsDelay( p_dec );
1172 vout_DisplayPicture( p_vout, p_pic );
1176 if( p_pic->date <= 0 )
1178 msg_Warn( p_vout, "non-dated video buffer received" );
1182 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1183 p_pic->date - mdate() );
1186 VoutDisplayedPicture( p_vout, p_pic );
1190 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1192 i_displayed += i_tmp_display;
1193 i_lost += i_tmp_lost;
1195 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1197 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1199 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1201 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1204 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1207 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1214 * \param p_dec the decoder object
1215 * \param p_block the block to decode
1216 * \return VLC_SUCCESS or an error code
1218 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
1220 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1221 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1223 if( p_block && p_block->i_buffer <= 0 )
1225 block_Release( p_block );
1230 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1232 block_t *p_sout_block;
1234 while( ( p_sout_block =
1235 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1237 if( !p_owner->p_sout_input )
1239 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1241 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1242 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1243 if( p_dec->fmt_in.psz_language )
1245 if( p_owner->sout.psz_language )
1246 free( p_owner->sout.psz_language );
1247 p_owner->sout.psz_language =
1248 strdup( p_dec->fmt_in.psz_language );
1251 p_owner->p_sout_input =
1252 sout_InputNew( p_owner->p_sout,
1255 if( p_owner->p_sout_input == NULL )
1257 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1258 (char *)&p_owner->sout.i_codec );
1259 p_dec->b_error = true;
1261 while( p_sout_block )
1263 block_t *p_next = p_sout_block->p_next;
1264 block_Release( p_sout_block );
1265 p_sout_block = p_next;
1271 while( p_sout_block )
1273 block_t *p_next = p_sout_block->p_next;
1275 p_sout_block->p_next = NULL;
1277 DecoderWaitUnpause( p_dec );
1279 const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
1281 DecoderSoutBufferFixTs( p_sout_block,
1282 p_owner->p_clock, i_delay, b_telx );
1284 sout_InputSendBuffer( p_owner->p_sout_input,
1287 p_sout_block = p_next;
1290 /* For now it's enough, as only sout impact on this flag */
1291 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1292 p_owner->p_input->p->b_out_pace_control )
1294 msg_Dbg( p_dec, "switching to sync mode" );
1295 p_owner->p_input->p->b_out_pace_control = false;
1297 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1298 !p_owner->p_input->p->b_out_pace_control )
1300 msg_Dbg( p_dec, "switching to async mode" );
1301 p_owner->p_input->p->b_out_pace_control = true;
1307 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1310 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1312 if( p_owner->p_packetizer )
1314 block_t *p_packetized_block;
1315 decoder_t *p_packetizer = p_owner->p_packetizer;
1317 while( (p_packetized_block =
1318 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1320 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1322 es_format_Clean( &p_dec->fmt_in );
1323 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1326 while( p_packetized_block )
1328 block_t *p_next = p_packetized_block->p_next;
1329 p_packetized_block->p_next = NULL;
1331 DecoderDecodeAudio( p_dec, p_packetized_block );
1333 p_packetized_block = p_next;
1339 DecoderDecodeAudio( p_dec, p_block );
1342 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1345 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1347 if( p_owner->p_packetizer )
1349 block_t *p_packetized_block;
1350 decoder_t *p_packetizer = p_owner->p_packetizer;
1352 while( (p_packetized_block =
1353 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1355 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1357 es_format_Clean( &p_dec->fmt_in );
1358 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1360 if( p_packetizer->pf_get_cc )
1361 DecoderGetCc( p_dec, p_packetizer );
1363 while( p_packetized_block )
1365 block_t *p_next = p_packetized_block->p_next;
1366 p_packetized_block->p_next = NULL;
1368 DecoderDecodeVideo( p_dec, p_packetized_block );
1370 p_packetized_block = p_next;
1376 DecoderDecodeVideo( p_dec, p_block );
1379 else if( p_dec->fmt_in.i_cat == SPU_ES )
1381 input_thread_t *p_input = p_owner->p_input;
1382 vout_thread_t *p_vout;
1383 subpicture_t *p_spu;
1384 bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1385 bool b_flush = false;
1389 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1390 b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1393 if( !b_flushing && b_flush && p_owner->p_spu_vout )
1395 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1397 if( p_vout && p_owner->p_spu_vout == p_vout )
1398 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1399 p_owner->i_spu_channel );
1402 vlc_object_release( p_vout );
1405 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1407 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1408 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1409 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1411 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1412 if( p_vout && p_owner->p_spu_vout == p_vout )
1414 /* Preroll does not work very well with subtitle */
1415 if( p_spu->i_start > 0 &&
1416 p_spu->i_start < p_owner->i_preroll_end &&
1417 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1419 subpicture_Delete( p_spu );
1423 DecoderWaitUnpause( p_dec );
1425 DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, p_input->i_pts_delay, b_telx );
1426 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1431 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1434 vlc_object_release( p_vout );
1439 msg_Err( p_dec, "unknown ES format" );
1443 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1447 * Destroys a decoder object
1449 * \param p_dec the decoder object
1452 static void DeleteDecoder( decoder_t * p_dec )
1454 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1456 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1457 (char*)&p_dec->fmt_in.i_codec,
1458 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1460 /* Free all packets still in the decoder fifo. */
1461 block_FifoEmpty( p_owner->p_fifo );
1462 block_FifoRelease( p_owner->p_fifo );
1465 if( p_owner->p_aout_input )
1466 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1467 if( p_owner->p_aout )
1469 vlc_object_release( p_owner->p_aout );
1470 p_owner->p_aout = NULL;
1472 if( p_owner->p_vout )
1476 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1477 /* Hack to make sure all the the pictures are freed by the decoder */
1478 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1481 if( p_pic->i_status == RESERVED_PICTURE )
1482 vout_DestroyPicture( p_owner->p_vout, p_pic );
1483 if( p_pic->i_refcount > 0 )
1484 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1488 /* We are about to die. Reattach video output to p_vlc. */
1489 vout_Request( p_dec, p_owner->p_vout, NULL );
1490 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1494 if( p_owner->p_sout_input )
1496 sout_InputDelete( p_owner->p_sout_input );
1497 es_format_Clean( &p_owner->sout );
1501 if( p_dec->fmt_in.i_cat == SPU_ES )
1503 vout_thread_t *p_vout;
1505 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1506 if( p_vout && p_owner->p_spu_vout == p_vout )
1508 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1509 p_owner->i_spu_channel );
1510 vlc_object_release( p_vout );
1514 es_format_Clean( &p_dec->fmt_in );
1515 es_format_Clean( &p_dec->fmt_out );
1517 if( p_owner->p_packetizer )
1519 module_unneed( p_owner->p_packetizer,
1520 p_owner->p_packetizer->p_module );
1521 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1522 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1523 vlc_object_detach( p_owner->p_packetizer );
1524 vlc_object_release( p_owner->p_packetizer );
1527 vlc_cond_destroy( &p_owner->wait );
1528 vlc_mutex_destroy( &p_owner->lock );
1530 vlc_object_detach( p_dec );
1535 /*****************************************************************************
1536 * Buffers allocation callbacks for the decoders
1537 *****************************************************************************/
1538 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1540 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1541 aout_buffer_t *p_buffer;
1543 if( p_owner->p_aout_input != NULL &&
1544 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1545 p_dec->fmt_out.audio.i_original_channels !=
1546 p_owner->audio.i_original_channels ||
1547 p_dec->fmt_out.audio.i_bytes_per_frame !=
1548 p_owner->audio.i_bytes_per_frame ) )
1550 aout_input_t *p_aout_input = p_owner->p_aout_input;
1552 /* Parameters changed, restart the aout */
1553 vlc_mutex_lock( &p_owner->lock );
1555 p_owner->p_aout_input = NULL;
1556 aout_DecDelete( p_owner->p_aout, p_aout_input );
1558 vlc_mutex_unlock( &p_owner->lock );
1561 if( p_owner->p_aout_input == NULL )
1563 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1564 audio_sample_format_t format;
1565 aout_input_t *p_aout_input;
1566 aout_instance_t *p_aout;
1568 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1569 p_owner->audio = p_dec->fmt_out.audio;
1571 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1572 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1573 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1575 if ( i_force_dolby == 1 )
1577 format.i_original_channels = format.i_original_channels |
1578 AOUT_CHAN_DOLBYSTEREO;
1580 else /* i_force_dolby == 2 */
1582 format.i_original_channels = format.i_original_channels &
1583 ~AOUT_CHAN_DOLBYSTEREO;
1587 p_aout = p_owner->p_aout;
1588 p_aout_input = aout_DecNew( p_dec, &p_aout,
1589 &format, &p_dec->fmt_out.audio_replay_gain );
1591 vlc_mutex_lock( &p_owner->lock );
1592 p_owner->p_aout = p_aout;
1593 p_owner->p_aout_input = p_aout_input;
1594 vlc_mutex_unlock( &p_owner->lock );
1596 if( p_owner->p_aout_input == NULL )
1598 msg_Err( p_dec, "failed to create audio output" );
1599 p_dec->b_error = true;
1602 p_dec->fmt_out.audio.i_bytes_per_frame =
1603 p_owner->audio.i_bytes_per_frame;
1606 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1611 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1613 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1615 aout_DecDeleteBuffer( p_owner->p_aout,
1616 p_owner->p_aout_input, p_buffer );
1620 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1622 static picture_t *vout_new_buffer( decoder_t *p_dec )
1624 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1627 if( p_owner->p_vout == NULL ||
1628 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1629 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1630 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1631 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1633 vout_thread_t *p_vout;
1635 if( !p_dec->fmt_out.video.i_width ||
1636 !p_dec->fmt_out.video.i_height )
1638 /* Can't create a new vout without display size */
1642 if( !p_dec->fmt_out.video.i_visible_width ||
1643 !p_dec->fmt_out.video.i_visible_height )
1645 if( p_dec->fmt_in.video.i_visible_width &&
1646 p_dec->fmt_in.video.i_visible_height )
1648 p_dec->fmt_out.video.i_visible_width =
1649 p_dec->fmt_in.video.i_visible_width;
1650 p_dec->fmt_out.video.i_visible_height =
1651 p_dec->fmt_in.video.i_visible_height;
1655 p_dec->fmt_out.video.i_visible_width =
1656 p_dec->fmt_out.video.i_width;
1657 p_dec->fmt_out.video.i_visible_height =
1658 p_dec->fmt_out.video.i_height;
1662 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1663 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1665 p_dec->fmt_out.video.i_visible_height = 1080;
1666 p_dec->fmt_out.video.i_sar_num *= 135;
1667 p_dec->fmt_out.video.i_sar_den *= 136;
1668 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1671 if( !p_dec->fmt_out.video.i_sar_num ||
1672 !p_dec->fmt_out.video.i_sar_den )
1674 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1675 p_dec->fmt_out.video.i_visible_height;
1677 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1678 p_dec->fmt_out.video.i_visible_width;
1681 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1682 &p_dec->fmt_out.video.i_sar_den,
1683 p_dec->fmt_out.video.i_sar_num,
1684 p_dec->fmt_out.video.i_sar_den, 50000 );
1686 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1687 p_owner->video = p_dec->fmt_out.video;
1689 vlc_mutex_lock( &p_owner->lock );
1690 p_vout = p_owner->p_vout;
1691 p_owner->p_vout = NULL;
1692 vlc_mutex_unlock( &p_owner->lock );
1694 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1696 vlc_mutex_lock( &p_owner->lock );
1697 p_owner->p_vout = p_vout;
1698 vlc_mutex_unlock( &p_owner->lock );
1700 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1701 if( p_vout == NULL )
1703 msg_Err( p_dec, "failed to create video output" );
1704 p_dec->b_error = true;
1708 if( p_owner->video.i_rmask )
1709 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1710 if( p_owner->video.i_gmask )
1711 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1712 if( p_owner->video.i_bmask )
1713 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1716 /* Get a new picture
1718 for( p_pic = NULL; ; )
1720 int i_pic, i_ready_pic;
1722 if( p_dec->b_die || p_dec->b_error )
1725 /* The video filter chain required that there is always 1 free buffer
1726 * that it will use as temporary one. It will release the temporary
1727 * buffer once its work is done, so this check is safe even if we don't
1728 * lock around both count() and create().
1730 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1732 p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1737 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1738 /* Check the decoder doesn't leak pictures */
1739 for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1741 if( p_pic->i_status == READY_PICTURE )
1744 /* If we have at least 2 ready pictures, wait for the vout thread to
1746 if( i_ready_pic >= 2 )
1752 if( p_pic->i_status == DISPLAYED_PICTURE )
1754 /* If at least one displayed picture is not referenced
1755 * let vout free it */
1756 if( p_pic->i_refcount == 0 )
1760 if( i_pic == p_owner->p_vout->render.i_pictures )
1762 /* Too many pictures are still referenced, there is probably a bug
1763 * with the decoder */
1764 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1766 /* Just free all the pictures */
1767 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1770 if( p_pic->i_status == RESERVED_PICTURE )
1771 vout_DestroyPicture( p_owner->p_vout, p_pic );
1772 if( p_pic->i_refcount > 0 )
1773 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1778 msleep( VOUT_OUTMEM_SLEEP );
1784 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1786 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1789 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1791 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1794 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1796 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1799 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1801 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1802 vout_thread_t *p_vout = NULL;
1803 subpicture_t *p_subpic;
1804 int i_attempts = 30;
1806 while( i_attempts-- )
1808 if( p_dec->b_die || p_dec->b_error )
1811 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1815 msleep( VOUT_DISPLAY_DELAY );
1820 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1824 if( p_owner->p_spu_vout != p_vout )
1826 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1827 &p_owner->i_spu_channel );
1828 p_owner->i_spu_order = 0;
1829 p_owner->p_spu_vout = p_vout;
1832 p_subpic = subpicture_New();
1835 p_subpic->i_channel = p_owner->i_spu_channel;
1836 p_subpic->i_order = p_owner->i_spu_order++;
1837 p_subpic->b_subtitle = true;
1840 vlc_object_release( p_vout );
1845 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1847 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1848 vout_thread_t *p_vout = NULL;
1850 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1851 if( !p_vout || p_owner->p_spu_vout != p_vout )
1854 vlc_object_release( p_vout );
1855 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1859 subpicture_Delete( p_subpic );
1861 vlc_object_release( p_vout );