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>
44 #include <vlc_interface.h>
45 #include "audio_output/aout_internal.h"
46 #include "stream_output/stream_output.h"
47 #include "input_internal.h"
51 #include "ressource.h"
53 #include "../video_output/vout_control.h"
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
56 static void DeleteDecoder( decoder_t * );
58 static void *DecoderThread( vlc_object_t * );
59 static void DecoderProcess( decoder_t *, block_t * );
60 static void DecoderError( decoder_t *p_dec, block_t *p_block );
61 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
62 static void DecoderFlush( decoder_t * );
63 static void DecoderSignalBuffering( decoder_t *, bool );
64 static void DecoderFlushBuffering( decoder_t * );
66 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
68 /* Buffers allocation callbacks for the decoders */
69 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
70 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
72 static picture_t *vout_new_buffer( decoder_t * );
73 static void vout_del_buffer( decoder_t *, picture_t * );
74 static void vout_link_picture( decoder_t *, picture_t * );
75 static void vout_unlink_picture( decoder_t *, picture_t * );
77 static subpicture_t *spu_new_buffer( decoder_t * );
78 static void spu_del_buffer( decoder_t *, subpicture_t * );
80 static es_format_t null_es_format;
82 struct decoder_owner_sys_t
84 int64_t i_preroll_end;
86 input_thread_t *p_input;
87 input_clock_t *p_clock;
90 vout_thread_t *p_spu_vout;
94 sout_instance_t *p_sout;
95 sout_packetizer_input_t *p_sout_input;
97 /* Some decoders require already packetized data (ie. not truncated) */
98 decoder_t *p_packetizer;
100 /* Current format in use by the output */
101 video_format_t video;
102 audio_format_t audio;
106 bool b_fmt_description;
107 es_format_t fmt_description;
108 vlc_meta_t *p_description;
111 block_fifo_t *p_fifo;
113 /* Lock for communication with decoder thread */
117 /* -- These variables need locking on write(only) -- */
118 aout_instance_t *p_aout;
119 aout_input_t *p_aout_input;
121 vout_thread_t *p_vout;
123 /* -- Theses variables need locking on read *and* write -- */
141 picture_t *p_picture;
142 picture_t **pp_picture_next;
144 subpicture_t *p_subpic;
145 subpicture_t **pp_subpic_next;
147 aout_buffer_t *p_audio;
148 aout_buffer_t **pp_audio_next;
151 block_t **pp_block_next;
162 decoder_t *pp_decoder[4];
169 #define DECODER_MAX_BUFFERING_COUNT (4)
170 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
171 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
173 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
174 * a bogus PTS and won't be displayed */
175 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
178 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
181 /*****************************************************************************
183 *****************************************************************************/
184 picture_t *decoder_NewPicture( decoder_t *p_decoder )
186 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
188 msg_Warn( p_decoder, "can't get output picture" );
191 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
193 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
195 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
197 p_decoder->pf_picture_link( p_decoder, p_picture );
199 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
201 p_decoder->pf_picture_unlink( p_decoder, p_picture );
204 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
206 if( !p_decoder->pf_aout_buffer_new )
208 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
210 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
212 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
215 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
217 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
219 msg_Warn( p_decoder, "can't get output subpicture" );
222 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
224 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
227 /* decoder_GetInputAttachments:
229 int decoder_GetInputAttachments( decoder_t *p_dec,
230 input_attachment_t ***ppp_attachment,
233 if( !p_dec->pf_get_attachments )
236 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
238 /* decoder_GetDisplayDate:
240 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
242 if( !p_dec->pf_get_display_date )
245 return p_dec->pf_get_display_date( p_dec, i_ts );
247 /* decoder_GetDisplayRate:
249 int decoder_GetDisplayRate( decoder_t *p_dec )
251 if( !p_dec->pf_get_display_rate )
252 return INPUT_RATE_DEFAULT;
254 return p_dec->pf_get_display_rate( p_dec );
258 * Spawns a new decoder thread
260 * \param p_input the input thread
261 * \param p_es the es descriptor
262 * \return the spawned decoder object
264 decoder_t *input_DecoderNew( input_thread_t *p_input,
265 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
267 decoder_t *p_dec = NULL;
271 /* If we are in sout mode, search for packetizer module */
274 /* Create the decoder configuration structure */
275 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
278 msg_Err( p_input, "could not create packetizer" );
279 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
280 _("VLC could not open the packetizer module.") );
287 /* Create the decoder configuration structure */
288 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
291 msg_Err( p_input, "could not create decoder" );
292 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
293 _("VLC could not open the decoder module.") );
298 if( !p_dec->p_module )
300 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
302 DeleteDecoder( p_dec );
303 vlc_object_release( p_dec );
307 p_dec->p_owner->p_clock = p_clock;
309 if( fmt->i_cat == AUDIO_ES )
310 i_priority = VLC_THREAD_PRIORITY_AUDIO;
312 i_priority = VLC_THREAD_PRIORITY_VIDEO;
314 /* Spawn the decoder thread */
315 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
316 i_priority, false ) )
318 msg_Err( p_dec, "cannot spawn decoder thread" );
319 module_unneed( p_dec, p_dec->p_module );
320 DeleteDecoder( p_dec );
321 vlc_object_release( p_dec );
329 * Kills a decoder thread and waits until it's finished
331 * \param p_input the input thread
332 * \param p_es the es descriptor
335 void input_DecoderDelete( decoder_t *p_dec )
337 decoder_owner_sys_t *p_owner = p_dec->p_owner;
339 vlc_object_kill( p_dec );
341 /* Make sure we aren't paused/buffering/waiting anymore */
342 vlc_mutex_lock( &p_owner->lock );
343 p_owner->b_paused = false;
344 p_owner->b_buffering = false;
345 p_owner->b_flushing = true;
346 vlc_cond_signal( &p_owner->wait );
347 vlc_mutex_unlock( &p_owner->lock );
349 vlc_thread_join( p_dec );
350 module_unneed( p_dec, p_dec->p_module );
353 if( p_dec->p_owner->cc.b_supported )
356 for( i = 0; i < 4; i++ )
357 input_DecoderSetCcState( p_dec, false, i );
360 /* Delete decoder configuration */
361 DeleteDecoder( p_dec );
363 /* Delete the decoder */
364 vlc_object_release( p_dec );
368 * Put a block_t in the decoder's fifo.
369 * Thread-safe w.r.t. the decoder. May be a cancellation point.
371 * \param p_dec the decoder object
372 * \param p_block the data block
374 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
376 decoder_owner_sys_t *p_owner = p_dec->p_owner;
378 if( p_owner->p_input->p->b_out_pace_control )
380 /* The fifo is not consummed when buffering and so will
382 * There is no need to lock as b_buffering is never modify
383 * inside decoder thread. */
384 if( !p_owner->b_buffering )
385 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
387 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
389 /* FIXME: ideally we would check the time amount of data
390 * in the FIFO instead of its size. */
391 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
392 "consumed quickly enough), resetting fifo!" );
393 block_FifoEmpty( p_owner->p_fifo );
396 block_FifoPut( p_owner->p_fifo, p_block );
399 bool input_DecoderIsEmpty( decoder_t * p_dec )
401 assert( !p_dec->p_owner->b_buffering );
403 /* FIXME that's not really true */
404 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
407 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
409 decoder_owner_sys_t *p_owner = p_dec->p_owner;
412 vlc_mutex_lock( &p_owner->lock );
413 for( i = 0; i < 4; i++ )
414 pb_present[i] = p_owner->cc.pb_present[i];
415 vlc_mutex_unlock( &p_owner->lock );
417 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
419 decoder_owner_sys_t *p_owner = p_dec->p_owner;
421 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
423 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
428 static const vlc_fourcc_t fcc[4] = {
429 VLC_FOURCC('c', 'c', '1', ' '),
430 VLC_FOURCC('c', 'c', '2', ' '),
431 VLC_FOURCC('c', 'c', '3', ' '),
432 VLC_FOURCC('c', 'c', '4', ' '),
437 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
438 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
441 msg_Err( p_dec, "could not create decoder" );
442 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
443 _("VLC could not open the decoder module.") );
446 else if( !p_cc->p_module )
448 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
449 DeleteDecoder( p_cc );
450 vlc_object_release( p_cc );
453 p_cc->p_owner->p_clock = p_owner->p_clock;
455 vlc_mutex_lock( &p_owner->lock );
456 p_owner->cc.pp_decoder[i_channel] = p_cc;
457 vlc_mutex_unlock( &p_owner->lock );
463 vlc_mutex_lock( &p_owner->lock );
464 p_cc = p_owner->cc.pp_decoder[i_channel];
465 p_owner->cc.pp_decoder[i_channel] = NULL;
466 vlc_mutex_unlock( &p_owner->lock );
470 vlc_object_kill( p_cc );
471 module_unneed( p_cc, p_cc->p_module );
472 DeleteDecoder( p_cc );
473 vlc_object_release( p_cc );
478 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
480 decoder_owner_sys_t *p_owner = p_dec->p_owner;
483 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
486 vlc_mutex_lock( &p_owner->lock );
487 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
488 vlc_mutex_unlock( &p_owner->lock );
492 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
494 decoder_owner_sys_t *p_owner = p_dec->p_owner;
496 vlc_mutex_lock( &p_owner->lock );
498 assert( !p_owner->b_paused || !b_paused );
500 p_owner->b_paused = b_paused;
501 p_owner->pause.i_date = i_date;
502 p_owner->pause.i_ignore = 0;
503 vlc_cond_signal( &p_owner->wait );
505 DecoderOutputChangePause( p_dec, b_paused, i_date );
507 vlc_mutex_unlock( &p_owner->lock );
510 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
512 decoder_owner_sys_t *p_owner = p_dec->p_owner;
514 vlc_mutex_lock( &p_owner->lock );
515 p_owner->i_ts_delay = i_delay;
516 vlc_mutex_unlock( &p_owner->lock );
519 void input_DecoderStartBuffering( decoder_t *p_dec )
521 decoder_owner_sys_t *p_owner = p_dec->p_owner;
523 vlc_mutex_lock( &p_owner->lock );
525 DecoderFlush( p_dec );
527 p_owner->buffer.b_first = true;
528 p_owner->buffer.b_full = false;
529 p_owner->buffer.i_count = 0;
531 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
532 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
534 p_owner->buffer.p_picture = NULL;
535 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
537 p_owner->buffer.p_subpic = NULL;
538 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
540 p_owner->buffer.p_audio = NULL;
541 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
543 p_owner->buffer.p_block = NULL;
544 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
547 p_owner->b_buffering = true;
549 vlc_cond_signal( &p_owner->wait );
551 vlc_mutex_unlock( &p_owner->lock );
554 void input_DecoderStopBuffering( decoder_t *p_dec )
556 decoder_owner_sys_t *p_owner = p_dec->p_owner;
558 vlc_mutex_lock( &p_owner->lock );
560 p_owner->b_buffering = false;
562 vlc_cond_signal( &p_owner->wait );
564 vlc_mutex_unlock( &p_owner->lock );
567 void input_DecoderWaitBuffering( decoder_t *p_dec )
569 decoder_owner_sys_t *p_owner = p_dec->p_owner;
571 vlc_mutex_lock( &p_owner->lock );
573 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
575 block_FifoWake( p_owner->p_fifo );
576 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
579 vlc_mutex_unlock( &p_owner->lock );
582 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
584 decoder_owner_sys_t *p_owner = p_dec->p_owner;
588 vlc_mutex_lock( &p_owner->lock );
589 if( p_dec->fmt_in.i_cat == VIDEO_ES )
591 if( p_owner->b_paused && p_owner->p_vout )
593 vout_NextPicture( p_owner->p_vout, pi_duration );
594 p_owner->pause.i_ignore++;
595 vlc_cond_signal( &p_owner->wait );
600 /* TODO subtitle should not be flushed */
601 DecoderFlush( p_dec );
603 vlc_mutex_unlock( &p_owner->lock );
606 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
608 decoder_owner_sys_t *p_owner = p_dec->p_owner;
611 vlc_mutex_lock( &p_owner->lock );
612 b_changed = p_owner->b_fmt_description;
616 es_format_Copy( p_fmt, &p_owner->fmt_description );
621 if( p_owner->p_description )
623 *pp_meta = vlc_meta_New();
625 vlc_meta_Merge( *pp_meta, p_owner->p_description );
628 p_owner->b_fmt_description = false;
630 vlc_mutex_unlock( &p_owner->lock );
634 /*****************************************************************************
636 *****************************************************************************/
637 static int DecoderGetInputAttachments( decoder_t *p_dec,
638 input_attachment_t ***ppp_attachment,
641 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
642 ppp_attachment, pi_attachment );
644 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
646 decoder_owner_sys_t *p_owner = p_dec->p_owner;
648 vlc_mutex_lock( &p_owner->lock );
649 if( p_owner->b_buffering || p_owner->b_paused )
651 vlc_mutex_unlock( &p_owner->lock );
653 if( !p_owner->p_clock || !i_ts )
656 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
658 static int DecoderGetDisplayRate( decoder_t *p_dec )
660 decoder_owner_sys_t *p_owner = p_dec->p_owner;
662 if( !p_owner->p_clock )
663 return INPUT_RATE_DEFAULT;
664 return input_clock_GetRate( p_owner->p_clock );
668 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
670 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
671 "VLC probably does not support this sound or video format.",
673 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
674 _("VLC does not support the audio or video format \"%4.4s\". "
675 "Unfortunately there is no way for you to fix this."), (char*)&codec );
680 * Create a decoder object
682 * \param p_input the input thread
683 * \param p_es the es descriptor
684 * \param i_object_type Object type as define in include/vlc_objects.h
685 * \return the decoder object
687 static decoder_t * CreateDecoder( input_thread_t *p_input,
688 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
691 decoder_owner_sys_t *p_owner;
694 p_dec = vlc_object_create( p_input, i_object_type );
698 p_dec->pf_decode_audio = NULL;
699 p_dec->pf_decode_video = NULL;
700 p_dec->pf_decode_sub = NULL;
701 p_dec->pf_get_cc = NULL;
702 p_dec->pf_packetize = NULL;
704 /* Initialize the decoder */
705 p_dec->p_module = NULL;
707 memset( &null_es_format, 0, sizeof(es_format_t) );
708 es_format_Copy( &p_dec->fmt_in, fmt );
709 es_format_Copy( &p_dec->fmt_out, &null_es_format );
711 p_dec->p_description = NULL;
713 /* Allocate our private structure for the decoder */
714 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
715 if( p_dec->p_owner == NULL )
717 vlc_object_release( p_dec );
720 p_dec->p_owner->i_preroll_end = -1;
721 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
722 p_dec->p_owner->p_input = p_input;
723 p_dec->p_owner->p_aout = NULL;
724 p_dec->p_owner->p_aout_input = NULL;
725 p_dec->p_owner->p_vout = NULL;
726 p_dec->p_owner->p_spu_vout = NULL;
727 p_dec->p_owner->i_spu_channel = 0;
728 p_dec->p_owner->i_spu_order = 0;
729 p_dec->p_owner->p_sout = p_sout;
730 p_dec->p_owner->p_sout_input = NULL;
731 p_dec->p_owner->p_packetizer = NULL;
734 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
736 free( p_dec->p_owner );
737 vlc_object_release( p_dec );
741 /* Set buffers allocation callbacks for the decoders */
742 p_dec->pf_aout_buffer_new = aout_new_buffer;
743 p_dec->pf_aout_buffer_del = aout_del_buffer;
744 p_dec->pf_vout_buffer_new = vout_new_buffer;
745 p_dec->pf_vout_buffer_del = vout_del_buffer;
746 p_dec->pf_picture_link = vout_link_picture;
747 p_dec->pf_picture_unlink = vout_unlink_picture;
748 p_dec->pf_spu_buffer_new = spu_new_buffer;
749 p_dec->pf_spu_buffer_del = spu_del_buffer;
751 p_dec->pf_get_attachments = DecoderGetInputAttachments;
752 p_dec->pf_get_display_date = DecoderGetDisplayDate;
753 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
755 vlc_object_attach( p_dec, p_input );
757 /* Find a suitable decoder/packetizer module */
758 if( i_object_type == VLC_OBJECT_DECODER )
759 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
761 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
763 /* Check if decoder requires already packetized data */
764 if( i_object_type == VLC_OBJECT_DECODER &&
765 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
767 p_dec->p_owner->p_packetizer =
768 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
769 if( p_dec->p_owner->p_packetizer )
771 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
774 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
777 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
779 p_dec->p_owner->p_packetizer->p_module =
780 module_need( p_dec->p_owner->p_packetizer,
781 "packetizer", "$packetizer", false );
783 if( !p_dec->p_owner->p_packetizer->p_module )
785 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
786 vlc_object_detach( p_dec->p_owner->p_packetizer );
787 vlc_object_release( p_dec->p_owner->p_packetizer );
792 /* Copy ourself the input replay gain */
793 if( fmt->i_cat == AUDIO_ES )
795 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
797 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
799 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
800 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
802 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
804 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
805 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
811 vlc_mutex_init( &p_owner->lock );
812 vlc_cond_init( &p_owner->wait );
814 p_owner->b_fmt_description = false;
815 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
816 p_owner->p_description = NULL;
818 p_owner->b_paused = false;
819 p_owner->pause.i_date = 0;
820 p_owner->pause.i_ignore = 0;
822 p_owner->b_buffering = false;
823 p_owner->buffer.b_first = true;
824 p_owner->buffer.b_full = false;
825 p_owner->buffer.i_count = 0;
826 p_owner->buffer.p_picture = NULL;
827 p_owner->buffer.p_subpic = NULL;
828 p_owner->buffer.p_audio = NULL;
829 p_owner->buffer.p_block = NULL;
831 p_owner->b_flushing = false;
834 p_owner->cc.b_supported = false;
835 if( i_object_type == VLC_OBJECT_DECODER )
837 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
838 p_owner->cc.b_supported = true;
839 if( p_dec->pf_get_cc )
840 p_owner->cc.b_supported = true;
843 for( i = 0; i < 4; i++ )
845 p_owner->cc.pb_present[i] = false;
846 p_owner->cc.pp_decoder[i] = NULL;
848 p_owner->i_ts_delay = 0;
853 * The decoding main loop
855 * \param p_dec the decoder
857 static void *DecoderThread( vlc_object_t *p_this )
859 decoder_t *p_dec = (decoder_t *)p_this;
860 decoder_owner_sys_t *p_owner = p_dec->p_owner;
862 /* The decoder's main loop */
865 block_t *p_block = block_FifoGet( p_owner->p_fifo );
867 /* Make sure there is no cancellation point other than this one^^.
868 * If you need one, be sure to push cleanup of p_block. */
869 DecoderSignalBuffering( p_dec, p_block == NULL );
873 int canc = vlc_savecancel();
876 DecoderError( p_dec, p_block );
878 DecoderProcess( p_dec, p_block );
880 vlc_restorecancel( canc );
886 static void DecoderFlush( decoder_t *p_dec )
888 decoder_owner_sys_t *p_owner = p_dec->p_owner;
891 vlc_assert_locked( &p_owner->lock );
894 block_FifoEmpty( p_owner->p_fifo );
896 /* Monitor for flush end */
897 p_owner->b_flushing = true;
898 vlc_cond_signal( &p_owner->wait );
900 /* Send a special block */
901 p_null = block_New( p_dec, 128 );
904 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
905 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
906 if( !p_dec->fmt_in.b_packetized )
907 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
908 memset( p_null->p_buffer, 0, p_null->i_buffer );
910 input_DecoderDecode( p_dec, p_null );
913 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
914 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
917 static void DecoderSignalFlushed( decoder_t *p_dec )
919 decoder_owner_sys_t *p_owner = p_dec->p_owner;
921 vlc_mutex_lock( &p_owner->lock );
923 if( p_owner->b_flushing )
925 p_owner->b_flushing = false;
926 vlc_cond_signal( &p_owner->wait );
929 vlc_mutex_unlock( &p_owner->lock );
932 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
934 decoder_owner_sys_t *p_owner = p_dec->p_owner;
936 vlc_mutex_lock( &p_owner->lock );
938 if( p_owner->b_buffering )
941 p_owner->buffer.b_full = true;
942 vlc_cond_signal( &p_owner->wait );
945 vlc_mutex_unlock( &p_owner->lock );
948 static bool DecoderIsFlushing( decoder_t *p_dec )
950 decoder_owner_sys_t *p_owner = p_dec->p_owner;
953 vlc_mutex_lock( &p_owner->lock );
955 b_flushing = p_owner->b_flushing;
957 vlc_mutex_unlock( &p_owner->lock );
962 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
964 decoder_owner_sys_t *p_owner = p_dec->p_owner;
966 vlc_assert_locked( &p_owner->lock );
968 while( !p_owner->b_flushing )
970 if( p_owner->b_paused )
972 if( p_owner->b_buffering && !p_owner->buffer.b_full )
974 if( p_owner->pause.i_ignore > 0 )
976 p_owner->pause.i_ignore--;
982 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
985 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
989 *pb_reject = p_owner->b_flushing;
992 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
994 decoder_owner_sys_t *p_owner = p_dec->p_owner;
996 vlc_assert_locked( &p_owner->lock );
998 /* XXX only audio and video output have to be paused.
999 * - for sout it is useless
1000 * - for subs, it is done by the vout
1002 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1004 if( p_owner->p_aout && p_owner->p_aout_input )
1005 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1008 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1010 if( p_owner->p_vout )
1011 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1014 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1016 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1017 *pi_preroll = INT64_MAX;
1018 else if( p->i_pts > 0 )
1019 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1020 else if( p->i_dts > 0 )
1021 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1024 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
1026 mtime_t current_date = mdate();
1028 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1029 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1031 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1032 * for teletext streams. */
1033 return current_date + 400000 + i_ts_delay;
1038 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1039 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1041 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1042 input_clock_t *p_clock = p_owner->p_clock;
1045 vlc_assert_locked( &p_owner->lock );
1047 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
1048 const mtime_t i_es_delay = p_owner->i_ts_delay;
1052 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1055 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1056 *pi_ts0 + i_es_delay );
1057 if( pi_ts1 && *pi_ts1 > 0 )
1058 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1059 *pi_ts1 + i_es_delay );
1061 /* Do not create ephemere data because of rounding errors */
1062 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1066 i_rate = input_clock_GetRate( p_clock );
1069 *pi_duration = ( *pi_duration * i_rate +
1070 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1077 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1078 if( pi_ts1 && *pi_ts1 <= 0 )
1084 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1085 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1089 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1090 int *pi_played_sum, int *pi_lost_sum )
1092 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1093 aout_instance_t *p_aout = p_owner->p_aout;
1094 aout_input_t *p_aout_input = p_owner->p_aout_input;
1097 if( p_audio->start_date <= 0 )
1099 msg_Warn( p_dec, "non-dated audio buffer received" );
1101 aout_BufferFree( p_audio );
1106 vlc_mutex_lock( &p_owner->lock );
1108 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1110 p_audio->p_next = NULL;
1112 *p_owner->buffer.pp_audio_next = p_audio;
1113 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1115 p_owner->buffer.i_count++;
1116 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1117 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1119 p_owner->buffer.b_full = true;
1120 vlc_cond_signal( &p_owner->wait );
1126 bool b_has_more = false;
1128 DecoderWaitUnblock( p_dec, &b_reject );
1130 if( p_owner->b_buffering )
1132 vlc_mutex_unlock( &p_owner->lock );
1137 if( p_owner->buffer.p_audio )
1139 p_audio = p_owner->buffer.p_audio;
1141 p_owner->buffer.p_audio = p_audio->p_next;
1142 p_owner->buffer.i_count--;
1144 b_has_more = p_owner->buffer.p_audio != NULL;
1146 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1150 int i_rate = INPUT_RATE_DEFAULT;
1153 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1154 &i_rate, &i_delay, false );
1156 vlc_mutex_unlock( &p_owner->lock );
1159 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1161 if( !p_aout || !p_aout_input ||
1162 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1163 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1164 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1167 /* Do not wait against unprotected date */
1168 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1169 while( !b_reject && i_deadline > mdate() )
1171 vlc_mutex_lock( &p_owner->lock );
1172 if( p_owner->b_flushing || p_dec->b_die )
1175 vlc_mutex_unlock( &p_owner->lock );
1178 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1179 vlc_mutex_unlock( &p_owner->lock );
1184 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1185 *pi_played_sum += 1;
1186 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1190 if( p_audio->start_date <= 0 )
1192 msg_Warn( p_dec, "non-dated audio buffer received" );
1194 else if( p_audio->start_date > i_max_date )
1196 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1197 p_audio->start_date - mdate() );
1200 aout_BufferFree( p_audio );
1206 vlc_mutex_lock( &p_owner->lock );
1207 if( !p_owner->buffer.p_audio )
1209 vlc_mutex_unlock( &p_owner->lock );
1215 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1217 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1218 input_thread_t *p_input = p_owner->p_input;
1219 aout_buffer_t *p_aout_buf;
1224 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1226 aout_instance_t *p_aout = p_owner->p_aout;
1227 aout_input_t *p_aout_input = p_owner->p_aout_input;
1231 /* It prevent freezing VLC in case of broken decoder */
1232 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1234 block_Release( p_block );
1239 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1241 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1245 if( p_owner->i_preroll_end > 0 )
1247 msg_Dbg( p_dec, "End of audio preroll" );
1248 if( p_owner->p_aout && p_owner->p_aout_input )
1249 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1251 p_owner->i_preroll_end = -1;
1254 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1257 /* Update ugly stat */
1258 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1260 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1262 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1264 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1266 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1269 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1272 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1274 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1280 assert( p_dec_cc->pf_get_cc != NULL );
1282 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1283 if( !p_owner->cc.b_supported )
1286 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1290 vlc_mutex_lock( &p_owner->lock );
1291 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1293 p_owner->cc.pb_present[i] |= pb_present[i];
1294 if( p_owner->cc.pp_decoder[i] )
1298 for( i = 0; i < 4; i++ )
1300 if( !p_owner->cc.pp_decoder[i] )
1303 if( i_cc_decoder > 1 )
1304 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1306 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1309 vlc_mutex_unlock( &p_owner->lock );
1312 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1313 int *pi_played_sum, int *pi_lost_sum )
1315 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1316 vout_thread_t *p_vout = p_owner->p_vout;
1317 bool b_first_buffered;
1319 if( p_picture->date <= 0 )
1321 msg_Warn( p_vout, "non-dated video buffer received" );
1323 vout_DropPicture( p_vout, p_picture );
1328 vlc_mutex_lock( &p_owner->lock );
1330 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1332 p_picture->p_next = NULL;
1334 *p_owner->buffer.pp_picture_next = p_picture;
1335 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1337 p_owner->buffer.i_count++;
1338 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1339 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1341 p_owner->buffer.b_full = true;
1342 vlc_cond_signal( &p_owner->wait );
1345 b_first_buffered = p_owner->buffer.p_picture != NULL;
1347 for( ;; b_first_buffered = false )
1349 bool b_has_more = false;
1353 DecoderWaitUnblock( p_dec, &b_reject );
1355 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1357 vlc_mutex_unlock( &p_owner->lock );
1360 bool b_buffering_first = p_owner->b_buffering;
1363 if( p_owner->buffer.p_picture )
1365 p_picture = p_owner->buffer.p_picture;
1367 p_owner->buffer.p_picture = p_picture->p_next;
1368 p_owner->buffer.i_count--;
1370 b_has_more = p_owner->buffer.p_picture != NULL;
1372 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1376 if( b_buffering_first )
1378 assert( p_owner->buffer.b_first );
1379 assert( !p_owner->buffer.i_count );
1380 msg_Dbg( p_dec, "Received first picture" );
1381 p_owner->buffer.b_first = false;
1382 p_picture->b_force = true;
1385 int i_rate = INPUT_RATE_DEFAULT;
1387 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1388 &i_rate, &i_delay, false );
1390 vlc_mutex_unlock( &p_owner->lock );
1393 const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1395 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1400 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1402 /* Be sure to not display old picture after our own */
1403 vout_Flush( p_vout, p_picture->date );
1404 p_owner->i_last_rate = i_rate;
1406 vout_DisplayPicture( p_vout, p_picture );
1410 if( p_picture->date <= 0 )
1412 msg_Warn( p_vout, "non-dated video buffer received" );
1416 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1417 p_picture->date - mdate() );
1420 vout_DropPicture( p_vout, p_picture );
1424 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1426 *pi_played_sum += i_tmp_display;
1427 *pi_lost_sum += i_tmp_lost;
1429 if( !b_has_more || b_buffering_first )
1432 vlc_mutex_lock( &p_owner->lock );
1433 if( !p_owner->buffer.p_picture )
1435 vlc_mutex_unlock( &p_owner->lock );
1441 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1443 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1444 input_thread_t *p_input = p_owner->p_input;
1448 int i_displayed = 0;
1450 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1452 vout_thread_t *p_vout = p_owner->p_vout;
1455 /* It prevent freezing VLC in case of broken decoder */
1456 vout_DropPicture( p_vout, p_pic );
1458 block_Release( p_block );
1464 if( p_pic->date < p_owner->i_preroll_end )
1466 vout_DropPicture( p_vout, p_pic );
1470 if( p_owner->i_preroll_end > 0 )
1472 msg_Dbg( p_dec, "End of video preroll" );
1474 vout_Flush( p_vout, 1 );
1476 p_owner->i_preroll_end = -1;
1479 if( p_dec->pf_get_cc &&
1480 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1481 DecoderGetCc( p_dec, p_dec );
1483 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1485 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1487 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1489 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1491 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1494 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1497 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1501 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1504 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1505 vout_thread_t *p_vout = p_owner->p_spu_vout;
1508 if( p_subpic->i_start <= 0 )
1510 msg_Warn( p_dec, "non-dated spu buffer received" );
1511 subpicture_Delete( p_subpic );
1516 vlc_mutex_lock( &p_owner->lock );
1518 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1520 p_subpic->p_next = NULL;
1522 *p_owner->buffer.pp_subpic_next = p_subpic;
1523 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1525 p_owner->buffer.i_count++;
1526 /* XXX it is important to be full after the first one */
1527 if( p_owner->buffer.i_count > 0 )
1529 p_owner->buffer.b_full = true;
1530 vlc_cond_signal( &p_owner->wait );
1536 bool b_has_more = false;
1538 DecoderWaitUnblock( p_dec, &b_reject );
1540 if( p_owner->b_buffering )
1542 vlc_mutex_unlock( &p_owner->lock );
1547 if( p_owner->buffer.p_subpic )
1549 p_subpic = p_owner->buffer.p_subpic;
1551 p_owner->buffer.p_subpic = p_subpic->p_next;
1552 p_owner->buffer.i_count--;
1554 b_has_more = p_owner->buffer.p_subpic != NULL;
1556 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1560 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1561 NULL, NULL, b_telx );
1563 vlc_mutex_unlock( &p_owner->lock );
1566 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1568 subpicture_Delete( p_subpic );
1572 vlc_mutex_lock( &p_owner->lock );
1573 if( !p_owner->buffer.p_subpic )
1575 vlc_mutex_unlock( &p_owner->lock );
1581 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1584 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1586 assert( p_owner->p_clock );
1587 assert( !p_sout_block->p_next );
1589 vlc_mutex_lock( &p_owner->lock );
1591 if( p_owner->b_buffering || p_owner->buffer.p_block )
1593 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1595 p_owner->buffer.i_count++;
1596 /* XXX it is important to be full after the first one */
1597 if( p_owner->buffer.i_count > 0 )
1599 p_owner->buffer.b_full = true;
1600 vlc_cond_signal( &p_owner->wait );
1606 bool b_has_more = false;
1608 DecoderWaitUnblock( p_dec, &b_reject );
1610 if( p_owner->b_buffering )
1612 vlc_mutex_unlock( &p_owner->lock );
1617 if( p_owner->buffer.p_block )
1619 p_sout_block = p_owner->buffer.p_block;
1621 p_owner->buffer.p_block = p_sout_block->p_next;
1622 p_owner->buffer.i_count--;
1624 b_has_more = p_owner->buffer.p_block != NULL;
1626 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1628 p_sout_block->p_next = NULL;
1630 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1631 &p_sout_block->i_length,
1632 &p_sout_block->i_rate, NULL, b_telx );
1634 vlc_mutex_unlock( &p_owner->lock );
1637 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1639 block_Release( p_sout_block );
1643 vlc_mutex_lock( &p_owner->lock );
1644 if( !p_owner->buffer.p_block )
1646 vlc_mutex_unlock( &p_owner->lock );
1653 static void DecoderFlushBuffering( decoder_t *p_dec )
1655 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1657 vlc_assert_locked( &p_owner->lock );
1659 while( p_owner->buffer.p_picture )
1661 picture_t *p_picture = p_owner->buffer.p_picture;
1663 p_owner->buffer.p_picture = p_picture->p_next;
1664 p_owner->buffer.i_count--;
1666 if( p_owner->p_vout )
1667 vout_DropPicture( p_owner->p_vout, p_picture );
1669 if( !p_owner->buffer.p_picture )
1670 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1672 while( p_owner->buffer.p_audio )
1674 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1676 p_owner->buffer.p_audio = p_audio->p_next;
1677 p_owner->buffer.i_count--;
1679 aout_BufferFree( p_audio );
1681 if( !p_owner->buffer.p_audio )
1682 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1684 while( p_owner->buffer.p_subpic )
1686 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1688 p_owner->buffer.p_subpic = p_subpic->p_next;
1689 p_owner->buffer.i_count--;
1691 subpicture_Delete( p_subpic );
1693 if( !p_owner->buffer.p_subpic )
1694 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1696 if( p_owner->buffer.p_block )
1698 block_ChainRelease( p_owner->buffer.p_block );
1700 p_owner->buffer.i_count = 0;
1701 p_owner->buffer.p_block = NULL;
1702 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1706 /* This function process a block for sout
1708 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1710 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1711 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1712 block_t *p_sout_block;
1714 while( ( p_sout_block =
1715 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1717 if( !p_owner->p_sout_input )
1719 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1721 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1722 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1723 if( p_dec->fmt_in.psz_language )
1725 if( p_owner->sout.psz_language )
1726 free( p_owner->sout.psz_language );
1727 p_owner->sout.psz_language =
1728 strdup( p_dec->fmt_in.psz_language );
1731 p_owner->p_sout_input =
1732 sout_InputNew( p_owner->p_sout,
1735 if( p_owner->p_sout_input == NULL )
1737 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1738 (char *)&p_owner->sout.i_codec );
1739 p_dec->b_error = true;
1741 while( p_sout_block )
1743 block_t *p_next = p_sout_block->p_next;
1744 block_Release( p_sout_block );
1745 p_sout_block = p_next;
1751 while( p_sout_block )
1753 block_t *p_next = p_sout_block->p_next;
1755 p_sout_block->p_next = NULL;
1757 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1759 p_sout_block = p_next;
1762 /* For now it's enough, as only sout impact on this flag */
1763 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1764 p_owner->p_input->p->b_out_pace_control )
1766 msg_Dbg( p_dec, "switching to sync mode" );
1767 p_owner->p_input->p->b_out_pace_control = false;
1769 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1770 !p_owner->p_input->p->b_out_pace_control )
1772 msg_Dbg( p_dec, "switching to async mode" );
1773 p_owner->p_input->p->b_out_pace_control = true;
1778 /* This function process a video block
1780 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1782 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1784 if( p_owner->p_packetizer )
1786 block_t *p_packetized_block;
1787 decoder_t *p_packetizer = p_owner->p_packetizer;
1789 while( (p_packetized_block =
1790 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1792 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1794 es_format_Clean( &p_dec->fmt_in );
1795 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1797 if( p_packetizer->pf_get_cc )
1798 DecoderGetCc( p_dec, p_packetizer );
1800 while( p_packetized_block )
1802 block_t *p_next = p_packetized_block->p_next;
1803 p_packetized_block->p_next = NULL;
1805 DecoderDecodeVideo( p_dec, p_packetized_block );
1807 p_packetized_block = p_next;
1813 DecoderDecodeVideo( p_dec, p_block );
1816 if( b_flush && p_owner->p_vout )
1817 vout_Flush( p_owner->p_vout, 1 );
1820 /* This function process a audio block
1822 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1824 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1826 if( p_owner->p_packetizer )
1828 block_t *p_packetized_block;
1829 decoder_t *p_packetizer = p_owner->p_packetizer;
1831 while( (p_packetized_block =
1832 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1834 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1836 es_format_Clean( &p_dec->fmt_in );
1837 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1840 while( p_packetized_block )
1842 block_t *p_next = p_packetized_block->p_next;
1843 p_packetized_block->p_next = NULL;
1845 DecoderDecodeAudio( p_dec, p_packetized_block );
1847 p_packetized_block = p_next;
1853 DecoderDecodeAudio( p_dec, p_block );
1856 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1857 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1860 /* This function process a subtitle block
1862 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1864 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1865 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1867 input_thread_t *p_input = p_owner->p_input;
1868 vout_thread_t *p_vout;
1869 subpicture_t *p_spu;
1871 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1873 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1874 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1875 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1877 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1878 if( p_vout && p_owner->p_spu_vout == p_vout )
1880 /* Preroll does not work very well with subtitle */
1881 if( p_spu->i_start > 0 &&
1882 p_spu->i_start < p_owner->i_preroll_end &&
1883 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1885 subpicture_Delete( p_spu );
1889 DecoderPlaySpu( p_dec, p_spu, b_telx );
1894 subpicture_Delete( p_spu );
1897 vlc_object_release( p_vout );
1900 if( b_flush && p_owner->p_spu_vout )
1902 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1904 if( p_vout && p_owner->p_spu_vout == p_vout )
1905 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1906 p_owner->i_spu_channel );
1909 vlc_object_release( p_vout );
1914 static void DecoderProcessOnFlush( decoder_t *p_dec )
1916 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1918 vlc_mutex_lock( &p_owner->lock );
1919 DecoderFlushBuffering( p_dec );
1920 vlc_mutex_unlock( &p_owner->lock );
1922 DecoderSignalFlushed( p_dec );
1927 * \param p_dec the decoder object
1928 * \param p_block the block to decode
1929 * \return VLC_SUCCESS or an error code
1931 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1933 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1934 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1936 if( p_block && p_block->i_buffer <= 0 )
1938 assert( !b_flush_request );
1939 block_Release( p_block );
1944 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1947 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1949 DecoderProcessSout( p_dec, p_block );
1954 bool b_flush = false;
1958 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1959 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1961 b_flush = !b_flushing && b_flush_request;
1963 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1966 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1968 DecoderProcessAudio( p_dec, p_block, b_flush );
1970 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1972 DecoderProcessVideo( p_dec, p_block, b_flush );
1974 else if( p_dec->fmt_in.i_cat == SPU_ES )
1976 DecoderProcessSpu( p_dec, p_block, b_flush );
1980 msg_Err( p_dec, "unknown ES format" );
1981 p_dec->b_error = true;
1986 if( b_flush_request )
1987 DecoderProcessOnFlush( p_dec );
1990 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1992 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1996 block_Release( p_block );
1998 if( b_flush_request )
1999 DecoderProcessOnFlush( p_dec );
2004 * Destroys a decoder object
2006 * \param p_dec the decoder object
2009 static void DeleteDecoder( decoder_t * p_dec )
2011 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2013 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2014 (char*)&p_dec->fmt_in.i_codec,
2015 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2017 /* Free all packets still in the decoder fifo. */
2018 block_FifoEmpty( p_owner->p_fifo );
2019 block_FifoRelease( p_owner->p_fifo );
2022 vlc_mutex_lock( &p_owner->lock );
2023 DecoderFlushBuffering( p_dec );
2024 vlc_mutex_unlock( &p_owner->lock );
2027 if( p_owner->p_aout_input )
2028 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2029 if( p_owner->p_aout )
2031 input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2033 input_SendEventAout( p_owner->p_input );
2034 p_owner->p_aout = NULL;
2036 if( p_owner->p_vout )
2038 /* Hack to make sure all the the pictures are freed by the decoder */
2039 vout_FixLeaks( p_owner->p_vout, true );
2042 input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
2043 input_SendEventVout( p_owner->p_input );
2047 if( p_owner->p_sout_input )
2049 sout_InputDelete( p_owner->p_sout_input );
2050 es_format_Clean( &p_owner->sout );
2054 if( p_dec->fmt_in.i_cat == SPU_ES )
2056 vout_thread_t *p_vout;
2058 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2061 if( p_owner->p_spu_vout == p_vout )
2062 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2063 vlc_object_release( p_vout );
2067 es_format_Clean( &p_dec->fmt_in );
2068 es_format_Clean( &p_dec->fmt_out );
2069 if( p_dec->p_description )
2070 vlc_meta_Delete( p_dec->p_description );
2071 es_format_Clean( &p_owner->fmt_description );
2072 if( p_owner->p_description )
2073 vlc_meta_Delete( p_owner->p_description );
2075 if( p_owner->p_packetizer )
2077 module_unneed( p_owner->p_packetizer,
2078 p_owner->p_packetizer->p_module );
2079 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2080 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2081 if( p_owner->p_packetizer->p_description )
2082 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2083 vlc_object_detach( p_owner->p_packetizer );
2084 vlc_object_release( p_owner->p_packetizer );
2087 vlc_cond_destroy( &p_owner->wait );
2088 vlc_mutex_destroy( &p_owner->lock );
2090 vlc_object_detach( p_dec );
2095 /*****************************************************************************
2096 * Buffers allocation callbacks for the decoders
2097 *****************************************************************************/
2098 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2100 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2102 vlc_assert_locked( &p_owner->lock );
2104 p_owner->b_fmt_description = true;
2106 /* Copy es_format */
2107 es_format_Clean( &p_owner->fmt_description );
2108 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2110 /* Move p_description */
2111 if( p_owner->p_description && p_dec->p_description )
2112 vlc_meta_Delete( p_owner->p_description );
2113 p_owner->p_description = p_dec->p_description;
2114 p_dec->p_description = NULL;
2116 static vout_thread_t *aout_request_vout( void *p_private,
2117 vout_thread_t *p_vout, video_format_t *p_fmt )
2119 decoder_t *p_dec = p_private;
2121 p_vout = input_ressource_RequestVout( p_dec->p_owner->p_input->p->p_ressource, p_vout, p_fmt );
2122 input_SendEventVout( p_dec->p_owner->p_input );
2127 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2129 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2130 aout_buffer_t *p_buffer;
2132 if( p_owner->p_aout_input != NULL &&
2133 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2134 p_dec->fmt_out.audio.i_original_channels !=
2135 p_owner->audio.i_original_channels ||
2136 p_dec->fmt_out.audio.i_bytes_per_frame !=
2137 p_owner->audio.i_bytes_per_frame ) )
2139 aout_input_t *p_aout_input = p_owner->p_aout_input;
2141 /* Parameters changed, restart the aout */
2142 vlc_mutex_lock( &p_owner->lock );
2144 DecoderFlushBuffering( p_dec );
2146 p_owner->p_aout_input = NULL;
2147 aout_DecDelete( p_owner->p_aout, p_aout_input );
2149 vlc_mutex_unlock( &p_owner->lock );
2152 if( p_owner->p_aout_input == NULL )
2154 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2155 audio_sample_format_t format;
2156 aout_input_t *p_aout_input;
2157 aout_instance_t *p_aout;
2158 aout_request_vout_t request_vout;
2160 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2161 p_owner->audio = p_dec->fmt_out.audio;
2163 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2164 if( i_force_dolby &&
2165 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2166 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2168 if( i_force_dolby == 1 )
2170 format.i_original_channels = format.i_original_channels |
2171 AOUT_CHAN_DOLBYSTEREO;
2173 else /* i_force_dolby == 2 */
2175 format.i_original_channels = format.i_original_channels &
2176 ~AOUT_CHAN_DOLBYSTEREO;
2180 request_vout.pf_request_vout = aout_request_vout;
2181 request_vout.p_private = p_dec;
2183 p_aout = p_owner->p_aout;
2185 p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2186 p_aout_input = aout_DecNew( p_dec, &p_aout,
2187 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2189 vlc_mutex_lock( &p_owner->lock );
2191 p_owner->p_aout = p_aout;
2192 p_owner->p_aout_input = p_aout_input;
2193 DecoderUpdateFormatLocked( p_dec );
2195 vlc_mutex_unlock( &p_owner->lock );
2197 input_SendEventAout( p_owner->p_input );
2199 if( p_owner->p_aout_input == NULL )
2201 msg_Err( p_dec, "failed to create audio output" );
2202 p_dec->b_error = true;
2205 p_dec->fmt_out.audio.i_bytes_per_frame =
2206 p_owner->audio.i_bytes_per_frame;
2209 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2214 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2216 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2218 aout_DecDeleteBuffer( p_owner->p_aout,
2219 p_owner->p_aout_input, p_buffer );
2223 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2225 static picture_t *vout_new_buffer( decoder_t *p_dec )
2227 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2229 if( p_owner->p_vout == NULL ||
2230 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2231 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2232 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2233 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2235 vout_thread_t *p_vout;
2237 if( !p_dec->fmt_out.video.i_width ||
2238 !p_dec->fmt_out.video.i_height )
2240 /* Can't create a new vout without display size */
2244 if( !p_dec->fmt_out.video.i_visible_width ||
2245 !p_dec->fmt_out.video.i_visible_height )
2247 if( p_dec->fmt_in.video.i_visible_width &&
2248 p_dec->fmt_in.video.i_visible_height )
2250 p_dec->fmt_out.video.i_visible_width =
2251 p_dec->fmt_in.video.i_visible_width;
2252 p_dec->fmt_out.video.i_visible_height =
2253 p_dec->fmt_in.video.i_visible_height;
2257 p_dec->fmt_out.video.i_visible_width =
2258 p_dec->fmt_out.video.i_width;
2259 p_dec->fmt_out.video.i_visible_height =
2260 p_dec->fmt_out.video.i_height;
2264 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2265 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2267 p_dec->fmt_out.video.i_visible_height = 1080;
2268 p_dec->fmt_out.video.i_sar_num *= 135;
2269 p_dec->fmt_out.video.i_sar_den *= 136;
2270 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2273 if( !p_dec->fmt_out.video.i_sar_num ||
2274 !p_dec->fmt_out.video.i_sar_den )
2276 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2277 p_dec->fmt_out.video.i_visible_height;
2279 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2280 p_dec->fmt_out.video.i_visible_width;
2283 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2284 &p_dec->fmt_out.video.i_sar_den,
2285 p_dec->fmt_out.video.i_sar_num,
2286 p_dec->fmt_out.video.i_sar_den, 50000 );
2288 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2289 p_owner->video = p_dec->fmt_out.video;
2291 vlc_mutex_lock( &p_owner->lock );
2293 DecoderFlushBuffering( p_dec );
2295 p_vout = p_owner->p_vout;
2296 p_owner->p_vout = NULL;
2297 vlc_mutex_unlock( &p_owner->lock );
2299 p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
2300 p_vout, &p_dec->fmt_out.video );
2302 vlc_mutex_lock( &p_owner->lock );
2303 p_owner->p_vout = p_vout;
2305 DecoderUpdateFormatLocked( p_dec );
2307 vlc_mutex_unlock( &p_owner->lock );
2309 input_SendEventVout( p_owner->p_input );
2310 if( p_vout == NULL )
2312 msg_Err( p_dec, "failed to create video output" );
2313 p_dec->b_error = true;
2317 if( p_owner->video.i_rmask )
2318 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2319 if( p_owner->video.i_gmask )
2320 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2321 if( p_owner->video.i_bmask )
2322 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2325 /* Get a new picture
2329 picture_t *p_picture;
2331 if( p_dec->b_die || p_dec->b_error )
2334 /* The video filter chain required that there is always 1 free buffer
2335 * that it will use as temporary one. It will release the temporary
2336 * buffer once its work is done, so this check is safe even if we don't
2337 * lock around both count() and create().
2339 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2341 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2346 if( DecoderIsFlushing( p_dec ) )
2350 DecoderSignalBuffering( p_dec, true );
2352 /* Check the decoder doesn't leak pictures */
2353 vout_FixLeaks( p_owner->p_vout, false );
2355 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2356 msleep( VOUT_OUTMEM_SLEEP );
2360 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2362 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2365 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2367 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2370 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2372 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2375 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2377 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2378 vout_thread_t *p_vout = NULL;
2379 subpicture_t *p_subpic;
2380 int i_attempts = 30;
2382 while( i_attempts-- )
2384 if( p_dec->b_die || p_dec->b_error )
2387 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2391 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2396 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2400 if( p_owner->p_spu_vout != p_vout )
2402 vlc_mutex_lock( &p_owner->lock );
2404 DecoderFlushBuffering( p_dec );
2406 vlc_mutex_unlock( &p_owner->lock );
2408 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2409 &p_owner->i_spu_channel );
2410 p_owner->i_spu_order = 0;
2411 p_owner->p_spu_vout = p_vout;
2414 p_subpic = subpicture_New();
2417 p_subpic->i_channel = p_owner->i_spu_channel;
2418 p_subpic->i_order = p_owner->i_spu_order++;
2419 p_subpic->b_subtitle = true;
2422 vlc_object_release( p_vout );
2427 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2429 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2430 vout_thread_t *p_vout = NULL;
2432 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2433 if( !p_vout || p_owner->p_spu_vout != p_vout )
2436 vlc_object_release( p_vout );
2437 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2441 subpicture_Delete( p_subpic );
2443 vlc_object_release( p_vout );