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;
116 bool pb_cc_present[4];
121 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
123 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
124 "VLC probably does not support this sound or video format.",
126 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
127 _("VLC does not support the audio or video format \"%4.4s\". "
128 "Unfortunately there is no way for you to fix this."), (char*)&codec );
131 /* decoder_GetInputAttachment:
133 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
134 const char *psz_name )
136 input_attachment_t *p_attachment;
137 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
141 /* decoder_GetInputAttachments:
143 int decoder_GetInputAttachments( decoder_t *p_dec,
144 input_attachment_t ***ppp_attachment,
147 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
148 ppp_attachment, pi_attachment );
150 /* decoder_GetDisplayDate:
152 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
154 decoder_owner_sys_t *p_owner = p_dec->p_owner;
156 if( !p_owner->p_clock )
158 return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
160 /* decoder_GetDisplayRate:
162 int decoder_GetDisplayRate( decoder_t *p_dec )
164 decoder_owner_sys_t *p_owner = p_dec->p_owner;
166 if( !p_owner->p_clock )
167 return INPUT_RATE_DEFAULT;
168 return input_clock_GetRate( p_owner->p_clock );
172 * Spawns a new decoder thread
174 * \param p_input the input thread
175 * \param p_es the es descriptor
176 * \return the spawned decoder object
178 decoder_t *input_DecoderNew( input_thread_t *p_input,
179 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
181 decoder_t *p_dec = NULL;
185 (void)b_force_decoder;
187 /* If we are in sout mode, search for packetizer module */
190 /* Create the decoder configuration structure */
191 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
194 msg_Err( p_input, "could not create packetizer" );
195 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
196 _("VLC could not open the packetizer module.") );
203 /* Create the decoder configuration structure */
204 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
207 msg_Err( p_input, "could not create decoder" );
208 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
209 _("VLC could not open the decoder module.") );
214 if( !p_dec->p_module )
216 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
218 DeleteDecoder( p_dec );
219 vlc_object_release( p_dec );
223 p_dec->p_owner->p_clock = p_clock;
225 if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
227 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
228 p_dec->p_owner->b_own_thread = false;
232 var_Get( p_input, "minimize-threads", &val );
233 p_dec->p_owner->b_own_thread = !val.b_bool;
236 if( p_dec->p_owner->b_own_thread )
239 if( fmt->i_cat == AUDIO_ES )
240 i_priority = VLC_THREAD_PRIORITY_AUDIO;
242 i_priority = VLC_THREAD_PRIORITY_VIDEO;
244 /* Spawn the decoder thread */
245 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
246 i_priority, false ) )
248 msg_Err( p_dec, "cannot spawn decoder thread" );
249 module_unneed( p_dec, p_dec->p_module );
250 DeleteDecoder( p_dec );
251 vlc_object_release( p_dec );
260 * Kills a decoder thread and waits until it's finished
262 * \param p_input the input thread
263 * \param p_es the es descriptor
266 void input_DecoderDelete( decoder_t *p_dec )
268 decoder_owner_sys_t *p_owner = p_dec->p_owner;
270 vlc_object_kill( p_dec );
272 if( p_owner->b_own_thread )
274 /* Make sure we aren't paused anymore */
275 vlc_mutex_lock( &p_owner->lock );
276 if( p_owner->b_paused )
278 p_owner->b_paused = false;
279 vlc_cond_signal( &p_owner->wait );
281 vlc_mutex_unlock( &p_owner->lock );
283 /* Make sure the thread leaves the function by
284 * sending it an empty block. */
285 block_t *p_block = block_New( p_dec, 0 );
286 input_DecoderDecode( p_dec, p_block );
288 vlc_thread_join( p_dec );
290 /* Don't module_unneed() here because of the dll loader that wants
291 * close() in the same thread than open()/decode() */
296 input_DecoderDecode( p_dec, NULL );
298 module_unneed( p_dec, p_dec->p_module );
302 if( p_dec->p_owner->b_cc_supported )
305 for( i = 0; i < 4; i++ )
306 input_DecoderSetCcState( p_dec, false, i );
309 /* Delete decoder configuration */
310 DeleteDecoder( p_dec );
312 /* Delete the decoder */
313 vlc_object_release( p_dec );
317 * Put a block_t in the decoder's fifo.
319 * \param p_dec the decoder object
320 * \param p_block the data block
322 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
324 decoder_owner_sys_t *p_owner = p_dec->p_owner;
326 if( p_owner->b_own_thread )
328 if( p_owner->p_input->p->b_out_pace_control )
331 while( !p_dec->b_die && !p_dec->b_error &&
332 block_FifoCount( p_owner->p_fifo ) > 10 )
337 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
339 /* FIXME: ideally we would check the time amount of data
340 * in the fifo instead of its size. */
341 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
342 "consumed quickly enough), resetting fifo!" );
343 block_FifoEmpty( p_owner->p_fifo );
346 block_FifoPut( p_owner->p_fifo, p_block );
350 if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
353 block_Release( p_block );
357 DecoderDecode( p_dec, p_block );
362 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
364 decoder_owner_sys_t *p_owner = p_dec->p_owner;
368 if( p_owner->b_own_thread && b_flush )
369 block_FifoEmpty( p_owner->p_fifo );
371 /* Send a special block */
372 p_null = block_New( p_dec, 128 );
373 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
374 if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
375 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
376 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
377 if( p_owner->p_packetizer && b_flush )
378 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
379 memset( p_null->p_buffer, 0, p_null->i_buffer );
381 input_DecoderDecode( p_dec, p_null );
384 bool input_DecoderEmpty( decoder_t * p_dec )
386 if( p_dec->p_owner->b_own_thread &&
387 block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
394 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
396 decoder_owner_sys_t *p_owner = p_dec->p_owner;
399 vlc_mutex_lock( &p_owner->lock );
400 for( i = 0; i < 4; i++ )
401 pb_present[i] = p_owner->pb_cc_present[i];
402 vlc_mutex_unlock( &p_owner->lock );
404 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
406 decoder_owner_sys_t *p_owner = p_dec->p_owner;
408 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
410 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
415 static const vlc_fourcc_t fcc[4] = {
416 VLC_FOURCC('c', 'c', '1', ' '),
417 VLC_FOURCC('c', 'c', '2', ' '),
418 VLC_FOURCC('c', 'c', '3', ' '),
419 VLC_FOURCC('c', 'c', '4', ' '),
424 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
425 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
428 msg_Err( p_dec, "could not create decoder" );
429 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
430 _("VLC could not open the decoder module.") );
433 else if( !p_cc->p_module )
435 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
436 DeleteDecoder( p_cc );
437 vlc_object_release( p_cc );
440 p_cc->p_owner->p_clock = p_owner->p_clock;
442 vlc_mutex_lock( &p_owner->lock );
443 p_owner->pp_cc[i_channel] = p_cc;
444 vlc_mutex_unlock( &p_owner->lock );
450 vlc_mutex_lock( &p_owner->lock );
451 p_cc = p_owner->pp_cc[i_channel];
452 p_owner->pp_cc[i_channel] = NULL;
453 vlc_mutex_unlock( &p_owner->lock );
457 vlc_object_kill( p_cc );
458 module_unneed( p_cc, p_cc->p_module );
459 DeleteDecoder( p_cc );
460 vlc_object_release( p_cc );
465 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
467 decoder_owner_sys_t *p_owner = p_dec->p_owner;
470 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
473 vlc_mutex_lock( &p_owner->lock );
474 *pb_decode = p_owner->pp_cc[i_channel] != NULL;
475 vlc_mutex_unlock( &p_owner->lock );
479 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
481 decoder_owner_sys_t *p_owner = p_dec->p_owner;
483 vlc_mutex_lock( &p_owner->lock );
485 assert( (!p_owner->b_paused) != (!b_paused) );
486 p_owner->b_paused = b_paused;
487 p_owner->i_pause_date = i_date;
488 if( p_owner->b_own_thread )
489 vlc_cond_signal( &p_owner->wait );
491 DecoderOutputChangePause( p_dec, b_paused, i_date );
492 vlc_mutex_unlock( &p_owner->lock );
495 * Create a decoder object
497 * \param p_input the input thread
498 * \param p_es the es descriptor
499 * \param i_object_type Object type as define in include/vlc_objects.h
500 * \return the decoder object
502 static decoder_t * CreateDecoder( input_thread_t *p_input,
503 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
506 decoder_owner_sys_t *p_owner;
509 p_dec = vlc_object_create( p_input, i_object_type );
513 p_dec->pf_decode_audio = NULL;
514 p_dec->pf_decode_video = NULL;
515 p_dec->pf_decode_sub = NULL;
516 p_dec->pf_get_cc = NULL;
517 p_dec->pf_packetize = NULL;
519 /* Initialize the decoder fifo */
520 p_dec->p_module = NULL;
522 memset( &null_es_format, 0, sizeof(es_format_t) );
523 es_format_Copy( &p_dec->fmt_in, fmt );
524 es_format_Copy( &p_dec->fmt_out, &null_es_format );
526 /* Allocate our private structure for the decoder */
527 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
528 if( p_dec->p_owner == NULL )
530 vlc_object_release( p_dec );
533 p_dec->p_owner->b_own_thread = true;
534 p_dec->p_owner->i_preroll_end = -1;
535 p_dec->p_owner->p_input = p_input;
536 p_dec->p_owner->p_aout = NULL;
537 p_dec->p_owner->p_aout_input = NULL;
538 p_dec->p_owner->p_vout = NULL;
539 p_dec->p_owner->p_spu_vout = NULL;
540 p_dec->p_owner->i_spu_channel = 0;
541 p_dec->p_owner->i_spu_order = 0;
542 p_dec->p_owner->p_sout = p_sout;
543 p_dec->p_owner->p_sout_input = NULL;
544 p_dec->p_owner->p_packetizer = NULL;
547 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
549 free( p_dec->p_owner );
550 vlc_object_release( p_dec );
554 /* Set buffers allocation callbacks for the decoders */
555 p_dec->pf_aout_buffer_new = aout_new_buffer;
556 p_dec->pf_aout_buffer_del = aout_del_buffer;
557 p_dec->pf_vout_buffer_new = vout_new_buffer;
558 p_dec->pf_vout_buffer_del = vout_del_buffer;
559 p_dec->pf_picture_link = vout_link_picture;
560 p_dec->pf_picture_unlink = vout_unlink_picture;
561 p_dec->pf_spu_buffer_new = spu_new_buffer;
562 p_dec->pf_spu_buffer_del = spu_del_buffer;
564 vlc_object_attach( p_dec, p_input );
566 /* Find a suitable decoder/packetizer module */
567 if( i_object_type == VLC_OBJECT_DECODER )
568 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
570 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
572 /* Check if decoder requires already packetized data */
573 if( i_object_type == VLC_OBJECT_DECODER &&
574 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
576 p_dec->p_owner->p_packetizer =
577 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
578 if( p_dec->p_owner->p_packetizer )
580 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
583 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
586 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
588 p_dec->p_owner->p_packetizer->p_module =
589 module_need( p_dec->p_owner->p_packetizer,
590 "packetizer", "$packetizer", 0 );
592 if( !p_dec->p_owner->p_packetizer->p_module )
594 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
595 vlc_object_detach( p_dec->p_owner->p_packetizer );
596 vlc_object_release( p_dec->p_owner->p_packetizer );
601 /* Copy ourself the input replay gain */
602 if( fmt->i_cat == AUDIO_ES )
604 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
606 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
608 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
609 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
611 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
613 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
614 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
619 p_owner->b_cc_supported = false;
620 if( i_object_type == VLC_OBJECT_DECODER )
622 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
623 p_owner->b_cc_supported = true;
624 if( p_dec->pf_get_cc )
625 p_owner->b_cc_supported = true;
628 vlc_mutex_init( &p_owner->lock );
629 vlc_cond_init( &p_owner->wait );
630 p_owner->b_paused = false;
631 p_owner->i_pause_date = 0;
632 for( i = 0; i < 4; i++ )
634 p_owner->pb_cc_present[i] = false;
635 p_owner->pp_cc[i] = NULL;
641 * The decoding main loop
643 * \param p_dec the decoder
645 static void* DecoderThread( vlc_object_t *p_this )
647 decoder_t *p_dec = (decoder_t *)p_this;
648 decoder_owner_sys_t *p_owner = p_dec->p_owner;
651 int canc = vlc_savecancel();
653 /* The decoder's main loop */
654 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
656 if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
662 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
666 while( vlc_object_alive( p_dec ) )
668 /* Trash all received PES packets */
669 p_block = block_FifoGet( p_owner->p_fifo );
671 block_Release( p_block );
674 /* We do it here because of the dll loader that wants close() in the
675 * same thread than open()/decode() */
676 module_unneed( p_dec, p_dec->p_module );
677 vlc_restorecancel( canc );
681 static void DecoderWaitUnpause( decoder_t *p_dec )
683 decoder_owner_sys_t *p_owner = p_dec->p_owner;
685 vlc_mutex_lock( &p_owner->lock );
687 while( p_owner->b_paused )
688 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
690 vlc_mutex_unlock( &p_owner->lock );
693 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
695 decoder_owner_sys_t *p_owner = p_dec->p_owner;
697 vlc_assert_locked( &p_owner->lock );
699 /* XXX only audio and video output have to be paused.
700 * - for sout it is useless
701 * - for subs, it is done by the vout
703 if( p_dec->fmt_in.i_cat == AUDIO_ES )
705 if( p_owner->p_aout && p_owner->p_aout_input )
706 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
709 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
711 if( p_owner->p_vout )
712 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
715 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
717 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
718 *pi_preroll = INT64_MAX;
719 else if( p->i_pts > 0 )
720 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
721 else if( p->i_dts > 0 )
722 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
725 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
727 mtime_t current_date = mdate();
729 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
730 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
732 /* ETSI EN 300 472 Annex A : do not take into account the PTS
733 * for teletext streams. */
734 return current_date + 400000 + i_ts_delay;
739 static void DecoderSoutBufferFixTs( block_t *p_block,
740 input_clock_t *p_clock, mtime_t i_ts_delay,
745 p_block->i_rate = input_clock_GetRate( p_clock );
747 if( p_block->i_dts > 0 )
748 p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
750 if( p_block->i_pts > 0 )
751 p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
753 if( p_block->i_length > 0 )
754 p_block->i_length = ( p_block->i_length * p_block->i_rate +
755 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
758 p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
760 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
761 input_clock_t *p_clock, mtime_t i_ts_delay )
763 /* sout display module does not set clock */
767 if( p_buffer->start_date )
768 p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
770 if( p_buffer->end_date )
771 p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
773 static void DecoderVoutBufferFixTs( picture_t *p_picture,
774 input_clock_t *p_clock, mtime_t i_ts_delay )
776 /* sout display module does not set clock */
780 if( p_picture->date )
781 p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
783 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
784 input_clock_t *p_clock, mtime_t i_ts_delay,
787 bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
789 /* sout display module does not set clock */
793 if( p_subpic->i_start )
794 p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
796 if( p_subpic->i_stop )
797 p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
799 /* Do not create ephemere picture because of rounding errors */
800 if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
804 p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
807 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
809 decoder_owner_sys_t *p_owner = p_dec->p_owner;
810 input_thread_t *p_input = p_owner->p_input;
811 input_clock_t *p_clock = p_owner->p_clock;
812 aout_buffer_t *p_aout_buf;
817 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
819 aout_instance_t *p_aout = p_owner->p_aout;
820 aout_input_t *p_aout_input = p_owner->p_aout_input;
827 /* It prevent freezing VLC in case of broken decoder */
828 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
830 block_Release( p_block );
835 if( p_aout_buf->start_date < p_owner->i_preroll_end )
837 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
841 if( p_owner->i_preroll_end > 0 )
843 /* FIXME TODO flush audio output (don't know how to do that) */
844 msg_Dbg( p_dec, "End of audio preroll" );
845 p_owner->i_preroll_end = -1;
848 DecoderWaitUnpause( p_dec );
850 DecoderAoutBufferFixTs( p_aout_buf, p_clock, p_input->i_pts_delay );
853 i_rate = input_clock_GetRate( p_clock );
854 else if( p_block && p_block->i_rate > 0 )
855 i_rate = p_block->i_rate;
857 i_rate = INPUT_RATE_DEFAULT;
859 /* FIXME TODO take care of audio-delay for mdate check */
860 const mtime_t i_max_date = mdate() + p_input->i_pts_delay + AOUT_MAX_ADVANCE_TIME;
862 if( p_aout_buf->start_date > 0 &&
863 p_aout_buf->start_date <= i_max_date &&
864 i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
865 i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
867 /* Wait if we are too early
868 * FIXME that's plain ugly to do it here */
869 mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
871 if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
873 i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
877 if( p_aout_buf->start_date <= 0 )
879 msg_Warn( p_dec, "non-dated audio buffer received" );
881 else if( p_aout_buf->start_date > i_max_date )
883 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
884 p_aout_buf->start_date - mdate() );
887 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
892 /* Update ugly stat */
893 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
895 vlc_mutex_lock( &p_input->p->counters.counters_lock);
897 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
899 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
901 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
904 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
907 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
909 decoder_owner_sys_t *p_owner = p_dec->p_owner;
915 assert( p_dec_cc->pf_get_cc != NULL );
917 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
918 if( !p_owner->b_cc_supported )
921 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
925 vlc_mutex_lock( &p_owner->lock );
926 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
928 p_owner->pb_cc_present[i] |= pb_present[i];
929 if( p_owner->pp_cc[i] )
933 for( i = 0; i < 4; i++ )
935 if( !p_owner->pp_cc[i] )
938 if( i_cc_decoder > 1 )
939 DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
941 DecoderDecode( p_owner->pp_cc[i], p_cc );
944 vlc_mutex_unlock( &p_owner->lock );
946 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
948 vlc_mutex_lock( &p_vout->picture_lock );
950 if( p_pic->i_status == READY_PICTURE )
952 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
955 else if( p_pic->i_refcount > 0 )
957 p_pic->i_status = DISPLAYED_PICTURE;
961 p_pic->i_status = DESTROYED_PICTURE;
962 picture_CleanupQuant( p_pic );
963 p_vout->i_heap_size--;
966 vlc_mutex_unlock( &p_vout->picture_lock );
968 static void VoutFlushPicture( vout_thread_t *p_vout )
971 vlc_mutex_lock( &p_vout->picture_lock );
972 for( i = 0; i < p_vout->render.i_pictures; i++ )
974 picture_t *p_pic = p_vout->render.pp_picture[i];
976 if( p_pic->i_status == READY_PICTURE ||
977 p_pic->i_status == DISPLAYED_PICTURE )
979 /* We cannot change picture status if it is in READY_PICTURE state,
980 * Just make sure they won't be displayed */
984 vlc_mutex_unlock( &p_vout->picture_lock );
988 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
990 input_thread_t *p_input = p_dec->p_owner->p_input;
991 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
992 input_thread_private_t *p_priv = p_input->p;
994 picture_t *p_old = NULL;
995 picture_t *p_young = NULL;
998 /* Enable with --auto-adjust-pts-delay */
999 if( !p_priv->pts_adjust.b_auto_adjust )
1002 for( i = 0; i < I_RENDERPICTURES; i++ )
1004 picture_t *p_pic = PP_RENDERPICTURE[i];
1006 if( p_pic->i_status != READY_PICTURE )
1009 if( !p_old || p_pic->date < p_old->date )
1011 if( !p_young || p_pic->date > p_young->date )
1015 if( !p_young || !p_old )
1018 /* Try to find if we can reduce the pts
1019 * This first draft is way too simple, and we can't say if the
1020 * algo will converge. It's also full of constants.
1021 * But this simple algo allows to reduce the latency
1023 * The whole point of this, is to bypass the pts_delay set
1024 * by the access but also the delay arbitraly set by
1025 * the remote server.
1026 * Actually the remote server's muxer may set up a
1027 * pts<->dts delay in the muxed stream. That is
1028 * why we may end up in having a negative pts_delay,
1029 * to compensate that artificial delay. */
1030 const mtime_t i_buffer_length = p_young->date - p_old->date;
1031 int64_t i_pts_slide = 0;
1032 if( i_buffer_length < 10000 )
1034 if( p_priv->pts_adjust.i_num_faulty > 10 )
1036 i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
1037 p_priv->pts_adjust.i_num_faulty = 0;
1039 if( p_priv->pts_adjust.b_to_high )
1041 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1042 p_priv->pts_adjust.i_num_faulty = 0;
1044 p_priv->pts_adjust.i_num_faulty++;
1046 else if( i_buffer_length > 100000 )
1048 if( p_priv->pts_adjust.i_num_faulty > 25 )
1050 i_pts_slide = -i_buffer_length/2;
1051 p_priv->pts_adjust.i_num_faulty = 0;
1053 if( p_priv->pts_adjust.b_to_high )
1055 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1056 p_priv->pts_adjust.i_num_faulty = 0;
1058 p_priv->pts_adjust.i_num_faulty++;
1060 if( i_pts_slide != 0 )
1062 const mtime_t i_pts_delay_org = p_input->i_pts_delay;
1064 p_input->i_pts_delay += i_pts_slide;
1066 /* Don't play with the pts delay for more than -2<->3sec */
1067 if( p_input->i_pts_delay < -2000000 )
1068 p_input->i_pts_delay = -2000000;
1069 else if( p_input->i_pts_delay > 3000000 )
1070 p_input->i_pts_delay = 3000000;
1071 i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
1073 msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
1074 (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
1076 vlc_mutex_lock( &p_vout->picture_lock );
1077 /* Slide all the picture */
1078 for( i = 0; i < I_RENDERPICTURES; i++ )
1079 PP_RENDERPICTURE[i]->date += i_pts_slide;
1080 /* FIXME: slide aout/spu */
1081 vlc_mutex_unlock( &p_vout->picture_lock );
1086 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1088 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1089 input_thread_t *p_input = p_owner->p_input;
1093 int i_displayed = 0;
1095 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1097 vout_thread_t *p_vout = p_owner->p_vout;
1100 /* It prevent freezing VLC in case of broken decoder */
1101 VoutDisplayedPicture( p_vout, p_pic );
1103 block_Release( p_block );
1109 if( p_pic->date < p_owner->i_preroll_end )
1111 VoutDisplayedPicture( p_vout, p_pic );
1115 if( p_owner->i_preroll_end > 0 )
1117 msg_Dbg( p_dec, "End of video preroll" );
1119 VoutFlushPicture( p_vout );
1121 p_owner->i_preroll_end = -1;
1124 if( p_dec->pf_get_cc &&
1125 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1126 DecoderGetCc( p_dec, p_dec );
1128 DecoderWaitUnpause( p_dec );
1130 DecoderVoutBufferFixTs( p_pic, p_owner->p_clock, p_input->i_pts_delay );
1132 /* Video is never delayed so simple */
1133 const mtime_t i_max_date = mdate() + p_input->i_pts_delay + VOUT_BOGUS_DELAY;
1135 if( p_pic->date > 0 && p_pic->date < i_max_date )
1137 vout_DatePicture( p_vout, p_pic, p_pic->date );
1139 /* Re-enable it but do it right this time */
1140 //DecoderOptimizePtsDelay( p_dec );
1141 vout_DisplayPicture( p_vout, p_pic );
1145 if( p_pic->date <= 0 )
1147 msg_Warn( p_vout, "non-dated video buffer received" );
1151 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1152 p_pic->date - mdate() );
1155 VoutDisplayedPicture( p_vout, p_pic );
1159 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1161 i_displayed += i_tmp_display;
1162 i_lost += i_tmp_lost;
1164 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1166 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1168 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1170 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1173 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1176 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1183 * \param p_dec the decoder object
1184 * \param p_block the block to decode
1185 * \return VLC_SUCCESS or an error code
1187 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
1189 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1190 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1192 if( p_block && p_block->i_buffer <= 0 )
1194 block_Release( p_block );
1199 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1201 block_t *p_sout_block;
1203 while( ( p_sout_block =
1204 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1206 if( !p_owner->p_sout_input )
1208 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1210 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1211 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1212 if( p_dec->fmt_in.psz_language )
1214 if( p_owner->sout.psz_language )
1215 free( p_owner->sout.psz_language );
1216 p_owner->sout.psz_language =
1217 strdup( p_dec->fmt_in.psz_language );
1220 p_owner->p_sout_input =
1221 sout_InputNew( p_owner->p_sout,
1224 if( p_owner->p_sout_input == NULL )
1226 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1227 (char *)&p_owner->sout.i_codec );
1228 p_dec->b_error = true;
1230 while( p_sout_block )
1232 block_t *p_next = p_sout_block->p_next;
1233 block_Release( p_sout_block );
1234 p_sout_block = p_next;
1240 while( p_sout_block )
1242 block_t *p_next = p_sout_block->p_next;
1244 p_sout_block->p_next = NULL;
1246 DecoderWaitUnpause( p_dec );
1248 DecoderSoutBufferFixTs( p_sout_block,
1249 p_owner->p_clock, p_owner->p_input->i_pts_delay, b_telx );
1251 sout_InputSendBuffer( p_owner->p_sout_input,
1254 p_sout_block = p_next;
1257 /* For now it's enough, as only sout impact on this flag */
1258 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1259 p_owner->p_input->p->b_out_pace_control )
1261 msg_Dbg( p_dec, "switching to sync mode" );
1262 p_owner->p_input->p->b_out_pace_control = false;
1264 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1265 !p_owner->p_input->p->b_out_pace_control )
1267 msg_Dbg( p_dec, "switching to async mode" );
1268 p_owner->p_input->p->b_out_pace_control = true;
1274 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1277 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1279 if( p_owner->p_packetizer )
1281 block_t *p_packetized_block;
1282 decoder_t *p_packetizer = p_owner->p_packetizer;
1284 while( (p_packetized_block =
1285 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1287 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1289 es_format_Clean( &p_dec->fmt_in );
1290 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1293 while( p_packetized_block )
1295 block_t *p_next = p_packetized_block->p_next;
1296 p_packetized_block->p_next = NULL;
1298 DecoderDecodeAudio( p_dec, p_packetized_block );
1300 p_packetized_block = p_next;
1306 DecoderDecodeAudio( p_dec, p_block );
1309 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1312 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1314 if( p_owner->p_packetizer )
1316 block_t *p_packetized_block;
1317 decoder_t *p_packetizer = p_owner->p_packetizer;
1319 while( (p_packetized_block =
1320 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1322 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1324 es_format_Clean( &p_dec->fmt_in );
1325 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1327 if( p_packetizer->pf_get_cc )
1328 DecoderGetCc( p_dec, p_packetizer );
1330 while( p_packetized_block )
1332 block_t *p_next = p_packetized_block->p_next;
1333 p_packetized_block->p_next = NULL;
1335 DecoderDecodeVideo( p_dec, p_packetized_block );
1337 p_packetized_block = p_next;
1343 DecoderDecodeVideo( p_dec, p_block );
1346 else if( p_dec->fmt_in.i_cat == SPU_ES )
1348 input_thread_t *p_input = p_owner->p_input;
1349 vout_thread_t *p_vout;
1350 subpicture_t *p_spu;
1351 bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1352 bool b_flush = false;
1356 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1357 b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1360 if( !b_flushing && b_flush && p_owner->p_spu_vout )
1362 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1364 if( p_vout && p_owner->p_spu_vout == p_vout )
1365 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1366 p_owner->i_spu_channel );
1369 vlc_object_release( p_vout );
1372 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1374 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1375 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1376 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1378 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1379 if( p_vout && p_owner->p_spu_vout == p_vout )
1381 /* Preroll does not work very well with subtitle */
1382 if( p_spu->i_start > 0 &&
1383 p_spu->i_start < p_owner->i_preroll_end &&
1384 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1386 subpicture_Delete( p_spu );
1390 DecoderWaitUnpause( p_dec );
1392 DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, p_input->i_pts_delay, b_telx );
1393 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1398 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1401 vlc_object_release( p_vout );
1406 msg_Err( p_dec, "unknown ES format" );
1410 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1414 * Destroys a decoder object
1416 * \param p_dec the decoder object
1419 static void DeleteDecoder( decoder_t * p_dec )
1421 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1423 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1424 (char*)&p_dec->fmt_in.i_codec,
1425 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1427 /* Free all packets still in the decoder fifo. */
1428 block_FifoEmpty( p_owner->p_fifo );
1429 block_FifoRelease( p_owner->p_fifo );
1432 if( p_owner->p_aout_input )
1433 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1434 if( p_owner->p_aout )
1436 vlc_object_release( p_owner->p_aout );
1437 p_owner->p_aout = NULL;
1439 if( p_owner->p_vout )
1443 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1444 /* Hack to make sure all the the pictures are freed by the decoder */
1445 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1448 if( p_pic->i_status == RESERVED_PICTURE )
1449 vout_DestroyPicture( p_owner->p_vout, p_pic );
1450 if( p_pic->i_refcount > 0 )
1451 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1455 /* We are about to die. Reattach video output to p_vlc. */
1456 vout_Request( p_dec, p_owner->p_vout, NULL );
1457 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1461 if( p_owner->p_sout_input )
1463 sout_InputDelete( p_owner->p_sout_input );
1464 es_format_Clean( &p_owner->sout );
1468 if( p_dec->fmt_in.i_cat == SPU_ES )
1470 vout_thread_t *p_vout;
1472 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1473 if( p_vout && p_owner->p_spu_vout == p_vout )
1475 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1476 p_owner->i_spu_channel );
1477 vlc_object_release( p_vout );
1481 es_format_Clean( &p_dec->fmt_in );
1482 es_format_Clean( &p_dec->fmt_out );
1484 if( p_owner->p_packetizer )
1486 module_unneed( p_owner->p_packetizer,
1487 p_owner->p_packetizer->p_module );
1488 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1489 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1490 vlc_object_detach( p_owner->p_packetizer );
1491 vlc_object_release( p_owner->p_packetizer );
1494 vlc_cond_destroy( &p_owner->wait );
1495 vlc_mutex_destroy( &p_owner->lock );
1497 vlc_object_detach( p_dec );
1502 /*****************************************************************************
1503 * Buffers allocation callbacks for the decoders
1504 *****************************************************************************/
1505 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1507 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1508 aout_buffer_t *p_buffer;
1510 if( p_owner->p_aout_input != NULL &&
1511 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1512 p_dec->fmt_out.audio.i_original_channels !=
1513 p_owner->audio.i_original_channels ||
1514 p_dec->fmt_out.audio.i_bytes_per_frame !=
1515 p_owner->audio.i_bytes_per_frame ) )
1517 aout_input_t *p_aout_input = p_owner->p_aout_input;
1519 /* Parameters changed, restart the aout */
1520 vlc_mutex_lock( &p_owner->lock );
1522 p_owner->p_aout_input = NULL;
1523 aout_DecDelete( p_owner->p_aout, p_aout_input );
1525 vlc_mutex_unlock( &p_owner->lock );
1528 if( p_owner->p_aout_input == NULL )
1530 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1531 audio_sample_format_t format;
1532 aout_input_t *p_aout_input;
1533 aout_instance_t *p_aout;
1535 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1536 p_owner->audio = p_dec->fmt_out.audio;
1538 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1539 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1540 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1542 if ( i_force_dolby == 1 )
1544 format.i_original_channels = format.i_original_channels |
1545 AOUT_CHAN_DOLBYSTEREO;
1547 else /* i_force_dolby == 2 */
1549 format.i_original_channels = format.i_original_channels &
1550 ~AOUT_CHAN_DOLBYSTEREO;
1554 p_aout = p_owner->p_aout;
1555 p_aout_input = aout_DecNew( p_dec, &p_aout,
1556 &format, &p_dec->fmt_out.audio_replay_gain );
1558 vlc_mutex_lock( &p_owner->lock );
1559 p_owner->p_aout = p_aout;
1560 p_owner->p_aout_input = p_aout_input;
1561 vlc_mutex_unlock( &p_owner->lock );
1563 if( p_owner->p_aout_input == NULL )
1565 msg_Err( p_dec, "failed to create audio output" );
1566 p_dec->b_error = true;
1569 p_dec->fmt_out.audio.i_bytes_per_frame =
1570 p_owner->audio.i_bytes_per_frame;
1573 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1578 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1580 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1582 aout_DecDeleteBuffer( p_owner->p_aout,
1583 p_owner->p_aout_input, p_buffer );
1587 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1589 static picture_t *vout_new_buffer( decoder_t *p_dec )
1591 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1594 if( p_owner->p_vout == NULL ||
1595 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1596 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1597 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1598 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1600 vout_thread_t *p_vout;
1602 if( !p_dec->fmt_out.video.i_width ||
1603 !p_dec->fmt_out.video.i_height )
1605 /* Can't create a new vout without display size */
1609 if( !p_dec->fmt_out.video.i_visible_width ||
1610 !p_dec->fmt_out.video.i_visible_height )
1612 if( p_dec->fmt_in.video.i_visible_width &&
1613 p_dec->fmt_in.video.i_visible_height )
1615 p_dec->fmt_out.video.i_visible_width =
1616 p_dec->fmt_in.video.i_visible_width;
1617 p_dec->fmt_out.video.i_visible_height =
1618 p_dec->fmt_in.video.i_visible_height;
1622 p_dec->fmt_out.video.i_visible_width =
1623 p_dec->fmt_out.video.i_width;
1624 p_dec->fmt_out.video.i_visible_height =
1625 p_dec->fmt_out.video.i_height;
1629 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1630 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1632 p_dec->fmt_out.video.i_visible_height = 1080;
1633 p_dec->fmt_out.video.i_sar_num *= 135;
1634 p_dec->fmt_out.video.i_sar_den *= 136;
1635 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1638 if( !p_dec->fmt_out.video.i_sar_num ||
1639 !p_dec->fmt_out.video.i_sar_den )
1641 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1642 p_dec->fmt_out.video.i_visible_height;
1644 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1645 p_dec->fmt_out.video.i_visible_width;
1648 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1649 &p_dec->fmt_out.video.i_sar_den,
1650 p_dec->fmt_out.video.i_sar_num,
1651 p_dec->fmt_out.video.i_sar_den, 50000 );
1653 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1654 p_owner->video = p_dec->fmt_out.video;
1656 vlc_mutex_lock( &p_owner->lock );
1657 p_vout = p_owner->p_vout;
1658 p_owner->p_vout = NULL;
1659 vlc_mutex_unlock( &p_owner->lock );
1661 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1663 vlc_mutex_lock( &p_owner->lock );
1664 p_owner->p_vout = p_vout;
1665 vlc_mutex_unlock( &p_owner->lock );
1667 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1668 if( p_vout == NULL )
1670 msg_Err( p_dec, "failed to create video output" );
1671 p_dec->b_error = true;
1675 if( p_owner->video.i_rmask )
1676 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1677 if( p_owner->video.i_gmask )
1678 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1679 if( p_owner->video.i_bmask )
1680 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1683 /* Get a new picture
1685 for( p_pic = NULL; ; )
1687 int i_pic, i_ready_pic;
1689 if( p_dec->b_die || p_dec->b_error )
1692 /* The video filter chain required that there is always 1 free buffer
1693 * that it will use as temporary one. It will release the temporary
1694 * buffer once its work is done, so this check is safe even if we don't
1695 * lock around both count() and create().
1697 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1699 p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1704 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1705 /* Check the decoder doesn't leak pictures */
1706 for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1708 if( p_pic->i_status == READY_PICTURE )
1711 /* If we have at least 2 ready pictures, wait for the vout thread to
1713 if( i_ready_pic >= 2 )
1719 if( p_pic->i_status == DISPLAYED_PICTURE )
1721 /* If at least one displayed picture is not referenced
1722 * let vout free it */
1723 if( p_pic->i_refcount == 0 )
1727 if( i_pic == p_owner->p_vout->render.i_pictures )
1729 /* Too many pictures are still referenced, there is probably a bug
1730 * with the decoder */
1731 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1733 /* Just free all the pictures */
1734 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1737 if( p_pic->i_status == RESERVED_PICTURE )
1738 vout_DestroyPicture( p_owner->p_vout, p_pic );
1739 if( p_pic->i_refcount > 0 )
1740 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1745 msleep( VOUT_OUTMEM_SLEEP );
1751 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1753 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1756 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1758 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1761 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1763 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1766 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1768 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1769 vout_thread_t *p_vout = NULL;
1770 subpicture_t *p_subpic;
1771 int i_attempts = 30;
1773 while( i_attempts-- )
1775 if( p_dec->b_die || p_dec->b_error )
1778 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1782 msleep( VOUT_DISPLAY_DELAY );
1787 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1791 if( p_owner->p_spu_vout != p_vout )
1793 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1794 &p_owner->i_spu_channel );
1795 p_owner->i_spu_order = 0;
1796 p_owner->p_spu_vout = p_vout;
1799 p_subpic = subpicture_New();
1802 p_subpic->i_channel = p_owner->i_spu_channel;
1803 p_subpic->i_order = p_owner->i_spu_order++;
1804 p_subpic->b_subtitle = true;
1807 vlc_object_release( p_vout );
1812 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1814 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1815 vout_thread_t *p_vout = NULL;
1817 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1818 if( !p_vout || p_owner->p_spu_vout != p_vout )
1821 vlc_object_release( p_vout );
1822 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1826 subpicture_Delete( p_subpic );
1828 vlc_object_release( p_vout );