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 );
882 /* Ensure fast cancellation in case the fifo is not empty */
888 static void DecoderFlush( decoder_t *p_dec )
890 decoder_owner_sys_t *p_owner = p_dec->p_owner;
893 vlc_assert_locked( &p_owner->lock );
896 block_FifoEmpty( p_owner->p_fifo );
898 /* Monitor for flush end */
899 p_owner->b_flushing = true;
900 vlc_cond_signal( &p_owner->wait );
902 /* Send a special block */
903 p_null = block_New( p_dec, 128 );
906 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
907 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
908 if( !p_dec->fmt_in.b_packetized )
909 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
910 memset( p_null->p_buffer, 0, p_null->i_buffer );
912 input_DecoderDecode( p_dec, p_null );
915 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
916 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
919 static void DecoderSignalFlushed( decoder_t *p_dec )
921 decoder_owner_sys_t *p_owner = p_dec->p_owner;
923 vlc_mutex_lock( &p_owner->lock );
925 if( p_owner->b_flushing )
927 p_owner->b_flushing = false;
928 vlc_cond_signal( &p_owner->wait );
931 vlc_mutex_unlock( &p_owner->lock );
934 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
936 decoder_owner_sys_t *p_owner = p_dec->p_owner;
938 vlc_mutex_lock( &p_owner->lock );
940 if( p_owner->b_buffering )
943 p_owner->buffer.b_full = true;
944 vlc_cond_signal( &p_owner->wait );
947 vlc_mutex_unlock( &p_owner->lock );
950 static bool DecoderIsFlushing( decoder_t *p_dec )
952 decoder_owner_sys_t *p_owner = p_dec->p_owner;
955 vlc_mutex_lock( &p_owner->lock );
957 b_flushing = p_owner->b_flushing;
959 vlc_mutex_unlock( &p_owner->lock );
964 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
966 decoder_owner_sys_t *p_owner = p_dec->p_owner;
968 vlc_assert_locked( &p_owner->lock );
970 while( !p_owner->b_flushing )
972 if( p_owner->b_paused )
974 if( p_owner->b_buffering && !p_owner->buffer.b_full )
976 if( p_owner->pause.i_ignore > 0 )
978 p_owner->pause.i_ignore--;
984 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
987 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
991 *pb_reject = p_owner->b_flushing;
994 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
996 decoder_owner_sys_t *p_owner = p_dec->p_owner;
998 vlc_assert_locked( &p_owner->lock );
1000 /* XXX only audio and video output have to be paused.
1001 * - for sout it is useless
1002 * - for subs, it is done by the vout
1004 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1006 if( p_owner->p_aout && p_owner->p_aout_input )
1007 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1010 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1012 if( p_owner->p_vout )
1013 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1016 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1018 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1019 *pi_preroll = INT64_MAX;
1020 else if( p->i_pts > 0 )
1021 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1022 else if( p->i_dts > 0 )
1023 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1026 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
1028 mtime_t current_date = mdate();
1030 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1031 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1033 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1034 * for teletext streams. */
1035 return current_date + 400000 + i_ts_delay;
1040 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1041 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1043 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1044 input_clock_t *p_clock = p_owner->p_clock;
1047 vlc_assert_locked( &p_owner->lock );
1049 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
1050 const mtime_t i_es_delay = p_owner->i_ts_delay;
1054 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1057 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1058 *pi_ts0 + i_es_delay );
1059 if( pi_ts1 && *pi_ts1 > 0 )
1060 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1061 *pi_ts1 + i_es_delay );
1063 /* Do not create ephemere data because of rounding errors */
1064 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1068 i_rate = input_clock_GetRate( p_clock );
1071 *pi_duration = ( *pi_duration * i_rate +
1072 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1079 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1080 if( pi_ts1 && *pi_ts1 <= 0 )
1086 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1087 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1091 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1092 int *pi_played_sum, int *pi_lost_sum )
1094 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1095 aout_instance_t *p_aout = p_owner->p_aout;
1096 aout_input_t *p_aout_input = p_owner->p_aout_input;
1099 if( p_audio->start_date <= 0 )
1101 msg_Warn( p_dec, "non-dated audio buffer received" );
1103 aout_BufferFree( p_audio );
1108 vlc_mutex_lock( &p_owner->lock );
1110 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1112 p_audio->p_next = NULL;
1114 *p_owner->buffer.pp_audio_next = p_audio;
1115 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1117 p_owner->buffer.i_count++;
1118 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1119 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1121 p_owner->buffer.b_full = true;
1122 vlc_cond_signal( &p_owner->wait );
1128 bool b_has_more = false;
1130 DecoderWaitUnblock( p_dec, &b_reject );
1132 if( p_owner->b_buffering )
1134 vlc_mutex_unlock( &p_owner->lock );
1139 if( p_owner->buffer.p_audio )
1141 p_audio = p_owner->buffer.p_audio;
1143 p_owner->buffer.p_audio = p_audio->p_next;
1144 p_owner->buffer.i_count--;
1146 b_has_more = p_owner->buffer.p_audio != NULL;
1148 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1152 int i_rate = INPUT_RATE_DEFAULT;
1155 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1156 &i_rate, &i_delay, false );
1158 vlc_mutex_unlock( &p_owner->lock );
1161 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1163 if( !p_aout || !p_aout_input ||
1164 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1165 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1166 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1169 /* Do not wait against unprotected date */
1170 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1171 while( !b_reject && i_deadline > mdate() )
1173 vlc_mutex_lock( &p_owner->lock );
1174 if( p_owner->b_flushing || p_dec->b_die )
1177 vlc_mutex_unlock( &p_owner->lock );
1180 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1181 vlc_mutex_unlock( &p_owner->lock );
1186 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1187 *pi_played_sum += 1;
1188 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1192 if( p_audio->start_date <= 0 )
1194 msg_Warn( p_dec, "non-dated audio buffer received" );
1196 else if( p_audio->start_date > i_max_date )
1198 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1199 p_audio->start_date - mdate() );
1202 aout_BufferFree( p_audio );
1208 vlc_mutex_lock( &p_owner->lock );
1209 if( !p_owner->buffer.p_audio )
1211 vlc_mutex_unlock( &p_owner->lock );
1217 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1219 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1220 input_thread_t *p_input = p_owner->p_input;
1221 aout_buffer_t *p_aout_buf;
1226 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1228 aout_instance_t *p_aout = p_owner->p_aout;
1229 aout_input_t *p_aout_input = p_owner->p_aout_input;
1233 /* It prevent freezing VLC in case of broken decoder */
1234 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1236 block_Release( p_block );
1241 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1243 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1247 if( p_owner->i_preroll_end > 0 )
1249 msg_Dbg( p_dec, "End of audio preroll" );
1250 if( p_owner->p_aout && p_owner->p_aout_input )
1251 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1253 p_owner->i_preroll_end = -1;
1256 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1259 /* Update ugly stat */
1260 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1262 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1264 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1266 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1268 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1271 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1274 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1276 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1282 assert( p_dec_cc->pf_get_cc != NULL );
1284 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1285 if( !p_owner->cc.b_supported )
1288 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1292 vlc_mutex_lock( &p_owner->lock );
1293 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1295 p_owner->cc.pb_present[i] |= pb_present[i];
1296 if( p_owner->cc.pp_decoder[i] )
1300 for( i = 0; i < 4; i++ )
1302 if( !p_owner->cc.pp_decoder[i] )
1305 if( i_cc_decoder > 1 )
1306 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1308 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1311 vlc_mutex_unlock( &p_owner->lock );
1314 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1315 int *pi_played_sum, int *pi_lost_sum )
1317 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1318 vout_thread_t *p_vout = p_owner->p_vout;
1319 bool b_first_buffered;
1321 if( p_picture->date <= 0 )
1323 msg_Warn( p_vout, "non-dated video buffer received" );
1325 vout_DropPicture( p_vout, p_picture );
1330 vlc_mutex_lock( &p_owner->lock );
1332 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1334 p_picture->p_next = NULL;
1336 *p_owner->buffer.pp_picture_next = p_picture;
1337 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1339 p_owner->buffer.i_count++;
1340 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1341 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1343 p_owner->buffer.b_full = true;
1344 vlc_cond_signal( &p_owner->wait );
1347 b_first_buffered = p_owner->buffer.p_picture != NULL;
1349 for( ;; b_first_buffered = false )
1351 bool b_has_more = false;
1355 DecoderWaitUnblock( p_dec, &b_reject );
1357 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1359 vlc_mutex_unlock( &p_owner->lock );
1362 bool b_buffering_first = p_owner->b_buffering;
1365 if( p_owner->buffer.p_picture )
1367 p_picture = p_owner->buffer.p_picture;
1369 p_owner->buffer.p_picture = p_picture->p_next;
1370 p_owner->buffer.i_count--;
1372 b_has_more = p_owner->buffer.p_picture != NULL;
1374 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1378 if( b_buffering_first )
1380 assert( p_owner->buffer.b_first );
1381 assert( !p_owner->buffer.i_count );
1382 msg_Dbg( p_dec, "Received first picture" );
1383 p_owner->buffer.b_first = false;
1384 p_picture->b_force = true;
1387 int i_rate = INPUT_RATE_DEFAULT;
1389 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1390 &i_rate, &i_delay, false );
1392 vlc_mutex_unlock( &p_owner->lock );
1395 const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1397 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1402 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1404 /* Be sure to not display old picture after our own */
1405 vout_Flush( p_vout, p_picture->date );
1406 p_owner->i_last_rate = i_rate;
1408 vout_DisplayPicture( p_vout, p_picture );
1412 if( p_picture->date <= 0 )
1414 msg_Warn( p_vout, "non-dated video buffer received" );
1418 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1419 p_picture->date - mdate() );
1422 vout_DropPicture( p_vout, p_picture );
1426 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1428 *pi_played_sum += i_tmp_display;
1429 *pi_lost_sum += i_tmp_lost;
1431 if( !b_has_more || b_buffering_first )
1434 vlc_mutex_lock( &p_owner->lock );
1435 if( !p_owner->buffer.p_picture )
1437 vlc_mutex_unlock( &p_owner->lock );
1443 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1445 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1446 input_thread_t *p_input = p_owner->p_input;
1450 int i_displayed = 0;
1452 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1454 vout_thread_t *p_vout = p_owner->p_vout;
1457 /* It prevent freezing VLC in case of broken decoder */
1458 vout_DropPicture( p_vout, p_pic );
1460 block_Release( p_block );
1466 if( p_pic->date < p_owner->i_preroll_end )
1468 vout_DropPicture( p_vout, p_pic );
1472 if( p_owner->i_preroll_end > 0 )
1474 msg_Dbg( p_dec, "End of video preroll" );
1476 vout_Flush( p_vout, 1 );
1478 p_owner->i_preroll_end = -1;
1481 if( p_dec->pf_get_cc &&
1482 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1483 DecoderGetCc( p_dec, p_dec );
1485 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1487 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1489 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1491 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1493 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1496 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1499 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1503 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1506 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1507 vout_thread_t *p_vout = p_owner->p_spu_vout;
1510 if( p_subpic->i_start <= 0 )
1512 msg_Warn( p_dec, "non-dated spu buffer received" );
1513 subpicture_Delete( p_subpic );
1518 vlc_mutex_lock( &p_owner->lock );
1520 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1522 p_subpic->p_next = NULL;
1524 *p_owner->buffer.pp_subpic_next = p_subpic;
1525 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1527 p_owner->buffer.i_count++;
1528 /* XXX it is important to be full after the first one */
1529 if( p_owner->buffer.i_count > 0 )
1531 p_owner->buffer.b_full = true;
1532 vlc_cond_signal( &p_owner->wait );
1538 bool b_has_more = false;
1540 DecoderWaitUnblock( p_dec, &b_reject );
1542 if( p_owner->b_buffering )
1544 vlc_mutex_unlock( &p_owner->lock );
1549 if( p_owner->buffer.p_subpic )
1551 p_subpic = p_owner->buffer.p_subpic;
1553 p_owner->buffer.p_subpic = p_subpic->p_next;
1554 p_owner->buffer.i_count--;
1556 b_has_more = p_owner->buffer.p_subpic != NULL;
1558 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1562 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1563 NULL, NULL, b_telx );
1565 vlc_mutex_unlock( &p_owner->lock );
1568 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1570 subpicture_Delete( p_subpic );
1574 vlc_mutex_lock( &p_owner->lock );
1575 if( !p_owner->buffer.p_subpic )
1577 vlc_mutex_unlock( &p_owner->lock );
1583 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1586 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1588 assert( p_owner->p_clock );
1589 assert( !p_sout_block->p_next );
1591 vlc_mutex_lock( &p_owner->lock );
1593 if( p_owner->b_buffering || p_owner->buffer.p_block )
1595 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1597 p_owner->buffer.i_count++;
1598 /* XXX it is important to be full after the first one */
1599 if( p_owner->buffer.i_count > 0 )
1601 p_owner->buffer.b_full = true;
1602 vlc_cond_signal( &p_owner->wait );
1608 bool b_has_more = false;
1610 DecoderWaitUnblock( p_dec, &b_reject );
1612 if( p_owner->b_buffering )
1614 vlc_mutex_unlock( &p_owner->lock );
1619 if( p_owner->buffer.p_block )
1621 p_sout_block = p_owner->buffer.p_block;
1623 p_owner->buffer.p_block = p_sout_block->p_next;
1624 p_owner->buffer.i_count--;
1626 b_has_more = p_owner->buffer.p_block != NULL;
1628 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1630 p_sout_block->p_next = NULL;
1632 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1633 &p_sout_block->i_length,
1634 &p_sout_block->i_rate, NULL, b_telx );
1636 vlc_mutex_unlock( &p_owner->lock );
1639 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1641 block_Release( p_sout_block );
1645 vlc_mutex_lock( &p_owner->lock );
1646 if( !p_owner->buffer.p_block )
1648 vlc_mutex_unlock( &p_owner->lock );
1655 static void DecoderFlushBuffering( decoder_t *p_dec )
1657 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1659 vlc_assert_locked( &p_owner->lock );
1661 while( p_owner->buffer.p_picture )
1663 picture_t *p_picture = p_owner->buffer.p_picture;
1665 p_owner->buffer.p_picture = p_picture->p_next;
1666 p_owner->buffer.i_count--;
1668 if( p_owner->p_vout )
1669 vout_DropPicture( p_owner->p_vout, p_picture );
1671 if( !p_owner->buffer.p_picture )
1672 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1674 while( p_owner->buffer.p_audio )
1676 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1678 p_owner->buffer.p_audio = p_audio->p_next;
1679 p_owner->buffer.i_count--;
1681 aout_BufferFree( p_audio );
1683 if( !p_owner->buffer.p_audio )
1684 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1686 while( p_owner->buffer.p_subpic )
1688 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1690 p_owner->buffer.p_subpic = p_subpic->p_next;
1691 p_owner->buffer.i_count--;
1693 subpicture_Delete( p_subpic );
1695 if( !p_owner->buffer.p_subpic )
1696 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1698 if( p_owner->buffer.p_block )
1700 block_ChainRelease( p_owner->buffer.p_block );
1702 p_owner->buffer.i_count = 0;
1703 p_owner->buffer.p_block = NULL;
1704 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1708 /* This function process a block for sout
1710 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1712 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1713 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1714 block_t *p_sout_block;
1716 while( ( p_sout_block =
1717 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1719 if( !p_owner->p_sout_input )
1721 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1723 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1724 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1725 if( p_dec->fmt_in.psz_language )
1727 if( p_owner->sout.psz_language )
1728 free( p_owner->sout.psz_language );
1729 p_owner->sout.psz_language =
1730 strdup( p_dec->fmt_in.psz_language );
1733 p_owner->p_sout_input =
1734 sout_InputNew( p_owner->p_sout,
1737 if( p_owner->p_sout_input == NULL )
1739 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1740 (char *)&p_owner->sout.i_codec );
1741 p_dec->b_error = true;
1743 while( p_sout_block )
1745 block_t *p_next = p_sout_block->p_next;
1746 block_Release( p_sout_block );
1747 p_sout_block = p_next;
1753 while( p_sout_block )
1755 block_t *p_next = p_sout_block->p_next;
1757 p_sout_block->p_next = NULL;
1759 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1761 p_sout_block = p_next;
1764 /* For now it's enough, as only sout impact on this flag */
1765 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1766 p_owner->p_input->p->b_out_pace_control )
1768 msg_Dbg( p_dec, "switching to sync mode" );
1769 p_owner->p_input->p->b_out_pace_control = false;
1771 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1772 !p_owner->p_input->p->b_out_pace_control )
1774 msg_Dbg( p_dec, "switching to async mode" );
1775 p_owner->p_input->p->b_out_pace_control = true;
1780 /* This function process a video block
1782 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1784 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1786 if( p_owner->p_packetizer )
1788 block_t *p_packetized_block;
1789 decoder_t *p_packetizer = p_owner->p_packetizer;
1791 while( (p_packetized_block =
1792 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1794 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1796 es_format_Clean( &p_dec->fmt_in );
1797 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1799 if( p_packetizer->pf_get_cc )
1800 DecoderGetCc( p_dec, p_packetizer );
1802 while( p_packetized_block )
1804 block_t *p_next = p_packetized_block->p_next;
1805 p_packetized_block->p_next = NULL;
1807 DecoderDecodeVideo( p_dec, p_packetized_block );
1809 p_packetized_block = p_next;
1815 DecoderDecodeVideo( p_dec, p_block );
1818 if( b_flush && p_owner->p_vout )
1819 vout_Flush( p_owner->p_vout, 1 );
1822 /* This function process a audio block
1824 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1826 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1828 if( p_owner->p_packetizer )
1830 block_t *p_packetized_block;
1831 decoder_t *p_packetizer = p_owner->p_packetizer;
1833 while( (p_packetized_block =
1834 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1836 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1838 es_format_Clean( &p_dec->fmt_in );
1839 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1842 while( p_packetized_block )
1844 block_t *p_next = p_packetized_block->p_next;
1845 p_packetized_block->p_next = NULL;
1847 DecoderDecodeAudio( p_dec, p_packetized_block );
1849 p_packetized_block = p_next;
1855 DecoderDecodeAudio( p_dec, p_block );
1858 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1859 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1862 /* This function process a subtitle block
1864 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1866 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1867 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1869 input_thread_t *p_input = p_owner->p_input;
1870 vout_thread_t *p_vout;
1871 subpicture_t *p_spu;
1873 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1875 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1876 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1877 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1879 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1880 if( p_vout && p_owner->p_spu_vout == p_vout )
1882 /* Preroll does not work very well with subtitle */
1883 if( p_spu->i_start > 0 &&
1884 p_spu->i_start < p_owner->i_preroll_end &&
1885 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1887 subpicture_Delete( p_spu );
1891 DecoderPlaySpu( p_dec, p_spu, b_telx );
1896 subpicture_Delete( p_spu );
1899 vlc_object_release( p_vout );
1902 if( b_flush && p_owner->p_spu_vout )
1904 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1906 if( p_vout && p_owner->p_spu_vout == p_vout )
1907 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1908 p_owner->i_spu_channel );
1911 vlc_object_release( p_vout );
1916 static void DecoderProcessOnFlush( decoder_t *p_dec )
1918 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1920 vlc_mutex_lock( &p_owner->lock );
1921 DecoderFlushBuffering( p_dec );
1922 vlc_mutex_unlock( &p_owner->lock );
1924 DecoderSignalFlushed( p_dec );
1929 * \param p_dec the decoder object
1930 * \param p_block the block to decode
1931 * \return VLC_SUCCESS or an error code
1933 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1935 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1936 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1938 if( p_block && p_block->i_buffer <= 0 )
1940 assert( !b_flush_request );
1941 block_Release( p_block );
1946 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1949 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1951 DecoderProcessSout( p_dec, p_block );
1956 bool b_flush = false;
1960 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1961 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1963 b_flush = !b_flushing && b_flush_request;
1965 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1968 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1970 DecoderProcessAudio( p_dec, p_block, b_flush );
1972 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1974 DecoderProcessVideo( p_dec, p_block, b_flush );
1976 else if( p_dec->fmt_in.i_cat == SPU_ES )
1978 DecoderProcessSpu( p_dec, p_block, b_flush );
1982 msg_Err( p_dec, "unknown ES format" );
1983 p_dec->b_error = true;
1988 if( b_flush_request )
1989 DecoderProcessOnFlush( p_dec );
1992 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1994 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1998 block_Release( p_block );
2000 if( b_flush_request )
2001 DecoderProcessOnFlush( p_dec );
2006 * Destroys a decoder object
2008 * \param p_dec the decoder object
2011 static void DeleteDecoder( decoder_t * p_dec )
2013 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2015 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2016 (char*)&p_dec->fmt_in.i_codec,
2017 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2019 /* Free all packets still in the decoder fifo. */
2020 block_FifoEmpty( p_owner->p_fifo );
2021 block_FifoRelease( p_owner->p_fifo );
2024 vlc_mutex_lock( &p_owner->lock );
2025 DecoderFlushBuffering( p_dec );
2026 vlc_mutex_unlock( &p_owner->lock );
2029 if( p_owner->p_aout_input )
2030 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2031 if( p_owner->p_aout )
2033 input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2035 p_owner->p_aout = NULL;
2037 if( p_owner->p_vout )
2039 /* Hack to make sure all the the pictures are freed by the decoder */
2040 vout_FixLeaks( p_owner->p_vout, true );
2042 /* We are about to die. Reattach video output to p_vlc. */
2043 input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
2044 input_SendEventVout( p_owner->p_input );
2048 if( p_owner->p_sout_input )
2050 sout_InputDelete( p_owner->p_sout_input );
2051 es_format_Clean( &p_owner->sout );
2055 if( p_dec->fmt_in.i_cat == SPU_ES )
2057 vout_thread_t *p_vout;
2059 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2062 if( p_owner->p_spu_vout == p_vout )
2063 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2064 vlc_object_release( p_vout );
2068 es_format_Clean( &p_dec->fmt_in );
2069 es_format_Clean( &p_dec->fmt_out );
2070 if( p_dec->p_description )
2071 vlc_meta_Delete( p_dec->p_description );
2072 es_format_Clean( &p_owner->fmt_description );
2073 if( p_owner->p_description )
2074 vlc_meta_Delete( p_owner->p_description );
2076 if( p_owner->p_packetizer )
2078 module_unneed( p_owner->p_packetizer,
2079 p_owner->p_packetizer->p_module );
2080 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2081 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2082 if( p_owner->p_packetizer->p_description )
2083 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2084 vlc_object_detach( p_owner->p_packetizer );
2085 vlc_object_release( p_owner->p_packetizer );
2088 vlc_cond_destroy( &p_owner->wait );
2089 vlc_mutex_destroy( &p_owner->lock );
2091 vlc_object_detach( p_dec );
2096 /*****************************************************************************
2097 * Buffers allocation callbacks for the decoders
2098 *****************************************************************************/
2099 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2101 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2103 vlc_assert_locked( &p_owner->lock );
2105 p_owner->b_fmt_description = true;
2107 /* Copy es_format */
2108 es_format_Clean( &p_owner->fmt_description );
2109 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2111 /* Move p_description */
2112 if( p_owner->p_description && p_dec->p_description )
2113 vlc_meta_Delete( p_owner->p_description );
2114 p_owner->p_description = p_dec->p_description;
2115 p_dec->p_description = NULL;
2117 static vout_thread_t *aout_request_vout( void *p_private,
2118 vout_thread_t *p_vout, video_format_t *p_fmt )
2120 decoder_t *p_dec = p_private;
2122 p_vout = input_ressource_RequestVout( p_dec->p_owner->p_input->p->p_ressource, p_vout, p_fmt );
2123 input_SendEventVout( p_dec->p_owner->p_input );
2128 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2130 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2131 aout_buffer_t *p_buffer;
2133 if( p_owner->p_aout_input != NULL &&
2134 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2135 p_dec->fmt_out.audio.i_original_channels !=
2136 p_owner->audio.i_original_channels ||
2137 p_dec->fmt_out.audio.i_bytes_per_frame !=
2138 p_owner->audio.i_bytes_per_frame ) )
2140 aout_input_t *p_aout_input = p_owner->p_aout_input;
2142 /* Parameters changed, restart the aout */
2143 vlc_mutex_lock( &p_owner->lock );
2145 DecoderFlushBuffering( p_dec );
2147 p_owner->p_aout_input = NULL;
2148 aout_DecDelete( p_owner->p_aout, p_aout_input );
2150 vlc_mutex_unlock( &p_owner->lock );
2153 if( p_owner->p_aout_input == NULL )
2155 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2156 audio_sample_format_t format;
2157 aout_input_t *p_aout_input;
2158 aout_instance_t *p_aout;
2159 aout_request_vout_t request_vout;
2161 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2162 p_owner->audio = p_dec->fmt_out.audio;
2164 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2165 if( i_force_dolby &&
2166 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2167 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2169 if( i_force_dolby == 1 )
2171 format.i_original_channels = format.i_original_channels |
2172 AOUT_CHAN_DOLBYSTEREO;
2174 else /* i_force_dolby == 2 */
2176 format.i_original_channels = format.i_original_channels &
2177 ~AOUT_CHAN_DOLBYSTEREO;
2181 request_vout.pf_request_vout = aout_request_vout;
2182 request_vout.p_private = p_dec;
2184 p_aout = p_owner->p_aout;
2186 p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2187 p_aout_input = aout_DecNew( p_dec, &p_aout,
2188 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2190 vlc_mutex_lock( &p_owner->lock );
2192 p_owner->p_aout = p_aout;
2193 p_owner->p_aout_input = p_aout_input;
2194 DecoderUpdateFormatLocked( p_dec );
2196 vlc_mutex_unlock( &p_owner->lock );
2198 if( p_owner->p_aout_input == NULL )
2200 msg_Err( p_dec, "failed to create audio output" );
2201 p_dec->b_error = true;
2204 p_dec->fmt_out.audio.i_bytes_per_frame =
2205 p_owner->audio.i_bytes_per_frame;
2208 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2213 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2215 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2217 aout_DecDeleteBuffer( p_owner->p_aout,
2218 p_owner->p_aout_input, p_buffer );
2222 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2224 static picture_t *vout_new_buffer( decoder_t *p_dec )
2226 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2228 if( p_owner->p_vout == NULL ||
2229 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2230 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2231 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2232 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2234 vout_thread_t *p_vout;
2236 if( !p_dec->fmt_out.video.i_width ||
2237 !p_dec->fmt_out.video.i_height )
2239 /* Can't create a new vout without display size */
2243 if( !p_dec->fmt_out.video.i_visible_width ||
2244 !p_dec->fmt_out.video.i_visible_height )
2246 if( p_dec->fmt_in.video.i_visible_width &&
2247 p_dec->fmt_in.video.i_visible_height )
2249 p_dec->fmt_out.video.i_visible_width =
2250 p_dec->fmt_in.video.i_visible_width;
2251 p_dec->fmt_out.video.i_visible_height =
2252 p_dec->fmt_in.video.i_visible_height;
2256 p_dec->fmt_out.video.i_visible_width =
2257 p_dec->fmt_out.video.i_width;
2258 p_dec->fmt_out.video.i_visible_height =
2259 p_dec->fmt_out.video.i_height;
2263 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2264 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2266 p_dec->fmt_out.video.i_visible_height = 1080;
2267 p_dec->fmt_out.video.i_sar_num *= 135;
2268 p_dec->fmt_out.video.i_sar_den *= 136;
2269 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2272 if( !p_dec->fmt_out.video.i_sar_num ||
2273 !p_dec->fmt_out.video.i_sar_den )
2275 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2276 p_dec->fmt_out.video.i_visible_height;
2278 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2279 p_dec->fmt_out.video.i_visible_width;
2282 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2283 &p_dec->fmt_out.video.i_sar_den,
2284 p_dec->fmt_out.video.i_sar_num,
2285 p_dec->fmt_out.video.i_sar_den, 50000 );
2287 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2288 p_owner->video = p_dec->fmt_out.video;
2290 vlc_mutex_lock( &p_owner->lock );
2292 DecoderFlushBuffering( p_dec );
2294 p_vout = p_owner->p_vout;
2295 p_owner->p_vout = NULL;
2296 vlc_mutex_unlock( &p_owner->lock );
2298 p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
2299 p_vout, &p_dec->fmt_out.video );
2301 vlc_mutex_lock( &p_owner->lock );
2302 p_owner->p_vout = p_vout;
2304 DecoderUpdateFormatLocked( p_dec );
2306 vlc_mutex_unlock( &p_owner->lock );
2308 input_SendEventVout( p_owner->p_input );
2309 if( p_vout == NULL )
2311 msg_Err( p_dec, "failed to create video output" );
2312 p_dec->b_error = true;
2316 if( p_owner->video.i_rmask )
2317 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2318 if( p_owner->video.i_gmask )
2319 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2320 if( p_owner->video.i_bmask )
2321 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2324 /* Get a new picture
2328 picture_t *p_picture;
2330 if( p_dec->b_die || p_dec->b_error )
2333 /* The video filter chain required that there is always 1 free buffer
2334 * that it will use as temporary one. It will release the temporary
2335 * buffer once its work is done, so this check is safe even if we don't
2336 * lock around both count() and create().
2338 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2340 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2345 if( DecoderIsFlushing( p_dec ) )
2349 DecoderSignalBuffering( p_dec, true );
2351 /* Check the decoder doesn't leak pictures */
2352 vout_FixLeaks( p_owner->p_vout, false );
2354 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2355 msleep( VOUT_OUTMEM_SLEEP );
2359 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2361 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2364 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2366 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2369 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2371 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2374 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2376 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2377 vout_thread_t *p_vout = NULL;
2378 subpicture_t *p_subpic;
2379 int i_attempts = 30;
2381 while( i_attempts-- )
2383 if( p_dec->b_die || p_dec->b_error )
2386 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2390 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2395 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2399 if( p_owner->p_spu_vout != p_vout )
2401 vlc_mutex_lock( &p_owner->lock );
2403 DecoderFlushBuffering( p_dec );
2405 vlc_mutex_unlock( &p_owner->lock );
2407 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2408 &p_owner->i_spu_channel );
2409 p_owner->i_spu_order = 0;
2410 p_owner->p_spu_vout = p_vout;
2413 p_subpic = subpicture_New();
2416 p_subpic->i_channel = p_owner->i_spu_channel;
2417 p_subpic->i_order = p_owner->i_spu_order++;
2418 p_subpic->b_subtitle = true;
2421 vlc_object_release( p_vout );
2426 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2428 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2429 vout_thread_t *p_vout = NULL;
2431 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2432 if( !p_vout || p_owner->p_spu_vout != p_vout )
2435 vlc_object_release( p_vout );
2436 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2440 subpicture_Delete( p_subpic );
2442 vlc_object_release( p_vout );