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 assert( p_owner->p_clock );
157 return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
159 /* decoder_GetDisplayRate:
161 int decoder_GetDisplayRate( decoder_t *p_dec )
163 decoder_owner_sys_t *p_owner = p_dec->p_owner;
165 assert( p_owner->p_clock );
166 return input_clock_GetRate( p_owner->p_clock );
170 * Spawns a new decoder thread
172 * \param p_input the input thread
173 * \param p_es the es descriptor
174 * \return the spawned decoder object
176 decoder_t *input_DecoderNew( input_thread_t *p_input,
177 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
179 decoder_t *p_dec = NULL;
183 (void)b_force_decoder;
185 /* If we are in sout mode, search for packetizer module */
188 /* Create the decoder configuration structure */
189 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
192 msg_Err( p_input, "could not create packetizer" );
193 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
194 _("VLC could not open the packetizer module.") );
201 /* Create the decoder configuration structure */
202 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
205 msg_Err( p_input, "could not create decoder" );
206 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
207 _("VLC could not open the decoder module.") );
212 if( !p_dec->p_module )
214 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
216 DeleteDecoder( p_dec );
217 vlc_object_release( p_dec );
221 p_dec->p_owner->p_clock = p_clock;
223 if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
225 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
226 p_dec->p_owner->b_own_thread = false;
230 var_Get( p_input, "minimize-threads", &val );
231 p_dec->p_owner->b_own_thread = !val.b_bool;
234 if( p_dec->p_owner->b_own_thread )
237 if( fmt->i_cat == AUDIO_ES )
238 i_priority = VLC_THREAD_PRIORITY_AUDIO;
240 i_priority = VLC_THREAD_PRIORITY_VIDEO;
242 /* Spawn the decoder thread */
243 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
244 i_priority, false ) )
246 msg_Err( p_dec, "cannot spawn decoder thread" );
247 module_unneed( p_dec, p_dec->p_module );
248 DeleteDecoder( p_dec );
249 vlc_object_release( p_dec );
258 * Kills a decoder thread and waits until it's finished
260 * \param p_input the input thread
261 * \param p_es the es descriptor
264 void input_DecoderDelete( decoder_t *p_dec )
266 decoder_owner_sys_t *p_owner = p_dec->p_owner;
268 vlc_object_kill( p_dec );
270 if( p_owner->b_own_thread )
272 /* Make sure we aren't paused anymore */
273 vlc_mutex_lock( &p_owner->lock );
274 if( p_owner->b_paused )
276 p_owner->b_paused = false;
277 vlc_cond_signal( &p_owner->wait );
279 vlc_mutex_unlock( &p_owner->lock );
281 /* Make sure the thread leaves the function by
282 * sending it an empty block. */
283 block_t *p_block = block_New( p_dec, 0 );
284 input_DecoderDecode( p_dec, p_block );
286 vlc_thread_join( p_dec );
288 /* Don't module_unneed() here because of the dll loader that wants
289 * close() in the same thread than open()/decode() */
294 input_DecoderDecode( p_dec, NULL );
296 module_unneed( p_dec, p_dec->p_module );
300 if( p_dec->p_owner->b_cc_supported )
303 for( i = 0; i < 4; i++ )
304 input_DecoderSetCcState( p_dec, false, i );
307 /* Delete decoder configuration */
308 DeleteDecoder( p_dec );
310 /* Delete the decoder */
311 vlc_object_release( p_dec );
315 * Put a block_t in the decoder's fifo.
317 * \param p_dec the decoder object
318 * \param p_block the data block
320 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
322 decoder_owner_sys_t *p_owner = p_dec->p_owner;
324 if( p_owner->b_own_thread )
326 if( p_owner->p_input->p->b_out_pace_control )
329 while( !p_dec->b_die && !p_dec->b_error &&
330 block_FifoCount( p_owner->p_fifo ) > 10 )
335 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
337 /* FIXME: ideally we would check the time amount of data
338 * in the fifo instead of its size. */
339 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
340 "consumed quickly enough), resetting fifo!" );
341 block_FifoEmpty( p_owner->p_fifo );
344 block_FifoPut( p_owner->p_fifo, p_block );
348 if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
351 block_Release( p_block );
355 DecoderDecode( p_dec, p_block );
360 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
362 decoder_owner_sys_t *p_owner = p_dec->p_owner;
366 if( p_owner->b_own_thread && b_flush )
367 block_FifoEmpty( p_owner->p_fifo );
369 /* Send a special block */
370 p_null = block_New( p_dec, 128 );
371 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
372 if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
373 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
374 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
375 if( p_owner->p_packetizer && b_flush )
376 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
377 memset( p_null->p_buffer, 0, p_null->i_buffer );
379 input_DecoderDecode( p_dec, p_null );
382 bool input_DecoderEmpty( decoder_t * p_dec )
384 if( p_dec->p_owner->b_own_thread &&
385 block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
392 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
394 decoder_owner_sys_t *p_owner = p_dec->p_owner;
397 vlc_mutex_lock( &p_owner->lock );
398 for( i = 0; i < 4; i++ )
399 pb_present[i] = p_owner->pb_cc_present[i];
400 vlc_mutex_unlock( &p_owner->lock );
402 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
404 decoder_owner_sys_t *p_owner = p_dec->p_owner;
406 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
408 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
413 static const vlc_fourcc_t fcc[4] = {
414 VLC_FOURCC('c', 'c', '1', ' '),
415 VLC_FOURCC('c', 'c', '2', ' '),
416 VLC_FOURCC('c', 'c', '3', ' '),
417 VLC_FOURCC('c', 'c', '4', ' '),
422 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
423 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
426 msg_Err( p_dec, "could not create decoder" );
427 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
428 _("VLC could not open the decoder module.") );
431 else if( !p_cc->p_module )
433 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
434 DeleteDecoder( p_cc );
435 vlc_object_release( p_cc );
438 p_cc->p_owner->p_clock = p_owner->p_clock;
440 vlc_mutex_lock( &p_owner->lock );
441 p_owner->pp_cc[i_channel] = p_cc;
442 vlc_mutex_unlock( &p_owner->lock );
448 vlc_mutex_lock( &p_owner->lock );
449 p_cc = p_owner->pp_cc[i_channel];
450 p_owner->pp_cc[i_channel] = NULL;
451 vlc_mutex_unlock( &p_owner->lock );
455 vlc_object_kill( p_cc );
456 module_unneed( p_cc, p_cc->p_module );
457 DeleteDecoder( p_cc );
458 vlc_object_release( p_cc );
463 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
465 decoder_owner_sys_t *p_owner = p_dec->p_owner;
468 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
471 vlc_mutex_lock( &p_owner->lock );
472 *pb_decode = p_owner->pp_cc[i_channel] != NULL;
473 vlc_mutex_unlock( &p_owner->lock );
477 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
479 decoder_owner_sys_t *p_owner = p_dec->p_owner;
481 vlc_mutex_lock( &p_owner->lock );
483 assert( (!p_owner->b_paused) != (!b_paused) );
484 p_owner->b_paused = b_paused;
485 p_owner->i_pause_date = i_date;
486 if( p_owner->b_own_thread )
487 vlc_cond_signal( &p_owner->wait );
489 DecoderOutputChangePause( p_dec, b_paused, i_date );
490 vlc_mutex_unlock( &p_owner->lock );
493 * Create a decoder object
495 * \param p_input the input thread
496 * \param p_es the es descriptor
497 * \param i_object_type Object type as define in include/vlc_objects.h
498 * \return the decoder object
500 static decoder_t * CreateDecoder( input_thread_t *p_input,
501 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
504 decoder_owner_sys_t *p_owner;
507 p_dec = vlc_object_create( p_input, i_object_type );
511 p_dec->pf_decode_audio = NULL;
512 p_dec->pf_decode_video = NULL;
513 p_dec->pf_decode_sub = NULL;
514 p_dec->pf_get_cc = NULL;
515 p_dec->pf_packetize = NULL;
517 /* Initialize the decoder fifo */
518 p_dec->p_module = NULL;
520 memset( &null_es_format, 0, sizeof(es_format_t) );
521 es_format_Copy( &p_dec->fmt_in, fmt );
522 es_format_Copy( &p_dec->fmt_out, &null_es_format );
524 /* Allocate our private structure for the decoder */
525 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
526 if( p_dec->p_owner == NULL )
528 vlc_object_release( p_dec );
531 p_dec->p_owner->b_own_thread = true;
532 p_dec->p_owner->i_preroll_end = -1;
533 p_dec->p_owner->p_input = p_input;
534 p_dec->p_owner->p_aout = NULL;
535 p_dec->p_owner->p_aout_input = NULL;
536 p_dec->p_owner->p_vout = NULL;
537 p_dec->p_owner->p_spu_vout = NULL;
538 p_dec->p_owner->i_spu_channel = 0;
539 p_dec->p_owner->i_spu_order = 0;
540 p_dec->p_owner->p_sout = p_sout;
541 p_dec->p_owner->p_sout_input = NULL;
542 p_dec->p_owner->p_packetizer = NULL;
545 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
547 free( p_dec->p_owner );
548 vlc_object_release( p_dec );
552 /* Set buffers allocation callbacks for the decoders */
553 p_dec->pf_aout_buffer_new = aout_new_buffer;
554 p_dec->pf_aout_buffer_del = aout_del_buffer;
555 p_dec->pf_vout_buffer_new = vout_new_buffer;
556 p_dec->pf_vout_buffer_del = vout_del_buffer;
557 p_dec->pf_picture_link = vout_link_picture;
558 p_dec->pf_picture_unlink = vout_unlink_picture;
559 p_dec->pf_spu_buffer_new = spu_new_buffer;
560 p_dec->pf_spu_buffer_del = spu_del_buffer;
562 vlc_object_attach( p_dec, p_input );
564 /* Find a suitable decoder/packetizer module */
565 if( i_object_type == VLC_OBJECT_DECODER )
566 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
568 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
570 /* Check if decoder requires already packetized data */
571 if( i_object_type == VLC_OBJECT_DECODER &&
572 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
574 p_dec->p_owner->p_packetizer =
575 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
576 if( p_dec->p_owner->p_packetizer )
578 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
581 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
584 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
586 p_dec->p_owner->p_packetizer->p_module =
587 module_need( p_dec->p_owner->p_packetizer,
588 "packetizer", "$packetizer", 0 );
590 if( !p_dec->p_owner->p_packetizer->p_module )
592 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
593 vlc_object_detach( p_dec->p_owner->p_packetizer );
594 vlc_object_release( p_dec->p_owner->p_packetizer );
599 /* Copy ourself the input replay gain */
600 if( fmt->i_cat == AUDIO_ES )
602 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
604 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
606 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
607 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
609 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
611 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
612 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
617 p_owner->b_cc_supported = false;
618 if( i_object_type == VLC_OBJECT_DECODER )
620 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
621 p_owner->b_cc_supported = true;
622 if( p_dec->pf_get_cc )
623 p_owner->b_cc_supported = true;
626 vlc_mutex_init( &p_owner->lock );
627 vlc_cond_init( &p_owner->wait );
628 p_owner->b_paused = false;
629 p_owner->i_pause_date = 0;
630 for( i = 0; i < 4; i++ )
632 p_owner->pb_cc_present[i] = false;
633 p_owner->pp_cc[i] = NULL;
639 * The decoding main loop
641 * \param p_dec the decoder
643 static void* DecoderThread( vlc_object_t *p_this )
645 decoder_t *p_dec = (decoder_t *)p_this;
646 decoder_owner_sys_t *p_owner = p_dec->p_owner;
649 int canc = vlc_savecancel();
651 /* The decoder's main loop */
652 while( vlc_object_alive( p_dec ) && !p_dec->b_error )
654 if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
660 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
664 while( vlc_object_alive( p_dec ) )
666 /* Trash all received PES packets */
667 p_block = block_FifoGet( p_owner->p_fifo );
669 block_Release( p_block );
672 /* We do it here because of the dll loader that wants close() in the
673 * same thread than open()/decode() */
674 module_unneed( p_dec, p_dec->p_module );
675 vlc_restorecancel( canc );
679 static void DecoderWaitUnpause( decoder_t *p_dec )
681 decoder_owner_sys_t *p_owner = p_dec->p_owner;
683 vlc_mutex_lock( &p_owner->lock );
685 while( p_owner->b_paused )
686 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
688 vlc_mutex_unlock( &p_owner->lock );
691 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
693 decoder_owner_sys_t *p_owner = p_dec->p_owner;
695 /* XXX only audio and video output have to be paused.
696 * - for sout it is useless
697 * - for subs, it is done by the vout
699 if( p_dec->fmt_in.i_cat == AUDIO_ES )
702 //if( p_own->p_vout )
703 // aout_ChangePause( p_own->p_aout, p_own->p_aout_input, b_paused, i_date );
705 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
707 if( p_owner->p_vout )
708 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
711 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
713 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
714 *pi_preroll = INT64_MAX;
715 else if( p->i_pts > 0 )
716 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
717 else if( p->i_dts > 0 )
718 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
721 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
723 mtime_t current_date = mdate();
725 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
726 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
728 /* ETSI EN 300 472 Annex A : do not take into account the PTS
729 * for teletext streams. */
730 return current_date + 400000 + i_ts_delay;
735 static void DecoderSoutBufferFixTs( block_t *p_block,
736 input_clock_t *p_clock, mtime_t i_ts_delay,
741 p_block->i_rate = input_clock_GetRate( p_clock );
743 if( p_block->i_dts > 0 )
744 p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
746 if( p_block->i_pts > 0 )
747 p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
749 if( p_block->i_length > 0 )
750 p_block->i_length = ( p_block->i_length * p_block->i_rate +
751 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
754 p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
756 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
757 input_clock_t *p_clock, mtime_t i_ts_delay )
759 /* sout display module does not set clock */
763 if( p_buffer->start_date )
764 p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
766 if( p_buffer->end_date )
767 p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
769 static void DecoderVoutBufferFixTs( picture_t *p_picture,
770 input_clock_t *p_clock, mtime_t i_ts_delay )
772 /* sout display module does not set clock */
776 if( p_picture->date )
777 p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
779 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
780 input_clock_t *p_clock, mtime_t i_ts_delay,
783 bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
785 /* sout display module does not set clock */
789 if( p_subpic->i_start )
790 p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
792 if( p_subpic->i_stop )
793 p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
795 /* Do not create ephemere picture because of rounding errors */
796 if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
800 p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
803 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
805 decoder_owner_sys_t *p_owner = p_dec->p_owner;
806 input_thread_t *p_input = p_owner->p_input;
807 input_clock_t *p_clock = p_owner->p_clock;
808 aout_buffer_t *p_aout_buf;
810 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
812 aout_instance_t *p_aout = p_owner->p_aout;
813 aout_input_t *p_aout_input = p_owner->p_aout_input;
817 /* It prevent freezing VLC in case of broken decoder */
818 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
820 block_Release( p_block );
823 vlc_mutex_lock( &p_input->p->counters.counters_lock );
824 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
825 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
827 if( p_aout_buf->start_date < p_owner->i_preroll_end )
829 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
833 if( p_owner->i_preroll_end > 0 )
835 /* FIXME TODO flush audio output (don't know how to do that) */
836 msg_Dbg( p_dec, "End of audio preroll" );
837 p_owner->i_preroll_end = -1;
840 DecoderWaitUnpause( p_dec );
842 const int i_rate = p_clock ? input_clock_GetRate( p_clock ) : p_block->i_rate;
844 DecoderAoutBufferFixTs( p_aout_buf, p_clock, p_input->i_pts_delay );
845 if( i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
846 i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
847 aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate );
849 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
852 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
854 decoder_owner_sys_t *p_owner = p_dec->p_owner;
860 assert( p_dec_cc->pf_get_cc != NULL );
862 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
863 if( !p_owner->b_cc_supported )
866 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
870 vlc_mutex_lock( &p_owner->lock );
871 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
873 p_owner->pb_cc_present[i] |= pb_present[i];
874 if( p_owner->pp_cc[i] )
878 for( i = 0; i < 4; i++ )
880 if( !p_owner->pp_cc[i] )
883 if( i_cc_decoder > 1 )
884 DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
886 DecoderDecode( p_owner->pp_cc[i], p_cc );
889 vlc_mutex_unlock( &p_owner->lock );
891 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
893 vlc_mutex_lock( &p_vout->picture_lock );
895 if( p_pic->i_status == READY_PICTURE )
897 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
900 else if( p_pic->i_refcount > 0 )
902 p_pic->i_status = DISPLAYED_PICTURE;
906 p_pic->i_status = DESTROYED_PICTURE;
907 picture_CleanupQuant( p_pic );
908 p_vout->i_heap_size--;
911 vlc_mutex_unlock( &p_vout->picture_lock );
913 static void VoutFlushPicture( vout_thread_t *p_vout )
916 vlc_mutex_lock( &p_vout->picture_lock );
917 for( i = 0; i < p_vout->render.i_pictures; i++ )
919 picture_t *p_pic = p_vout->render.pp_picture[i];
921 if( p_pic->i_status == READY_PICTURE ||
922 p_pic->i_status == DISPLAYED_PICTURE )
924 /* We cannot change picture status if it is in READY_PICTURE state,
925 * Just make sure they won't be displayed */
929 vlc_mutex_unlock( &p_vout->picture_lock );
933 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
935 input_thread_t *p_input = p_dec->p_owner->p_input;
936 vout_thread_t *p_vout = p_dec->p_owner->p_vout;
937 input_thread_private_t *p_priv = p_input->p;
939 picture_t *p_old = NULL;
940 picture_t *p_young = NULL;
943 /* Enable with --auto-adjust-pts-delay */
944 if( !p_priv->pts_adjust.b_auto_adjust )
947 for( i = 0; i < I_RENDERPICTURES; i++ )
949 picture_t *p_pic = PP_RENDERPICTURE[i];
951 if( p_pic->i_status != READY_PICTURE )
954 if( !p_old || p_pic->date < p_old->date )
956 if( !p_young || p_pic->date > p_young->date )
960 if( !p_young || !p_old )
963 /* Try to find if we can reduce the pts
964 * This first draft is way too simple, and we can't say if the
965 * algo will converge. It's also full of constants.
966 * But this simple algo allows to reduce the latency
968 * The whole point of this, is to bypass the pts_delay set
969 * by the access but also the delay arbitraly set by
971 * Actually the remote server's muxer may set up a
972 * pts<->dts delay in the muxed stream. That is
973 * why we may end up in having a negative pts_delay,
974 * to compensate that artificial delay. */
975 const mtime_t i_buffer_length = p_young->date - p_old->date;
976 int64_t i_pts_slide = 0;
977 if( i_buffer_length < 10000 )
979 if( p_priv->pts_adjust.i_num_faulty > 10 )
981 i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
982 p_priv->pts_adjust.i_num_faulty = 0;
984 if( p_priv->pts_adjust.b_to_high )
986 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
987 p_priv->pts_adjust.i_num_faulty = 0;
989 p_priv->pts_adjust.i_num_faulty++;
991 else if( i_buffer_length > 100000 )
993 if( p_priv->pts_adjust.i_num_faulty > 25 )
995 i_pts_slide = -i_buffer_length/2;
996 p_priv->pts_adjust.i_num_faulty = 0;
998 if( p_priv->pts_adjust.b_to_high )
1000 p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1001 p_priv->pts_adjust.i_num_faulty = 0;
1003 p_priv->pts_adjust.i_num_faulty++;
1005 if( i_pts_slide != 0 )
1007 const mtime_t i_pts_delay_org = p_input->i_pts_delay;
1009 p_input->i_pts_delay += i_pts_slide;
1011 /* Don't play with the pts delay for more than -2<->3sec */
1012 if( p_input->i_pts_delay < -2000000 )
1013 p_input->i_pts_delay = -2000000;
1014 else if( p_input->i_pts_delay > 3000000 )
1015 p_input->i_pts_delay = 3000000;
1016 i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
1018 msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
1019 (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
1021 vlc_mutex_lock( &p_vout->picture_lock );
1022 /* Slide all the picture */
1023 for( i = 0; i < I_RENDERPICTURES; i++ )
1024 PP_RENDERPICTURE[i]->date += i_pts_slide;
1025 /* FIXME: slide aout/spu */
1026 vlc_mutex_unlock( &p_vout->picture_lock );
1031 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1033 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1034 input_thread_t *p_input = p_owner->p_input;
1037 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1039 vout_thread_t *p_vout = p_owner->p_vout;
1042 /* It prevent freezing VLC in case of broken decoder */
1043 VoutDisplayedPicture( p_vout, p_pic );
1045 block_Release( p_block );
1049 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1050 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
1051 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1053 if( p_pic->date < p_owner->i_preroll_end )
1055 VoutDisplayedPicture( p_vout, p_pic );
1059 if( p_owner->i_preroll_end > 0 )
1061 msg_Dbg( p_dec, "End of video preroll" );
1063 VoutFlushPicture( p_vout );
1065 p_owner->i_preroll_end = -1;
1068 if( p_dec->pf_get_cc &&
1069 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1070 DecoderGetCc( p_dec, p_dec );
1072 DecoderWaitUnpause( p_dec );
1074 DecoderVoutBufferFixTs( p_pic, p_owner->p_clock, p_input->i_pts_delay );
1076 vout_DatePicture( p_vout, p_pic, p_pic->date );
1078 /* Re-enable it but do it right this time */
1079 //DecoderOptimizePtsDelay( p_dec );
1081 vout_DisplayPicture( p_vout, p_pic );
1088 * \param p_dec the decoder object
1089 * \param p_block the block to decode
1090 * \return VLC_SUCCESS or an error code
1092 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
1094 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1095 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1097 if( p_block && p_block->i_buffer <= 0 )
1099 block_Release( p_block );
1104 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1106 block_t *p_sout_block;
1108 while( ( p_sout_block =
1109 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1111 if( !p_owner->p_sout_input )
1113 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1115 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1116 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1117 if( p_dec->fmt_in.psz_language )
1119 if( p_owner->sout.psz_language )
1120 free( p_owner->sout.psz_language );
1121 p_owner->sout.psz_language =
1122 strdup( p_dec->fmt_in.psz_language );
1125 p_owner->p_sout_input =
1126 sout_InputNew( p_owner->p_sout,
1129 if( p_owner->p_sout_input == NULL )
1131 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1132 (char *)&p_owner->sout.i_codec );
1133 p_dec->b_error = true;
1135 while( p_sout_block )
1137 block_t *p_next = p_sout_block->p_next;
1138 block_Release( p_sout_block );
1139 p_sout_block = p_next;
1145 while( p_sout_block )
1147 block_t *p_next = p_sout_block->p_next;
1149 p_sout_block->p_next = NULL;
1151 DecoderWaitUnpause( p_dec );
1153 DecoderSoutBufferFixTs( p_sout_block,
1154 p_owner->p_clock, p_owner->p_input->i_pts_delay, b_telx );
1156 sout_InputSendBuffer( p_owner->p_sout_input,
1159 p_sout_block = p_next;
1162 /* For now it's enough, as only sout impact on this flag */
1163 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1164 p_owner->p_input->p->b_out_pace_control )
1166 msg_Dbg( p_dec, "switching to sync mode" );
1167 p_owner->p_input->p->b_out_pace_control = false;
1169 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1170 !p_owner->p_input->p->b_out_pace_control )
1172 msg_Dbg( p_dec, "switching to async mode" );
1173 p_owner->p_input->p->b_out_pace_control = true;
1179 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1182 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1184 if( p_owner->p_packetizer )
1186 block_t *p_packetized_block;
1187 decoder_t *p_packetizer = p_owner->p_packetizer;
1189 while( (p_packetized_block =
1190 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1192 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1194 es_format_Clean( &p_dec->fmt_in );
1195 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1198 while( p_packetized_block )
1200 block_t *p_next = p_packetized_block->p_next;
1201 p_packetized_block->p_next = NULL;
1203 DecoderDecodeAudio( p_dec, p_packetized_block );
1205 p_packetized_block = p_next;
1211 DecoderDecodeAudio( p_dec, p_block );
1214 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1217 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1219 if( p_owner->p_packetizer )
1221 block_t *p_packetized_block;
1222 decoder_t *p_packetizer = p_owner->p_packetizer;
1224 while( (p_packetized_block =
1225 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1227 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1229 es_format_Clean( &p_dec->fmt_in );
1230 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1232 if( p_packetizer->pf_get_cc )
1233 DecoderGetCc( p_dec, p_packetizer );
1235 while( p_packetized_block )
1237 block_t *p_next = p_packetized_block->p_next;
1238 p_packetized_block->p_next = NULL;
1240 DecoderDecodeVideo( p_dec, p_packetized_block );
1242 p_packetized_block = p_next;
1248 DecoderDecodeVideo( p_dec, p_block );
1251 else if( p_dec->fmt_in.i_cat == SPU_ES )
1253 input_thread_t *p_input = p_owner->p_input;
1254 vout_thread_t *p_vout;
1255 subpicture_t *p_spu;
1256 bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1257 bool b_flush = false;
1261 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1262 b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1265 if( !b_flushing && b_flush && p_owner->p_spu_vout )
1267 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1269 if( p_vout && p_owner->p_spu_vout == p_vout )
1270 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1271 p_owner->i_spu_channel );
1274 vlc_object_release( p_vout );
1277 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1279 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1280 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1281 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1283 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1284 if( p_vout && p_owner->p_spu_vout == p_vout )
1286 /* Preroll does not work very well with subtitle */
1287 if( p_spu->i_start < p_owner->i_preroll_end &&
1288 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1290 subpicture_Delete( p_spu );
1294 DecoderWaitUnpause( p_dec );
1296 DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, p_input->i_pts_delay, b_telx );
1297 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1302 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1305 vlc_object_release( p_vout );
1310 msg_Err( p_dec, "unknown ES format" );
1314 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1318 * Destroys a decoder object
1320 * \param p_dec the decoder object
1323 static void DeleteDecoder( decoder_t * p_dec )
1325 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1327 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1328 (char*)&p_dec->fmt_in.i_codec,
1329 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1331 /* Free all packets still in the decoder fifo. */
1332 block_FifoEmpty( p_owner->p_fifo );
1333 block_FifoRelease( p_owner->p_fifo );
1336 if( p_owner->p_aout_input )
1337 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1338 if( p_owner->p_aout )
1340 vlc_object_release( p_owner->p_aout );
1341 p_owner->p_aout = NULL;
1343 if( p_owner->p_vout )
1347 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1348 /* Hack to make sure all the the pictures are freed by the decoder */
1349 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1352 if( p_pic->i_status == RESERVED_PICTURE )
1353 vout_DestroyPicture( p_owner->p_vout, p_pic );
1354 if( p_pic->i_refcount > 0 )
1355 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1359 /* We are about to die. Reattach video output to p_vlc. */
1360 vout_Request( p_dec, p_owner->p_vout, NULL );
1361 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1365 if( p_owner->p_sout_input )
1367 sout_InputDelete( p_owner->p_sout_input );
1368 es_format_Clean( &p_owner->sout );
1372 if( p_dec->fmt_in.i_cat == SPU_ES )
1374 vout_thread_t *p_vout;
1376 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1377 if( p_vout && p_owner->p_spu_vout == p_vout )
1379 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1380 p_owner->i_spu_channel );
1381 vlc_object_release( p_vout );
1385 es_format_Clean( &p_dec->fmt_in );
1386 es_format_Clean( &p_dec->fmt_out );
1388 if( p_owner->p_packetizer )
1390 module_unneed( p_owner->p_packetizer,
1391 p_owner->p_packetizer->p_module );
1392 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1393 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1394 vlc_object_detach( p_owner->p_packetizer );
1395 vlc_object_release( p_owner->p_packetizer );
1398 vlc_cond_destroy( &p_owner->wait );
1399 vlc_mutex_destroy( &p_owner->lock );
1401 vlc_object_detach( p_dec );
1406 /*****************************************************************************
1407 * Buffers allocation callbacks for the decoders
1408 *****************************************************************************/
1409 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1411 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1412 aout_buffer_t *p_buffer;
1414 if( p_owner->p_aout_input != NULL &&
1415 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1416 p_dec->fmt_out.audio.i_original_channels !=
1417 p_owner->audio.i_original_channels ||
1418 p_dec->fmt_out.audio.i_bytes_per_frame !=
1419 p_owner->audio.i_bytes_per_frame ) )
1421 aout_input_t *p_aout_input = p_owner->p_aout_input;
1423 /* Parameters changed, restart the aout */
1424 vlc_mutex_lock( &p_owner->lock );
1426 p_owner->p_aout_input = NULL;
1427 aout_DecDelete( p_owner->p_aout, p_aout_input );
1429 vlc_mutex_unlock( &p_owner->lock );
1432 if( p_owner->p_aout_input == NULL )
1434 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1435 audio_sample_format_t format;
1436 aout_input_t *p_aout_input;
1437 aout_instance_t *p_aout;
1439 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1440 p_owner->audio = p_dec->fmt_out.audio;
1442 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1443 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1444 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1446 if ( i_force_dolby == 1 )
1448 format.i_original_channels = format.i_original_channels |
1449 AOUT_CHAN_DOLBYSTEREO;
1451 else /* i_force_dolby == 2 */
1453 format.i_original_channels = format.i_original_channels &
1454 ~AOUT_CHAN_DOLBYSTEREO;
1458 p_aout = p_owner->p_aout;
1459 p_aout_input = aout_DecNew( p_dec, &p_aout,
1460 &format, &p_dec->fmt_out.audio_replay_gain );
1462 vlc_mutex_lock( &p_owner->lock );
1463 p_owner->p_aout = p_aout;
1464 p_owner->p_aout_input = p_aout_input;
1465 vlc_mutex_unlock( &p_owner->lock );
1467 if( p_owner->p_aout_input == NULL )
1469 msg_Err( p_dec, "failed to create audio output" );
1470 p_dec->b_error = true;
1473 p_dec->fmt_out.audio.i_bytes_per_frame =
1474 p_owner->audio.i_bytes_per_frame;
1477 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1482 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1484 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1486 aout_DecDeleteBuffer( p_owner->p_aout,
1487 p_owner->p_aout_input, p_buffer );
1491 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1493 static picture_t *vout_new_buffer( decoder_t *p_dec )
1495 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1498 if( p_owner->p_vout == NULL ||
1499 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1500 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1501 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1502 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1504 vout_thread_t *p_vout;
1506 if( !p_dec->fmt_out.video.i_width ||
1507 !p_dec->fmt_out.video.i_height )
1509 /* Can't create a new vout without display size */
1513 if( !p_dec->fmt_out.video.i_visible_width ||
1514 !p_dec->fmt_out.video.i_visible_height )
1516 if( p_dec->fmt_in.video.i_visible_width &&
1517 p_dec->fmt_in.video.i_visible_height )
1519 p_dec->fmt_out.video.i_visible_width =
1520 p_dec->fmt_in.video.i_visible_width;
1521 p_dec->fmt_out.video.i_visible_height =
1522 p_dec->fmt_in.video.i_visible_height;
1526 p_dec->fmt_out.video.i_visible_width =
1527 p_dec->fmt_out.video.i_width;
1528 p_dec->fmt_out.video.i_visible_height =
1529 p_dec->fmt_out.video.i_height;
1533 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1534 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1536 p_dec->fmt_out.video.i_visible_height = 1080;
1537 p_dec->fmt_out.video.i_sar_num *= 135;
1538 p_dec->fmt_out.video.i_sar_den *= 136;
1539 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1542 if( !p_dec->fmt_out.video.i_sar_num ||
1543 !p_dec->fmt_out.video.i_sar_den )
1545 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1546 p_dec->fmt_out.video.i_visible_height;
1548 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1549 p_dec->fmt_out.video.i_visible_width;
1552 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1553 &p_dec->fmt_out.video.i_sar_den,
1554 p_dec->fmt_out.video.i_sar_num,
1555 p_dec->fmt_out.video.i_sar_den, 50000 );
1557 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1558 p_owner->video = p_dec->fmt_out.video;
1560 p_vout = vout_Request( p_dec, p_owner->p_vout,
1561 &p_dec->fmt_out.video );
1563 vlc_mutex_lock( &p_owner->lock );
1564 p_owner->p_vout = p_vout;
1565 vlc_mutex_unlock( &p_owner->lock );
1567 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1568 if( p_vout == NULL )
1570 msg_Err( p_dec, "failed to create video output" );
1571 p_dec->b_error = true;
1575 if( p_owner->video.i_rmask )
1576 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1577 if( p_owner->video.i_gmask )
1578 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1579 if( p_owner->video.i_bmask )
1580 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1583 /* Get a new picture
1585 for( p_pic = NULL; ; )
1587 int i_pic, i_ready_pic;
1589 if( p_dec->b_die || p_dec->b_error )
1592 /* The video filter chain required that there is always 1 free buffer
1593 * that it will use as temporary one. It will release the temporary
1594 * buffer once its work is done, so this check is safe even if we don't
1595 * lock around both count() and create().
1597 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1599 p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1604 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1605 /* Check the decoder doesn't leak pictures */
1606 for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1608 if( p_pic->i_status == READY_PICTURE )
1611 /* If we have at least 2 ready pictures, wait for the vout thread to
1613 if( i_ready_pic >= 2 )
1619 if( p_pic->i_status == DISPLAYED_PICTURE )
1621 /* If at least one displayed picture is not referenced
1622 * let vout free it */
1623 if( p_pic->i_refcount == 0 )
1627 if( i_pic == p_owner->p_vout->render.i_pictures )
1629 /* Too many pictures are still referenced, there is probably a bug
1630 * with the decoder */
1631 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1633 /* Just free all the pictures */
1634 for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1637 if( p_pic->i_status == RESERVED_PICTURE )
1638 vout_DestroyPicture( p_owner->p_vout, p_pic );
1639 if( p_pic->i_refcount > 0 )
1640 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1645 msleep( VOUT_OUTMEM_SLEEP );
1651 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1653 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1656 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1658 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1661 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1663 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1666 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1668 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1669 vout_thread_t *p_vout = NULL;
1670 subpicture_t *p_subpic;
1671 int i_attempts = 30;
1673 while( i_attempts-- )
1675 if( p_dec->b_die || p_dec->b_error )
1678 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1682 msleep( VOUT_DISPLAY_DELAY );
1687 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1691 if( p_owner->p_spu_vout != p_vout )
1693 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1694 &p_owner->i_spu_channel );
1695 p_owner->i_spu_order = 0;
1696 p_owner->p_spu_vout = p_vout;
1699 p_subpic = subpicture_New();
1702 p_subpic->i_channel = p_owner->i_spu_channel;
1703 p_subpic->i_order = p_owner->i_spu_order++;
1704 p_subpic->b_subtitle = true;
1707 vlc_object_release( p_vout );
1712 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1714 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1715 vout_thread_t *p_vout = NULL;
1717 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1718 if( !p_vout || p_owner->p_spu_vout != p_vout )
1721 vlc_object_release( p_vout );
1722 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1726 subpicture_Delete( p_subpic );
1728 vlc_object_release( p_vout );