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 *, int, sout_instance_t *p_sout );
56 static void DeleteDecoder( decoder_t * );
58 static void *DecoderThread( vlc_object_t * );
59 static void DecoderProcess( decoder_t *, block_t * );
60 static void DecoderError( decoder_t *p_dec, block_t *p_block );
61 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
62 static void DecoderFlush( decoder_t * );
63 static void DecoderSignalBuffering( decoder_t *, bool );
64 static void DecoderFlushBuffering( decoder_t * );
66 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
68 /* Buffers allocation callbacks for the decoders */
69 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
70 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
72 static picture_t *vout_new_buffer( decoder_t * );
73 static void vout_del_buffer( decoder_t *, picture_t * );
74 static void vout_link_picture( decoder_t *, picture_t * );
75 static void vout_unlink_picture( decoder_t *, picture_t * );
77 static subpicture_t *spu_new_buffer( decoder_t * );
78 static void spu_del_buffer( decoder_t *, subpicture_t * );
80 struct decoder_owner_sys_t
82 int64_t i_preroll_end;
84 input_thread_t *p_input;
85 input_clock_t *p_clock;
88 vout_thread_t *p_spu_vout;
92 sout_instance_t *p_sout;
93 sout_packetizer_input_t *p_sout_input;
95 /* Some decoders require already packetized data (ie. not truncated) */
96 decoder_t *p_packetizer;
98 /* Current format in use by the output */
100 audio_format_t audio;
104 bool b_fmt_description;
105 es_format_t fmt_description;
106 vlc_meta_t *p_description;
109 block_fifo_t *p_fifo;
111 /* Lock for communication with decoder thread */
113 vlc_cond_t wait_request;
114 vlc_cond_t wait_acknowledge;
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 )
242 return VLC_TS_INVALID;
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 dialog_Fatal( p_input, _("Streaming / Transcoding failed"), "%s",
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 dialog_Fatal( p_input, _("Streaming / Transcoding failed"), "%s",
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;
307 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
309 if( p_dec->fmt_out.i_cat == AUDIO_ES )
310 i_priority = VLC_THREAD_PRIORITY_AUDIO;
312 i_priority = VLC_THREAD_PRIORITY_VIDEO;
314 /* Spawn the decoder thread */
315 if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
317 msg_Err( p_dec, "cannot spawn decoder thread" );
318 module_unneed( p_dec, p_dec->p_module );
319 DeleteDecoder( p_dec );
320 vlc_object_release( p_dec );
328 * Kills a decoder thread and waits until it's finished
330 * \param p_input the input thread
331 * \param p_es the es descriptor
334 void input_DecoderDelete( decoder_t *p_dec )
336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
338 vlc_object_kill( p_dec );
340 /* Make sure we aren't paused/buffering/waiting anymore */
341 vlc_mutex_lock( &p_owner->lock );
342 const bool b_was_paused = p_owner->b_paused;
343 p_owner->b_paused = false;
344 p_owner->b_buffering = false;
345 p_owner->b_flushing = true;
346 vlc_cond_signal( &p_owner->wait_request );
347 vlc_mutex_unlock( &p_owner->lock );
349 vlc_thread_join( p_dec );
350 p_owner->b_paused = b_was_paused;
352 module_unneed( p_dec, p_dec->p_module );
355 if( p_dec->p_owner->cc.b_supported )
358 for( i = 0; i < 4; i++ )
359 input_DecoderSetCcState( p_dec, false, i );
362 /* Delete decoder configuration */
363 DeleteDecoder( p_dec );
365 /* Delete the decoder */
366 vlc_object_release( p_dec );
370 * Put a block_t in the decoder's fifo.
371 * Thread-safe w.r.t. the decoder. May be a cancellation point.
373 * \param p_dec the decoder object
374 * \param p_block the data block
376 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
378 decoder_owner_sys_t *p_owner = p_dec->p_owner;
382 /* The fifo is not consummed when buffering and so will
384 * There is no need to lock as b_buffering is never modify
385 * inside decoder thread. */
386 if( !p_owner->b_buffering )
387 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
389 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
391 /* FIXME: ideally we would check the time amount of data
392 * in the FIFO instead of its size. */
393 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
394 "consumed quickly enough), resetting fifo!" );
395 block_FifoEmpty( p_owner->p_fifo );
398 block_FifoPut( p_owner->p_fifo, p_block );
401 bool input_DecoderIsEmpty( decoder_t * p_dec )
403 assert( !p_dec->p_owner->b_buffering );
405 /* FIXME that's not really true */
406 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
409 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
411 decoder_owner_sys_t *p_owner = p_dec->p_owner;
414 vlc_mutex_lock( &p_owner->lock );
415 for( i = 0; i < 4; i++ )
416 pb_present[i] = p_owner->cc.pb_present[i];
417 vlc_mutex_unlock( &p_owner->lock );
419 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
421 decoder_owner_sys_t *p_owner = p_dec->p_owner;
423 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
425 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
430 static const vlc_fourcc_t fcc[4] = {
431 VLC_FOURCC('c', 'c', '1', ' '),
432 VLC_FOURCC('c', 'c', '2', ' '),
433 VLC_FOURCC('c', 'c', '3', ' '),
434 VLC_FOURCC('c', 'c', '4', ' '),
439 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
440 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
443 msg_Err( p_dec, "could not create decoder" );
444 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
445 _("VLC could not open the decoder module.") );
448 else if( !p_cc->p_module )
450 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
451 DeleteDecoder( p_cc );
452 vlc_object_release( p_cc );
455 p_cc->p_owner->p_clock = p_owner->p_clock;
457 vlc_mutex_lock( &p_owner->lock );
458 p_owner->cc.pp_decoder[i_channel] = p_cc;
459 vlc_mutex_unlock( &p_owner->lock );
465 vlc_mutex_lock( &p_owner->lock );
466 p_cc = p_owner->cc.pp_decoder[i_channel];
467 p_owner->cc.pp_decoder[i_channel] = NULL;
468 vlc_mutex_unlock( &p_owner->lock );
472 vlc_object_kill( p_cc );
473 module_unneed( p_cc, p_cc->p_module );
474 DeleteDecoder( p_cc );
475 vlc_object_release( p_cc );
480 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
482 decoder_owner_sys_t *p_owner = p_dec->p_owner;
485 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
488 vlc_mutex_lock( &p_owner->lock );
489 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
490 vlc_mutex_unlock( &p_owner->lock );
494 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
496 decoder_owner_sys_t *p_owner = p_dec->p_owner;
498 vlc_mutex_lock( &p_owner->lock );
500 assert( !p_owner->b_paused || !b_paused );
502 p_owner->b_paused = b_paused;
503 p_owner->pause.i_date = i_date;
504 p_owner->pause.i_ignore = 0;
505 vlc_cond_signal( &p_owner->wait_request );
507 DecoderOutputChangePause( p_dec, b_paused, i_date );
509 vlc_mutex_unlock( &p_owner->lock );
512 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
514 decoder_owner_sys_t *p_owner = p_dec->p_owner;
516 vlc_mutex_lock( &p_owner->lock );
517 p_owner->i_ts_delay = i_delay;
518 vlc_mutex_unlock( &p_owner->lock );
521 void input_DecoderStartBuffering( decoder_t *p_dec )
523 decoder_owner_sys_t *p_owner = p_dec->p_owner;
525 vlc_mutex_lock( &p_owner->lock );
527 DecoderFlush( p_dec );
529 p_owner->buffer.b_first = true;
530 p_owner->buffer.b_full = false;
531 p_owner->buffer.i_count = 0;
533 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
534 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
536 p_owner->buffer.p_picture = NULL;
537 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
539 p_owner->buffer.p_subpic = NULL;
540 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
542 p_owner->buffer.p_audio = NULL;
543 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
545 p_owner->buffer.p_block = NULL;
546 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
549 p_owner->b_buffering = true;
551 vlc_cond_signal( &p_owner->wait_request );
553 vlc_mutex_unlock( &p_owner->lock );
556 void input_DecoderStopBuffering( decoder_t *p_dec )
558 decoder_owner_sys_t *p_owner = p_dec->p_owner;
560 vlc_mutex_lock( &p_owner->lock );
562 p_owner->b_buffering = false;
564 vlc_cond_signal( &p_owner->wait_request );
566 vlc_mutex_unlock( &p_owner->lock );
569 void input_DecoderWaitBuffering( decoder_t *p_dec )
571 decoder_owner_sys_t *p_owner = p_dec->p_owner;
573 vlc_mutex_lock( &p_owner->lock );
575 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
577 block_FifoWake( p_owner->p_fifo );
578 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
581 vlc_mutex_unlock( &p_owner->lock );
584 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
586 decoder_owner_sys_t *p_owner = p_dec->p_owner;
590 vlc_mutex_lock( &p_owner->lock );
591 if( p_dec->fmt_out.i_cat == VIDEO_ES )
593 if( p_owner->b_paused && p_owner->p_vout )
595 vout_NextPicture( p_owner->p_vout, pi_duration );
596 p_owner->pause.i_ignore++;
597 vlc_cond_signal( &p_owner->wait_request );
602 /* TODO subtitle should not be flushed */
603 DecoderFlush( p_dec );
605 vlc_mutex_unlock( &p_owner->lock );
608 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
610 decoder_owner_sys_t *p_owner = p_dec->p_owner;
613 vlc_mutex_lock( &p_owner->lock );
614 b_changed = p_owner->b_fmt_description;
618 es_format_Copy( p_fmt, &p_owner->fmt_description );
623 if( p_owner->p_description )
625 *pp_meta = vlc_meta_New();
627 vlc_meta_Merge( *pp_meta, p_owner->p_description );
630 p_owner->b_fmt_description = false;
632 vlc_mutex_unlock( &p_owner->lock );
636 /*****************************************************************************
638 *****************************************************************************/
639 static int DecoderGetInputAttachments( decoder_t *p_dec,
640 input_attachment_t ***ppp_attachment,
643 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
644 ppp_attachment, pi_attachment );
646 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
648 decoder_owner_sys_t *p_owner = p_dec->p_owner;
650 vlc_mutex_lock( &p_owner->lock );
651 if( p_owner->b_buffering || p_owner->b_paused )
652 i_ts = VLC_TS_INVALID;
653 vlc_mutex_unlock( &p_owner->lock );
655 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
658 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
659 return VLC_TS_INVALID;
663 static int DecoderGetDisplayRate( decoder_t *p_dec )
665 decoder_owner_sys_t *p_owner = p_dec->p_owner;
667 if( !p_owner->p_clock )
668 return INPUT_RATE_DEFAULT;
669 return input_clock_GetRate( p_owner->p_clock );
673 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
675 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
676 "VLC probably does not support this sound or video format.",
678 dialog_Fatal( p_dec, _("No suitable decoder module"),
679 _("VLC does not support the audio or video format \"%4.4s\". "
680 "Unfortunately there is no way for you to fix this."),
686 * Create a decoder object
688 * \param p_input the input thread
689 * \param p_es the es descriptor
690 * \param i_object_type Object type as define in include/vlc_objects.h
691 * \return the decoder object
693 static decoder_t * CreateDecoder( input_thread_t *p_input,
694 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
697 decoder_owner_sys_t *p_owner;
698 es_format_t null_es_format;
702 p_dec = vlc_object_create( p_input, i_object_type );
706 p_dec->pf_decode_audio = NULL;
707 p_dec->pf_decode_video = NULL;
708 p_dec->pf_decode_sub = NULL;
709 p_dec->pf_get_cc = NULL;
710 p_dec->pf_packetize = NULL;
712 /* Initialize the decoder */
713 p_dec->p_module = NULL;
715 memset( &null_es_format, 0, sizeof(es_format_t) );
716 es_format_Copy( &p_dec->fmt_in, fmt );
717 es_format_Copy( &p_dec->fmt_out, &null_es_format );
719 p_dec->p_description = NULL;
721 /* Allocate our private structure for the decoder */
722 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
723 if( p_dec->p_owner == NULL )
725 vlc_object_release( p_dec );
728 p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
729 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
730 p_dec->p_owner->p_input = p_input;
731 p_dec->p_owner->p_aout = NULL;
732 p_dec->p_owner->p_aout_input = NULL;
733 p_dec->p_owner->p_vout = NULL;
734 p_dec->p_owner->p_spu_vout = NULL;
735 p_dec->p_owner->i_spu_channel = 0;
736 p_dec->p_owner->i_spu_order = 0;
737 p_dec->p_owner->p_sout = p_sout;
738 p_dec->p_owner->p_sout_input = NULL;
739 p_dec->p_owner->p_packetizer = NULL;
742 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
744 free( p_dec->p_owner );
745 vlc_object_release( p_dec );
749 /* Set buffers allocation callbacks for the decoders */
750 p_dec->pf_aout_buffer_new = aout_new_buffer;
751 p_dec->pf_aout_buffer_del = aout_del_buffer;
752 p_dec->pf_vout_buffer_new = vout_new_buffer;
753 p_dec->pf_vout_buffer_del = vout_del_buffer;
754 p_dec->pf_picture_link = vout_link_picture;
755 p_dec->pf_picture_unlink = vout_unlink_picture;
756 p_dec->pf_spu_buffer_new = spu_new_buffer;
757 p_dec->pf_spu_buffer_del = spu_del_buffer;
759 p_dec->pf_get_attachments = DecoderGetInputAttachments;
760 p_dec->pf_get_display_date = DecoderGetDisplayDate;
761 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
763 vlc_object_attach( p_dec, p_input );
765 /* Find a suitable decoder/packetizer module */
766 if( i_object_type == VLC_OBJECT_DECODER )
767 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
769 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
771 /* Check if decoder requires already packetized data */
772 if( i_object_type == VLC_OBJECT_DECODER &&
773 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
775 p_dec->p_owner->p_packetizer =
776 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
777 if( p_dec->p_owner->p_packetizer )
779 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
782 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
785 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
787 p_dec->p_owner->p_packetizer->p_module =
788 module_need( p_dec->p_owner->p_packetizer,
789 "packetizer", "$packetizer", false );
791 if( !p_dec->p_owner->p_packetizer->p_module )
793 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
794 vlc_object_detach( p_dec->p_owner->p_packetizer );
795 vlc_object_release( p_dec->p_owner->p_packetizer );
800 /* Copy ourself the input replay gain */
801 if( fmt->i_cat == AUDIO_ES )
803 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
805 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
807 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
808 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
810 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
812 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
813 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
819 vlc_mutex_init( &p_owner->lock );
820 vlc_cond_init( &p_owner->wait_request );
821 vlc_cond_init( &p_owner->wait_acknowledge );
823 p_owner->b_fmt_description = false;
824 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
825 p_owner->p_description = NULL;
827 p_owner->b_paused = false;
828 p_owner->pause.i_date = VLC_TS_INVALID;
829 p_owner->pause.i_ignore = 0;
831 p_owner->b_buffering = false;
832 p_owner->buffer.b_first = true;
833 p_owner->buffer.b_full = false;
834 p_owner->buffer.i_count = 0;
835 p_owner->buffer.p_picture = NULL;
836 p_owner->buffer.p_subpic = NULL;
837 p_owner->buffer.p_audio = NULL;
838 p_owner->buffer.p_block = NULL;
840 p_owner->b_flushing = false;
843 p_owner->cc.b_supported = false;
844 if( i_object_type == VLC_OBJECT_DECODER )
846 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
847 p_owner->cc.b_supported = true;
848 if( p_dec->pf_get_cc )
849 p_owner->cc.b_supported = true;
852 for( i = 0; i < 4; i++ )
854 p_owner->cc.pb_present[i] = false;
855 p_owner->cc.pp_decoder[i] = NULL;
857 p_owner->i_ts_delay = 0;
862 * The decoding main loop
864 * \param p_dec the decoder
866 static void *DecoderThread( vlc_object_t *p_this )
868 decoder_t *p_dec = (decoder_t *)p_this;
869 decoder_owner_sys_t *p_owner = p_dec->p_owner;
871 /* The decoder's main loop */
874 block_t *p_block = block_FifoGet( p_owner->p_fifo );
876 /* Make sure there is no cancellation point other than this one^^.
877 * If you need one, be sure to push cleanup of p_block. */
878 DecoderSignalBuffering( p_dec, p_block == NULL );
882 int canc = vlc_savecancel();
885 DecoderError( p_dec, p_block );
887 DecoderProcess( p_dec, p_block );
889 vlc_restorecancel( canc );
895 static block_t *DecoderBlockFlushNew()
897 block_t *p_null = block_Alloc( 128 );
901 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
902 BLOCK_FLAG_CORRUPTED |
903 BLOCK_FLAG_CORE_FLUSH;
904 memset( p_null->p_buffer, 0, p_null->i_buffer );
909 static void DecoderFlush( decoder_t *p_dec )
911 decoder_owner_sys_t *p_owner = p_dec->p_owner;
913 vlc_assert_locked( &p_owner->lock );
916 block_FifoEmpty( p_owner->p_fifo );
918 /* Monitor for flush end */
919 p_owner->b_flushing = true;
920 vlc_cond_signal( &p_owner->wait_request );
922 /* Send a special block */
923 block_t *p_null = DecoderBlockFlushNew();
926 input_DecoderDecode( p_dec, p_null, false );
929 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
930 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
933 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
935 decoder_owner_sys_t *p_owner = p_dec->p_owner;
937 vlc_mutex_lock( &p_owner->lock );
939 if( p_owner->b_buffering )
942 p_owner->buffer.b_full = true;
943 vlc_cond_signal( &p_owner->wait_acknowledge );
946 vlc_mutex_unlock( &p_owner->lock );
949 static bool DecoderIsFlushing( decoder_t *p_dec )
951 decoder_owner_sys_t *p_owner = p_dec->p_owner;
954 vlc_mutex_lock( &p_owner->lock );
956 b_flushing = p_owner->b_flushing;
958 vlc_mutex_unlock( &p_owner->lock );
963 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
965 decoder_owner_sys_t *p_owner = p_dec->p_owner;
967 vlc_assert_locked( &p_owner->lock );
969 while( !p_owner->b_flushing )
971 if( p_owner->b_paused )
973 if( p_owner->b_buffering && !p_owner->buffer.b_full )
975 if( p_owner->pause.i_ignore > 0 )
977 p_owner->pause.i_ignore--;
983 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
986 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
990 *pb_reject = p_owner->b_flushing;
993 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
995 decoder_owner_sys_t *p_owner = p_dec->p_owner;
997 vlc_assert_locked( &p_owner->lock );
999 /* XXX only audio and video output have to be paused.
1000 * - for sout it is useless
1001 * - for subs, it is done by the vout
1003 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1005 if( p_owner->p_aout && p_owner->p_aout_input )
1006 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1009 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1011 if( p_owner->p_vout )
1012 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1015 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1017 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1018 *pi_preroll = INT64_MAX;
1019 else if( p->i_dts > VLC_TS_INVALID )
1020 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1021 else if( p->i_pts > VLC_TS_INVALID )
1022 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1025 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1027 mtime_t current_date = mdate();
1029 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1030 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1032 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1033 * for teletext streams. */
1034 return current_date + 400000;
1039 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1040 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1042 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1043 input_clock_t *p_clock = p_owner->p_clock;
1045 vlc_assert_locked( &p_owner->lock );
1047 const mtime_t i_es_delay = p_owner->i_ts_delay;
1051 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1054 if( *pi_ts0 > VLC_TS_INVALID )
1056 *pi_ts0 += i_es_delay;
1057 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1058 *pi_ts1 += i_es_delay;
1059 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1060 *pi_ts0 = VLC_TS_INVALID;
1064 i_rate = input_clock_GetRate( p_clock );
1067 /* Do not create ephemere data because of rounding errors */
1068 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1072 *pi_duration = ( *pi_duration * i_rate +
1073 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1080 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1081 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1087 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1088 int *pi_played_sum, int *pi_lost_sum )
1090 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1091 aout_instance_t *p_aout = p_owner->p_aout;
1092 aout_input_t *p_aout_input = p_owner->p_aout_input;
1095 if( p_audio->start_date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1097 msg_Warn( p_dec, "non-dated audio buffer received" );
1099 aout_BufferFree( p_audio );
1104 vlc_mutex_lock( &p_owner->lock );
1106 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1108 p_audio->p_next = NULL;
1110 *p_owner->buffer.pp_audio_next = p_audio;
1111 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1113 p_owner->buffer.i_count++;
1114 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1115 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1117 p_owner->buffer.b_full = true;
1118 vlc_cond_signal( &p_owner->wait_acknowledge );
1124 bool b_has_more = false;
1126 DecoderWaitUnblock( p_dec, &b_reject );
1128 if( p_owner->b_buffering )
1130 vlc_mutex_unlock( &p_owner->lock );
1135 if( p_owner->buffer.p_audio )
1137 p_audio = p_owner->buffer.p_audio;
1139 p_owner->buffer.p_audio = p_audio->p_next;
1140 p_owner->buffer.i_count--;
1142 b_has_more = p_owner->buffer.p_audio != NULL;
1144 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1148 const bool b_dated = p_audio->start_date > VLC_TS_INVALID;
1149 int i_rate = INPUT_RATE_DEFAULT;
1151 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1152 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1154 vlc_mutex_unlock( &p_owner->lock );
1156 if( !p_aout || !p_aout_input ||
1157 p_audio->start_date <= VLC_TS_INVALID ||
1158 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1159 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1162 /* Do not wait against unprotected date */
1163 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1164 while( !b_reject && i_deadline > mdate() )
1166 vlc_mutex_lock( &p_owner->lock );
1167 if( p_owner->b_flushing || p_dec->b_die )
1170 vlc_mutex_unlock( &p_owner->lock );
1173 vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock, i_deadline );
1174 vlc_mutex_unlock( &p_owner->lock );
1179 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1180 *pi_played_sum += 1;
1181 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1186 msg_Warn( p_aout, "received buffer in the future" );
1188 msg_Warn( p_dec, "non-dated audio buffer received" );
1191 aout_BufferFree( p_audio );
1197 vlc_mutex_lock( &p_owner->lock );
1198 if( !p_owner->buffer.p_audio )
1200 vlc_mutex_unlock( &p_owner->lock );
1206 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1208 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1209 input_thread_t *p_input = p_owner->p_input;
1210 aout_buffer_t *p_aout_buf;
1215 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1217 aout_instance_t *p_aout = p_owner->p_aout;
1218 aout_input_t *p_aout_input = p_owner->p_aout_input;
1222 /* It prevent freezing VLC in case of broken decoder */
1223 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1225 block_Release( p_block );
1230 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1231 p_aout_buf->start_date < p_owner->i_preroll_end )
1233 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1237 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1239 msg_Dbg( p_dec, "End of audio preroll" );
1240 if( p_owner->p_aout && p_owner->p_aout_input )
1241 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1243 p_owner->i_preroll_end = VLC_TS_INVALID;
1246 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1249 /* Update ugly stat */
1250 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1252 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1254 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1256 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1258 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1261 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1264 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1266 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1272 assert( p_dec_cc->pf_get_cc != NULL );
1274 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1275 if( !p_owner->cc.b_supported )
1278 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1282 vlc_mutex_lock( &p_owner->lock );
1283 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1285 p_owner->cc.pb_present[i] |= pb_present[i];
1286 if( p_owner->cc.pp_decoder[i] )
1290 for( i = 0; i < 4; i++ )
1292 if( !p_owner->cc.pp_decoder[i] )
1295 if( i_cc_decoder > 1 )
1296 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1298 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1301 vlc_mutex_unlock( &p_owner->lock );
1304 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1305 int *pi_played_sum, int *pi_lost_sum )
1307 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1308 vout_thread_t *p_vout = p_owner->p_vout;
1309 bool b_first_buffered;
1311 if( p_picture->date <= VLC_TS_INVALID )
1313 msg_Warn( p_vout, "non-dated video buffer received" );
1315 vout_DropPicture( p_vout, p_picture );
1320 vlc_mutex_lock( &p_owner->lock );
1322 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1324 p_picture->p_next = NULL;
1326 *p_owner->buffer.pp_picture_next = p_picture;
1327 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1329 p_owner->buffer.i_count++;
1330 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1331 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1333 p_owner->buffer.b_full = true;
1334 vlc_cond_signal( &p_owner->wait_acknowledge );
1337 b_first_buffered = p_owner->buffer.p_picture != NULL;
1339 for( ;; b_first_buffered = false )
1341 bool b_has_more = false;
1345 DecoderWaitUnblock( p_dec, &b_reject );
1347 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1349 vlc_mutex_unlock( &p_owner->lock );
1352 bool b_buffering_first = p_owner->b_buffering;
1355 if( p_owner->buffer.p_picture )
1357 p_picture = p_owner->buffer.p_picture;
1359 p_owner->buffer.p_picture = p_picture->p_next;
1360 p_owner->buffer.i_count--;
1362 b_has_more = p_owner->buffer.p_picture != NULL;
1364 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1368 if( b_buffering_first )
1370 assert( p_owner->buffer.b_first );
1371 assert( !p_owner->buffer.i_count );
1372 msg_Dbg( p_dec, "Received first picture" );
1373 p_owner->buffer.b_first = false;
1374 p_picture->b_force = true;
1377 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1378 int i_rate = INPUT_RATE_DEFAULT;
1379 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1380 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1382 vlc_mutex_unlock( &p_owner->lock );
1385 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1390 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1392 /* Be sure to not display old picture after our own */
1393 vout_Flush( p_vout, p_picture->date );
1394 p_owner->i_last_rate = i_rate;
1396 vout_DisplayPicture( p_vout, p_picture );
1401 msg_Warn( p_vout, "early picture skipped" );
1403 msg_Warn( p_vout, "non-dated video buffer received" );
1406 vout_DropPicture( p_vout, p_picture );
1410 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1412 *pi_played_sum += i_tmp_display;
1413 *pi_lost_sum += i_tmp_lost;
1415 if( !b_has_more || b_buffering_first )
1418 vlc_mutex_lock( &p_owner->lock );
1419 if( !p_owner->buffer.p_picture )
1421 vlc_mutex_unlock( &p_owner->lock );
1427 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1429 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1430 input_thread_t *p_input = p_owner->p_input;
1434 int i_displayed = 0;
1436 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1438 vout_thread_t *p_vout = p_owner->p_vout;
1441 /* It prevent freezing VLC in case of broken decoder */
1442 vout_DropPicture( p_vout, p_pic );
1444 block_Release( p_block );
1450 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1452 vout_DropPicture( p_vout, p_pic );
1456 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1458 msg_Dbg( p_dec, "End of video preroll" );
1460 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1462 p_owner->i_preroll_end = VLC_TS_INVALID;
1465 if( p_dec->pf_get_cc &&
1466 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1467 DecoderGetCc( p_dec, p_dec );
1469 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1471 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1473 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1475 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1477 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1480 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1483 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1487 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1490 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1491 vout_thread_t *p_vout = p_owner->p_spu_vout;
1494 if( p_subpic->i_start <= VLC_TS_INVALID )
1496 msg_Warn( p_dec, "non-dated spu buffer received" );
1497 subpicture_Delete( p_subpic );
1502 vlc_mutex_lock( &p_owner->lock );
1504 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1506 p_subpic->p_next = NULL;
1508 *p_owner->buffer.pp_subpic_next = p_subpic;
1509 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1511 p_owner->buffer.i_count++;
1512 /* XXX it is important to be full after the first one */
1513 if( p_owner->buffer.i_count > 0 )
1515 p_owner->buffer.b_full = true;
1516 vlc_cond_signal( &p_owner->wait_acknowledge );
1522 bool b_has_more = false;
1524 DecoderWaitUnblock( p_dec, &b_reject );
1526 if( p_owner->b_buffering )
1528 vlc_mutex_unlock( &p_owner->lock );
1533 if( p_owner->buffer.p_subpic )
1535 p_subpic = p_owner->buffer.p_subpic;
1537 p_owner->buffer.p_subpic = p_subpic->p_next;
1538 p_owner->buffer.i_count--;
1540 b_has_more = p_owner->buffer.p_subpic != NULL;
1542 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1546 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1547 NULL, INT64_MAX, b_telx );
1549 vlc_mutex_unlock( &p_owner->lock );
1552 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1554 subpicture_Delete( p_subpic );
1558 vlc_mutex_lock( &p_owner->lock );
1559 if( !p_owner->buffer.p_subpic )
1561 vlc_mutex_unlock( &p_owner->lock );
1567 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1570 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1572 assert( p_owner->p_clock );
1573 assert( !p_sout_block->p_next );
1575 vlc_mutex_lock( &p_owner->lock );
1577 if( p_owner->b_buffering || p_owner->buffer.p_block )
1579 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1581 p_owner->buffer.i_count++;
1582 /* XXX it is important to be full after the first one */
1583 if( p_owner->buffer.i_count > 0 )
1585 p_owner->buffer.b_full = true;
1586 vlc_cond_signal( &p_owner->wait_acknowledge );
1592 bool b_has_more = false;
1594 DecoderWaitUnblock( p_dec, &b_reject );
1596 if( p_owner->b_buffering )
1598 vlc_mutex_unlock( &p_owner->lock );
1603 if( p_owner->buffer.p_block )
1605 p_sout_block = p_owner->buffer.p_block;
1607 p_owner->buffer.p_block = p_sout_block->p_next;
1608 p_owner->buffer.i_count--;
1610 b_has_more = p_owner->buffer.p_block != NULL;
1612 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1614 p_sout_block->p_next = NULL;
1616 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1617 &p_sout_block->i_length,
1618 &p_sout_block->i_rate, INT64_MAX, b_telx );
1620 vlc_mutex_unlock( &p_owner->lock );
1623 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1625 block_Release( p_sout_block );
1629 vlc_mutex_lock( &p_owner->lock );
1630 if( !p_owner->buffer.p_block )
1632 vlc_mutex_unlock( &p_owner->lock );
1639 static void DecoderFlushBuffering( decoder_t *p_dec )
1641 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1643 vlc_assert_locked( &p_owner->lock );
1645 while( p_owner->buffer.p_picture )
1647 picture_t *p_picture = p_owner->buffer.p_picture;
1649 p_owner->buffer.p_picture = p_picture->p_next;
1650 p_owner->buffer.i_count--;
1652 if( p_owner->p_vout )
1653 vout_DropPicture( p_owner->p_vout, p_picture );
1655 if( !p_owner->buffer.p_picture )
1656 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1658 while( p_owner->buffer.p_audio )
1660 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1662 p_owner->buffer.p_audio = p_audio->p_next;
1663 p_owner->buffer.i_count--;
1665 aout_BufferFree( p_audio );
1667 if( !p_owner->buffer.p_audio )
1668 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1670 while( p_owner->buffer.p_subpic )
1672 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1674 p_owner->buffer.p_subpic = p_subpic->p_next;
1675 p_owner->buffer.i_count--;
1677 subpicture_Delete( p_subpic );
1679 if( !p_owner->buffer.p_subpic )
1680 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1682 if( p_owner->buffer.p_block )
1684 block_ChainRelease( p_owner->buffer.p_block );
1686 p_owner->buffer.i_count = 0;
1687 p_owner->buffer.p_block = NULL;
1688 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1692 /* This function process a block for sout
1694 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1696 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1697 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1698 block_t *p_sout_block;
1700 while( ( p_sout_block =
1701 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1703 if( !p_owner->p_sout_input )
1705 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1707 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1708 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1709 if( p_dec->fmt_in.psz_language )
1711 if( p_owner->sout.psz_language )
1712 free( p_owner->sout.psz_language );
1713 p_owner->sout.psz_language =
1714 strdup( p_dec->fmt_in.psz_language );
1717 p_owner->p_sout_input =
1718 sout_InputNew( p_owner->p_sout,
1721 if( p_owner->p_sout_input == NULL )
1723 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1724 (char *)&p_owner->sout.i_codec );
1725 p_dec->b_error = true;
1727 while( p_sout_block )
1729 block_t *p_next = p_sout_block->p_next;
1730 block_Release( p_sout_block );
1731 p_sout_block = p_next;
1737 while( p_sout_block )
1739 block_t *p_next = p_sout_block->p_next;
1741 p_sout_block->p_next = NULL;
1743 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1745 p_sout_block = p_next;
1750 /* This function process a video block
1752 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1754 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1756 if( p_owner->p_packetizer )
1758 block_t *p_packetized_block;
1759 decoder_t *p_packetizer = p_owner->p_packetizer;
1761 while( (p_packetized_block =
1762 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1764 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1766 es_format_Clean( &p_dec->fmt_in );
1767 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1769 if( p_packetizer->pf_get_cc )
1770 DecoderGetCc( p_dec, p_packetizer );
1772 while( p_packetized_block )
1774 block_t *p_next = p_packetized_block->p_next;
1775 p_packetized_block->p_next = NULL;
1777 DecoderDecodeVideo( p_dec, p_packetized_block );
1779 p_packetized_block = p_next;
1782 /* The packetizer does not output a block that tell the decoder to flush
1786 block_t *p_null = DecoderBlockFlushNew();
1788 DecoderDecodeVideo( p_dec, p_null );
1793 DecoderDecodeVideo( p_dec, p_block );
1796 if( b_flush && p_owner->p_vout )
1797 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1800 /* This function process a audio block
1802 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1804 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1806 if( p_owner->p_packetizer )
1808 block_t *p_packetized_block;
1809 decoder_t *p_packetizer = p_owner->p_packetizer;
1811 while( (p_packetized_block =
1812 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1814 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1816 es_format_Clean( &p_dec->fmt_in );
1817 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1820 while( p_packetized_block )
1822 block_t *p_next = p_packetized_block->p_next;
1823 p_packetized_block->p_next = NULL;
1825 DecoderDecodeAudio( p_dec, p_packetized_block );
1827 p_packetized_block = p_next;
1830 /* The packetizer does not output a block that tell the decoder to flush
1834 block_t *p_null = DecoderBlockFlushNew();
1836 DecoderDecodeAudio( p_dec, p_null );
1841 DecoderDecodeAudio( p_dec, p_block );
1844 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1845 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1848 /* This function process a subtitle block
1850 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1852 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1853 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1855 input_thread_t *p_input = p_owner->p_input;
1856 vout_thread_t *p_vout;
1857 subpicture_t *p_spu;
1859 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1861 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1862 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1863 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1865 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1866 if( p_vout && p_owner->p_spu_vout == p_vout )
1868 /* Preroll does not work very well with subtitle */
1869 if( p_spu->i_start > VLC_TS_INVALID &&
1870 p_spu->i_start < p_owner->i_preroll_end &&
1871 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1873 subpicture_Delete( p_spu );
1877 DecoderPlaySpu( p_dec, p_spu, b_telx );
1882 subpicture_Delete( p_spu );
1885 vlc_object_release( p_vout );
1888 if( b_flush && p_owner->p_spu_vout )
1890 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1892 if( p_vout && p_owner->p_spu_vout == p_vout )
1893 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1894 p_owner->i_spu_channel );
1897 vlc_object_release( p_vout );
1902 static void DecoderProcessOnFlush( decoder_t *p_dec )
1904 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1906 vlc_mutex_lock( &p_owner->lock );
1907 DecoderFlushBuffering( p_dec );
1909 if( p_owner->b_flushing )
1911 p_owner->b_flushing = false;
1912 vlc_cond_signal( &p_owner->wait_acknowledge );
1914 vlc_mutex_unlock( &p_owner->lock );
1920 * \param p_dec the decoder object
1921 * \param p_block the block to decode
1922 * \return VLC_SUCCESS or an error code
1924 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1926 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1927 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1929 if( p_block && p_block->i_buffer <= 0 )
1931 assert( !b_flush_request );
1932 block_Release( p_block );
1937 if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
1940 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1942 DecoderProcessSout( p_dec, p_block );
1947 bool b_flush = false;
1951 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1952 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1954 b_flush = !b_flushing && b_flush_request;
1956 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1959 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1961 DecoderProcessAudio( p_dec, p_block, b_flush );
1963 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1965 DecoderProcessVideo( p_dec, p_block, b_flush );
1967 else if( p_dec->fmt_out.i_cat == SPU_ES )
1969 DecoderProcessSpu( p_dec, p_block, b_flush );
1973 msg_Err( p_dec, "unknown ES format" );
1974 p_dec->b_error = true;
1979 if( b_flush_request )
1980 DecoderProcessOnFlush( p_dec );
1983 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1985 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1989 block_Release( p_block );
1991 if( b_flush_request )
1992 DecoderProcessOnFlush( p_dec );
1997 * Destroys a decoder object
1999 * \param p_dec the decoder object
2002 static void DeleteDecoder( decoder_t * p_dec )
2004 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2006 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2007 (char*)&p_dec->fmt_in.i_codec,
2008 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2010 /* Free all packets still in the decoder fifo. */
2011 block_FifoEmpty( p_owner->p_fifo );
2012 block_FifoRelease( p_owner->p_fifo );
2015 vlc_mutex_lock( &p_owner->lock );
2016 DecoderFlushBuffering( p_dec );
2017 vlc_mutex_unlock( &p_owner->lock );
2020 if( p_owner->p_aout_input )
2021 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2022 if( p_owner->p_aout )
2024 input_resource_RequestAout( p_owner->p_input->p->p_resource,
2026 input_SendEventAout( p_owner->p_input );
2027 p_owner->p_aout = NULL;
2029 if( p_owner->p_vout )
2031 /* Hack to make sure all the the pictures are freed by the decoder
2032 * and that the vout is not paused anymore */
2033 vout_FixLeaks( p_owner->p_vout, true );
2034 if( p_owner->b_paused )
2035 vout_ChangePause( p_owner->p_vout, false, mdate() );
2038 input_resource_RequestVout( p_owner->p_input->p->p_resource, p_owner->p_vout, NULL, true );
2039 input_SendEventVout( p_owner->p_input );
2043 if( p_owner->p_sout_input )
2045 sout_InputDelete( p_owner->p_sout_input );
2046 es_format_Clean( &p_owner->sout );
2050 if( p_dec->fmt_out.i_cat == SPU_ES )
2052 vout_thread_t *p_vout;
2054 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2057 if( p_owner->p_spu_vout == p_vout )
2058 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2059 vlc_object_release( p_vout );
2063 es_format_Clean( &p_dec->fmt_in );
2064 es_format_Clean( &p_dec->fmt_out );
2065 if( p_dec->p_description )
2066 vlc_meta_Delete( p_dec->p_description );
2067 es_format_Clean( &p_owner->fmt_description );
2068 if( p_owner->p_description )
2069 vlc_meta_Delete( p_owner->p_description );
2071 if( p_owner->p_packetizer )
2073 module_unneed( p_owner->p_packetizer,
2074 p_owner->p_packetizer->p_module );
2075 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2076 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2077 if( p_owner->p_packetizer->p_description )
2078 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2079 vlc_object_detach( p_owner->p_packetizer );
2080 vlc_object_release( p_owner->p_packetizer );
2083 vlc_cond_destroy( &p_owner->wait_acknowledge );
2084 vlc_cond_destroy( &p_owner->wait_request );
2085 vlc_mutex_destroy( &p_owner->lock );
2087 vlc_object_detach( p_dec );
2092 /*****************************************************************************
2093 * Buffers allocation callbacks for the decoders
2094 *****************************************************************************/
2095 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2097 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2099 vlc_assert_locked( &p_owner->lock );
2101 p_owner->b_fmt_description = true;
2103 /* Copy es_format */
2104 es_format_Clean( &p_owner->fmt_description );
2105 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2107 /* Move p_description */
2108 if( p_owner->p_description && p_dec->p_description )
2109 vlc_meta_Delete( p_owner->p_description );
2110 p_owner->p_description = p_dec->p_description;
2111 p_dec->p_description = NULL;
2113 static vout_thread_t *aout_request_vout( void *p_private,
2114 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2116 decoder_t *p_dec = p_private;
2117 input_thread_t *p_input = p_dec->p_owner->p_input;
2119 p_vout = input_resource_RequestVout( p_input->p->p_resource, p_vout, p_fmt, b_recyle );
2120 input_SendEventVout( p_input );
2125 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2127 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2128 aout_buffer_t *p_buffer;
2130 if( p_owner->p_aout_input != NULL &&
2131 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2132 p_dec->fmt_out.audio.i_original_channels !=
2133 p_owner->audio.i_original_channels ||
2134 p_dec->fmt_out.audio.i_bytes_per_frame !=
2135 p_owner->audio.i_bytes_per_frame ) )
2137 aout_input_t *p_aout_input = p_owner->p_aout_input;
2139 /* Parameters changed, restart the aout */
2140 vlc_mutex_lock( &p_owner->lock );
2142 DecoderFlushBuffering( p_dec );
2144 p_owner->p_aout_input = NULL;
2145 aout_DecDelete( p_owner->p_aout, p_aout_input );
2147 vlc_mutex_unlock( &p_owner->lock );
2150 if( p_owner->p_aout_input == NULL )
2152 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2153 audio_sample_format_t format;
2154 aout_input_t *p_aout_input;
2155 aout_instance_t *p_aout;
2156 aout_request_vout_t request_vout;
2158 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2159 p_owner->audio = p_dec->fmt_out.audio;
2161 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2162 if( i_force_dolby &&
2163 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2164 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2166 if( i_force_dolby == 1 )
2168 format.i_original_channels = format.i_original_channels |
2169 AOUT_CHAN_DOLBYSTEREO;
2171 else /* i_force_dolby == 2 */
2173 format.i_original_channels = format.i_original_channels &
2174 ~AOUT_CHAN_DOLBYSTEREO;
2178 request_vout.pf_request_vout = aout_request_vout;
2179 request_vout.p_private = p_dec;
2181 p_aout = p_owner->p_aout;
2183 p_aout = input_resource_RequestAout( p_owner->p_input->p->p_resource, NULL );
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 input_SendEventAout( p_owner->p_input );
2197 if( p_owner->p_aout_input == NULL )
2199 msg_Err( p_dec, "failed to create audio output" );
2200 p_dec->b_error = true;
2203 p_dec->fmt_out.audio.i_bytes_per_frame =
2204 p_owner->audio.i_bytes_per_frame;
2207 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2212 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2214 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2216 aout_DecDeleteBuffer( p_owner->p_aout,
2217 p_owner->p_aout_input, p_buffer );
2221 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2223 static picture_t *vout_new_buffer( decoder_t *p_dec )
2225 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2227 if( p_owner->p_vout == NULL ||
2228 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2229 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2230 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2231 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2233 vout_thread_t *p_vout;
2235 if( !p_dec->fmt_out.video.i_width ||
2236 !p_dec->fmt_out.video.i_height )
2238 /* Can't create a new vout without display size */
2242 if( !p_dec->fmt_out.video.i_visible_width ||
2243 !p_dec->fmt_out.video.i_visible_height )
2245 if( p_dec->fmt_in.video.i_visible_width &&
2246 p_dec->fmt_in.video.i_visible_height )
2248 p_dec->fmt_out.video.i_visible_width =
2249 p_dec->fmt_in.video.i_visible_width;
2250 p_dec->fmt_out.video.i_visible_height =
2251 p_dec->fmt_in.video.i_visible_height;
2255 p_dec->fmt_out.video.i_visible_width =
2256 p_dec->fmt_out.video.i_width;
2257 p_dec->fmt_out.video.i_visible_height =
2258 p_dec->fmt_out.video.i_height;
2262 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2263 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2265 p_dec->fmt_out.video.i_visible_height = 1080;
2266 p_dec->fmt_out.video.i_sar_num *= 135;
2267 p_dec->fmt_out.video.i_sar_den *= 136;
2268 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2271 if( !p_dec->fmt_out.video.i_sar_num ||
2272 !p_dec->fmt_out.video.i_sar_den )
2274 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2275 p_dec->fmt_out.video.i_visible_height;
2277 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2278 p_dec->fmt_out.video.i_visible_width;
2281 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2282 &p_dec->fmt_out.video.i_sar_den,
2283 p_dec->fmt_out.video.i_sar_num,
2284 p_dec->fmt_out.video.i_sar_den, 50000 );
2286 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2287 p_owner->video = p_dec->fmt_out.video;
2289 vlc_mutex_lock( &p_owner->lock );
2291 DecoderFlushBuffering( p_dec );
2293 p_vout = p_owner->p_vout;
2294 p_owner->p_vout = NULL;
2295 vlc_mutex_unlock( &p_owner->lock );
2297 p_vout = input_resource_RequestVout( p_owner->p_input->p->p_resource,
2298 p_vout, &p_dec->fmt_out.video, true );
2300 vlc_mutex_lock( &p_owner->lock );
2301 p_owner->p_vout = p_vout;
2303 DecoderUpdateFormatLocked( p_dec );
2305 vlc_mutex_unlock( &p_owner->lock );
2307 input_SendEventVout( p_owner->p_input );
2308 if( p_vout == NULL )
2310 msg_Err( p_dec, "failed to create video output" );
2311 p_dec->b_error = true;
2315 if( p_owner->video.i_rmask )
2316 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2317 if( p_owner->video.i_gmask )
2318 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2319 if( p_owner->video.i_bmask )
2320 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2323 /* Get a new picture
2327 picture_t *p_picture;
2329 if( p_dec->b_die || p_dec->b_error )
2332 /* The video filter chain required that there is always 1 free buffer
2333 * that it will use as temporary one. It will release the temporary
2334 * buffer once its work is done, so this check is safe even if we don't
2335 * lock around both count() and create().
2337 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2339 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2344 if( DecoderIsFlushing( p_dec ) )
2348 DecoderSignalBuffering( p_dec, true );
2350 /* Check the decoder doesn't leak pictures */
2351 vout_FixLeaks( p_owner->p_vout, false );
2353 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2354 msleep( VOUT_OUTMEM_SLEEP );
2358 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2360 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2363 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2365 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2368 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2370 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2373 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2375 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2376 vout_thread_t *p_vout = NULL;
2377 subpicture_t *p_subpic;
2378 int i_attempts = 30;
2380 while( i_attempts-- )
2382 if( p_dec->b_die || p_dec->b_error )
2385 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2389 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2394 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2398 if( p_owner->p_spu_vout != p_vout )
2400 vlc_mutex_lock( &p_owner->lock );
2402 DecoderFlushBuffering( p_dec );
2404 vlc_mutex_unlock( &p_owner->lock );
2406 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2407 &p_owner->i_spu_channel );
2408 p_owner->i_spu_order = 0;
2409 p_owner->p_spu_vout = p_vout;
2412 p_subpic = subpicture_New();
2415 p_subpic->i_channel = p_owner->i_spu_channel;
2416 p_subpic->i_order = p_owner->i_spu_order++;
2417 p_subpic->b_subtitle = true;
2420 vlc_object_release( p_vout );
2425 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2427 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2428 vout_thread_t *p_vout = NULL;
2430 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2431 if( !p_vout || p_owner->p_spu_vout != p_vout )
2434 vlc_object_release( p_vout );
2435 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2439 subpicture_Delete( p_subpic );
2441 vlc_object_release( p_vout );