1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * 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 );
1022 if( p_owner->b_flushing )
1024 if( p_owner->b_paused )
1026 if( p_owner->b_buffering && !p_owner->buffer.b_full )
1028 if( p_owner->pause.i_ignore > 0 )
1030 p_owner->pause.i_ignore--;
1036 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
1039 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1043 *pb_reject = p_owner->b_flushing;
1046 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1048 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1050 vlc_assert_locked( &p_owner->lock );
1052 /* XXX only audio and video output have to be paused.
1053 * - for sout it is useless
1054 * - for subs, it is done by the vout
1056 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1058 if( p_owner->p_aout )
1059 aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
1061 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1063 if( p_owner->p_vout )
1064 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1067 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1069 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1070 *pi_preroll = INT64_MAX;
1071 else if( p->i_dts > VLC_TS_INVALID )
1072 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1073 else if( p->i_pts > VLC_TS_INVALID )
1074 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1077 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1078 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
1080 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1081 input_clock_t *p_clock = p_owner->p_clock;
1083 vlc_assert_locked( &p_owner->lock );
1085 const mtime_t i_es_delay = p_owner->i_ts_delay;
1090 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1093 if( *pi_ts0 > VLC_TS_INVALID )
1095 *pi_ts0 += i_es_delay;
1096 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1097 *pi_ts1 += i_es_delay;
1098 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1099 *pi_ts0 = VLC_TS_INVALID;
1103 i_rate = input_clock_GetRate( p_clock );
1106 /* Do not create ephemere data because of rounding errors */
1107 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1111 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
1112 / INPUT_RATE_DEFAULT;
1118 static bool DecoderIsExitRequested( decoder_t *p_dec )
1120 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1122 vlc_mutex_lock( &p_owner->lock );
1123 bool b_exit = p_owner->b_exit;
1124 vlc_mutex_unlock( &p_owner->lock );
1130 * If *pb_reject, it does nothing, otherwise it waits for the given
1131 * deadline or a flush request (in which case it set *pi_reject to true.
1133 static void DecoderWaitDate( decoder_t *p_dec,
1134 bool *pb_reject, mtime_t i_deadline )
1136 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1138 vlc_assert_locked( &p_owner->lock );
1140 if( *pb_reject || i_deadline < 0 )
1145 if( p_owner->b_flushing || p_owner->b_exit )
1151 while( vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1152 i_deadline ) == 0 );
1155 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1156 int *pi_played_sum, int *pi_lost_sum )
1158 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1159 audio_output_t *p_aout = p_owner->p_aout;
1162 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1164 msg_Warn( p_dec, "non-dated audio buffer received" );
1166 block_Release( p_audio );
1171 vlc_mutex_lock( &p_owner->lock );
1173 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1175 p_audio->p_next = NULL;
1177 *p_owner->buffer.pp_audio_next = p_audio;
1178 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1180 p_owner->buffer.i_count++;
1181 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1182 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1184 p_owner->buffer.b_full = true;
1185 vlc_cond_signal( &p_owner->wait_acknowledge );
1191 bool b_has_more = false, b_paused, b_reject;
1193 DecoderWaitUnblock( p_dec, &b_reject );
1194 if( p_owner->b_buffering )
1197 b_paused = p_owner->b_paused;
1200 if( p_owner->buffer.p_audio )
1202 p_audio = p_owner->buffer.p_audio;
1204 p_owner->buffer.p_audio = p_audio->p_next;
1205 p_owner->buffer.i_count--;
1207 b_has_more = p_owner->buffer.p_audio != NULL;
1209 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1213 int i_rate = INPUT_RATE_DEFAULT;
1215 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1216 &i_rate, AOUT_MAX_ADVANCE_TIME );
1218 if( p_audio->i_pts <= VLC_TS_INVALID
1219 || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1220 || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1223 DecoderWaitDate( p_dec, &b_reject,
1224 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1226 if( unlikely(p_owner->b_paused != b_paused) )
1227 continue; /* race with input thread? retry... */
1228 if( p_aout == NULL )
1233 assert( !p_owner->b_paused );
1234 if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1235 *pi_played_sum += 1;
1236 *pi_lost_sum += aout_DecGetResetLost( p_aout );
1240 msg_Dbg( p_dec, "discarded audio buffer" );
1242 block_Release( p_audio );
1247 if( !p_owner->buffer.p_audio )
1250 vlc_mutex_unlock( &p_owner->lock );
1253 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1255 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1256 aout_buffer_t *p_aout_buf;
1261 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1263 audio_output_t *p_aout = p_owner->p_aout;
1265 if( DecoderIsExitRequested( p_dec ) )
1267 /* It prevent freezing VLC in case of broken decoder */
1268 aout_DecDeleteBuffer( p_aout, p_aout_buf );
1270 block_Release( p_block );
1275 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1276 p_aout_buf->i_pts < p_owner->i_preroll_end )
1278 aout_DecDeleteBuffer( p_aout, p_aout_buf );
1282 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1284 msg_Dbg( p_dec, "End of audio preroll" );
1285 if( p_owner->p_aout )
1286 aout_DecFlush( p_owner->p_aout );
1288 p_owner->i_preroll_end = VLC_TS_INVALID;
1291 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1294 /* Update ugly stat */
1295 input_thread_t *p_input = p_owner->p_input;
1297 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1299 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1300 stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1301 stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1302 stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1303 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1306 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1308 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1311 bool b_processed = false;
1315 assert( p_dec_cc->pf_get_cc != NULL );
1317 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1318 if( !p_owner->cc.b_supported )
1321 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1325 vlc_mutex_lock( &p_owner->lock );
1326 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1328 p_owner->cc.pb_present[i] |= pb_present[i];
1329 if( p_owner->cc.pp_decoder[i] )
1333 for( i = 0; i < 4; i++ )
1335 if( !p_owner->cc.pp_decoder[i] )
1338 if( i_cc_decoder > 1 )
1339 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1341 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1345 vlc_mutex_unlock( &p_owner->lock );
1348 block_Release( p_cc );
1351 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1352 int *pi_played_sum, int *pi_lost_sum )
1354 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1355 vout_thread_t *p_vout = p_owner->p_vout;
1356 bool b_first_buffered;
1358 if( p_picture->date <= VLC_TS_INVALID )
1360 msg_Warn( p_dec, "non-dated video buffer received" );
1362 vout_ReleasePicture( p_vout, p_picture );
1367 vlc_mutex_lock( &p_owner->lock );
1369 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1371 p_picture->p_next = NULL;
1373 *p_owner->buffer.pp_picture_next = p_picture;
1374 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1376 p_owner->buffer.i_count++;
1377 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1378 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1380 p_owner->buffer.b_full = true;
1381 vlc_cond_signal( &p_owner->wait_acknowledge );
1384 b_first_buffered = p_owner->buffer.p_picture != NULL;
1386 for( ;; b_first_buffered = false )
1388 bool b_has_more = false;
1392 DecoderWaitUnblock( p_dec, &b_reject );
1394 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1396 vlc_mutex_unlock( &p_owner->lock );
1399 bool b_buffering_first = p_owner->b_buffering;
1402 if( p_owner->buffer.p_picture )
1404 p_picture = p_owner->buffer.p_picture;
1406 p_owner->buffer.p_picture = p_picture->p_next;
1407 p_owner->buffer.i_count--;
1409 b_has_more = p_owner->buffer.p_picture != NULL;
1411 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1415 if( b_buffering_first )
1417 assert( p_owner->buffer.b_first );
1418 assert( !p_owner->buffer.i_count );
1419 msg_Dbg( p_dec, "Received first picture" );
1420 p_owner->buffer.b_first = false;
1421 p_picture->b_force = true;
1424 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1425 int i_rate = INPUT_RATE_DEFAULT;
1426 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1427 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1429 vlc_mutex_unlock( &p_owner->lock );
1432 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1437 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1439 /* Be sure to not display old picture after our own */
1440 vout_Flush( p_vout, p_picture->date );
1441 p_owner->i_last_rate = i_rate;
1443 vout_PutPicture( p_vout, p_picture );
1448 msg_Warn( p_dec, "early picture skipped" );
1450 msg_Warn( p_dec, "non-dated video buffer received" );
1453 vout_ReleasePicture( p_vout, p_picture );
1457 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1459 *pi_played_sum += i_tmp_display;
1460 *pi_lost_sum += i_tmp_lost;
1462 if( !b_has_more || b_buffering_first )
1465 vlc_mutex_lock( &p_owner->lock );
1466 if( !p_owner->buffer.p_picture )
1468 vlc_mutex_unlock( &p_owner->lock );
1474 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1476 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1480 int i_displayed = 0;
1482 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1484 vout_thread_t *p_vout = p_owner->p_vout;
1485 if( DecoderIsExitRequested( p_dec ) )
1487 /* It prevent freezing VLC in case of broken decoder */
1488 vout_ReleasePicture( p_vout, p_pic );
1490 block_Release( p_block );
1496 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1498 vout_ReleasePicture( p_vout, p_pic );
1502 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1504 msg_Dbg( p_dec, "End of video preroll" );
1506 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1508 p_owner->i_preroll_end = VLC_TS_INVALID;
1511 if( p_dec->pf_get_cc &&
1512 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1513 DecoderGetCc( p_dec, p_dec );
1515 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1518 /* Update ugly stat */
1519 input_thread_t *p_input = p_owner->p_input;
1521 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1523 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1524 stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
1525 stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
1526 stats_Update( p_input->p->counters.p_displayed_pictures,
1528 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1532 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1534 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1535 vout_thread_t *p_vout = p_owner->p_spu_vout;
1538 if( p_subpic->i_start <= VLC_TS_INVALID )
1540 msg_Warn( p_dec, "non-dated spu buffer received" );
1541 subpicture_Delete( p_subpic );
1546 vlc_mutex_lock( &p_owner->lock );
1548 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1550 p_subpic->p_next = NULL;
1552 *p_owner->buffer.pp_subpic_next = p_subpic;
1553 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1555 p_owner->buffer.i_count++;
1556 /* XXX it is important to be full after the first one */
1557 if( p_owner->buffer.i_count > 0 )
1559 p_owner->buffer.b_full = true;
1560 vlc_cond_signal( &p_owner->wait_acknowledge );
1566 bool b_has_more = false;
1568 DecoderWaitUnblock( p_dec, &b_reject );
1570 if( p_owner->b_buffering )
1572 vlc_mutex_unlock( &p_owner->lock );
1577 if( p_owner->buffer.p_subpic )
1579 p_subpic = p_owner->buffer.p_subpic;
1581 p_owner->buffer.p_subpic = p_subpic->p_next;
1582 p_owner->buffer.i_count--;
1584 b_has_more = p_owner->buffer.p_subpic != NULL;
1586 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1590 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1593 if( p_subpic->i_start <= VLC_TS_INVALID )
1596 DecoderWaitDate( p_dec, &b_reject,
1597 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1598 vlc_mutex_unlock( &p_owner->lock );
1601 vout_PutSubpicture( p_vout, p_subpic );
1603 subpicture_Delete( p_subpic );
1607 vlc_mutex_lock( &p_owner->lock );
1608 if( !p_owner->buffer.p_subpic )
1610 vlc_mutex_unlock( &p_owner->lock );
1617 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
1619 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1621 assert( p_owner->p_clock );
1622 assert( !p_sout_block->p_next );
1624 vlc_mutex_lock( &p_owner->lock );
1626 if( p_owner->b_buffering || p_owner->buffer.p_block )
1628 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1630 p_owner->buffer.i_count++;
1631 /* XXX it is important to be full after the first one */
1632 if( p_owner->buffer.i_count > 0 )
1634 p_owner->buffer.b_full = true;
1635 vlc_cond_signal( &p_owner->wait_acknowledge );
1641 bool b_has_more = false;
1643 DecoderWaitUnblock( p_dec, &b_reject );
1645 if( p_owner->b_buffering )
1647 vlc_mutex_unlock( &p_owner->lock );
1652 if( p_owner->buffer.p_block )
1654 p_sout_block = p_owner->buffer.p_block;
1656 p_owner->buffer.p_block = p_sout_block->p_next;
1657 p_owner->buffer.i_count--;
1659 b_has_more = p_owner->buffer.p_block != NULL;
1661 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1663 p_sout_block->p_next = NULL;
1665 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1666 &p_sout_block->i_length, NULL, INT64_MAX );
1668 vlc_mutex_unlock( &p_owner->lock );
1671 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1673 block_Release( p_sout_block );
1677 vlc_mutex_lock( &p_owner->lock );
1678 if( !p_owner->buffer.p_block )
1680 vlc_mutex_unlock( &p_owner->lock );
1688 static void DecoderFlushBuffering( decoder_t *p_dec )
1690 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1692 vlc_assert_locked( &p_owner->lock );
1694 while( p_owner->buffer.p_picture )
1696 picture_t *p_picture = p_owner->buffer.p_picture;
1698 p_owner->buffer.p_picture = p_picture->p_next;
1699 p_owner->buffer.i_count--;
1701 if( p_owner->p_vout )
1703 vout_ReleasePicture( p_owner->p_vout, p_picture );
1706 if( !p_owner->buffer.p_picture )
1707 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1709 while( p_owner->buffer.p_audio )
1711 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1713 p_owner->buffer.p_audio = p_audio->p_next;
1714 p_owner->buffer.i_count--;
1716 block_Release( p_audio );
1718 if( !p_owner->buffer.p_audio )
1719 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1721 while( p_owner->buffer.p_subpic )
1723 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1725 p_owner->buffer.p_subpic = p_subpic->p_next;
1726 p_owner->buffer.i_count--;
1728 subpicture_Delete( p_subpic );
1730 if( !p_owner->buffer.p_subpic )
1731 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1733 if( p_owner->buffer.p_block )
1735 block_ChainRelease( p_owner->buffer.p_block );
1737 p_owner->buffer.i_count = 0;
1738 p_owner->buffer.p_block = NULL;
1739 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1744 /* This function process a block for sout
1746 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1748 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1749 block_t *p_sout_block;
1751 while( ( p_sout_block =
1752 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1754 if( !p_owner->p_sout_input )
1756 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1758 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1759 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1760 if( p_dec->fmt_in.psz_language )
1762 free( p_owner->sout.psz_language );
1763 p_owner->sout.psz_language =
1764 strdup( p_dec->fmt_in.psz_language );
1767 p_owner->p_sout_input =
1768 sout_InputNew( p_owner->p_sout,
1771 if( p_owner->p_sout_input == NULL )
1773 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1774 (char *)&p_owner->sout.i_codec );
1775 p_dec->b_error = true;
1777 while( p_sout_block )
1779 block_t *p_next = p_sout_block->p_next;
1780 block_Release( p_sout_block );
1781 p_sout_block = p_next;
1787 while( p_sout_block )
1789 block_t *p_next = p_sout_block->p_next;
1791 p_sout_block->p_next = NULL;
1793 DecoderPlaySout( p_dec, p_sout_block );
1795 p_sout_block = p_next;
1801 /* This function process a video block
1803 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1805 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1807 if( p_owner->p_packetizer )
1809 block_t *p_packetized_block;
1810 decoder_t *p_packetizer = p_owner->p_packetizer;
1812 while( (p_packetized_block =
1813 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1815 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1817 es_format_Clean( &p_dec->fmt_in );
1818 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1820 if( p_packetizer->pf_get_cc )
1821 DecoderGetCc( p_dec, p_packetizer );
1823 while( p_packetized_block )
1825 block_t *p_next = p_packetized_block->p_next;
1826 p_packetized_block->p_next = NULL;
1828 DecoderDecodeVideo( p_dec, p_packetized_block );
1830 p_packetized_block = p_next;
1833 /* The packetizer does not output a block that tell the decoder to flush
1837 block_t *p_null = DecoderBlockFlushNew();
1839 DecoderDecodeVideo( p_dec, p_null );
1844 DecoderDecodeVideo( p_dec, p_block );
1847 if( b_flush && p_owner->p_vout )
1848 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1851 /* This function process a audio block
1853 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1855 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1857 if( p_owner->p_packetizer )
1859 block_t *p_packetized_block;
1860 decoder_t *p_packetizer = p_owner->p_packetizer;
1862 while( (p_packetized_block =
1863 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1865 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1867 es_format_Clean( &p_dec->fmt_in );
1868 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1871 while( p_packetized_block )
1873 block_t *p_next = p_packetized_block->p_next;
1874 p_packetized_block->p_next = NULL;
1876 DecoderDecodeAudio( p_dec, p_packetized_block );
1878 p_packetized_block = p_next;
1881 /* The packetizer does not output a block that tell the decoder to flush
1885 block_t *p_null = DecoderBlockFlushNew();
1887 DecoderDecodeAudio( p_dec, p_null );
1892 DecoderDecodeAudio( p_dec, p_block );
1895 if( b_flush && p_owner->p_aout )
1896 aout_DecFlush( p_owner->p_aout );
1899 /* This function process a subtitle block
1901 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1903 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1905 input_thread_t *p_input = p_owner->p_input;
1906 vout_thread_t *p_vout;
1907 subpicture_t *p_spu;
1909 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1911 if( p_input != NULL )
1913 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1914 stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1915 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1918 p_vout = input_resource_HoldVout( p_owner->p_resource );
1919 if( p_vout && p_owner->p_spu_vout == p_vout )
1921 /* Preroll does not work very well with subtitle */
1922 if( p_spu->i_start > VLC_TS_INVALID &&
1923 p_spu->i_start < p_owner->i_preroll_end &&
1924 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1926 subpicture_Delete( p_spu );
1930 DecoderPlaySpu( p_dec, p_spu );
1935 subpicture_Delete( p_spu );
1938 vlc_object_release( p_vout );
1941 if( b_flush && p_owner->p_spu_vout )
1943 p_vout = input_resource_HoldVout( p_owner->p_resource );
1945 if( p_vout && p_owner->p_spu_vout == p_vout )
1946 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1949 vlc_object_release( p_vout );
1954 static void DecoderProcessOnFlush( decoder_t *p_dec )
1956 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1958 vlc_mutex_lock( &p_owner->lock );
1959 DecoderFlushBuffering( p_dec );
1961 if( p_owner->b_flushing )
1963 p_owner->b_flushing = false;
1964 vlc_cond_signal( &p_owner->wait_acknowledge );
1966 vlc_mutex_unlock( &p_owner->lock );
1972 * \param p_dec the decoder object
1973 * \param p_block the block to decode
1974 * \return VLC_SUCCESS or an error code
1976 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1978 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1979 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1981 if( p_block && p_block->i_buffer <= 0 )
1983 assert( !b_flush_request );
1984 block_Release( p_block );
1989 if( p_owner->b_packetizer )
1992 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1994 DecoderProcessSout( p_dec, p_block );
1999 bool b_flush = false;
2003 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
2004 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
2006 b_flush = !b_flushing && b_flush_request;
2008 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2011 if( p_dec->fmt_out.i_cat == AUDIO_ES )
2013 DecoderProcessAudio( p_dec, p_block, b_flush );
2015 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2017 DecoderProcessVideo( p_dec, p_block, b_flush );
2019 else if( p_dec->fmt_out.i_cat == SPU_ES )
2021 DecoderProcessSpu( p_dec, p_block, b_flush );
2025 msg_Err( p_dec, "unknown ES format" );
2026 p_dec->b_error = true;
2031 if( b_flush_request )
2032 DecoderProcessOnFlush( p_dec );
2035 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2037 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2041 block_Release( p_block );
2043 if( b_flush_request )
2044 DecoderProcessOnFlush( p_dec );
2049 * Destroys a decoder object
2051 * \param p_dec the decoder object
2054 static void DeleteDecoder( decoder_t * p_dec )
2056 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2058 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2059 (char*)&p_dec->fmt_in.i_codec,
2060 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2062 /* Free all packets still in the decoder fifo. */
2063 block_FifoEmpty( p_owner->p_fifo );
2064 block_FifoRelease( p_owner->p_fifo );
2067 vlc_mutex_lock( &p_owner->lock );
2068 DecoderFlushBuffering( p_dec );
2069 vlc_mutex_unlock( &p_owner->lock );
2072 if( p_owner->p_aout )
2074 /* TODO: REVISIT gap-less audio */
2075 aout_DecFlush( p_owner->p_aout );
2076 aout_DecDelete( p_owner->p_aout );
2077 input_resource_RequestAout( p_owner->p_resource, p_owner->p_aout );
2078 if( p_owner->p_input != NULL )
2079 input_SendEventAout( p_owner->p_input );
2081 if( p_owner->p_vout )
2083 /* Hack to make sure all the the pictures are freed by the decoder
2084 * and that the vout is not paused anymore */
2085 vout_Reset( p_owner->p_vout );
2088 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
2090 if( p_owner->p_input != NULL )
2091 input_SendEventVout( p_owner->p_input );
2095 if( p_owner->p_sout_input )
2097 sout_InputDelete( p_owner->p_sout_input );
2098 es_format_Clean( &p_owner->sout );
2102 if( p_dec->fmt_out.i_cat == SPU_ES )
2104 vout_thread_t *p_vout;
2106 p_vout = input_resource_HoldVout( p_owner->p_resource );
2109 if( p_owner->p_spu_vout == p_vout )
2110 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2111 vlc_object_release( p_vout );
2115 es_format_Clean( &p_dec->fmt_in );
2116 es_format_Clean( &p_dec->fmt_out );
2117 if( p_dec->p_description )
2118 vlc_meta_Delete( p_dec->p_description );
2119 es_format_Clean( &p_owner->fmt_description );
2120 if( p_owner->p_description )
2121 vlc_meta_Delete( p_owner->p_description );
2123 if( p_owner->p_packetizer )
2125 module_unneed( p_owner->p_packetizer,
2126 p_owner->p_packetizer->p_module );
2127 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2128 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2129 if( p_owner->p_packetizer->p_description )
2130 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2131 vlc_object_release( p_owner->p_packetizer );
2134 vlc_cond_destroy( &p_owner->wait_acknowledge );
2135 vlc_cond_destroy( &p_owner->wait_request );
2136 vlc_mutex_destroy( &p_owner->lock );
2138 vlc_object_release( p_dec );
2143 /*****************************************************************************
2144 * Buffers allocation callbacks for the decoders
2145 *****************************************************************************/
2146 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2148 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2150 vlc_assert_locked( &p_owner->lock );
2152 p_owner->b_fmt_description = true;
2154 /* Copy es_format */
2155 es_format_Clean( &p_owner->fmt_description );
2156 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2158 /* Move p_description */
2159 if( p_owner->p_description && p_dec->p_description )
2160 vlc_meta_Delete( p_owner->p_description );
2161 p_owner->p_description = p_dec->p_description;
2162 p_dec->p_description = NULL;
2164 static vout_thread_t *aout_request_vout( void *p_private,
2165 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2167 decoder_t *p_dec = p_private;
2168 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2169 input_thread_t *p_input = p_owner->p_input;
2171 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
2173 if( p_input != NULL )
2174 input_SendEventVout( p_input );
2179 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2181 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2182 aout_buffer_t *p_buffer;
2185 && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->audio) )
2187 audio_output_t *p_aout = p_owner->p_aout;
2189 /* Parameters changed, restart the aout */
2190 vlc_mutex_lock( &p_owner->lock );
2192 DecoderFlushBuffering( p_dec );
2194 aout_DecDelete( p_owner->p_aout );
2195 p_owner->p_aout = NULL;
2197 vlc_mutex_unlock( &p_owner->lock );
2198 input_resource_RequestAout( p_owner->p_resource, p_aout );
2201 if( p_owner->p_aout == NULL )
2203 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2204 audio_sample_format_t format;
2205 audio_output_t *p_aout;
2206 aout_request_vout_t request_vout;
2208 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2209 p_owner->audio = p_dec->fmt_out.audio;
2211 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2212 if( i_force_dolby &&
2213 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2214 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2216 if( i_force_dolby == 1 )
2218 format.i_original_channels = format.i_original_channels |
2219 AOUT_CHAN_DOLBYSTEREO;
2221 else /* i_force_dolby == 2 */
2223 format.i_original_channels = format.i_original_channels &
2224 ~AOUT_CHAN_DOLBYSTEREO;
2228 request_vout.pf_request_vout = aout_request_vout;
2229 request_vout.p_private = p_dec;
2231 assert( p_owner->p_aout == NULL );
2232 p_aout = input_resource_RequestAout( p_owner->p_resource, NULL );
2235 aout_FormatPrepare( &format );
2236 if( aout_DecNew( p_aout, &format,
2237 &p_dec->fmt_out.audio_replay_gain,
2240 input_resource_RequestAout( p_owner->p_resource, p_aout );
2245 vlc_mutex_lock( &p_owner->lock );
2247 p_owner->p_aout = p_aout;
2248 DecoderUpdateFormatLocked( p_dec );
2249 if( unlikely(p_owner->b_paused) ) /* fake pause if needed */
2250 aout_DecChangePause( p_aout, true, mdate() );
2252 vlc_mutex_unlock( &p_owner->lock );
2254 if( p_owner->p_input != NULL )
2255 input_SendEventAout( p_owner->p_input );
2257 if( p_aout == NULL )
2259 msg_Err( p_dec, "failed to create audio output" );
2260 p_dec->b_error = true;
2263 p_dec->fmt_out.audio.i_bytes_per_frame =
2264 p_owner->audio.i_bytes_per_frame;
2267 p_buffer = aout_DecNewBuffer( p_owner->p_aout, i_samples );
2272 static picture_t *vout_new_buffer( decoder_t *p_dec )
2274 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2276 if( p_owner->p_vout == NULL ||
2277 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2278 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2279 p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2280 p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2281 p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset ||
2282 p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset ||
2283 p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2284 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2285 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2287 vout_thread_t *p_vout;
2289 if( !p_dec->fmt_out.video.i_width ||
2290 !p_dec->fmt_out.video.i_height )
2292 /* Can't create a new vout without display size */
2296 video_format_t fmt = p_dec->fmt_out.video;
2297 fmt.i_chroma = p_dec->fmt_out.i_codec;
2298 p_owner->video = fmt;
2300 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
2302 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
2303 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
2305 while( fmt.i_width % dsc->p[i].w.den )
2307 while( fmt.i_height % dsc->p[i].h.den )
2312 if( !fmt.i_visible_width || !fmt.i_visible_height )
2314 if( p_dec->fmt_in.video.i_visible_width &&
2315 p_dec->fmt_in.video.i_visible_height )
2317 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
2318 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2319 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
2320 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
2324 fmt.i_visible_width = fmt.i_width;
2325 fmt.i_visible_height = fmt.i_height;
2331 if( fmt.i_visible_height == 1088 &&
2332 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2334 fmt.i_visible_height = 1080;
2335 if( !(fmt.i_sar_num % 136))
2337 fmt.i_sar_num *= 135;
2338 fmt.i_sar_den *= 136;
2340 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2343 if( !fmt.i_sar_num || !fmt.i_sar_den )
2349 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2350 fmt.i_sar_num, fmt.i_sar_den, 50000 );
2352 vlc_mutex_lock( &p_owner->lock );
2354 DecoderFlushBuffering( p_dec );
2356 p_vout = p_owner->p_vout;
2357 p_owner->p_vout = NULL;
2358 vlc_mutex_unlock( &p_owner->lock );
2361 switch( p_dec->fmt_in.i_codec )
2363 case VLC_CODEC_H264:
2364 case VLC_CODEC_DIRAC: /* FIXME valid ? */
2369 case VLC_CODEC_VP6F:
2377 p_vout = input_resource_RequestVout( p_owner->p_resource,
2380 p_dec->i_extra_picture_buffers +
2381 1 + DECODER_MAX_BUFFERING_COUNT,
2383 vlc_mutex_lock( &p_owner->lock );
2384 p_owner->p_vout = p_vout;
2386 DecoderUpdateFormatLocked( p_dec );
2388 vlc_mutex_unlock( &p_owner->lock );
2390 if( p_owner->p_input != NULL )
2391 input_SendEventVout( p_owner->p_input );
2392 if( p_vout == NULL )
2394 msg_Err( p_dec, "failed to create video output" );
2395 p_dec->b_error = true;
2400 /* Get a new picture
2404 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2407 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2411 if( DecoderIsFlushing( p_dec ) )
2415 DecoderSignalBuffering( p_dec, true );
2417 /* Check the decoder doesn't leak pictures */
2418 vout_FixLeaks( p_owner->p_vout );
2420 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2421 msleep( VOUT_OUTMEM_SLEEP );
2425 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2427 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2430 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2432 vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2435 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2437 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2440 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2441 const subpicture_updater_t *p_updater )
2443 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2444 vout_thread_t *p_vout = NULL;
2445 subpicture_t *p_subpic;
2446 int i_attempts = 30;
2448 while( i_attempts-- )
2450 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2453 p_vout = input_resource_HoldVout( p_owner->p_resource );
2457 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2462 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2466 if( p_owner->p_spu_vout != p_vout )
2468 vlc_mutex_lock( &p_owner->lock );
2470 DecoderFlushBuffering( p_dec );
2472 vlc_mutex_unlock( &p_owner->lock );
2474 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2475 p_owner->i_spu_order = 0;
2476 p_owner->p_spu_vout = p_vout;
2479 p_subpic = subpicture_New( p_updater );
2482 p_subpic->i_channel = p_owner->i_spu_channel;
2483 p_subpic->i_order = p_owner->i_spu_order++;
2484 p_subpic->b_subtitle = true;
2487 vlc_object_release( p_vout );
2492 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2494 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2495 vout_thread_t *p_vout = NULL;
2497 p_vout = input_resource_HoldVout( p_owner->p_resource );
2498 if( !p_vout || p_owner->p_spu_vout != p_vout )
2501 vlc_object_release( p_vout );
2502 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2506 subpicture_Delete( p_subpic );
2508 vlc_object_release( p_vout );