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 *, const subpicture_updater_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,
217 const subpicture_updater_t *p_dyn )
219 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder, p_dyn );
221 msg_Warn( p_decoder, "can't get output subpicture" );
225 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
227 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
230 /* decoder_GetInputAttachments:
232 int decoder_GetInputAttachments( decoder_t *p_dec,
233 input_attachment_t ***ppp_attachment,
236 if( !p_dec->pf_get_attachments )
239 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
241 /* decoder_GetDisplayDate:
243 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
245 if( !p_dec->pf_get_display_date )
246 return VLC_TS_INVALID;
248 return p_dec->pf_get_display_date( p_dec, i_ts );
250 /* decoder_GetDisplayRate:
252 int decoder_GetDisplayRate( decoder_t *p_dec )
254 if( !p_dec->pf_get_display_rate )
255 return INPUT_RATE_DEFAULT;
257 return p_dec->pf_get_display_rate( p_dec );
261 * Spawns a new decoder thread
263 * \param p_input the input thread
264 * \param p_es the es descriptor
265 * \return the spawned decoder object
267 decoder_t *input_DecoderNew( input_thread_t *p_input,
268 es_format_t *fmt, input_clock_t *p_clock,
269 sout_instance_t *p_sout )
271 decoder_t *p_dec = NULL;
272 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
275 /* Create the decoder configuration structure */
276 p_dec = CreateDecoder( p_input, fmt, p_sout != NULL, p_sout );
279 msg_Err( p_input, "could not create %s", psz_type );
280 dialog_Fatal( p_input, _("Streaming / Transcoding failed"),
281 _("VLC could not open the %s module."),
282 vlc_gettext( psz_type ) );
286 if( !p_dec->p_module )
288 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
290 DeleteDecoder( p_dec );
294 p_dec->p_owner->p_clock = p_clock;
295 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
297 if( p_dec->fmt_out.i_cat == AUDIO_ES )
298 i_priority = VLC_THREAD_PRIORITY_AUDIO;
300 i_priority = VLC_THREAD_PRIORITY_VIDEO;
302 /* Spawn the decoder thread */
303 if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
305 msg_Err( p_dec, "cannot spawn decoder thread" );
306 module_unneed( p_dec, p_dec->p_module );
307 DeleteDecoder( p_dec );
315 * Kills a decoder thread and waits until it's finished
317 * \param p_input the input thread
318 * \param p_es the es descriptor
321 void input_DecoderDelete( decoder_t *p_dec )
323 decoder_owner_sys_t *p_owner = p_dec->p_owner;
325 vlc_object_kill( p_dec );
327 /* Make sure we aren't paused/buffering/waiting anymore */
328 vlc_mutex_lock( &p_owner->lock );
329 const bool b_was_paused = p_owner->b_paused;
330 p_owner->b_paused = false;
331 p_owner->b_buffering = false;
332 p_owner->b_flushing = true;
333 vlc_cond_signal( &p_owner->wait_request );
334 vlc_mutex_unlock( &p_owner->lock );
336 vlc_thread_join( p_dec );
337 p_owner->b_paused = b_was_paused;
339 module_unneed( p_dec, p_dec->p_module );
342 if( p_dec->p_owner->cc.b_supported )
345 for( i = 0; i < 4; i++ )
346 input_DecoderSetCcState( p_dec, false, i );
350 DeleteDecoder( p_dec );
354 * Put a block_t in the decoder's fifo.
355 * Thread-safe w.r.t. the decoder. May be a cancellation point.
357 * \param p_dec the decoder object
358 * \param p_block the data block
360 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
362 decoder_owner_sys_t *p_owner = p_dec->p_owner;
366 /* The fifo is not consummed when buffering and so will
368 * There is no need to lock as b_buffering is never modify
369 * inside decoder thread. */
370 if( !p_owner->b_buffering )
371 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
374 else if( block_FifoSize( p_owner->p_fifo ) > 50*1024*1024 /* 50 MiB */ )
376 else if( block_FifoSize( p_owner->p_fifo ) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
379 /* FIXME: ideally we would check the time amount of data
380 * in the FIFO instead of its size. */
381 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
382 "consumed quickly enough), resetting fifo!" );
383 block_FifoEmpty( p_owner->p_fifo );
386 block_FifoPut( p_owner->p_fifo, p_block );
389 bool input_DecoderIsEmpty( decoder_t * p_dec )
391 assert( !p_dec->p_owner->b_buffering );
393 /* FIXME that's not really true */
394 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
397 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
399 decoder_owner_sys_t *p_owner = p_dec->p_owner;
402 vlc_mutex_lock( &p_owner->lock );
403 for( i = 0; i < 4; i++ )
404 pb_present[i] = p_owner->cc.pb_present[i];
405 vlc_mutex_unlock( &p_owner->lock );
407 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
409 decoder_owner_sys_t *p_owner = p_dec->p_owner;
411 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
413 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
418 static const vlc_fourcc_t fcc[4] = {
419 VLC_FOURCC('c', 'c', '1', ' '),
420 VLC_FOURCC('c', 'c', '2', ' '),
421 VLC_FOURCC('c', 'c', '3', ' '),
422 VLC_FOURCC('c', 'c', '4', ' '),
427 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
428 p_cc = CreateDecoder( p_owner->p_input, &fmt, false, p_owner->p_sout );
431 msg_Err( p_dec, "could not create decoder" );
432 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
433 _("VLC could not open the decoder module.") );
436 else if( !p_cc->p_module )
438 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
439 DeleteDecoder( p_cc );
442 p_cc->p_owner->p_clock = p_owner->p_clock;
444 vlc_mutex_lock( &p_owner->lock );
445 p_owner->cc.pp_decoder[i_channel] = p_cc;
446 vlc_mutex_unlock( &p_owner->lock );
452 vlc_mutex_lock( &p_owner->lock );
453 p_cc = p_owner->cc.pp_decoder[i_channel];
454 p_owner->cc.pp_decoder[i_channel] = NULL;
455 vlc_mutex_unlock( &p_owner->lock );
459 vlc_object_kill( p_cc );
460 module_unneed( p_cc, p_cc->p_module );
461 DeleteDecoder( p_cc );
466 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
468 decoder_owner_sys_t *p_owner = p_dec->p_owner;
471 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
474 vlc_mutex_lock( &p_owner->lock );
475 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
476 vlc_mutex_unlock( &p_owner->lock );
480 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
482 decoder_owner_sys_t *p_owner = p_dec->p_owner;
484 vlc_mutex_lock( &p_owner->lock );
486 assert( !p_owner->b_paused || !b_paused );
488 p_owner->b_paused = b_paused;
489 p_owner->pause.i_date = i_date;
490 p_owner->pause.i_ignore = 0;
491 vlc_cond_signal( &p_owner->wait_request );
493 DecoderOutputChangePause( p_dec, b_paused, i_date );
495 vlc_mutex_unlock( &p_owner->lock );
498 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
500 decoder_owner_sys_t *p_owner = p_dec->p_owner;
502 vlc_mutex_lock( &p_owner->lock );
503 p_owner->i_ts_delay = i_delay;
504 vlc_mutex_unlock( &p_owner->lock );
507 void input_DecoderStartBuffering( decoder_t *p_dec )
509 decoder_owner_sys_t *p_owner = p_dec->p_owner;
511 vlc_mutex_lock( &p_owner->lock );
513 DecoderFlush( p_dec );
515 p_owner->buffer.b_first = true;
516 p_owner->buffer.b_full = false;
517 p_owner->buffer.i_count = 0;
519 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
520 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
522 p_owner->buffer.p_picture = NULL;
523 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
525 p_owner->buffer.p_subpic = NULL;
526 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
528 p_owner->buffer.p_audio = NULL;
529 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
531 p_owner->buffer.p_block = NULL;
532 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
535 p_owner->b_buffering = true;
537 vlc_cond_signal( &p_owner->wait_request );
539 vlc_mutex_unlock( &p_owner->lock );
542 void input_DecoderStopBuffering( decoder_t *p_dec )
544 decoder_owner_sys_t *p_owner = p_dec->p_owner;
546 vlc_mutex_lock( &p_owner->lock );
548 p_owner->b_buffering = false;
550 vlc_cond_signal( &p_owner->wait_request );
552 vlc_mutex_unlock( &p_owner->lock );
555 void input_DecoderWaitBuffering( decoder_t *p_dec )
557 decoder_owner_sys_t *p_owner = p_dec->p_owner;
559 vlc_mutex_lock( &p_owner->lock );
561 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
563 block_FifoWake( p_owner->p_fifo );
564 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
567 vlc_mutex_unlock( &p_owner->lock );
570 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
572 decoder_owner_sys_t *p_owner = p_dec->p_owner;
576 vlc_mutex_lock( &p_owner->lock );
577 if( p_dec->fmt_out.i_cat == VIDEO_ES )
579 if( p_owner->b_paused && p_owner->p_vout )
581 vout_NextPicture( p_owner->p_vout, pi_duration );
582 p_owner->pause.i_ignore++;
583 vlc_cond_signal( &p_owner->wait_request );
588 /* TODO subtitle should not be flushed */
589 DecoderFlush( p_dec );
591 vlc_mutex_unlock( &p_owner->lock );
594 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
596 decoder_owner_sys_t *p_owner = p_dec->p_owner;
599 vlc_mutex_lock( &p_owner->lock );
600 b_changed = p_owner->b_fmt_description;
604 es_format_Copy( p_fmt, &p_owner->fmt_description );
609 if( p_owner->p_description )
611 *pp_meta = vlc_meta_New();
613 vlc_meta_Merge( *pp_meta, p_owner->p_description );
616 p_owner->b_fmt_description = false;
618 vlc_mutex_unlock( &p_owner->lock );
622 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
624 decoder_owner_sys_t *p_owner = p_dec->p_owner;
626 return block_FifoSize( p_owner->p_fifo );
629 void input_DecoderGetObjects( decoder_t *p_dec,
630 vout_thread_t **pp_vout, aout_instance_t **pp_aout )
632 decoder_owner_sys_t *p_owner = p_dec->p_owner;
634 vlc_mutex_lock( &p_owner->lock );
636 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
638 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
639 vlc_mutex_unlock( &p_owner->lock );
642 /*****************************************************************************
644 *****************************************************************************/
645 static int DecoderGetInputAttachments( decoder_t *p_dec,
646 input_attachment_t ***ppp_attachment,
649 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
650 ppp_attachment, pi_attachment );
652 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
654 decoder_owner_sys_t *p_owner = p_dec->p_owner;
656 vlc_mutex_lock( &p_owner->lock );
657 if( p_owner->b_buffering || p_owner->b_paused )
658 i_ts = VLC_TS_INVALID;
659 vlc_mutex_unlock( &p_owner->lock );
661 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
664 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
665 return VLC_TS_INVALID;
669 static int DecoderGetDisplayRate( decoder_t *p_dec )
671 decoder_owner_sys_t *p_owner = p_dec->p_owner;
673 if( !p_owner->p_clock )
674 return INPUT_RATE_DEFAULT;
675 return input_clock_GetRate( p_owner->p_clock );
679 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
681 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
682 "VLC probably does not support this sound or video format.",
684 dialog_Fatal( p_dec, _("No suitable decoder module"),
685 _("VLC does not support the audio or video format \"%4.4s\". "
686 "Unfortunately there is no way for you to fix this."),
692 * Create a decoder object
694 * \param p_input the input thread
695 * \param p_es the es descriptor
696 * \param b_packetizer instead of a decoder
697 * \return the decoder object
699 static decoder_t * CreateDecoder( input_thread_t *p_input,
700 es_format_t *fmt, bool b_packetizer,
701 sout_instance_t *p_sout )
704 decoder_owner_sys_t *p_owner;
705 es_format_t null_es_format;
707 p_dec = vlc_custom_create( p_input, sizeof( *p_dec ), VLC_OBJECT_DECODER,
712 p_dec->pf_decode_audio = NULL;
713 p_dec->pf_decode_video = NULL;
714 p_dec->pf_decode_sub = NULL;
715 p_dec->pf_get_cc = NULL;
716 p_dec->pf_packetize = NULL;
718 /* Initialize the decoder */
719 p_dec->p_module = NULL;
721 memset( &null_es_format, 0, sizeof(es_format_t) );
722 es_format_Copy( &p_dec->fmt_in, fmt );
723 es_format_Copy( &p_dec->fmt_out, &null_es_format );
725 p_dec->p_description = NULL;
727 /* Allocate our private structure for the decoder */
728 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
729 if( p_dec->p_owner == NULL )
731 vlc_object_release( p_dec );
734 p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
735 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
736 p_dec->p_owner->p_input = p_input;
737 p_dec->p_owner->p_aout = NULL;
738 p_dec->p_owner->p_aout_input = NULL;
739 p_dec->p_owner->p_vout = NULL;
740 p_dec->p_owner->p_spu_vout = NULL;
741 p_dec->p_owner->i_spu_channel = 0;
742 p_dec->p_owner->i_spu_order = 0;
743 p_dec->p_owner->p_sout = p_sout;
744 p_dec->p_owner->p_sout_input = NULL;
745 p_dec->p_owner->p_packetizer = NULL;
746 p_dec->p_owner->b_packetizer = b_packetizer;
749 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
751 free( p_dec->p_owner );
752 vlc_object_release( p_dec );
756 /* Set buffers allocation callbacks for the decoders */
757 p_dec->pf_aout_buffer_new = aout_new_buffer;
758 p_dec->pf_aout_buffer_del = aout_del_buffer;
759 p_dec->pf_vout_buffer_new = vout_new_buffer;
760 p_dec->pf_vout_buffer_del = vout_del_buffer;
761 p_dec->pf_picture_link = vout_link_picture;
762 p_dec->pf_picture_unlink = vout_unlink_picture;
763 p_dec->pf_spu_buffer_new = spu_new_buffer;
764 p_dec->pf_spu_buffer_del = spu_del_buffer;
766 p_dec->pf_get_attachments = DecoderGetInputAttachments;
767 p_dec->pf_get_display_date = DecoderGetDisplayDate;
768 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
770 vlc_object_attach( p_dec, p_input );
772 /* Find a suitable decoder/packetizer module */
774 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
776 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
778 /* Check if decoder requires already packetized data */
780 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
782 p_dec->p_owner->p_packetizer =
783 vlc_custom_create( p_input, sizeof( decoder_t ),
784 VLC_OBJECT_DECODER, "packetizer" );
785 if( p_dec->p_owner->p_packetizer )
787 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
790 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
793 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
795 p_dec->p_owner->p_packetizer->p_module =
796 module_need( p_dec->p_owner->p_packetizer,
797 "packetizer", "$packetizer", false );
799 if( !p_dec->p_owner->p_packetizer->p_module )
801 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
802 vlc_object_release( p_dec->p_owner->p_packetizer );
807 /* Copy ourself the input replay gain */
808 if( fmt->i_cat == AUDIO_ES )
810 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
812 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
814 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
815 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
817 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
819 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
820 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
826 vlc_mutex_init( &p_owner->lock );
827 vlc_cond_init( &p_owner->wait_request );
828 vlc_cond_init( &p_owner->wait_acknowledge );
830 p_owner->b_fmt_description = false;
831 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
832 p_owner->p_description = NULL;
834 p_owner->b_paused = false;
835 p_owner->pause.i_date = VLC_TS_INVALID;
836 p_owner->pause.i_ignore = 0;
838 p_owner->b_buffering = false;
839 p_owner->buffer.b_first = true;
840 p_owner->buffer.b_full = false;
841 p_owner->buffer.i_count = 0;
842 p_owner->buffer.p_picture = NULL;
843 p_owner->buffer.p_subpic = NULL;
844 p_owner->buffer.p_audio = NULL;
845 p_owner->buffer.p_block = NULL;
847 p_owner->b_flushing = false;
850 p_owner->cc.b_supported = false;
853 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
854 p_owner->cc.b_supported = true;
855 if( p_dec->pf_get_cc )
856 p_owner->cc.b_supported = true;
859 for( unsigned i = 0; i < 4; i++ )
861 p_owner->cc.pb_present[i] = false;
862 p_owner->cc.pp_decoder[i] = NULL;
864 p_owner->i_ts_delay = 0;
869 * The decoding main loop
871 * \param p_dec the decoder
873 static void *DecoderThread( vlc_object_t *p_this )
875 decoder_t *p_dec = (decoder_t *)p_this;
876 decoder_owner_sys_t *p_owner = p_dec->p_owner;
878 /* The decoder's main loop */
881 block_t *p_block = block_FifoGet( p_owner->p_fifo );
883 /* Make sure there is no cancellation point other than this one^^.
884 * If you need one, be sure to push cleanup of p_block. */
885 DecoderSignalBuffering( p_dec, p_block == NULL );
889 int canc = vlc_savecancel();
892 DecoderError( p_dec, p_block );
894 DecoderProcess( p_dec, p_block );
896 vlc_restorecancel( canc );
902 static block_t *DecoderBlockFlushNew()
904 block_t *p_null = block_Alloc( 128 );
908 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
909 BLOCK_FLAG_CORRUPTED |
910 BLOCK_FLAG_CORE_FLUSH;
911 memset( p_null->p_buffer, 0, p_null->i_buffer );
916 static void DecoderFlush( decoder_t *p_dec )
918 decoder_owner_sys_t *p_owner = p_dec->p_owner;
920 vlc_assert_locked( &p_owner->lock );
923 block_FifoEmpty( p_owner->p_fifo );
925 /* Monitor for flush end */
926 p_owner->b_flushing = true;
927 vlc_cond_signal( &p_owner->wait_request );
929 /* Send a special block */
930 block_t *p_null = DecoderBlockFlushNew();
933 input_DecoderDecode( p_dec, p_null, false );
936 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
937 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
940 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
942 decoder_owner_sys_t *p_owner = p_dec->p_owner;
944 vlc_mutex_lock( &p_owner->lock );
946 if( p_owner->b_buffering )
949 p_owner->buffer.b_full = true;
950 vlc_cond_signal( &p_owner->wait_acknowledge );
953 vlc_mutex_unlock( &p_owner->lock );
956 static bool DecoderIsFlushing( decoder_t *p_dec )
958 decoder_owner_sys_t *p_owner = p_dec->p_owner;
961 vlc_mutex_lock( &p_owner->lock );
963 b_flushing = p_owner->b_flushing;
965 vlc_mutex_unlock( &p_owner->lock );
970 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
972 decoder_owner_sys_t *p_owner = p_dec->p_owner;
974 vlc_assert_locked( &p_owner->lock );
976 while( !p_owner->b_flushing )
978 if( p_owner->b_paused )
980 if( p_owner->b_buffering && !p_owner->buffer.b_full )
982 if( p_owner->pause.i_ignore > 0 )
984 p_owner->pause.i_ignore--;
990 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
993 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
997 *pb_reject = p_owner->b_flushing;
1000 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1002 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1004 vlc_assert_locked( &p_owner->lock );
1006 /* XXX only audio and video output have to be paused.
1007 * - for sout it is useless
1008 * - for subs, it is done by the vout
1010 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1012 if( p_owner->p_aout && p_owner->p_aout_input )
1013 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1016 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1018 if( p_owner->p_vout )
1019 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1022 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1024 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1025 *pi_preroll = INT64_MAX;
1026 else if( p->i_dts > VLC_TS_INVALID )
1027 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1028 else if( p->i_pts > VLC_TS_INVALID )
1029 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1032 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1034 mtime_t current_date = mdate();
1036 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1037 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1039 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1040 * for teletext streams. */
1041 return current_date + 400000;
1046 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1047 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1049 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1050 input_clock_t *p_clock = p_owner->p_clock;
1052 vlc_assert_locked( &p_owner->lock );
1054 const mtime_t i_es_delay = p_owner->i_ts_delay;
1058 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1061 if( *pi_ts0 > VLC_TS_INVALID )
1063 *pi_ts0 += i_es_delay;
1064 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1065 *pi_ts1 += i_es_delay;
1066 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1067 *pi_ts0 = VLC_TS_INVALID;
1071 i_rate = input_clock_GetRate( p_clock );
1074 /* Do not create ephemere data because of rounding errors */
1075 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1079 *pi_duration = ( *pi_duration * i_rate +
1080 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1087 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1088 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1095 * If *pb_reject, it does nothing, otherwise it waits for the given
1096 * deadline or a flush request (in which case it set *pi_reject to true.
1098 static void DecoderWaitDate( decoder_t *p_dec,
1099 bool *pb_reject, mtime_t i_deadline )
1101 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1103 if( *pb_reject || i_deadline < 0 )
1108 vlc_mutex_lock( &p_owner->lock );
1109 if( p_owner->b_flushing || p_dec->b_die )
1112 vlc_mutex_unlock( &p_owner->lock );
1115 int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1117 vlc_mutex_unlock( &p_owner->lock );
1123 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1124 int *pi_played_sum, int *pi_lost_sum )
1126 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1127 aout_instance_t *p_aout = p_owner->p_aout;
1128 aout_input_t *p_aout_input = p_owner->p_aout_input;
1131 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1133 msg_Warn( p_dec, "non-dated audio buffer received" );
1135 aout_BufferFree( p_audio );
1140 vlc_mutex_lock( &p_owner->lock );
1142 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1144 p_audio->p_next = NULL;
1146 *p_owner->buffer.pp_audio_next = p_audio;
1147 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1149 p_owner->buffer.i_count++;
1150 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1151 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1153 p_owner->buffer.b_full = true;
1154 vlc_cond_signal( &p_owner->wait_acknowledge );
1160 bool b_has_more = false;
1162 DecoderWaitUnblock( p_dec, &b_reject );
1164 if( p_owner->b_buffering )
1166 vlc_mutex_unlock( &p_owner->lock );
1171 if( p_owner->buffer.p_audio )
1173 p_audio = p_owner->buffer.p_audio;
1175 p_owner->buffer.p_audio = p_audio->p_next;
1176 p_owner->buffer.i_count--;
1178 b_has_more = p_owner->buffer.p_audio != NULL;
1180 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1184 const bool b_dated = p_audio->i_pts > VLC_TS_INVALID;
1185 int i_rate = INPUT_RATE_DEFAULT;
1187 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1188 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1190 vlc_mutex_unlock( &p_owner->lock );
1192 if( !p_aout || !p_aout_input ||
1193 p_audio->i_pts <= VLC_TS_INVALID ||
1194 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1195 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1198 DecoderWaitDate( p_dec, &b_reject,
1199 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1203 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1204 *pi_played_sum += 1;
1205 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1210 msg_Warn( p_aout, "received buffer in the future" );
1212 msg_Warn( p_dec, "non-dated audio buffer received" );
1215 aout_BufferFree( p_audio );
1221 vlc_mutex_lock( &p_owner->lock );
1222 if( !p_owner->buffer.p_audio )
1224 vlc_mutex_unlock( &p_owner->lock );
1230 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1232 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1233 input_thread_t *p_input = p_owner->p_input;
1234 aout_buffer_t *p_aout_buf;
1239 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1241 aout_instance_t *p_aout = p_owner->p_aout;
1242 aout_input_t *p_aout_input = p_owner->p_aout_input;
1246 /* It prevent freezing VLC in case of broken decoder */
1247 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1249 block_Release( p_block );
1254 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1255 p_aout_buf->i_pts < p_owner->i_preroll_end )
1257 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1261 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1263 msg_Dbg( p_dec, "End of audio preroll" );
1264 if( p_owner->p_aout && p_owner->p_aout_input )
1265 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1267 p_owner->i_preroll_end = VLC_TS_INVALID;
1270 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1273 /* Update ugly stat */
1274 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1276 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1278 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1280 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1282 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1285 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1288 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1290 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1293 bool b_processed = false;
1297 assert( p_dec_cc->pf_get_cc != NULL );
1299 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1300 if( !p_owner->cc.b_supported )
1303 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1307 vlc_mutex_lock( &p_owner->lock );
1308 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1310 p_owner->cc.pb_present[i] |= pb_present[i];
1311 if( p_owner->cc.pp_decoder[i] )
1315 for( i = 0; i < 4; i++ )
1317 if( !p_owner->cc.pp_decoder[i] )
1320 if( i_cc_decoder > 1 )
1321 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1323 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1327 vlc_mutex_unlock( &p_owner->lock );
1330 block_Release( p_cc );
1333 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1334 int *pi_played_sum, int *pi_lost_sum )
1336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1337 vout_thread_t *p_vout = p_owner->p_vout;
1338 bool b_first_buffered;
1340 if( p_picture->date <= VLC_TS_INVALID )
1342 msg_Warn( p_vout, "non-dated video buffer received" );
1344 vout_ReleasePicture( p_vout, p_picture );
1349 vlc_mutex_lock( &p_owner->lock );
1351 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1353 p_picture->p_next = NULL;
1355 *p_owner->buffer.pp_picture_next = p_picture;
1356 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1358 p_owner->buffer.i_count++;
1359 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1360 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1362 p_owner->buffer.b_full = true;
1363 vlc_cond_signal( &p_owner->wait_acknowledge );
1366 b_first_buffered = p_owner->buffer.p_picture != NULL;
1368 for( ;; b_first_buffered = false )
1370 bool b_has_more = false;
1374 DecoderWaitUnblock( p_dec, &b_reject );
1376 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1378 vlc_mutex_unlock( &p_owner->lock );
1381 bool b_buffering_first = p_owner->b_buffering;
1384 if( p_owner->buffer.p_picture )
1386 p_picture = p_owner->buffer.p_picture;
1388 p_owner->buffer.p_picture = p_picture->p_next;
1389 p_owner->buffer.i_count--;
1391 b_has_more = p_owner->buffer.p_picture != NULL;
1393 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1397 if( b_buffering_first )
1399 assert( p_owner->buffer.b_first );
1400 assert( !p_owner->buffer.i_count );
1401 msg_Dbg( p_dec, "Received first picture" );
1402 p_owner->buffer.b_first = false;
1403 p_picture->b_force = true;
1406 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1407 int i_rate = INPUT_RATE_DEFAULT;
1408 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1409 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1411 vlc_mutex_unlock( &p_owner->lock );
1414 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1419 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1421 /* Be sure to not display old picture after our own */
1422 vout_Flush( p_vout, p_picture->date );
1423 p_owner->i_last_rate = i_rate;
1425 vout_PutPicture( p_vout, p_picture );
1430 msg_Warn( p_vout, "early picture skipped" );
1432 msg_Warn( p_vout, "non-dated video buffer received" );
1435 vout_ReleasePicture( p_vout, p_picture );
1439 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1441 *pi_played_sum += i_tmp_display;
1442 *pi_lost_sum += i_tmp_lost;
1444 if( !b_has_more || b_buffering_first )
1447 vlc_mutex_lock( &p_owner->lock );
1448 if( !p_owner->buffer.p_picture )
1450 vlc_mutex_unlock( &p_owner->lock );
1456 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1458 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1459 input_thread_t *p_input = p_owner->p_input;
1463 int i_displayed = 0;
1465 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1467 vout_thread_t *p_vout = p_owner->p_vout;
1470 /* It prevent freezing VLC in case of broken decoder */
1471 vout_ReleasePicture( p_vout, p_pic );
1473 block_Release( p_block );
1479 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1481 vout_ReleasePicture( p_vout, p_pic );
1485 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1487 msg_Dbg( p_dec, "End of video preroll" );
1489 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1491 p_owner->i_preroll_end = VLC_TS_INVALID;
1494 if( p_dec->pf_get_cc &&
1495 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1496 DecoderGetCc( p_dec, p_dec );
1498 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1500 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1502 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1504 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1506 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1509 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1512 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1516 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1519 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1520 vout_thread_t *p_vout = p_owner->p_spu_vout;
1523 if( p_subpic->i_start <= VLC_TS_INVALID && !b_telx )
1525 msg_Warn( p_dec, "non-dated spu buffer received" );
1526 subpicture_Delete( p_subpic );
1531 vlc_mutex_lock( &p_owner->lock );
1533 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1535 p_subpic->p_next = NULL;
1537 *p_owner->buffer.pp_subpic_next = p_subpic;
1538 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1540 p_owner->buffer.i_count++;
1541 /* XXX it is important to be full after the first one */
1542 if( p_owner->buffer.i_count > 0 )
1544 p_owner->buffer.b_full = true;
1545 vlc_cond_signal( &p_owner->wait_acknowledge );
1551 bool b_has_more = false;
1553 DecoderWaitUnblock( p_dec, &b_reject );
1555 if( p_owner->b_buffering )
1557 vlc_mutex_unlock( &p_owner->lock );
1562 if( p_owner->buffer.p_subpic )
1564 p_subpic = p_owner->buffer.p_subpic;
1566 p_owner->buffer.p_subpic = p_subpic->p_next;
1567 p_owner->buffer.i_count--;
1569 b_has_more = p_owner->buffer.p_subpic != NULL;
1571 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1575 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1576 NULL, INT64_MAX, b_telx );
1578 vlc_mutex_unlock( &p_owner->lock );
1580 if( p_subpic->i_start <= VLC_TS_INVALID )
1583 DecoderWaitDate( p_dec, &b_reject,
1584 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1587 spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_subpic );
1589 subpicture_Delete( p_subpic );
1593 vlc_mutex_lock( &p_owner->lock );
1594 if( !p_owner->buffer.p_subpic )
1596 vlc_mutex_unlock( &p_owner->lock );
1602 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1605 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1607 assert( p_owner->p_clock );
1608 assert( !p_sout_block->p_next );
1610 vlc_mutex_lock( &p_owner->lock );
1612 if( p_owner->b_buffering || p_owner->buffer.p_block )
1614 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1616 p_owner->buffer.i_count++;
1617 /* XXX it is important to be full after the first one */
1618 if( p_owner->buffer.i_count > 0 )
1620 p_owner->buffer.b_full = true;
1621 vlc_cond_signal( &p_owner->wait_acknowledge );
1627 bool b_has_more = false;
1629 DecoderWaitUnblock( p_dec, &b_reject );
1631 if( p_owner->b_buffering )
1633 vlc_mutex_unlock( &p_owner->lock );
1638 if( p_owner->buffer.p_block )
1640 p_sout_block = p_owner->buffer.p_block;
1642 p_owner->buffer.p_block = p_sout_block->p_next;
1643 p_owner->buffer.i_count--;
1645 b_has_more = p_owner->buffer.p_block != NULL;
1647 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1649 p_sout_block->p_next = NULL;
1651 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1652 &p_sout_block->i_length,
1653 &p_sout_block->i_rate, INT64_MAX, b_telx );
1655 vlc_mutex_unlock( &p_owner->lock );
1658 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1660 block_Release( p_sout_block );
1664 vlc_mutex_lock( &p_owner->lock );
1665 if( !p_owner->buffer.p_block )
1667 vlc_mutex_unlock( &p_owner->lock );
1674 static void DecoderFlushBuffering( decoder_t *p_dec )
1676 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1678 vlc_assert_locked( &p_owner->lock );
1680 while( p_owner->buffer.p_picture )
1682 picture_t *p_picture = p_owner->buffer.p_picture;
1684 p_owner->buffer.p_picture = p_picture->p_next;
1685 p_owner->buffer.i_count--;
1687 if( p_owner->p_vout )
1689 vout_ReleasePicture( p_owner->p_vout, p_picture );
1692 if( !p_owner->buffer.p_picture )
1693 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1695 while( p_owner->buffer.p_audio )
1697 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1699 p_owner->buffer.p_audio = p_audio->p_next;
1700 p_owner->buffer.i_count--;
1702 aout_BufferFree( p_audio );
1704 if( !p_owner->buffer.p_audio )
1705 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1707 while( p_owner->buffer.p_subpic )
1709 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1711 p_owner->buffer.p_subpic = p_subpic->p_next;
1712 p_owner->buffer.i_count--;
1714 subpicture_Delete( p_subpic );
1716 if( !p_owner->buffer.p_subpic )
1717 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1719 if( p_owner->buffer.p_block )
1721 block_ChainRelease( p_owner->buffer.p_block );
1723 p_owner->buffer.i_count = 0;
1724 p_owner->buffer.p_block = NULL;
1725 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1729 /* This function process a block for sout
1731 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1733 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1734 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1735 block_t *p_sout_block;
1737 while( ( p_sout_block =
1738 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1740 if( !p_owner->p_sout_input )
1742 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1744 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1745 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1746 if( p_dec->fmt_in.psz_language )
1748 free( p_owner->sout.psz_language );
1749 p_owner->sout.psz_language =
1750 strdup( p_dec->fmt_in.psz_language );
1753 p_owner->p_sout_input =
1754 sout_InputNew( p_owner->p_sout,
1757 if( p_owner->p_sout_input == NULL )
1759 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1760 (char *)&p_owner->sout.i_codec );
1761 p_dec->b_error = true;
1763 while( p_sout_block )
1765 block_t *p_next = p_sout_block->p_next;
1766 block_Release( p_sout_block );
1767 p_sout_block = p_next;
1773 while( p_sout_block )
1775 block_t *p_next = p_sout_block->p_next;
1777 p_sout_block->p_next = NULL;
1779 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1781 p_sout_block = p_next;
1786 /* This function process a video block
1788 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1790 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1792 if( p_owner->p_packetizer )
1794 block_t *p_packetized_block;
1795 decoder_t *p_packetizer = p_owner->p_packetizer;
1797 while( (p_packetized_block =
1798 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1800 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1802 es_format_Clean( &p_dec->fmt_in );
1803 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1805 if( p_packetizer->pf_get_cc )
1806 DecoderGetCc( p_dec, p_packetizer );
1808 while( p_packetized_block )
1810 block_t *p_next = p_packetized_block->p_next;
1811 p_packetized_block->p_next = NULL;
1813 DecoderDecodeVideo( p_dec, p_packetized_block );
1815 p_packetized_block = p_next;
1818 /* The packetizer does not output a block that tell the decoder to flush
1822 block_t *p_null = DecoderBlockFlushNew();
1824 DecoderDecodeVideo( p_dec, p_null );
1829 DecoderDecodeVideo( p_dec, p_block );
1832 if( b_flush && p_owner->p_vout )
1833 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1836 /* This function process a audio block
1838 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1840 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1842 if( p_owner->p_packetizer )
1844 block_t *p_packetized_block;
1845 decoder_t *p_packetizer = p_owner->p_packetizer;
1847 while( (p_packetized_block =
1848 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1850 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1852 es_format_Clean( &p_dec->fmt_in );
1853 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1856 while( p_packetized_block )
1858 block_t *p_next = p_packetized_block->p_next;
1859 p_packetized_block->p_next = NULL;
1861 DecoderDecodeAudio( p_dec, p_packetized_block );
1863 p_packetized_block = p_next;
1866 /* The packetizer does not output a block that tell the decoder to flush
1870 block_t *p_null = DecoderBlockFlushNew();
1872 DecoderDecodeAudio( p_dec, p_null );
1877 DecoderDecodeAudio( p_dec, p_block );
1880 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1881 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1884 /* This function process a subtitle block
1886 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1888 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1889 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1891 input_thread_t *p_input = p_owner->p_input;
1892 vout_thread_t *p_vout;
1893 subpicture_t *p_spu;
1895 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1897 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1898 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1899 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1901 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1902 if( p_vout && p_owner->p_spu_vout == p_vout )
1904 /* Preroll does not work very well with subtitle */
1905 if( p_spu->i_start > VLC_TS_INVALID &&
1906 p_spu->i_start < p_owner->i_preroll_end &&
1907 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1909 subpicture_Delete( p_spu );
1913 DecoderPlaySpu( p_dec, p_spu, b_telx );
1918 subpicture_Delete( p_spu );
1921 vlc_object_release( p_vout );
1924 if( b_flush && p_owner->p_spu_vout )
1926 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1928 if( p_vout && p_owner->p_spu_vout == p_vout )
1929 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR,
1930 p_owner->i_spu_channel );
1933 vlc_object_release( p_vout );
1938 static void DecoderProcessOnFlush( decoder_t *p_dec )
1940 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1942 vlc_mutex_lock( &p_owner->lock );
1943 DecoderFlushBuffering( p_dec );
1945 if( p_owner->b_flushing )
1947 p_owner->b_flushing = false;
1948 vlc_cond_signal( &p_owner->wait_acknowledge );
1950 vlc_mutex_unlock( &p_owner->lock );
1956 * \param p_dec the decoder object
1957 * \param p_block the block to decode
1958 * \return VLC_SUCCESS or an error code
1960 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1962 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1963 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1965 if( p_block && p_block->i_buffer <= 0 )
1967 assert( !b_flush_request );
1968 block_Release( p_block );
1973 if( p_owner->b_packetizer )
1976 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1978 DecoderProcessSout( p_dec, p_block );
1983 bool b_flush = false;
1987 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1988 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1990 b_flush = !b_flushing && b_flush_request;
1992 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1995 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1997 DecoderProcessAudio( p_dec, p_block, b_flush );
1999 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2001 DecoderProcessVideo( p_dec, p_block, b_flush );
2003 else if( p_dec->fmt_out.i_cat == SPU_ES )
2005 DecoderProcessSpu( p_dec, p_block, b_flush );
2009 msg_Err( p_dec, "unknown ES format" );
2010 p_dec->b_error = true;
2015 if( b_flush_request )
2016 DecoderProcessOnFlush( p_dec );
2019 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2021 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2025 block_Release( p_block );
2027 if( b_flush_request )
2028 DecoderProcessOnFlush( p_dec );
2033 * Destroys a decoder object
2035 * \param p_dec the decoder object
2038 static void DeleteDecoder( decoder_t * p_dec )
2040 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2042 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2043 (char*)&p_dec->fmt_in.i_codec,
2044 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2046 /* Free all packets still in the decoder fifo. */
2047 block_FifoEmpty( p_owner->p_fifo );
2048 block_FifoRelease( p_owner->p_fifo );
2051 vlc_mutex_lock( &p_owner->lock );
2052 DecoderFlushBuffering( p_dec );
2053 vlc_mutex_unlock( &p_owner->lock );
2056 if( p_owner->p_aout_input )
2057 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2058 if( p_owner->p_aout )
2060 input_resource_RequestAout( p_owner->p_input->p->p_resource,
2062 input_SendEventAout( p_owner->p_input );
2063 p_owner->p_aout = NULL;
2065 if( p_owner->p_vout )
2067 /* Hack to make sure all the the pictures are freed by the decoder
2068 * and that the vout is not paused anymore */
2069 vout_Reset( p_owner->p_vout );
2072 input_resource_RequestVout( p_owner->p_input->p->p_resource, p_owner->p_vout, NULL, true );
2073 input_SendEventVout( p_owner->p_input );
2077 if( p_owner->p_sout_input )
2079 sout_InputDelete( p_owner->p_sout_input );
2080 es_format_Clean( &p_owner->sout );
2084 if( p_dec->fmt_out.i_cat == SPU_ES )
2086 vout_thread_t *p_vout;
2088 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2091 if( p_owner->p_spu_vout == p_vout )
2092 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2093 vlc_object_release( p_vout );
2097 es_format_Clean( &p_dec->fmt_in );
2098 es_format_Clean( &p_dec->fmt_out );
2099 if( p_dec->p_description )
2100 vlc_meta_Delete( p_dec->p_description );
2101 es_format_Clean( &p_owner->fmt_description );
2102 if( p_owner->p_description )
2103 vlc_meta_Delete( p_owner->p_description );
2105 if( p_owner->p_packetizer )
2107 module_unneed( p_owner->p_packetizer,
2108 p_owner->p_packetizer->p_module );
2109 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2110 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2111 if( p_owner->p_packetizer->p_description )
2112 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2113 vlc_object_release( p_owner->p_packetizer );
2116 vlc_cond_destroy( &p_owner->wait_acknowledge );
2117 vlc_cond_destroy( &p_owner->wait_request );
2118 vlc_mutex_destroy( &p_owner->lock );
2120 vlc_object_release( p_dec );
2125 /*****************************************************************************
2126 * Buffers allocation callbacks for the decoders
2127 *****************************************************************************/
2128 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2130 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2132 vlc_assert_locked( &p_owner->lock );
2134 p_owner->b_fmt_description = true;
2136 /* Copy es_format */
2137 es_format_Clean( &p_owner->fmt_description );
2138 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2140 /* Move p_description */
2141 if( p_owner->p_description && p_dec->p_description )
2142 vlc_meta_Delete( p_owner->p_description );
2143 p_owner->p_description = p_dec->p_description;
2144 p_dec->p_description = NULL;
2146 static vout_thread_t *aout_request_vout( void *p_private,
2147 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2149 decoder_t *p_dec = p_private;
2150 input_thread_t *p_input = p_dec->p_owner->p_input;
2152 p_vout = input_resource_RequestVout( p_input->p->p_resource, p_vout, p_fmt, b_recyle );
2153 input_SendEventVout( p_input );
2158 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2160 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2161 aout_buffer_t *p_buffer;
2163 if( p_owner->p_aout_input != NULL &&
2164 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2165 p_dec->fmt_out.audio.i_original_channels !=
2166 p_owner->audio.i_original_channels ||
2167 p_dec->fmt_out.audio.i_bytes_per_frame !=
2168 p_owner->audio.i_bytes_per_frame ) )
2170 aout_input_t *p_aout_input = p_owner->p_aout_input;
2172 /* Parameters changed, restart the aout */
2173 vlc_mutex_lock( &p_owner->lock );
2175 DecoderFlushBuffering( p_dec );
2177 p_owner->p_aout_input = NULL;
2178 aout_DecDelete( p_owner->p_aout, p_aout_input );
2180 vlc_mutex_unlock( &p_owner->lock );
2183 if( p_owner->p_aout_input == NULL )
2185 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2186 audio_sample_format_t format;
2187 aout_input_t *p_aout_input;
2188 aout_instance_t *p_aout;
2189 aout_request_vout_t request_vout;
2191 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2192 p_owner->audio = p_dec->fmt_out.audio;
2194 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2195 if( i_force_dolby &&
2196 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2197 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2199 if( i_force_dolby == 1 )
2201 format.i_original_channels = format.i_original_channels |
2202 AOUT_CHAN_DOLBYSTEREO;
2204 else /* i_force_dolby == 2 */
2206 format.i_original_channels = format.i_original_channels &
2207 ~AOUT_CHAN_DOLBYSTEREO;
2211 request_vout.pf_request_vout = aout_request_vout;
2212 request_vout.p_private = p_dec;
2214 p_aout = p_owner->p_aout;
2216 p_aout = input_resource_RequestAout( p_owner->p_input->p->p_resource, NULL );
2217 p_aout_input = aout_DecNew( p_dec, &p_aout,
2218 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2220 vlc_mutex_lock( &p_owner->lock );
2222 p_owner->p_aout = p_aout;
2223 p_owner->p_aout_input = p_aout_input;
2224 DecoderUpdateFormatLocked( p_dec );
2226 vlc_mutex_unlock( &p_owner->lock );
2228 input_SendEventAout( p_owner->p_input );
2230 if( p_owner->p_aout_input == NULL )
2232 msg_Err( p_dec, "failed to create audio output" );
2233 p_dec->b_error = true;
2236 p_dec->fmt_out.audio.i_bytes_per_frame =
2237 p_owner->audio.i_bytes_per_frame;
2240 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2245 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2247 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2249 aout_DecDeleteBuffer( p_owner->p_aout,
2250 p_owner->p_aout_input, p_buffer );
2253 static picture_t *vout_new_buffer( decoder_t *p_dec )
2255 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2257 if( p_owner->p_vout == NULL ||
2258 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2259 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2260 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2261 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2262 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2264 vout_thread_t *p_vout;
2266 if( !p_dec->fmt_out.video.i_width ||
2267 !p_dec->fmt_out.video.i_height )
2269 /* Can't create a new vout without display size */
2273 if( !p_dec->fmt_out.video.i_visible_width ||
2274 !p_dec->fmt_out.video.i_visible_height )
2276 if( p_dec->fmt_in.video.i_visible_width &&
2277 p_dec->fmt_in.video.i_visible_height )
2279 p_dec->fmt_out.video.i_visible_width =
2280 p_dec->fmt_in.video.i_visible_width;
2281 p_dec->fmt_out.video.i_visible_height =
2282 p_dec->fmt_in.video.i_visible_height;
2286 p_dec->fmt_out.video.i_visible_width =
2287 p_dec->fmt_out.video.i_width;
2288 p_dec->fmt_out.video.i_visible_height =
2289 p_dec->fmt_out.video.i_height;
2293 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2294 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2296 p_dec->fmt_out.video.i_visible_height = 1080;
2297 if( !(p_dec->fmt_out.video.i_sar_num % 136))
2299 p_dec->fmt_out.video.i_sar_num *= 135;
2300 p_dec->fmt_out.video.i_sar_den *= 136;
2302 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2305 if( !p_dec->fmt_out.video.i_sar_num ||
2306 !p_dec->fmt_out.video.i_sar_den )
2308 p_dec->fmt_out.video.i_sar_num = 1;
2309 p_dec->fmt_out.video.i_sar_den = 1;
2312 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2313 &p_dec->fmt_out.video.i_sar_den,
2314 p_dec->fmt_out.video.i_sar_num,
2315 p_dec->fmt_out.video.i_sar_den, 50000 );
2317 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2318 p_owner->video = p_dec->fmt_out.video;
2320 vlc_mutex_lock( &p_owner->lock );
2322 DecoderFlushBuffering( p_dec );
2324 p_vout = p_owner->p_vout;
2325 p_owner->p_vout = NULL;
2326 vlc_mutex_unlock( &p_owner->lock );
2328 p_vout = input_resource_RequestVout( p_owner->p_input->p->p_resource,
2329 p_vout, &p_dec->fmt_out.video, true );
2331 vlc_mutex_lock( &p_owner->lock );
2332 p_owner->p_vout = p_vout;
2334 DecoderUpdateFormatLocked( p_dec );
2336 vlc_mutex_unlock( &p_owner->lock );
2338 input_SendEventVout( p_owner->p_input );
2339 if( p_vout == NULL )
2341 msg_Err( p_dec, "failed to create video output" );
2342 p_dec->b_error = true;
2347 /* Get a new picture
2351 if( p_dec->b_die || p_dec->b_error )
2354 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2358 if( DecoderIsFlushing( p_dec ) )
2362 DecoderSignalBuffering( p_dec, true );
2364 /* Check the decoder doesn't leak pictures */
2365 vout_FixLeaks( p_owner->p_vout );
2367 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2368 msleep( VOUT_OUTMEM_SLEEP );
2372 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2374 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2377 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2379 vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2382 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2384 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2387 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2388 const subpicture_updater_t *p_updater )
2390 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2391 vout_thread_t *p_vout = NULL;
2392 subpicture_t *p_subpic;
2393 int i_attempts = 30;
2395 while( i_attempts-- )
2397 if( p_dec->b_die || p_dec->b_error )
2400 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2404 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2409 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2413 if( p_owner->p_spu_vout != p_vout )
2415 vlc_mutex_lock( &p_owner->lock );
2417 DecoderFlushBuffering( p_dec );
2419 vlc_mutex_unlock( &p_owner->lock );
2421 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_REGISTER,
2422 &p_owner->i_spu_channel );
2423 p_owner->i_spu_order = 0;
2424 p_owner->p_spu_vout = p_vout;
2427 p_subpic = subpicture_New( p_updater );
2430 p_subpic->i_channel = p_owner->i_spu_channel;
2431 p_subpic->i_order = p_owner->i_spu_order++;
2432 p_subpic->b_subtitle = true;
2435 vlc_object_release( p_vout );
2440 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2442 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2443 vout_thread_t *p_vout = NULL;
2445 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2446 if( !p_vout || p_owner->p_spu_vout != p_vout )
2449 vlc_object_release( p_vout );
2450 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2454 subpicture_Delete( p_subpic );
2456 vlc_object_release( p_vout );