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>
44 #include <vlc_modules.h>
46 #include "audio_output/aout_internal.h"
47 #include "stream_output/stream_output.h"
48 #include "input_internal.h"
54 #include "../video_output/vout_control.h"
56 static decoder_t *CreateDecoder( vlc_object_t *, input_thread_t *,
57 es_format_t *, bool, input_resource_t *,
58 sout_instance_t *p_sout );
59 static void DeleteDecoder( decoder_t * );
61 static void *DecoderThread( void * );
62 static void DecoderProcess( decoder_t *, block_t * );
63 static void DecoderError( decoder_t *p_dec, block_t *p_block );
64 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
65 static void DecoderFlush( decoder_t * );
66 static void DecoderSignalBuffering( decoder_t *, bool );
67 static void DecoderFlushBuffering( decoder_t * );
69 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
71 /* Buffers allocation callbacks for the decoders */
72 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
74 static picture_t *vout_new_buffer( decoder_t * );
75 static void vout_del_buffer( decoder_t *, picture_t * );
76 static void vout_link_picture( decoder_t *, picture_t * );
77 static void vout_unlink_picture( decoder_t *, picture_t * );
79 static subpicture_t *spu_new_buffer( decoder_t *, const subpicture_updater_t * );
80 static void spu_del_buffer( decoder_t *, subpicture_t * );
82 struct decoder_owner_sys_t
84 int64_t i_preroll_end;
86 input_thread_t *p_input;
87 input_resource_t*p_resource;
88 input_clock_t *p_clock;
91 vout_thread_t *p_spu_vout;
95 sout_instance_t *p_sout;
96 sout_packetizer_input_t *p_sout_input;
100 /* Some decoders require already packetized data (ie. not truncated) */
101 decoder_t *p_packetizer;
104 /* Current format in use by the output */
105 video_format_t video;
106 audio_format_t audio;
110 bool b_fmt_description;
111 es_format_t fmt_description;
112 vlc_meta_t *p_description;
115 block_fifo_t *p_fifo;
117 /* Lock for communication with decoder thread */
119 vlc_cond_t wait_request;
120 vlc_cond_t wait_acknowledge;
122 /* -- These variables need locking on write(only) -- */
123 audio_output_t *p_aout;
125 vout_thread_t *p_vout;
127 /* -- Theses variables need locking on read *and* write -- */
146 picture_t *p_picture;
147 picture_t **pp_picture_next;
149 subpicture_t *p_subpic;
150 subpicture_t **pp_subpic_next;
152 aout_buffer_t *p_audio;
153 aout_buffer_t **pp_audio_next;
156 block_t **pp_block_next;
167 decoder_t *pp_decoder[4];
174 #define DECODER_MAX_BUFFERING_COUNT (4)
175 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
176 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
178 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
179 * a bogus PTS and won't be displayed */
180 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
183 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
186 /*****************************************************************************
188 *****************************************************************************/
189 picture_t *decoder_NewPicture( decoder_t *p_decoder )
191 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
193 msg_Warn( p_decoder, "can't get output picture" );
196 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
198 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
200 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
202 p_decoder->pf_picture_link( p_decoder, p_picture );
204 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
206 p_decoder->pf_picture_unlink( p_decoder, p_picture );
209 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
211 if( !p_decoder->pf_aout_buffer_new )
213 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
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 );
260 /* TODO: pass p_sout through p_resource? -- Courmisch */
261 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
262 es_format_t *fmt, input_clock_t *p_clock,
263 input_resource_t *p_resource,
264 sout_instance_t *p_sout )
266 decoder_t *p_dec = NULL;
267 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
270 /* Create the decoder configuration structure */
271 p_dec = CreateDecoder( p_parent, p_input, fmt,
272 p_sout != NULL, p_resource, p_sout );
275 msg_Err( p_parent, "could not create %s", psz_type );
276 dialog_Fatal( p_parent, _("Streaming / Transcoding failed"),
277 _("VLC could not open the %s module."),
278 vlc_gettext( psz_type ) );
282 if( !p_dec->p_module )
284 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
286 DeleteDecoder( p_dec );
290 p_dec->p_owner->p_clock = p_clock;
291 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
293 if( p_dec->fmt_out.i_cat == AUDIO_ES )
294 i_priority = VLC_THREAD_PRIORITY_AUDIO;
296 i_priority = VLC_THREAD_PRIORITY_VIDEO;
298 /* Spawn the decoder thread */
299 if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
301 msg_Err( p_dec, "cannot spawn decoder thread" );
302 module_unneed( p_dec, p_dec->p_module );
303 DeleteDecoder( p_dec );
312 * Spawns a new decoder thread from the input thread
314 * \param p_input the input thread
315 * \param p_es the es descriptor
316 * \return the spawned decoder object
318 decoder_t *input_DecoderNew( input_thread_t *p_input,
319 es_format_t *fmt, input_clock_t *p_clock,
320 sout_instance_t *p_sout )
322 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
323 p_input->p->p_resource, p_sout );
327 * Spawn a decoder thread outside of the input thread.
329 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, es_format_t *fmt,
330 input_resource_t *p_resource )
332 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
337 * Kills a decoder thread and waits until it's finished
339 * \param p_input the input thread
340 * \param p_es the es descriptor
343 void input_DecoderDelete( decoder_t *p_dec )
345 decoder_owner_sys_t *p_owner = p_dec->p_owner;
347 vlc_cancel( p_owner->thread );
349 /* Make sure we aren't paused/buffering/waiting/decoding anymore */
350 vlc_mutex_lock( &p_owner->lock );
351 const bool b_was_paused = p_owner->b_paused;
352 p_owner->b_paused = false;
353 p_owner->b_buffering = false;
354 p_owner->b_flushing = true;
355 p_owner->b_exit = true;
356 vlc_cond_signal( &p_owner->wait_request );
357 vlc_mutex_unlock( &p_owner->lock );
359 vlc_join( p_owner->thread, NULL );
360 p_owner->b_paused = b_was_paused;
362 module_unneed( p_dec, p_dec->p_module );
365 if( p_dec->p_owner->cc.b_supported )
368 for( i = 0; i < 4; i++ )
369 input_DecoderSetCcState( p_dec, false, i );
373 DeleteDecoder( p_dec );
377 * Put a block_t in the decoder's fifo.
378 * Thread-safe w.r.t. the decoder. May be a cancellation point.
380 * \param p_dec the decoder object
381 * \param p_block the data block
383 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
385 decoder_owner_sys_t *p_owner = p_dec->p_owner;
389 /* The fifo is not consummed when buffering and so will
391 * There is no need to lock as b_buffering is never modify
392 * inside decoder thread. */
393 if( !p_owner->b_buffering )
394 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
397 else if( block_FifoSize( p_owner->p_fifo ) > 50*1024*1024 /* 50 MiB */ )
399 else if( block_FifoSize( p_owner->p_fifo ) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
402 /* FIXME: ideally we would check the time amount of data
403 * in the FIFO instead of its size. */
404 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
405 "consumed quickly enough), resetting fifo!" );
406 block_FifoEmpty( p_owner->p_fifo );
409 block_FifoPut( p_owner->p_fifo, p_block );
412 bool input_DecoderIsEmpty( decoder_t * p_dec )
414 decoder_owner_sys_t *p_owner = p_dec->p_owner;
415 assert( !p_owner->b_buffering );
417 bool b_empty = block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
420 vlc_mutex_lock( &p_owner->lock );
421 /* TODO subtitles support */
422 if( p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout )
423 b_empty = vout_IsEmpty( p_owner->p_vout );
424 else if( p_dec->fmt_out.i_cat == AUDIO_ES && p_owner->p_aout )
425 b_empty = aout_DecIsEmpty( p_owner->p_aout );
426 vlc_mutex_unlock( &p_owner->lock );
431 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
433 decoder_owner_sys_t *p_owner = p_dec->p_owner;
436 vlc_mutex_lock( &p_owner->lock );
437 for( i = 0; i < 4; i++ )
438 pb_present[i] = p_owner->cc.pb_present[i];
439 vlc_mutex_unlock( &p_owner->lock );
441 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
443 decoder_owner_sys_t *p_owner = p_dec->p_owner;
445 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
447 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
452 static const vlc_fourcc_t fcc[4] = {
453 VLC_FOURCC('c', 'c', '1', ' '),
454 VLC_FOURCC('c', 'c', '2', ' '),
455 VLC_FOURCC('c', 'c', '3', ' '),
456 VLC_FOURCC('c', 'c', '4', ' '),
461 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
462 p_cc = CreateDecoder( VLC_OBJECT(p_dec), p_owner->p_input, &fmt,
463 false, p_owner->p_resource, p_owner->p_sout );
466 msg_Err( p_dec, "could not create decoder" );
467 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
468 _("VLC could not open the decoder module.") );
471 else if( !p_cc->p_module )
473 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
474 DeleteDecoder( p_cc );
477 p_cc->p_owner->p_clock = p_owner->p_clock;
479 vlc_mutex_lock( &p_owner->lock );
480 p_owner->cc.pp_decoder[i_channel] = p_cc;
481 vlc_mutex_unlock( &p_owner->lock );
487 vlc_mutex_lock( &p_owner->lock );
488 p_cc = p_owner->cc.pp_decoder[i_channel];
489 p_owner->cc.pp_decoder[i_channel] = NULL;
490 vlc_mutex_unlock( &p_owner->lock );
494 module_unneed( p_cc, p_cc->p_module );
495 DeleteDecoder( p_cc );
500 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
502 decoder_owner_sys_t *p_owner = p_dec->p_owner;
505 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
508 vlc_mutex_lock( &p_owner->lock );
509 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
510 vlc_mutex_unlock( &p_owner->lock );
514 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
516 decoder_owner_sys_t *p_owner = p_dec->p_owner;
518 vlc_mutex_lock( &p_owner->lock );
520 assert( !p_owner->b_paused || !b_paused );
522 p_owner->b_paused = b_paused;
523 p_owner->pause.i_date = i_date;
524 p_owner->pause.i_ignore = 0;
525 vlc_cond_signal( &p_owner->wait_request );
527 DecoderOutputChangePause( p_dec, b_paused, i_date );
529 vlc_mutex_unlock( &p_owner->lock );
532 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
534 decoder_owner_sys_t *p_owner = p_dec->p_owner;
536 vlc_mutex_lock( &p_owner->lock );
537 p_owner->i_ts_delay = i_delay;
538 vlc_mutex_unlock( &p_owner->lock );
541 void input_DecoderStartBuffering( decoder_t *p_dec )
543 decoder_owner_sys_t *p_owner = p_dec->p_owner;
545 vlc_mutex_lock( &p_owner->lock );
547 DecoderFlush( p_dec );
549 p_owner->buffer.b_first = true;
550 p_owner->buffer.b_full = false;
551 p_owner->buffer.i_count = 0;
553 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
554 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
556 p_owner->buffer.p_picture = NULL;
557 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
559 p_owner->buffer.p_subpic = NULL;
560 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
562 p_owner->buffer.p_audio = NULL;
563 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
565 p_owner->buffer.p_block = NULL;
566 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
569 p_owner->b_buffering = true;
571 vlc_cond_signal( &p_owner->wait_request );
573 vlc_mutex_unlock( &p_owner->lock );
576 void input_DecoderStopBuffering( decoder_t *p_dec )
578 decoder_owner_sys_t *p_owner = p_dec->p_owner;
580 vlc_mutex_lock( &p_owner->lock );
582 p_owner->b_buffering = false;
584 vlc_cond_signal( &p_owner->wait_request );
586 vlc_mutex_unlock( &p_owner->lock );
589 void input_DecoderWaitBuffering( decoder_t *p_dec )
591 decoder_owner_sys_t *p_owner = p_dec->p_owner;
593 vlc_mutex_lock( &p_owner->lock );
595 while( p_owner->b_buffering && !p_owner->buffer.b_full )
597 block_FifoWake( p_owner->p_fifo );
598 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
601 vlc_mutex_unlock( &p_owner->lock );
604 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
606 decoder_owner_sys_t *p_owner = p_dec->p_owner;
610 vlc_mutex_lock( &p_owner->lock );
611 if( p_dec->fmt_out.i_cat == VIDEO_ES )
613 if( p_owner->b_paused && p_owner->p_vout )
615 vout_NextPicture( p_owner->p_vout, pi_duration );
616 p_owner->pause.i_ignore++;
617 vlc_cond_signal( &p_owner->wait_request );
622 /* TODO subtitle should not be flushed */
623 DecoderFlush( p_dec );
625 vlc_mutex_unlock( &p_owner->lock );
628 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
630 decoder_owner_sys_t *p_owner = p_dec->p_owner;
633 vlc_mutex_lock( &p_owner->lock );
634 b_changed = p_owner->b_fmt_description;
638 es_format_Copy( p_fmt, &p_owner->fmt_description );
643 if( p_owner->p_description )
645 *pp_meta = vlc_meta_New();
647 vlc_meta_Merge( *pp_meta, p_owner->p_description );
650 p_owner->b_fmt_description = false;
652 vlc_mutex_unlock( &p_owner->lock );
656 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
658 decoder_owner_sys_t *p_owner = p_dec->p_owner;
660 return block_FifoSize( p_owner->p_fifo );
663 void input_DecoderGetObjects( decoder_t *p_dec,
664 vout_thread_t **pp_vout, audio_output_t **pp_aout )
666 decoder_owner_sys_t *p_owner = p_dec->p_owner;
668 vlc_mutex_lock( &p_owner->lock );
670 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
672 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
673 vlc_mutex_unlock( &p_owner->lock );
676 /*****************************************************************************
678 *****************************************************************************/
679 static int DecoderGetInputAttachments( decoder_t *p_dec,
680 input_attachment_t ***ppp_attachment,
683 input_thread_t *p_input = p_dec->p_owner->p_input;
685 if( unlikely(p_input == NULL) )
687 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
688 ppp_attachment, pi_attachment );
690 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
692 decoder_owner_sys_t *p_owner = p_dec->p_owner;
694 vlc_mutex_lock( &p_owner->lock );
695 if( p_owner->b_buffering || p_owner->b_paused )
696 i_ts = VLC_TS_INVALID;
697 vlc_mutex_unlock( &p_owner->lock );
699 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
702 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
703 return VLC_TS_INVALID;
707 static int DecoderGetDisplayRate( decoder_t *p_dec )
709 decoder_owner_sys_t *p_owner = p_dec->p_owner;
711 if( !p_owner->p_clock )
712 return INPUT_RATE_DEFAULT;
713 return input_clock_GetRate( p_owner->p_clock );
717 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
719 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
720 "VLC probably does not support this sound or video format.",
722 dialog_Fatal( p_dec, _("No suitable decoder module"),
723 _("VLC does not support the audio or video format \"%4.4s\". "
724 "Unfortunately there is no way for you to fix this."),
730 * Create a decoder object
732 * \param p_input the input thread
733 * \param p_es the es descriptor
734 * \param b_packetizer instead of a decoder
735 * \return the decoder object
737 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
738 input_thread_t *p_input,
739 es_format_t *fmt, bool b_packetizer,
740 input_resource_t *p_resource,
741 sout_instance_t *p_sout )
744 decoder_owner_sys_t *p_owner;
745 es_format_t null_es_format;
747 p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
751 p_dec->pf_decode_audio = NULL;
752 p_dec->pf_decode_video = NULL;
753 p_dec->pf_decode_sub = NULL;
754 p_dec->pf_get_cc = NULL;
755 p_dec->pf_packetize = NULL;
757 /* Initialize the decoder */
758 p_dec->p_module = NULL;
760 memset( &null_es_format, 0, sizeof(es_format_t) );
761 es_format_Copy( &p_dec->fmt_in, fmt );
762 es_format_Copy( &p_dec->fmt_out, &null_es_format );
764 p_dec->p_description = NULL;
766 /* Allocate our private structure for the decoder */
767 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
768 if( unlikely(p_owner == NULL) )
770 vlc_object_release( p_dec );
773 p_owner->i_preroll_end = VLC_TS_INVALID;
774 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
775 p_owner->p_input = p_input;
776 p_owner->p_resource = p_resource;
777 p_owner->p_aout = NULL;
778 p_owner->p_vout = NULL;
779 p_owner->p_spu_vout = NULL;
780 p_owner->i_spu_channel = 0;
781 p_owner->i_spu_order = 0;
782 p_owner->p_sout = p_sout;
783 p_owner->p_sout_input = NULL;
784 p_owner->p_packetizer = NULL;
785 p_owner->b_packetizer = b_packetizer;
788 p_owner->p_fifo = block_FifoNew();
789 if( unlikely(p_owner->p_fifo == NULL) )
792 vlc_object_release( p_dec );
796 /* Set buffers allocation callbacks for the decoders */
797 p_dec->pf_aout_buffer_new = aout_new_buffer;
798 p_dec->pf_vout_buffer_new = vout_new_buffer;
799 p_dec->pf_vout_buffer_del = vout_del_buffer;
800 p_dec->pf_picture_link = vout_link_picture;
801 p_dec->pf_picture_unlink = vout_unlink_picture;
802 p_dec->pf_spu_buffer_new = spu_new_buffer;
803 p_dec->pf_spu_buffer_del = spu_del_buffer;
805 p_dec->pf_get_attachments = DecoderGetInputAttachments;
806 p_dec->pf_get_display_date = DecoderGetDisplayDate;
807 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
809 /* Find a suitable decoder/packetizer module */
811 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
813 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
815 /* Check if decoder requires already packetized data */
817 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
819 p_owner->p_packetizer =
820 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
821 if( p_owner->p_packetizer )
823 es_format_Copy( &p_owner->p_packetizer->fmt_in,
826 es_format_Copy( &p_owner->p_packetizer->fmt_out,
829 p_owner->p_packetizer->p_module =
830 module_need( p_owner->p_packetizer,
831 "packetizer", "$packetizer", false );
833 if( !p_owner->p_packetizer->p_module )
835 es_format_Clean( &p_owner->p_packetizer->fmt_in );
836 vlc_object_release( p_owner->p_packetizer );
841 /* Copy ourself the input replay gain */
842 if( fmt->i_cat == AUDIO_ES )
844 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
846 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
848 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
849 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
851 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
853 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
854 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
860 vlc_mutex_init( &p_owner->lock );
861 vlc_cond_init( &p_owner->wait_request );
862 vlc_cond_init( &p_owner->wait_acknowledge );
864 p_owner->b_fmt_description = false;
865 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
866 p_owner->p_description = NULL;
868 p_owner->b_exit = false;
870 p_owner->b_paused = false;
871 p_owner->pause.i_date = VLC_TS_INVALID;
872 p_owner->pause.i_ignore = 0;
874 p_owner->b_buffering = false;
875 p_owner->buffer.b_first = true;
876 p_owner->buffer.b_full = false;
877 p_owner->buffer.i_count = 0;
878 p_owner->buffer.p_picture = NULL;
879 p_owner->buffer.p_subpic = NULL;
880 p_owner->buffer.p_audio = NULL;
881 p_owner->buffer.p_block = NULL;
883 p_owner->b_flushing = false;
886 p_owner->cc.b_supported = false;
889 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
890 p_owner->cc.b_supported = true;
891 if( p_dec->pf_get_cc )
892 p_owner->cc.b_supported = true;
895 for( unsigned i = 0; i < 4; i++ )
897 p_owner->cc.pb_present[i] = false;
898 p_owner->cc.pp_decoder[i] = NULL;
900 p_owner->i_ts_delay = 0;
905 * The decoding main loop
907 * \param p_dec the decoder
909 static void *DecoderThread( void *p_data )
911 decoder_t *p_dec = (decoder_t *)p_data;
912 decoder_owner_sys_t *p_owner = p_dec->p_owner;
914 /* The decoder's main loop */
917 block_t *p_block = block_FifoGet( p_owner->p_fifo );
919 /* Make sure there is no cancellation point other than this one^^.
920 * If you need one, be sure to push cleanup of p_block. */
921 DecoderSignalBuffering( p_dec, p_block == NULL );
925 int canc = vlc_savecancel();
927 if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
929 /* calling DecoderProcess() with NULL block will make
930 * decoders/packetizers flush their buffers */
931 block_Release( p_block );
936 DecoderError( p_dec, p_block );
938 DecoderProcess( p_dec, p_block );
940 vlc_restorecancel( canc );
946 static block_t *DecoderBlockFlushNew()
948 block_t *p_null = block_Alloc( 128 );
952 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
953 BLOCK_FLAG_CORRUPTED |
954 BLOCK_FLAG_CORE_FLUSH;
955 memset( p_null->p_buffer, 0, p_null->i_buffer );
960 static void DecoderFlush( decoder_t *p_dec )
962 decoder_owner_sys_t *p_owner = p_dec->p_owner;
964 vlc_assert_locked( &p_owner->lock );
967 block_FifoEmpty( p_owner->p_fifo );
969 /* Monitor for flush end */
970 p_owner->b_flushing = true;
971 vlc_cond_signal( &p_owner->wait_request );
973 /* Send a special block */
974 block_t *p_null = DecoderBlockFlushNew();
977 input_DecoderDecode( p_dec, p_null, false );
980 while( p_owner->b_flushing )
981 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
984 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
986 decoder_owner_sys_t *p_owner = p_dec->p_owner;
988 vlc_mutex_lock( &p_owner->lock );
990 if( p_owner->b_buffering )
993 p_owner->buffer.b_full = true;
994 vlc_cond_signal( &p_owner->wait_acknowledge );
997 vlc_mutex_unlock( &p_owner->lock );
1000 static bool DecoderIsFlushing( decoder_t *p_dec )
1002 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1005 vlc_mutex_lock( &p_owner->lock );
1007 b_flushing = p_owner->b_flushing;
1009 vlc_mutex_unlock( &p_owner->lock );
1014 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
1016 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1018 vlc_assert_locked( &p_owner->lock );
1020 while( !p_owner->b_flushing )
1022 if( p_owner->b_paused )
1024 if( p_owner->b_buffering && !p_owner->buffer.b_full )
1026 if( p_owner->pause.i_ignore > 0 )
1028 p_owner->pause.i_ignore--;
1034 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
1037 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1041 *pb_reject = p_owner->b_flushing;
1044 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1046 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1048 vlc_assert_locked( &p_owner->lock );
1050 /* XXX only audio and video output have to be paused.
1051 * - for sout it is useless
1052 * - for subs, it is done by the vout
1054 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1056 if( p_owner->p_aout )
1057 aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
1059 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1061 if( p_owner->p_vout )
1062 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1065 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1067 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1068 *pi_preroll = INT64_MAX;
1069 else if( p->i_dts > VLC_TS_INVALID )
1070 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1071 else if( p->i_pts > VLC_TS_INVALID )
1072 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1075 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1077 mtime_t current_date = mdate();
1079 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1080 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1082 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1083 * for teletext streams. */
1084 return current_date + 400000;
1089 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1090 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1092 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1093 input_clock_t *p_clock = p_owner->p_clock;
1095 vlc_assert_locked( &p_owner->lock );
1097 const mtime_t i_es_delay = p_owner->i_ts_delay;
1101 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1104 if( *pi_ts0 > VLC_TS_INVALID )
1106 *pi_ts0 += i_es_delay;
1107 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1108 *pi_ts1 += i_es_delay;
1109 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1110 *pi_ts0 = VLC_TS_INVALID;
1114 i_rate = input_clock_GetRate( p_clock );
1117 /* Do not create ephemere data because of rounding errors */
1118 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1122 *pi_duration = ( *pi_duration * i_rate +
1123 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1130 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1131 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1137 static bool DecoderIsExitRequested( decoder_t *p_dec )
1139 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1141 vlc_mutex_lock( &p_owner->lock );
1142 bool b_exit = p_owner->b_exit;
1143 vlc_mutex_unlock( &p_owner->lock );
1149 * If *pb_reject, it does nothing, otherwise it waits for the given
1150 * deadline or a flush request (in which case it set *pi_reject to true.
1152 static void DecoderWaitDate( decoder_t *p_dec,
1153 bool *pb_reject, mtime_t i_deadline )
1155 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1157 if( *pb_reject || i_deadline < 0 )
1162 vlc_mutex_lock( &p_owner->lock );
1163 if( p_owner->b_flushing || p_owner->b_exit )
1166 vlc_mutex_unlock( &p_owner->lock );
1169 int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1171 vlc_mutex_unlock( &p_owner->lock );
1177 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1178 int *pi_played_sum, int *pi_lost_sum )
1180 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1181 audio_output_t *p_aout = p_owner->p_aout;
1184 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1186 msg_Warn( p_dec, "non-dated audio buffer received" );
1188 aout_BufferFree( p_audio );
1193 vlc_mutex_lock( &p_owner->lock );
1195 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1197 p_audio->p_next = NULL;
1199 *p_owner->buffer.pp_audio_next = p_audio;
1200 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1202 p_owner->buffer.i_count++;
1203 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1204 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1206 p_owner->buffer.b_full = true;
1207 vlc_cond_signal( &p_owner->wait_acknowledge );
1213 bool b_has_more = false;
1215 DecoderWaitUnblock( p_dec, &b_reject );
1217 if( p_owner->b_buffering )
1219 vlc_mutex_unlock( &p_owner->lock );
1224 if( p_owner->buffer.p_audio )
1226 p_audio = p_owner->buffer.p_audio;
1228 p_owner->buffer.p_audio = p_audio->p_next;
1229 p_owner->buffer.i_count--;
1231 b_has_more = p_owner->buffer.p_audio != NULL;
1233 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1237 const bool b_dated = p_audio->i_pts > VLC_TS_INVALID;
1238 int i_rate = INPUT_RATE_DEFAULT;
1240 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1241 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1243 vlc_mutex_unlock( &p_owner->lock );
1246 p_audio->i_pts <= VLC_TS_INVALID ||
1247 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1248 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1251 DecoderWaitDate( p_dec, &b_reject,
1252 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1256 if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1257 *pi_played_sum += 1;
1258 *pi_lost_sum += aout_DecGetResetLost( p_aout );
1263 msg_Warn( p_dec, "received buffer in the future" );
1265 msg_Warn( p_dec, "non-dated audio buffer received" );
1268 aout_BufferFree( p_audio );
1274 vlc_mutex_lock( &p_owner->lock );
1275 if( !p_owner->buffer.p_audio )
1277 vlc_mutex_unlock( &p_owner->lock );
1283 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1285 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1286 aout_buffer_t *p_aout_buf;
1291 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1293 audio_output_t *p_aout = p_owner->p_aout;
1295 if( DecoderIsExitRequested( p_dec ) )
1297 /* It prevent freezing VLC in case of broken decoder */
1298 aout_DecDeleteBuffer( p_aout, p_aout_buf );
1300 block_Release( p_block );
1305 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1306 p_aout_buf->i_pts < p_owner->i_preroll_end )
1308 aout_DecDeleteBuffer( p_aout, p_aout_buf );
1312 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1314 msg_Dbg( p_dec, "End of audio preroll" );
1315 if( p_owner->p_aout )
1316 aout_DecFlush( p_owner->p_aout );
1318 p_owner->i_preroll_end = VLC_TS_INVALID;
1321 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1324 /* Update ugly stat */
1325 input_thread_t *p_input = p_owner->p_input;
1327 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1329 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1331 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1333 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1335 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1338 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1341 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1343 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1346 bool b_processed = false;
1350 assert( p_dec_cc->pf_get_cc != NULL );
1352 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1353 if( !p_owner->cc.b_supported )
1356 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1360 vlc_mutex_lock( &p_owner->lock );
1361 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1363 p_owner->cc.pb_present[i] |= pb_present[i];
1364 if( p_owner->cc.pp_decoder[i] )
1368 for( i = 0; i < 4; i++ )
1370 if( !p_owner->cc.pp_decoder[i] )
1373 if( i_cc_decoder > 1 )
1374 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1376 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1380 vlc_mutex_unlock( &p_owner->lock );
1383 block_Release( p_cc );
1386 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1387 int *pi_played_sum, int *pi_lost_sum )
1389 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1390 vout_thread_t *p_vout = p_owner->p_vout;
1391 bool b_first_buffered;
1393 if( p_picture->date <= VLC_TS_INVALID )
1395 msg_Warn( p_dec, "non-dated video buffer received" );
1397 vout_ReleasePicture( p_vout, p_picture );
1402 vlc_mutex_lock( &p_owner->lock );
1404 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1406 p_picture->p_next = NULL;
1408 *p_owner->buffer.pp_picture_next = p_picture;
1409 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1411 p_owner->buffer.i_count++;
1412 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1413 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1415 p_owner->buffer.b_full = true;
1416 vlc_cond_signal( &p_owner->wait_acknowledge );
1419 b_first_buffered = p_owner->buffer.p_picture != NULL;
1421 for( ;; b_first_buffered = false )
1423 bool b_has_more = false;
1427 DecoderWaitUnblock( p_dec, &b_reject );
1429 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1431 vlc_mutex_unlock( &p_owner->lock );
1434 bool b_buffering_first = p_owner->b_buffering;
1437 if( p_owner->buffer.p_picture )
1439 p_picture = p_owner->buffer.p_picture;
1441 p_owner->buffer.p_picture = p_picture->p_next;
1442 p_owner->buffer.i_count--;
1444 b_has_more = p_owner->buffer.p_picture != NULL;
1446 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1450 if( b_buffering_first )
1452 assert( p_owner->buffer.b_first );
1453 assert( !p_owner->buffer.i_count );
1454 msg_Dbg( p_dec, "Received first picture" );
1455 p_owner->buffer.b_first = false;
1456 p_picture->b_force = true;
1459 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1460 int i_rate = INPUT_RATE_DEFAULT;
1461 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1462 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1464 vlc_mutex_unlock( &p_owner->lock );
1467 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1472 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1474 /* Be sure to not display old picture after our own */
1475 vout_Flush( p_vout, p_picture->date );
1476 p_owner->i_last_rate = i_rate;
1478 vout_PutPicture( p_vout, p_picture );
1483 msg_Warn( p_dec, "early picture skipped" );
1485 msg_Warn( p_dec, "non-dated video buffer received" );
1488 vout_ReleasePicture( p_vout, p_picture );
1492 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1494 *pi_played_sum += i_tmp_display;
1495 *pi_lost_sum += i_tmp_lost;
1497 if( !b_has_more || b_buffering_first )
1500 vlc_mutex_lock( &p_owner->lock );
1501 if( !p_owner->buffer.p_picture )
1503 vlc_mutex_unlock( &p_owner->lock );
1509 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1511 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1515 int i_displayed = 0;
1517 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1519 vout_thread_t *p_vout = p_owner->p_vout;
1520 if( DecoderIsExitRequested( p_dec ) )
1522 /* It prevent freezing VLC in case of broken decoder */
1523 vout_ReleasePicture( p_vout, p_pic );
1525 block_Release( p_block );
1531 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1533 vout_ReleasePicture( p_vout, p_pic );
1537 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1539 msg_Dbg( p_dec, "End of video preroll" );
1541 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1543 p_owner->i_preroll_end = VLC_TS_INVALID;
1546 if( p_dec->pf_get_cc &&
1547 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1548 DecoderGetCc( p_dec, p_dec );
1550 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1553 /* Update ugly stat */
1554 input_thread_t *p_input = p_owner->p_input;
1556 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1558 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1560 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1562 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1565 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1568 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1572 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1575 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1576 vout_thread_t *p_vout = p_owner->p_spu_vout;
1579 if( p_subpic->i_start <= VLC_TS_INVALID && !b_telx )
1581 msg_Warn( p_dec, "non-dated spu buffer received" );
1582 subpicture_Delete( p_subpic );
1587 vlc_mutex_lock( &p_owner->lock );
1589 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1591 p_subpic->p_next = NULL;
1593 *p_owner->buffer.pp_subpic_next = p_subpic;
1594 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1596 p_owner->buffer.i_count++;
1597 /* XXX it is important to be full after the first one */
1598 if( p_owner->buffer.i_count > 0 )
1600 p_owner->buffer.b_full = true;
1601 vlc_cond_signal( &p_owner->wait_acknowledge );
1607 bool b_has_more = false;
1609 DecoderWaitUnblock( p_dec, &b_reject );
1611 if( p_owner->b_buffering )
1613 vlc_mutex_unlock( &p_owner->lock );
1618 if( p_owner->buffer.p_subpic )
1620 p_subpic = p_owner->buffer.p_subpic;
1622 p_owner->buffer.p_subpic = p_subpic->p_next;
1623 p_owner->buffer.i_count--;
1625 b_has_more = p_owner->buffer.p_subpic != NULL;
1627 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1631 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1632 NULL, INT64_MAX, b_telx );
1634 vlc_mutex_unlock( &p_owner->lock );
1636 if( p_subpic->i_start <= VLC_TS_INVALID )
1639 DecoderWaitDate( p_dec, &b_reject,
1640 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1643 vout_PutSubpicture( p_vout, p_subpic );
1645 subpicture_Delete( p_subpic );
1649 vlc_mutex_lock( &p_owner->lock );
1650 if( !p_owner->buffer.p_subpic )
1652 vlc_mutex_unlock( &p_owner->lock );
1659 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1662 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1664 assert( p_owner->p_clock );
1665 assert( !p_sout_block->p_next );
1667 vlc_mutex_lock( &p_owner->lock );
1669 if( p_owner->b_buffering || p_owner->buffer.p_block )
1671 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1673 p_owner->buffer.i_count++;
1674 /* XXX it is important to be full after the first one */
1675 if( p_owner->buffer.i_count > 0 )
1677 p_owner->buffer.b_full = true;
1678 vlc_cond_signal( &p_owner->wait_acknowledge );
1684 bool b_has_more = false;
1686 DecoderWaitUnblock( p_dec, &b_reject );
1688 if( p_owner->b_buffering )
1690 vlc_mutex_unlock( &p_owner->lock );
1695 if( p_owner->buffer.p_block )
1697 p_sout_block = p_owner->buffer.p_block;
1699 p_owner->buffer.p_block = p_sout_block->p_next;
1700 p_owner->buffer.i_count--;
1702 b_has_more = p_owner->buffer.p_block != NULL;
1704 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1706 p_sout_block->p_next = NULL;
1708 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1709 &p_sout_block->i_length, NULL, INT64_MAX, b_telx );
1711 vlc_mutex_unlock( &p_owner->lock );
1714 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1716 block_Release( p_sout_block );
1720 vlc_mutex_lock( &p_owner->lock );
1721 if( !p_owner->buffer.p_block )
1723 vlc_mutex_unlock( &p_owner->lock );
1731 static void DecoderFlushBuffering( decoder_t *p_dec )
1733 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1735 vlc_assert_locked( &p_owner->lock );
1737 while( p_owner->buffer.p_picture )
1739 picture_t *p_picture = p_owner->buffer.p_picture;
1741 p_owner->buffer.p_picture = p_picture->p_next;
1742 p_owner->buffer.i_count--;
1744 if( p_owner->p_vout )
1746 vout_ReleasePicture( p_owner->p_vout, p_picture );
1749 if( !p_owner->buffer.p_picture )
1750 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1752 while( p_owner->buffer.p_audio )
1754 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1756 p_owner->buffer.p_audio = p_audio->p_next;
1757 p_owner->buffer.i_count--;
1759 aout_BufferFree( p_audio );
1761 if( !p_owner->buffer.p_audio )
1762 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1764 while( p_owner->buffer.p_subpic )
1766 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1768 p_owner->buffer.p_subpic = p_subpic->p_next;
1769 p_owner->buffer.i_count--;
1771 subpicture_Delete( p_subpic );
1773 if( !p_owner->buffer.p_subpic )
1774 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1776 if( p_owner->buffer.p_block )
1778 block_ChainRelease( p_owner->buffer.p_block );
1780 p_owner->buffer.i_count = 0;
1781 p_owner->buffer.p_block = NULL;
1782 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1787 /* This function process a block for sout
1789 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1791 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1792 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1793 block_t *p_sout_block;
1795 while( ( p_sout_block =
1796 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1798 if( !p_owner->p_sout_input )
1800 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1802 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1803 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1804 if( p_dec->fmt_in.psz_language )
1806 free( p_owner->sout.psz_language );
1807 p_owner->sout.psz_language =
1808 strdup( p_dec->fmt_in.psz_language );
1811 p_owner->p_sout_input =
1812 sout_InputNew( p_owner->p_sout,
1815 if( p_owner->p_sout_input == NULL )
1817 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1818 (char *)&p_owner->sout.i_codec );
1819 p_dec->b_error = true;
1821 while( p_sout_block )
1823 block_t *p_next = p_sout_block->p_next;
1824 block_Release( p_sout_block );
1825 p_sout_block = p_next;
1831 while( p_sout_block )
1833 block_t *p_next = p_sout_block->p_next;
1835 p_sout_block->p_next = NULL;
1837 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1839 p_sout_block = p_next;
1845 /* This function process a video block
1847 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1849 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1851 if( p_owner->p_packetizer )
1853 block_t *p_packetized_block;
1854 decoder_t *p_packetizer = p_owner->p_packetizer;
1856 while( (p_packetized_block =
1857 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1859 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1861 es_format_Clean( &p_dec->fmt_in );
1862 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1864 if( p_packetizer->pf_get_cc )
1865 DecoderGetCc( p_dec, p_packetizer );
1867 while( p_packetized_block )
1869 block_t *p_next = p_packetized_block->p_next;
1870 p_packetized_block->p_next = NULL;
1872 DecoderDecodeVideo( p_dec, p_packetized_block );
1874 p_packetized_block = p_next;
1877 /* The packetizer does not output a block that tell the decoder to flush
1881 block_t *p_null = DecoderBlockFlushNew();
1883 DecoderDecodeVideo( p_dec, p_null );
1888 DecoderDecodeVideo( p_dec, p_block );
1891 if( b_flush && p_owner->p_vout )
1892 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1895 /* This function process a audio block
1897 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1899 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1901 if( p_owner->p_packetizer )
1903 block_t *p_packetized_block;
1904 decoder_t *p_packetizer = p_owner->p_packetizer;
1906 while( (p_packetized_block =
1907 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1909 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1911 es_format_Clean( &p_dec->fmt_in );
1912 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1915 while( p_packetized_block )
1917 block_t *p_next = p_packetized_block->p_next;
1918 p_packetized_block->p_next = NULL;
1920 DecoderDecodeAudio( p_dec, p_packetized_block );
1922 p_packetized_block = p_next;
1925 /* The packetizer does not output a block that tell the decoder to flush
1929 block_t *p_null = DecoderBlockFlushNew();
1931 DecoderDecodeAudio( p_dec, p_null );
1936 DecoderDecodeAudio( p_dec, p_block );
1939 if( b_flush && p_owner->p_aout )
1940 aout_DecFlush( p_owner->p_aout );
1943 /* This function process a subtitle block
1945 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1947 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1948 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1950 input_thread_t *p_input = p_owner->p_input;
1951 vout_thread_t *p_vout;
1952 subpicture_t *p_spu;
1954 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1956 if( p_input != NULL )
1958 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1959 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1,
1961 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1964 p_vout = input_resource_HoldVout( p_owner->p_resource );
1965 if( p_vout && p_owner->p_spu_vout == p_vout )
1967 /* Preroll does not work very well with subtitle */
1968 if( p_spu->i_start > VLC_TS_INVALID &&
1969 p_spu->i_start < p_owner->i_preroll_end &&
1970 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1972 subpicture_Delete( p_spu );
1976 DecoderPlaySpu( p_dec, p_spu, b_telx );
1981 subpicture_Delete( p_spu );
1984 vlc_object_release( p_vout );
1987 if( b_flush && p_owner->p_spu_vout )
1989 p_vout = input_resource_HoldVout( p_owner->p_resource );
1991 if( p_vout && p_owner->p_spu_vout == p_vout )
1992 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1995 vlc_object_release( p_vout );
2000 static void DecoderProcessOnFlush( decoder_t *p_dec )
2002 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2004 vlc_mutex_lock( &p_owner->lock );
2005 DecoderFlushBuffering( p_dec );
2007 if( p_owner->b_flushing )
2009 p_owner->b_flushing = false;
2010 vlc_cond_signal( &p_owner->wait_acknowledge );
2012 vlc_mutex_unlock( &p_owner->lock );
2018 * \param p_dec the decoder object
2019 * \param p_block the block to decode
2020 * \return VLC_SUCCESS or an error code
2022 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
2024 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
2025 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2027 if( p_block && p_block->i_buffer <= 0 )
2029 assert( !b_flush_request );
2030 block_Release( p_block );
2035 if( p_owner->b_packetizer )
2038 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2040 DecoderProcessSout( p_dec, p_block );
2045 bool b_flush = false;
2049 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
2050 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
2052 b_flush = !b_flushing && b_flush_request;
2054 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2057 if( p_dec->fmt_out.i_cat == AUDIO_ES )
2059 DecoderProcessAudio( p_dec, p_block, b_flush );
2061 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2063 DecoderProcessVideo( p_dec, p_block, b_flush );
2065 else if( p_dec->fmt_out.i_cat == SPU_ES )
2067 DecoderProcessSpu( p_dec, p_block, b_flush );
2071 msg_Err( p_dec, "unknown ES format" );
2072 p_dec->b_error = true;
2077 if( b_flush_request )
2078 DecoderProcessOnFlush( p_dec );
2081 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2083 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2087 block_Release( p_block );
2089 if( b_flush_request )
2090 DecoderProcessOnFlush( p_dec );
2095 * Destroys a decoder object
2097 * \param p_dec the decoder object
2100 static void DeleteDecoder( decoder_t * p_dec )
2102 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2104 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2105 (char*)&p_dec->fmt_in.i_codec,
2106 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2108 /* Free all packets still in the decoder fifo. */
2109 block_FifoEmpty( p_owner->p_fifo );
2110 block_FifoRelease( p_owner->p_fifo );
2113 vlc_mutex_lock( &p_owner->lock );
2114 DecoderFlushBuffering( p_dec );
2115 vlc_mutex_unlock( &p_owner->lock );
2118 if( p_owner->p_aout )
2120 /* TODO: REVISIT gap-less audio */
2121 aout_DecFlush( p_owner->p_aout );
2122 aout_DecDelete( p_owner->p_aout );
2123 input_resource_RequestAout( p_owner->p_resource, p_owner->p_aout );
2124 if( p_owner->p_input != NULL )
2125 input_SendEventAout( p_owner->p_input );
2127 if( p_owner->p_vout )
2129 /* Hack to make sure all the the pictures are freed by the decoder
2130 * and that the vout is not paused anymore */
2131 vout_Reset( p_owner->p_vout );
2134 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
2136 if( p_owner->p_input != NULL )
2137 input_SendEventVout( p_owner->p_input );
2141 if( p_owner->p_sout_input )
2143 sout_InputDelete( p_owner->p_sout_input );
2144 es_format_Clean( &p_owner->sout );
2148 if( p_dec->fmt_out.i_cat == SPU_ES )
2150 vout_thread_t *p_vout;
2152 p_vout = input_resource_HoldVout( p_owner->p_resource );
2155 if( p_owner->p_spu_vout == p_vout )
2156 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2157 vlc_object_release( p_vout );
2161 es_format_Clean( &p_dec->fmt_in );
2162 es_format_Clean( &p_dec->fmt_out );
2163 if( p_dec->p_description )
2164 vlc_meta_Delete( p_dec->p_description );
2165 es_format_Clean( &p_owner->fmt_description );
2166 if( p_owner->p_description )
2167 vlc_meta_Delete( p_owner->p_description );
2169 if( p_owner->p_packetizer )
2171 module_unneed( p_owner->p_packetizer,
2172 p_owner->p_packetizer->p_module );
2173 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2174 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2175 if( p_owner->p_packetizer->p_description )
2176 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2177 vlc_object_release( p_owner->p_packetizer );
2180 vlc_cond_destroy( &p_owner->wait_acknowledge );
2181 vlc_cond_destroy( &p_owner->wait_request );
2182 vlc_mutex_destroy( &p_owner->lock );
2184 vlc_object_release( p_dec );
2189 /*****************************************************************************
2190 * Buffers allocation callbacks for the decoders
2191 *****************************************************************************/
2192 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2194 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2196 vlc_assert_locked( &p_owner->lock );
2198 p_owner->b_fmt_description = true;
2200 /* Copy es_format */
2201 es_format_Clean( &p_owner->fmt_description );
2202 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2204 /* Move p_description */
2205 if( p_owner->p_description && p_dec->p_description )
2206 vlc_meta_Delete( p_owner->p_description );
2207 p_owner->p_description = p_dec->p_description;
2208 p_dec->p_description = NULL;
2210 static vout_thread_t *aout_request_vout( void *p_private,
2211 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2213 decoder_t *p_dec = p_private;
2214 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2215 input_thread_t *p_input = p_owner->p_input;
2217 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
2219 if( p_input != NULL )
2220 input_SendEventVout( p_input );
2225 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2227 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2228 aout_buffer_t *p_buffer;
2230 if( p_owner->p_aout &&
2231 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2232 p_dec->fmt_out.audio.i_original_channels !=
2233 p_owner->audio.i_original_channels ||
2234 p_dec->fmt_out.audio.i_bytes_per_frame !=
2235 p_owner->audio.i_bytes_per_frame ) )
2237 audio_output_t *p_aout = p_owner->p_aout;
2239 /* Parameters changed, restart the aout */
2240 vlc_mutex_lock( &p_owner->lock );
2242 DecoderFlushBuffering( p_dec );
2244 aout_DecDelete( p_owner->p_aout );
2245 p_owner->p_aout = NULL;
2247 vlc_mutex_unlock( &p_owner->lock );
2248 input_resource_RequestAout( p_owner->p_resource, p_aout );
2251 if( p_owner->p_aout == NULL )
2253 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2254 audio_sample_format_t format;
2255 audio_output_t *p_aout;
2256 aout_request_vout_t request_vout;
2258 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2259 p_owner->audio = p_dec->fmt_out.audio;
2261 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2262 if( i_force_dolby &&
2263 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2264 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2266 if( i_force_dolby == 1 )
2268 format.i_original_channels = format.i_original_channels |
2269 AOUT_CHAN_DOLBYSTEREO;
2271 else /* i_force_dolby == 2 */
2273 format.i_original_channels = format.i_original_channels &
2274 ~AOUT_CHAN_DOLBYSTEREO;
2278 request_vout.pf_request_vout = aout_request_vout;
2279 request_vout.p_private = p_dec;
2281 assert( p_owner->p_aout == NULL );
2282 p_aout = input_resource_RequestAout( p_owner->p_resource, NULL );
2285 aout_FormatPrepare( &format );
2286 if( aout_DecNew( p_aout, &format,
2287 &p_dec->fmt_out.audio_replay_gain,
2290 input_resource_RequestAout( p_owner->p_resource, p_aout );
2295 vlc_mutex_lock( &p_owner->lock );
2297 p_owner->p_aout = p_aout;
2298 DecoderUpdateFormatLocked( p_dec );
2300 vlc_mutex_unlock( &p_owner->lock );
2302 if( p_owner->p_input != NULL )
2303 input_SendEventAout( p_owner->p_input );
2305 if( p_aout == NULL )
2307 msg_Err( p_dec, "failed to create audio output" );
2308 p_dec->b_error = true;
2311 p_dec->fmt_out.audio.i_bytes_per_frame =
2312 p_owner->audio.i_bytes_per_frame;
2315 p_buffer = aout_DecNewBuffer( p_owner->p_aout, i_samples );
2320 static picture_t *vout_new_buffer( decoder_t *p_dec )
2322 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2324 if( p_owner->p_vout == NULL ||
2325 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2326 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2327 p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2328 p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2329 p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset ||
2330 p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset ||
2331 p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2332 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2333 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2335 vout_thread_t *p_vout;
2337 if( !p_dec->fmt_out.video.i_width ||
2338 !p_dec->fmt_out.video.i_height )
2340 /* Can't create a new vout without display size */
2344 video_format_t fmt = p_dec->fmt_out.video;
2345 fmt.i_chroma = p_dec->fmt_out.i_codec;
2346 p_owner->video = fmt;
2348 if( !fmt.i_visible_width || !fmt.i_visible_height )
2350 if( p_dec->fmt_in.video.i_visible_width &&
2351 p_dec->fmt_in.video.i_visible_height )
2353 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
2354 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2355 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
2356 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
2360 fmt.i_visible_width = fmt.i_width;
2361 fmt.i_visible_height = fmt.i_height;
2367 if( fmt.i_visible_height == 1088 &&
2368 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2370 fmt.i_visible_height = 1080;
2371 if( !(fmt.i_sar_num % 136))
2373 fmt.i_sar_num *= 135;
2374 fmt.i_sar_den *= 136;
2376 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2379 if( !fmt.i_sar_num || !fmt.i_sar_den )
2385 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2386 fmt.i_sar_num, fmt.i_sar_den, 50000 );
2388 vlc_mutex_lock( &p_owner->lock );
2390 DecoderFlushBuffering( p_dec );
2392 p_vout = p_owner->p_vout;
2393 p_owner->p_vout = NULL;
2394 vlc_mutex_unlock( &p_owner->lock );
2397 switch( p_dec->fmt_in.i_codec )
2399 case VLC_CODEC_H264:
2400 case VLC_CODEC_DIRAC: /* FIXME valid ? */
2405 case VLC_CODEC_VP6F:
2413 p_vout = input_resource_RequestVout( p_owner->p_resource,
2415 dpb_size + 1 + DECODER_MAX_BUFFERING_COUNT,
2417 vlc_mutex_lock( &p_owner->lock );
2418 p_owner->p_vout = p_vout;
2420 DecoderUpdateFormatLocked( p_dec );
2422 vlc_mutex_unlock( &p_owner->lock );
2424 if( p_owner->p_input != NULL )
2425 input_SendEventVout( p_owner->p_input );
2426 if( p_vout == NULL )
2428 msg_Err( p_dec, "failed to create video output" );
2429 p_dec->b_error = true;
2434 /* Get a new picture
2438 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2441 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2445 if( DecoderIsFlushing( p_dec ) )
2449 DecoderSignalBuffering( p_dec, true );
2451 /* Check the decoder doesn't leak pictures */
2452 vout_FixLeaks( p_owner->p_vout );
2454 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2455 msleep( VOUT_OUTMEM_SLEEP );
2459 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2461 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2464 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2466 vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2469 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2471 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2474 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2475 const subpicture_updater_t *p_updater )
2477 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2478 vout_thread_t *p_vout = NULL;
2479 subpicture_t *p_subpic;
2480 int i_attempts = 30;
2482 while( i_attempts-- )
2484 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2487 p_vout = input_resource_HoldVout( p_owner->p_resource );
2491 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2496 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2500 if( p_owner->p_spu_vout != p_vout )
2502 vlc_mutex_lock( &p_owner->lock );
2504 DecoderFlushBuffering( p_dec );
2506 vlc_mutex_unlock( &p_owner->lock );
2508 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2509 p_owner->i_spu_order = 0;
2510 p_owner->p_spu_vout = p_vout;
2513 p_subpic = subpicture_New( p_updater );
2516 p_subpic->i_channel = p_owner->i_spu_channel;
2517 p_subpic->i_order = p_owner->i_spu_order++;
2518 p_subpic->b_subtitle = true;
2521 vlc_object_release( p_vout );
2526 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2528 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2529 vout_thread_t *p_vout = NULL;
2531 p_vout = input_resource_HoldVout( p_owner->p_resource );
2532 if( !p_vout || p_owner->p_spu_vout != p_vout )
2535 vlc_object_release( p_vout );
2536 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2540 subpicture_Delete( p_subpic );
2542 vlc_object_release( p_vout );