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>
44 #include <vlc_interface.h>
45 #include "audio_output/aout_internal.h"
46 #include "stream_output/stream_output.h"
47 #include "input_internal.h"
51 #include "ressource.h"
53 #include "../video_output/vout_control.h"
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
56 static void DeleteDecoder( decoder_t * );
58 static void *DecoderThread( vlc_object_t * );
59 static void DecoderProcess( decoder_t *, block_t * );
60 static void DecoderError( decoder_t *p_dec, block_t *p_block );
61 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
62 static void DecoderFlush( decoder_t * );
63 static void DecoderSignalBuffering( decoder_t *, bool );
64 static void DecoderFlushBuffering( decoder_t * );
66 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
68 /* Buffers allocation callbacks for the decoders */
69 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
70 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
72 static picture_t *vout_new_buffer( decoder_t * );
73 static void vout_del_buffer( decoder_t *, picture_t * );
74 static void vout_link_picture( decoder_t *, picture_t * );
75 static void vout_unlink_picture( decoder_t *, picture_t * );
77 static subpicture_t *spu_new_buffer( decoder_t * );
78 static void spu_del_buffer( decoder_t *, subpicture_t * );
80 static es_format_t null_es_format;
82 struct decoder_owner_sys_t
84 int64_t i_preroll_end;
86 input_thread_t *p_input;
87 input_clock_t *p_clock;
90 vout_thread_t *p_spu_vout;
94 sout_instance_t *p_sout;
95 sout_packetizer_input_t *p_sout_input;
97 /* Some decoders require already packetized data (ie. not truncated) */
98 decoder_t *p_packetizer;
100 /* Current format in use by the output */
101 video_format_t video;
102 audio_format_t audio;
106 bool b_fmt_description;
107 es_format_t fmt_description;
108 vlc_meta_t *p_description;
111 block_fifo_t *p_fifo;
113 /* Lock for communication with decoder thread */
117 /* -- These variables need locking on write(only) -- */
118 aout_instance_t *p_aout;
119 aout_input_t *p_aout_input;
121 vout_thread_t *p_vout;
123 /* -- Theses variables need locking on read *and* write -- */
141 picture_t *p_picture;
142 picture_t **pp_picture_next;
144 subpicture_t *p_subpic;
145 subpicture_t **pp_subpic_next;
147 aout_buffer_t *p_audio;
148 aout_buffer_t **pp_audio_next;
151 block_t **pp_block_next;
162 decoder_t *pp_decoder[4];
169 #define DECODER_MAX_BUFFERING_COUNT (4)
170 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
171 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
173 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
174 * a bogus PTS and won't be displayed */
175 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
178 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
181 /*****************************************************************************
183 *****************************************************************************/
184 picture_t *decoder_NewPicture( decoder_t *p_decoder )
186 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
188 msg_Warn( p_decoder, "can't get output picture" );
191 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
193 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
195 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
197 p_decoder->pf_picture_link( p_decoder, p_picture );
199 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
201 p_decoder->pf_picture_unlink( p_decoder, p_picture );
204 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
206 if( !p_decoder->pf_aout_buffer_new )
208 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
210 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
212 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
215 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
217 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
219 msg_Warn( p_decoder, "can't get output subpicture" );
222 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
224 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
227 /* decoder_GetInputAttachments:
229 int decoder_GetInputAttachments( decoder_t *p_dec,
230 input_attachment_t ***ppp_attachment,
233 if( !p_dec->pf_get_attachments )
236 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
238 /* decoder_GetDisplayDate:
240 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
242 if( !p_dec->pf_get_display_date )
245 return p_dec->pf_get_display_date( p_dec, i_ts );
247 /* decoder_GetDisplayRate:
249 int decoder_GetDisplayRate( decoder_t *p_dec )
251 if( !p_dec->pf_get_display_rate )
252 return INPUT_RATE_DEFAULT;
254 return p_dec->pf_get_display_rate( p_dec );
258 * Spawns a new decoder thread
260 * \param p_input the input thread
261 * \param p_es the es descriptor
262 * \return the spawned decoder object
264 decoder_t *input_DecoderNew( input_thread_t *p_input,
265 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
267 decoder_t *p_dec = NULL;
271 /* If we are in sout mode, search for packetizer module */
274 /* Create the decoder configuration structure */
275 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
278 msg_Err( p_input, "could not create packetizer" );
279 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
280 _("VLC could not open the packetizer module.") );
287 /* Create the decoder configuration structure */
288 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
291 msg_Err( p_input, "could not create decoder" );
292 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
293 _("VLC could not open the decoder module.") );
298 if( !p_dec->p_module )
300 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
302 DeleteDecoder( p_dec );
303 vlc_object_release( p_dec );
307 p_dec->p_owner->p_clock = p_clock;
309 if( fmt->i_cat == AUDIO_ES )
310 i_priority = VLC_THREAD_PRIORITY_AUDIO;
312 i_priority = VLC_THREAD_PRIORITY_VIDEO;
314 /* Spawn the decoder thread */
315 if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
317 msg_Err( p_dec, "cannot spawn decoder thread" );
318 module_unneed( p_dec, p_dec->p_module );
319 DeleteDecoder( p_dec );
320 vlc_object_release( p_dec );
328 * Kills a decoder thread and waits until it's finished
330 * \param p_input the input thread
331 * \param p_es the es descriptor
334 void input_DecoderDelete( decoder_t *p_dec )
336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
338 vlc_object_kill( p_dec );
340 /* Make sure we aren't paused/buffering/waiting anymore */
341 vlc_mutex_lock( &p_owner->lock );
342 const bool b_was_paused = p_owner->b_paused;
343 p_owner->b_paused = false;
344 p_owner->b_buffering = false;
345 p_owner->b_flushing = true;
346 vlc_cond_signal( &p_owner->wait );
347 vlc_mutex_unlock( &p_owner->lock );
349 vlc_thread_join( p_dec );
350 p_owner->b_paused = b_was_paused;
352 module_unneed( p_dec, p_dec->p_module );
355 if( p_dec->p_owner->cc.b_supported )
358 for( i = 0; i < 4; i++ )
359 input_DecoderSetCcState( p_dec, false, i );
362 /* Delete decoder configuration */
363 DeleteDecoder( p_dec );
365 /* Delete the decoder */
366 vlc_object_release( p_dec );
370 * Put a block_t in the decoder's fifo.
371 * Thread-safe w.r.t. the decoder. May be a cancellation point.
373 * \param p_dec the decoder object
374 * \param p_block the data block
376 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
378 decoder_owner_sys_t *p_owner = p_dec->p_owner;
380 if( p_owner->p_input->p->b_out_pace_control )
382 /* The fifo is not consummed when buffering and so will
384 * There is no need to lock as b_buffering is never modify
385 * inside decoder thread. */
386 if( !p_owner->b_buffering )
387 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
389 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
391 /* FIXME: ideally we would check the time amount of data
392 * in the FIFO instead of its size. */
393 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
394 "consumed quickly enough), resetting fifo!" );
395 block_FifoEmpty( p_owner->p_fifo );
398 block_FifoPut( p_owner->p_fifo, p_block );
401 bool input_DecoderIsEmpty( decoder_t * p_dec )
403 assert( !p_dec->p_owner->b_buffering );
405 /* FIXME that's not really true */
406 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
409 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
411 decoder_owner_sys_t *p_owner = p_dec->p_owner;
414 vlc_mutex_lock( &p_owner->lock );
415 for( i = 0; i < 4; i++ )
416 pb_present[i] = p_owner->cc.pb_present[i];
417 vlc_mutex_unlock( &p_owner->lock );
419 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
421 decoder_owner_sys_t *p_owner = p_dec->p_owner;
423 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
425 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
430 static const vlc_fourcc_t fcc[4] = {
431 VLC_FOURCC('c', 'c', '1', ' '),
432 VLC_FOURCC('c', 'c', '2', ' '),
433 VLC_FOURCC('c', 'c', '3', ' '),
434 VLC_FOURCC('c', 'c', '4', ' '),
439 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
440 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
443 msg_Err( p_dec, "could not create decoder" );
444 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
445 _("VLC could not open the decoder module.") );
448 else if( !p_cc->p_module )
450 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
451 DeleteDecoder( p_cc );
452 vlc_object_release( p_cc );
455 p_cc->p_owner->p_clock = p_owner->p_clock;
457 vlc_mutex_lock( &p_owner->lock );
458 p_owner->cc.pp_decoder[i_channel] = p_cc;
459 vlc_mutex_unlock( &p_owner->lock );
465 vlc_mutex_lock( &p_owner->lock );
466 p_cc = p_owner->cc.pp_decoder[i_channel];
467 p_owner->cc.pp_decoder[i_channel] = NULL;
468 vlc_mutex_unlock( &p_owner->lock );
472 vlc_object_kill( p_cc );
473 module_unneed( p_cc, p_cc->p_module );
474 DeleteDecoder( p_cc );
475 vlc_object_release( p_cc );
480 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
482 decoder_owner_sys_t *p_owner = p_dec->p_owner;
485 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
488 vlc_mutex_lock( &p_owner->lock );
489 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
490 vlc_mutex_unlock( &p_owner->lock );
494 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
496 decoder_owner_sys_t *p_owner = p_dec->p_owner;
498 vlc_mutex_lock( &p_owner->lock );
500 assert( !p_owner->b_paused || !b_paused );
502 p_owner->b_paused = b_paused;
503 p_owner->pause.i_date = i_date;
504 p_owner->pause.i_ignore = 0;
505 vlc_cond_signal( &p_owner->wait );
507 DecoderOutputChangePause( p_dec, b_paused, i_date );
509 vlc_mutex_unlock( &p_owner->lock );
512 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
514 decoder_owner_sys_t *p_owner = p_dec->p_owner;
516 vlc_mutex_lock( &p_owner->lock );
517 p_owner->i_ts_delay = i_delay;
518 vlc_mutex_unlock( &p_owner->lock );
521 void input_DecoderStartBuffering( decoder_t *p_dec )
523 decoder_owner_sys_t *p_owner = p_dec->p_owner;
525 vlc_mutex_lock( &p_owner->lock );
527 DecoderFlush( p_dec );
529 p_owner->buffer.b_first = true;
530 p_owner->buffer.b_full = false;
531 p_owner->buffer.i_count = 0;
533 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
534 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
536 p_owner->buffer.p_picture = NULL;
537 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
539 p_owner->buffer.p_subpic = NULL;
540 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
542 p_owner->buffer.p_audio = NULL;
543 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
545 p_owner->buffer.p_block = NULL;
546 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
549 p_owner->b_buffering = true;
551 vlc_cond_signal( &p_owner->wait );
553 vlc_mutex_unlock( &p_owner->lock );
556 void input_DecoderStopBuffering( decoder_t *p_dec )
558 decoder_owner_sys_t *p_owner = p_dec->p_owner;
560 vlc_mutex_lock( &p_owner->lock );
562 p_owner->b_buffering = false;
564 vlc_cond_signal( &p_owner->wait );
566 vlc_mutex_unlock( &p_owner->lock );
569 void input_DecoderWaitBuffering( decoder_t *p_dec )
571 decoder_owner_sys_t *p_owner = p_dec->p_owner;
573 vlc_mutex_lock( &p_owner->lock );
575 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
577 block_FifoWake( p_owner->p_fifo );
578 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
581 vlc_mutex_unlock( &p_owner->lock );
584 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
586 decoder_owner_sys_t *p_owner = p_dec->p_owner;
590 vlc_mutex_lock( &p_owner->lock );
591 if( p_dec->fmt_in.i_cat == VIDEO_ES )
593 if( p_owner->b_paused && p_owner->p_vout )
595 vout_NextPicture( p_owner->p_vout, pi_duration );
596 p_owner->pause.i_ignore++;
597 vlc_cond_signal( &p_owner->wait );
602 /* TODO subtitle should not be flushed */
603 DecoderFlush( p_dec );
605 vlc_mutex_unlock( &p_owner->lock );
608 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
610 decoder_owner_sys_t *p_owner = p_dec->p_owner;
613 vlc_mutex_lock( &p_owner->lock );
614 b_changed = p_owner->b_fmt_description;
618 es_format_Copy( p_fmt, &p_owner->fmt_description );
623 if( p_owner->p_description )
625 *pp_meta = vlc_meta_New();
627 vlc_meta_Merge( *pp_meta, p_owner->p_description );
630 p_owner->b_fmt_description = false;
632 vlc_mutex_unlock( &p_owner->lock );
636 /*****************************************************************************
638 *****************************************************************************/
639 static int DecoderGetInputAttachments( decoder_t *p_dec,
640 input_attachment_t ***ppp_attachment,
643 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
644 ppp_attachment, pi_attachment );
646 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
648 decoder_owner_sys_t *p_owner = p_dec->p_owner;
650 vlc_mutex_lock( &p_owner->lock );
651 if( p_owner->b_buffering || p_owner->b_paused )
653 vlc_mutex_unlock( &p_owner->lock );
655 if( !p_owner->p_clock || !i_ts )
658 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
663 static int DecoderGetDisplayRate( decoder_t *p_dec )
665 decoder_owner_sys_t *p_owner = p_dec->p_owner;
667 if( !p_owner->p_clock )
668 return INPUT_RATE_DEFAULT;
669 return input_clock_GetRate( p_owner->p_clock );
673 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
675 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
676 "VLC probably does not support this sound or video format.",
678 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
679 _("VLC does not support the audio or video format \"%4.4s\". "
680 "Unfortunately there is no way for you to fix this."), (char*)&codec );
685 * Create a decoder object
687 * \param p_input the input thread
688 * \param p_es the es descriptor
689 * \param i_object_type Object type as define in include/vlc_objects.h
690 * \return the decoder object
692 static decoder_t * CreateDecoder( input_thread_t *p_input,
693 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
696 decoder_owner_sys_t *p_owner;
699 p_dec = vlc_object_create( p_input, i_object_type );
703 p_dec->pf_decode_audio = NULL;
704 p_dec->pf_decode_video = NULL;
705 p_dec->pf_decode_sub = NULL;
706 p_dec->pf_get_cc = NULL;
707 p_dec->pf_packetize = NULL;
709 /* Initialize the decoder */
710 p_dec->p_module = NULL;
712 memset( &null_es_format, 0, sizeof(es_format_t) );
713 es_format_Copy( &p_dec->fmt_in, fmt );
714 es_format_Copy( &p_dec->fmt_out, &null_es_format );
716 p_dec->p_description = NULL;
718 /* Allocate our private structure for the decoder */
719 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
720 if( p_dec->p_owner == NULL )
722 vlc_object_release( p_dec );
725 p_dec->p_owner->i_preroll_end = -1;
726 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
727 p_dec->p_owner->p_input = p_input;
728 p_dec->p_owner->p_aout = NULL;
729 p_dec->p_owner->p_aout_input = NULL;
730 p_dec->p_owner->p_vout = NULL;
731 p_dec->p_owner->p_spu_vout = NULL;
732 p_dec->p_owner->i_spu_channel = 0;
733 p_dec->p_owner->i_spu_order = 0;
734 p_dec->p_owner->p_sout = p_sout;
735 p_dec->p_owner->p_sout_input = NULL;
736 p_dec->p_owner->p_packetizer = NULL;
739 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
741 free( p_dec->p_owner );
742 vlc_object_release( p_dec );
746 /* Set buffers allocation callbacks for the decoders */
747 p_dec->pf_aout_buffer_new = aout_new_buffer;
748 p_dec->pf_aout_buffer_del = aout_del_buffer;
749 p_dec->pf_vout_buffer_new = vout_new_buffer;
750 p_dec->pf_vout_buffer_del = vout_del_buffer;
751 p_dec->pf_picture_link = vout_link_picture;
752 p_dec->pf_picture_unlink = vout_unlink_picture;
753 p_dec->pf_spu_buffer_new = spu_new_buffer;
754 p_dec->pf_spu_buffer_del = spu_del_buffer;
756 p_dec->pf_get_attachments = DecoderGetInputAttachments;
757 p_dec->pf_get_display_date = DecoderGetDisplayDate;
758 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
760 vlc_object_attach( p_dec, p_input );
762 /* Find a suitable decoder/packetizer module */
763 if( i_object_type == VLC_OBJECT_DECODER )
764 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
766 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
768 /* Check if decoder requires already packetized data */
769 if( i_object_type == VLC_OBJECT_DECODER &&
770 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
772 p_dec->p_owner->p_packetizer =
773 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
774 if( p_dec->p_owner->p_packetizer )
776 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
779 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
782 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
784 p_dec->p_owner->p_packetizer->p_module =
785 module_need( p_dec->p_owner->p_packetizer,
786 "packetizer", "$packetizer", false );
788 if( !p_dec->p_owner->p_packetizer->p_module )
790 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
791 vlc_object_detach( p_dec->p_owner->p_packetizer );
792 vlc_object_release( p_dec->p_owner->p_packetizer );
797 /* Copy ourself the input replay gain */
798 if( fmt->i_cat == AUDIO_ES )
800 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
802 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
804 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
805 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
807 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
809 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
810 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
816 vlc_mutex_init( &p_owner->lock );
817 vlc_cond_init( &p_owner->wait );
819 p_owner->b_fmt_description = false;
820 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
821 p_owner->p_description = NULL;
823 p_owner->b_paused = false;
824 p_owner->pause.i_date = 0;
825 p_owner->pause.i_ignore = 0;
827 p_owner->b_buffering = false;
828 p_owner->buffer.b_first = true;
829 p_owner->buffer.b_full = false;
830 p_owner->buffer.i_count = 0;
831 p_owner->buffer.p_picture = NULL;
832 p_owner->buffer.p_subpic = NULL;
833 p_owner->buffer.p_audio = NULL;
834 p_owner->buffer.p_block = NULL;
836 p_owner->b_flushing = false;
839 p_owner->cc.b_supported = false;
840 if( i_object_type == VLC_OBJECT_DECODER )
842 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
843 p_owner->cc.b_supported = true;
844 if( p_dec->pf_get_cc )
845 p_owner->cc.b_supported = true;
848 for( i = 0; i < 4; i++ )
850 p_owner->cc.pb_present[i] = false;
851 p_owner->cc.pp_decoder[i] = NULL;
853 p_owner->i_ts_delay = 0;
858 * The decoding main loop
860 * \param p_dec the decoder
862 static void *DecoderThread( vlc_object_t *p_this )
864 decoder_t *p_dec = (decoder_t *)p_this;
865 decoder_owner_sys_t *p_owner = p_dec->p_owner;
867 /* The decoder's main loop */
870 block_t *p_block = block_FifoGet( p_owner->p_fifo );
872 /* Make sure there is no cancellation point other than this one^^.
873 * If you need one, be sure to push cleanup of p_block. */
874 DecoderSignalBuffering( p_dec, p_block == NULL );
878 int canc = vlc_savecancel();
881 DecoderError( p_dec, p_block );
883 DecoderProcess( p_dec, p_block );
885 vlc_restorecancel( canc );
891 static void DecoderFlush( decoder_t *p_dec )
893 decoder_owner_sys_t *p_owner = p_dec->p_owner;
896 vlc_assert_locked( &p_owner->lock );
899 block_FifoEmpty( p_owner->p_fifo );
901 /* Monitor for flush end */
902 p_owner->b_flushing = true;
903 vlc_cond_signal( &p_owner->wait );
905 /* Send a special block */
906 p_null = block_New( p_dec, 128 );
909 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
910 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
911 if( !p_dec->fmt_in.b_packetized )
912 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
913 memset( p_null->p_buffer, 0, p_null->i_buffer );
915 input_DecoderDecode( p_dec, p_null );
918 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
919 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
922 static void DecoderSignalFlushed( decoder_t *p_dec )
924 decoder_owner_sys_t *p_owner = p_dec->p_owner;
926 vlc_mutex_lock( &p_owner->lock );
928 if( p_owner->b_flushing )
930 p_owner->b_flushing = false;
931 vlc_cond_signal( &p_owner->wait );
934 vlc_mutex_unlock( &p_owner->lock );
937 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
939 decoder_owner_sys_t *p_owner = p_dec->p_owner;
941 vlc_mutex_lock( &p_owner->lock );
943 if( p_owner->b_buffering )
946 p_owner->buffer.b_full = true;
947 vlc_cond_signal( &p_owner->wait );
950 vlc_mutex_unlock( &p_owner->lock );
953 static bool DecoderIsFlushing( decoder_t *p_dec )
955 decoder_owner_sys_t *p_owner = p_dec->p_owner;
958 vlc_mutex_lock( &p_owner->lock );
960 b_flushing = p_owner->b_flushing;
962 vlc_mutex_unlock( &p_owner->lock );
967 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
969 decoder_owner_sys_t *p_owner = p_dec->p_owner;
971 vlc_assert_locked( &p_owner->lock );
973 while( !p_owner->b_flushing )
975 if( p_owner->b_paused )
977 if( p_owner->b_buffering && !p_owner->buffer.b_full )
979 if( p_owner->pause.i_ignore > 0 )
981 p_owner->pause.i_ignore--;
987 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
990 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
994 *pb_reject = p_owner->b_flushing;
997 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
999 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1001 vlc_assert_locked( &p_owner->lock );
1003 /* XXX only audio and video output have to be paused.
1004 * - for sout it is useless
1005 * - for subs, it is done by the vout
1007 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1009 if( p_owner->p_aout && p_owner->p_aout_input )
1010 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1013 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1015 if( p_owner->p_vout )
1016 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1019 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1021 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1022 *pi_preroll = INT64_MAX;
1023 else if( p->i_pts > 0 )
1024 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1025 else if( p->i_dts > 0 )
1026 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1029 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1031 mtime_t current_date = mdate();
1033 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1034 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1036 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1037 * for teletext streams. */
1038 return current_date + 400000;
1043 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1044 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1046 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1047 input_clock_t *p_clock = p_owner->p_clock;
1049 vlc_assert_locked( &p_owner->lock );
1051 const mtime_t i_es_delay = p_owner->i_ts_delay;
1055 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1060 *pi_ts0 += i_es_delay;
1061 if( pi_ts1 && *pi_ts1 > 0 )
1062 *pi_ts1 += i_es_delay;
1063 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1068 i_rate = input_clock_GetRate( p_clock );
1071 /* Do not create ephemere data because of rounding errors */
1072 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1076 *pi_duration = ( *pi_duration * i_rate +
1077 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1084 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1085 if( pi_ts1 && *pi_ts1 <= 0 )
1091 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1092 int *pi_played_sum, int *pi_lost_sum )
1094 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1095 aout_instance_t *p_aout = p_owner->p_aout;
1096 aout_input_t *p_aout_input = p_owner->p_aout_input;
1099 if( p_audio->start_date <= 0 )
1101 msg_Warn( p_dec, "non-dated audio buffer received" );
1103 aout_BufferFree( p_audio );
1108 vlc_mutex_lock( &p_owner->lock );
1110 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1112 p_audio->p_next = NULL;
1114 *p_owner->buffer.pp_audio_next = p_audio;
1115 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1117 p_owner->buffer.i_count++;
1118 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1119 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1121 p_owner->buffer.b_full = true;
1122 vlc_cond_signal( &p_owner->wait );
1128 bool b_has_more = false;
1130 DecoderWaitUnblock( p_dec, &b_reject );
1132 if( p_owner->b_buffering )
1134 vlc_mutex_unlock( &p_owner->lock );
1139 if( p_owner->buffer.p_audio )
1141 p_audio = p_owner->buffer.p_audio;
1143 p_owner->buffer.p_audio = p_audio->p_next;
1144 p_owner->buffer.i_count--;
1146 b_has_more = p_owner->buffer.p_audio != NULL;
1148 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1152 const bool b_dated = p_audio->start_date > 0;
1153 int i_rate = INPUT_RATE_DEFAULT;
1155 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1156 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1158 vlc_mutex_unlock( &p_owner->lock );
1160 if( !p_aout || !p_aout_input ||
1161 p_audio->start_date <= 0 ||
1162 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1163 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1166 /* Do not wait against unprotected date */
1167 const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1168 while( !b_reject && i_deadline > mdate() )
1170 vlc_mutex_lock( &p_owner->lock );
1171 if( p_owner->b_flushing || p_dec->b_die )
1174 vlc_mutex_unlock( &p_owner->lock );
1177 vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1178 vlc_mutex_unlock( &p_owner->lock );
1183 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1184 *pi_played_sum += 1;
1185 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1190 msg_Warn( p_aout, "received buffer in the future" );
1192 msg_Warn( p_dec, "non-dated audio buffer received" );
1195 aout_BufferFree( p_audio );
1201 vlc_mutex_lock( &p_owner->lock );
1202 if( !p_owner->buffer.p_audio )
1204 vlc_mutex_unlock( &p_owner->lock );
1210 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1212 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1213 input_thread_t *p_input = p_owner->p_input;
1214 aout_buffer_t *p_aout_buf;
1219 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1221 aout_instance_t *p_aout = p_owner->p_aout;
1222 aout_input_t *p_aout_input = p_owner->p_aout_input;
1226 /* It prevent freezing VLC in case of broken decoder */
1227 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1229 block_Release( p_block );
1234 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1236 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1240 if( p_owner->i_preroll_end > 0 )
1242 msg_Dbg( p_dec, "End of audio preroll" );
1243 if( p_owner->p_aout && p_owner->p_aout_input )
1244 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1246 p_owner->i_preroll_end = -1;
1249 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1252 /* Update ugly stat */
1253 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1255 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1257 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1259 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1261 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1264 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1267 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1269 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1275 assert( p_dec_cc->pf_get_cc != NULL );
1277 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1278 if( !p_owner->cc.b_supported )
1281 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1285 vlc_mutex_lock( &p_owner->lock );
1286 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1288 p_owner->cc.pb_present[i] |= pb_present[i];
1289 if( p_owner->cc.pp_decoder[i] )
1293 for( i = 0; i < 4; i++ )
1295 if( !p_owner->cc.pp_decoder[i] )
1298 if( i_cc_decoder > 1 )
1299 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1301 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1304 vlc_mutex_unlock( &p_owner->lock );
1307 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1308 int *pi_played_sum, int *pi_lost_sum )
1310 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1311 vout_thread_t *p_vout = p_owner->p_vout;
1312 bool b_first_buffered;
1314 if( p_picture->date <= 0 )
1316 msg_Warn( p_vout, "non-dated video buffer received" );
1318 vout_DropPicture( p_vout, p_picture );
1323 vlc_mutex_lock( &p_owner->lock );
1325 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1327 p_picture->p_next = NULL;
1329 *p_owner->buffer.pp_picture_next = p_picture;
1330 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1332 p_owner->buffer.i_count++;
1333 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1334 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1336 p_owner->buffer.b_full = true;
1337 vlc_cond_signal( &p_owner->wait );
1340 b_first_buffered = p_owner->buffer.p_picture != NULL;
1342 for( ;; b_first_buffered = false )
1344 bool b_has_more = false;
1348 DecoderWaitUnblock( p_dec, &b_reject );
1350 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1352 vlc_mutex_unlock( &p_owner->lock );
1355 bool b_buffering_first = p_owner->b_buffering;
1358 if( p_owner->buffer.p_picture )
1360 p_picture = p_owner->buffer.p_picture;
1362 p_owner->buffer.p_picture = p_picture->p_next;
1363 p_owner->buffer.i_count--;
1365 b_has_more = p_owner->buffer.p_picture != NULL;
1367 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1371 if( b_buffering_first )
1373 assert( p_owner->buffer.b_first );
1374 assert( !p_owner->buffer.i_count );
1375 msg_Dbg( p_dec, "Received first picture" );
1376 p_owner->buffer.b_first = false;
1377 p_picture->b_force = true;
1380 const bool b_dated = p_picture->date > 0;
1381 int i_rate = INPUT_RATE_DEFAULT;
1382 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1383 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1385 vlc_mutex_unlock( &p_owner->lock );
1388 if( !p_picture->b_force && p_picture->date <= 0 )
1393 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1395 /* Be sure to not display old picture after our own */
1396 vout_Flush( p_vout, p_picture->date );
1397 p_owner->i_last_rate = i_rate;
1399 vout_DisplayPicture( p_vout, p_picture );
1404 msg_Warn( p_vout, "early picture skipped" );
1406 msg_Warn( p_vout, "non-dated video buffer received" );
1409 vout_DropPicture( p_vout, p_picture );
1413 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1415 *pi_played_sum += i_tmp_display;
1416 *pi_lost_sum += i_tmp_lost;
1418 if( !b_has_more || b_buffering_first )
1421 vlc_mutex_lock( &p_owner->lock );
1422 if( !p_owner->buffer.p_picture )
1424 vlc_mutex_unlock( &p_owner->lock );
1430 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1432 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1433 input_thread_t *p_input = p_owner->p_input;
1437 int i_displayed = 0;
1439 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1441 vout_thread_t *p_vout = p_owner->p_vout;
1444 /* It prevent freezing VLC in case of broken decoder */
1445 vout_DropPicture( p_vout, p_pic );
1447 block_Release( p_block );
1453 if( p_pic->date < p_owner->i_preroll_end )
1455 vout_DropPicture( p_vout, p_pic );
1459 if( p_owner->i_preroll_end > 0 )
1461 msg_Dbg( p_dec, "End of video preroll" );
1463 vout_Flush( p_vout, 1 );
1465 p_owner->i_preroll_end = -1;
1468 if( p_dec->pf_get_cc &&
1469 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1470 DecoderGetCc( p_dec, p_dec );
1472 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1474 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1476 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1478 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1480 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1483 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1486 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1490 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1493 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1494 vout_thread_t *p_vout = p_owner->p_spu_vout;
1497 if( p_subpic->i_start <= 0 )
1499 msg_Warn( p_dec, "non-dated spu buffer received" );
1500 subpicture_Delete( p_subpic );
1505 vlc_mutex_lock( &p_owner->lock );
1507 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1509 p_subpic->p_next = NULL;
1511 *p_owner->buffer.pp_subpic_next = p_subpic;
1512 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1514 p_owner->buffer.i_count++;
1515 /* XXX it is important to be full after the first one */
1516 if( p_owner->buffer.i_count > 0 )
1518 p_owner->buffer.b_full = true;
1519 vlc_cond_signal( &p_owner->wait );
1525 bool b_has_more = false;
1527 DecoderWaitUnblock( p_dec, &b_reject );
1529 if( p_owner->b_buffering )
1531 vlc_mutex_unlock( &p_owner->lock );
1536 if( p_owner->buffer.p_subpic )
1538 p_subpic = p_owner->buffer.p_subpic;
1540 p_owner->buffer.p_subpic = p_subpic->p_next;
1541 p_owner->buffer.i_count--;
1543 b_has_more = p_owner->buffer.p_subpic != NULL;
1545 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1549 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1550 NULL, INT64_MAX, b_telx );
1552 vlc_mutex_unlock( &p_owner->lock );
1555 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1557 subpicture_Delete( p_subpic );
1561 vlc_mutex_lock( &p_owner->lock );
1562 if( !p_owner->buffer.p_subpic )
1564 vlc_mutex_unlock( &p_owner->lock );
1570 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1573 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1575 assert( p_owner->p_clock );
1576 assert( !p_sout_block->p_next );
1578 vlc_mutex_lock( &p_owner->lock );
1580 if( p_owner->b_buffering || p_owner->buffer.p_block )
1582 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1584 p_owner->buffer.i_count++;
1585 /* XXX it is important to be full after the first one */
1586 if( p_owner->buffer.i_count > 0 )
1588 p_owner->buffer.b_full = true;
1589 vlc_cond_signal( &p_owner->wait );
1595 bool b_has_more = false;
1597 DecoderWaitUnblock( p_dec, &b_reject );
1599 if( p_owner->b_buffering )
1601 vlc_mutex_unlock( &p_owner->lock );
1606 if( p_owner->buffer.p_block )
1608 p_sout_block = p_owner->buffer.p_block;
1610 p_owner->buffer.p_block = p_sout_block->p_next;
1611 p_owner->buffer.i_count--;
1613 b_has_more = p_owner->buffer.p_block != NULL;
1615 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1617 p_sout_block->p_next = NULL;
1619 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1620 &p_sout_block->i_length,
1621 &p_sout_block->i_rate, INT64_MAX, b_telx );
1623 vlc_mutex_unlock( &p_owner->lock );
1626 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1628 block_Release( p_sout_block );
1632 vlc_mutex_lock( &p_owner->lock );
1633 if( !p_owner->buffer.p_block )
1635 vlc_mutex_unlock( &p_owner->lock );
1642 static void DecoderFlushBuffering( decoder_t *p_dec )
1644 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1646 vlc_assert_locked( &p_owner->lock );
1648 while( p_owner->buffer.p_picture )
1650 picture_t *p_picture = p_owner->buffer.p_picture;
1652 p_owner->buffer.p_picture = p_picture->p_next;
1653 p_owner->buffer.i_count--;
1655 if( p_owner->p_vout )
1656 vout_DropPicture( p_owner->p_vout, p_picture );
1658 if( !p_owner->buffer.p_picture )
1659 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1661 while( p_owner->buffer.p_audio )
1663 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1665 p_owner->buffer.p_audio = p_audio->p_next;
1666 p_owner->buffer.i_count--;
1668 aout_BufferFree( p_audio );
1670 if( !p_owner->buffer.p_audio )
1671 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1673 while( p_owner->buffer.p_subpic )
1675 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1677 p_owner->buffer.p_subpic = p_subpic->p_next;
1678 p_owner->buffer.i_count--;
1680 subpicture_Delete( p_subpic );
1682 if( !p_owner->buffer.p_subpic )
1683 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1685 if( p_owner->buffer.p_block )
1687 block_ChainRelease( p_owner->buffer.p_block );
1689 p_owner->buffer.i_count = 0;
1690 p_owner->buffer.p_block = NULL;
1691 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1695 /* This function process a block for sout
1697 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1699 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1700 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1701 block_t *p_sout_block;
1703 while( ( p_sout_block =
1704 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1706 if( !p_owner->p_sout_input )
1708 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1710 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1711 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1712 if( p_dec->fmt_in.psz_language )
1714 if( p_owner->sout.psz_language )
1715 free( p_owner->sout.psz_language );
1716 p_owner->sout.psz_language =
1717 strdup( p_dec->fmt_in.psz_language );
1720 p_owner->p_sout_input =
1721 sout_InputNew( p_owner->p_sout,
1724 if( p_owner->p_sout_input == NULL )
1726 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1727 (char *)&p_owner->sout.i_codec );
1728 p_dec->b_error = true;
1730 while( p_sout_block )
1732 block_t *p_next = p_sout_block->p_next;
1733 block_Release( p_sout_block );
1734 p_sout_block = p_next;
1740 while( p_sout_block )
1742 block_t *p_next = p_sout_block->p_next;
1744 p_sout_block->p_next = NULL;
1746 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1748 p_sout_block = p_next;
1751 /* For now it's enough, as only sout impact on this flag */
1752 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1753 p_owner->p_input->p->b_out_pace_control )
1755 msg_Dbg( p_dec, "switching to sync mode" );
1756 p_owner->p_input->p->b_out_pace_control = false;
1758 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1759 !p_owner->p_input->p->b_out_pace_control )
1761 msg_Dbg( p_dec, "switching to async mode" );
1762 p_owner->p_input->p->b_out_pace_control = true;
1767 /* This function process a video block
1769 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1771 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1773 if( p_owner->p_packetizer )
1775 block_t *p_packetized_block;
1776 decoder_t *p_packetizer = p_owner->p_packetizer;
1778 while( (p_packetized_block =
1779 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1781 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1783 es_format_Clean( &p_dec->fmt_in );
1784 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1786 if( p_packetizer->pf_get_cc )
1787 DecoderGetCc( p_dec, p_packetizer );
1789 while( p_packetized_block )
1791 block_t *p_next = p_packetized_block->p_next;
1792 p_packetized_block->p_next = NULL;
1794 DecoderDecodeVideo( p_dec, p_packetized_block );
1796 p_packetized_block = p_next;
1802 DecoderDecodeVideo( p_dec, p_block );
1805 if( b_flush && p_owner->p_vout )
1806 vout_Flush( p_owner->p_vout, 1 );
1809 /* This function process a audio block
1811 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1813 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1815 if( p_owner->p_packetizer )
1817 block_t *p_packetized_block;
1818 decoder_t *p_packetizer = p_owner->p_packetizer;
1820 while( (p_packetized_block =
1821 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1823 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1825 es_format_Clean( &p_dec->fmt_in );
1826 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1829 while( p_packetized_block )
1831 block_t *p_next = p_packetized_block->p_next;
1832 p_packetized_block->p_next = NULL;
1834 DecoderDecodeAudio( p_dec, p_packetized_block );
1836 p_packetized_block = p_next;
1842 DecoderDecodeAudio( p_dec, p_block );
1845 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1846 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1849 /* This function process a subtitle block
1851 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1853 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1854 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1856 input_thread_t *p_input = p_owner->p_input;
1857 vout_thread_t *p_vout;
1858 subpicture_t *p_spu;
1860 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1862 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1863 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1864 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1866 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1867 if( p_vout && p_owner->p_spu_vout == p_vout )
1869 /* Preroll does not work very well with subtitle */
1870 if( p_spu->i_start > 0 &&
1871 p_spu->i_start < p_owner->i_preroll_end &&
1872 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1874 subpicture_Delete( p_spu );
1878 DecoderPlaySpu( p_dec, p_spu, b_telx );
1883 subpicture_Delete( p_spu );
1886 vlc_object_release( p_vout );
1889 if( b_flush && p_owner->p_spu_vout )
1891 p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1893 if( p_vout && p_owner->p_spu_vout == p_vout )
1894 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1895 p_owner->i_spu_channel );
1898 vlc_object_release( p_vout );
1903 static void DecoderProcessOnFlush( decoder_t *p_dec )
1905 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1907 vlc_mutex_lock( &p_owner->lock );
1908 DecoderFlushBuffering( p_dec );
1909 vlc_mutex_unlock( &p_owner->lock );
1911 DecoderSignalFlushed( p_dec );
1916 * \param p_dec the decoder object
1917 * \param p_block the block to decode
1918 * \return VLC_SUCCESS or an error code
1920 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1922 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1923 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1925 if( p_block && p_block->i_buffer <= 0 )
1927 assert( !b_flush_request );
1928 block_Release( p_block );
1933 if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
1936 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1938 DecoderProcessSout( p_dec, p_block );
1943 bool b_flush = false;
1947 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1948 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1950 b_flush = !b_flushing && b_flush_request;
1952 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1955 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1957 DecoderProcessAudio( p_dec, p_block, b_flush );
1959 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1961 DecoderProcessVideo( p_dec, p_block, b_flush );
1963 else if( p_dec->fmt_in.i_cat == SPU_ES )
1965 DecoderProcessSpu( p_dec, p_block, b_flush );
1969 msg_Err( p_dec, "unknown ES format" );
1970 p_dec->b_error = true;
1975 if( b_flush_request )
1976 DecoderProcessOnFlush( p_dec );
1979 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1981 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1985 block_Release( p_block );
1987 if( b_flush_request )
1988 DecoderProcessOnFlush( p_dec );
1993 * Destroys a decoder object
1995 * \param p_dec the decoder object
1998 static void DeleteDecoder( decoder_t * p_dec )
2000 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2002 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2003 (char*)&p_dec->fmt_in.i_codec,
2004 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2006 /* Free all packets still in the decoder fifo. */
2007 block_FifoEmpty( p_owner->p_fifo );
2008 block_FifoRelease( p_owner->p_fifo );
2011 vlc_mutex_lock( &p_owner->lock );
2012 DecoderFlushBuffering( p_dec );
2013 vlc_mutex_unlock( &p_owner->lock );
2016 if( p_owner->p_aout_input )
2017 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2018 if( p_owner->p_aout )
2020 input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2022 input_SendEventAout( p_owner->p_input );
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 * and that the vout is not paused anymore */
2029 vout_FixLeaks( p_owner->p_vout, true );
2030 if( p_owner->b_paused )
2031 vout_ChangePause( p_owner->p_vout, false, mdate() );
2034 input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL, true );
2035 input_SendEventVout( p_owner->p_input );
2039 if( p_owner->p_sout_input )
2041 sout_InputDelete( p_owner->p_sout_input );
2042 es_format_Clean( &p_owner->sout );
2046 if( p_dec->fmt_in.i_cat == SPU_ES )
2048 vout_thread_t *p_vout;
2050 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2053 if( p_owner->p_spu_vout == p_vout )
2054 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2055 vlc_object_release( p_vout );
2059 es_format_Clean( &p_dec->fmt_in );
2060 es_format_Clean( &p_dec->fmt_out );
2061 if( p_dec->p_description )
2062 vlc_meta_Delete( p_dec->p_description );
2063 es_format_Clean( &p_owner->fmt_description );
2064 if( p_owner->p_description )
2065 vlc_meta_Delete( p_owner->p_description );
2067 if( p_owner->p_packetizer )
2069 module_unneed( p_owner->p_packetizer,
2070 p_owner->p_packetizer->p_module );
2071 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2072 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2073 if( p_owner->p_packetizer->p_description )
2074 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2075 vlc_object_detach( p_owner->p_packetizer );
2076 vlc_object_release( p_owner->p_packetizer );
2079 vlc_cond_destroy( &p_owner->wait );
2080 vlc_mutex_destroy( &p_owner->lock );
2082 vlc_object_detach( p_dec );
2087 /*****************************************************************************
2088 * Buffers allocation callbacks for the decoders
2089 *****************************************************************************/
2090 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2092 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2094 vlc_assert_locked( &p_owner->lock );
2096 p_owner->b_fmt_description = true;
2098 /* Copy es_format */
2099 es_format_Clean( &p_owner->fmt_description );
2100 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2102 /* Move p_description */
2103 if( p_owner->p_description && p_dec->p_description )
2104 vlc_meta_Delete( p_owner->p_description );
2105 p_owner->p_description = p_dec->p_description;
2106 p_dec->p_description = NULL;
2108 static vout_thread_t *aout_request_vout( void *p_private,
2109 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2111 decoder_t *p_dec = p_private;
2112 input_thread_t *p_input = p_dec->p_owner->p_input;
2114 p_vout = input_ressource_RequestVout( p_input->p->p_ressource, p_vout, p_fmt, b_recyle );
2115 input_SendEventVout( p_input );
2120 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2122 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2123 aout_buffer_t *p_buffer;
2125 if( p_owner->p_aout_input != NULL &&
2126 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2127 p_dec->fmt_out.audio.i_original_channels !=
2128 p_owner->audio.i_original_channels ||
2129 p_dec->fmt_out.audio.i_bytes_per_frame !=
2130 p_owner->audio.i_bytes_per_frame ) )
2132 aout_input_t *p_aout_input = p_owner->p_aout_input;
2134 /* Parameters changed, restart the aout */
2135 vlc_mutex_lock( &p_owner->lock );
2137 DecoderFlushBuffering( p_dec );
2139 p_owner->p_aout_input = NULL;
2140 aout_DecDelete( p_owner->p_aout, p_aout_input );
2142 vlc_mutex_unlock( &p_owner->lock );
2145 if( p_owner->p_aout_input == NULL )
2147 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2148 audio_sample_format_t format;
2149 aout_input_t *p_aout_input;
2150 aout_instance_t *p_aout;
2151 aout_request_vout_t request_vout;
2153 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2154 p_owner->audio = p_dec->fmt_out.audio;
2156 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2157 if( i_force_dolby &&
2158 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2159 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2161 if( i_force_dolby == 1 )
2163 format.i_original_channels = format.i_original_channels |
2164 AOUT_CHAN_DOLBYSTEREO;
2166 else /* i_force_dolby == 2 */
2168 format.i_original_channels = format.i_original_channels &
2169 ~AOUT_CHAN_DOLBYSTEREO;
2173 request_vout.pf_request_vout = aout_request_vout;
2174 request_vout.p_private = p_dec;
2176 p_aout = p_owner->p_aout;
2178 p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2179 p_aout_input = aout_DecNew( p_dec, &p_aout,
2180 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2182 vlc_mutex_lock( &p_owner->lock );
2184 p_owner->p_aout = p_aout;
2185 p_owner->p_aout_input = p_aout_input;
2186 DecoderUpdateFormatLocked( p_dec );
2188 vlc_mutex_unlock( &p_owner->lock );
2190 input_SendEventAout( p_owner->p_input );
2192 if( p_owner->p_aout_input == NULL )
2194 msg_Err( p_dec, "failed to create audio output" );
2195 p_dec->b_error = true;
2198 p_dec->fmt_out.audio.i_bytes_per_frame =
2199 p_owner->audio.i_bytes_per_frame;
2202 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2207 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2209 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2211 aout_DecDeleteBuffer( p_owner->p_aout,
2212 p_owner->p_aout_input, p_buffer );
2216 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2218 static picture_t *vout_new_buffer( decoder_t *p_dec )
2220 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2222 if( p_owner->p_vout == NULL ||
2223 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2224 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2225 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2226 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2228 vout_thread_t *p_vout;
2230 if( !p_dec->fmt_out.video.i_width ||
2231 !p_dec->fmt_out.video.i_height )
2233 /* Can't create a new vout without display size */
2237 if( !p_dec->fmt_out.video.i_visible_width ||
2238 !p_dec->fmt_out.video.i_visible_height )
2240 if( p_dec->fmt_in.video.i_visible_width &&
2241 p_dec->fmt_in.video.i_visible_height )
2243 p_dec->fmt_out.video.i_visible_width =
2244 p_dec->fmt_in.video.i_visible_width;
2245 p_dec->fmt_out.video.i_visible_height =
2246 p_dec->fmt_in.video.i_visible_height;
2250 p_dec->fmt_out.video.i_visible_width =
2251 p_dec->fmt_out.video.i_width;
2252 p_dec->fmt_out.video.i_visible_height =
2253 p_dec->fmt_out.video.i_height;
2257 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2258 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2260 p_dec->fmt_out.video.i_visible_height = 1080;
2261 p_dec->fmt_out.video.i_sar_num *= 135;
2262 p_dec->fmt_out.video.i_sar_den *= 136;
2263 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2266 if( !p_dec->fmt_out.video.i_sar_num ||
2267 !p_dec->fmt_out.video.i_sar_den )
2269 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2270 p_dec->fmt_out.video.i_visible_height;
2272 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2273 p_dec->fmt_out.video.i_visible_width;
2276 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2277 &p_dec->fmt_out.video.i_sar_den,
2278 p_dec->fmt_out.video.i_sar_num,
2279 p_dec->fmt_out.video.i_sar_den, 50000 );
2281 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2282 p_owner->video = p_dec->fmt_out.video;
2284 vlc_mutex_lock( &p_owner->lock );
2286 DecoderFlushBuffering( p_dec );
2288 p_vout = p_owner->p_vout;
2289 p_owner->p_vout = NULL;
2290 vlc_mutex_unlock( &p_owner->lock );
2292 p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
2293 p_vout, &p_dec->fmt_out.video, true );
2295 vlc_mutex_lock( &p_owner->lock );
2296 p_owner->p_vout = p_vout;
2298 DecoderUpdateFormatLocked( p_dec );
2300 vlc_mutex_unlock( &p_owner->lock );
2302 input_SendEventVout( p_owner->p_input );
2303 if( p_vout == NULL )
2305 msg_Err( p_dec, "failed to create video output" );
2306 p_dec->b_error = true;
2310 if( p_owner->video.i_rmask )
2311 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2312 if( p_owner->video.i_gmask )
2313 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2314 if( p_owner->video.i_bmask )
2315 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2318 /* Get a new picture
2322 picture_t *p_picture;
2324 if( p_dec->b_die || p_dec->b_error )
2327 /* The video filter chain required that there is always 1 free buffer
2328 * that it will use as temporary one. It will release the temporary
2329 * buffer once its work is done, so this check is safe even if we don't
2330 * lock around both count() and create().
2332 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2334 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2339 if( DecoderIsFlushing( p_dec ) )
2343 DecoderSignalBuffering( p_dec, true );
2345 /* Check the decoder doesn't leak pictures */
2346 vout_FixLeaks( p_owner->p_vout, false );
2348 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2349 msleep( VOUT_OUTMEM_SLEEP );
2353 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2355 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2358 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2360 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2363 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2365 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2368 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2370 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2371 vout_thread_t *p_vout = NULL;
2372 subpicture_t *p_subpic;
2373 int i_attempts = 30;
2375 while( i_attempts-- )
2377 if( p_dec->b_die || p_dec->b_error )
2380 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2384 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2389 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2393 if( p_owner->p_spu_vout != p_vout )
2395 vlc_mutex_lock( &p_owner->lock );
2397 DecoderFlushBuffering( p_dec );
2399 vlc_mutex_unlock( &p_owner->lock );
2401 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2402 &p_owner->i_spu_channel );
2403 p_owner->i_spu_order = 0;
2404 p_owner->p_spu_vout = p_vout;
2407 p_subpic = subpicture_New();
2410 p_subpic->i_channel = p_owner->i_spu_channel;
2411 p_subpic->i_order = p_owner->i_spu_order++;
2412 p_subpic->b_subtitle = true;
2415 vlc_object_release( p_vout );
2420 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2422 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2423 vout_thread_t *p_vout = NULL;
2425 p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2426 if( !p_vout || p_owner->p_spu_vout != p_vout )
2429 vlc_object_release( p_vout );
2430 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2434 subpicture_Delete( p_subpic );
2436 vlc_object_release( p_vout );