1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_block.h>
40 #include <vlc_codec.h>
43 #include <vlc_dialog.h>
45 #include "audio_output/aout_internal.h"
46 #include "stream_output/stream_output.h"
47 #include "input_internal.h"
53 #include "../video_output/vout_control.h"
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, bool,
56 sout_instance_t *p_sout );
57 static void DeleteDecoder( decoder_t * );
59 static void *DecoderThread( vlc_object_t * );
60 static void DecoderProcess( decoder_t *, block_t * );
61 static void DecoderError( decoder_t *p_dec, block_t *p_block );
62 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
63 static void DecoderFlush( decoder_t * );
64 static void DecoderSignalBuffering( decoder_t *, bool );
65 static void DecoderFlushBuffering( decoder_t * );
67 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
69 /* Buffers allocation callbacks for the decoders */
70 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
71 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
73 static picture_t *vout_new_buffer( decoder_t * );
74 static void vout_del_buffer( decoder_t *, picture_t * );
75 static void vout_link_picture( decoder_t *, picture_t * );
76 static void vout_unlink_picture( decoder_t *, picture_t * );
78 static subpicture_t *spu_new_buffer( decoder_t * );
79 static void spu_del_buffer( decoder_t *, subpicture_t * );
81 struct decoder_owner_sys_t
83 int64_t i_preroll_end;
85 input_thread_t *p_input;
86 input_clock_t *p_clock;
89 vout_thread_t *p_spu_vout;
93 sout_instance_t *p_sout;
94 sout_packetizer_input_t *p_sout_input;
96 /* Some decoders require already packetized data (ie. not truncated) */
97 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 */
115 vlc_cond_t wait_request;
116 vlc_cond_t wait_acknowledge;
118 /* -- These variables need locking on write(only) -- */
119 aout_instance_t *p_aout;
120 aout_input_t *p_aout_input;
122 vout_thread_t *p_vout;
124 /* -- Theses variables need locking on read *and* write -- */
142 picture_t *p_picture;
143 picture_t **pp_picture_next;
145 subpicture_t *p_subpic;
146 subpicture_t **pp_subpic_next;
148 aout_buffer_t *p_audio;
149 aout_buffer_t **pp_audio_next;
152 block_t **pp_block_next;
163 decoder_t *pp_decoder[4];
170 #define DECODER_MAX_BUFFERING_COUNT (4)
171 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
172 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
174 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
175 * a bogus PTS and won't be displayed */
176 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
179 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
182 /*****************************************************************************
184 *****************************************************************************/
185 picture_t *decoder_NewPicture( decoder_t *p_decoder )
187 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
189 msg_Warn( p_decoder, "can't get output picture" );
192 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
194 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
196 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
198 p_decoder->pf_picture_link( p_decoder, p_picture );
200 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
202 p_decoder->pf_picture_unlink( p_decoder, p_picture );
205 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
207 if( !p_decoder->pf_aout_buffer_new )
209 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
211 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
213 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
216 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
218 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
220 msg_Warn( p_decoder, "can't get output subpicture" );
223 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
225 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
228 /* decoder_GetInputAttachments:
230 int decoder_GetInputAttachments( decoder_t *p_dec,
231 input_attachment_t ***ppp_attachment,
234 if( !p_dec->pf_get_attachments )
237 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
239 /* decoder_GetDisplayDate:
241 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
243 if( !p_dec->pf_get_display_date )
244 return VLC_TS_INVALID;
246 return p_dec->pf_get_display_date( p_dec, i_ts );
248 /* decoder_GetDisplayRate:
250 int decoder_GetDisplayRate( decoder_t *p_dec )
252 if( !p_dec->pf_get_display_rate )
253 return INPUT_RATE_DEFAULT;
255 return p_dec->pf_get_display_rate( p_dec );
259 * Spawns a new decoder thread
261 * \param p_input the input thread
262 * \param p_es the es descriptor
263 * \return the spawned decoder object
265 decoder_t *input_DecoderNew( input_thread_t *p_input,
266 es_format_t *fmt, input_clock_t *p_clock,
267 sout_instance_t *p_sout )
269 decoder_t *p_dec = NULL;
270 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
273 /* Create the decoder configuration structure */
274 p_dec = CreateDecoder( p_input, fmt, p_sout != NULL, p_sout );
277 msg_Err( p_input, "could not create %s", psz_type );
278 dialog_Fatal( p_input, _("Streaming / Transcoding failed"),
279 _("VLC could not open the %s module."),
280 vlc_gettext( psz_type ) );
284 if( !p_dec->p_module )
286 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
288 DeleteDecoder( p_dec );
289 vlc_object_release( p_dec );
293 p_dec->p_owner->p_clock = p_clock;
294 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
296 if( p_dec->fmt_out.i_cat == AUDIO_ES )
297 i_priority = VLC_THREAD_PRIORITY_AUDIO;
299 i_priority = VLC_THREAD_PRIORITY_VIDEO;
301 /* Spawn the decoder thread */
302 if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
304 msg_Err( p_dec, "cannot spawn decoder thread" );
305 module_unneed( p_dec, p_dec->p_module );
306 DeleteDecoder( p_dec );
307 vlc_object_release( p_dec );
315 * Kills a decoder thread and waits until it's finished
317 * \param p_input the input thread
318 * \param p_es the es descriptor
321 void input_DecoderDelete( decoder_t *p_dec )
323 decoder_owner_sys_t *p_owner = p_dec->p_owner;
325 vlc_object_kill( p_dec );
327 /* Make sure we aren't paused/buffering/waiting anymore */
328 vlc_mutex_lock( &p_owner->lock );
329 const bool b_was_paused = p_owner->b_paused;
330 p_owner->b_paused = false;
331 p_owner->b_buffering = false;
332 p_owner->b_flushing = true;
333 vlc_cond_signal( &p_owner->wait_request );
334 vlc_mutex_unlock( &p_owner->lock );
336 vlc_thread_join( p_dec );
337 p_owner->b_paused = b_was_paused;
339 module_unneed( p_dec, p_dec->p_module );
342 if( p_dec->p_owner->cc.b_supported )
345 for( i = 0; i < 4; i++ )
346 input_DecoderSetCcState( p_dec, false, i );
349 /* Delete decoder configuration */
350 DeleteDecoder( p_dec );
352 /* Delete the decoder */
353 vlc_object_release( p_dec );
357 * Put a block_t in the decoder's fifo.
358 * Thread-safe w.r.t. the decoder. May be a cancellation point.
360 * \param p_dec the decoder object
361 * \param p_block the data block
363 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
365 decoder_owner_sys_t *p_owner = p_dec->p_owner;
369 /* The fifo is not consummed when buffering and so will
371 * There is no need to lock as b_buffering is never modify
372 * inside decoder thread. */
373 if( !p_owner->b_buffering )
374 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
376 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
378 /* FIXME: ideally we would check the time amount of data
379 * in the FIFO instead of its size. */
380 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
381 "consumed quickly enough), resetting fifo!" );
382 block_FifoEmpty( p_owner->p_fifo );
385 block_FifoPut( p_owner->p_fifo, p_block );
388 bool input_DecoderIsEmpty( decoder_t * p_dec )
390 assert( !p_dec->p_owner->b_buffering );
392 /* FIXME that's not really true */
393 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
396 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
398 decoder_owner_sys_t *p_owner = p_dec->p_owner;
401 vlc_mutex_lock( &p_owner->lock );
402 for( i = 0; i < 4; i++ )
403 pb_present[i] = p_owner->cc.pb_present[i];
404 vlc_mutex_unlock( &p_owner->lock );
406 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
408 decoder_owner_sys_t *p_owner = p_dec->p_owner;
410 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
412 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
417 static const vlc_fourcc_t fcc[4] = {
418 VLC_FOURCC('c', 'c', '1', ' '),
419 VLC_FOURCC('c', 'c', '2', ' '),
420 VLC_FOURCC('c', 'c', '3', ' '),
421 VLC_FOURCC('c', 'c', '4', ' '),
426 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
427 p_cc = CreateDecoder( p_owner->p_input, &fmt, false, p_owner->p_sout );
430 msg_Err( p_dec, "could not create decoder" );
431 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
432 _("VLC could not open the decoder module.") );
435 else if( !p_cc->p_module )
437 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
438 DeleteDecoder( p_cc );
439 vlc_object_release( p_cc );
442 p_cc->p_owner->p_clock = p_owner->p_clock;
444 vlc_mutex_lock( &p_owner->lock );
445 p_owner->cc.pp_decoder[i_channel] = p_cc;
446 vlc_mutex_unlock( &p_owner->lock );
452 vlc_mutex_lock( &p_owner->lock );
453 p_cc = p_owner->cc.pp_decoder[i_channel];
454 p_owner->cc.pp_decoder[i_channel] = NULL;
455 vlc_mutex_unlock( &p_owner->lock );
459 vlc_object_kill( p_cc );
460 module_unneed( p_cc, p_cc->p_module );
461 DeleteDecoder( p_cc );
462 vlc_object_release( p_cc );
467 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
469 decoder_owner_sys_t *p_owner = p_dec->p_owner;
472 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
475 vlc_mutex_lock( &p_owner->lock );
476 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
477 vlc_mutex_unlock( &p_owner->lock );
481 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
483 decoder_owner_sys_t *p_owner = p_dec->p_owner;
485 vlc_mutex_lock( &p_owner->lock );
487 assert( !p_owner->b_paused || !b_paused );
489 p_owner->b_paused = b_paused;
490 p_owner->pause.i_date = i_date;
491 p_owner->pause.i_ignore = 0;
492 vlc_cond_signal( &p_owner->wait_request );
494 DecoderOutputChangePause( p_dec, b_paused, i_date );
496 vlc_mutex_unlock( &p_owner->lock );
499 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
501 decoder_owner_sys_t *p_owner = p_dec->p_owner;
503 vlc_mutex_lock( &p_owner->lock );
504 p_owner->i_ts_delay = i_delay;
505 vlc_mutex_unlock( &p_owner->lock );
508 void input_DecoderStartBuffering( decoder_t *p_dec )
510 decoder_owner_sys_t *p_owner = p_dec->p_owner;
512 vlc_mutex_lock( &p_owner->lock );
514 DecoderFlush( p_dec );
516 p_owner->buffer.b_first = true;
517 p_owner->buffer.b_full = false;
518 p_owner->buffer.i_count = 0;
520 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
521 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
523 p_owner->buffer.p_picture = NULL;
524 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
526 p_owner->buffer.p_subpic = NULL;
527 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
529 p_owner->buffer.p_audio = NULL;
530 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
532 p_owner->buffer.p_block = NULL;
533 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
536 p_owner->b_buffering = true;
538 vlc_cond_signal( &p_owner->wait_request );
540 vlc_mutex_unlock( &p_owner->lock );
543 void input_DecoderStopBuffering( decoder_t *p_dec )
545 decoder_owner_sys_t *p_owner = p_dec->p_owner;
547 vlc_mutex_lock( &p_owner->lock );
549 p_owner->b_buffering = false;
551 vlc_cond_signal( &p_owner->wait_request );
553 vlc_mutex_unlock( &p_owner->lock );
556 void input_DecoderWaitBuffering( decoder_t *p_dec )
558 decoder_owner_sys_t *p_owner = p_dec->p_owner;
560 vlc_mutex_lock( &p_owner->lock );
562 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
564 block_FifoWake( p_owner->p_fifo );
565 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
568 vlc_mutex_unlock( &p_owner->lock );
571 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
573 decoder_owner_sys_t *p_owner = p_dec->p_owner;
577 vlc_mutex_lock( &p_owner->lock );
578 if( p_dec->fmt_out.i_cat == VIDEO_ES )
580 if( p_owner->b_paused && p_owner->p_vout )
582 vout_NextPicture( p_owner->p_vout, pi_duration );
583 p_owner->pause.i_ignore++;
584 vlc_cond_signal( &p_owner->wait_request );
589 /* TODO subtitle should not be flushed */
590 DecoderFlush( p_dec );
592 vlc_mutex_unlock( &p_owner->lock );
595 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
597 decoder_owner_sys_t *p_owner = p_dec->p_owner;
600 vlc_mutex_lock( &p_owner->lock );
601 b_changed = p_owner->b_fmt_description;
605 es_format_Copy( p_fmt, &p_owner->fmt_description );
610 if( p_owner->p_description )
612 *pp_meta = vlc_meta_New();
614 vlc_meta_Merge( *pp_meta, p_owner->p_description );
617 p_owner->b_fmt_description = false;
619 vlc_mutex_unlock( &p_owner->lock );
623 /*****************************************************************************
625 *****************************************************************************/
626 static int DecoderGetInputAttachments( decoder_t *p_dec,
627 input_attachment_t ***ppp_attachment,
630 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
631 ppp_attachment, pi_attachment );
633 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
635 decoder_owner_sys_t *p_owner = p_dec->p_owner;
637 vlc_mutex_lock( &p_owner->lock );
638 if( p_owner->b_buffering || p_owner->b_paused )
639 i_ts = VLC_TS_INVALID;
640 vlc_mutex_unlock( &p_owner->lock );
642 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
645 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
646 return VLC_TS_INVALID;
650 static int DecoderGetDisplayRate( decoder_t *p_dec )
652 decoder_owner_sys_t *p_owner = p_dec->p_owner;
654 if( !p_owner->p_clock )
655 return INPUT_RATE_DEFAULT;
656 return input_clock_GetRate( p_owner->p_clock );
660 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
662 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
663 "VLC probably does not support this sound or video format.",
665 dialog_Fatal( p_dec, _("No suitable decoder module"),
666 _("VLC does not support the audio or video format \"%4.4s\". "
667 "Unfortunately there is no way for you to fix this."),
673 * Create a decoder object
675 * \param p_input the input thread
676 * \param p_es the es descriptor
677 * \param b_packetizer instead of a decoder
678 * \return the decoder object
680 static decoder_t * CreateDecoder( input_thread_t *p_input,
681 es_format_t *fmt, bool b_packetizer,
682 sout_instance_t *p_sout )
685 decoder_owner_sys_t *p_owner;
686 es_format_t null_es_format;
688 p_dec = vlc_object_create( p_input, VLC_OBJECT_DECODER );
692 p_dec->pf_decode_audio = NULL;
693 p_dec->pf_decode_video = NULL;
694 p_dec->pf_decode_sub = NULL;
695 p_dec->pf_get_cc = NULL;
696 p_dec->pf_packetize = NULL;
698 /* Initialize the decoder */
699 p_dec->p_module = NULL;
701 memset( &null_es_format, 0, sizeof(es_format_t) );
702 es_format_Copy( &p_dec->fmt_in, fmt );
703 es_format_Copy( &p_dec->fmt_out, &null_es_format );
705 p_dec->p_description = NULL;
707 /* Allocate our private structure for the decoder */
708 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
709 if( p_dec->p_owner == NULL )
711 vlc_object_release( p_dec );
714 p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
715 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
716 p_dec->p_owner->p_input = p_input;
717 p_dec->p_owner->p_aout = NULL;
718 p_dec->p_owner->p_aout_input = NULL;
719 p_dec->p_owner->p_vout = NULL;
720 p_dec->p_owner->p_spu_vout = NULL;
721 p_dec->p_owner->i_spu_channel = 0;
722 p_dec->p_owner->i_spu_order = 0;
723 p_dec->p_owner->p_sout = p_sout;
724 p_dec->p_owner->p_sout_input = NULL;
725 p_dec->p_owner->p_packetizer = NULL;
726 p_dec->p_owner->b_packetizer = b_packetizer;
729 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
731 free( p_dec->p_owner );
732 vlc_object_release( p_dec );
736 /* Set buffers allocation callbacks for the decoders */
737 p_dec->pf_aout_buffer_new = aout_new_buffer;
738 p_dec->pf_aout_buffer_del = aout_del_buffer;
739 p_dec->pf_vout_buffer_new = vout_new_buffer;
740 p_dec->pf_vout_buffer_del = vout_del_buffer;
741 p_dec->pf_picture_link = vout_link_picture;
742 p_dec->pf_picture_unlink = vout_unlink_picture;
743 p_dec->pf_spu_buffer_new = spu_new_buffer;
744 p_dec->pf_spu_buffer_del = spu_del_buffer;
746 p_dec->pf_get_attachments = DecoderGetInputAttachments;
747 p_dec->pf_get_display_date = DecoderGetDisplayDate;
748 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
750 vlc_object_attach( p_dec, p_input );
752 /* Find a suitable decoder/packetizer module */
754 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
756 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
758 /* Check if decoder requires already packetized data */
760 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
762 p_dec->p_owner->p_packetizer =
763 vlc_object_create( p_input, VLC_OBJECT_DECODER );
764 if( p_dec->p_owner->p_packetizer )
766 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
769 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
772 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
774 p_dec->p_owner->p_packetizer->p_module =
775 module_need( p_dec->p_owner->p_packetizer,
776 "packetizer", "$packetizer", false );
778 if( !p_dec->p_owner->p_packetizer->p_module )
780 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
781 vlc_object_detach( p_dec->p_owner->p_packetizer );
782 vlc_object_release( p_dec->p_owner->p_packetizer );
787 /* Copy ourself the input replay gain */
788 if( fmt->i_cat == AUDIO_ES )
790 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
792 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
794 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
795 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
797 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
799 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
800 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
806 vlc_mutex_init( &p_owner->lock );
807 vlc_cond_init( &p_owner->wait_request );
808 vlc_cond_init( &p_owner->wait_acknowledge );
810 p_owner->b_fmt_description = false;
811 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
812 p_owner->p_description = NULL;
814 p_owner->b_paused = false;
815 p_owner->pause.i_date = VLC_TS_INVALID;
816 p_owner->pause.i_ignore = 0;
818 p_owner->b_buffering = false;
819 p_owner->buffer.b_first = true;
820 p_owner->buffer.b_full = false;
821 p_owner->buffer.i_count = 0;
822 p_owner->buffer.p_picture = NULL;
823 p_owner->buffer.p_subpic = NULL;
824 p_owner->buffer.p_audio = NULL;
825 p_owner->buffer.p_block = NULL;
827 p_owner->b_flushing = false;
830 p_owner->cc.b_supported = false;
833 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
834 p_owner->cc.b_supported = true;
835 if( p_dec->pf_get_cc )
836 p_owner->cc.b_supported = true;
839 for( unsigned i = 0; i < 4; i++ )
841 p_owner->cc.pb_present[i] = false;
842 p_owner->cc.pp_decoder[i] = NULL;
844 p_owner->i_ts_delay = 0;
849 * The decoding main loop
851 * \param p_dec the decoder
853 static void *DecoderThread( vlc_object_t *p_this )
855 decoder_t *p_dec = (decoder_t *)p_this;
856 decoder_owner_sys_t *p_owner = p_dec->p_owner;
858 /* The decoder's main loop */
861 block_t *p_block = block_FifoGet( p_owner->p_fifo );
863 /* Make sure there is no cancellation point other than this one^^.
864 * If you need one, be sure to push cleanup of p_block. */
865 DecoderSignalBuffering( p_dec, p_block == NULL );
869 int canc = vlc_savecancel();
872 DecoderError( p_dec, p_block );
874 DecoderProcess( p_dec, p_block );
876 vlc_restorecancel( canc );
882 static block_t *DecoderBlockFlushNew()
884 block_t *p_null = block_Alloc( 128 );
888 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
889 BLOCK_FLAG_CORRUPTED |
890 BLOCK_FLAG_CORE_FLUSH;
891 memset( p_null->p_buffer, 0, p_null->i_buffer );
896 static void DecoderFlush( decoder_t *p_dec )
898 decoder_owner_sys_t *p_owner = p_dec->p_owner;
900 vlc_assert_locked( &p_owner->lock );
903 block_FifoEmpty( p_owner->p_fifo );
905 /* Monitor for flush end */
906 p_owner->b_flushing = true;
907 vlc_cond_signal( &p_owner->wait_request );
909 /* Send a special block */
910 block_t *p_null = DecoderBlockFlushNew();
913 input_DecoderDecode( p_dec, p_null, false );
916 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
917 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
920 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
922 decoder_owner_sys_t *p_owner = p_dec->p_owner;
924 vlc_mutex_lock( &p_owner->lock );
926 if( p_owner->b_buffering )
929 p_owner->buffer.b_full = true;
930 vlc_cond_signal( &p_owner->wait_acknowledge );
933 vlc_mutex_unlock( &p_owner->lock );
936 static bool DecoderIsFlushing( decoder_t *p_dec )
938 decoder_owner_sys_t *p_owner = p_dec->p_owner;
941 vlc_mutex_lock( &p_owner->lock );
943 b_flushing = p_owner->b_flushing;
945 vlc_mutex_unlock( &p_owner->lock );
950 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
952 decoder_owner_sys_t *p_owner = p_dec->p_owner;
954 vlc_assert_locked( &p_owner->lock );
956 while( !p_owner->b_flushing )
958 if( p_owner->b_paused )
960 if( p_owner->b_buffering && !p_owner->buffer.b_full )
962 if( p_owner->pause.i_ignore > 0 )
964 p_owner->pause.i_ignore--;
970 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
973 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
977 *pb_reject = p_owner->b_flushing;
980 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
982 decoder_owner_sys_t *p_owner = p_dec->p_owner;
984 vlc_assert_locked( &p_owner->lock );
986 /* XXX only audio and video output have to be paused.
987 * - for sout it is useless
988 * - for subs, it is done by the vout
990 if( p_dec->fmt_out.i_cat == AUDIO_ES )
992 if( p_owner->p_aout && p_owner->p_aout_input )
993 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
996 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
998 if( p_owner->p_vout )
999 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1002 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1004 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1005 *pi_preroll = INT64_MAX;
1006 else if( p->i_dts > VLC_TS_INVALID )
1007 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1008 else if( p->i_pts > VLC_TS_INVALID )
1009 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1012 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1014 mtime_t current_date = mdate();
1016 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1017 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1019 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1020 * for teletext streams. */
1021 return current_date + 400000;
1026 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1027 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1029 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1030 input_clock_t *p_clock = p_owner->p_clock;
1032 vlc_assert_locked( &p_owner->lock );
1034 const mtime_t i_es_delay = p_owner->i_ts_delay;
1038 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1041 if( *pi_ts0 > VLC_TS_INVALID )
1043 *pi_ts0 += i_es_delay;
1044 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1045 *pi_ts1 += i_es_delay;
1046 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1047 *pi_ts0 = VLC_TS_INVALID;
1051 i_rate = input_clock_GetRate( p_clock );
1054 /* Do not create ephemere data because of rounding errors */
1055 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1059 *pi_duration = ( *pi_duration * i_rate +
1060 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1067 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1068 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1074 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1075 int *pi_played_sum, int *pi_lost_sum )
1077 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1078 aout_instance_t *p_aout = p_owner->p_aout;
1079 aout_input_t *p_aout_input = p_owner->p_aout_input;
1082 if( p_audio->start_date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1084 msg_Warn( p_dec, "non-dated audio buffer received" );
1086 aout_BufferFree( p_audio );
1091 vlc_mutex_lock( &p_owner->lock );
1093 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1095 p_audio->p_next = NULL;
1097 *p_owner->buffer.pp_audio_next = p_audio;
1098 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1100 p_owner->buffer.i_count++;
1101 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1102 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1104 p_owner->buffer.b_full = true;
1105 vlc_cond_signal( &p_owner->wait_acknowledge );
1111 bool b_has_more = false;
1113 DecoderWaitUnblock( p_dec, &b_reject );
1115 if( p_owner->b_buffering )
1117 vlc_mutex_unlock( &p_owner->lock );
1122 if( p_owner->buffer.p_audio )
1124 p_audio = p_owner->buffer.p_audio;
1126 p_owner->buffer.p_audio = p_audio->p_next;
1127 p_owner->buffer.i_count--;
1129 b_has_more = p_owner->buffer.p_audio != NULL;
1131 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1135 const bool b_dated = p_audio->start_date > VLC_TS_INVALID;
1136 int i_rate = INPUT_RATE_DEFAULT;
1138 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1139 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1141 vlc_mutex_unlock( &p_owner->lock );
1143 if( !p_aout || !p_aout_input ||
1144 p_audio->start_date <= VLC_TS_INVALID ||
1145 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1146 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1149 /* Do not wait against unprotected date */
1150 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1151 while( !b_reject && i_deadline > mdate() )
1153 vlc_mutex_lock( &p_owner->lock );
1154 if( p_owner->b_flushing || p_dec->b_die )
1157 vlc_mutex_unlock( &p_owner->lock );
1160 vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock, i_deadline );
1161 vlc_mutex_unlock( &p_owner->lock );
1166 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1167 *pi_played_sum += 1;
1168 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1173 msg_Warn( p_aout, "received buffer in the future" );
1175 msg_Warn( p_dec, "non-dated audio buffer received" );
1178 aout_BufferFree( p_audio );
1184 vlc_mutex_lock( &p_owner->lock );
1185 if( !p_owner->buffer.p_audio )
1187 vlc_mutex_unlock( &p_owner->lock );
1193 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1195 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1196 input_thread_t *p_input = p_owner->p_input;
1197 aout_buffer_t *p_aout_buf;
1202 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1204 aout_instance_t *p_aout = p_owner->p_aout;
1205 aout_input_t *p_aout_input = p_owner->p_aout_input;
1209 /* It prevent freezing VLC in case of broken decoder */
1210 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1212 block_Release( p_block );
1217 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1218 p_aout_buf->start_date < p_owner->i_preroll_end )
1220 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1224 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1226 msg_Dbg( p_dec, "End of audio preroll" );
1227 if( p_owner->p_aout && p_owner->p_aout_input )
1228 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1230 p_owner->i_preroll_end = VLC_TS_INVALID;
1233 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1236 /* Update ugly stat */
1237 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1239 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1241 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1243 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1245 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1248 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1251 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1253 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1259 assert( p_dec_cc->pf_get_cc != NULL );
1261 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1262 if( !p_owner->cc.b_supported )
1265 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1269 vlc_mutex_lock( &p_owner->lock );
1270 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1272 p_owner->cc.pb_present[i] |= pb_present[i];
1273 if( p_owner->cc.pp_decoder[i] )
1277 for( i = 0; i < 4; i++ )
1279 if( !p_owner->cc.pp_decoder[i] )
1282 if( i_cc_decoder > 1 )
1283 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1285 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1288 vlc_mutex_unlock( &p_owner->lock );
1291 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1292 int *pi_played_sum, int *pi_lost_sum )
1294 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1295 vout_thread_t *p_vout = p_owner->p_vout;
1296 bool b_first_buffered;
1298 if( p_picture->date <= VLC_TS_INVALID )
1300 msg_Warn( p_vout, "non-dated video buffer received" );
1302 vout_DropPicture( p_vout, p_picture );
1307 vlc_mutex_lock( &p_owner->lock );
1309 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1311 p_picture->p_next = NULL;
1313 *p_owner->buffer.pp_picture_next = p_picture;
1314 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1316 p_owner->buffer.i_count++;
1317 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1318 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1320 p_owner->buffer.b_full = true;
1321 vlc_cond_signal( &p_owner->wait_acknowledge );
1324 b_first_buffered = p_owner->buffer.p_picture != NULL;
1326 for( ;; b_first_buffered = false )
1328 bool b_has_more = false;
1332 DecoderWaitUnblock( p_dec, &b_reject );
1334 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1336 vlc_mutex_unlock( &p_owner->lock );
1339 bool b_buffering_first = p_owner->b_buffering;
1342 if( p_owner->buffer.p_picture )
1344 p_picture = p_owner->buffer.p_picture;
1346 p_owner->buffer.p_picture = p_picture->p_next;
1347 p_owner->buffer.i_count--;
1349 b_has_more = p_owner->buffer.p_picture != NULL;
1351 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1355 if( b_buffering_first )
1357 assert( p_owner->buffer.b_first );
1358 assert( !p_owner->buffer.i_count );
1359 msg_Dbg( p_dec, "Received first picture" );
1360 p_owner->buffer.b_first = false;
1361 p_picture->b_force = true;
1364 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1365 int i_rate = INPUT_RATE_DEFAULT;
1366 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1367 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1369 vlc_mutex_unlock( &p_owner->lock );
1372 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1377 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1379 /* Be sure to not display old picture after our own */
1380 vout_Flush( p_vout, p_picture->date );
1381 p_owner->i_last_rate = i_rate;
1383 vout_DisplayPicture( p_vout, p_picture );
1388 msg_Warn( p_vout, "early picture skipped" );
1390 msg_Warn( p_vout, "non-dated video buffer received" );
1393 vout_DropPicture( p_vout, p_picture );
1397 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1399 *pi_played_sum += i_tmp_display;
1400 *pi_lost_sum += i_tmp_lost;
1402 if( !b_has_more || b_buffering_first )
1405 vlc_mutex_lock( &p_owner->lock );
1406 if( !p_owner->buffer.p_picture )
1408 vlc_mutex_unlock( &p_owner->lock );
1414 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1416 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1417 input_thread_t *p_input = p_owner->p_input;
1421 int i_displayed = 0;
1423 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1425 vout_thread_t *p_vout = p_owner->p_vout;
1428 /* It prevent freezing VLC in case of broken decoder */
1429 vout_DropPicture( p_vout, p_pic );
1431 block_Release( p_block );
1437 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1439 vout_DropPicture( p_vout, p_pic );
1443 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1445 msg_Dbg( p_dec, "End of video preroll" );
1447 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1449 p_owner->i_preroll_end = VLC_TS_INVALID;
1452 if( p_dec->pf_get_cc &&
1453 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1454 DecoderGetCc( p_dec, p_dec );
1456 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1458 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1460 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1462 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1464 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1467 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1470 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1474 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1477 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1478 vout_thread_t *p_vout = p_owner->p_spu_vout;
1481 if( p_subpic->i_start <= VLC_TS_INVALID )
1483 msg_Warn( p_dec, "non-dated spu buffer received" );
1484 subpicture_Delete( p_subpic );
1489 vlc_mutex_lock( &p_owner->lock );
1491 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1493 p_subpic->p_next = NULL;
1495 *p_owner->buffer.pp_subpic_next = p_subpic;
1496 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1498 p_owner->buffer.i_count++;
1499 /* XXX it is important to be full after the first one */
1500 if( p_owner->buffer.i_count > 0 )
1502 p_owner->buffer.b_full = true;
1503 vlc_cond_signal( &p_owner->wait_acknowledge );
1509 bool b_has_more = false;
1511 DecoderWaitUnblock( p_dec, &b_reject );
1513 if( p_owner->b_buffering )
1515 vlc_mutex_unlock( &p_owner->lock );
1520 if( p_owner->buffer.p_subpic )
1522 p_subpic = p_owner->buffer.p_subpic;
1524 p_owner->buffer.p_subpic = p_subpic->p_next;
1525 p_owner->buffer.i_count--;
1527 b_has_more = p_owner->buffer.p_subpic != NULL;
1529 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1533 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1534 NULL, INT64_MAX, b_telx );
1536 vlc_mutex_unlock( &p_owner->lock );
1539 spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_subpic );
1541 subpicture_Delete( p_subpic );
1545 vlc_mutex_lock( &p_owner->lock );
1546 if( !p_owner->buffer.p_subpic )
1548 vlc_mutex_unlock( &p_owner->lock );
1554 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1557 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1559 assert( p_owner->p_clock );
1560 assert( !p_sout_block->p_next );
1562 vlc_mutex_lock( &p_owner->lock );
1564 if( p_owner->b_buffering || p_owner->buffer.p_block )
1566 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1568 p_owner->buffer.i_count++;
1569 /* XXX it is important to be full after the first one */
1570 if( p_owner->buffer.i_count > 0 )
1572 p_owner->buffer.b_full = true;
1573 vlc_cond_signal( &p_owner->wait_acknowledge );
1579 bool b_has_more = false;
1581 DecoderWaitUnblock( p_dec, &b_reject );
1583 if( p_owner->b_buffering )
1585 vlc_mutex_unlock( &p_owner->lock );
1590 if( p_owner->buffer.p_block )
1592 p_sout_block = p_owner->buffer.p_block;
1594 p_owner->buffer.p_block = p_sout_block->p_next;
1595 p_owner->buffer.i_count--;
1597 b_has_more = p_owner->buffer.p_block != NULL;
1599 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1601 p_sout_block->p_next = NULL;
1603 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1604 &p_sout_block->i_length,
1605 &p_sout_block->i_rate, INT64_MAX, b_telx );
1607 vlc_mutex_unlock( &p_owner->lock );
1610 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1612 block_Release( p_sout_block );
1616 vlc_mutex_lock( &p_owner->lock );
1617 if( !p_owner->buffer.p_block )
1619 vlc_mutex_unlock( &p_owner->lock );
1626 static void DecoderFlushBuffering( decoder_t *p_dec )
1628 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1630 vlc_assert_locked( &p_owner->lock );
1632 while( p_owner->buffer.p_picture )
1634 picture_t *p_picture = p_owner->buffer.p_picture;
1636 p_owner->buffer.p_picture = p_picture->p_next;
1637 p_owner->buffer.i_count--;
1639 if( p_owner->p_vout )
1640 vout_DropPicture( p_owner->p_vout, p_picture );
1642 if( !p_owner->buffer.p_picture )
1643 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1645 while( p_owner->buffer.p_audio )
1647 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1649 p_owner->buffer.p_audio = p_audio->p_next;
1650 p_owner->buffer.i_count--;
1652 aout_BufferFree( p_audio );
1654 if( !p_owner->buffer.p_audio )
1655 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1657 while( p_owner->buffer.p_subpic )
1659 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1661 p_owner->buffer.p_subpic = p_subpic->p_next;
1662 p_owner->buffer.i_count--;
1664 subpicture_Delete( p_subpic );
1666 if( !p_owner->buffer.p_subpic )
1667 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1669 if( p_owner->buffer.p_block )
1671 block_ChainRelease( p_owner->buffer.p_block );
1673 p_owner->buffer.i_count = 0;
1674 p_owner->buffer.p_block = NULL;
1675 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1679 /* This function process a block for sout
1681 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1683 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1684 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1685 block_t *p_sout_block;
1687 while( ( p_sout_block =
1688 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1690 if( !p_owner->p_sout_input )
1692 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1694 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1695 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1696 if( p_dec->fmt_in.psz_language )
1698 if( p_owner->sout.psz_language )
1699 free( p_owner->sout.psz_language );
1700 p_owner->sout.psz_language =
1701 strdup( p_dec->fmt_in.psz_language );
1704 p_owner->p_sout_input =
1705 sout_InputNew( p_owner->p_sout,
1708 if( p_owner->p_sout_input == NULL )
1710 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1711 (char *)&p_owner->sout.i_codec );
1712 p_dec->b_error = true;
1714 while( p_sout_block )
1716 block_t *p_next = p_sout_block->p_next;
1717 block_Release( p_sout_block );
1718 p_sout_block = p_next;
1724 while( p_sout_block )
1726 block_t *p_next = p_sout_block->p_next;
1728 p_sout_block->p_next = NULL;
1730 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1732 p_sout_block = p_next;
1737 /* This function process a video block
1739 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1741 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1743 if( p_owner->p_packetizer )
1745 block_t *p_packetized_block;
1746 decoder_t *p_packetizer = p_owner->p_packetizer;
1748 while( (p_packetized_block =
1749 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1751 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1753 es_format_Clean( &p_dec->fmt_in );
1754 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1756 if( p_packetizer->pf_get_cc )
1757 DecoderGetCc( p_dec, p_packetizer );
1759 while( p_packetized_block )
1761 block_t *p_next = p_packetized_block->p_next;
1762 p_packetized_block->p_next = NULL;
1764 DecoderDecodeVideo( p_dec, p_packetized_block );
1766 p_packetized_block = p_next;
1769 /* The packetizer does not output a block that tell the decoder to flush
1773 block_t *p_null = DecoderBlockFlushNew();
1775 DecoderDecodeVideo( p_dec, p_null );
1780 DecoderDecodeVideo( p_dec, p_block );
1783 if( b_flush && p_owner->p_vout )
1784 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1787 /* This function process a audio block
1789 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1791 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1793 if( p_owner->p_packetizer )
1795 block_t *p_packetized_block;
1796 decoder_t *p_packetizer = p_owner->p_packetizer;
1798 while( (p_packetized_block =
1799 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1801 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1803 es_format_Clean( &p_dec->fmt_in );
1804 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1807 while( p_packetized_block )
1809 block_t *p_next = p_packetized_block->p_next;
1810 p_packetized_block->p_next = NULL;
1812 DecoderDecodeAudio( p_dec, p_packetized_block );
1814 p_packetized_block = p_next;
1817 /* The packetizer does not output a block that tell the decoder to flush
1821 block_t *p_null = DecoderBlockFlushNew();
1823 DecoderDecodeAudio( p_dec, p_null );
1828 DecoderDecodeAudio( p_dec, p_block );
1831 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1832 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1835 /* This function process a subtitle block
1837 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1839 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1840 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1842 input_thread_t *p_input = p_owner->p_input;
1843 vout_thread_t *p_vout;
1844 subpicture_t *p_spu;
1846 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1848 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1849 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1850 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1852 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1853 if( p_vout && p_owner->p_spu_vout == p_vout )
1855 /* Preroll does not work very well with subtitle */
1856 if( p_spu->i_start > VLC_TS_INVALID &&
1857 p_spu->i_start < p_owner->i_preroll_end &&
1858 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1860 subpicture_Delete( p_spu );
1864 DecoderPlaySpu( p_dec, p_spu, b_telx );
1869 subpicture_Delete( p_spu );
1872 vlc_object_release( p_vout );
1875 if( b_flush && p_owner->p_spu_vout )
1877 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1879 if( p_vout && p_owner->p_spu_vout == p_vout )
1880 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR,
1881 p_owner->i_spu_channel );
1884 vlc_object_release( p_vout );
1889 static void DecoderProcessOnFlush( decoder_t *p_dec )
1891 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1893 vlc_mutex_lock( &p_owner->lock );
1894 DecoderFlushBuffering( p_dec );
1896 if( p_owner->b_flushing )
1898 p_owner->b_flushing = false;
1899 vlc_cond_signal( &p_owner->wait_acknowledge );
1901 vlc_mutex_unlock( &p_owner->lock );
1907 * \param p_dec the decoder object
1908 * \param p_block the block to decode
1909 * \return VLC_SUCCESS or an error code
1911 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1913 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1914 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1916 if( p_block && p_block->i_buffer <= 0 )
1918 assert( !b_flush_request );
1919 block_Release( p_block );
1924 if( p_owner->b_packetizer )
1927 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1929 DecoderProcessSout( p_dec, p_block );
1934 bool b_flush = false;
1938 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1939 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1941 b_flush = !b_flushing && b_flush_request;
1943 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1946 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1948 DecoderProcessAudio( p_dec, p_block, b_flush );
1950 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1952 DecoderProcessVideo( p_dec, p_block, b_flush );
1954 else if( p_dec->fmt_out.i_cat == SPU_ES )
1956 DecoderProcessSpu( p_dec, p_block, b_flush );
1960 msg_Err( p_dec, "unknown ES format" );
1961 p_dec->b_error = true;
1966 if( b_flush_request )
1967 DecoderProcessOnFlush( p_dec );
1970 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1972 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1976 block_Release( p_block );
1978 if( b_flush_request )
1979 DecoderProcessOnFlush( p_dec );
1984 * Destroys a decoder object
1986 * \param p_dec the decoder object
1989 static void DeleteDecoder( decoder_t * p_dec )
1991 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1993 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1994 (char*)&p_dec->fmt_in.i_codec,
1995 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1997 /* Free all packets still in the decoder fifo. */
1998 block_FifoEmpty( p_owner->p_fifo );
1999 block_FifoRelease( p_owner->p_fifo );
2002 vlc_mutex_lock( &p_owner->lock );
2003 DecoderFlushBuffering( p_dec );
2004 vlc_mutex_unlock( &p_owner->lock );
2007 if( p_owner->p_aout_input )
2008 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2009 if( p_owner->p_aout )
2011 input_resource_RequestAout( p_owner->p_input->p->p_resource,
2013 input_SendEventAout( p_owner->p_input );
2014 p_owner->p_aout = NULL;
2016 if( p_owner->p_vout )
2018 /* Hack to make sure all the the pictures are freed by the decoder
2019 * and that the vout is not paused anymore */
2020 vout_FixLeaks( p_owner->p_vout, true );
2021 if( p_owner->b_paused )
2022 vout_ChangePause( p_owner->p_vout, false, mdate() );
2025 input_resource_RequestVout( p_owner->p_input->p->p_resource, p_owner->p_vout, NULL, true );
2026 input_SendEventVout( p_owner->p_input );
2030 if( p_owner->p_sout_input )
2032 sout_InputDelete( p_owner->p_sout_input );
2033 es_format_Clean( &p_owner->sout );
2037 if( p_dec->fmt_out.i_cat == SPU_ES )
2039 vout_thread_t *p_vout;
2041 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2044 if( p_owner->p_spu_vout == p_vout )
2045 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2046 vlc_object_release( p_vout );
2050 es_format_Clean( &p_dec->fmt_in );
2051 es_format_Clean( &p_dec->fmt_out );
2052 if( p_dec->p_description )
2053 vlc_meta_Delete( p_dec->p_description );
2054 es_format_Clean( &p_owner->fmt_description );
2055 if( p_owner->p_description )
2056 vlc_meta_Delete( p_owner->p_description );
2058 if( p_owner->p_packetizer )
2060 module_unneed( p_owner->p_packetizer,
2061 p_owner->p_packetizer->p_module );
2062 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2063 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2064 if( p_owner->p_packetizer->p_description )
2065 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2066 vlc_object_detach( p_owner->p_packetizer );
2067 vlc_object_release( p_owner->p_packetizer );
2070 vlc_cond_destroy( &p_owner->wait_acknowledge );
2071 vlc_cond_destroy( &p_owner->wait_request );
2072 vlc_mutex_destroy( &p_owner->lock );
2074 vlc_object_detach( p_dec );
2079 /*****************************************************************************
2080 * Buffers allocation callbacks for the decoders
2081 *****************************************************************************/
2082 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2084 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2086 vlc_assert_locked( &p_owner->lock );
2088 p_owner->b_fmt_description = true;
2090 /* Copy es_format */
2091 es_format_Clean( &p_owner->fmt_description );
2092 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2094 /* Move p_description */
2095 if( p_owner->p_description && p_dec->p_description )
2096 vlc_meta_Delete( p_owner->p_description );
2097 p_owner->p_description = p_dec->p_description;
2098 p_dec->p_description = NULL;
2100 static vout_thread_t *aout_request_vout( void *p_private,
2101 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2103 decoder_t *p_dec = p_private;
2104 input_thread_t *p_input = p_dec->p_owner->p_input;
2106 p_vout = input_resource_RequestVout( p_input->p->p_resource, p_vout, p_fmt, b_recyle );
2107 input_SendEventVout( p_input );
2112 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2114 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2115 aout_buffer_t *p_buffer;
2117 if( p_owner->p_aout_input != NULL &&
2118 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2119 p_dec->fmt_out.audio.i_original_channels !=
2120 p_owner->audio.i_original_channels ||
2121 p_dec->fmt_out.audio.i_bytes_per_frame !=
2122 p_owner->audio.i_bytes_per_frame ) )
2124 aout_input_t *p_aout_input = p_owner->p_aout_input;
2126 /* Parameters changed, restart the aout */
2127 vlc_mutex_lock( &p_owner->lock );
2129 DecoderFlushBuffering( p_dec );
2131 p_owner->p_aout_input = NULL;
2132 aout_DecDelete( p_owner->p_aout, p_aout_input );
2134 vlc_mutex_unlock( &p_owner->lock );
2137 if( p_owner->p_aout_input == NULL )
2139 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2140 audio_sample_format_t format;
2141 aout_input_t *p_aout_input;
2142 aout_instance_t *p_aout;
2143 aout_request_vout_t request_vout;
2145 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2146 p_owner->audio = p_dec->fmt_out.audio;
2148 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2149 if( i_force_dolby &&
2150 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2151 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2153 if( i_force_dolby == 1 )
2155 format.i_original_channels = format.i_original_channels |
2156 AOUT_CHAN_DOLBYSTEREO;
2158 else /* i_force_dolby == 2 */
2160 format.i_original_channels = format.i_original_channels &
2161 ~AOUT_CHAN_DOLBYSTEREO;
2165 request_vout.pf_request_vout = aout_request_vout;
2166 request_vout.p_private = p_dec;
2168 p_aout = p_owner->p_aout;
2170 p_aout = input_resource_RequestAout( p_owner->p_input->p->p_resource, NULL );
2171 p_aout_input = aout_DecNew( p_dec, &p_aout,
2172 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2174 vlc_mutex_lock( &p_owner->lock );
2176 p_owner->p_aout = p_aout;
2177 p_owner->p_aout_input = p_aout_input;
2178 DecoderUpdateFormatLocked( p_dec );
2180 vlc_mutex_unlock( &p_owner->lock );
2182 input_SendEventAout( p_owner->p_input );
2184 if( p_owner->p_aout_input == NULL )
2186 msg_Err( p_dec, "failed to create audio output" );
2187 p_dec->b_error = true;
2190 p_dec->fmt_out.audio.i_bytes_per_frame =
2191 p_owner->audio.i_bytes_per_frame;
2194 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2199 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2201 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2203 aout_DecDeleteBuffer( p_owner->p_aout,
2204 p_owner->p_aout_input, p_buffer );
2208 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2210 static picture_t *vout_new_buffer( decoder_t *p_dec )
2212 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2214 if( p_owner->p_vout == NULL ||
2215 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2216 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2217 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2218 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2220 vout_thread_t *p_vout;
2222 if( !p_dec->fmt_out.video.i_width ||
2223 !p_dec->fmt_out.video.i_height )
2225 /* Can't create a new vout without display size */
2229 if( !p_dec->fmt_out.video.i_visible_width ||
2230 !p_dec->fmt_out.video.i_visible_height )
2232 if( p_dec->fmt_in.video.i_visible_width &&
2233 p_dec->fmt_in.video.i_visible_height )
2235 p_dec->fmt_out.video.i_visible_width =
2236 p_dec->fmt_in.video.i_visible_width;
2237 p_dec->fmt_out.video.i_visible_height =
2238 p_dec->fmt_in.video.i_visible_height;
2242 p_dec->fmt_out.video.i_visible_width =
2243 p_dec->fmt_out.video.i_width;
2244 p_dec->fmt_out.video.i_visible_height =
2245 p_dec->fmt_out.video.i_height;
2249 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2250 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2252 p_dec->fmt_out.video.i_visible_height = 1080;
2253 p_dec->fmt_out.video.i_sar_num *= 135;
2254 p_dec->fmt_out.video.i_sar_den *= 136;
2255 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2258 if( !p_dec->fmt_out.video.i_sar_num ||
2259 !p_dec->fmt_out.video.i_sar_den )
2261 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2262 p_dec->fmt_out.video.i_visible_height;
2264 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2265 p_dec->fmt_out.video.i_visible_width;
2268 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2269 &p_dec->fmt_out.video.i_sar_den,
2270 p_dec->fmt_out.video.i_sar_num,
2271 p_dec->fmt_out.video.i_sar_den, 50000 );
2273 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2274 p_owner->video = p_dec->fmt_out.video;
2276 vlc_mutex_lock( &p_owner->lock );
2278 DecoderFlushBuffering( p_dec );
2280 p_vout = p_owner->p_vout;
2281 p_owner->p_vout = NULL;
2282 vlc_mutex_unlock( &p_owner->lock );
2284 p_vout = input_resource_RequestVout( p_owner->p_input->p->p_resource,
2285 p_vout, &p_dec->fmt_out.video, true );
2287 vlc_mutex_lock( &p_owner->lock );
2288 p_owner->p_vout = p_vout;
2290 DecoderUpdateFormatLocked( p_dec );
2292 vlc_mutex_unlock( &p_owner->lock );
2294 input_SendEventVout( p_owner->p_input );
2295 if( p_vout == NULL )
2297 msg_Err( p_dec, "failed to create video output" );
2298 p_dec->b_error = true;
2302 if( p_owner->video.i_rmask )
2303 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2304 if( p_owner->video.i_gmask )
2305 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2306 if( p_owner->video.i_bmask )
2307 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2310 /* Get a new picture
2314 picture_t *p_picture;
2316 if( p_dec->b_die || p_dec->b_error )
2319 /* The video filter chain required that there is always 1 free buffer
2320 * that it will use as temporary one. It will release the temporary
2321 * buffer once its work is done, so this check is safe even if we don't
2322 * lock around both count() and create().
2324 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2326 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2331 if( DecoderIsFlushing( p_dec ) )
2335 DecoderSignalBuffering( p_dec, true );
2337 /* Check the decoder doesn't leak pictures */
2338 vout_FixLeaks( p_owner->p_vout, false );
2340 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2341 msleep( VOUT_OUTMEM_SLEEP );
2345 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2347 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2350 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2352 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2355 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2357 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2360 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2362 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2363 vout_thread_t *p_vout = NULL;
2364 subpicture_t *p_subpic;
2365 int i_attempts = 30;
2367 while( i_attempts-- )
2369 if( p_dec->b_die || p_dec->b_error )
2372 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2376 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2381 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2385 if( p_owner->p_spu_vout != p_vout )
2387 vlc_mutex_lock( &p_owner->lock );
2389 DecoderFlushBuffering( p_dec );
2391 vlc_mutex_unlock( &p_owner->lock );
2393 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_REGISTER,
2394 &p_owner->i_spu_channel );
2395 p_owner->i_spu_order = 0;
2396 p_owner->p_spu_vout = p_vout;
2399 p_subpic = subpicture_New();
2402 p_subpic->i_channel = p_owner->i_spu_channel;
2403 p_subpic->i_order = p_owner->i_spu_order++;
2404 p_subpic->b_subtitle = true;
2407 vlc_object_release( p_vout );
2412 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2414 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2415 vout_thread_t *p_vout = NULL;
2417 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2418 if( !p_vout || p_owner->p_spu_vout != p_vout )
2421 vlc_object_release( p_vout );
2422 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2426 subpicture_Delete( p_subpic );
2428 vlc_object_release( p_vout );