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 const bool b_was_paused = p_owner->b_paused;
343 p_owner->b_paused = false;
344 p_owner->b_buffering = false;
345 p_owner->b_flushing = true;
346 vlc_cond_signal( &p_owner->wait );
347 vlc_mutex_unlock( &p_owner->lock );
349 vlc_thread_join( p_dec );
350 p_owner->b_paused = b_was_paused;
352 module_unneed( p_dec, p_dec->p_module );
355 if( p_dec->p_owner->cc.b_supported )
358 for( i = 0; i < 4; i++ )
359 input_DecoderSetCcState( p_dec, false, i );
362 /* Delete decoder configuration */
363 DeleteDecoder( p_dec );
365 /* Delete the decoder */
366 vlc_object_release( p_dec );
370 * Put a block_t in the decoder's fifo.
371 * Thread-safe w.r.t. the decoder. May be a cancellation point.
373 * \param p_dec the decoder object
374 * \param p_block the data block
376 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
378 decoder_owner_sys_t *p_owner = p_dec->p_owner;
380 if( p_owner->p_input->p->b_out_pace_control )
382 /* The fifo is not consummed when buffering and so will
384 * There is no need to lock as b_buffering is never modify
385 * inside decoder thread. */
386 if( !p_owner->b_buffering )
387 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
389 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
391 /* FIXME: ideally we would check the time amount of data
392 * in the FIFO instead of its size. */
393 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
394 "consumed quickly enough), resetting fifo!" );
395 block_FifoEmpty( p_owner->p_fifo );
398 block_FifoPut( p_owner->p_fifo, p_block );
401 bool input_DecoderIsEmpty( decoder_t * p_dec )
403 assert( !p_dec->p_owner->b_buffering );
405 /* FIXME that's not really true */
406 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
409 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
411 decoder_owner_sys_t *p_owner = p_dec->p_owner;
414 vlc_mutex_lock( &p_owner->lock );
415 for( i = 0; i < 4; i++ )
416 pb_present[i] = p_owner->cc.pb_present[i];
417 vlc_mutex_unlock( &p_owner->lock );
419 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
421 decoder_owner_sys_t *p_owner = p_dec->p_owner;
423 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
425 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
430 static const vlc_fourcc_t fcc[4] = {
431 VLC_FOURCC('c', 'c', '1', ' '),
432 VLC_FOURCC('c', 'c', '2', ' '),
433 VLC_FOURCC('c', 'c', '3', ' '),
434 VLC_FOURCC('c', 'c', '4', ' '),
439 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
440 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
443 msg_Err( p_dec, "could not create decoder" );
444 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
445 _("VLC could not open the decoder module.") );
448 else if( !p_cc->p_module )
450 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
451 DeleteDecoder( p_cc );
452 vlc_object_release( p_cc );
455 p_cc->p_owner->p_clock = p_owner->p_clock;
457 vlc_mutex_lock( &p_owner->lock );
458 p_owner->cc.pp_decoder[i_channel] = p_cc;
459 vlc_mutex_unlock( &p_owner->lock );
465 vlc_mutex_lock( &p_owner->lock );
466 p_cc = p_owner->cc.pp_decoder[i_channel];
467 p_owner->cc.pp_decoder[i_channel] = NULL;
468 vlc_mutex_unlock( &p_owner->lock );
472 vlc_object_kill( p_cc );
473 module_unneed( p_cc, p_cc->p_module );
474 DeleteDecoder( p_cc );
475 vlc_object_release( p_cc );
480 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
482 decoder_owner_sys_t *p_owner = p_dec->p_owner;
485 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
488 vlc_mutex_lock( &p_owner->lock );
489 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
490 vlc_mutex_unlock( &p_owner->lock );
494 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
496 decoder_owner_sys_t *p_owner = p_dec->p_owner;
498 vlc_mutex_lock( &p_owner->lock );
500 assert( !p_owner->b_paused || !b_paused );
502 p_owner->b_paused = b_paused;
503 p_owner->pause.i_date = i_date;
504 p_owner->pause.i_ignore = 0;
505 vlc_cond_signal( &p_owner->wait );
507 DecoderOutputChangePause( p_dec, b_paused, i_date );
509 vlc_mutex_unlock( &p_owner->lock );
512 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
514 decoder_owner_sys_t *p_owner = p_dec->p_owner;
516 vlc_mutex_lock( &p_owner->lock );
517 p_owner->i_ts_delay = i_delay;
518 vlc_mutex_unlock( &p_owner->lock );
521 void input_DecoderStartBuffering( decoder_t *p_dec )
523 decoder_owner_sys_t *p_owner = p_dec->p_owner;
525 vlc_mutex_lock( &p_owner->lock );
527 DecoderFlush( p_dec );
529 p_owner->buffer.b_first = true;
530 p_owner->buffer.b_full = false;
531 p_owner->buffer.i_count = 0;
533 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
534 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
536 p_owner->buffer.p_picture = NULL;
537 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
539 p_owner->buffer.p_subpic = NULL;
540 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
542 p_owner->buffer.p_audio = NULL;
543 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
545 p_owner->buffer.p_block = NULL;
546 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
549 p_owner->b_buffering = true;
551 vlc_cond_signal( &p_owner->wait );
553 vlc_mutex_unlock( &p_owner->lock );
556 void input_DecoderStopBuffering( decoder_t *p_dec )
558 decoder_owner_sys_t *p_owner = p_dec->p_owner;
560 vlc_mutex_lock( &p_owner->lock );
562 p_owner->b_buffering = false;
564 vlc_cond_signal( &p_owner->wait );
566 vlc_mutex_unlock( &p_owner->lock );
569 void input_DecoderWaitBuffering( decoder_t *p_dec )
571 decoder_owner_sys_t *p_owner = p_dec->p_owner;
573 vlc_mutex_lock( &p_owner->lock );
575 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
577 block_FifoWake( p_owner->p_fifo );
578 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
581 vlc_mutex_unlock( &p_owner->lock );
584 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
586 decoder_owner_sys_t *p_owner = p_dec->p_owner;
590 vlc_mutex_lock( &p_owner->lock );
591 if( p_dec->fmt_in.i_cat == VIDEO_ES )
593 if( p_owner->b_paused && p_owner->p_vout )
595 vout_NextPicture( p_owner->p_vout, pi_duration );
596 p_owner->pause.i_ignore++;
597 vlc_cond_signal( &p_owner->wait );
602 /* TODO subtitle should not be flushed */
603 DecoderFlush( p_dec );
605 vlc_mutex_unlock( &p_owner->lock );
608 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
610 decoder_owner_sys_t *p_owner = p_dec->p_owner;
613 vlc_mutex_lock( &p_owner->lock );
614 b_changed = p_owner->b_fmt_description;
618 es_format_Copy( p_fmt, &p_owner->fmt_description );
623 if( p_owner->p_description )
625 *pp_meta = vlc_meta_New();
627 vlc_meta_Merge( *pp_meta, p_owner->p_description );
630 p_owner->b_fmt_description = false;
632 vlc_mutex_unlock( &p_owner->lock );
636 /*****************************************************************************
638 *****************************************************************************/
639 static int DecoderGetInputAttachments( decoder_t *p_dec,
640 input_attachment_t ***ppp_attachment,
643 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
644 ppp_attachment, pi_attachment );
646 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
648 decoder_owner_sys_t *p_owner = p_dec->p_owner;
650 vlc_mutex_lock( &p_owner->lock );
651 if( p_owner->b_buffering || p_owner->b_paused )
653 vlc_mutex_unlock( &p_owner->lock );
655 if( !p_owner->p_clock || !i_ts )
658 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
663 static int DecoderGetDisplayRate( decoder_t *p_dec )
665 decoder_owner_sys_t *p_owner = p_dec->p_owner;
667 if( !p_owner->p_clock )
668 return INPUT_RATE_DEFAULT;
669 return input_clock_GetRate( p_owner->p_clock );
673 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
675 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
676 "VLC probably does not support this sound or video format.",
678 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
679 _("VLC does not support the audio or video format \"%4.4s\". "
680 "Unfortunately there is no way for you to fix this."), (char*)&codec );
685 * Create a decoder object
687 * \param p_input the input thread
688 * \param p_es the es descriptor
689 * \param i_object_type Object type as define in include/vlc_objects.h
690 * \return the decoder object
692 static decoder_t * CreateDecoder( input_thread_t *p_input,
693 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
696 decoder_owner_sys_t *p_owner;
699 p_dec = vlc_object_create( p_input, i_object_type );
703 p_dec->pf_decode_audio = NULL;
704 p_dec->pf_decode_video = NULL;
705 p_dec->pf_decode_sub = NULL;
706 p_dec->pf_get_cc = NULL;
707 p_dec->pf_packetize = NULL;
709 /* Initialize the decoder */
710 p_dec->p_module = NULL;
712 memset( &null_es_format, 0, sizeof(es_format_t) );
713 es_format_Copy( &p_dec->fmt_in, fmt );
714 es_format_Copy( &p_dec->fmt_out, &null_es_format );
716 p_dec->p_description = NULL;
718 /* Allocate our private structure for the decoder */
719 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
720 if( p_dec->p_owner == NULL )
722 vlc_object_release( p_dec );
725 p_dec->p_owner->i_preroll_end = -1;
726 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
727 p_dec->p_owner->p_input = p_input;
728 p_dec->p_owner->p_aout = NULL;
729 p_dec->p_owner->p_aout_input = NULL;
730 p_dec->p_owner->p_vout = NULL;
731 p_dec->p_owner->p_spu_vout = NULL;
732 p_dec->p_owner->i_spu_channel = 0;
733 p_dec->p_owner->i_spu_order = 0;
734 p_dec->p_owner->p_sout = p_sout;
735 p_dec->p_owner->p_sout_input = NULL;
736 p_dec->p_owner->p_packetizer = NULL;
739 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
741 free( p_dec->p_owner );
742 vlc_object_release( p_dec );
746 /* Set buffers allocation callbacks for the decoders */
747 p_dec->pf_aout_buffer_new = aout_new_buffer;
748 p_dec->pf_aout_buffer_del = aout_del_buffer;
749 p_dec->pf_vout_buffer_new = vout_new_buffer;
750 p_dec->pf_vout_buffer_del = vout_del_buffer;
751 p_dec->pf_picture_link = vout_link_picture;
752 p_dec->pf_picture_unlink = vout_unlink_picture;
753 p_dec->pf_spu_buffer_new = spu_new_buffer;
754 p_dec->pf_spu_buffer_del = spu_del_buffer;
756 p_dec->pf_get_attachments = DecoderGetInputAttachments;
757 p_dec->pf_get_display_date = DecoderGetDisplayDate;
758 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
760 vlc_object_attach( p_dec, p_input );
762 /* Find a suitable decoder/packetizer module */
763 if( i_object_type == VLC_OBJECT_DECODER )
764 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
766 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
768 /* Check if decoder requires already packetized data */
769 if( i_object_type == VLC_OBJECT_DECODER &&
770 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
772 p_dec->p_owner->p_packetizer =
773 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
774 if( p_dec->p_owner->p_packetizer )
776 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
779 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
782 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
784 p_dec->p_owner->p_packetizer->p_module =
785 module_need( p_dec->p_owner->p_packetizer,
786 "packetizer", "$packetizer", false );
788 if( !p_dec->p_owner->p_packetizer->p_module )
790 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
791 vlc_object_detach( p_dec->p_owner->p_packetizer );
792 vlc_object_release( p_dec->p_owner->p_packetizer );
797 /* Copy ourself the input replay gain */
798 if( fmt->i_cat == AUDIO_ES )
800 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
802 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
804 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
805 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
807 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
809 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
810 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
816 vlc_mutex_init( &p_owner->lock );
817 vlc_cond_init( &p_owner->wait );
819 p_owner->b_fmt_description = false;
820 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
821 p_owner->p_description = NULL;
823 p_owner->b_paused = false;
824 p_owner->pause.i_date = 0;
825 p_owner->pause.i_ignore = 0;
827 p_owner->b_buffering = false;
828 p_owner->buffer.b_first = true;
829 p_owner->buffer.b_full = false;
830 p_owner->buffer.i_count = 0;
831 p_owner->buffer.p_picture = NULL;
832 p_owner->buffer.p_subpic = NULL;
833 p_owner->buffer.p_audio = NULL;
834 p_owner->buffer.p_block = NULL;
836 p_owner->b_flushing = false;
839 p_owner->cc.b_supported = false;
840 if( i_object_type == VLC_OBJECT_DECODER )
842 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
843 p_owner->cc.b_supported = true;
844 if( p_dec->pf_get_cc )
845 p_owner->cc.b_supported = true;
848 for( i = 0; i < 4; i++ )
850 p_owner->cc.pb_present[i] = false;
851 p_owner->cc.pp_decoder[i] = NULL;
853 p_owner->i_ts_delay = 0;
858 * The decoding main loop
860 * \param p_dec the decoder
862 static void *DecoderThread( vlc_object_t *p_this )
864 decoder_t *p_dec = (decoder_t *)p_this;
865 decoder_owner_sys_t *p_owner = p_dec->p_owner;
867 /* The decoder's main loop */
870 block_t *p_block = block_FifoGet( p_owner->p_fifo );
872 /* Make sure there is no cancellation point other than this one^^.
873 * If you need one, be sure to push cleanup of p_block. */
874 DecoderSignalBuffering( p_dec, p_block == NULL );
878 int canc = vlc_savecancel();
881 DecoderError( p_dec, p_block );
883 DecoderProcess( p_dec, p_block );
885 vlc_restorecancel( canc );
891 static void DecoderFlush( decoder_t *p_dec )
893 decoder_owner_sys_t *p_owner = p_dec->p_owner;
896 vlc_assert_locked( &p_owner->lock );
899 block_FifoEmpty( p_owner->p_fifo );
901 /* Monitor for flush end */
902 p_owner->b_flushing = true;
903 vlc_cond_signal( &p_owner->wait );
905 /* Send a special block */
906 p_null = block_New( p_dec, 128 );
909 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
910 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
911 if( !p_dec->fmt_in.b_packetized )
912 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
913 memset( p_null->p_buffer, 0, p_null->i_buffer );
915 input_DecoderDecode( p_dec, p_null );
918 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
919 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
922 static void DecoderSignalFlushed( decoder_t *p_dec )
924 decoder_owner_sys_t *p_owner = p_dec->p_owner;
926 vlc_mutex_lock( &p_owner->lock );
928 if( p_owner->b_flushing )
930 p_owner->b_flushing = false;
931 vlc_cond_signal( &p_owner->wait );
934 vlc_mutex_unlock( &p_owner->lock );
937 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
939 decoder_owner_sys_t *p_owner = p_dec->p_owner;
941 vlc_mutex_lock( &p_owner->lock );
943 if( p_owner->b_buffering )
946 p_owner->buffer.b_full = true;
947 vlc_cond_signal( &p_owner->wait );
950 vlc_mutex_unlock( &p_owner->lock );
953 static bool DecoderIsFlushing( decoder_t *p_dec )
955 decoder_owner_sys_t *p_owner = p_dec->p_owner;
958 vlc_mutex_lock( &p_owner->lock );
960 b_flushing = p_owner->b_flushing;
962 vlc_mutex_unlock( &p_owner->lock );
967 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
969 decoder_owner_sys_t *p_owner = p_dec->p_owner;
971 vlc_assert_locked( &p_owner->lock );
973 while( !p_owner->b_flushing )
975 if( p_owner->b_paused )
977 if( p_owner->b_buffering && !p_owner->buffer.b_full )
979 if( p_owner->pause.i_ignore > 0 )
981 p_owner->pause.i_ignore--;
987 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
990 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
994 *pb_reject = p_owner->b_flushing;
997 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
999 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1001 vlc_assert_locked( &p_owner->lock );
1003 /* XXX only audio and video output have to be paused.
1004 * - for sout it is useless
1005 * - for subs, it is done by the vout
1007 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1009 if( p_owner->p_aout && p_owner->p_aout_input )
1010 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1013 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1015 if( p_owner->p_vout )
1016 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1019 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1021 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1022 *pi_preroll = INT64_MAX;
1023 else if( p->i_pts > 0 )
1024 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1025 else if( p->i_dts > 0 )
1026 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1029 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1031 mtime_t current_date = mdate();
1033 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1034 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1036 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1037 * for teletext streams. */
1038 return current_date + 400000;
1043 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1044 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1046 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1047 input_clock_t *p_clock = p_owner->p_clock;
1049 vlc_assert_locked( &p_owner->lock );
1051 const mtime_t i_es_delay = p_owner->i_ts_delay;
1055 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1060 *pi_ts0 += i_es_delay;
1061 if( pi_ts1 && *pi_ts1 > 0 )
1062 *pi_ts1 += i_es_delay;
1063 input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound );
1067 i_rate = input_clock_GetRate( p_clock );
1070 /* Do not create ephemere data because of rounding errors */
1071 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1075 *pi_duration = ( *pi_duration * i_rate +
1076 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1083 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1084 if( pi_ts1 && *pi_ts1 <= 0 )
1090 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1091 int *pi_played_sum, int *pi_lost_sum )
1093 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1094 aout_instance_t *p_aout = p_owner->p_aout;
1095 aout_input_t *p_aout_input = p_owner->p_aout_input;
1098 if( p_audio->start_date <= 0 )
1100 msg_Warn( p_dec, "non-dated audio buffer received" );
1102 aout_BufferFree( p_audio );
1107 vlc_mutex_lock( &p_owner->lock );
1109 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1111 p_audio->p_next = NULL;
1113 *p_owner->buffer.pp_audio_next = p_audio;
1114 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1116 p_owner->buffer.i_count++;
1117 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1118 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1120 p_owner->buffer.b_full = true;
1121 vlc_cond_signal( &p_owner->wait );
1127 bool b_has_more = false;
1129 DecoderWaitUnblock( p_dec, &b_reject );
1131 if( p_owner->b_buffering )
1133 vlc_mutex_unlock( &p_owner->lock );
1138 if( p_owner->buffer.p_audio )
1140 p_audio = p_owner->buffer.p_audio;
1142 p_owner->buffer.p_audio = p_audio->p_next;
1143 p_owner->buffer.i_count--;
1145 b_has_more = p_owner->buffer.p_audio != NULL;
1147 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1151 const bool b_dated = p_audio->start_date > 0;
1152 int i_rate = INPUT_RATE_DEFAULT;
1154 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1155 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1157 vlc_mutex_unlock( &p_owner->lock );
1159 if( !p_aout || !p_aout_input ||
1160 p_audio->start_date <= 0 ||
1161 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1162 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1165 /* Do not wait against unprotected date */
1166 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1167 while( !b_reject && i_deadline > mdate() )
1169 vlc_mutex_lock( &p_owner->lock );
1170 if( p_owner->b_flushing || p_dec->b_die )
1173 vlc_mutex_unlock( &p_owner->lock );
1176 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1177 vlc_mutex_unlock( &p_owner->lock );
1182 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1183 *pi_played_sum += 1;
1184 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1189 msg_Warn( p_aout, "received buffer in the future" );
1191 msg_Warn( p_dec, "non-dated audio buffer received" );
1194 aout_BufferFree( p_audio );
1200 vlc_mutex_lock( &p_owner->lock );
1201 if( !p_owner->buffer.p_audio )
1203 vlc_mutex_unlock( &p_owner->lock );
1209 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1211 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1212 input_thread_t *p_input = p_owner->p_input;
1213 aout_buffer_t *p_aout_buf;
1218 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1220 aout_instance_t *p_aout = p_owner->p_aout;
1221 aout_input_t *p_aout_input = p_owner->p_aout_input;
1225 /* It prevent freezing VLC in case of broken decoder */
1226 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1228 block_Release( p_block );
1233 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1235 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1239 if( p_owner->i_preroll_end > 0 )
1241 msg_Dbg( p_dec, "End of audio preroll" );
1242 if( p_owner->p_aout && p_owner->p_aout_input )
1243 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1245 p_owner->i_preroll_end = -1;
1248 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1251 /* Update ugly stat */
1252 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1254 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1256 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1258 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1260 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1263 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1266 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1268 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1274 assert( p_dec_cc->pf_get_cc != NULL );
1276 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1277 if( !p_owner->cc.b_supported )
1280 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1284 vlc_mutex_lock( &p_owner->lock );
1285 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1287 p_owner->cc.pb_present[i] |= pb_present[i];
1288 if( p_owner->cc.pp_decoder[i] )
1292 for( i = 0; i < 4; i++ )
1294 if( !p_owner->cc.pp_decoder[i] )
1297 if( i_cc_decoder > 1 )
1298 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1300 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1303 vlc_mutex_unlock( &p_owner->lock );
1306 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1307 int *pi_played_sum, int *pi_lost_sum )
1309 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1310 vout_thread_t *p_vout = p_owner->p_vout;
1311 bool b_first_buffered;
1313 if( p_picture->date <= 0 )
1315 msg_Warn( p_vout, "non-dated video buffer received" );
1317 vout_DropPicture( p_vout, p_picture );
1322 vlc_mutex_lock( &p_owner->lock );
1324 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1326 p_picture->p_next = NULL;
1328 *p_owner->buffer.pp_picture_next = p_picture;
1329 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1331 p_owner->buffer.i_count++;
1332 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1333 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1335 p_owner->buffer.b_full = true;
1336 vlc_cond_signal( &p_owner->wait );
1339 b_first_buffered = p_owner->buffer.p_picture != NULL;
1341 for( ;; b_first_buffered = false )
1343 bool b_has_more = false;
1347 DecoderWaitUnblock( p_dec, &b_reject );
1349 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1351 vlc_mutex_unlock( &p_owner->lock );
1354 bool b_buffering_first = p_owner->b_buffering;
1357 if( p_owner->buffer.p_picture )
1359 p_picture = p_owner->buffer.p_picture;
1361 p_owner->buffer.p_picture = p_picture->p_next;
1362 p_owner->buffer.i_count--;
1364 b_has_more = p_owner->buffer.p_picture != NULL;
1366 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1370 if( b_buffering_first )
1372 assert( p_owner->buffer.b_first );
1373 assert( !p_owner->buffer.i_count );
1374 msg_Dbg( p_dec, "Received first picture" );
1375 p_owner->buffer.b_first = false;
1376 p_picture->b_force = true;
1379 const bool b_dated = p_picture->date > 0;
1380 int i_rate = INPUT_RATE_DEFAULT;
1381 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1382 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1384 vlc_mutex_unlock( &p_owner->lock );
1387 if( !p_picture->b_force && p_picture->date <= 0 )
1392 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1394 /* Be sure to not display old picture after our own */
1395 vout_Flush( p_vout, p_picture->date );
1396 p_owner->i_last_rate = i_rate;
1398 vout_DisplayPicture( p_vout, p_picture );
1403 msg_Warn( p_vout, "early picture skipped" );
1405 msg_Warn( p_vout, "non-dated video buffer received" );
1408 vout_DropPicture( p_vout, p_picture );
1412 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1414 *pi_played_sum += i_tmp_display;
1415 *pi_lost_sum += i_tmp_lost;
1417 if( !b_has_more || b_buffering_first )
1420 vlc_mutex_lock( &p_owner->lock );
1421 if( !p_owner->buffer.p_picture )
1423 vlc_mutex_unlock( &p_owner->lock );
1429 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1431 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1432 input_thread_t *p_input = p_owner->p_input;
1436 int i_displayed = 0;
1438 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1440 vout_thread_t *p_vout = p_owner->p_vout;
1443 /* It prevent freezing VLC in case of broken decoder */
1444 vout_DropPicture( p_vout, p_pic );
1446 block_Release( p_block );
1452 if( p_pic->date < p_owner->i_preroll_end )
1454 vout_DropPicture( p_vout, p_pic );
1458 if( p_owner->i_preroll_end > 0 )
1460 msg_Dbg( p_dec, "End of video preroll" );
1462 vout_Flush( p_vout, 1 );
1464 p_owner->i_preroll_end = -1;
1467 if( p_dec->pf_get_cc &&
1468 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1469 DecoderGetCc( p_dec, p_dec );
1471 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1473 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1475 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1477 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1479 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1482 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1485 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1489 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1492 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1493 vout_thread_t *p_vout = p_owner->p_spu_vout;
1496 if( p_subpic->i_start <= 0 )
1498 msg_Warn( p_dec, "non-dated spu buffer received" );
1499 subpicture_Delete( p_subpic );
1504 vlc_mutex_lock( &p_owner->lock );
1506 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1508 p_subpic->p_next = NULL;
1510 *p_owner->buffer.pp_subpic_next = p_subpic;
1511 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1513 p_owner->buffer.i_count++;
1514 /* XXX it is important to be full after the first one */
1515 if( p_owner->buffer.i_count > 0 )
1517 p_owner->buffer.b_full = true;
1518 vlc_cond_signal( &p_owner->wait );
1524 bool b_has_more = false;
1526 DecoderWaitUnblock( p_dec, &b_reject );
1528 if( p_owner->b_buffering )
1530 vlc_mutex_unlock( &p_owner->lock );
1535 if( p_owner->buffer.p_subpic )
1537 p_subpic = p_owner->buffer.p_subpic;
1539 p_owner->buffer.p_subpic = p_subpic->p_next;
1540 p_owner->buffer.i_count--;
1542 b_has_more = p_owner->buffer.p_subpic != NULL;
1544 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1548 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1549 NULL, INT64_MAX, b_telx );
1551 vlc_mutex_unlock( &p_owner->lock );
1554 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1556 subpicture_Delete( p_subpic );
1560 vlc_mutex_lock( &p_owner->lock );
1561 if( !p_owner->buffer.p_subpic )
1563 vlc_mutex_unlock( &p_owner->lock );
1569 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1572 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1574 assert( p_owner->p_clock );
1575 assert( !p_sout_block->p_next );
1577 vlc_mutex_lock( &p_owner->lock );
1579 if( p_owner->b_buffering || p_owner->buffer.p_block )
1581 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1583 p_owner->buffer.i_count++;
1584 /* XXX it is important to be full after the first one */
1585 if( p_owner->buffer.i_count > 0 )
1587 p_owner->buffer.b_full = true;
1588 vlc_cond_signal( &p_owner->wait );
1594 bool b_has_more = false;
1596 DecoderWaitUnblock( p_dec, &b_reject );
1598 if( p_owner->b_buffering )
1600 vlc_mutex_unlock( &p_owner->lock );
1605 if( p_owner->buffer.p_block )
1607 p_sout_block = p_owner->buffer.p_block;
1609 p_owner->buffer.p_block = p_sout_block->p_next;
1610 p_owner->buffer.i_count--;
1612 b_has_more = p_owner->buffer.p_block != NULL;
1614 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1616 p_sout_block->p_next = NULL;
1618 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1619 &p_sout_block->i_length,
1620 &p_sout_block->i_rate, INT64_MAX, b_telx );
1622 vlc_mutex_unlock( &p_owner->lock );
1625 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1627 block_Release( p_sout_block );
1631 vlc_mutex_lock( &p_owner->lock );
1632 if( !p_owner->buffer.p_block )
1634 vlc_mutex_unlock( &p_owner->lock );
1641 static void DecoderFlushBuffering( decoder_t *p_dec )
1643 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1645 vlc_assert_locked( &p_owner->lock );
1647 while( p_owner->buffer.p_picture )
1649 picture_t *p_picture = p_owner->buffer.p_picture;
1651 p_owner->buffer.p_picture = p_picture->p_next;
1652 p_owner->buffer.i_count--;
1654 if( p_owner->p_vout )
1655 vout_DropPicture( p_owner->p_vout, p_picture );
1657 if( !p_owner->buffer.p_picture )
1658 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1660 while( p_owner->buffer.p_audio )
1662 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1664 p_owner->buffer.p_audio = p_audio->p_next;
1665 p_owner->buffer.i_count--;
1667 aout_BufferFree( p_audio );
1669 if( !p_owner->buffer.p_audio )
1670 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1672 while( p_owner->buffer.p_subpic )
1674 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1676 p_owner->buffer.p_subpic = p_subpic->p_next;
1677 p_owner->buffer.i_count--;
1679 subpicture_Delete( p_subpic );
1681 if( !p_owner->buffer.p_subpic )
1682 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1684 if( p_owner->buffer.p_block )
1686 block_ChainRelease( p_owner->buffer.p_block );
1688 p_owner->buffer.i_count = 0;
1689 p_owner->buffer.p_block = NULL;
1690 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1694 /* This function process a block for sout
1696 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1698 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1699 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1700 block_t *p_sout_block;
1702 while( ( p_sout_block =
1703 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1705 if( !p_owner->p_sout_input )
1707 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1709 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1710 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1711 if( p_dec->fmt_in.psz_language )
1713 if( p_owner->sout.psz_language )
1714 free( p_owner->sout.psz_language );
1715 p_owner->sout.psz_language =
1716 strdup( p_dec->fmt_in.psz_language );
1719 p_owner->p_sout_input =
1720 sout_InputNew( p_owner->p_sout,
1723 if( p_owner->p_sout_input == NULL )
1725 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1726 (char *)&p_owner->sout.i_codec );
1727 p_dec->b_error = true;
1729 while( p_sout_block )
1731 block_t *p_next = p_sout_block->p_next;
1732 block_Release( p_sout_block );
1733 p_sout_block = p_next;
1739 while( p_sout_block )
1741 block_t *p_next = p_sout_block->p_next;
1743 p_sout_block->p_next = NULL;
1745 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1747 p_sout_block = p_next;
1750 /* For now it's enough, as only sout impact on this flag */
1751 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1752 p_owner->p_input->p->b_out_pace_control )
1754 msg_Dbg( p_dec, "switching to sync mode" );
1755 p_owner->p_input->p->b_out_pace_control = false;
1757 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1758 !p_owner->p_input->p->b_out_pace_control )
1760 msg_Dbg( p_dec, "switching to async mode" );
1761 p_owner->p_input->p->b_out_pace_control = true;
1766 /* This function process a video block
1768 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1770 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1772 if( p_owner->p_packetizer )
1774 block_t *p_packetized_block;
1775 decoder_t *p_packetizer = p_owner->p_packetizer;
1777 while( (p_packetized_block =
1778 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1780 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1782 es_format_Clean( &p_dec->fmt_in );
1783 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1785 if( p_packetizer->pf_get_cc )
1786 DecoderGetCc( p_dec, p_packetizer );
1788 while( p_packetized_block )
1790 block_t *p_next = p_packetized_block->p_next;
1791 p_packetized_block->p_next = NULL;
1793 DecoderDecodeVideo( p_dec, p_packetized_block );
1795 p_packetized_block = p_next;
1801 DecoderDecodeVideo( p_dec, p_block );
1804 if( b_flush && p_owner->p_vout )
1805 vout_Flush( p_owner->p_vout, 1 );
1808 /* This function process a audio block
1810 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1812 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1814 if( p_owner->p_packetizer )
1816 block_t *p_packetized_block;
1817 decoder_t *p_packetizer = p_owner->p_packetizer;
1819 while( (p_packetized_block =
1820 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1822 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1824 es_format_Clean( &p_dec->fmt_in );
1825 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1828 while( p_packetized_block )
1830 block_t *p_next = p_packetized_block->p_next;
1831 p_packetized_block->p_next = NULL;
1833 DecoderDecodeAudio( p_dec, p_packetized_block );
1835 p_packetized_block = p_next;
1841 DecoderDecodeAudio( p_dec, p_block );
1844 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1845 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1848 /* This function process a subtitle block
1850 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1852 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1853 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1855 input_thread_t *p_input = p_owner->p_input;
1856 vout_thread_t *p_vout;
1857 subpicture_t *p_spu;
1859 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1861 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1862 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1863 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1865 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1866 if( p_vout && p_owner->p_spu_vout == p_vout )
1868 /* Preroll does not work very well with subtitle */
1869 if( p_spu->i_start > 0 &&
1870 p_spu->i_start < p_owner->i_preroll_end &&
1871 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1873 subpicture_Delete( p_spu );
1877 DecoderPlaySpu( p_dec, p_spu, b_telx );
1882 subpicture_Delete( p_spu );
1885 vlc_object_release( p_vout );
1888 if( b_flush && p_owner->p_spu_vout )
1890 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1892 if( p_vout && p_owner->p_spu_vout == p_vout )
1893 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1894 p_owner->i_spu_channel );
1897 vlc_object_release( p_vout );
1902 static void DecoderProcessOnFlush( decoder_t *p_dec )
1904 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1906 vlc_mutex_lock( &p_owner->lock );
1907 DecoderFlushBuffering( p_dec );
1908 vlc_mutex_unlock( &p_owner->lock );
1910 DecoderSignalFlushed( p_dec );
1915 * \param p_dec the decoder object
1916 * \param p_block the block to decode
1917 * \return VLC_SUCCESS or an error code
1919 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1921 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1922 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1924 if( p_block && p_block->i_buffer <= 0 )
1926 assert( !b_flush_request );
1927 block_Release( p_block );
1932 if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
1935 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1937 DecoderProcessSout( p_dec, p_block );
1942 bool b_flush = false;
1946 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1947 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1949 b_flush = !b_flushing && b_flush_request;
1951 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1954 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1956 DecoderProcessAudio( p_dec, p_block, b_flush );
1958 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1960 DecoderProcessVideo( p_dec, p_block, b_flush );
1962 else if( p_dec->fmt_in.i_cat == SPU_ES )
1964 DecoderProcessSpu( p_dec, p_block, b_flush );
1968 msg_Err( p_dec, "unknown ES format" );
1969 p_dec->b_error = true;
1974 if( b_flush_request )
1975 DecoderProcessOnFlush( p_dec );
1978 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1980 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1984 block_Release( p_block );
1986 if( b_flush_request )
1987 DecoderProcessOnFlush( p_dec );
1992 * Destroys a decoder object
1994 * \param p_dec the decoder object
1997 static void DeleteDecoder( decoder_t * p_dec )
1999 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2001 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2002 (char*)&p_dec->fmt_in.i_codec,
2003 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2005 /* Free all packets still in the decoder fifo. */
2006 block_FifoEmpty( p_owner->p_fifo );
2007 block_FifoRelease( p_owner->p_fifo );
2010 vlc_mutex_lock( &p_owner->lock );
2011 DecoderFlushBuffering( p_dec );
2012 vlc_mutex_unlock( &p_owner->lock );
2015 if( p_owner->p_aout_input )
2016 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2017 if( p_owner->p_aout )
2019 input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2021 input_SendEventAout( p_owner->p_input );
2022 p_owner->p_aout = NULL;
2024 if( p_owner->p_vout )
2026 /* Hack to make sure all the the pictures are freed by the decoder
2027 * and that the vout is not paused anymore */
2028 vout_FixLeaks( p_owner->p_vout, true );
2029 if( p_owner->b_paused )
2030 vout_ChangePause( p_owner->p_vout, false, mdate() );
2033 input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
2034 input_SendEventVout( p_owner->p_input );
2038 if( p_owner->p_sout_input )
2040 sout_InputDelete( p_owner->p_sout_input );
2041 es_format_Clean( &p_owner->sout );
2045 if( p_dec->fmt_in.i_cat == SPU_ES )
2047 vout_thread_t *p_vout;
2049 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2052 if( p_owner->p_spu_vout == p_vout )
2053 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2054 vlc_object_release( p_vout );
2058 es_format_Clean( &p_dec->fmt_in );
2059 es_format_Clean( &p_dec->fmt_out );
2060 if( p_dec->p_description )
2061 vlc_meta_Delete( p_dec->p_description );
2062 es_format_Clean( &p_owner->fmt_description );
2063 if( p_owner->p_description )
2064 vlc_meta_Delete( p_owner->p_description );
2066 if( p_owner->p_packetizer )
2068 module_unneed( p_owner->p_packetizer,
2069 p_owner->p_packetizer->p_module );
2070 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2071 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2072 if( p_owner->p_packetizer->p_description )
2073 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2074 vlc_object_detach( p_owner->p_packetizer );
2075 vlc_object_release( p_owner->p_packetizer );
2078 vlc_cond_destroy( &p_owner->wait );
2079 vlc_mutex_destroy( &p_owner->lock );
2081 vlc_object_detach( p_dec );
2086 /*****************************************************************************
2087 * Buffers allocation callbacks for the decoders
2088 *****************************************************************************/
2089 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2091 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2093 vlc_assert_locked( &p_owner->lock );
2095 p_owner->b_fmt_description = true;
2097 /* Copy es_format */
2098 es_format_Clean( &p_owner->fmt_description );
2099 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2101 /* Move p_description */
2102 if( p_owner->p_description && p_dec->p_description )
2103 vlc_meta_Delete( p_owner->p_description );
2104 p_owner->p_description = p_dec->p_description;
2105 p_dec->p_description = NULL;
2107 static vout_thread_t *aout_request_vout( void *p_private,
2108 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2110 decoder_t *p_dec = p_private;
2111 input_thread_t *p_input = p_dec->p_owner->p_input;
2113 p_vout = input_ressource_RequestVout( p_input->p->p_ressource, p_vout, p_fmt );
2114 /* TODO it would be better to give b_recyle to input_ressource_RequestVout
2115 * as here we are not sure of which vout we destroy */
2117 input_ressource_TerminateVout( p_input->p->p_ressource );
2118 input_SendEventVout( p_input );
2123 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2125 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2126 aout_buffer_t *p_buffer;
2128 if( p_owner->p_aout_input != NULL &&
2129 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2130 p_dec->fmt_out.audio.i_original_channels !=
2131 p_owner->audio.i_original_channels ||
2132 p_dec->fmt_out.audio.i_bytes_per_frame !=
2133 p_owner->audio.i_bytes_per_frame ) )
2135 aout_input_t *p_aout_input = p_owner->p_aout_input;
2137 /* Parameters changed, restart the aout */
2138 vlc_mutex_lock( &p_owner->lock );
2140 DecoderFlushBuffering( p_dec );
2142 p_owner->p_aout_input = NULL;
2143 aout_DecDelete( p_owner->p_aout, p_aout_input );
2145 vlc_mutex_unlock( &p_owner->lock );
2148 if( p_owner->p_aout_input == NULL )
2150 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2151 audio_sample_format_t format;
2152 aout_input_t *p_aout_input;
2153 aout_instance_t *p_aout;
2154 aout_request_vout_t request_vout;
2156 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2157 p_owner->audio = p_dec->fmt_out.audio;
2159 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2160 if( i_force_dolby &&
2161 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2162 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2164 if( i_force_dolby == 1 )
2166 format.i_original_channels = format.i_original_channels |
2167 AOUT_CHAN_DOLBYSTEREO;
2169 else /* i_force_dolby == 2 */
2171 format.i_original_channels = format.i_original_channels &
2172 ~AOUT_CHAN_DOLBYSTEREO;
2176 request_vout.pf_request_vout = aout_request_vout;
2177 request_vout.p_private = p_dec;
2179 p_aout = p_owner->p_aout;
2181 p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2182 p_aout_input = aout_DecNew( p_dec, &p_aout,
2183 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2185 vlc_mutex_lock( &p_owner->lock );
2187 p_owner->p_aout = p_aout;
2188 p_owner->p_aout_input = p_aout_input;
2189 DecoderUpdateFormatLocked( p_dec );
2191 vlc_mutex_unlock( &p_owner->lock );
2193 input_SendEventAout( p_owner->p_input );
2195 if( p_owner->p_aout_input == NULL )
2197 msg_Err( p_dec, "failed to create audio output" );
2198 p_dec->b_error = true;
2201 p_dec->fmt_out.audio.i_bytes_per_frame =
2202 p_owner->audio.i_bytes_per_frame;
2205 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2210 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2212 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2214 aout_DecDeleteBuffer( p_owner->p_aout,
2215 p_owner->p_aout_input, p_buffer );
2219 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2221 static picture_t *vout_new_buffer( decoder_t *p_dec )
2223 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2225 if( p_owner->p_vout == NULL ||
2226 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2227 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2228 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2229 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2231 vout_thread_t *p_vout;
2233 if( !p_dec->fmt_out.video.i_width ||
2234 !p_dec->fmt_out.video.i_height )
2236 /* Can't create a new vout without display size */
2240 if( !p_dec->fmt_out.video.i_visible_width ||
2241 !p_dec->fmt_out.video.i_visible_height )
2243 if( p_dec->fmt_in.video.i_visible_width &&
2244 p_dec->fmt_in.video.i_visible_height )
2246 p_dec->fmt_out.video.i_visible_width =
2247 p_dec->fmt_in.video.i_visible_width;
2248 p_dec->fmt_out.video.i_visible_height =
2249 p_dec->fmt_in.video.i_visible_height;
2253 p_dec->fmt_out.video.i_visible_width =
2254 p_dec->fmt_out.video.i_width;
2255 p_dec->fmt_out.video.i_visible_height =
2256 p_dec->fmt_out.video.i_height;
2260 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2261 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2263 p_dec->fmt_out.video.i_visible_height = 1080;
2264 p_dec->fmt_out.video.i_sar_num *= 135;
2265 p_dec->fmt_out.video.i_sar_den *= 136;
2266 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2269 if( !p_dec->fmt_out.video.i_sar_num ||
2270 !p_dec->fmt_out.video.i_sar_den )
2272 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2273 p_dec->fmt_out.video.i_visible_height;
2275 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2276 p_dec->fmt_out.video.i_visible_width;
2279 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2280 &p_dec->fmt_out.video.i_sar_den,
2281 p_dec->fmt_out.video.i_sar_num,
2282 p_dec->fmt_out.video.i_sar_den, 50000 );
2284 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2285 p_owner->video = p_dec->fmt_out.video;
2287 vlc_mutex_lock( &p_owner->lock );
2289 DecoderFlushBuffering( p_dec );
2291 p_vout = p_owner->p_vout;
2292 p_owner->p_vout = NULL;
2293 vlc_mutex_unlock( &p_owner->lock );
2295 p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
2296 p_vout, &p_dec->fmt_out.video );
2298 vlc_mutex_lock( &p_owner->lock );
2299 p_owner->p_vout = p_vout;
2301 DecoderUpdateFormatLocked( p_dec );
2303 vlc_mutex_unlock( &p_owner->lock );
2305 input_SendEventVout( p_owner->p_input );
2306 if( p_vout == NULL )
2308 msg_Err( p_dec, "failed to create video output" );
2309 p_dec->b_error = true;
2313 if( p_owner->video.i_rmask )
2314 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2315 if( p_owner->video.i_gmask )
2316 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2317 if( p_owner->video.i_bmask )
2318 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2321 /* Get a new picture
2325 picture_t *p_picture;
2327 if( p_dec->b_die || p_dec->b_error )
2330 /* The video filter chain required that there is always 1 free buffer
2331 * that it will use as temporary one. It will release the temporary
2332 * buffer once its work is done, so this check is safe even if we don't
2333 * lock around both count() and create().
2335 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2337 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2342 if( DecoderIsFlushing( p_dec ) )
2346 DecoderSignalBuffering( p_dec, true );
2348 /* Check the decoder doesn't leak pictures */
2349 vout_FixLeaks( p_owner->p_vout, false );
2351 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2352 msleep( VOUT_OUTMEM_SLEEP );
2356 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2358 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2361 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2363 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2366 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2368 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2371 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2373 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2374 vout_thread_t *p_vout = NULL;
2375 subpicture_t *p_subpic;
2376 int i_attempts = 30;
2378 while( i_attempts-- )
2380 if( p_dec->b_die || p_dec->b_error )
2383 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2387 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2392 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2396 if( p_owner->p_spu_vout != p_vout )
2398 vlc_mutex_lock( &p_owner->lock );
2400 DecoderFlushBuffering( p_dec );
2402 vlc_mutex_unlock( &p_owner->lock );
2404 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2405 &p_owner->i_spu_channel );
2406 p_owner->i_spu_order = 0;
2407 p_owner->p_spu_vout = p_vout;
2410 p_subpic = subpicture_New();
2413 p_subpic->i_channel = p_owner->i_spu_channel;
2414 p_subpic->i_order = p_owner->i_spu_order++;
2415 p_subpic->b_subtitle = true;
2418 vlc_object_release( p_vout );
2423 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2425 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2426 vout_thread_t *p_vout = NULL;
2428 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2429 if( !p_vout || p_owner->p_spu_vout != p_vout )
2432 vlc_object_release( p_vout );
2433 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2437 subpicture_Delete( p_subpic );
2439 vlc_object_release( p_vout );