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"
52 #include "../video_output/vout_control.h"
54 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
55 static void DeleteDecoder( decoder_t * );
57 static void *DecoderThread( vlc_object_t * );
58 static void DecoderProcess( decoder_t *, block_t * );
59 static void DecoderError( decoder_t *p_dec, block_t *p_block );
60 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
61 static void DecoderFlush( decoder_t * );
62 static void DecoderSignalBuffering( decoder_t *, bool );
63 static void DecoderFlushBuffering( decoder_t * );
65 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
67 /* Buffers allocation callbacks for the decoders */
68 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
69 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
71 static picture_t *vout_new_buffer( decoder_t * );
72 static void vout_del_buffer( decoder_t *, picture_t * );
73 static void vout_link_picture( decoder_t *, picture_t * );
74 static void vout_unlink_picture( decoder_t *, picture_t * );
76 static subpicture_t *spu_new_buffer( decoder_t * );
77 static void spu_del_buffer( decoder_t *, subpicture_t * );
79 static es_format_t null_es_format;
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;
99 /* Current format in use by the output */
100 video_format_t video;
101 audio_format_t audio;
105 bool b_fmt_description;
106 es_format_t fmt_description;
107 vlc_meta_t *p_description;
110 block_fifo_t *p_fifo;
112 /* Lock for communication with decoder thread */
116 /* -- These variables need locking on write(only) -- */
117 aout_instance_t *p_aout;
118 aout_input_t *p_aout_input;
120 vout_thread_t *p_vout;
122 /* -- Theses variables need locking on read *and* write -- */
140 picture_t *p_picture;
141 picture_t **pp_picture_next;
143 subpicture_t *p_subpic;
144 subpicture_t **pp_subpic_next;
146 aout_buffer_t *p_audio;
147 aout_buffer_t **pp_audio_next;
150 block_t **pp_block_next;
161 decoder_t *pp_decoder[4];
168 #define DECODER_MAX_BUFFERING_COUNT (4)
169 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
170 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
172 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
173 * a bogus PTS and won't be displayed */
174 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
177 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
180 /*****************************************************************************
182 *****************************************************************************/
183 picture_t *decoder_NewPicture( decoder_t *p_decoder )
185 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
187 msg_Warn( p_decoder, "can't get output picture" );
190 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
192 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
194 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
196 p_decoder->pf_picture_link( p_decoder, p_picture );
198 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
200 p_decoder->pf_picture_unlink( p_decoder, p_picture );
203 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
205 if( !p_decoder->pf_aout_buffer_new )
207 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
209 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
211 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
214 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
216 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
218 msg_Warn( p_decoder, "can't get output subpicture" );
221 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
223 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
226 /* decoder_GetInputAttachments:
228 int decoder_GetInputAttachments( decoder_t *p_dec,
229 input_attachment_t ***ppp_attachment,
232 if( !p_dec->pf_get_attachments )
235 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
237 /* decoder_GetDisplayDate:
239 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
241 if( !p_dec->pf_get_display_date )
244 return p_dec->pf_get_display_date( p_dec, i_ts );
246 /* decoder_GetDisplayRate:
248 int decoder_GetDisplayRate( decoder_t *p_dec )
250 if( !p_dec->pf_get_display_rate )
251 return INPUT_RATE_DEFAULT;
253 return p_dec->pf_get_display_rate( p_dec );
257 * Spawns a new decoder thread
259 * \param p_input the input thread
260 * \param p_es the es descriptor
261 * \return the spawned decoder object
263 decoder_t *input_DecoderNew( input_thread_t *p_input,
264 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
266 decoder_t *p_dec = NULL;
270 /* If we are in sout mode, search for packetizer module */
273 /* Create the decoder configuration structure */
274 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
277 msg_Err( p_input, "could not create packetizer" );
278 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
279 _("VLC could not open the packetizer module.") );
286 /* Create the decoder configuration structure */
287 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
290 msg_Err( p_input, "could not create decoder" );
291 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
292 _("VLC could not open the decoder module.") );
297 if( !p_dec->p_module )
299 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
301 DeleteDecoder( p_dec );
302 vlc_object_release( p_dec );
306 p_dec->p_owner->p_clock = p_clock;
308 if( fmt->i_cat == AUDIO_ES )
309 i_priority = VLC_THREAD_PRIORITY_AUDIO;
311 i_priority = VLC_THREAD_PRIORITY_VIDEO;
313 /* Spawn the decoder thread */
314 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
315 i_priority, false ) )
317 msg_Err( p_dec, "cannot spawn decoder thread" );
318 module_unneed( p_dec, p_dec->p_module );
319 DeleteDecoder( p_dec );
320 vlc_object_release( p_dec );
328 * Kills a decoder thread and waits until it's finished
330 * \param p_input the input thread
331 * \param p_es the es descriptor
334 void input_DecoderDelete( decoder_t *p_dec )
336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
338 vlc_object_kill( p_dec );
340 /* Make sure we aren't paused/buffering/waiting anymore */
341 vlc_mutex_lock( &p_owner->lock );
342 p_owner->b_paused = false;
343 p_owner->b_buffering = false;
344 p_owner->b_flushing = true;
345 vlc_cond_signal( &p_owner->wait );
346 vlc_mutex_unlock( &p_owner->lock );
348 vlc_thread_join( p_dec );
349 module_unneed( p_dec, p_dec->p_module );
352 if( p_dec->p_owner->cc.b_supported )
355 for( i = 0; i < 4; i++ )
356 input_DecoderSetCcState( p_dec, false, i );
359 /* Delete decoder configuration */
360 DeleteDecoder( p_dec );
362 /* Delete the decoder */
363 vlc_object_release( p_dec );
367 * Put a block_t in the decoder's fifo.
368 * Thread-safe w.r.t. the decoder. May be a cancellation point.
370 * \param p_dec the decoder object
371 * \param p_block the data block
373 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
375 decoder_owner_sys_t *p_owner = p_dec->p_owner;
377 if( p_owner->p_input->p->b_out_pace_control )
379 /* The fifo is not consummed when buffering and so will
381 * There is no need to lock as b_buffering is never modify
382 * inside decoder thread. */
383 if( !p_owner->b_buffering )
384 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
386 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
388 /* FIXME: ideally we would check the time amount of data
389 * in the FIFO instead of its size. */
390 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
391 "consumed quickly enough), resetting fifo!" );
392 block_FifoEmpty( p_owner->p_fifo );
395 block_FifoPut( p_owner->p_fifo, p_block );
398 bool input_DecoderIsEmpty( decoder_t * p_dec )
400 assert( !p_dec->p_owner->b_buffering );
402 /* FIXME that's not really true */
403 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
406 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
408 decoder_owner_sys_t *p_owner = p_dec->p_owner;
411 vlc_mutex_lock( &p_owner->lock );
412 for( i = 0; i < 4; i++ )
413 pb_present[i] = p_owner->cc.pb_present[i];
414 vlc_mutex_unlock( &p_owner->lock );
416 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
418 decoder_owner_sys_t *p_owner = p_dec->p_owner;
420 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
422 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
427 static const vlc_fourcc_t fcc[4] = {
428 VLC_FOURCC('c', 'c', '1', ' '),
429 VLC_FOURCC('c', 'c', '2', ' '),
430 VLC_FOURCC('c', 'c', '3', ' '),
431 VLC_FOURCC('c', 'c', '4', ' '),
436 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
437 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
440 msg_Err( p_dec, "could not create decoder" );
441 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
442 _("VLC could not open the decoder module.") );
445 else if( !p_cc->p_module )
447 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
448 DeleteDecoder( p_cc );
449 vlc_object_release( p_cc );
452 p_cc->p_owner->p_clock = p_owner->p_clock;
454 vlc_mutex_lock( &p_owner->lock );
455 p_owner->cc.pp_decoder[i_channel] = p_cc;
456 vlc_mutex_unlock( &p_owner->lock );
462 vlc_mutex_lock( &p_owner->lock );
463 p_cc = p_owner->cc.pp_decoder[i_channel];
464 p_owner->cc.pp_decoder[i_channel] = NULL;
465 vlc_mutex_unlock( &p_owner->lock );
469 vlc_object_kill( p_cc );
470 module_unneed( p_cc, p_cc->p_module );
471 DeleteDecoder( p_cc );
472 vlc_object_release( p_cc );
477 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
479 decoder_owner_sys_t *p_owner = p_dec->p_owner;
482 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
485 vlc_mutex_lock( &p_owner->lock );
486 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
487 vlc_mutex_unlock( &p_owner->lock );
491 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
493 decoder_owner_sys_t *p_owner = p_dec->p_owner;
495 vlc_mutex_lock( &p_owner->lock );
497 assert( !p_owner->b_paused || !b_paused );
499 p_owner->b_paused = b_paused;
500 p_owner->pause.i_date = i_date;
501 p_owner->pause.i_ignore = 0;
502 vlc_cond_signal( &p_owner->wait );
504 DecoderOutputChangePause( p_dec, b_paused, i_date );
506 vlc_mutex_unlock( &p_owner->lock );
509 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
511 decoder_owner_sys_t *p_owner = p_dec->p_owner;
513 vlc_mutex_lock( &p_owner->lock );
514 p_owner->i_ts_delay = i_delay;
515 vlc_mutex_unlock( &p_owner->lock );
518 void input_DecoderStartBuffering( decoder_t *p_dec )
520 decoder_owner_sys_t *p_owner = p_dec->p_owner;
522 vlc_mutex_lock( &p_owner->lock );
524 DecoderFlush( p_dec );
526 p_owner->buffer.b_first = true;
527 p_owner->buffer.b_full = false;
528 p_owner->buffer.i_count = 0;
530 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
531 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
533 p_owner->buffer.p_picture = NULL;
534 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
536 p_owner->buffer.p_subpic = NULL;
537 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
539 p_owner->buffer.p_audio = NULL;
540 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
542 p_owner->buffer.p_block = NULL;
543 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
546 p_owner->b_buffering = true;
548 vlc_cond_signal( &p_owner->wait );
550 vlc_mutex_unlock( &p_owner->lock );
553 void input_DecoderStopBuffering( decoder_t *p_dec )
555 decoder_owner_sys_t *p_owner = p_dec->p_owner;
557 vlc_mutex_lock( &p_owner->lock );
559 p_owner->b_buffering = false;
561 vlc_cond_signal( &p_owner->wait );
563 vlc_mutex_unlock( &p_owner->lock );
566 void input_DecoderWaitBuffering( decoder_t *p_dec )
568 decoder_owner_sys_t *p_owner = p_dec->p_owner;
570 vlc_mutex_lock( &p_owner->lock );
572 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
574 block_FifoWake( p_owner->p_fifo );
575 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
578 vlc_mutex_unlock( &p_owner->lock );
581 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
583 decoder_owner_sys_t *p_owner = p_dec->p_owner;
587 vlc_mutex_lock( &p_owner->lock );
588 if( p_dec->fmt_in.i_cat == VIDEO_ES )
590 if( p_owner->b_paused && p_owner->p_vout )
592 vout_NextPicture( p_owner->p_vout, pi_duration );
593 p_owner->pause.i_ignore++;
594 vlc_cond_signal( &p_owner->wait );
599 /* TODO subtitle should not be flushed */
600 DecoderFlush( p_dec );
602 vlc_mutex_unlock( &p_owner->lock );
605 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
607 decoder_owner_sys_t *p_owner = p_dec->p_owner;
610 vlc_mutex_lock( &p_owner->lock );
611 b_changed = p_owner->b_fmt_description;
615 es_format_Copy( p_fmt, &p_owner->fmt_description );
620 if( p_owner->p_description )
622 *pp_meta = vlc_meta_New();
624 vlc_meta_Merge( *pp_meta, p_owner->p_description );
627 p_owner->b_fmt_description = false;
629 vlc_mutex_unlock( &p_owner->lock );
633 /*****************************************************************************
635 *****************************************************************************/
636 static int DecoderGetInputAttachments( decoder_t *p_dec,
637 input_attachment_t ***ppp_attachment,
640 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
641 ppp_attachment, pi_attachment );
643 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
645 decoder_owner_sys_t *p_owner = p_dec->p_owner;
647 vlc_mutex_lock( &p_owner->lock );
648 if( p_owner->b_buffering || p_owner->b_paused )
650 vlc_mutex_unlock( &p_owner->lock );
652 if( !p_owner->p_clock || !i_ts )
655 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
657 static int DecoderGetDisplayRate( decoder_t *p_dec )
659 decoder_owner_sys_t *p_owner = p_dec->p_owner;
661 if( !p_owner->p_clock )
662 return INPUT_RATE_DEFAULT;
663 return input_clock_GetRate( p_owner->p_clock );
667 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
669 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
670 "VLC probably does not support this sound or video format.",
672 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
673 _("VLC does not support the audio or video format \"%4.4s\". "
674 "Unfortunately there is no way for you to fix this."), (char*)&codec );
679 * Create a decoder object
681 * \param p_input the input thread
682 * \param p_es the es descriptor
683 * \param i_object_type Object type as define in include/vlc_objects.h
684 * \return the decoder object
686 static decoder_t * CreateDecoder( input_thread_t *p_input,
687 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
690 decoder_owner_sys_t *p_owner;
693 p_dec = vlc_object_create( p_input, i_object_type );
697 p_dec->pf_decode_audio = NULL;
698 p_dec->pf_decode_video = NULL;
699 p_dec->pf_decode_sub = NULL;
700 p_dec->pf_get_cc = NULL;
701 p_dec->pf_packetize = NULL;
703 /* Initialize the decoder */
704 p_dec->p_module = NULL;
706 memset( &null_es_format, 0, sizeof(es_format_t) );
707 es_format_Copy( &p_dec->fmt_in, fmt );
708 es_format_Copy( &p_dec->fmt_out, &null_es_format );
710 p_dec->p_description = NULL;
712 /* Allocate our private structure for the decoder */
713 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
714 if( p_dec->p_owner == NULL )
716 vlc_object_release( p_dec );
719 p_dec->p_owner->i_preroll_end = -1;
720 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
721 p_dec->p_owner->p_input = p_input;
722 p_dec->p_owner->p_aout = NULL;
723 p_dec->p_owner->p_aout_input = NULL;
724 p_dec->p_owner->p_vout = NULL;
725 p_dec->p_owner->p_spu_vout = NULL;
726 p_dec->p_owner->i_spu_channel = 0;
727 p_dec->p_owner->i_spu_order = 0;
728 p_dec->p_owner->p_sout = p_sout;
729 p_dec->p_owner->p_sout_input = NULL;
730 p_dec->p_owner->p_packetizer = NULL;
733 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
735 free( p_dec->p_owner );
736 vlc_object_release( p_dec );
740 /* Set buffers allocation callbacks for the decoders */
741 p_dec->pf_aout_buffer_new = aout_new_buffer;
742 p_dec->pf_aout_buffer_del = aout_del_buffer;
743 p_dec->pf_vout_buffer_new = vout_new_buffer;
744 p_dec->pf_vout_buffer_del = vout_del_buffer;
745 p_dec->pf_picture_link = vout_link_picture;
746 p_dec->pf_picture_unlink = vout_unlink_picture;
747 p_dec->pf_spu_buffer_new = spu_new_buffer;
748 p_dec->pf_spu_buffer_del = spu_del_buffer;
750 p_dec->pf_get_attachments = DecoderGetInputAttachments;
751 p_dec->pf_get_display_date = DecoderGetDisplayDate;
752 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
754 vlc_object_attach( p_dec, p_input );
756 /* Find a suitable decoder/packetizer module */
757 if( i_object_type == VLC_OBJECT_DECODER )
758 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
760 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
762 /* Check if decoder requires already packetized data */
763 if( i_object_type == VLC_OBJECT_DECODER &&
764 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
766 p_dec->p_owner->p_packetizer =
767 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
768 if( p_dec->p_owner->p_packetizer )
770 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
773 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
776 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
778 p_dec->p_owner->p_packetizer->p_module =
779 module_need( p_dec->p_owner->p_packetizer,
780 "packetizer", "$packetizer", false );
782 if( !p_dec->p_owner->p_packetizer->p_module )
784 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
785 vlc_object_detach( p_dec->p_owner->p_packetizer );
786 vlc_object_release( p_dec->p_owner->p_packetizer );
791 /* Copy ourself the input replay gain */
792 if( fmt->i_cat == AUDIO_ES )
794 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
796 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
798 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
799 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
801 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
803 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
804 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
810 vlc_mutex_init( &p_owner->lock );
811 vlc_cond_init( &p_owner->wait );
813 p_owner->b_fmt_description = false;
814 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
815 p_owner->p_description = NULL;
817 p_owner->b_paused = false;
818 p_owner->pause.i_date = 0;
819 p_owner->pause.i_ignore = 0;
821 p_owner->b_buffering = false;
822 p_owner->buffer.b_first = true;
823 p_owner->buffer.b_full = false;
824 p_owner->buffer.i_count = 0;
825 p_owner->buffer.p_picture = NULL;
826 p_owner->buffer.p_subpic = NULL;
827 p_owner->buffer.p_audio = NULL;
828 p_owner->buffer.p_block = NULL;
830 p_owner->b_flushing = false;
833 p_owner->cc.b_supported = false;
834 if( i_object_type == VLC_OBJECT_DECODER )
836 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
837 p_owner->cc.b_supported = true;
838 if( p_dec->pf_get_cc )
839 p_owner->cc.b_supported = true;
842 for( i = 0; i < 4; i++ )
844 p_owner->cc.pb_present[i] = false;
845 p_owner->cc.pp_decoder[i] = NULL;
847 p_owner->i_ts_delay = 0;
852 * The decoding main loop
854 * \param p_dec the decoder
856 static void *DecoderThread( vlc_object_t *p_this )
858 decoder_t *p_dec = (decoder_t *)p_this;
859 decoder_owner_sys_t *p_owner = p_dec->p_owner;
861 /* The decoder's main loop */
864 block_t *p_block = block_FifoGet( p_owner->p_fifo );
865 /* Make sure there is no cancellation point other than this one^^.
866 * If you need one, be sure to push cleanup of p_block. */
867 DecoderSignalBuffering( p_dec, p_block == NULL );
871 int canc = vlc_savecancel();
874 DecoderError( p_dec, p_block );
876 DecoderProcess( p_dec, p_block );
878 vlc_restorecancel( canc );
882 DecoderSignalBuffering( p_dec, true );
886 static void DecoderFlush( decoder_t *p_dec )
888 decoder_owner_sys_t *p_owner = p_dec->p_owner;
891 vlc_assert_locked( &p_owner->lock );
894 block_FifoEmpty( p_owner->p_fifo );
896 /* Monitor for flush end */
897 p_owner->b_flushing = true;
898 vlc_cond_signal( &p_owner->wait );
900 /* Send a special block */
901 p_null = block_New( p_dec, 128 );
904 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
905 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
906 if( !p_dec->fmt_in.b_packetized )
907 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
908 memset( p_null->p_buffer, 0, p_null->i_buffer );
910 input_DecoderDecode( p_dec, p_null );
913 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
914 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
917 static void DecoderSignalFlushed( decoder_t *p_dec )
919 decoder_owner_sys_t *p_owner = p_dec->p_owner;
921 vlc_mutex_lock( &p_owner->lock );
923 if( p_owner->b_flushing )
925 p_owner->b_flushing = false;
926 vlc_cond_signal( &p_owner->wait );
929 vlc_mutex_unlock( &p_owner->lock );
932 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
934 decoder_owner_sys_t *p_owner = p_dec->p_owner;
936 vlc_mutex_lock( &p_owner->lock );
938 if( p_owner->b_buffering )
941 p_owner->buffer.b_full = true;
942 vlc_cond_signal( &p_owner->wait );
945 vlc_mutex_unlock( &p_owner->lock );
948 static bool DecoderIsFlushing( decoder_t *p_dec )
950 decoder_owner_sys_t *p_owner = p_dec->p_owner;
953 vlc_mutex_lock( &p_owner->lock );
955 b_flushing = p_owner->b_flushing;
957 vlc_mutex_unlock( &p_owner->lock );
962 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
964 decoder_owner_sys_t *p_owner = p_dec->p_owner;
966 vlc_assert_locked( &p_owner->lock );
968 while( !p_owner->b_flushing )
970 if( p_owner->b_paused )
972 if( p_owner->b_buffering && !p_owner->buffer.b_full )
974 if( p_owner->pause.i_ignore > 0 )
976 p_owner->pause.i_ignore--;
982 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
985 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
989 *pb_reject = p_owner->b_flushing;
992 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
994 decoder_owner_sys_t *p_owner = p_dec->p_owner;
996 vlc_assert_locked( &p_owner->lock );
998 /* XXX only audio and video output have to be paused.
999 * - for sout it is useless
1000 * - for subs, it is done by the vout
1002 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1004 if( p_owner->p_aout && p_owner->p_aout_input )
1005 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1008 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1010 if( p_owner->p_vout )
1011 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1014 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1016 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1017 *pi_preroll = INT64_MAX;
1018 else if( p->i_pts > 0 )
1019 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1020 else if( p->i_dts > 0 )
1021 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1024 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
1026 mtime_t current_date = mdate();
1028 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1029 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1031 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1032 * for teletext streams. */
1033 return current_date + 400000 + i_ts_delay;
1038 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1039 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1041 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1042 input_clock_t *p_clock = p_owner->p_clock;
1045 vlc_assert_locked( &p_owner->lock );
1047 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
1048 const mtime_t i_es_delay = p_owner->i_ts_delay;
1052 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1055 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1056 *pi_ts0 + i_es_delay );
1057 if( pi_ts1 && *pi_ts1 > 0 )
1058 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1059 *pi_ts1 + i_es_delay );
1061 /* Do not create ephemere data because of rounding errors */
1062 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1066 i_rate = input_clock_GetRate( p_clock );
1069 *pi_duration = ( *pi_duration * i_rate +
1070 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1077 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1078 if( pi_ts1 && *pi_ts1 <= 0 )
1084 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1085 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1089 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1090 int *pi_played_sum, int *pi_lost_sum )
1092 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1093 aout_instance_t *p_aout = p_owner->p_aout;
1094 aout_input_t *p_aout_input = p_owner->p_aout_input;
1097 if( p_audio->start_date <= 0 )
1099 msg_Warn( p_dec, "non-dated audio buffer received" );
1101 aout_BufferFree( p_audio );
1106 vlc_mutex_lock( &p_owner->lock );
1108 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1110 p_audio->p_next = NULL;
1112 *p_owner->buffer.pp_audio_next = p_audio;
1113 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1115 p_owner->buffer.i_count++;
1116 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1117 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1119 p_owner->buffer.b_full = true;
1120 vlc_cond_signal( &p_owner->wait );
1126 bool b_has_more = false;
1128 DecoderWaitUnblock( p_dec, &b_reject );
1130 if( p_owner->b_buffering )
1132 vlc_mutex_unlock( &p_owner->lock );
1137 if( p_owner->buffer.p_audio )
1139 p_audio = p_owner->buffer.p_audio;
1141 p_owner->buffer.p_audio = p_audio->p_next;
1142 p_owner->buffer.i_count--;
1144 b_has_more = p_owner->buffer.p_audio != NULL;
1146 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1150 int i_rate = INPUT_RATE_DEFAULT;
1153 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1154 &i_rate, &i_delay, false );
1156 vlc_mutex_unlock( &p_owner->lock );
1159 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1161 if( !p_aout || !p_aout_input ||
1162 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1163 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1164 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1167 /* Do not wait against unprotected date */
1168 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1169 while( !b_reject && i_deadline > mdate() )
1171 vlc_mutex_lock( &p_owner->lock );
1172 if( p_owner->b_flushing || p_dec->b_die )
1175 vlc_mutex_unlock( &p_owner->lock );
1178 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1179 vlc_mutex_unlock( &p_owner->lock );
1184 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1185 *pi_played_sum += 1;
1186 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1190 if( p_audio->start_date <= 0 )
1192 msg_Warn( p_dec, "non-dated audio buffer received" );
1194 else if( p_audio->start_date > i_max_date )
1196 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1197 p_audio->start_date - mdate() );
1200 aout_BufferFree( p_audio );
1206 vlc_mutex_lock( &p_owner->lock );
1207 if( !p_owner->buffer.p_audio )
1209 vlc_mutex_unlock( &p_owner->lock );
1215 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1217 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1218 input_thread_t *p_input = p_owner->p_input;
1219 aout_buffer_t *p_aout_buf;
1224 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1226 aout_instance_t *p_aout = p_owner->p_aout;
1227 aout_input_t *p_aout_input = p_owner->p_aout_input;
1233 /* It prevent freezing VLC in case of broken decoder */
1234 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1236 block_Release( p_block );
1241 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1243 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1247 if( p_owner->i_preroll_end > 0 )
1249 msg_Dbg( p_dec, "End of audio preroll" );
1250 if( p_owner->p_aout && p_owner->p_aout_input )
1251 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1253 p_owner->i_preroll_end = -1;
1256 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1259 /* Update ugly stat */
1260 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1262 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1264 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1266 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1268 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1271 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1274 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1276 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1282 assert( p_dec_cc->pf_get_cc != NULL );
1284 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1285 if( !p_owner->cc.b_supported )
1288 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1292 vlc_mutex_lock( &p_owner->lock );
1293 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1295 p_owner->cc.pb_present[i] |= pb_present[i];
1296 if( p_owner->cc.pp_decoder[i] )
1300 for( i = 0; i < 4; i++ )
1302 if( !p_owner->cc.pp_decoder[i] )
1305 if( i_cc_decoder > 1 )
1306 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1308 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1311 vlc_mutex_unlock( &p_owner->lock );
1314 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1315 int *pi_played_sum, int *pi_lost_sum )
1317 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1318 vout_thread_t *p_vout = p_owner->p_vout;
1319 bool b_first_buffered;
1321 if( p_picture->date <= 0 )
1323 msg_Warn( p_vout, "non-dated video buffer received" );
1325 vout_DropPicture( p_vout, p_picture );
1330 vlc_mutex_lock( &p_owner->lock );
1332 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1334 p_picture->p_next = NULL;
1336 *p_owner->buffer.pp_picture_next = p_picture;
1337 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1339 p_owner->buffer.i_count++;
1340 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1341 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1343 p_owner->buffer.b_full = true;
1344 vlc_cond_signal( &p_owner->wait );
1347 b_first_buffered = p_owner->buffer.p_picture != NULL;
1349 for( ;; b_first_buffered = false )
1351 bool b_has_more = false;
1355 DecoderWaitUnblock( p_dec, &b_reject );
1357 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1359 vlc_mutex_unlock( &p_owner->lock );
1362 bool b_buffering_first = p_owner->b_buffering;
1365 if( p_owner->buffer.p_picture )
1367 p_picture = p_owner->buffer.p_picture;
1369 p_owner->buffer.p_picture = p_picture->p_next;
1370 p_owner->buffer.i_count--;
1372 b_has_more = p_owner->buffer.p_picture != NULL;
1374 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1378 if( b_buffering_first )
1380 assert( p_owner->buffer.b_first );
1381 assert( !p_owner->buffer.i_count );
1382 msg_Dbg( p_dec, "Received first picture" );
1383 p_owner->buffer.b_first = false;
1384 p_picture->b_force = true;
1387 int i_rate = INPUT_RATE_DEFAULT;
1389 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1390 &i_rate, &i_delay, false );
1392 vlc_mutex_unlock( &p_owner->lock );
1395 const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1397 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1402 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1404 /* Be sure to not display old picture after our own */
1405 vout_Flush( p_vout, p_picture->date );
1406 p_owner->i_last_rate = i_rate;
1408 vout_DisplayPicture( p_vout, p_picture );
1412 if( p_picture->date <= 0 )
1414 msg_Warn( p_vout, "non-dated video buffer received" );
1418 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1419 p_picture->date - mdate() );
1422 vout_DropPicture( p_vout, p_picture );
1426 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1428 *pi_played_sum += i_tmp_display;
1429 *pi_lost_sum += i_tmp_lost;
1431 if( !b_has_more || b_buffering_first )
1434 vlc_mutex_lock( &p_owner->lock );
1435 if( !p_owner->buffer.p_picture )
1437 vlc_mutex_unlock( &p_owner->lock );
1443 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1445 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1446 input_thread_t *p_input = p_owner->p_input;
1450 int i_displayed = 0;
1452 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1454 vout_thread_t *p_vout = p_owner->p_vout;
1457 /* It prevent freezing VLC in case of broken decoder */
1458 vout_DropPicture( p_vout, p_pic );
1460 block_Release( p_block );
1466 if( p_pic->date < p_owner->i_preroll_end )
1468 vout_DropPicture( p_vout, p_pic );
1472 if( p_owner->i_preroll_end > 0 )
1474 msg_Dbg( p_dec, "End of video preroll" );
1476 vout_Flush( p_vout, 1 );
1478 p_owner->i_preroll_end = -1;
1481 if( p_dec->pf_get_cc &&
1482 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1483 DecoderGetCc( p_dec, p_dec );
1485 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1487 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1489 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1491 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1493 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1496 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1499 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1503 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1506 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1507 vout_thread_t *p_vout = p_owner->p_spu_vout;
1510 if( p_subpic->i_start <= 0 )
1512 msg_Warn( p_dec, "non-dated spu buffer received" );
1513 subpicture_Delete( p_subpic );
1518 vlc_mutex_lock( &p_owner->lock );
1520 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1522 p_subpic->p_next = NULL;
1524 *p_owner->buffer.pp_subpic_next = p_subpic;
1525 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1527 p_owner->buffer.i_count++;
1528 /* XXX it is important to be full after the first one */
1529 if( p_owner->buffer.i_count > 0 )
1531 p_owner->buffer.b_full = true;
1532 vlc_cond_signal( &p_owner->wait );
1538 bool b_has_more = false;
1540 DecoderWaitUnblock( p_dec, &b_reject );
1542 if( p_owner->b_buffering )
1544 vlc_mutex_unlock( &p_owner->lock );
1549 if( p_owner->buffer.p_subpic )
1551 p_subpic = p_owner->buffer.p_subpic;
1553 p_owner->buffer.p_subpic = p_subpic->p_next;
1554 p_owner->buffer.i_count--;
1556 b_has_more = p_owner->buffer.p_subpic != NULL;
1558 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1562 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1563 NULL, NULL, b_telx );
1565 vlc_mutex_unlock( &p_owner->lock );
1568 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1570 subpicture_Delete( p_subpic );
1574 vlc_mutex_lock( &p_owner->lock );
1575 if( !p_owner->buffer.p_subpic )
1577 vlc_mutex_unlock( &p_owner->lock );
1583 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1586 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1588 assert( p_owner->p_clock );
1589 assert( !p_sout_block->p_next );
1591 vlc_mutex_lock( &p_owner->lock );
1593 if( p_owner->b_buffering || p_owner->buffer.p_block )
1595 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1597 p_owner->buffer.i_count++;
1598 /* XXX it is important to be full after the first one */
1599 if( p_owner->buffer.i_count > 0 )
1601 p_owner->buffer.b_full = true;
1602 vlc_cond_signal( &p_owner->wait );
1608 bool b_has_more = false;
1610 DecoderWaitUnblock( p_dec, &b_reject );
1612 if( p_owner->b_buffering )
1614 vlc_mutex_unlock( &p_owner->lock );
1619 if( p_owner->buffer.p_block )
1621 p_sout_block = p_owner->buffer.p_block;
1623 p_owner->buffer.p_block = p_sout_block->p_next;
1624 p_owner->buffer.i_count--;
1626 b_has_more = p_owner->buffer.p_block != NULL;
1628 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1630 p_sout_block->p_next = NULL;
1632 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1633 &p_sout_block->i_length,
1634 &p_sout_block->i_rate, NULL, b_telx );
1636 vlc_mutex_unlock( &p_owner->lock );
1639 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1641 block_Release( p_sout_block );
1645 vlc_mutex_lock( &p_owner->lock );
1646 if( !p_owner->buffer.p_block )
1648 vlc_mutex_unlock( &p_owner->lock );
1655 static void DecoderFlushBuffering( decoder_t *p_dec )
1657 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1659 vlc_assert_locked( &p_owner->lock );
1661 while( p_owner->buffer.p_picture )
1663 picture_t *p_picture = p_owner->buffer.p_picture;
1665 p_owner->buffer.p_picture = p_picture->p_next;
1666 p_owner->buffer.i_count--;
1668 if( p_owner->p_vout )
1669 vout_DropPicture( p_owner->p_vout, p_picture );
1671 if( !p_owner->buffer.p_picture )
1672 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1674 while( p_owner->buffer.p_audio )
1676 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1678 p_owner->buffer.p_audio = p_audio->p_next;
1679 p_owner->buffer.i_count--;
1681 aout_BufferFree( p_audio );
1683 if( !p_owner->buffer.p_audio )
1684 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1686 while( p_owner->buffer.p_subpic )
1688 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1690 p_owner->buffer.p_subpic = p_subpic->p_next;
1691 p_owner->buffer.i_count--;
1693 subpicture_Delete( p_subpic );
1695 if( !p_owner->buffer.p_subpic )
1696 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1698 if( p_owner->buffer.p_block )
1700 block_ChainRelease( p_owner->buffer.p_block );
1702 p_owner->buffer.i_count = 0;
1703 p_owner->buffer.p_block = NULL;
1704 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1708 /* This function process a block for sout
1710 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1712 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1713 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1714 block_t *p_sout_block;
1716 while( ( p_sout_block =
1717 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1719 if( !p_owner->p_sout_input )
1721 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1723 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1724 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1725 if( p_dec->fmt_in.psz_language )
1727 if( p_owner->sout.psz_language )
1728 free( p_owner->sout.psz_language );
1729 p_owner->sout.psz_language =
1730 strdup( p_dec->fmt_in.psz_language );
1733 p_owner->p_sout_input =
1734 sout_InputNew( p_owner->p_sout,
1737 if( p_owner->p_sout_input == NULL )
1739 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1740 (char *)&p_owner->sout.i_codec );
1741 p_dec->b_error = true;
1743 while( p_sout_block )
1745 block_t *p_next = p_sout_block->p_next;
1746 block_Release( p_sout_block );
1747 p_sout_block = p_next;
1753 while( p_sout_block )
1755 block_t *p_next = p_sout_block->p_next;
1757 p_sout_block->p_next = NULL;
1759 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1761 p_sout_block = p_next;
1764 /* For now it's enough, as only sout impact on this flag */
1765 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1766 p_owner->p_input->p->b_out_pace_control )
1768 msg_Dbg( p_dec, "switching to sync mode" );
1769 p_owner->p_input->p->b_out_pace_control = false;
1771 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1772 !p_owner->p_input->p->b_out_pace_control )
1774 msg_Dbg( p_dec, "switching to async mode" );
1775 p_owner->p_input->p->b_out_pace_control = true;
1780 /* This function process a video block
1782 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1784 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1786 if( p_owner->p_packetizer )
1788 block_t *p_packetized_block;
1789 decoder_t *p_packetizer = p_owner->p_packetizer;
1791 while( (p_packetized_block =
1792 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1794 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1796 es_format_Clean( &p_dec->fmt_in );
1797 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1799 if( p_packetizer->pf_get_cc )
1800 DecoderGetCc( p_dec, p_packetizer );
1802 while( p_packetized_block )
1804 block_t *p_next = p_packetized_block->p_next;
1805 p_packetized_block->p_next = NULL;
1807 DecoderDecodeVideo( p_dec, p_packetized_block );
1809 p_packetized_block = p_next;
1815 DecoderDecodeVideo( p_dec, p_block );
1818 if( b_flush && p_owner->p_vout )
1819 vout_Flush( p_owner->p_vout, 1 );
1822 /* This function process a audio block
1824 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1826 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1828 if( p_owner->p_packetizer )
1830 block_t *p_packetized_block;
1831 decoder_t *p_packetizer = p_owner->p_packetizer;
1833 while( (p_packetized_block =
1834 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1836 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1838 es_format_Clean( &p_dec->fmt_in );
1839 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1842 while( p_packetized_block )
1844 block_t *p_next = p_packetized_block->p_next;
1845 p_packetized_block->p_next = NULL;
1847 DecoderDecodeAudio( p_dec, p_packetized_block );
1849 p_packetized_block = p_next;
1855 DecoderDecodeAudio( p_dec, p_block );
1858 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1859 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1862 /* This function process a subtitle block
1864 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1866 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1867 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1869 input_thread_t *p_input = p_owner->p_input;
1870 vout_thread_t *p_vout;
1871 subpicture_t *p_spu;
1873 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1875 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1876 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1877 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1879 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1880 if( p_vout && p_owner->p_spu_vout == p_vout )
1882 /* Preroll does not work very well with subtitle */
1883 if( p_spu->i_start > 0 &&
1884 p_spu->i_start < p_owner->i_preroll_end &&
1885 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1887 subpicture_Delete( p_spu );
1891 DecoderPlaySpu( p_dec, p_spu, b_telx );
1896 subpicture_Delete( p_spu );
1899 vlc_object_release( p_vout );
1902 if( b_flush && p_owner->p_spu_vout )
1904 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1906 if( p_vout && p_owner->p_spu_vout == p_vout )
1907 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1908 p_owner->i_spu_channel );
1911 vlc_object_release( p_vout );
1916 static void DecoderProcessOnFlush( decoder_t *p_dec )
1918 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1920 vlc_mutex_lock( &p_owner->lock );
1921 DecoderFlushBuffering( p_dec );
1922 vlc_mutex_unlock( &p_owner->lock );
1924 DecoderSignalFlushed( p_dec );
1929 * \param p_dec the decoder object
1930 * \param p_block the block to decode
1931 * \return VLC_SUCCESS or an error code
1933 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1935 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1936 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1938 if( p_block && p_block->i_buffer <= 0 )
1940 assert( !b_flush_request );
1941 block_Release( p_block );
1946 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1949 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1951 DecoderProcessSout( p_dec, p_block );
1956 bool b_flush = false;
1960 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1961 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1963 b_flush = !b_flushing && b_flush_request;
1965 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1968 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1970 DecoderProcessAudio( p_dec, p_block, b_flush );
1972 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1974 DecoderProcessVideo( p_dec, p_block, b_flush );
1976 else if( p_dec->fmt_in.i_cat == SPU_ES )
1978 DecoderProcessSpu( p_dec, p_block, b_flush );
1982 msg_Err( p_dec, "unknown ES format" );
1983 p_dec->b_error = true;
1988 if( b_flush_request )
1989 DecoderProcessOnFlush( p_dec );
1992 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1994 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1998 block_Release( p_block );
2000 if( b_flush_request )
2001 DecoderProcessOnFlush( p_dec );
2006 * Destroys a decoder object
2008 * \param p_dec the decoder object
2011 static void DeleteDecoder( decoder_t * p_dec )
2013 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2015 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2016 (char*)&p_dec->fmt_in.i_codec,
2017 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2019 /* Free all packets still in the decoder fifo. */
2020 block_FifoEmpty( p_owner->p_fifo );
2021 block_FifoRelease( p_owner->p_fifo );
2024 vlc_mutex_lock( &p_owner->lock );
2025 DecoderFlushBuffering( p_dec );
2026 vlc_mutex_unlock( &p_owner->lock );
2029 if( p_owner->p_aout_input )
2030 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2031 if( p_owner->p_aout )
2033 vlc_object_release( p_owner->p_aout );
2034 p_owner->p_aout = NULL;
2036 if( p_owner->p_vout )
2038 /* Hack to make sure all the the pictures are freed by the decoder */
2039 vout_FixLeaks( p_owner->p_vout, true );
2041 /* We are about to die. Reattach video output to p_vlc. */
2042 vout_Request( p_dec, p_owner->p_vout, NULL );
2043 input_SendEventVout( p_owner->p_input );
2047 if( p_owner->p_sout_input )
2049 sout_InputDelete( p_owner->p_sout_input );
2050 es_format_Clean( &p_owner->sout );
2054 if( p_dec->fmt_in.i_cat == SPU_ES )
2056 vout_thread_t *p_vout;
2058 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2061 if( p_owner->p_spu_vout == p_vout )
2062 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2063 vlc_object_release( p_vout );
2067 es_format_Clean( &p_dec->fmt_in );
2068 es_format_Clean( &p_dec->fmt_out );
2069 if( p_dec->p_description )
2070 vlc_meta_Delete( p_dec->p_description );
2071 es_format_Clean( &p_owner->fmt_description );
2072 if( p_owner->p_description )
2073 vlc_meta_Delete( p_owner->p_description );
2075 if( p_owner->p_packetizer )
2077 module_unneed( p_owner->p_packetizer,
2078 p_owner->p_packetizer->p_module );
2079 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2080 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2081 if( p_owner->p_packetizer->p_description )
2082 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2083 vlc_object_detach( p_owner->p_packetizer );
2084 vlc_object_release( p_owner->p_packetizer );
2087 vlc_cond_destroy( &p_owner->wait );
2088 vlc_mutex_destroy( &p_owner->lock );
2090 vlc_object_detach( p_dec );
2095 /*****************************************************************************
2096 * Buffers allocation callbacks for the decoders
2097 *****************************************************************************/
2098 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2100 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2102 vlc_assert_locked( &p_owner->lock );
2104 p_owner->b_fmt_description = true;
2106 /* Copy es_format */
2107 es_format_Clean( &p_owner->fmt_description );
2108 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2110 /* Move p_description */
2111 if( p_owner->p_description && p_dec->p_description )
2112 vlc_meta_Delete( p_owner->p_description );
2113 p_owner->p_description = p_dec->p_description;
2114 p_dec->p_description = NULL;
2116 static vout_thread_t *aout_request_vout( void *p_private,
2117 vout_thread_t *p_vout, video_format_t *p_fmt )
2119 decoder_t *p_dec = p_private;
2121 p_vout = vout_Request( p_dec, p_vout, p_fmt );
2122 input_SendEventVout( p_dec->p_owner->p_input );
2127 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2129 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2130 aout_buffer_t *p_buffer;
2132 if( p_owner->p_aout_input != NULL &&
2133 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2134 p_dec->fmt_out.audio.i_original_channels !=
2135 p_owner->audio.i_original_channels ||
2136 p_dec->fmt_out.audio.i_bytes_per_frame !=
2137 p_owner->audio.i_bytes_per_frame ) )
2139 aout_input_t *p_aout_input = p_owner->p_aout_input;
2141 /* Parameters changed, restart the aout */
2142 vlc_mutex_lock( &p_owner->lock );
2144 DecoderFlushBuffering( p_dec );
2146 p_owner->p_aout_input = NULL;
2147 aout_DecDelete( p_owner->p_aout, p_aout_input );
2149 vlc_mutex_unlock( &p_owner->lock );
2152 if( p_owner->p_aout_input == NULL )
2154 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2155 audio_sample_format_t format;
2156 aout_input_t *p_aout_input;
2157 aout_instance_t *p_aout;
2158 aout_request_vout_t request_vout;
2160 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2161 p_owner->audio = p_dec->fmt_out.audio;
2163 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2164 if( i_force_dolby &&
2165 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2166 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2168 if( i_force_dolby == 1 )
2170 format.i_original_channels = format.i_original_channels |
2171 AOUT_CHAN_DOLBYSTEREO;
2173 else /* i_force_dolby == 2 */
2175 format.i_original_channels = format.i_original_channels &
2176 ~AOUT_CHAN_DOLBYSTEREO;
2180 request_vout.pf_request_vout = aout_request_vout;
2181 request_vout.p_private = p_dec;
2183 p_aout = p_owner->p_aout;
2184 p_aout_input = aout_DecNew( p_dec, &p_aout,
2185 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2187 vlc_mutex_lock( &p_owner->lock );
2189 p_owner->p_aout = p_aout;
2190 p_owner->p_aout_input = p_aout_input;
2191 DecoderUpdateFormatLocked( p_dec );
2193 vlc_mutex_unlock( &p_owner->lock );
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 = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2297 vlc_mutex_lock( &p_owner->lock );
2298 p_owner->p_vout = p_vout;
2300 DecoderUpdateFormatLocked( p_dec );
2302 vlc_mutex_unlock( &p_owner->lock );
2304 input_SendEventVout( p_owner->p_input );
2305 if( p_vout == NULL )
2307 msg_Err( p_dec, "failed to create video output" );
2308 p_dec->b_error = true;
2312 if( p_owner->video.i_rmask )
2313 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2314 if( p_owner->video.i_gmask )
2315 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2316 if( p_owner->video.i_bmask )
2317 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2320 /* Get a new picture
2324 picture_t *p_picture;
2326 if( p_dec->b_die || p_dec->b_error )
2329 /* The video filter chain required that there is always 1 free buffer
2330 * that it will use as temporary one. It will release the temporary
2331 * buffer once its work is done, so this check is safe even if we don't
2332 * lock around both count() and create().
2334 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2336 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2341 if( DecoderIsFlushing( p_dec ) )
2345 DecoderSignalBuffering( p_dec, true );
2347 /* Check the decoder doesn't leak pictures */
2348 vout_FixLeaks( p_owner->p_vout, false );
2350 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2351 msleep( VOUT_OUTMEM_SLEEP );
2355 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2357 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2360 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2362 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2365 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2367 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2370 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2372 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2373 vout_thread_t *p_vout = NULL;
2374 subpicture_t *p_subpic;
2375 int i_attempts = 30;
2377 while( i_attempts-- )
2379 if( p_dec->b_die || p_dec->b_error )
2382 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2386 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2391 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2395 if( p_owner->p_spu_vout != p_vout )
2397 vlc_mutex_lock( &p_owner->lock );
2399 DecoderFlushBuffering( p_dec );
2401 vlc_mutex_unlock( &p_owner->lock );
2403 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2404 &p_owner->i_spu_channel );
2405 p_owner->i_spu_order = 0;
2406 p_owner->p_spu_vout = p_vout;
2409 p_subpic = subpicture_New();
2412 p_subpic->i_channel = p_owner->i_spu_channel;
2413 p_subpic->i_order = p_owner->i_spu_order++;
2414 p_subpic->b_subtitle = true;
2417 vlc_object_release( p_vout );
2422 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2424 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2425 vout_thread_t *p_vout = NULL;
2427 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2428 if( !p_vout || p_owner->p_spu_vout != p_vout )
2431 vlc_object_release( p_vout );
2432 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2436 subpicture_Delete( p_subpic );
2438 vlc_object_release( p_vout );