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, i_priority ) )
317 msg_Err( p_dec, "cannot spawn decoder thread" );
318 module_unneed( p_dec, p_dec->p_module );
319 DeleteDecoder( p_dec );
320 vlc_object_release( p_dec );
328 * Kills a decoder thread and waits until it's finished
330 * \param p_input the input thread
331 * \param p_es the es descriptor
334 void input_DecoderDelete( decoder_t *p_dec )
336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
338 vlc_object_kill( p_dec );
340 /* Make sure we aren't paused/buffering/waiting anymore */
341 vlc_mutex_lock( &p_owner->lock );
342 p_owner->b_paused = false;
343 p_owner->b_buffering = false;
344 p_owner->b_flushing = true;
345 vlc_cond_signal( &p_owner->wait );
346 vlc_mutex_unlock( &p_owner->lock );
348 vlc_thread_join( p_dec );
349 module_unneed( p_dec, p_dec->p_module );
352 if( p_dec->p_owner->cc.b_supported )
355 for( i = 0; i < 4; i++ )
356 input_DecoderSetCcState( p_dec, false, i );
359 /* Delete decoder configuration */
360 DeleteDecoder( p_dec );
362 /* Delete the decoder */
363 vlc_object_release( p_dec );
367 * Put a block_t in the decoder's fifo.
368 * Thread-safe w.r.t. the decoder. May be a cancellation point.
370 * \param p_dec the decoder object
371 * \param p_block the data block
373 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
375 decoder_owner_sys_t *p_owner = p_dec->p_owner;
377 if( p_owner->p_input->p->b_out_pace_control )
379 /* The fifo is not consummed when buffering and so will
381 * There is no need to lock as b_buffering is never modify
382 * inside decoder thread. */
383 if( !p_owner->b_buffering )
384 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
386 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
388 /* FIXME: ideally we would check the time amount of data
389 * in the FIFO instead of its size. */
390 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
391 "consumed quickly enough), resetting fifo!" );
392 block_FifoEmpty( p_owner->p_fifo );
395 block_FifoPut( p_owner->p_fifo, p_block );
398 bool input_DecoderIsEmpty( decoder_t * p_dec )
400 assert( !p_dec->p_owner->b_buffering );
402 /* FIXME that's not really true */
403 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
406 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
408 decoder_owner_sys_t *p_owner = p_dec->p_owner;
411 vlc_mutex_lock( &p_owner->lock );
412 for( i = 0; i < 4; i++ )
413 pb_present[i] = p_owner->cc.pb_present[i];
414 vlc_mutex_unlock( &p_owner->lock );
416 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
418 decoder_owner_sys_t *p_owner = p_dec->p_owner;
420 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
422 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
427 static const vlc_fourcc_t fcc[4] = {
428 VLC_FOURCC('c', 'c', '1', ' '),
429 VLC_FOURCC('c', 'c', '2', ' '),
430 VLC_FOURCC('c', 'c', '3', ' '),
431 VLC_FOURCC('c', 'c', '4', ' '),
436 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
437 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
440 msg_Err( p_dec, "could not create decoder" );
441 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
442 _("VLC could not open the decoder module.") );
445 else if( !p_cc->p_module )
447 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
448 DeleteDecoder( p_cc );
449 vlc_object_release( p_cc );
452 p_cc->p_owner->p_clock = p_owner->p_clock;
454 vlc_mutex_lock( &p_owner->lock );
455 p_owner->cc.pp_decoder[i_channel] = p_cc;
456 vlc_mutex_unlock( &p_owner->lock );
462 vlc_mutex_lock( &p_owner->lock );
463 p_cc = p_owner->cc.pp_decoder[i_channel];
464 p_owner->cc.pp_decoder[i_channel] = NULL;
465 vlc_mutex_unlock( &p_owner->lock );
469 vlc_object_kill( p_cc );
470 module_unneed( p_cc, p_cc->p_module );
471 DeleteDecoder( p_cc );
472 vlc_object_release( p_cc );
477 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
479 decoder_owner_sys_t *p_owner = p_dec->p_owner;
482 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
485 vlc_mutex_lock( &p_owner->lock );
486 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
487 vlc_mutex_unlock( &p_owner->lock );
491 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
493 decoder_owner_sys_t *p_owner = p_dec->p_owner;
495 vlc_mutex_lock( &p_owner->lock );
497 assert( !p_owner->b_paused || !b_paused );
499 p_owner->b_paused = b_paused;
500 p_owner->pause.i_date = i_date;
501 p_owner->pause.i_ignore = 0;
502 vlc_cond_signal( &p_owner->wait );
504 DecoderOutputChangePause( p_dec, b_paused, i_date );
506 vlc_mutex_unlock( &p_owner->lock );
509 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
511 decoder_owner_sys_t *p_owner = p_dec->p_owner;
513 vlc_mutex_lock( &p_owner->lock );
514 p_owner->i_ts_delay = i_delay;
515 vlc_mutex_unlock( &p_owner->lock );
518 void input_DecoderStartBuffering( decoder_t *p_dec )
520 decoder_owner_sys_t *p_owner = p_dec->p_owner;
522 vlc_mutex_lock( &p_owner->lock );
524 DecoderFlush( p_dec );
526 p_owner->buffer.b_first = true;
527 p_owner->buffer.b_full = false;
528 p_owner->buffer.i_count = 0;
530 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
531 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
533 p_owner->buffer.p_picture = NULL;
534 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
536 p_owner->buffer.p_subpic = NULL;
537 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
539 p_owner->buffer.p_audio = NULL;
540 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
542 p_owner->buffer.p_block = NULL;
543 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
546 p_owner->b_buffering = true;
548 vlc_cond_signal( &p_owner->wait );
550 vlc_mutex_unlock( &p_owner->lock );
553 void input_DecoderStopBuffering( decoder_t *p_dec )
555 decoder_owner_sys_t *p_owner = p_dec->p_owner;
557 vlc_mutex_lock( &p_owner->lock );
559 p_owner->b_buffering = false;
561 vlc_cond_signal( &p_owner->wait );
563 vlc_mutex_unlock( &p_owner->lock );
566 void input_DecoderWaitBuffering( decoder_t *p_dec )
568 decoder_owner_sys_t *p_owner = p_dec->p_owner;
570 vlc_mutex_lock( &p_owner->lock );
572 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
574 block_FifoWake( p_owner->p_fifo );
575 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
578 vlc_mutex_unlock( &p_owner->lock );
581 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
583 decoder_owner_sys_t *p_owner = p_dec->p_owner;
587 vlc_mutex_lock( &p_owner->lock );
588 if( p_dec->fmt_in.i_cat == VIDEO_ES )
590 if( p_owner->b_paused && p_owner->p_vout )
592 vout_NextPicture( p_owner->p_vout, pi_duration );
593 p_owner->pause.i_ignore++;
594 vlc_cond_signal( &p_owner->wait );
599 /* TODO subtitle should not be flushed */
600 DecoderFlush( p_dec );
602 vlc_mutex_unlock( &p_owner->lock );
605 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
607 decoder_owner_sys_t *p_owner = p_dec->p_owner;
610 vlc_mutex_lock( &p_owner->lock );
611 b_changed = p_owner->b_fmt_description;
615 es_format_Copy( p_fmt, &p_owner->fmt_description );
620 if( p_owner->p_description )
622 *pp_meta = vlc_meta_New();
624 vlc_meta_Merge( *pp_meta, p_owner->p_description );
627 p_owner->b_fmt_description = false;
629 vlc_mutex_unlock( &p_owner->lock );
633 /*****************************************************************************
635 *****************************************************************************/
636 static int DecoderGetInputAttachments( decoder_t *p_dec,
637 input_attachment_t ***ppp_attachment,
640 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
641 ppp_attachment, pi_attachment );
643 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
645 decoder_owner_sys_t *p_owner = p_dec->p_owner;
647 vlc_mutex_lock( &p_owner->lock );
648 if( p_owner->b_buffering || p_owner->b_paused )
650 vlc_mutex_unlock( &p_owner->lock );
652 if( !p_owner->p_clock || !i_ts )
655 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->p->i_pts_delay, i_ts );
657 static int DecoderGetDisplayRate( decoder_t *p_dec )
659 decoder_owner_sys_t *p_owner = p_dec->p_owner;
661 if( !p_owner->p_clock )
662 return INPUT_RATE_DEFAULT;
663 return input_clock_GetRate( p_owner->p_clock );
667 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
669 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
670 "VLC probably does not support this sound or video format.",
672 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
673 _("VLC does not support the audio or video format \"%4.4s\". "
674 "Unfortunately there is no way for you to fix this."), (char*)&codec );
679 * Create a decoder object
681 * \param p_input the input thread
682 * \param p_es the es descriptor
683 * \param i_object_type Object type as define in include/vlc_objects.h
684 * \return the decoder object
686 static decoder_t * CreateDecoder( input_thread_t *p_input,
687 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
690 decoder_owner_sys_t *p_owner;
693 p_dec = vlc_object_create( p_input, i_object_type );
697 p_dec->pf_decode_audio = NULL;
698 p_dec->pf_decode_video = NULL;
699 p_dec->pf_decode_sub = NULL;
700 p_dec->pf_get_cc = NULL;
701 p_dec->pf_packetize = NULL;
703 /* Initialize the decoder */
704 p_dec->p_module = NULL;
706 memset( &null_es_format, 0, sizeof(es_format_t) );
707 es_format_Copy( &p_dec->fmt_in, fmt );
708 es_format_Copy( &p_dec->fmt_out, &null_es_format );
710 p_dec->p_description = NULL;
712 /* Allocate our private structure for the decoder */
713 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
714 if( p_dec->p_owner == NULL )
716 vlc_object_release( p_dec );
719 p_dec->p_owner->i_preroll_end = -1;
720 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
721 p_dec->p_owner->p_input = p_input;
722 p_dec->p_owner->p_aout = NULL;
723 p_dec->p_owner->p_aout_input = NULL;
724 p_dec->p_owner->p_vout = NULL;
725 p_dec->p_owner->p_spu_vout = NULL;
726 p_dec->p_owner->i_spu_channel = 0;
727 p_dec->p_owner->i_spu_order = 0;
728 p_dec->p_owner->p_sout = p_sout;
729 p_dec->p_owner->p_sout_input = NULL;
730 p_dec->p_owner->p_packetizer = NULL;
733 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
735 free( p_dec->p_owner );
736 vlc_object_release( p_dec );
740 /* Set buffers allocation callbacks for the decoders */
741 p_dec->pf_aout_buffer_new = aout_new_buffer;
742 p_dec->pf_aout_buffer_del = aout_del_buffer;
743 p_dec->pf_vout_buffer_new = vout_new_buffer;
744 p_dec->pf_vout_buffer_del = vout_del_buffer;
745 p_dec->pf_picture_link = vout_link_picture;
746 p_dec->pf_picture_unlink = vout_unlink_picture;
747 p_dec->pf_spu_buffer_new = spu_new_buffer;
748 p_dec->pf_spu_buffer_del = spu_del_buffer;
750 p_dec->pf_get_attachments = DecoderGetInputAttachments;
751 p_dec->pf_get_display_date = DecoderGetDisplayDate;
752 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
754 vlc_object_attach( p_dec, p_input );
756 /* Find a suitable decoder/packetizer module */
757 if( i_object_type == VLC_OBJECT_DECODER )
758 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
760 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
762 /* Check if decoder requires already packetized data */
763 if( i_object_type == VLC_OBJECT_DECODER &&
764 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
766 p_dec->p_owner->p_packetizer =
767 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
768 if( p_dec->p_owner->p_packetizer )
770 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
773 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
776 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
778 p_dec->p_owner->p_packetizer->p_module =
779 module_need( p_dec->p_owner->p_packetizer,
780 "packetizer", "$packetizer", false );
782 if( !p_dec->p_owner->p_packetizer->p_module )
784 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
785 vlc_object_detach( p_dec->p_owner->p_packetizer );
786 vlc_object_release( p_dec->p_owner->p_packetizer );
791 /* Copy ourself the input replay gain */
792 if( fmt->i_cat == AUDIO_ES )
794 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
796 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
798 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
799 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
801 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
803 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
804 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
810 vlc_mutex_init( &p_owner->lock );
811 vlc_cond_init( &p_owner->wait );
813 p_owner->b_fmt_description = false;
814 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
815 p_owner->p_description = NULL;
817 p_owner->b_paused = false;
818 p_owner->pause.i_date = 0;
819 p_owner->pause.i_ignore = 0;
821 p_owner->b_buffering = false;
822 p_owner->buffer.b_first = true;
823 p_owner->buffer.b_full = false;
824 p_owner->buffer.i_count = 0;
825 p_owner->buffer.p_picture = NULL;
826 p_owner->buffer.p_subpic = NULL;
827 p_owner->buffer.p_audio = NULL;
828 p_owner->buffer.p_block = NULL;
830 p_owner->b_flushing = false;
833 p_owner->cc.b_supported = false;
834 if( i_object_type == VLC_OBJECT_DECODER )
836 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
837 p_owner->cc.b_supported = true;
838 if( p_dec->pf_get_cc )
839 p_owner->cc.b_supported = true;
842 for( i = 0; i < 4; i++ )
844 p_owner->cc.pb_present[i] = false;
845 p_owner->cc.pp_decoder[i] = NULL;
847 p_owner->i_ts_delay = 0;
852 * The decoding main loop
854 * \param p_dec the decoder
856 static void *DecoderThread( vlc_object_t *p_this )
858 decoder_t *p_dec = (decoder_t *)p_this;
859 decoder_owner_sys_t *p_owner = p_dec->p_owner;
861 /* The decoder's main loop */
864 block_t *p_block = block_FifoGet( p_owner->p_fifo );
866 /* Make sure there is no cancellation point other than this one^^.
867 * If you need one, be sure to push cleanup of p_block. */
868 DecoderSignalBuffering( p_dec, p_block == NULL );
872 int canc = vlc_savecancel();
875 DecoderError( p_dec, p_block );
877 DecoderProcess( p_dec, p_block );
879 vlc_restorecancel( canc );
885 static void DecoderFlush( decoder_t *p_dec )
887 decoder_owner_sys_t *p_owner = p_dec->p_owner;
890 vlc_assert_locked( &p_owner->lock );
893 block_FifoEmpty( p_owner->p_fifo );
895 /* Monitor for flush end */
896 p_owner->b_flushing = true;
897 vlc_cond_signal( &p_owner->wait );
899 /* Send a special block */
900 p_null = block_New( p_dec, 128 );
903 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
904 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
905 if( !p_dec->fmt_in.b_packetized )
906 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
907 memset( p_null->p_buffer, 0, p_null->i_buffer );
909 input_DecoderDecode( p_dec, p_null );
912 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
913 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
916 static void DecoderSignalFlushed( decoder_t *p_dec )
918 decoder_owner_sys_t *p_owner = p_dec->p_owner;
920 vlc_mutex_lock( &p_owner->lock );
922 if( p_owner->b_flushing )
924 p_owner->b_flushing = false;
925 vlc_cond_signal( &p_owner->wait );
928 vlc_mutex_unlock( &p_owner->lock );
931 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
933 decoder_owner_sys_t *p_owner = p_dec->p_owner;
935 vlc_mutex_lock( &p_owner->lock );
937 if( p_owner->b_buffering )
940 p_owner->buffer.b_full = true;
941 vlc_cond_signal( &p_owner->wait );
944 vlc_mutex_unlock( &p_owner->lock );
947 static bool DecoderIsFlushing( decoder_t *p_dec )
949 decoder_owner_sys_t *p_owner = p_dec->p_owner;
952 vlc_mutex_lock( &p_owner->lock );
954 b_flushing = p_owner->b_flushing;
956 vlc_mutex_unlock( &p_owner->lock );
961 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
963 decoder_owner_sys_t *p_owner = p_dec->p_owner;
965 vlc_assert_locked( &p_owner->lock );
967 while( !p_owner->b_flushing )
969 if( p_owner->b_paused )
971 if( p_owner->b_buffering && !p_owner->buffer.b_full )
973 if( p_owner->pause.i_ignore > 0 )
975 p_owner->pause.i_ignore--;
981 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
984 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
988 *pb_reject = p_owner->b_flushing;
991 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
993 decoder_owner_sys_t *p_owner = p_dec->p_owner;
995 vlc_assert_locked( &p_owner->lock );
997 /* XXX only audio and video output have to be paused.
998 * - for sout it is useless
999 * - for subs, it is done by the vout
1001 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1003 if( p_owner->p_aout && p_owner->p_aout_input )
1004 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1007 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1009 if( p_owner->p_vout )
1010 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1013 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1015 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1016 *pi_preroll = INT64_MAX;
1017 else if( p->i_pts > 0 )
1018 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1019 else if( p->i_dts > 0 )
1020 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1023 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
1025 mtime_t current_date = mdate();
1027 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1028 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1030 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1031 * for teletext streams. */
1032 return current_date + 400000 + i_ts_delay;
1037 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1038 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1040 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1041 input_clock_t *p_clock = p_owner->p_clock;
1044 vlc_assert_locked( &p_owner->lock );
1046 const mtime_t i_ts_delay = p_owner->p_input->p->i_pts_delay;
1047 const mtime_t i_es_delay = p_owner->i_ts_delay;
1051 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1054 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1055 *pi_ts0 + i_es_delay );
1056 if( pi_ts1 && *pi_ts1 > 0 )
1057 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1058 *pi_ts1 + i_es_delay );
1060 /* Do not create ephemere data because of rounding errors */
1061 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1065 i_rate = input_clock_GetRate( p_clock );
1068 *pi_duration = ( *pi_duration * i_rate +
1069 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1076 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1077 if( pi_ts1 && *pi_ts1 <= 0 )
1083 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1084 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1088 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1089 int *pi_played_sum, int *pi_lost_sum )
1091 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1092 aout_instance_t *p_aout = p_owner->p_aout;
1093 aout_input_t *p_aout_input = p_owner->p_aout_input;
1096 if( p_audio->start_date <= 0 )
1098 msg_Warn( p_dec, "non-dated audio buffer received" );
1100 aout_BufferFree( p_audio );
1105 vlc_mutex_lock( &p_owner->lock );
1107 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1109 p_audio->p_next = NULL;
1111 *p_owner->buffer.pp_audio_next = p_audio;
1112 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1114 p_owner->buffer.i_count++;
1115 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1116 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1118 p_owner->buffer.b_full = true;
1119 vlc_cond_signal( &p_owner->wait );
1125 bool b_has_more = false;
1127 DecoderWaitUnblock( p_dec, &b_reject );
1129 if( p_owner->b_buffering )
1131 vlc_mutex_unlock( &p_owner->lock );
1136 if( p_owner->buffer.p_audio )
1138 p_audio = p_owner->buffer.p_audio;
1140 p_owner->buffer.p_audio = p_audio->p_next;
1141 p_owner->buffer.i_count--;
1143 b_has_more = p_owner->buffer.p_audio != NULL;
1145 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1149 int i_rate = INPUT_RATE_DEFAULT;
1152 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1153 &i_rate, &i_delay, false );
1155 vlc_mutex_unlock( &p_owner->lock );
1158 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1160 if( !p_aout || !p_aout_input ||
1161 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1162 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1163 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1166 /* Do not wait against unprotected date */
1167 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1168 while( !b_reject && i_deadline > mdate() )
1170 vlc_mutex_lock( &p_owner->lock );
1171 if( p_owner->b_flushing || p_dec->b_die )
1174 vlc_mutex_unlock( &p_owner->lock );
1177 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1178 vlc_mutex_unlock( &p_owner->lock );
1183 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1184 *pi_played_sum += 1;
1185 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1189 if( p_audio->start_date <= 0 )
1191 msg_Warn( p_dec, "non-dated audio buffer received" );
1193 else if( p_audio->start_date > i_max_date )
1195 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1196 p_audio->start_date - mdate() );
1199 aout_BufferFree( p_audio );
1205 vlc_mutex_lock( &p_owner->lock );
1206 if( !p_owner->buffer.p_audio )
1208 vlc_mutex_unlock( &p_owner->lock );
1214 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1216 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1217 input_thread_t *p_input = p_owner->p_input;
1218 aout_buffer_t *p_aout_buf;
1223 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1225 aout_instance_t *p_aout = p_owner->p_aout;
1226 aout_input_t *p_aout_input = p_owner->p_aout_input;
1230 /* It prevent freezing VLC in case of broken decoder */
1231 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1233 block_Release( p_block );
1238 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1240 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1244 if( p_owner->i_preroll_end > 0 )
1246 msg_Dbg( p_dec, "End of audio preroll" );
1247 if( p_owner->p_aout && p_owner->p_aout_input )
1248 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1250 p_owner->i_preroll_end = -1;
1253 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1256 /* Update ugly stat */
1257 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1259 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1261 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1263 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1265 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1268 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1271 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1273 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1279 assert( p_dec_cc->pf_get_cc != NULL );
1281 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1282 if( !p_owner->cc.b_supported )
1285 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1289 vlc_mutex_lock( &p_owner->lock );
1290 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1292 p_owner->cc.pb_present[i] |= pb_present[i];
1293 if( p_owner->cc.pp_decoder[i] )
1297 for( i = 0; i < 4; i++ )
1299 if( !p_owner->cc.pp_decoder[i] )
1302 if( i_cc_decoder > 1 )
1303 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1305 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1308 vlc_mutex_unlock( &p_owner->lock );
1311 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1312 int *pi_played_sum, int *pi_lost_sum )
1314 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1315 vout_thread_t *p_vout = p_owner->p_vout;
1316 bool b_first_buffered;
1318 if( p_picture->date <= 0 )
1320 msg_Warn( p_vout, "non-dated video buffer received" );
1322 vout_DropPicture( p_vout, p_picture );
1327 vlc_mutex_lock( &p_owner->lock );
1329 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1331 p_picture->p_next = NULL;
1333 *p_owner->buffer.pp_picture_next = p_picture;
1334 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1336 p_owner->buffer.i_count++;
1337 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1338 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1340 p_owner->buffer.b_full = true;
1341 vlc_cond_signal( &p_owner->wait );
1344 b_first_buffered = p_owner->buffer.p_picture != NULL;
1346 for( ;; b_first_buffered = false )
1348 bool b_has_more = false;
1352 DecoderWaitUnblock( p_dec, &b_reject );
1354 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1356 vlc_mutex_unlock( &p_owner->lock );
1359 bool b_buffering_first = p_owner->b_buffering;
1362 if( p_owner->buffer.p_picture )
1364 p_picture = p_owner->buffer.p_picture;
1366 p_owner->buffer.p_picture = p_picture->p_next;
1367 p_owner->buffer.i_count--;
1369 b_has_more = p_owner->buffer.p_picture != NULL;
1371 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1375 if( b_buffering_first )
1377 assert( p_owner->buffer.b_first );
1378 assert( !p_owner->buffer.i_count );
1379 msg_Dbg( p_dec, "Received first picture" );
1380 p_owner->buffer.b_first = false;
1381 p_picture->b_force = true;
1384 int i_rate = INPUT_RATE_DEFAULT;
1386 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1387 &i_rate, &i_delay, false );
1389 vlc_mutex_unlock( &p_owner->lock );
1392 const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1394 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1399 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1401 /* Be sure to not display old picture after our own */
1402 vout_Flush( p_vout, p_picture->date );
1403 p_owner->i_last_rate = i_rate;
1405 vout_DisplayPicture( p_vout, p_picture );
1409 if( p_picture->date <= 0 )
1411 msg_Warn( p_vout, "non-dated video buffer received" );
1415 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1416 p_picture->date - mdate() );
1419 vout_DropPicture( p_vout, p_picture );
1423 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1425 *pi_played_sum += i_tmp_display;
1426 *pi_lost_sum += i_tmp_lost;
1428 if( !b_has_more || b_buffering_first )
1431 vlc_mutex_lock( &p_owner->lock );
1432 if( !p_owner->buffer.p_picture )
1434 vlc_mutex_unlock( &p_owner->lock );
1440 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1442 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1443 input_thread_t *p_input = p_owner->p_input;
1447 int i_displayed = 0;
1449 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1451 vout_thread_t *p_vout = p_owner->p_vout;
1454 /* It prevent freezing VLC in case of broken decoder */
1455 vout_DropPicture( p_vout, p_pic );
1457 block_Release( p_block );
1463 if( p_pic->date < p_owner->i_preroll_end )
1465 vout_DropPicture( p_vout, p_pic );
1469 if( p_owner->i_preroll_end > 0 )
1471 msg_Dbg( p_dec, "End of video preroll" );
1473 vout_Flush( p_vout, 1 );
1475 p_owner->i_preroll_end = -1;
1478 if( p_dec->pf_get_cc &&
1479 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1480 DecoderGetCc( p_dec, p_dec );
1482 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1484 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1486 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1488 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1490 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1493 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1496 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1500 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1503 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1504 vout_thread_t *p_vout = p_owner->p_spu_vout;
1507 if( p_subpic->i_start <= 0 )
1509 msg_Warn( p_dec, "non-dated spu buffer received" );
1510 subpicture_Delete( p_subpic );
1515 vlc_mutex_lock( &p_owner->lock );
1517 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1519 p_subpic->p_next = NULL;
1521 *p_owner->buffer.pp_subpic_next = p_subpic;
1522 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1524 p_owner->buffer.i_count++;
1525 /* XXX it is important to be full after the first one */
1526 if( p_owner->buffer.i_count > 0 )
1528 p_owner->buffer.b_full = true;
1529 vlc_cond_signal( &p_owner->wait );
1535 bool b_has_more = false;
1537 DecoderWaitUnblock( p_dec, &b_reject );
1539 if( p_owner->b_buffering )
1541 vlc_mutex_unlock( &p_owner->lock );
1546 if( p_owner->buffer.p_subpic )
1548 p_subpic = p_owner->buffer.p_subpic;
1550 p_owner->buffer.p_subpic = p_subpic->p_next;
1551 p_owner->buffer.i_count--;
1553 b_has_more = p_owner->buffer.p_subpic != NULL;
1555 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1559 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1560 NULL, NULL, b_telx );
1562 vlc_mutex_unlock( &p_owner->lock );
1565 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1567 subpicture_Delete( p_subpic );
1571 vlc_mutex_lock( &p_owner->lock );
1572 if( !p_owner->buffer.p_subpic )
1574 vlc_mutex_unlock( &p_owner->lock );
1580 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1583 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1585 assert( p_owner->p_clock );
1586 assert( !p_sout_block->p_next );
1588 vlc_mutex_lock( &p_owner->lock );
1590 if( p_owner->b_buffering || p_owner->buffer.p_block )
1592 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1594 p_owner->buffer.i_count++;
1595 /* XXX it is important to be full after the first one */
1596 if( p_owner->buffer.i_count > 0 )
1598 p_owner->buffer.b_full = true;
1599 vlc_cond_signal( &p_owner->wait );
1605 bool b_has_more = false;
1607 DecoderWaitUnblock( p_dec, &b_reject );
1609 if( p_owner->b_buffering )
1611 vlc_mutex_unlock( &p_owner->lock );
1616 if( p_owner->buffer.p_block )
1618 p_sout_block = p_owner->buffer.p_block;
1620 p_owner->buffer.p_block = p_sout_block->p_next;
1621 p_owner->buffer.i_count--;
1623 b_has_more = p_owner->buffer.p_block != NULL;
1625 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1627 p_sout_block->p_next = NULL;
1629 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1630 &p_sout_block->i_length,
1631 &p_sout_block->i_rate, NULL, b_telx );
1633 vlc_mutex_unlock( &p_owner->lock );
1636 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1638 block_Release( p_sout_block );
1642 vlc_mutex_lock( &p_owner->lock );
1643 if( !p_owner->buffer.p_block )
1645 vlc_mutex_unlock( &p_owner->lock );
1652 static void DecoderFlushBuffering( decoder_t *p_dec )
1654 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1656 vlc_assert_locked( &p_owner->lock );
1658 while( p_owner->buffer.p_picture )
1660 picture_t *p_picture = p_owner->buffer.p_picture;
1662 p_owner->buffer.p_picture = p_picture->p_next;
1663 p_owner->buffer.i_count--;
1665 if( p_owner->p_vout )
1666 vout_DropPicture( p_owner->p_vout, p_picture );
1668 if( !p_owner->buffer.p_picture )
1669 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1671 while( p_owner->buffer.p_audio )
1673 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1675 p_owner->buffer.p_audio = p_audio->p_next;
1676 p_owner->buffer.i_count--;
1678 aout_BufferFree( p_audio );
1680 if( !p_owner->buffer.p_audio )
1681 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1683 while( p_owner->buffer.p_subpic )
1685 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1687 p_owner->buffer.p_subpic = p_subpic->p_next;
1688 p_owner->buffer.i_count--;
1690 subpicture_Delete( p_subpic );
1692 if( !p_owner->buffer.p_subpic )
1693 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1695 if( p_owner->buffer.p_block )
1697 block_ChainRelease( p_owner->buffer.p_block );
1699 p_owner->buffer.i_count = 0;
1700 p_owner->buffer.p_block = NULL;
1701 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1705 /* This function process a block for sout
1707 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1709 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1710 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1711 block_t *p_sout_block;
1713 while( ( p_sout_block =
1714 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1716 if( !p_owner->p_sout_input )
1718 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1720 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1721 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1722 if( p_dec->fmt_in.psz_language )
1724 if( p_owner->sout.psz_language )
1725 free( p_owner->sout.psz_language );
1726 p_owner->sout.psz_language =
1727 strdup( p_dec->fmt_in.psz_language );
1730 p_owner->p_sout_input =
1731 sout_InputNew( p_owner->p_sout,
1734 if( p_owner->p_sout_input == NULL )
1736 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1737 (char *)&p_owner->sout.i_codec );
1738 p_dec->b_error = true;
1740 while( p_sout_block )
1742 block_t *p_next = p_sout_block->p_next;
1743 block_Release( p_sout_block );
1744 p_sout_block = p_next;
1750 while( p_sout_block )
1752 block_t *p_next = p_sout_block->p_next;
1754 p_sout_block->p_next = NULL;
1756 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1758 p_sout_block = p_next;
1761 /* For now it's enough, as only sout impact on this flag */
1762 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1763 p_owner->p_input->p->b_out_pace_control )
1765 msg_Dbg( p_dec, "switching to sync mode" );
1766 p_owner->p_input->p->b_out_pace_control = false;
1768 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1769 !p_owner->p_input->p->b_out_pace_control )
1771 msg_Dbg( p_dec, "switching to async mode" );
1772 p_owner->p_input->p->b_out_pace_control = true;
1777 /* This function process a video block
1779 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1781 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1783 if( p_owner->p_packetizer )
1785 block_t *p_packetized_block;
1786 decoder_t *p_packetizer = p_owner->p_packetizer;
1788 while( (p_packetized_block =
1789 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1791 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1793 es_format_Clean( &p_dec->fmt_in );
1794 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1796 if( p_packetizer->pf_get_cc )
1797 DecoderGetCc( p_dec, p_packetizer );
1799 while( p_packetized_block )
1801 block_t *p_next = p_packetized_block->p_next;
1802 p_packetized_block->p_next = NULL;
1804 DecoderDecodeVideo( p_dec, p_packetized_block );
1806 p_packetized_block = p_next;
1812 DecoderDecodeVideo( p_dec, p_block );
1815 if( b_flush && p_owner->p_vout )
1816 vout_Flush( p_owner->p_vout, 1 );
1819 /* This function process a audio block
1821 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1823 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1825 if( p_owner->p_packetizer )
1827 block_t *p_packetized_block;
1828 decoder_t *p_packetizer = p_owner->p_packetizer;
1830 while( (p_packetized_block =
1831 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1833 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1835 es_format_Clean( &p_dec->fmt_in );
1836 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1839 while( p_packetized_block )
1841 block_t *p_next = p_packetized_block->p_next;
1842 p_packetized_block->p_next = NULL;
1844 DecoderDecodeAudio( p_dec, p_packetized_block );
1846 p_packetized_block = p_next;
1852 DecoderDecodeAudio( p_dec, p_block );
1855 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1856 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1859 /* This function process a subtitle block
1861 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1863 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1864 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1866 input_thread_t *p_input = p_owner->p_input;
1867 vout_thread_t *p_vout;
1868 subpicture_t *p_spu;
1870 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1872 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1873 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1874 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1876 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1877 if( p_vout && p_owner->p_spu_vout == p_vout )
1879 /* Preroll does not work very well with subtitle */
1880 if( p_spu->i_start > 0 &&
1881 p_spu->i_start < p_owner->i_preroll_end &&
1882 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1884 subpicture_Delete( p_spu );
1888 DecoderPlaySpu( p_dec, p_spu, b_telx );
1893 subpicture_Delete( p_spu );
1896 vlc_object_release( p_vout );
1899 if( b_flush && p_owner->p_spu_vout )
1901 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1903 if( p_vout && p_owner->p_spu_vout == p_vout )
1904 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1905 p_owner->i_spu_channel );
1908 vlc_object_release( p_vout );
1913 static void DecoderProcessOnFlush( decoder_t *p_dec )
1915 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1917 vlc_mutex_lock( &p_owner->lock );
1918 DecoderFlushBuffering( p_dec );
1919 vlc_mutex_unlock( &p_owner->lock );
1921 DecoderSignalFlushed( p_dec );
1926 * \param p_dec the decoder object
1927 * \param p_block the block to decode
1928 * \return VLC_SUCCESS or an error code
1930 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1932 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1933 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1935 if( p_block && p_block->i_buffer <= 0 )
1937 assert( !b_flush_request );
1938 block_Release( p_block );
1943 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1946 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1948 DecoderProcessSout( p_dec, p_block );
1953 bool b_flush = false;
1957 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1958 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1960 b_flush = !b_flushing && b_flush_request;
1962 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1965 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1967 DecoderProcessAudio( p_dec, p_block, b_flush );
1969 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1971 DecoderProcessVideo( p_dec, p_block, b_flush );
1973 else if( p_dec->fmt_in.i_cat == SPU_ES )
1975 DecoderProcessSpu( p_dec, p_block, b_flush );
1979 msg_Err( p_dec, "unknown ES format" );
1980 p_dec->b_error = true;
1985 if( b_flush_request )
1986 DecoderProcessOnFlush( p_dec );
1989 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1991 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1995 block_Release( p_block );
1997 if( b_flush_request )
1998 DecoderProcessOnFlush( p_dec );
2003 * Destroys a decoder object
2005 * \param p_dec the decoder object
2008 static void DeleteDecoder( decoder_t * p_dec )
2010 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2012 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2013 (char*)&p_dec->fmt_in.i_codec,
2014 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2016 /* Free all packets still in the decoder fifo. */
2017 block_FifoEmpty( p_owner->p_fifo );
2018 block_FifoRelease( p_owner->p_fifo );
2021 vlc_mutex_lock( &p_owner->lock );
2022 DecoderFlushBuffering( p_dec );
2023 vlc_mutex_unlock( &p_owner->lock );
2026 if( p_owner->p_aout_input )
2027 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2028 if( p_owner->p_aout )
2030 input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2032 input_SendEventAout( p_owner->p_input );
2033 p_owner->p_aout = NULL;
2035 if( p_owner->p_vout )
2037 /* Hack to make sure all the the pictures are freed by the decoder */
2038 vout_FixLeaks( p_owner->p_vout, true );
2041 input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
2042 input_SendEventVout( p_owner->p_input );
2046 if( p_owner->p_sout_input )
2048 sout_InputDelete( p_owner->p_sout_input );
2049 es_format_Clean( &p_owner->sout );
2053 if( p_dec->fmt_in.i_cat == SPU_ES )
2055 vout_thread_t *p_vout;
2057 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2060 if( p_owner->p_spu_vout == p_vout )
2061 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2062 vlc_object_release( p_vout );
2066 es_format_Clean( &p_dec->fmt_in );
2067 es_format_Clean( &p_dec->fmt_out );
2068 if( p_dec->p_description )
2069 vlc_meta_Delete( p_dec->p_description );
2070 es_format_Clean( &p_owner->fmt_description );
2071 if( p_owner->p_description )
2072 vlc_meta_Delete( p_owner->p_description );
2074 if( p_owner->p_packetizer )
2076 module_unneed( p_owner->p_packetizer,
2077 p_owner->p_packetizer->p_module );
2078 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2079 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2080 if( p_owner->p_packetizer->p_description )
2081 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2082 vlc_object_detach( p_owner->p_packetizer );
2083 vlc_object_release( p_owner->p_packetizer );
2086 vlc_cond_destroy( &p_owner->wait );
2087 vlc_mutex_destroy( &p_owner->lock );
2089 vlc_object_detach( p_dec );
2094 /*****************************************************************************
2095 * Buffers allocation callbacks for the decoders
2096 *****************************************************************************/
2097 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2099 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2101 vlc_assert_locked( &p_owner->lock );
2103 p_owner->b_fmt_description = true;
2105 /* Copy es_format */
2106 es_format_Clean( &p_owner->fmt_description );
2107 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2109 /* Move p_description */
2110 if( p_owner->p_description && p_dec->p_description )
2111 vlc_meta_Delete( p_owner->p_description );
2112 p_owner->p_description = p_dec->p_description;
2113 p_dec->p_description = NULL;
2115 static vout_thread_t *aout_request_vout( void *p_private,
2116 vout_thread_t *p_vout, video_format_t *p_fmt )
2118 decoder_t *p_dec = p_private;
2120 p_vout = input_ressource_RequestVout( p_dec->p_owner->p_input->p->p_ressource, p_vout, p_fmt );
2121 input_SendEventVout( p_dec->p_owner->p_input );
2126 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2128 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2129 aout_buffer_t *p_buffer;
2131 if( p_owner->p_aout_input != NULL &&
2132 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2133 p_dec->fmt_out.audio.i_original_channels !=
2134 p_owner->audio.i_original_channels ||
2135 p_dec->fmt_out.audio.i_bytes_per_frame !=
2136 p_owner->audio.i_bytes_per_frame ) )
2138 aout_input_t *p_aout_input = p_owner->p_aout_input;
2140 /* Parameters changed, restart the aout */
2141 vlc_mutex_lock( &p_owner->lock );
2143 DecoderFlushBuffering( p_dec );
2145 p_owner->p_aout_input = NULL;
2146 aout_DecDelete( p_owner->p_aout, p_aout_input );
2148 vlc_mutex_unlock( &p_owner->lock );
2151 if( p_owner->p_aout_input == NULL )
2153 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2154 audio_sample_format_t format;
2155 aout_input_t *p_aout_input;
2156 aout_instance_t *p_aout;
2157 aout_request_vout_t request_vout;
2159 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2160 p_owner->audio = p_dec->fmt_out.audio;
2162 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2163 if( i_force_dolby &&
2164 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2165 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2167 if( i_force_dolby == 1 )
2169 format.i_original_channels = format.i_original_channels |
2170 AOUT_CHAN_DOLBYSTEREO;
2172 else /* i_force_dolby == 2 */
2174 format.i_original_channels = format.i_original_channels &
2175 ~AOUT_CHAN_DOLBYSTEREO;
2179 request_vout.pf_request_vout = aout_request_vout;
2180 request_vout.p_private = p_dec;
2182 p_aout = p_owner->p_aout;
2184 p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2185 p_aout_input = aout_DecNew( p_dec, &p_aout,
2186 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2188 vlc_mutex_lock( &p_owner->lock );
2190 p_owner->p_aout = p_aout;
2191 p_owner->p_aout_input = p_aout_input;
2192 DecoderUpdateFormatLocked( p_dec );
2194 vlc_mutex_unlock( &p_owner->lock );
2196 input_SendEventAout( p_owner->p_input );
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 );