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_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
51 #include "../video_output/vout_control.h"
53 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
54 static void DeleteDecoder( decoder_t * );
56 static void *DecoderThread( vlc_object_t * );
57 static void DecoderProcess( decoder_t *, block_t * );
58 static void DecoderError( decoder_t *p_dec, block_t *p_block );
59 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
60 static void DecoderFlush( decoder_t * );
61 static void DecoderSignalBuffering( decoder_t *, bool );
62 static void DecoderFlushBuffering( decoder_t * );
64 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
66 /* Buffers allocation callbacks for the decoders */
67 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
68 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
70 static picture_t *vout_new_buffer( decoder_t * );
71 static void vout_del_buffer( decoder_t *, picture_t * );
72 static void vout_link_picture( decoder_t *, picture_t * );
73 static void vout_unlink_picture( decoder_t *, picture_t * );
75 static subpicture_t *spu_new_buffer( decoder_t * );
76 static void spu_del_buffer( decoder_t *, subpicture_t * );
78 static es_format_t null_es_format;
80 struct decoder_owner_sys_t
82 int64_t i_preroll_end;
84 input_thread_t *p_input;
85 input_clock_t *p_clock;
88 vout_thread_t *p_spu_vout;
92 sout_instance_t *p_sout;
93 sout_packetizer_input_t *p_sout_input;
95 /* Some decoders require already packetized data (ie. not truncated) */
96 decoder_t *p_packetizer;
98 /* Current format in use by the output */
100 audio_format_t audio;
104 bool b_fmt_description;
105 es_format_t fmt_description;
108 block_fifo_t *p_fifo;
110 /* Lock for communication with decoder thread */
114 /* -- These variables need locking on write(only) -- */
115 aout_instance_t *p_aout;
116 aout_input_t *p_aout_input;
118 vout_thread_t *p_vout;
120 /* -- Theses variables need locking on read *and* write -- */
138 picture_t *p_picture;
139 picture_t **pp_picture_next;
141 subpicture_t *p_subpic;
142 subpicture_t **pp_subpic_next;
144 aout_buffer_t *p_audio;
145 aout_buffer_t **pp_audio_next;
148 block_t **pp_block_next;
159 decoder_t *pp_decoder[4];
166 #define DECODER_MAX_BUFFERING_COUNT (4)
167 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
168 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
170 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
171 * a bogus PTS and won't be displayed */
172 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
175 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
178 /*****************************************************************************
180 *****************************************************************************/
181 picture_t *decoder_NewPicture( decoder_t *p_decoder )
183 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
185 msg_Warn( p_decoder, "can't get output picture" );
188 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
190 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
192 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
194 p_decoder->pf_picture_link( p_decoder, p_picture );
196 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
198 p_decoder->pf_picture_unlink( p_decoder, p_picture );
201 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
203 if( !p_decoder->pf_aout_buffer_new )
205 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
207 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
209 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
212 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
214 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
216 msg_Warn( p_decoder, "can't get output subpicture" );
219 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
221 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
224 /* decoder_GetInputAttachments:
226 int decoder_GetInputAttachments( decoder_t *p_dec,
227 input_attachment_t ***ppp_attachment,
230 if( !p_dec->pf_get_attachments )
233 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
235 /* decoder_GetDisplayDate:
237 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
239 if( !p_dec->pf_get_display_date )
242 return p_dec->pf_get_display_date( p_dec, i_ts );
244 /* decoder_GetDisplayRate:
246 int decoder_GetDisplayRate( decoder_t *p_dec )
248 if( !p_dec->pf_get_display_rate )
249 return INPUT_RATE_DEFAULT;
251 return p_dec->pf_get_display_rate( p_dec );
255 * Spawns a new decoder thread
257 * \param p_input the input thread
258 * \param p_es the es descriptor
259 * \return the spawned decoder object
261 decoder_t *input_DecoderNew( input_thread_t *p_input,
262 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
264 decoder_t *p_dec = NULL;
268 /* If we are in sout mode, search for packetizer module */
271 /* Create the decoder configuration structure */
272 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
275 msg_Err( p_input, "could not create packetizer" );
276 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
277 _("VLC could not open the packetizer module.") );
284 /* Create the decoder configuration structure */
285 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
288 msg_Err( p_input, "could not create decoder" );
289 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
290 _("VLC could not open the decoder module.") );
295 if( !p_dec->p_module )
297 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
299 DeleteDecoder( p_dec );
300 vlc_object_release( p_dec );
304 p_dec->p_owner->p_clock = p_clock;
306 if( fmt->i_cat == AUDIO_ES )
307 i_priority = VLC_THREAD_PRIORITY_AUDIO;
309 i_priority = VLC_THREAD_PRIORITY_VIDEO;
311 /* Spawn the decoder thread */
312 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
313 i_priority, false ) )
315 msg_Err( p_dec, "cannot spawn decoder thread" );
316 module_unneed( p_dec, p_dec->p_module );
317 DeleteDecoder( p_dec );
318 vlc_object_release( p_dec );
326 * Kills a decoder thread and waits until it's finished
328 * \param p_input the input thread
329 * \param p_es the es descriptor
332 void input_DecoderDelete( decoder_t *p_dec )
334 decoder_owner_sys_t *p_owner = p_dec->p_owner;
336 vlc_object_kill( p_dec );
338 /* Make sure we aren't paused/buffering/waiting anymore */
339 vlc_mutex_lock( &p_owner->lock );
340 p_owner->b_paused = false;
341 p_owner->b_buffering = false;
342 p_owner->b_flushing = true;
343 vlc_cond_signal( &p_owner->wait );
344 vlc_mutex_unlock( &p_owner->lock );
346 vlc_thread_join( p_dec );
347 module_unneed( p_dec, p_dec->p_module );
350 if( p_dec->p_owner->cc.b_supported )
353 for( i = 0; i < 4; i++ )
354 input_DecoderSetCcState( p_dec, false, i );
357 /* Delete decoder configuration */
358 DeleteDecoder( p_dec );
360 /* Delete the decoder */
361 vlc_object_release( p_dec );
365 * Put a block_t in the decoder's fifo.
366 * Thread-safe w.r.t. the decoder. May be a cancellation point.
368 * \param p_dec the decoder object
369 * \param p_block the data block
371 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
373 decoder_owner_sys_t *p_owner = p_dec->p_owner;
375 if( p_owner->p_input->p->b_out_pace_control )
377 /* The fifo is not consummed when buffering and so will
379 * There is no need to lock as b_buffering is never modify
380 * inside decoder thread. */
381 if( !p_owner->b_buffering )
382 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
384 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
386 /* FIXME: ideally we would check the time amount of data
387 * in the FIFO instead of its size. */
388 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
389 "consumed quickly enough), resetting fifo!" );
390 block_FifoEmpty( p_owner->p_fifo );
393 block_FifoPut( p_owner->p_fifo, p_block );
396 bool input_DecoderIsEmpty( decoder_t * p_dec )
398 assert( !p_dec->p_owner->b_buffering );
400 /* FIXME that's not really true */
401 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
404 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
406 decoder_owner_sys_t *p_owner = p_dec->p_owner;
409 vlc_mutex_lock( &p_owner->lock );
410 for( i = 0; i < 4; i++ )
411 pb_present[i] = p_owner->cc.pb_present[i];
412 vlc_mutex_unlock( &p_owner->lock );
414 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
416 decoder_owner_sys_t *p_owner = p_dec->p_owner;
418 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
420 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
425 static const vlc_fourcc_t fcc[4] = {
426 VLC_FOURCC('c', 'c', '1', ' '),
427 VLC_FOURCC('c', 'c', '2', ' '),
428 VLC_FOURCC('c', 'c', '3', ' '),
429 VLC_FOURCC('c', 'c', '4', ' '),
434 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
435 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
438 msg_Err( p_dec, "could not create decoder" );
439 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
440 _("VLC could not open the decoder module.") );
443 else if( !p_cc->p_module )
445 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
446 DeleteDecoder( p_cc );
447 vlc_object_release( p_cc );
450 p_cc->p_owner->p_clock = p_owner->p_clock;
452 vlc_mutex_lock( &p_owner->lock );
453 p_owner->cc.pp_decoder[i_channel] = p_cc;
454 vlc_mutex_unlock( &p_owner->lock );
460 vlc_mutex_lock( &p_owner->lock );
461 p_cc = p_owner->cc.pp_decoder[i_channel];
462 p_owner->cc.pp_decoder[i_channel] = NULL;
463 vlc_mutex_unlock( &p_owner->lock );
467 vlc_object_kill( p_cc );
468 module_unneed( p_cc, p_cc->p_module );
469 DeleteDecoder( p_cc );
470 vlc_object_release( p_cc );
475 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
477 decoder_owner_sys_t *p_owner = p_dec->p_owner;
480 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
483 vlc_mutex_lock( &p_owner->lock );
484 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
485 vlc_mutex_unlock( &p_owner->lock );
489 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
491 decoder_owner_sys_t *p_owner = p_dec->p_owner;
493 vlc_mutex_lock( &p_owner->lock );
495 assert( !p_owner->b_paused || !b_paused );
497 p_owner->b_paused = b_paused;
498 p_owner->pause.i_date = i_date;
499 p_owner->pause.i_ignore = 0;
500 vlc_cond_signal( &p_owner->wait );
502 DecoderOutputChangePause( p_dec, b_paused, i_date );
504 vlc_mutex_unlock( &p_owner->lock );
507 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
509 decoder_owner_sys_t *p_owner = p_dec->p_owner;
511 vlc_mutex_lock( &p_owner->lock );
512 p_owner->i_ts_delay = i_delay;
513 vlc_mutex_unlock( &p_owner->lock );
516 void input_DecoderStartBuffering( decoder_t *p_dec )
518 decoder_owner_sys_t *p_owner = p_dec->p_owner;
520 vlc_mutex_lock( &p_owner->lock );
522 DecoderFlush( p_dec );
524 p_owner->buffer.b_first = true;
525 p_owner->buffer.b_full = false;
526 p_owner->buffer.i_count = 0;
528 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
529 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
531 p_owner->buffer.p_picture = NULL;
532 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
534 p_owner->buffer.p_subpic = NULL;
535 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
537 p_owner->buffer.p_audio = NULL;
538 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
540 p_owner->buffer.p_block = NULL;
541 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
544 p_owner->b_buffering = true;
546 vlc_cond_signal( &p_owner->wait );
548 vlc_mutex_unlock( &p_owner->lock );
551 void input_DecoderStopBuffering( decoder_t *p_dec )
553 decoder_owner_sys_t *p_owner = p_dec->p_owner;
555 vlc_mutex_lock( &p_owner->lock );
557 p_owner->b_buffering = false;
559 vlc_cond_signal( &p_owner->wait );
561 vlc_mutex_unlock( &p_owner->lock );
564 void input_DecoderWaitBuffering( decoder_t *p_dec )
566 decoder_owner_sys_t *p_owner = p_dec->p_owner;
568 vlc_mutex_lock( &p_owner->lock );
570 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
572 block_FifoWake( p_owner->p_fifo );
573 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
576 vlc_mutex_unlock( &p_owner->lock );
579 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
581 decoder_owner_sys_t *p_owner = p_dec->p_owner;
585 vlc_mutex_lock( &p_owner->lock );
586 if( p_dec->fmt_in.i_cat == VIDEO_ES )
588 if( p_owner->b_paused && p_owner->p_vout )
590 vout_NextPicture( p_owner->p_vout, pi_duration );
591 p_owner->pause.i_ignore++;
592 vlc_cond_signal( &p_owner->wait );
597 /* TODO subtitle should not be flushed */
598 DecoderFlush( p_dec );
600 vlc_mutex_unlock( &p_owner->lock );
603 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt )
605 decoder_owner_sys_t *p_owner = p_dec->p_owner;
608 vlc_mutex_lock( &p_owner->lock );
609 b_changed = p_owner->b_fmt_description;
613 es_format_Copy( p_fmt, &p_owner->fmt_description );
614 p_owner->b_fmt_description = false;
616 vlc_mutex_unlock( &p_owner->lock );
620 /*****************************************************************************
622 *****************************************************************************/
623 static int DecoderGetInputAttachments( decoder_t *p_dec,
624 input_attachment_t ***ppp_attachment,
627 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
628 ppp_attachment, pi_attachment );
630 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
632 decoder_owner_sys_t *p_owner = p_dec->p_owner;
634 vlc_mutex_lock( &p_owner->lock );
635 if( p_owner->b_buffering || p_owner->b_paused )
637 vlc_mutex_unlock( &p_owner->lock );
639 if( !p_owner->p_clock || !i_ts )
642 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
644 static int DecoderGetDisplayRate( decoder_t *p_dec )
646 decoder_owner_sys_t *p_owner = p_dec->p_owner;
648 if( !p_owner->p_clock )
649 return INPUT_RATE_DEFAULT;
650 return input_clock_GetRate( p_owner->p_clock );
654 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
656 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
657 "VLC probably does not support this sound or video format.",
659 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
660 _("VLC does not support the audio or video format \"%4.4s\". "
661 "Unfortunately there is no way for you to fix this."), (char*)&codec );
666 * Create a decoder object
668 * \param p_input the input thread
669 * \param p_es the es descriptor
670 * \param i_object_type Object type as define in include/vlc_objects.h
671 * \return the decoder object
673 static decoder_t * CreateDecoder( input_thread_t *p_input,
674 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
677 decoder_owner_sys_t *p_owner;
680 p_dec = vlc_object_create( p_input, i_object_type );
684 p_dec->pf_decode_audio = NULL;
685 p_dec->pf_decode_video = NULL;
686 p_dec->pf_decode_sub = NULL;
687 p_dec->pf_get_cc = NULL;
688 p_dec->pf_packetize = NULL;
690 /* Initialize the decoder */
691 p_dec->p_module = NULL;
693 memset( &null_es_format, 0, sizeof(es_format_t) );
694 es_format_Copy( &p_dec->fmt_in, fmt );
695 es_format_Copy( &p_dec->fmt_out, &null_es_format );
697 p_dec->p_description = NULL;
699 /* Allocate our private structure for the decoder */
700 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
701 if( p_dec->p_owner == NULL )
703 vlc_object_release( p_dec );
706 p_dec->p_owner->i_preroll_end = -1;
707 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
708 p_dec->p_owner->p_input = p_input;
709 p_dec->p_owner->p_aout = NULL;
710 p_dec->p_owner->p_aout_input = NULL;
711 p_dec->p_owner->p_vout = NULL;
712 p_dec->p_owner->p_spu_vout = NULL;
713 p_dec->p_owner->i_spu_channel = 0;
714 p_dec->p_owner->i_spu_order = 0;
715 p_dec->p_owner->p_sout = p_sout;
716 p_dec->p_owner->p_sout_input = NULL;
717 p_dec->p_owner->p_packetizer = NULL;
720 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
722 free( p_dec->p_owner );
723 vlc_object_release( p_dec );
727 /* Set buffers allocation callbacks for the decoders */
728 p_dec->pf_aout_buffer_new = aout_new_buffer;
729 p_dec->pf_aout_buffer_del = aout_del_buffer;
730 p_dec->pf_vout_buffer_new = vout_new_buffer;
731 p_dec->pf_vout_buffer_del = vout_del_buffer;
732 p_dec->pf_picture_link = vout_link_picture;
733 p_dec->pf_picture_unlink = vout_unlink_picture;
734 p_dec->pf_spu_buffer_new = spu_new_buffer;
735 p_dec->pf_spu_buffer_del = spu_del_buffer;
737 p_dec->pf_get_attachments = DecoderGetInputAttachments;
738 p_dec->pf_get_display_date = DecoderGetDisplayDate;
739 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
741 vlc_object_attach( p_dec, p_input );
743 /* Find a suitable decoder/packetizer module */
744 if( i_object_type == VLC_OBJECT_DECODER )
745 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
747 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
749 /* Check if decoder requires already packetized data */
750 if( i_object_type == VLC_OBJECT_DECODER &&
751 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
753 p_dec->p_owner->p_packetizer =
754 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
755 if( p_dec->p_owner->p_packetizer )
757 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
760 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
763 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
765 p_dec->p_owner->p_packetizer->p_module =
766 module_need( p_dec->p_owner->p_packetizer,
767 "packetizer", "$packetizer", 0 );
769 if( !p_dec->p_owner->p_packetizer->p_module )
771 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
772 vlc_object_detach( p_dec->p_owner->p_packetizer );
773 vlc_object_release( p_dec->p_owner->p_packetizer );
778 /* Copy ourself the input replay gain */
779 if( fmt->i_cat == AUDIO_ES )
781 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
783 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
785 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
786 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
788 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
790 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
791 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
797 vlc_mutex_init( &p_owner->lock );
798 vlc_cond_init( &p_owner->wait );
800 p_owner->b_fmt_description = false;
801 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
803 p_owner->b_paused = false;
804 p_owner->pause.i_date = 0;
805 p_owner->pause.i_ignore = 0;
807 p_owner->b_buffering = false;
808 p_owner->buffer.b_first = true;
809 p_owner->buffer.b_full = false;
810 p_owner->buffer.i_count = 0;
811 p_owner->buffer.p_picture = NULL;
812 p_owner->buffer.p_subpic = NULL;
813 p_owner->buffer.p_audio = NULL;
814 p_owner->buffer.p_block = NULL;
816 p_owner->b_flushing = false;
819 p_owner->cc.b_supported = false;
820 if( i_object_type == VLC_OBJECT_DECODER )
822 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
823 p_owner->cc.b_supported = true;
824 if( p_dec->pf_get_cc )
825 p_owner->cc.b_supported = true;
828 for( i = 0; i < 4; i++ )
830 p_owner->cc.pb_present[i] = false;
831 p_owner->cc.pp_decoder[i] = NULL;
833 p_owner->i_ts_delay = 0;
838 * The decoding main loop
840 * \param p_dec the decoder
842 static void *DecoderThread( vlc_object_t *p_this )
844 decoder_t *p_dec = (decoder_t *)p_this;
845 decoder_owner_sys_t *p_owner = p_dec->p_owner;
847 /* The decoder's main loop */
850 block_t *p_block = block_FifoGet( p_owner->p_fifo );
851 /* Make sure there is no cancellation point other than this one^^.
852 * If you need one, be sure to push cleanup of p_block. */
853 DecoderSignalBuffering( p_dec, p_block == NULL );
857 int canc = vlc_savecancel();
860 DecoderError( p_dec, p_block );
862 DecoderProcess( p_dec, p_block );
864 vlc_restorecancel( canc );
868 DecoderSignalBuffering( p_dec, true );
872 static void DecoderFlush( decoder_t *p_dec )
874 decoder_owner_sys_t *p_owner = p_dec->p_owner;
877 vlc_assert_locked( &p_owner->lock );
880 block_FifoEmpty( p_owner->p_fifo );
882 /* Monitor for flush end */
883 p_owner->b_flushing = true;
884 vlc_cond_signal( &p_owner->wait );
886 /* Send a special block */
887 p_null = block_New( p_dec, 128 );
890 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
891 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
892 if( !p_dec->fmt_in.b_packetized )
893 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
894 memset( p_null->p_buffer, 0, p_null->i_buffer );
896 input_DecoderDecode( p_dec, p_null );
899 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
900 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
903 static void DecoderSignalFlushed( decoder_t *p_dec )
905 decoder_owner_sys_t *p_owner = p_dec->p_owner;
907 vlc_mutex_lock( &p_owner->lock );
909 if( p_owner->b_flushing )
911 p_owner->b_flushing = false;
912 vlc_cond_signal( &p_owner->wait );
915 vlc_mutex_unlock( &p_owner->lock );
918 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
920 decoder_owner_sys_t *p_owner = p_dec->p_owner;
922 vlc_mutex_lock( &p_owner->lock );
924 if( p_owner->b_buffering )
927 p_owner->buffer.b_full = true;
928 vlc_cond_signal( &p_owner->wait );
931 vlc_mutex_unlock( &p_owner->lock );
934 static bool DecoderIsFlushing( decoder_t *p_dec )
936 decoder_owner_sys_t *p_owner = p_dec->p_owner;
939 vlc_mutex_lock( &p_owner->lock );
941 b_flushing = p_owner->b_flushing;
943 vlc_mutex_unlock( &p_owner->lock );
948 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
950 decoder_owner_sys_t *p_owner = p_dec->p_owner;
952 vlc_assert_locked( &p_owner->lock );
954 while( !p_owner->b_flushing )
956 if( p_owner->b_paused )
958 if( p_owner->b_buffering && !p_owner->buffer.b_full )
960 if( p_owner->pause.i_ignore > 0 )
962 p_owner->pause.i_ignore--;
968 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
971 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
975 *pb_reject = p_owner->b_flushing;
978 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
980 decoder_owner_sys_t *p_owner = p_dec->p_owner;
982 vlc_assert_locked( &p_owner->lock );
984 /* XXX only audio and video output have to be paused.
985 * - for sout it is useless
986 * - for subs, it is done by the vout
988 if( p_dec->fmt_in.i_cat == AUDIO_ES )
990 if( p_owner->p_aout && p_owner->p_aout_input )
991 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
994 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
996 if( p_owner->p_vout )
997 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1000 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1002 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1003 *pi_preroll = INT64_MAX;
1004 else if( p->i_pts > 0 )
1005 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1006 else if( p->i_dts > 0 )
1007 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1010 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
1012 mtime_t current_date = mdate();
1014 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1015 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1017 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1018 * for teletext streams. */
1019 return current_date + 400000 + i_ts_delay;
1024 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1025 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1027 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1028 input_clock_t *p_clock = p_owner->p_clock;
1031 vlc_assert_locked( &p_owner->lock );
1033 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
1034 const mtime_t i_es_delay = p_owner->i_ts_delay;
1038 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1041 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1042 *pi_ts0 + i_es_delay );
1043 if( pi_ts1 && *pi_ts1 > 0 )
1044 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1045 *pi_ts1 + i_es_delay );
1047 /* Do not create ephemere data because of rounding errors */
1048 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1052 i_rate = input_clock_GetRate( p_clock );
1055 *pi_duration = ( *pi_duration * i_rate +
1056 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1063 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1064 if( pi_ts1 && *pi_ts1 <= 0 )
1070 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1071 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1075 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1076 int *pi_played_sum, int *pi_lost_sum )
1078 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1079 aout_instance_t *p_aout = p_owner->p_aout;
1080 aout_input_t *p_aout_input = p_owner->p_aout_input;
1083 if( p_audio->start_date <= 0 )
1085 msg_Warn( p_dec, "non-dated audio buffer received" );
1087 aout_BufferFree( p_audio );
1092 vlc_mutex_lock( &p_owner->lock );
1094 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1096 p_audio->p_next = NULL;
1098 *p_owner->buffer.pp_audio_next = p_audio;
1099 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1101 p_owner->buffer.i_count++;
1102 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1103 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1105 p_owner->buffer.b_full = true;
1106 vlc_cond_signal( &p_owner->wait );
1112 bool b_has_more = false;
1114 DecoderWaitUnblock( p_dec, &b_reject );
1116 if( p_owner->b_buffering )
1118 vlc_mutex_unlock( &p_owner->lock );
1123 if( p_owner->buffer.p_audio )
1125 p_audio = p_owner->buffer.p_audio;
1127 p_owner->buffer.p_audio = p_audio->p_next;
1128 p_owner->buffer.i_count--;
1130 b_has_more = p_owner->buffer.p_audio != NULL;
1132 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1136 int i_rate = INPUT_RATE_DEFAULT;
1139 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1140 &i_rate, &i_delay, false );
1142 vlc_mutex_unlock( &p_owner->lock );
1145 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1147 if( !p_aout || !p_aout_input ||
1148 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1149 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1150 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1153 /* Do not wait against unprotected date */
1154 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1155 while( !b_reject && i_deadline > mdate() )
1157 vlc_mutex_lock( &p_owner->lock );
1158 if( p_owner->b_flushing || p_dec->b_die )
1161 vlc_mutex_unlock( &p_owner->lock );
1164 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1165 vlc_mutex_unlock( &p_owner->lock );
1170 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1171 *pi_played_sum += 1;
1172 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1176 if( p_audio->start_date <= 0 )
1178 msg_Warn( p_dec, "non-dated audio buffer received" );
1180 else if( p_audio->start_date > i_max_date )
1182 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1183 p_audio->start_date - mdate() );
1186 aout_BufferFree( p_audio );
1192 vlc_mutex_lock( &p_owner->lock );
1193 if( !p_owner->buffer.p_audio )
1195 vlc_mutex_unlock( &p_owner->lock );
1201 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1203 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1204 input_thread_t *p_input = p_owner->p_input;
1205 aout_buffer_t *p_aout_buf;
1210 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1212 aout_instance_t *p_aout = p_owner->p_aout;
1213 aout_input_t *p_aout_input = p_owner->p_aout_input;
1219 /* It prevent freezing VLC in case of broken decoder */
1220 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1222 block_Release( p_block );
1227 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1229 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1233 if( p_owner->i_preroll_end > 0 )
1235 msg_Dbg( p_dec, "End of audio preroll" );
1236 if( p_owner->p_aout && p_owner->p_aout_input )
1237 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1239 p_owner->i_preroll_end = -1;
1242 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1245 /* Update ugly stat */
1246 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1248 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1250 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1252 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1254 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1257 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1260 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1262 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1268 assert( p_dec_cc->pf_get_cc != NULL );
1270 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1271 if( !p_owner->cc.b_supported )
1274 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1278 vlc_mutex_lock( &p_owner->lock );
1279 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1281 p_owner->cc.pb_present[i] |= pb_present[i];
1282 if( p_owner->cc.pp_decoder[i] )
1286 for( i = 0; i < 4; i++ )
1288 if( !p_owner->cc.pp_decoder[i] )
1291 if( i_cc_decoder > 1 )
1292 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1294 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1297 vlc_mutex_unlock( &p_owner->lock );
1300 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1301 int *pi_played_sum, int *pi_lost_sum )
1303 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1304 vout_thread_t *p_vout = p_owner->p_vout;
1305 bool b_first_buffered;
1307 if( p_picture->date <= 0 )
1309 msg_Warn( p_vout, "non-dated video buffer received" );
1311 vout_DropPicture( p_vout, p_picture );
1316 vlc_mutex_lock( &p_owner->lock );
1318 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1320 p_picture->p_next = NULL;
1322 *p_owner->buffer.pp_picture_next = p_picture;
1323 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1325 p_owner->buffer.i_count++;
1326 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1327 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1329 p_owner->buffer.b_full = true;
1330 vlc_cond_signal( &p_owner->wait );
1333 b_first_buffered = p_owner->buffer.p_picture != NULL;
1335 for( ;; b_first_buffered = false )
1337 bool b_has_more = false;
1341 DecoderWaitUnblock( p_dec, &b_reject );
1343 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1345 vlc_mutex_unlock( &p_owner->lock );
1348 bool b_buffering_first = p_owner->b_buffering;
1351 if( p_owner->buffer.p_picture )
1353 p_picture = p_owner->buffer.p_picture;
1355 p_owner->buffer.p_picture = p_picture->p_next;
1356 p_owner->buffer.i_count--;
1358 b_has_more = p_owner->buffer.p_picture != NULL;
1360 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1364 int i_rate = INPUT_RATE_DEFAULT;
1367 if( b_buffering_first )
1369 assert( p_owner->buffer.b_first );
1370 assert( !p_owner->buffer.i_count );
1371 msg_Dbg( p_dec, "Received first picture" );
1372 p_owner->buffer.b_first = false;
1373 p_picture->b_force = true;
1375 if( p_owner->p_clock )
1376 i_rate = input_clock_GetRate( p_owner->p_clock );
1378 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1379 &i_rate, &i_delay, false );
1381 vlc_mutex_unlock( &p_owner->lock );
1384 const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1386 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1391 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1393 /* Be sure to not display old picture after our own */
1394 vout_Flush( p_vout, p_picture->date );
1395 p_owner->i_last_rate = i_rate;
1397 vout_DisplayPicture( p_vout, p_picture );
1401 if( p_picture->date <= 0 )
1403 msg_Warn( p_vout, "non-dated video buffer received" );
1407 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1408 p_picture->date - mdate() );
1411 vout_DropPicture( p_vout, p_picture );
1415 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1417 *pi_played_sum += i_tmp_display;
1418 *pi_lost_sum += i_tmp_lost;
1420 if( !b_has_more || b_buffering_first )
1423 vlc_mutex_lock( &p_owner->lock );
1424 if( !p_owner->buffer.p_picture )
1426 vlc_mutex_unlock( &p_owner->lock );
1432 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1434 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1435 input_thread_t *p_input = p_owner->p_input;
1439 int i_displayed = 0;
1441 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1443 vout_thread_t *p_vout = p_owner->p_vout;
1446 /* It prevent freezing VLC in case of broken decoder */
1447 vout_DropPicture( p_vout, p_pic );
1449 block_Release( p_block );
1455 if( p_pic->date < p_owner->i_preroll_end )
1457 vout_DropPicture( p_vout, p_pic );
1461 if( p_owner->i_preroll_end > 0 )
1463 msg_Dbg( p_dec, "End of video preroll" );
1465 vout_Flush( p_vout, 1 );
1467 p_owner->i_preroll_end = -1;
1470 if( p_dec->pf_get_cc &&
1471 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1472 DecoderGetCc( p_dec, p_dec );
1474 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1476 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1478 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1480 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1482 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1485 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1488 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1492 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1495 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1496 vout_thread_t *p_vout = p_owner->p_spu_vout;
1499 if( p_subpic->i_start <= 0 )
1501 msg_Warn( p_dec, "non-dated spu buffer received" );
1502 subpicture_Delete( p_subpic );
1507 vlc_mutex_lock( &p_owner->lock );
1509 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1511 p_subpic->p_next = NULL;
1513 *p_owner->buffer.pp_subpic_next = p_subpic;
1514 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1516 p_owner->buffer.i_count++;
1517 /* XXX it is important to be full after the first one */
1518 if( p_owner->buffer.i_count > 0 )
1520 p_owner->buffer.b_full = true;
1521 vlc_cond_signal( &p_owner->wait );
1527 bool b_has_more = false;
1529 DecoderWaitUnblock( p_dec, &b_reject );
1531 if( p_owner->b_buffering )
1533 vlc_mutex_unlock( &p_owner->lock );
1538 if( p_owner->buffer.p_subpic )
1540 p_subpic = p_owner->buffer.p_subpic;
1542 p_owner->buffer.p_subpic = p_subpic->p_next;
1543 p_owner->buffer.i_count--;
1545 b_has_more = p_owner->buffer.p_subpic != NULL;
1547 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1551 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1552 NULL, NULL, b_telx );
1554 vlc_mutex_unlock( &p_owner->lock );
1557 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1559 subpicture_Delete( p_subpic );
1563 vlc_mutex_lock( &p_owner->lock );
1564 if( !p_owner->buffer.p_subpic )
1566 vlc_mutex_unlock( &p_owner->lock );
1572 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1575 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1577 assert( p_owner->p_clock );
1578 assert( !p_sout_block->p_next );
1580 vlc_mutex_lock( &p_owner->lock );
1582 if( p_owner->b_buffering || p_owner->buffer.p_block )
1584 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1586 p_owner->buffer.i_count++;
1587 /* XXX it is important to be full after the first one */
1588 if( p_owner->buffer.i_count > 0 )
1590 p_owner->buffer.b_full = true;
1591 vlc_cond_signal( &p_owner->wait );
1597 bool b_has_more = false;
1599 DecoderWaitUnblock( p_dec, &b_reject );
1601 if( p_owner->b_buffering )
1603 vlc_mutex_unlock( &p_owner->lock );
1608 if( p_owner->buffer.p_block )
1610 p_sout_block = p_owner->buffer.p_block;
1612 p_owner->buffer.p_block = p_sout_block->p_next;
1613 p_owner->buffer.i_count--;
1615 b_has_more = p_owner->buffer.p_block != NULL;
1617 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1619 p_sout_block->p_next = NULL;
1621 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1622 &p_sout_block->i_length,
1623 &p_sout_block->i_rate, NULL, b_telx );
1625 vlc_mutex_unlock( &p_owner->lock );
1628 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1630 block_Release( p_sout_block );
1634 vlc_mutex_lock( &p_owner->lock );
1635 if( !p_owner->buffer.p_block )
1637 vlc_mutex_unlock( &p_owner->lock );
1644 static void DecoderFlushBuffering( decoder_t *p_dec )
1646 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1648 vlc_assert_locked( &p_owner->lock );
1650 while( p_owner->buffer.p_picture )
1652 picture_t *p_picture = p_owner->buffer.p_picture;
1654 p_owner->buffer.p_picture = p_picture->p_next;
1655 p_owner->buffer.i_count--;
1657 if( p_owner->p_vout )
1658 vout_DropPicture( p_owner->p_vout, p_picture );
1660 if( !p_owner->buffer.p_picture )
1661 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1663 while( p_owner->buffer.p_audio )
1665 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1667 p_owner->buffer.p_audio = p_audio->p_next;
1668 p_owner->buffer.i_count--;
1670 aout_BufferFree( p_audio );
1672 if( !p_owner->buffer.p_audio )
1673 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1675 while( p_owner->buffer.p_subpic )
1677 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1679 p_owner->buffer.p_subpic = p_subpic->p_next;
1680 p_owner->buffer.i_count--;
1682 subpicture_Delete( p_subpic );
1684 if( !p_owner->buffer.p_subpic )
1685 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1687 if( p_owner->buffer.p_block )
1689 block_ChainRelease( p_owner->buffer.p_block );
1691 p_owner->buffer.i_count = 0;
1692 p_owner->buffer.p_block = NULL;
1693 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1697 /* This function process a block for sout
1699 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1701 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1702 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1703 block_t *p_sout_block;
1705 while( ( p_sout_block =
1706 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1708 if( !p_owner->p_sout_input )
1710 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1712 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1713 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1714 if( p_dec->fmt_in.psz_language )
1716 if( p_owner->sout.psz_language )
1717 free( p_owner->sout.psz_language );
1718 p_owner->sout.psz_language =
1719 strdup( p_dec->fmt_in.psz_language );
1722 p_owner->p_sout_input =
1723 sout_InputNew( p_owner->p_sout,
1726 if( p_owner->p_sout_input == NULL )
1728 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1729 (char *)&p_owner->sout.i_codec );
1730 p_dec->b_error = true;
1732 while( p_sout_block )
1734 block_t *p_next = p_sout_block->p_next;
1735 block_Release( p_sout_block );
1736 p_sout_block = p_next;
1742 while( p_sout_block )
1744 block_t *p_next = p_sout_block->p_next;
1746 p_sout_block->p_next = NULL;
1748 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1750 p_sout_block = p_next;
1753 /* For now it's enough, as only sout impact on this flag */
1754 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1755 p_owner->p_input->p->b_out_pace_control )
1757 msg_Dbg( p_dec, "switching to sync mode" );
1758 p_owner->p_input->p->b_out_pace_control = false;
1760 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1761 !p_owner->p_input->p->b_out_pace_control )
1763 msg_Dbg( p_dec, "switching to async mode" );
1764 p_owner->p_input->p->b_out_pace_control = true;
1769 /* This function process a video block
1771 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1773 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1775 if( p_owner->p_packetizer )
1777 block_t *p_packetized_block;
1778 decoder_t *p_packetizer = p_owner->p_packetizer;
1780 while( (p_packetized_block =
1781 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1783 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1785 es_format_Clean( &p_dec->fmt_in );
1786 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1788 if( p_packetizer->pf_get_cc )
1789 DecoderGetCc( p_dec, p_packetizer );
1791 while( p_packetized_block )
1793 block_t *p_next = p_packetized_block->p_next;
1794 p_packetized_block->p_next = NULL;
1796 DecoderDecodeVideo( p_dec, p_packetized_block );
1798 p_packetized_block = p_next;
1804 DecoderDecodeVideo( p_dec, p_block );
1807 if( b_flush && p_owner->p_vout )
1808 vout_Flush( p_owner->p_vout, 1 );
1811 /* This function process a audio block
1813 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1815 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1817 if( p_owner->p_packetizer )
1819 block_t *p_packetized_block;
1820 decoder_t *p_packetizer = p_owner->p_packetizer;
1822 while( (p_packetized_block =
1823 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1825 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1827 es_format_Clean( &p_dec->fmt_in );
1828 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1831 while( p_packetized_block )
1833 block_t *p_next = p_packetized_block->p_next;
1834 p_packetized_block->p_next = NULL;
1836 DecoderDecodeAudio( p_dec, p_packetized_block );
1838 p_packetized_block = p_next;
1844 DecoderDecodeAudio( p_dec, p_block );
1847 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1848 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1851 /* This function process a subtitle block
1853 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1855 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1856 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1858 input_thread_t *p_input = p_owner->p_input;
1859 vout_thread_t *p_vout;
1860 subpicture_t *p_spu;
1862 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1864 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1865 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1866 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1868 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1869 if( p_vout && p_owner->p_spu_vout == p_vout )
1871 /* Preroll does not work very well with subtitle */
1872 if( p_spu->i_start > 0 &&
1873 p_spu->i_start < p_owner->i_preroll_end &&
1874 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1876 subpicture_Delete( p_spu );
1880 DecoderPlaySpu( p_dec, p_spu, b_telx );
1885 subpicture_Delete( p_spu );
1888 vlc_object_release( p_vout );
1891 if( b_flush && p_owner->p_spu_vout )
1893 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1895 if( p_vout && p_owner->p_spu_vout == p_vout )
1896 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1897 p_owner->i_spu_channel );
1900 vlc_object_release( p_vout );
1905 static void DecoderProcessOnFlush( decoder_t *p_dec )
1907 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1909 vlc_mutex_lock( &p_owner->lock );
1910 DecoderFlushBuffering( p_dec );
1911 vlc_mutex_unlock( &p_owner->lock );
1913 DecoderSignalFlushed( p_dec );
1918 * \param p_dec the decoder object
1919 * \param p_block the block to decode
1920 * \return VLC_SUCCESS or an error code
1922 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1924 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1925 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1927 if( p_block && p_block->i_buffer <= 0 )
1929 assert( !b_flush_request );
1930 block_Release( p_block );
1935 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1938 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1940 DecoderProcessSout( p_dec, p_block );
1945 bool b_flush = false;
1949 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1950 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1952 b_flush = !b_flushing && b_flush_request;
1954 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1957 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1959 DecoderProcessAudio( p_dec, p_block, b_flush );
1961 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1963 DecoderProcessVideo( p_dec, p_block, b_flush );
1965 else if( p_dec->fmt_in.i_cat == SPU_ES )
1967 DecoderProcessSpu( p_dec, p_block, b_flush );
1971 msg_Err( p_dec, "unknown ES format" );
1972 p_dec->b_error = true;
1977 if( b_flush_request )
1978 DecoderProcessOnFlush( p_dec );
1981 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1983 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1987 block_Release( p_block );
1989 if( b_flush_request )
1990 DecoderProcessOnFlush( p_dec );
1995 * Destroys a decoder object
1997 * \param p_dec the decoder object
2000 static void DeleteDecoder( decoder_t * p_dec )
2002 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2004 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2005 (char*)&p_dec->fmt_in.i_codec,
2006 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2008 /* Free all packets still in the decoder fifo. */
2009 block_FifoEmpty( p_owner->p_fifo );
2010 block_FifoRelease( p_owner->p_fifo );
2013 vlc_mutex_lock( &p_owner->lock );
2014 DecoderFlushBuffering( p_dec );
2015 vlc_mutex_unlock( &p_owner->lock );
2018 if( p_owner->p_aout_input )
2019 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2020 if( p_owner->p_aout )
2022 vlc_object_release( p_owner->p_aout );
2023 p_owner->p_aout = NULL;
2025 if( p_owner->p_vout )
2027 /* Hack to make sure all the the pictures are freed by the decoder */
2028 vout_FixLeaks( p_owner->p_vout, true );
2030 /* We are about to die. Reattach video output to p_vlc. */
2031 vout_Request( p_dec, p_owner->p_vout, NULL );
2032 input_SendEventVout( p_owner->p_input );
2036 if( p_owner->p_sout_input )
2038 sout_InputDelete( p_owner->p_sout_input );
2039 es_format_Clean( &p_owner->sout );
2043 if( p_dec->fmt_in.i_cat == SPU_ES )
2045 vout_thread_t *p_vout;
2047 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2050 if( p_owner->p_spu_vout == p_vout )
2051 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2052 vlc_object_release( p_vout );
2056 es_format_Clean( &p_dec->fmt_in );
2057 es_format_Clean( &p_dec->fmt_out );
2058 if( p_dec->p_description )
2059 vlc_meta_Delete( p_dec->p_description );
2060 es_format_Clean( &p_owner->fmt_description );
2062 if( p_owner->p_packetizer )
2064 module_unneed( p_owner->p_packetizer,
2065 p_owner->p_packetizer->p_module );
2066 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2067 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2068 if( p_owner->p_packetizer->p_description )
2069 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2070 vlc_object_detach( p_owner->p_packetizer );
2071 vlc_object_release( p_owner->p_packetizer );
2074 vlc_cond_destroy( &p_owner->wait );
2075 vlc_mutex_destroy( &p_owner->lock );
2077 vlc_object_detach( p_dec );
2082 /*****************************************************************************
2083 * Buffers allocation callbacks for the decoders
2084 *****************************************************************************/
2085 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2087 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2089 vlc_assert_locked( &p_owner->lock );
2091 es_format_Clean( &p_owner->fmt_description );
2093 p_owner->b_fmt_description = true;
2094 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2096 static vout_thread_t *aout_request_vout( void *p_private,
2097 vout_thread_t *p_vout, video_format_t *p_fmt )
2099 decoder_t *p_dec = p_private;
2101 p_vout = vout_Request( p_dec, p_vout, p_fmt );
2102 input_SendEventVout( p_dec->p_owner->p_input );
2107 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2109 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2110 aout_buffer_t *p_buffer;
2112 if( p_owner->p_aout_input != NULL &&
2113 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2114 p_dec->fmt_out.audio.i_original_channels !=
2115 p_owner->audio.i_original_channels ||
2116 p_dec->fmt_out.audio.i_bytes_per_frame !=
2117 p_owner->audio.i_bytes_per_frame ) )
2119 aout_input_t *p_aout_input = p_owner->p_aout_input;
2121 /* Parameters changed, restart the aout */
2122 vlc_mutex_lock( &p_owner->lock );
2124 DecoderFlushBuffering( p_dec );
2126 p_owner->p_aout_input = NULL;
2127 aout_DecDelete( p_owner->p_aout, p_aout_input );
2129 vlc_mutex_unlock( &p_owner->lock );
2132 if( p_owner->p_aout_input == NULL )
2134 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2135 audio_sample_format_t format;
2136 aout_input_t *p_aout_input;
2137 aout_instance_t *p_aout;
2138 aout_request_vout_t request_vout;
2140 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2141 p_owner->audio = p_dec->fmt_out.audio;
2143 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2144 if( i_force_dolby &&
2145 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2146 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2148 if( i_force_dolby == 1 )
2150 format.i_original_channels = format.i_original_channels |
2151 AOUT_CHAN_DOLBYSTEREO;
2153 else /* i_force_dolby == 2 */
2155 format.i_original_channels = format.i_original_channels &
2156 ~AOUT_CHAN_DOLBYSTEREO;
2160 request_vout.pf_request_vout = aout_request_vout;
2161 request_vout.p_private = p_dec;
2163 p_aout = p_owner->p_aout;
2164 p_aout_input = aout_DecNew( p_dec, &p_aout,
2165 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2167 vlc_mutex_lock( &p_owner->lock );
2169 p_owner->p_aout = p_aout;
2170 p_owner->p_aout_input = p_aout_input;
2171 DecoderUpdateFormatLocked( p_dec );
2173 vlc_mutex_unlock( &p_owner->lock );
2175 if( p_owner->p_aout_input == NULL )
2177 msg_Err( p_dec, "failed to create audio output" );
2178 p_dec->b_error = true;
2181 p_dec->fmt_out.audio.i_bytes_per_frame =
2182 p_owner->audio.i_bytes_per_frame;
2185 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2190 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2192 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2194 aout_DecDeleteBuffer( p_owner->p_aout,
2195 p_owner->p_aout_input, p_buffer );
2199 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2201 static picture_t *vout_new_buffer( decoder_t *p_dec )
2203 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2205 if( p_owner->p_vout == NULL ||
2206 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2207 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2208 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2209 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2211 vout_thread_t *p_vout;
2213 if( !p_dec->fmt_out.video.i_width ||
2214 !p_dec->fmt_out.video.i_height )
2216 /* Can't create a new vout without display size */
2220 if( !p_dec->fmt_out.video.i_visible_width ||
2221 !p_dec->fmt_out.video.i_visible_height )
2223 if( p_dec->fmt_in.video.i_visible_width &&
2224 p_dec->fmt_in.video.i_visible_height )
2226 p_dec->fmt_out.video.i_visible_width =
2227 p_dec->fmt_in.video.i_visible_width;
2228 p_dec->fmt_out.video.i_visible_height =
2229 p_dec->fmt_in.video.i_visible_height;
2233 p_dec->fmt_out.video.i_visible_width =
2234 p_dec->fmt_out.video.i_width;
2235 p_dec->fmt_out.video.i_visible_height =
2236 p_dec->fmt_out.video.i_height;
2240 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2241 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2243 p_dec->fmt_out.video.i_visible_height = 1080;
2244 p_dec->fmt_out.video.i_sar_num *= 135;
2245 p_dec->fmt_out.video.i_sar_den *= 136;
2246 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2249 if( !p_dec->fmt_out.video.i_sar_num ||
2250 !p_dec->fmt_out.video.i_sar_den )
2252 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2253 p_dec->fmt_out.video.i_visible_height;
2255 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2256 p_dec->fmt_out.video.i_visible_width;
2259 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2260 &p_dec->fmt_out.video.i_sar_den,
2261 p_dec->fmt_out.video.i_sar_num,
2262 p_dec->fmt_out.video.i_sar_den, 50000 );
2264 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2265 p_owner->video = p_dec->fmt_out.video;
2267 vlc_mutex_lock( &p_owner->lock );
2269 DecoderFlushBuffering( p_dec );
2271 p_vout = p_owner->p_vout;
2272 p_owner->p_vout = NULL;
2273 vlc_mutex_unlock( &p_owner->lock );
2275 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2277 vlc_mutex_lock( &p_owner->lock );
2278 p_owner->p_vout = p_vout;
2280 DecoderUpdateFormatLocked( p_dec );
2282 vlc_mutex_unlock( &p_owner->lock );
2284 input_SendEventVout( p_owner->p_input );
2285 if( p_vout == NULL )
2287 msg_Err( p_dec, "failed to create video output" );
2288 p_dec->b_error = true;
2292 if( p_owner->video.i_rmask )
2293 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2294 if( p_owner->video.i_gmask )
2295 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2296 if( p_owner->video.i_bmask )
2297 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2300 /* Get a new picture
2304 picture_t *p_picture;
2306 if( p_dec->b_die || p_dec->b_error )
2309 /* The video filter chain required that there is always 1 free buffer
2310 * that it will use as temporary one. It will release the temporary
2311 * buffer once its work is done, so this check is safe even if we don't
2312 * lock around both count() and create().
2314 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2316 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2321 if( DecoderIsFlushing( p_dec ) )
2325 DecoderSignalBuffering( p_dec, true );
2327 /* Check the decoder doesn't leak pictures */
2328 vout_FixLeaks( p_owner->p_vout, false );
2330 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2331 msleep( VOUT_OUTMEM_SLEEP );
2335 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2337 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2340 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2342 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2345 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2347 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2350 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2352 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2353 vout_thread_t *p_vout = NULL;
2354 subpicture_t *p_subpic;
2355 int i_attempts = 30;
2357 while( i_attempts-- )
2359 if( p_dec->b_die || p_dec->b_error )
2362 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2366 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2371 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2375 if( p_owner->p_spu_vout != p_vout )
2377 vlc_mutex_lock( &p_owner->lock );
2379 DecoderFlushBuffering( p_dec );
2381 vlc_mutex_unlock( &p_owner->lock );
2383 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2384 &p_owner->i_spu_channel );
2385 p_owner->i_spu_order = 0;
2386 p_owner->p_spu_vout = p_vout;
2389 p_subpic = subpicture_New();
2392 p_subpic->i_channel = p_owner->i_spu_channel;
2393 p_subpic->i_order = p_owner->i_spu_order++;
2394 p_subpic->b_subtitle = true;
2397 vlc_object_release( p_vout );
2402 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2404 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2405 vout_thread_t *p_vout = NULL;
2407 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2408 if( !p_vout || p_owner->p_spu_vout != p_vout )
2411 vlc_object_release( p_vout );
2412 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2416 subpicture_Delete( p_subpic );
2418 vlc_object_release( p_vout );