1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_block.h>
40 #include <vlc_codec.h>
43 #include <vlc_dialog.h>
45 #include "audio_output/aout_internal.h"
46 #include "stream_output/stream_output.h"
47 #include "input_internal.h"
53 #include "../video_output/vout_control.h"
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, bool,
56 sout_instance_t *p_sout );
57 static void DeleteDecoder( decoder_t * );
59 static void *DecoderThread( vlc_object_t * );
60 static void DecoderProcess( decoder_t *, block_t * );
61 static void DecoderError( decoder_t *p_dec, block_t *p_block );
62 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
63 static void DecoderFlush( decoder_t * );
64 static void DecoderSignalBuffering( decoder_t *, bool );
65 static void DecoderFlushBuffering( decoder_t * );
67 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
69 /* Buffers allocation callbacks for the decoders */
70 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
71 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
73 static picture_t *vout_new_buffer( decoder_t * );
74 static void vout_del_buffer( decoder_t *, picture_t * );
75 static void vout_link_picture( decoder_t *, picture_t * );
76 static void vout_unlink_picture( decoder_t *, picture_t * );
78 static subpicture_t *spu_new_buffer( decoder_t * );
79 static void spu_del_buffer( decoder_t *, subpicture_t * );
81 struct decoder_owner_sys_t
83 int64_t i_preroll_end;
85 input_thread_t *p_input;
86 input_clock_t *p_clock;
89 vout_thread_t *p_spu_vout;
93 sout_instance_t *p_sout;
94 sout_packetizer_input_t *p_sout_input;
96 /* Some decoders require already packetized data (ie. not truncated) */
97 decoder_t *p_packetizer;
100 /* Current format in use by the output */
101 video_format_t video;
102 audio_format_t audio;
106 bool b_fmt_description;
107 es_format_t fmt_description;
108 vlc_meta_t *p_description;
111 block_fifo_t *p_fifo;
113 /* Lock for communication with decoder thread */
115 vlc_cond_t wait_request;
116 vlc_cond_t wait_acknowledge;
118 /* -- These variables need locking on write(only) -- */
119 aout_instance_t *p_aout;
120 aout_input_t *p_aout_input;
122 vout_thread_t *p_vout;
124 /* -- Theses variables need locking on read *and* write -- */
142 picture_t *p_picture;
143 picture_t **pp_picture_next;
145 subpicture_t *p_subpic;
146 subpicture_t **pp_subpic_next;
148 aout_buffer_t *p_audio;
149 aout_buffer_t **pp_audio_next;
152 block_t **pp_block_next;
163 decoder_t *pp_decoder[4];
170 #define DECODER_MAX_BUFFERING_COUNT (4)
171 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
172 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
174 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
175 * a bogus PTS and won't be displayed */
176 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
179 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
182 /*****************************************************************************
184 *****************************************************************************/
185 picture_t *decoder_NewPicture( decoder_t *p_decoder )
187 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
189 msg_Warn( p_decoder, "can't get output picture" );
192 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
194 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
196 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
198 p_decoder->pf_picture_link( p_decoder, p_picture );
200 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
202 p_decoder->pf_picture_unlink( p_decoder, p_picture );
205 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
207 if( !p_decoder->pf_aout_buffer_new )
209 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
211 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
213 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
216 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
218 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
220 msg_Warn( p_decoder, "can't get output subpicture" );
223 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
225 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
228 /* decoder_GetInputAttachments:
230 int decoder_GetInputAttachments( decoder_t *p_dec,
231 input_attachment_t ***ppp_attachment,
234 if( !p_dec->pf_get_attachments )
237 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
239 /* decoder_GetDisplayDate:
241 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
243 if( !p_dec->pf_get_display_date )
244 return VLC_TS_INVALID;
246 return p_dec->pf_get_display_date( p_dec, i_ts );
248 /* decoder_GetDisplayRate:
250 int decoder_GetDisplayRate( decoder_t *p_dec )
252 if( !p_dec->pf_get_display_rate )
253 return INPUT_RATE_DEFAULT;
255 return p_dec->pf_get_display_rate( p_dec );
259 * Spawns a new decoder thread
261 * \param p_input the input thread
262 * \param p_es the es descriptor
263 * \return the spawned decoder object
265 decoder_t *input_DecoderNew( input_thread_t *p_input,
266 es_format_t *fmt, input_clock_t *p_clock,
267 sout_instance_t *p_sout )
269 decoder_t *p_dec = NULL;
270 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
273 /* Create the decoder configuration structure */
274 p_dec = CreateDecoder( p_input, fmt, p_sout != NULL, p_sout );
277 msg_Err( p_input, "could not create %s", psz_type );
278 dialog_Fatal( p_input, _("Streaming / Transcoding failed"),
279 _("VLC could not open the %s module."),
280 vlc_gettext( psz_type ) );
284 if( !p_dec->p_module )
286 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
288 DeleteDecoder( p_dec );
292 p_dec->p_owner->p_clock = p_clock;
293 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
295 if( p_dec->fmt_out.i_cat == AUDIO_ES )
296 i_priority = VLC_THREAD_PRIORITY_AUDIO;
298 i_priority = VLC_THREAD_PRIORITY_VIDEO;
300 /* Spawn the decoder thread */
301 if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
303 msg_Err( p_dec, "cannot spawn decoder thread" );
304 module_unneed( p_dec, p_dec->p_module );
305 DeleteDecoder( p_dec );
313 * Kills a decoder thread and waits until it's finished
315 * \param p_input the input thread
316 * \param p_es the es descriptor
319 void input_DecoderDelete( decoder_t *p_dec )
321 decoder_owner_sys_t *p_owner = p_dec->p_owner;
323 vlc_object_kill( p_dec );
325 /* Make sure we aren't paused/buffering/waiting anymore */
326 vlc_mutex_lock( &p_owner->lock );
327 const bool b_was_paused = p_owner->b_paused;
328 p_owner->b_paused = false;
329 p_owner->b_buffering = false;
330 p_owner->b_flushing = true;
331 vlc_cond_signal( &p_owner->wait_request );
332 vlc_mutex_unlock( &p_owner->lock );
334 vlc_thread_join( p_dec );
335 p_owner->b_paused = b_was_paused;
337 module_unneed( p_dec, p_dec->p_module );
340 if( p_dec->p_owner->cc.b_supported )
343 for( i = 0; i < 4; i++ )
344 input_DecoderSetCcState( p_dec, false, i );
348 DeleteDecoder( p_dec );
352 * Put a block_t in the decoder's fifo.
353 * Thread-safe w.r.t. the decoder. May be a cancellation point.
355 * \param p_dec the decoder object
356 * \param p_block the data block
358 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
360 decoder_owner_sys_t *p_owner = p_dec->p_owner;
364 /* The fifo is not consummed when buffering and so will
366 * There is no need to lock as b_buffering is never modify
367 * inside decoder thread. */
368 if( !p_owner->b_buffering )
369 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
372 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
374 else if( block_FifoSize( p_owner->p_fifo ) > 400000000 /* 400 MB, ie ~ 50mb/s for 60s */ )
377 /* FIXME: ideally we would check the time amount of data
378 * in the FIFO instead of its size. */
379 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
380 "consumed quickly enough), resetting fifo!" );
381 block_FifoEmpty( p_owner->p_fifo );
384 block_FifoPut( p_owner->p_fifo, p_block );
387 bool input_DecoderIsEmpty( decoder_t * p_dec )
389 assert( !p_dec->p_owner->b_buffering );
391 /* FIXME that's not really true */
392 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
395 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
397 decoder_owner_sys_t *p_owner = p_dec->p_owner;
400 vlc_mutex_lock( &p_owner->lock );
401 for( i = 0; i < 4; i++ )
402 pb_present[i] = p_owner->cc.pb_present[i];
403 vlc_mutex_unlock( &p_owner->lock );
405 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
407 decoder_owner_sys_t *p_owner = p_dec->p_owner;
409 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
411 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
416 static const vlc_fourcc_t fcc[4] = {
417 VLC_FOURCC('c', 'c', '1', ' '),
418 VLC_FOURCC('c', 'c', '2', ' '),
419 VLC_FOURCC('c', 'c', '3', ' '),
420 VLC_FOURCC('c', 'c', '4', ' '),
425 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
426 p_cc = CreateDecoder( p_owner->p_input, &fmt, false, p_owner->p_sout );
429 msg_Err( p_dec, "could not create decoder" );
430 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
431 _("VLC could not open the decoder module.") );
434 else if( !p_cc->p_module )
436 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
437 DeleteDecoder( p_cc );
440 p_cc->p_owner->p_clock = p_owner->p_clock;
442 vlc_mutex_lock( &p_owner->lock );
443 p_owner->cc.pp_decoder[i_channel] = p_cc;
444 vlc_mutex_unlock( &p_owner->lock );
450 vlc_mutex_lock( &p_owner->lock );
451 p_cc = p_owner->cc.pp_decoder[i_channel];
452 p_owner->cc.pp_decoder[i_channel] = NULL;
453 vlc_mutex_unlock( &p_owner->lock );
457 vlc_object_kill( p_cc );
458 module_unneed( p_cc, p_cc->p_module );
459 DeleteDecoder( p_cc );
464 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
466 decoder_owner_sys_t *p_owner = p_dec->p_owner;
469 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
472 vlc_mutex_lock( &p_owner->lock );
473 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
474 vlc_mutex_unlock( &p_owner->lock );
478 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
480 decoder_owner_sys_t *p_owner = p_dec->p_owner;
482 vlc_mutex_lock( &p_owner->lock );
484 assert( !p_owner->b_paused || !b_paused );
486 p_owner->b_paused = b_paused;
487 p_owner->pause.i_date = i_date;
488 p_owner->pause.i_ignore = 0;
489 vlc_cond_signal( &p_owner->wait_request );
491 DecoderOutputChangePause( p_dec, b_paused, i_date );
493 vlc_mutex_unlock( &p_owner->lock );
496 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
498 decoder_owner_sys_t *p_owner = p_dec->p_owner;
500 vlc_mutex_lock( &p_owner->lock );
501 p_owner->i_ts_delay = i_delay;
502 vlc_mutex_unlock( &p_owner->lock );
505 void input_DecoderStartBuffering( decoder_t *p_dec )
507 decoder_owner_sys_t *p_owner = p_dec->p_owner;
509 vlc_mutex_lock( &p_owner->lock );
511 DecoderFlush( p_dec );
513 p_owner->buffer.b_first = true;
514 p_owner->buffer.b_full = false;
515 p_owner->buffer.i_count = 0;
517 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
518 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
520 p_owner->buffer.p_picture = NULL;
521 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
523 p_owner->buffer.p_subpic = NULL;
524 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
526 p_owner->buffer.p_audio = NULL;
527 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
529 p_owner->buffer.p_block = NULL;
530 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
533 p_owner->b_buffering = true;
535 vlc_cond_signal( &p_owner->wait_request );
537 vlc_mutex_unlock( &p_owner->lock );
540 void input_DecoderStopBuffering( decoder_t *p_dec )
542 decoder_owner_sys_t *p_owner = p_dec->p_owner;
544 vlc_mutex_lock( &p_owner->lock );
546 p_owner->b_buffering = false;
548 vlc_cond_signal( &p_owner->wait_request );
550 vlc_mutex_unlock( &p_owner->lock );
553 void input_DecoderWaitBuffering( decoder_t *p_dec )
555 decoder_owner_sys_t *p_owner = p_dec->p_owner;
557 vlc_mutex_lock( &p_owner->lock );
559 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
561 block_FifoWake( p_owner->p_fifo );
562 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
565 vlc_mutex_unlock( &p_owner->lock );
568 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
570 decoder_owner_sys_t *p_owner = p_dec->p_owner;
574 vlc_mutex_lock( &p_owner->lock );
575 if( p_dec->fmt_out.i_cat == VIDEO_ES )
577 if( p_owner->b_paused && p_owner->p_vout )
579 vout_NextPicture( p_owner->p_vout, pi_duration );
580 p_owner->pause.i_ignore++;
581 vlc_cond_signal( &p_owner->wait_request );
586 /* TODO subtitle should not be flushed */
587 DecoderFlush( p_dec );
589 vlc_mutex_unlock( &p_owner->lock );
592 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
594 decoder_owner_sys_t *p_owner = p_dec->p_owner;
597 vlc_mutex_lock( &p_owner->lock );
598 b_changed = p_owner->b_fmt_description;
602 es_format_Copy( p_fmt, &p_owner->fmt_description );
607 if( p_owner->p_description )
609 *pp_meta = vlc_meta_New();
611 vlc_meta_Merge( *pp_meta, p_owner->p_description );
614 p_owner->b_fmt_description = false;
616 vlc_mutex_unlock( &p_owner->lock );
620 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
622 decoder_owner_sys_t *p_owner = p_dec->p_owner;
624 return block_FifoSize( p_owner->p_fifo );
627 void input_DecoderGetObjects( decoder_t *p_dec,
628 vout_thread_t **pp_vout, aout_instance_t **pp_aout )
630 decoder_owner_sys_t *p_owner = p_dec->p_owner;
632 vlc_mutex_lock( &p_owner->lock );
634 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
636 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
637 vlc_mutex_unlock( &p_owner->lock );
640 /*****************************************************************************
642 *****************************************************************************/
643 static int DecoderGetInputAttachments( decoder_t *p_dec,
644 input_attachment_t ***ppp_attachment,
647 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
648 ppp_attachment, pi_attachment );
650 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
652 decoder_owner_sys_t *p_owner = p_dec->p_owner;
654 vlc_mutex_lock( &p_owner->lock );
655 if( p_owner->b_buffering || p_owner->b_paused )
656 i_ts = VLC_TS_INVALID;
657 vlc_mutex_unlock( &p_owner->lock );
659 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
662 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
663 return VLC_TS_INVALID;
667 static int DecoderGetDisplayRate( decoder_t *p_dec )
669 decoder_owner_sys_t *p_owner = p_dec->p_owner;
671 if( !p_owner->p_clock )
672 return INPUT_RATE_DEFAULT;
673 return input_clock_GetRate( p_owner->p_clock );
677 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
679 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
680 "VLC probably does not support this sound or video format.",
682 dialog_Fatal( p_dec, _("No suitable decoder module"),
683 _("VLC does not support the audio or video format \"%4.4s\". "
684 "Unfortunately there is no way for you to fix this."),
690 * Create a decoder object
692 * \param p_input the input thread
693 * \param p_es the es descriptor
694 * \param b_packetizer instead of a decoder
695 * \return the decoder object
697 static decoder_t * CreateDecoder( input_thread_t *p_input,
698 es_format_t *fmt, bool b_packetizer,
699 sout_instance_t *p_sout )
702 decoder_owner_sys_t *p_owner;
703 es_format_t null_es_format;
705 p_dec = vlc_custom_create( p_input, sizeof( *p_dec ), VLC_OBJECT_DECODER,
710 p_dec->pf_decode_audio = NULL;
711 p_dec->pf_decode_video = NULL;
712 p_dec->pf_decode_sub = NULL;
713 p_dec->pf_get_cc = NULL;
714 p_dec->pf_packetize = NULL;
716 /* Initialize the decoder */
717 p_dec->p_module = NULL;
719 memset( &null_es_format, 0, sizeof(es_format_t) );
720 es_format_Copy( &p_dec->fmt_in, fmt );
721 es_format_Copy( &p_dec->fmt_out, &null_es_format );
723 p_dec->p_description = NULL;
725 /* Allocate our private structure for the decoder */
726 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
727 if( p_dec->p_owner == NULL )
729 vlc_object_release( p_dec );
732 p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
733 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
734 p_dec->p_owner->p_input = p_input;
735 p_dec->p_owner->p_aout = NULL;
736 p_dec->p_owner->p_aout_input = NULL;
737 p_dec->p_owner->p_vout = NULL;
738 p_dec->p_owner->p_spu_vout = NULL;
739 p_dec->p_owner->i_spu_channel = 0;
740 p_dec->p_owner->i_spu_order = 0;
741 p_dec->p_owner->p_sout = p_sout;
742 p_dec->p_owner->p_sout_input = NULL;
743 p_dec->p_owner->p_packetizer = NULL;
744 p_dec->p_owner->b_packetizer = b_packetizer;
747 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
749 free( p_dec->p_owner );
750 vlc_object_release( p_dec );
754 /* Set buffers allocation callbacks for the decoders */
755 p_dec->pf_aout_buffer_new = aout_new_buffer;
756 p_dec->pf_aout_buffer_del = aout_del_buffer;
757 p_dec->pf_vout_buffer_new = vout_new_buffer;
758 p_dec->pf_vout_buffer_del = vout_del_buffer;
759 p_dec->pf_picture_link = vout_link_picture;
760 p_dec->pf_picture_unlink = vout_unlink_picture;
761 p_dec->pf_spu_buffer_new = spu_new_buffer;
762 p_dec->pf_spu_buffer_del = spu_del_buffer;
764 p_dec->pf_get_attachments = DecoderGetInputAttachments;
765 p_dec->pf_get_display_date = DecoderGetDisplayDate;
766 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
768 vlc_object_attach( p_dec, p_input );
770 /* Find a suitable decoder/packetizer module */
772 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
774 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
776 /* Check if decoder requires already packetized data */
778 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
780 p_dec->p_owner->p_packetizer =
781 vlc_custom_create( p_input, sizeof( decoder_t ),
782 VLC_OBJECT_DECODER, "packetizer" );
783 if( p_dec->p_owner->p_packetizer )
785 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
788 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
791 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
793 p_dec->p_owner->p_packetizer->p_module =
794 module_need( p_dec->p_owner->p_packetizer,
795 "packetizer", "$packetizer", false );
797 if( !p_dec->p_owner->p_packetizer->p_module )
799 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
800 vlc_object_release( p_dec->p_owner->p_packetizer );
805 /* Copy ourself the input replay gain */
806 if( fmt->i_cat == AUDIO_ES )
808 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
810 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
812 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
813 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
815 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
817 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
818 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
824 vlc_mutex_init( &p_owner->lock );
825 vlc_cond_init( &p_owner->wait_request );
826 vlc_cond_init( &p_owner->wait_acknowledge );
828 p_owner->b_fmt_description = false;
829 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
830 p_owner->p_description = NULL;
832 p_owner->b_paused = false;
833 p_owner->pause.i_date = VLC_TS_INVALID;
834 p_owner->pause.i_ignore = 0;
836 p_owner->b_buffering = false;
837 p_owner->buffer.b_first = true;
838 p_owner->buffer.b_full = false;
839 p_owner->buffer.i_count = 0;
840 p_owner->buffer.p_picture = NULL;
841 p_owner->buffer.p_subpic = NULL;
842 p_owner->buffer.p_audio = NULL;
843 p_owner->buffer.p_block = NULL;
845 p_owner->b_flushing = false;
848 p_owner->cc.b_supported = false;
851 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
852 p_owner->cc.b_supported = true;
853 if( p_dec->pf_get_cc )
854 p_owner->cc.b_supported = true;
857 for( unsigned i = 0; i < 4; i++ )
859 p_owner->cc.pb_present[i] = false;
860 p_owner->cc.pp_decoder[i] = NULL;
862 p_owner->i_ts_delay = 0;
867 * The decoding main loop
869 * \param p_dec the decoder
871 static void *DecoderThread( vlc_object_t *p_this )
873 decoder_t *p_dec = (decoder_t *)p_this;
874 decoder_owner_sys_t *p_owner = p_dec->p_owner;
876 /* The decoder's main loop */
879 block_t *p_block = block_FifoGet( p_owner->p_fifo );
881 /* Make sure there is no cancellation point other than this one^^.
882 * If you need one, be sure to push cleanup of p_block. */
883 DecoderSignalBuffering( p_dec, p_block == NULL );
887 int canc = vlc_savecancel();
890 DecoderError( p_dec, p_block );
892 DecoderProcess( p_dec, p_block );
894 vlc_restorecancel( canc );
900 static block_t *DecoderBlockFlushNew()
902 block_t *p_null = block_Alloc( 128 );
906 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
907 BLOCK_FLAG_CORRUPTED |
908 BLOCK_FLAG_CORE_FLUSH;
909 memset( p_null->p_buffer, 0, p_null->i_buffer );
914 static void DecoderFlush( decoder_t *p_dec )
916 decoder_owner_sys_t *p_owner = p_dec->p_owner;
918 vlc_assert_locked( &p_owner->lock );
921 block_FifoEmpty( p_owner->p_fifo );
923 /* Monitor for flush end */
924 p_owner->b_flushing = true;
925 vlc_cond_signal( &p_owner->wait_request );
927 /* Send a special block */
928 block_t *p_null = DecoderBlockFlushNew();
931 input_DecoderDecode( p_dec, p_null, false );
934 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
935 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
938 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
940 decoder_owner_sys_t *p_owner = p_dec->p_owner;
942 vlc_mutex_lock( &p_owner->lock );
944 if( p_owner->b_buffering )
947 p_owner->buffer.b_full = true;
948 vlc_cond_signal( &p_owner->wait_acknowledge );
951 vlc_mutex_unlock( &p_owner->lock );
954 static bool DecoderIsFlushing( decoder_t *p_dec )
956 decoder_owner_sys_t *p_owner = p_dec->p_owner;
959 vlc_mutex_lock( &p_owner->lock );
961 b_flushing = p_owner->b_flushing;
963 vlc_mutex_unlock( &p_owner->lock );
968 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
970 decoder_owner_sys_t *p_owner = p_dec->p_owner;
972 vlc_assert_locked( &p_owner->lock );
974 while( !p_owner->b_flushing )
976 if( p_owner->b_paused )
978 if( p_owner->b_buffering && !p_owner->buffer.b_full )
980 if( p_owner->pause.i_ignore > 0 )
982 p_owner->pause.i_ignore--;
988 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
991 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
995 *pb_reject = p_owner->b_flushing;
998 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1000 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1002 vlc_assert_locked( &p_owner->lock );
1004 /* XXX only audio and video output have to be paused.
1005 * - for sout it is useless
1006 * - for subs, it is done by the vout
1008 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1010 if( p_owner->p_aout && p_owner->p_aout_input )
1011 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1014 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1016 if( p_owner->p_vout )
1017 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1020 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1022 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1023 *pi_preroll = INT64_MAX;
1024 else if( p->i_dts > VLC_TS_INVALID )
1025 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1026 else if( p->i_pts > VLC_TS_INVALID )
1027 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1030 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1032 mtime_t current_date = mdate();
1034 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1035 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1037 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1038 * for teletext streams. */
1039 return current_date + 400000;
1044 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1045 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1047 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1048 input_clock_t *p_clock = p_owner->p_clock;
1050 vlc_assert_locked( &p_owner->lock );
1052 const mtime_t i_es_delay = p_owner->i_ts_delay;
1056 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1059 if( *pi_ts0 > VLC_TS_INVALID )
1061 *pi_ts0 += i_es_delay;
1062 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1063 *pi_ts1 += i_es_delay;
1064 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1065 *pi_ts0 = VLC_TS_INVALID;
1069 i_rate = input_clock_GetRate( p_clock );
1072 /* Do not create ephemere data because of rounding errors */
1073 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1077 *pi_duration = ( *pi_duration * i_rate +
1078 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1085 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1086 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1093 * If *pb_reject, it does nothing, otherwise it waits for the given
1094 * deadline or a flush request (in which case it set *pi_reject to true.
1096 static void DecoderWaitDate( decoder_t *p_dec,
1097 bool *pb_reject, mtime_t i_deadline )
1099 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1101 if( *pb_reject || i_deadline < 0 )
1106 vlc_mutex_lock( &p_owner->lock );
1107 if( p_owner->b_flushing || p_dec->b_die )
1110 vlc_mutex_unlock( &p_owner->lock );
1113 int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1115 vlc_mutex_unlock( &p_owner->lock );
1121 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1122 int *pi_played_sum, int *pi_lost_sum )
1124 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1125 aout_instance_t *p_aout = p_owner->p_aout;
1126 aout_input_t *p_aout_input = p_owner->p_aout_input;
1129 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1131 msg_Warn( p_dec, "non-dated audio buffer received" );
1133 aout_BufferFree( p_audio );
1138 vlc_mutex_lock( &p_owner->lock );
1140 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1142 p_audio->p_next = NULL;
1144 *p_owner->buffer.pp_audio_next = p_audio;
1145 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1147 p_owner->buffer.i_count++;
1148 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1149 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1151 p_owner->buffer.b_full = true;
1152 vlc_cond_signal( &p_owner->wait_acknowledge );
1158 bool b_has_more = false;
1160 DecoderWaitUnblock( p_dec, &b_reject );
1162 if( p_owner->b_buffering )
1164 vlc_mutex_unlock( &p_owner->lock );
1169 if( p_owner->buffer.p_audio )
1171 p_audio = p_owner->buffer.p_audio;
1173 p_owner->buffer.p_audio = p_audio->p_next;
1174 p_owner->buffer.i_count--;
1176 b_has_more = p_owner->buffer.p_audio != NULL;
1178 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1182 const bool b_dated = p_audio->i_pts > VLC_TS_INVALID;
1183 int i_rate = INPUT_RATE_DEFAULT;
1185 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1186 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1188 vlc_mutex_unlock( &p_owner->lock );
1190 if( !p_aout || !p_aout_input ||
1191 p_audio->i_pts <= VLC_TS_INVALID ||
1192 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1193 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1196 DecoderWaitDate( p_dec, &b_reject,
1197 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1201 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1202 *pi_played_sum += 1;
1203 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1208 msg_Warn( p_aout, "received buffer in the future" );
1210 msg_Warn( p_dec, "non-dated audio buffer received" );
1213 aout_BufferFree( p_audio );
1219 vlc_mutex_lock( &p_owner->lock );
1220 if( !p_owner->buffer.p_audio )
1222 vlc_mutex_unlock( &p_owner->lock );
1228 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1230 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1231 input_thread_t *p_input = p_owner->p_input;
1232 aout_buffer_t *p_aout_buf;
1237 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1239 aout_instance_t *p_aout = p_owner->p_aout;
1240 aout_input_t *p_aout_input = p_owner->p_aout_input;
1244 /* It prevent freezing VLC in case of broken decoder */
1245 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1247 block_Release( p_block );
1252 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1253 p_aout_buf->i_pts < p_owner->i_preroll_end )
1255 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1259 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1261 msg_Dbg( p_dec, "End of audio preroll" );
1262 if( p_owner->p_aout && p_owner->p_aout_input )
1263 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1265 p_owner->i_preroll_end = VLC_TS_INVALID;
1268 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1271 /* Update ugly stat */
1272 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1274 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1276 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1278 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1280 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1283 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1286 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1288 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1291 bool b_processed = false;
1295 assert( p_dec_cc->pf_get_cc != NULL );
1297 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1298 if( !p_owner->cc.b_supported )
1301 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1305 vlc_mutex_lock( &p_owner->lock );
1306 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1308 p_owner->cc.pb_present[i] |= pb_present[i];
1309 if( p_owner->cc.pp_decoder[i] )
1313 for( i = 0; i < 4; i++ )
1315 if( !p_owner->cc.pp_decoder[i] )
1318 if( i_cc_decoder > 1 )
1319 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1321 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1325 vlc_mutex_unlock( &p_owner->lock );
1328 block_Release( p_cc );
1331 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1332 int *pi_played_sum, int *pi_lost_sum )
1334 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1335 vout_thread_t *p_vout = p_owner->p_vout;
1336 bool b_first_buffered;
1338 if( p_picture->date <= VLC_TS_INVALID )
1340 msg_Warn( p_vout, "non-dated video buffer received" );
1342 vout_DropPicture( p_vout, p_picture );
1347 vlc_mutex_lock( &p_owner->lock );
1349 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1351 p_picture->p_next = NULL;
1353 *p_owner->buffer.pp_picture_next = p_picture;
1354 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1356 p_owner->buffer.i_count++;
1357 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1358 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1360 p_owner->buffer.b_full = true;
1361 vlc_cond_signal( &p_owner->wait_acknowledge );
1364 b_first_buffered = p_owner->buffer.p_picture != NULL;
1366 for( ;; b_first_buffered = false )
1368 bool b_has_more = false;
1372 DecoderWaitUnblock( p_dec, &b_reject );
1374 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1376 vlc_mutex_unlock( &p_owner->lock );
1379 bool b_buffering_first = p_owner->b_buffering;
1382 if( p_owner->buffer.p_picture )
1384 p_picture = p_owner->buffer.p_picture;
1386 p_owner->buffer.p_picture = p_picture->p_next;
1387 p_owner->buffer.i_count--;
1389 b_has_more = p_owner->buffer.p_picture != NULL;
1391 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1395 if( b_buffering_first )
1397 assert( p_owner->buffer.b_first );
1398 assert( !p_owner->buffer.i_count );
1399 msg_Dbg( p_dec, "Received first picture" );
1400 p_owner->buffer.b_first = false;
1401 p_picture->b_force = true;
1404 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1405 int i_rate = INPUT_RATE_DEFAULT;
1406 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1407 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1409 vlc_mutex_unlock( &p_owner->lock );
1412 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1417 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1419 /* Be sure to not display old picture after our own */
1420 vout_Flush( p_vout, p_picture->date );
1421 p_owner->i_last_rate = i_rate;
1423 vout_DisplayPicture( p_vout, p_picture );
1428 msg_Warn( p_vout, "early picture skipped" );
1430 msg_Warn( p_vout, "non-dated video buffer received" );
1433 vout_DropPicture( p_vout, p_picture );
1437 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1439 *pi_played_sum += i_tmp_display;
1440 *pi_lost_sum += i_tmp_lost;
1442 if( !b_has_more || b_buffering_first )
1445 vlc_mutex_lock( &p_owner->lock );
1446 if( !p_owner->buffer.p_picture )
1448 vlc_mutex_unlock( &p_owner->lock );
1454 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1456 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1457 input_thread_t *p_input = p_owner->p_input;
1461 int i_displayed = 0;
1463 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1465 vout_thread_t *p_vout = p_owner->p_vout;
1468 /* It prevent freezing VLC in case of broken decoder */
1469 vout_DropPicture( p_vout, p_pic );
1471 block_Release( p_block );
1477 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1479 vout_DropPicture( p_vout, p_pic );
1483 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1485 msg_Dbg( p_dec, "End of video preroll" );
1487 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1489 p_owner->i_preroll_end = VLC_TS_INVALID;
1492 if( p_dec->pf_get_cc &&
1493 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1494 DecoderGetCc( p_dec, p_dec );
1496 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1498 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1500 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1502 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1504 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1507 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1510 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1514 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1517 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1518 vout_thread_t *p_vout = p_owner->p_spu_vout;
1521 if( p_subpic->i_start <= VLC_TS_INVALID && !b_telx )
1523 msg_Warn( p_dec, "non-dated spu buffer received" );
1524 subpicture_Delete( p_subpic );
1529 vlc_mutex_lock( &p_owner->lock );
1531 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1533 p_subpic->p_next = NULL;
1535 *p_owner->buffer.pp_subpic_next = p_subpic;
1536 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1538 p_owner->buffer.i_count++;
1539 /* XXX it is important to be full after the first one */
1540 if( p_owner->buffer.i_count > 0 )
1542 p_owner->buffer.b_full = true;
1543 vlc_cond_signal( &p_owner->wait_acknowledge );
1549 bool b_has_more = false;
1551 DecoderWaitUnblock( p_dec, &b_reject );
1553 if( p_owner->b_buffering )
1555 vlc_mutex_unlock( &p_owner->lock );
1560 if( p_owner->buffer.p_subpic )
1562 p_subpic = p_owner->buffer.p_subpic;
1564 p_owner->buffer.p_subpic = p_subpic->p_next;
1565 p_owner->buffer.i_count--;
1567 b_has_more = p_owner->buffer.p_subpic != NULL;
1569 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1573 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1574 NULL, INT64_MAX, b_telx );
1576 vlc_mutex_unlock( &p_owner->lock );
1578 if( p_subpic->i_start <= VLC_TS_INVALID )
1581 DecoderWaitDate( p_dec, &b_reject,
1582 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1585 spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_subpic );
1587 subpicture_Delete( p_subpic );
1591 vlc_mutex_lock( &p_owner->lock );
1592 if( !p_owner->buffer.p_subpic )
1594 vlc_mutex_unlock( &p_owner->lock );
1600 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1603 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1605 assert( p_owner->p_clock );
1606 assert( !p_sout_block->p_next );
1608 vlc_mutex_lock( &p_owner->lock );
1610 if( p_owner->b_buffering || p_owner->buffer.p_block )
1612 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1614 p_owner->buffer.i_count++;
1615 /* XXX it is important to be full after the first one */
1616 if( p_owner->buffer.i_count > 0 )
1618 p_owner->buffer.b_full = true;
1619 vlc_cond_signal( &p_owner->wait_acknowledge );
1625 bool b_has_more = false;
1627 DecoderWaitUnblock( p_dec, &b_reject );
1629 if( p_owner->b_buffering )
1631 vlc_mutex_unlock( &p_owner->lock );
1636 if( p_owner->buffer.p_block )
1638 p_sout_block = p_owner->buffer.p_block;
1640 p_owner->buffer.p_block = p_sout_block->p_next;
1641 p_owner->buffer.i_count--;
1643 b_has_more = p_owner->buffer.p_block != NULL;
1645 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1647 p_sout_block->p_next = NULL;
1649 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1650 &p_sout_block->i_length,
1651 &p_sout_block->i_rate, INT64_MAX, b_telx );
1653 vlc_mutex_unlock( &p_owner->lock );
1656 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1658 block_Release( p_sout_block );
1662 vlc_mutex_lock( &p_owner->lock );
1663 if( !p_owner->buffer.p_block )
1665 vlc_mutex_unlock( &p_owner->lock );
1672 static void DecoderFlushBuffering( decoder_t *p_dec )
1674 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1676 vlc_assert_locked( &p_owner->lock );
1678 while( p_owner->buffer.p_picture )
1680 picture_t *p_picture = p_owner->buffer.p_picture;
1682 p_owner->buffer.p_picture = p_picture->p_next;
1683 p_owner->buffer.i_count--;
1685 if( p_owner->p_vout )
1686 vout_DropPicture( p_owner->p_vout, p_picture );
1688 if( !p_owner->buffer.p_picture )
1689 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1691 while( p_owner->buffer.p_audio )
1693 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1695 p_owner->buffer.p_audio = p_audio->p_next;
1696 p_owner->buffer.i_count--;
1698 aout_BufferFree( p_audio );
1700 if( !p_owner->buffer.p_audio )
1701 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1703 while( p_owner->buffer.p_subpic )
1705 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1707 p_owner->buffer.p_subpic = p_subpic->p_next;
1708 p_owner->buffer.i_count--;
1710 subpicture_Delete( p_subpic );
1712 if( !p_owner->buffer.p_subpic )
1713 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1715 if( p_owner->buffer.p_block )
1717 block_ChainRelease( p_owner->buffer.p_block );
1719 p_owner->buffer.i_count = 0;
1720 p_owner->buffer.p_block = NULL;
1721 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1725 /* This function process a block for sout
1727 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1729 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1730 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1731 block_t *p_sout_block;
1733 while( ( p_sout_block =
1734 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1736 if( !p_owner->p_sout_input )
1738 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1740 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1741 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1742 if( p_dec->fmt_in.psz_language )
1744 free( p_owner->sout.psz_language );
1745 p_owner->sout.psz_language =
1746 strdup( p_dec->fmt_in.psz_language );
1749 p_owner->p_sout_input =
1750 sout_InputNew( p_owner->p_sout,
1753 if( p_owner->p_sout_input == NULL )
1755 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1756 (char *)&p_owner->sout.i_codec );
1757 p_dec->b_error = true;
1759 while( p_sout_block )
1761 block_t *p_next = p_sout_block->p_next;
1762 block_Release( p_sout_block );
1763 p_sout_block = p_next;
1769 while( p_sout_block )
1771 block_t *p_next = p_sout_block->p_next;
1773 p_sout_block->p_next = NULL;
1775 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1777 p_sout_block = p_next;
1782 /* This function process a video block
1784 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1786 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1788 if( p_owner->p_packetizer )
1790 block_t *p_packetized_block;
1791 decoder_t *p_packetizer = p_owner->p_packetizer;
1793 while( (p_packetized_block =
1794 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1796 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1798 es_format_Clean( &p_dec->fmt_in );
1799 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1801 if( p_packetizer->pf_get_cc )
1802 DecoderGetCc( p_dec, p_packetizer );
1804 while( p_packetized_block )
1806 block_t *p_next = p_packetized_block->p_next;
1807 p_packetized_block->p_next = NULL;
1809 DecoderDecodeVideo( p_dec, p_packetized_block );
1811 p_packetized_block = p_next;
1814 /* The packetizer does not output a block that tell the decoder to flush
1818 block_t *p_null = DecoderBlockFlushNew();
1820 DecoderDecodeVideo( p_dec, p_null );
1825 DecoderDecodeVideo( p_dec, p_block );
1828 if( b_flush && p_owner->p_vout )
1829 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1832 /* This function process a audio block
1834 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1836 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1838 if( p_owner->p_packetizer )
1840 block_t *p_packetized_block;
1841 decoder_t *p_packetizer = p_owner->p_packetizer;
1843 while( (p_packetized_block =
1844 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1846 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1848 es_format_Clean( &p_dec->fmt_in );
1849 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1852 while( p_packetized_block )
1854 block_t *p_next = p_packetized_block->p_next;
1855 p_packetized_block->p_next = NULL;
1857 DecoderDecodeAudio( p_dec, p_packetized_block );
1859 p_packetized_block = p_next;
1862 /* The packetizer does not output a block that tell the decoder to flush
1866 block_t *p_null = DecoderBlockFlushNew();
1868 DecoderDecodeAudio( p_dec, p_null );
1873 DecoderDecodeAudio( p_dec, p_block );
1876 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1877 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1880 /* This function process a subtitle block
1882 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1884 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1885 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1887 input_thread_t *p_input = p_owner->p_input;
1888 vout_thread_t *p_vout;
1889 subpicture_t *p_spu;
1891 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1893 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1894 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1895 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1897 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1898 if( p_vout && p_owner->p_spu_vout == p_vout )
1900 /* Preroll does not work very well with subtitle */
1901 if( p_spu->i_start > VLC_TS_INVALID &&
1902 p_spu->i_start < p_owner->i_preroll_end &&
1903 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1905 subpicture_Delete( p_spu );
1909 DecoderPlaySpu( p_dec, p_spu, b_telx );
1914 subpicture_Delete( p_spu );
1917 vlc_object_release( p_vout );
1920 if( b_flush && p_owner->p_spu_vout )
1922 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1924 if( p_vout && p_owner->p_spu_vout == p_vout )
1925 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR,
1926 p_owner->i_spu_channel );
1929 vlc_object_release( p_vout );
1934 static void DecoderProcessOnFlush( decoder_t *p_dec )
1936 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1938 vlc_mutex_lock( &p_owner->lock );
1939 DecoderFlushBuffering( p_dec );
1941 if( p_owner->b_flushing )
1943 p_owner->b_flushing = false;
1944 vlc_cond_signal( &p_owner->wait_acknowledge );
1946 vlc_mutex_unlock( &p_owner->lock );
1952 * \param p_dec the decoder object
1953 * \param p_block the block to decode
1954 * \return VLC_SUCCESS or an error code
1956 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1958 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1959 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1961 if( p_block && p_block->i_buffer <= 0 )
1963 assert( !b_flush_request );
1964 block_Release( p_block );
1969 if( p_owner->b_packetizer )
1972 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1974 DecoderProcessSout( p_dec, p_block );
1979 bool b_flush = false;
1983 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1984 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1986 b_flush = !b_flushing && b_flush_request;
1988 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1991 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1993 DecoderProcessAudio( p_dec, p_block, b_flush );
1995 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1997 DecoderProcessVideo( p_dec, p_block, b_flush );
1999 else if( p_dec->fmt_out.i_cat == SPU_ES )
2001 DecoderProcessSpu( p_dec, p_block, b_flush );
2005 msg_Err( p_dec, "unknown ES format" );
2006 p_dec->b_error = true;
2011 if( b_flush_request )
2012 DecoderProcessOnFlush( p_dec );
2015 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2017 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2021 block_Release( p_block );
2023 if( b_flush_request )
2024 DecoderProcessOnFlush( p_dec );
2029 * Destroys a decoder object
2031 * \param p_dec the decoder object
2034 static void DeleteDecoder( decoder_t * p_dec )
2036 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2038 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2039 (char*)&p_dec->fmt_in.i_codec,
2040 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2042 /* Free all packets still in the decoder fifo. */
2043 block_FifoEmpty( p_owner->p_fifo );
2044 block_FifoRelease( p_owner->p_fifo );
2047 vlc_mutex_lock( &p_owner->lock );
2048 DecoderFlushBuffering( p_dec );
2049 vlc_mutex_unlock( &p_owner->lock );
2052 if( p_owner->p_aout_input )
2053 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2054 if( p_owner->p_aout )
2056 input_resource_RequestAout( p_owner->p_input->p->p_resource,
2058 input_SendEventAout( p_owner->p_input );
2059 p_owner->p_aout = NULL;
2061 if( p_owner->p_vout )
2063 /* Hack to make sure all the the pictures are freed by the decoder
2064 * and that the vout is not paused anymore */
2065 vout_FixLeaks( p_owner->p_vout, true );
2066 if( p_owner->b_paused )
2067 vout_ChangePause( p_owner->p_vout, false, mdate() );
2070 input_resource_RequestVout( p_owner->p_input->p->p_resource, p_owner->p_vout, NULL, true );
2071 input_SendEventVout( p_owner->p_input );
2075 if( p_owner->p_sout_input )
2077 sout_InputDelete( p_owner->p_sout_input );
2078 es_format_Clean( &p_owner->sout );
2082 if( p_dec->fmt_out.i_cat == SPU_ES )
2084 vout_thread_t *p_vout;
2086 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2089 if( p_owner->p_spu_vout == p_vout )
2090 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2091 vlc_object_release( p_vout );
2095 es_format_Clean( &p_dec->fmt_in );
2096 es_format_Clean( &p_dec->fmt_out );
2097 if( p_dec->p_description )
2098 vlc_meta_Delete( p_dec->p_description );
2099 es_format_Clean( &p_owner->fmt_description );
2100 if( p_owner->p_description )
2101 vlc_meta_Delete( p_owner->p_description );
2103 if( p_owner->p_packetizer )
2105 module_unneed( p_owner->p_packetizer,
2106 p_owner->p_packetizer->p_module );
2107 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2108 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2109 if( p_owner->p_packetizer->p_description )
2110 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2111 vlc_object_release( p_owner->p_packetizer );
2114 vlc_cond_destroy( &p_owner->wait_acknowledge );
2115 vlc_cond_destroy( &p_owner->wait_request );
2116 vlc_mutex_destroy( &p_owner->lock );
2118 vlc_object_release( p_dec );
2123 /*****************************************************************************
2124 * Buffers allocation callbacks for the decoders
2125 *****************************************************************************/
2126 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2128 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2130 vlc_assert_locked( &p_owner->lock );
2132 p_owner->b_fmt_description = true;
2134 /* Copy es_format */
2135 es_format_Clean( &p_owner->fmt_description );
2136 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2138 /* Move p_description */
2139 if( p_owner->p_description && p_dec->p_description )
2140 vlc_meta_Delete( p_owner->p_description );
2141 p_owner->p_description = p_dec->p_description;
2142 p_dec->p_description = NULL;
2144 static vout_thread_t *aout_request_vout( void *p_private,
2145 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2147 decoder_t *p_dec = p_private;
2148 input_thread_t *p_input = p_dec->p_owner->p_input;
2150 p_vout = input_resource_RequestVout( p_input->p->p_resource, p_vout, p_fmt, b_recyle );
2151 input_SendEventVout( p_input );
2156 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2158 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2159 aout_buffer_t *p_buffer;
2161 if( p_owner->p_aout_input != NULL &&
2162 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2163 p_dec->fmt_out.audio.i_original_channels !=
2164 p_owner->audio.i_original_channels ||
2165 p_dec->fmt_out.audio.i_bytes_per_frame !=
2166 p_owner->audio.i_bytes_per_frame ) )
2168 aout_input_t *p_aout_input = p_owner->p_aout_input;
2170 /* Parameters changed, restart the aout */
2171 vlc_mutex_lock( &p_owner->lock );
2173 DecoderFlushBuffering( p_dec );
2175 p_owner->p_aout_input = NULL;
2176 aout_DecDelete( p_owner->p_aout, p_aout_input );
2178 vlc_mutex_unlock( &p_owner->lock );
2181 if( p_owner->p_aout_input == NULL )
2183 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2184 audio_sample_format_t format;
2185 aout_input_t *p_aout_input;
2186 aout_instance_t *p_aout;
2187 aout_request_vout_t request_vout;
2189 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2190 p_owner->audio = p_dec->fmt_out.audio;
2192 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2193 if( i_force_dolby &&
2194 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2195 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2197 if( i_force_dolby == 1 )
2199 format.i_original_channels = format.i_original_channels |
2200 AOUT_CHAN_DOLBYSTEREO;
2202 else /* i_force_dolby == 2 */
2204 format.i_original_channels = format.i_original_channels &
2205 ~AOUT_CHAN_DOLBYSTEREO;
2209 request_vout.pf_request_vout = aout_request_vout;
2210 request_vout.p_private = p_dec;
2212 p_aout = p_owner->p_aout;
2214 p_aout = input_resource_RequestAout( p_owner->p_input->p->p_resource, NULL );
2215 p_aout_input = aout_DecNew( p_dec, &p_aout,
2216 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2218 vlc_mutex_lock( &p_owner->lock );
2220 p_owner->p_aout = p_aout;
2221 p_owner->p_aout_input = p_aout_input;
2222 DecoderUpdateFormatLocked( p_dec );
2224 vlc_mutex_unlock( &p_owner->lock );
2226 input_SendEventAout( p_owner->p_input );
2228 if( p_owner->p_aout_input == NULL )
2230 msg_Err( p_dec, "failed to create audio output" );
2231 p_dec->b_error = true;
2234 p_dec->fmt_out.audio.i_bytes_per_frame =
2235 p_owner->audio.i_bytes_per_frame;
2238 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2243 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2245 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2247 aout_DecDeleteBuffer( p_owner->p_aout,
2248 p_owner->p_aout_input, p_buffer );
2252 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2254 static picture_t *vout_new_buffer( decoder_t *p_dec )
2256 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2258 if( p_owner->p_vout == NULL ||
2259 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2260 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2261 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2262 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2263 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2265 vout_thread_t *p_vout;
2267 if( !p_dec->fmt_out.video.i_width ||
2268 !p_dec->fmt_out.video.i_height )
2270 /* Can't create a new vout without display size */
2274 if( !p_dec->fmt_out.video.i_visible_width ||
2275 !p_dec->fmt_out.video.i_visible_height )
2277 if( p_dec->fmt_in.video.i_visible_width &&
2278 p_dec->fmt_in.video.i_visible_height )
2280 p_dec->fmt_out.video.i_visible_width =
2281 p_dec->fmt_in.video.i_visible_width;
2282 p_dec->fmt_out.video.i_visible_height =
2283 p_dec->fmt_in.video.i_visible_height;
2287 p_dec->fmt_out.video.i_visible_width =
2288 p_dec->fmt_out.video.i_width;
2289 p_dec->fmt_out.video.i_visible_height =
2290 p_dec->fmt_out.video.i_height;
2294 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2295 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2297 p_dec->fmt_out.video.i_visible_height = 1080;
2298 if( !(p_dec->fmt_out.video.i_sar_num % 136))
2300 p_dec->fmt_out.video.i_sar_num *= 135;
2301 p_dec->fmt_out.video.i_sar_den *= 136;
2303 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2306 if( !p_dec->fmt_out.video.i_sar_num ||
2307 !p_dec->fmt_out.video.i_sar_den )
2309 p_dec->fmt_out.video.i_sar_num = 1;
2310 p_dec->fmt_out.video.i_sar_den = 1;
2313 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2314 &p_dec->fmt_out.video.i_sar_den,
2315 p_dec->fmt_out.video.i_sar_num,
2316 p_dec->fmt_out.video.i_sar_den, 50000 );
2318 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2319 p_owner->video = p_dec->fmt_out.video;
2321 vlc_mutex_lock( &p_owner->lock );
2323 DecoderFlushBuffering( p_dec );
2325 p_vout = p_owner->p_vout;
2326 p_owner->p_vout = NULL;
2327 vlc_mutex_unlock( &p_owner->lock );
2329 p_vout = input_resource_RequestVout( p_owner->p_input->p->p_resource,
2330 p_vout, &p_dec->fmt_out.video, true );
2332 vlc_mutex_lock( &p_owner->lock );
2333 p_owner->p_vout = p_vout;
2335 DecoderUpdateFormatLocked( p_dec );
2337 vlc_mutex_unlock( &p_owner->lock );
2339 input_SendEventVout( p_owner->p_input );
2340 if( p_vout == NULL )
2342 msg_Err( p_dec, "failed to create video output" );
2343 p_dec->b_error = true;
2347 if( p_owner->video.i_rmask )
2348 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2349 if( p_owner->video.i_gmask )
2350 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2351 if( p_owner->video.i_bmask )
2352 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2355 /* Get a new picture
2359 picture_t *p_picture;
2361 if( p_dec->b_die || p_dec->b_error )
2364 /* The video filter chain required that there is always 1 free buffer
2365 * that it will use as temporary one. It will release the temporary
2366 * buffer once its work is done, so this check is safe even if we don't
2367 * lock around both count() and create().
2369 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2371 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2376 if( DecoderIsFlushing( p_dec ) )
2380 DecoderSignalBuffering( p_dec, true );
2382 /* Check the decoder doesn't leak pictures */
2383 vout_FixLeaks( p_owner->p_vout, false );
2385 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2386 msleep( VOUT_OUTMEM_SLEEP );
2390 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2392 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2395 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2397 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2400 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2402 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2405 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2407 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2408 vout_thread_t *p_vout = NULL;
2409 subpicture_t *p_subpic;
2410 int i_attempts = 30;
2412 while( i_attempts-- )
2414 if( p_dec->b_die || p_dec->b_error )
2417 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2421 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2426 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2430 if( p_owner->p_spu_vout != p_vout )
2432 vlc_mutex_lock( &p_owner->lock );
2434 DecoderFlushBuffering( p_dec );
2436 vlc_mutex_unlock( &p_owner->lock );
2438 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_REGISTER,
2439 &p_owner->i_spu_channel );
2440 p_owner->i_spu_order = 0;
2441 p_owner->p_spu_vout = p_vout;
2444 p_subpic = subpicture_New();
2447 p_subpic->i_channel = p_owner->i_spu_channel;
2448 p_subpic->i_order = p_owner->i_spu_order++;
2449 p_subpic->b_subtitle = true;
2452 vlc_object_release( p_vout );
2457 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2459 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2460 vout_thread_t *p_vout = NULL;
2462 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2463 if( !p_vout || p_owner->p_spu_vout != p_vout )
2466 vlc_object_release( p_vout );
2467 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2471 subpicture_Delete( p_subpic );
2473 vlc_object_release( p_vout );