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"
50 #include "../video_output/vout_control.h"
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void DeleteDecoder( decoder_t * );
55 static void *DecoderThread( vlc_object_t * );
56 static int DecoderProcess( decoder_t *, block_t * );
57 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
58 static void DecoderFlush( decoder_t * );
59 static void DecoderSignalBuffering( decoder_t *, bool );
60 static void DecoderFlushBuffering( decoder_t * );
62 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
64 /* Buffers allocation callbacks for the decoders */
65 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
66 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
68 static picture_t *vout_new_buffer( decoder_t * );
69 static void vout_del_buffer( decoder_t *, picture_t * );
70 static void vout_link_picture( decoder_t *, picture_t * );
71 static void vout_unlink_picture( decoder_t *, picture_t * );
73 static subpicture_t *spu_new_buffer( decoder_t * );
74 static void spu_del_buffer( decoder_t *, subpicture_t * );
76 static es_format_t null_es_format;
78 struct decoder_owner_sys_t
80 int64_t i_preroll_end;
82 input_thread_t *p_input;
83 input_clock_t *p_clock;
86 vout_thread_t *p_spu_vout;
90 sout_instance_t *p_sout;
91 sout_packetizer_input_t *p_sout_input;
93 /* Some decoders require already packetized data (ie. not truncated) */
94 decoder_t *p_packetizer;
96 /* Current format in use by the output */
102 block_fifo_t *p_fifo;
104 /* Lock for communication with decoder thread */
108 /* -- These variables need locking on write(only) -- */
109 aout_instance_t *p_aout;
110 aout_input_t *p_aout_input;
112 vout_thread_t *p_vout;
114 /* -- Theses variables need locking on read *and* write -- */
132 picture_t *p_picture;
133 picture_t **pp_picture_next;
135 subpicture_t *p_subpic;
136 subpicture_t **pp_subpic_next;
138 aout_buffer_t *p_audio;
139 aout_buffer_t **pp_audio_next;
150 decoder_t *pp_decoder[4];
157 #define DECODER_MAX_BUFFERING_COUNT (4)
158 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
159 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
161 /*****************************************************************************
163 *****************************************************************************/
164 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
166 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
168 msg_Warn( p_decoder, "can't get output subpicture" );
171 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
173 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
176 /* decoder_GetInputAttachments:
178 int decoder_GetInputAttachments( decoder_t *p_dec,
179 input_attachment_t ***ppp_attachment,
182 if( !p_dec->pf_get_attachments )
185 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
187 /* decoder_GetDisplayDate:
189 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
191 if( !p_dec->pf_get_display_date )
194 return p_dec->pf_get_display_date( p_dec, i_ts );
196 /* decoder_GetDisplayRate:
198 int decoder_GetDisplayRate( decoder_t *p_dec )
200 if( !p_dec->pf_get_display_rate )
201 return INPUT_RATE_DEFAULT;
203 return p_dec->pf_get_display_rate( p_dec );
207 * Spawns a new decoder thread
209 * \param p_input the input thread
210 * \param p_es the es descriptor
211 * \return the spawned decoder object
213 decoder_t *input_DecoderNew( input_thread_t *p_input,
214 es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout )
216 decoder_t *p_dec = NULL;
220 /* If we are in sout mode, search for packetizer module */
223 /* Create the decoder configuration structure */
224 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
227 msg_Err( p_input, "could not create packetizer" );
228 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
229 _("VLC could not open the packetizer module.") );
236 /* Create the decoder configuration structure */
237 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
240 msg_Err( p_input, "could not create decoder" );
241 intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
242 _("VLC could not open the decoder module.") );
247 if( !p_dec->p_module )
249 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
251 DeleteDecoder( p_dec );
252 vlc_object_release( p_dec );
256 p_dec->p_owner->p_clock = p_clock;
258 if( fmt->i_cat == AUDIO_ES )
259 i_priority = VLC_THREAD_PRIORITY_AUDIO;
261 i_priority = VLC_THREAD_PRIORITY_VIDEO;
263 /* Spawn the decoder thread */
264 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
265 i_priority, false ) )
267 msg_Err( p_dec, "cannot spawn decoder thread" );
268 module_unneed( p_dec, p_dec->p_module );
269 DeleteDecoder( p_dec );
270 vlc_object_release( p_dec );
278 * Kills a decoder thread and waits until it's finished
280 * \param p_input the input thread
281 * \param p_es the es descriptor
284 void input_DecoderDelete( decoder_t *p_dec )
286 decoder_owner_sys_t *p_owner = p_dec->p_owner;
288 vlc_object_kill( p_dec );
290 /* Make sure we aren't paused anymore */
291 vlc_mutex_lock( &p_owner->lock );
292 if( p_owner->b_paused || p_owner->b_buffering )
294 p_owner->b_paused = false;
295 p_owner->b_buffering = false;
296 vlc_cond_signal( &p_owner->wait );
298 vlc_mutex_unlock( &p_owner->lock );
300 vlc_thread_join( p_dec );
301 module_unneed( p_dec, p_dec->p_module );
304 if( p_dec->p_owner->cc.b_supported )
307 for( i = 0; i < 4; i++ )
308 input_DecoderSetCcState( p_dec, false, i );
311 /* Delete decoder configuration */
312 DeleteDecoder( p_dec );
314 /* Delete the decoder */
315 vlc_object_release( p_dec );
319 * Put a block_t in the decoder's fifo.
320 * Thread-safe w.r.t. the decoder. May be a cancellation point.
322 * \param p_dec the decoder object
323 * \param p_block the data block
325 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
327 decoder_owner_sys_t *p_owner = p_dec->p_owner;
329 if( p_owner->p_input->p->b_out_pace_control )
331 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
333 else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
335 /* FIXME: ideally we would check the time amount of data
336 * in the FIFO instead of its size. */
337 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
338 "consumed quickly enough), resetting fifo!" );
339 block_FifoEmpty( p_owner->p_fifo );
342 block_FifoPut( p_owner->p_fifo, p_block );
345 bool input_DecoderIsEmpty( decoder_t * p_dec )
347 assert( !p_dec->p_owner->b_buffering );
349 /* FIXME that's not really true */
350 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
353 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
355 decoder_owner_sys_t *p_owner = p_dec->p_owner;
358 vlc_mutex_lock( &p_owner->lock );
359 for( i = 0; i < 4; i++ )
360 pb_present[i] = p_owner->cc.pb_present[i];
361 vlc_mutex_unlock( &p_owner->lock );
363 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
365 decoder_owner_sys_t *p_owner = p_dec->p_owner;
367 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
369 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
374 static const vlc_fourcc_t fcc[4] = {
375 VLC_FOURCC('c', 'c', '1', ' '),
376 VLC_FOURCC('c', 'c', '2', ' '),
377 VLC_FOURCC('c', 'c', '3', ' '),
378 VLC_FOURCC('c', 'c', '4', ' '),
383 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
384 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
387 msg_Err( p_dec, "could not create decoder" );
388 intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
389 _("VLC could not open the decoder module.") );
392 else if( !p_cc->p_module )
394 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
395 DeleteDecoder( p_cc );
396 vlc_object_release( p_cc );
399 p_cc->p_owner->p_clock = p_owner->p_clock;
401 vlc_mutex_lock( &p_owner->lock );
402 p_owner->cc.pp_decoder[i_channel] = p_cc;
403 vlc_mutex_unlock( &p_owner->lock );
409 vlc_mutex_lock( &p_owner->lock );
410 p_cc = p_owner->cc.pp_decoder[i_channel];
411 p_owner->cc.pp_decoder[i_channel] = NULL;
412 vlc_mutex_unlock( &p_owner->lock );
416 vlc_object_kill( p_cc );
417 module_unneed( p_cc, p_cc->p_module );
418 DeleteDecoder( p_cc );
419 vlc_object_release( p_cc );
424 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
426 decoder_owner_sys_t *p_owner = p_dec->p_owner;
429 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
432 vlc_mutex_lock( &p_owner->lock );
433 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
434 vlc_mutex_unlock( &p_owner->lock );
438 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
440 decoder_owner_sys_t *p_owner = p_dec->p_owner;
442 vlc_mutex_lock( &p_owner->lock );
444 assert( !p_owner->b_paused || !b_paused );
446 p_owner->b_paused = b_paused;
447 p_owner->pause.i_date = i_date;
448 p_owner->pause.i_ignore = 0;
449 vlc_cond_signal( &p_owner->wait );
451 DecoderOutputChangePause( p_dec, b_paused, i_date );
453 vlc_mutex_unlock( &p_owner->lock );
456 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
458 decoder_owner_sys_t *p_owner = p_dec->p_owner;
460 vlc_mutex_lock( &p_owner->lock );
461 p_owner->i_ts_delay = i_delay;
462 vlc_mutex_unlock( &p_owner->lock );
465 void input_DecoderStartBuffering( decoder_t *p_dec )
467 decoder_owner_sys_t *p_owner = p_dec->p_owner;
469 vlc_mutex_lock( &p_owner->lock );
471 DecoderFlush( p_dec );
473 p_owner->buffer.b_first = true;
474 p_owner->buffer.b_full = false;
475 p_owner->buffer.i_count = 0;
477 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic && !p_owner->buffer.p_audio );
479 p_owner->buffer.p_picture = NULL;
480 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
482 p_owner->buffer.p_subpic = NULL;
483 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
485 p_owner->buffer.p_audio = NULL;
486 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
488 p_owner->b_buffering = true;
490 vlc_cond_signal( &p_owner->wait );
492 vlc_mutex_unlock( &p_owner->lock );
495 void input_DecoderStopBuffering( decoder_t *p_dec )
497 decoder_owner_sys_t *p_owner = p_dec->p_owner;
499 vlc_mutex_lock( &p_owner->lock );
501 p_owner->b_buffering = false;
503 vlc_cond_signal( &p_owner->wait );
505 vlc_mutex_unlock( &p_owner->lock );
508 void input_DecoderWaitBuffering( decoder_t *p_dec )
510 decoder_owner_sys_t *p_owner = p_dec->p_owner;
512 vlc_mutex_lock( &p_owner->lock );
514 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
516 block_FifoWake( p_owner->p_fifo );
517 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
520 vlc_mutex_unlock( &p_owner->lock );
522 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
524 decoder_owner_sys_t *p_owner = p_dec->p_owner;
528 vlc_mutex_lock( &p_owner->lock );
529 if( p_dec->fmt_in.i_cat == VIDEO_ES )
531 if( p_owner->b_paused && p_owner->p_vout )
533 vout_NextPicture( p_owner->p_vout, pi_duration );
534 p_owner->pause.i_ignore++;
535 vlc_cond_signal( &p_owner->wait );
540 /* TODO subtitle should not be flushed */
541 DecoderFlush( p_dec );
543 vlc_mutex_unlock( &p_owner->lock );
546 /*****************************************************************************
548 *****************************************************************************/
549 static int DecoderGetInputAttachments( decoder_t *p_dec,
550 input_attachment_t ***ppp_attachment,
553 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
554 ppp_attachment, pi_attachment );
556 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
558 decoder_owner_sys_t *p_owner = p_dec->p_owner;
560 vlc_mutex_lock( &p_owner->lock );
561 if( p_owner->b_buffering || p_owner->b_paused )
563 vlc_mutex_unlock( &p_owner->lock );
565 if( !p_owner->p_clock || !i_ts )
568 return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
570 static int DecoderGetDisplayRate( decoder_t *p_dec )
572 decoder_owner_sys_t *p_owner = p_dec->p_owner;
574 if( !p_owner->p_clock )
575 return INPUT_RATE_DEFAULT;
576 return input_clock_GetRate( p_owner->p_clock );
580 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
582 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
583 "VLC probably does not support this sound or video format.",
585 intf_UserFatal( p_dec, false, _("No suitable decoder module"),
586 _("VLC does not support the audio or video format \"%4.4s\". "
587 "Unfortunately there is no way for you to fix this."), (char*)&codec );
592 * Create a decoder object
594 * \param p_input the input thread
595 * \param p_es the es descriptor
596 * \param i_object_type Object type as define in include/vlc_objects.h
597 * \return the decoder object
599 static decoder_t * CreateDecoder( input_thread_t *p_input,
600 es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
603 decoder_owner_sys_t *p_owner;
606 p_dec = vlc_object_create( p_input, i_object_type );
610 p_dec->pf_decode_audio = NULL;
611 p_dec->pf_decode_video = NULL;
612 p_dec->pf_decode_sub = NULL;
613 p_dec->pf_get_cc = NULL;
614 p_dec->pf_packetize = NULL;
616 /* Initialize the decoder fifo */
617 p_dec->p_module = NULL;
619 memset( &null_es_format, 0, sizeof(es_format_t) );
620 es_format_Copy( &p_dec->fmt_in, fmt );
621 es_format_Copy( &p_dec->fmt_out, &null_es_format );
623 /* Allocate our private structure for the decoder */
624 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
625 if( p_dec->p_owner == NULL )
627 vlc_object_release( p_dec );
630 p_dec->p_owner->i_preroll_end = -1;
631 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
632 p_dec->p_owner->p_input = p_input;
633 p_dec->p_owner->p_aout = NULL;
634 p_dec->p_owner->p_aout_input = NULL;
635 p_dec->p_owner->p_vout = NULL;
636 p_dec->p_owner->p_spu_vout = NULL;
637 p_dec->p_owner->i_spu_channel = 0;
638 p_dec->p_owner->i_spu_order = 0;
639 p_dec->p_owner->p_sout = p_sout;
640 p_dec->p_owner->p_sout_input = NULL;
641 p_dec->p_owner->p_packetizer = NULL;
644 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
646 free( p_dec->p_owner );
647 vlc_object_release( p_dec );
651 /* Set buffers allocation callbacks for the decoders */
652 p_dec->pf_aout_buffer_new = aout_new_buffer;
653 p_dec->pf_aout_buffer_del = aout_del_buffer;
654 p_dec->pf_vout_buffer_new = vout_new_buffer;
655 p_dec->pf_vout_buffer_del = vout_del_buffer;
656 p_dec->pf_picture_link = vout_link_picture;
657 p_dec->pf_picture_unlink = vout_unlink_picture;
658 p_dec->pf_spu_buffer_new = spu_new_buffer;
659 p_dec->pf_spu_buffer_del = spu_del_buffer;
661 p_dec->pf_get_attachments = DecoderGetInputAttachments;
662 p_dec->pf_get_display_date = DecoderGetDisplayDate;
663 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
665 vlc_object_attach( p_dec, p_input );
667 /* Find a suitable decoder/packetizer module */
668 if( i_object_type == VLC_OBJECT_DECODER )
669 p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
671 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
673 /* Check if decoder requires already packetized data */
674 if( i_object_type == VLC_OBJECT_DECODER &&
675 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
677 p_dec->p_owner->p_packetizer =
678 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
679 if( p_dec->p_owner->p_packetizer )
681 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
684 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
687 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
689 p_dec->p_owner->p_packetizer->p_module =
690 module_need( p_dec->p_owner->p_packetizer,
691 "packetizer", "$packetizer", 0 );
693 if( !p_dec->p_owner->p_packetizer->p_module )
695 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
696 vlc_object_detach( p_dec->p_owner->p_packetizer );
697 vlc_object_release( p_dec->p_owner->p_packetizer );
702 /* Copy ourself the input replay gain */
703 if( fmt->i_cat == AUDIO_ES )
705 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
707 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
709 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
710 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
712 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
714 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
715 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
721 vlc_mutex_init( &p_owner->lock );
722 vlc_cond_init( &p_owner->wait );
724 p_owner->b_paused = false;
725 p_owner->pause.i_date = 0;
726 p_owner->pause.i_ignore = 0;
728 p_owner->b_buffering = false;
729 p_owner->buffer.b_first = true;
730 p_owner->buffer.b_full = false;
731 p_owner->buffer.i_count = 0;
732 p_owner->buffer.p_picture = NULL;
733 p_owner->buffer.p_subpic = NULL;
734 p_owner->buffer.p_audio = NULL;
736 p_owner->b_flushing = false;
739 p_owner->cc.b_supported = false;
740 if( i_object_type == VLC_OBJECT_DECODER )
742 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
743 p_owner->cc.b_supported = true;
744 if( p_dec->pf_get_cc )
745 p_owner->cc.b_supported = true;
748 for( i = 0; i < 4; i++ )
750 p_owner->cc.pb_present[i] = false;
751 p_owner->cc.pp_decoder[i] = NULL;
753 p_owner->i_ts_delay = 0;
758 * The decoding main loop
760 * \param p_dec the decoder
762 static void *DecoderThread( vlc_object_t *p_this )
764 decoder_t *p_dec = (decoder_t *)p_this;
765 decoder_owner_sys_t *p_owner = p_dec->p_owner;
767 /* The decoder's main loop */
770 block_t *p_block = block_FifoGet( p_owner->p_fifo );
771 /* Make sure there is no cancellation point other than this one^^.
772 * If you need one, be sure to push cleanup of p_block. */
773 DecoderSignalBuffering( p_dec, p_block == NULL );
778 { /* Trash all received PES packets */
779 block_Release( p_block );
781 else if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
788 DecoderSignalBuffering( p_dec, true );
792 static void DecoderFlush( decoder_t *p_dec )
794 decoder_owner_sys_t *p_owner = p_dec->p_owner;
797 vlc_assert_locked( &p_owner->lock );
800 block_FifoEmpty( p_owner->p_fifo );
802 /* Monitor for flush end */
803 p_owner->b_flushing = true;
804 vlc_cond_signal( &p_owner->wait );
806 /* Send a special block */
807 p_null = block_New( p_dec, 128 );
810 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
811 p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
812 if( !p_dec->fmt_in.b_packetized )
813 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
814 memset( p_null->p_buffer, 0, p_null->i_buffer );
816 input_DecoderDecode( p_dec, p_null );
819 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
820 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
823 static void DecoderSignalFlushed( decoder_t *p_dec )
825 decoder_owner_sys_t *p_owner = p_dec->p_owner;
827 vlc_mutex_lock( &p_owner->lock );
829 if( p_owner->b_flushing )
831 p_owner->b_flushing = false;
832 vlc_cond_signal( &p_owner->wait );
835 vlc_mutex_unlock( &p_owner->lock );
838 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
840 decoder_owner_sys_t *p_owner = p_dec->p_owner;
842 vlc_mutex_lock( &p_owner->lock );
844 if( p_owner->b_buffering )
847 p_owner->buffer.b_full = true;
848 vlc_cond_signal( &p_owner->wait );
851 vlc_mutex_unlock( &p_owner->lock );
854 static bool DecoderIsFlushing( decoder_t *p_dec )
856 decoder_owner_sys_t *p_owner = p_dec->p_owner;
859 vlc_mutex_lock( &p_owner->lock );
861 b_flushing = p_owner->b_flushing;
863 vlc_mutex_unlock( &p_owner->lock );
868 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
870 decoder_owner_sys_t *p_owner = p_dec->p_owner;
872 vlc_assert_locked( &p_owner->lock );
874 while( !p_owner->b_flushing )
876 if( p_owner->b_paused )
878 if( p_owner->b_buffering && !p_owner->buffer.b_full )
880 if( p_owner->pause.i_ignore > 0 )
882 p_owner->pause.i_ignore--;
888 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
891 vlc_cond_wait( &p_owner->wait, &p_owner->lock );
895 *pb_reject = p_owner->b_flushing;
898 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
900 decoder_owner_sys_t *p_owner = p_dec->p_owner;
902 vlc_assert_locked( &p_owner->lock );
904 /* XXX only audio and video output have to be paused.
905 * - for sout it is useless
906 * - for subs, it is done by the vout
908 if( p_dec->fmt_in.i_cat == AUDIO_ES )
910 if( p_owner->p_aout && p_owner->p_aout_input )
911 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
914 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
916 if( p_owner->p_vout )
917 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
920 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
922 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
923 *pi_preroll = INT64_MAX;
924 else if( p->i_pts > 0 )
925 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
926 else if( p->i_dts > 0 )
927 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
930 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
932 mtime_t current_date = mdate();
934 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
935 if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
937 /* ETSI EN 300 472 Annex A : do not take into account the PTS
938 * for teletext streams. */
939 return current_date + 400000 + i_ts_delay;
944 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
945 mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
947 decoder_owner_sys_t *p_owner = p_dec->p_owner;
948 input_clock_t *p_clock = p_owner->p_clock;
951 vlc_assert_locked( &p_owner->lock );
953 const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
954 const mtime_t i_es_delay = p_owner->i_ts_delay;
958 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
961 *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
962 *pi_ts0 + i_es_delay );
963 if( pi_ts1 && *pi_ts1 > 0 )
964 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
965 *pi_ts1 + i_es_delay );
967 /* Do not create ephemere data because of rounding errors */
968 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
972 i_rate = input_clock_GetRate( p_clock );
975 *pi_duration = ( *pi_duration * i_rate +
976 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
983 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
984 if( pi_ts1 && *pi_ts1 <= 0 )
990 const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
991 *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
995 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
996 int *pi_played_sum, int *pi_lost_sum )
998 decoder_owner_sys_t *p_owner = p_dec->p_owner;
999 aout_instance_t *p_aout = p_owner->p_aout;
1000 aout_input_t *p_aout_input = p_owner->p_aout_input;
1003 if( p_audio->start_date <= 0 )
1005 msg_Warn( p_dec, "non-dated audio buffer received" );
1007 aout_BufferFree( p_audio );
1012 vlc_mutex_lock( &p_owner->lock );
1014 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1016 p_audio->p_next = NULL;
1018 *p_owner->buffer.pp_audio_next = p_audio;
1019 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1021 p_owner->buffer.i_count++;
1022 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1023 p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1025 p_owner->buffer.b_full = true;
1026 vlc_cond_signal( &p_owner->wait );
1032 bool b_has_more = false;
1034 DecoderWaitUnblock( p_dec, &b_reject );
1036 if( p_owner->b_buffering )
1038 vlc_mutex_unlock( &p_owner->lock );
1043 if( p_owner->buffer.p_audio )
1045 p_audio = p_owner->buffer.p_audio;
1047 p_owner->buffer.p_audio = p_audio->p_next;
1048 p_owner->buffer.i_count--;
1050 b_has_more = p_owner->buffer.p_audio != NULL;
1052 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1056 int i_rate = INPUT_RATE_DEFAULT;
1059 DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1060 &i_rate, &i_delay, false );
1062 vlc_mutex_unlock( &p_owner->lock );
1065 const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1067 if( !p_aout || !p_aout_input ||
1068 p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1069 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1070 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1075 /* Wait if we are too early
1076 * FIXME that's plain ugly to do it here */
1077 mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1079 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1080 *pi_played_sum += 1;
1081 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1085 if( p_audio->start_date <= 0 )
1087 msg_Warn( p_dec, "non-dated audio buffer received" );
1089 else if( p_audio->start_date > i_max_date )
1091 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1092 p_audio->start_date - mdate() );
1095 aout_BufferFree( p_audio );
1101 vlc_mutex_lock( &p_owner->lock );
1102 if( !p_owner->buffer.p_audio )
1104 vlc_mutex_unlock( &p_owner->lock );
1110 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1112 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1113 input_thread_t *p_input = p_owner->p_input;
1114 aout_buffer_t *p_aout_buf;
1119 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1121 aout_instance_t *p_aout = p_owner->p_aout;
1122 aout_input_t *p_aout_input = p_owner->p_aout_input;
1128 /* It prevent freezing VLC in case of broken decoder */
1129 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1131 block_Release( p_block );
1136 if( p_aout_buf->start_date < p_owner->i_preroll_end )
1138 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1142 if( p_owner->i_preroll_end > 0 )
1144 msg_Dbg( p_dec, "End of audio preroll" );
1145 if( p_owner->p_aout && p_owner->p_aout_input )
1146 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1148 p_owner->i_preroll_end = -1;
1151 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1154 /* Update ugly stat */
1155 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1157 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1159 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1161 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1163 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1166 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1169 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1171 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1177 assert( p_dec_cc->pf_get_cc != NULL );
1179 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1180 if( !p_owner->cc.b_supported )
1183 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1187 vlc_mutex_lock( &p_owner->lock );
1188 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1190 p_owner->cc.pb_present[i] |= pb_present[i];
1191 if( p_owner->cc.pp_decoder[i] )
1195 for( i = 0; i < 4; i++ )
1197 if( !p_owner->cc.pp_decoder[i] )
1200 if( i_cc_decoder > 1 )
1201 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1203 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1206 vlc_mutex_unlock( &p_owner->lock );
1209 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1210 int *pi_played_sum, int *pi_lost_sum )
1212 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1213 vout_thread_t *p_vout = p_owner->p_vout;
1214 bool b_first_buffered;
1216 if( p_picture->date <= 0 )
1218 msg_Warn( p_vout, "non-dated video buffer received" );
1220 vout_DropPicture( p_vout, p_picture );
1225 vlc_mutex_lock( &p_owner->lock );
1227 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1229 p_picture->p_next = NULL;
1231 *p_owner->buffer.pp_picture_next = p_picture;
1232 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1234 p_owner->buffer.i_count++;
1235 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1236 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1238 p_owner->buffer.b_full = true;
1239 vlc_cond_signal( &p_owner->wait );
1242 b_first_buffered = p_owner->buffer.p_picture != NULL;
1244 for( ;; b_first_buffered = false )
1246 bool b_has_more = false;
1250 DecoderWaitUnblock( p_dec, &b_reject );
1252 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1254 vlc_mutex_unlock( &p_owner->lock );
1257 bool b_buffering_first = p_owner->b_buffering;
1260 if( p_owner->buffer.p_picture )
1262 p_picture = p_owner->buffer.p_picture;
1264 p_owner->buffer.p_picture = p_picture->p_next;
1265 p_owner->buffer.i_count--;
1267 b_has_more = p_owner->buffer.p_picture != NULL;
1269 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1273 int i_rate = INPUT_RATE_DEFAULT;
1276 if( b_buffering_first )
1278 assert( p_owner->buffer.b_first );
1279 assert( !p_owner->buffer.i_count );
1280 msg_Dbg( p_dec, "Received first picture" );
1281 p_owner->buffer.b_first = false;
1282 p_picture->b_force = true;
1284 if( p_owner->p_clock )
1285 i_rate = input_clock_GetRate( p_owner->p_clock );
1287 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1288 &i_rate, &i_delay, false );
1290 vlc_mutex_unlock( &p_owner->lock );
1293 const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1295 if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1300 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1302 /* Be sure to not display old picture after our own */
1303 vout_Flush( p_vout, p_picture->date );
1304 p_owner->i_last_rate = i_rate;
1306 vout_DisplayPicture( p_vout, p_picture );
1310 if( p_picture->date <= 0 )
1312 msg_Warn( p_vout, "non-dated video buffer received" );
1316 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1317 p_picture->date - mdate() );
1320 vout_DropPicture( p_vout, p_picture );
1324 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1326 *pi_played_sum += i_tmp_display;
1327 *pi_lost_sum += i_tmp_lost;
1329 if( !b_has_more || b_buffering_first )
1332 vlc_mutex_lock( &p_owner->lock );
1333 if( !p_owner->buffer.p_picture )
1335 vlc_mutex_unlock( &p_owner->lock );
1341 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1343 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1344 input_thread_t *p_input = p_owner->p_input;
1348 int i_displayed = 0;
1350 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1352 vout_thread_t *p_vout = p_owner->p_vout;
1355 /* It prevent freezing VLC in case of broken decoder */
1356 vout_DropPicture( p_vout, p_pic );
1358 block_Release( p_block );
1364 if( p_pic->date < p_owner->i_preroll_end )
1366 vout_DropPicture( p_vout, p_pic );
1370 if( p_owner->i_preroll_end > 0 )
1372 msg_Dbg( p_dec, "End of video preroll" );
1374 vout_Flush( p_vout, 1 );
1376 p_owner->i_preroll_end = -1;
1379 if( p_dec->pf_get_cc &&
1380 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1381 DecoderGetCc( p_dec, p_dec );
1383 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1385 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1387 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1389 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1391 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1394 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1397 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1401 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1404 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1405 vout_thread_t *p_vout = p_owner->p_spu_vout;
1408 if( p_subpic->i_start <= 0 )
1410 msg_Warn( p_dec, "non-dated spu buffer received" );
1411 subpicture_Delete( p_subpic );
1416 vlc_mutex_lock( &p_owner->lock );
1418 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1420 p_subpic->p_next = NULL;
1422 *p_owner->buffer.pp_subpic_next = p_subpic;
1423 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1425 p_owner->buffer.i_count++;
1426 /* XXX it is important to be full after the first one */
1427 if( p_owner->buffer.i_count > 0 )
1429 p_owner->buffer.b_full = true;
1430 vlc_cond_signal( &p_owner->wait );
1436 bool b_has_more = false;
1438 DecoderWaitUnblock( p_dec, &b_reject );
1440 if( p_owner->b_buffering )
1442 vlc_mutex_unlock( &p_owner->lock );
1447 if( p_owner->buffer.p_subpic )
1449 p_subpic = p_owner->buffer.p_subpic;
1451 p_owner->buffer.p_subpic = p_subpic->p_next;
1452 p_owner->buffer.i_count--;
1454 b_has_more = p_owner->buffer.p_subpic != NULL;
1456 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1460 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1461 NULL, NULL, b_telx );
1463 vlc_mutex_unlock( &p_owner->lock );
1466 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1468 subpicture_Delete( p_subpic );
1472 vlc_mutex_lock( &p_owner->lock );
1473 if( !p_owner->buffer.p_subpic )
1475 vlc_mutex_unlock( &p_owner->lock );
1481 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1484 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1486 assert( p_owner->p_clock );
1488 vlc_mutex_lock( &p_owner->lock );
1491 DecoderWaitUnblock( p_dec, &b_reject );
1493 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1494 &p_sout_block->i_rate, NULL, b_telx );
1496 vlc_mutex_unlock( &p_owner->lock );
1498 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1502 static void DecoderFlushBuffering( decoder_t *p_dec )
1504 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1506 vlc_assert_locked( &p_owner->lock );
1508 while( p_owner->buffer.p_picture )
1510 picture_t *p_picture = p_owner->buffer.p_picture;
1512 p_owner->buffer.p_picture = p_picture->p_next;
1513 p_owner->buffer.i_count--;
1515 if( p_owner->p_vout )
1516 vout_DropPicture( p_owner->p_vout, p_picture );
1518 if( !p_owner->buffer.p_picture )
1519 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1521 while( p_owner->buffer.p_audio )
1523 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1525 p_owner->buffer.p_audio = p_audio->p_next;
1526 p_owner->buffer.i_count--;
1528 aout_BufferFree( p_audio );
1530 if( !p_owner->buffer.p_audio )
1531 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1533 while( p_owner->buffer.p_subpic )
1535 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1537 p_owner->buffer.p_subpic = p_subpic->p_next;
1538 p_owner->buffer.i_count--;
1540 subpicture_Delete( p_subpic );
1542 if( !p_owner->buffer.p_subpic )
1543 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1547 /* This function process a block for sout
1549 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1551 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1552 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1553 block_t *p_sout_block;
1555 while( ( p_sout_block =
1556 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1558 if( !p_owner->p_sout_input )
1560 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1562 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1563 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1564 if( p_dec->fmt_in.psz_language )
1566 if( p_owner->sout.psz_language )
1567 free( p_owner->sout.psz_language );
1568 p_owner->sout.psz_language =
1569 strdup( p_dec->fmt_in.psz_language );
1572 p_owner->p_sout_input =
1573 sout_InputNew( p_owner->p_sout,
1576 if( p_owner->p_sout_input == NULL )
1578 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1579 (char *)&p_owner->sout.i_codec );
1580 p_dec->b_error = true;
1582 while( p_sout_block )
1584 block_t *p_next = p_sout_block->p_next;
1585 block_Release( p_sout_block );
1586 p_sout_block = p_next;
1592 while( p_sout_block )
1594 block_t *p_next = p_sout_block->p_next;
1596 p_sout_block->p_next = NULL;
1598 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1600 p_sout_block = p_next;
1603 /* For now it's enough, as only sout impact on this flag */
1604 if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1605 p_owner->p_input->p->b_out_pace_control )
1607 msg_Dbg( p_dec, "switching to sync mode" );
1608 p_owner->p_input->p->b_out_pace_control = false;
1610 else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1611 !p_owner->p_input->p->b_out_pace_control )
1613 msg_Dbg( p_dec, "switching to async mode" );
1614 p_owner->p_input->p->b_out_pace_control = true;
1619 /* This function process a video block
1621 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1623 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1625 if( p_owner->p_packetizer )
1627 block_t *p_packetized_block;
1628 decoder_t *p_packetizer = p_owner->p_packetizer;
1630 while( (p_packetized_block =
1631 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1633 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1635 es_format_Clean( &p_dec->fmt_in );
1636 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1638 if( p_packetizer->pf_get_cc )
1639 DecoderGetCc( p_dec, p_packetizer );
1641 while( p_packetized_block )
1643 block_t *p_next = p_packetized_block->p_next;
1644 p_packetized_block->p_next = NULL;
1646 DecoderDecodeVideo( p_dec, p_packetized_block );
1648 p_packetized_block = p_next;
1654 DecoderDecodeVideo( p_dec, p_block );
1657 if( b_flush && p_owner->p_vout )
1658 vout_Flush( p_owner->p_vout, 1 );
1661 /* This function process a audio block
1663 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1665 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1667 if( p_owner->p_packetizer )
1669 block_t *p_packetized_block;
1670 decoder_t *p_packetizer = p_owner->p_packetizer;
1672 while( (p_packetized_block =
1673 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1675 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1677 es_format_Clean( &p_dec->fmt_in );
1678 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1681 while( p_packetized_block )
1683 block_t *p_next = p_packetized_block->p_next;
1684 p_packetized_block->p_next = NULL;
1686 DecoderDecodeAudio( p_dec, p_packetized_block );
1688 p_packetized_block = p_next;
1694 DecoderDecodeAudio( p_dec, p_block );
1697 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1698 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1701 /* This function process a subtitle block
1703 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1705 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1706 const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1708 input_thread_t *p_input = p_owner->p_input;
1709 vout_thread_t *p_vout;
1710 subpicture_t *p_spu;
1712 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1714 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1715 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1716 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1718 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1719 if( p_vout && p_owner->p_spu_vout == p_vout )
1721 /* Preroll does not work very well with subtitle */
1722 if( p_spu->i_start > 0 &&
1723 p_spu->i_start < p_owner->i_preroll_end &&
1724 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1726 subpicture_Delete( p_spu );
1730 DecoderPlaySpu( p_dec, p_spu, b_telx );
1735 subpicture_Delete( p_spu );
1738 vlc_object_release( p_vout );
1741 if( b_flush && p_owner->p_spu_vout )
1743 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1745 if( p_vout && p_owner->p_spu_vout == p_vout )
1746 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1747 p_owner->i_spu_channel );
1750 vlc_object_release( p_vout );
1757 * \param p_dec the decoder object
1758 * \param p_block the block to decode
1759 * \return VLC_SUCCESS or an error code
1761 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1763 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1764 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1766 if( p_block && p_block->i_buffer <= 0 )
1768 assert( !b_flush_request );
1769 block_Release( p_block );
1773 int canc = vlc_savecancel ();
1775 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1778 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1780 DecoderProcessSout( p_dec, p_block );
1785 bool b_flush = false;
1789 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1790 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1792 b_flush = !b_flushing && b_flush_request;
1794 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1797 if( p_dec->fmt_in.i_cat == AUDIO_ES )
1799 DecoderProcessAudio( p_dec, p_block, b_flush );
1801 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1803 DecoderProcessVideo( p_dec, p_block, b_flush );
1805 else if( p_dec->fmt_in.i_cat == SPU_ES )
1807 DecoderProcessSpu( p_dec, p_block, b_flush );
1811 msg_Err( p_dec, "unknown ES format" );
1812 p_dec->b_error = true;
1817 if( b_flush_request )
1819 vlc_mutex_lock( &p_owner->lock );
1820 DecoderFlushBuffering( p_dec );
1821 vlc_mutex_unlock( &p_owner->lock );
1823 DecoderSignalFlushed( p_dec );
1825 vlc_restorecancel(canc);
1827 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1831 * Destroys a decoder object
1833 * \param p_dec the decoder object
1836 static void DeleteDecoder( decoder_t * p_dec )
1838 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1840 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1841 (char*)&p_dec->fmt_in.i_codec,
1842 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1844 /* Free all packets still in the decoder fifo. */
1845 block_FifoEmpty( p_owner->p_fifo );
1846 block_FifoRelease( p_owner->p_fifo );
1849 vlc_mutex_lock( &p_owner->lock );
1850 DecoderFlushBuffering( p_dec );
1851 vlc_mutex_unlock( &p_owner->lock );
1854 if( p_owner->p_aout_input )
1855 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1856 if( p_owner->p_aout )
1858 vlc_object_release( p_owner->p_aout );
1859 p_owner->p_aout = NULL;
1861 if( p_owner->p_vout )
1863 /* Hack to make sure all the the pictures are freed by the decoder */
1864 vout_FixLeaks( p_owner->p_vout, true );
1866 /* We are about to die. Reattach video output to p_vlc. */
1867 vout_Request( p_dec, p_owner->p_vout, NULL );
1868 var_SetBool( p_owner->p_input, "intf-change-vout", true );
1872 if( p_owner->p_sout_input )
1874 sout_InputDelete( p_owner->p_sout_input );
1875 es_format_Clean( &p_owner->sout );
1879 if( p_dec->fmt_in.i_cat == SPU_ES )
1881 vout_thread_t *p_vout;
1883 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1884 if( p_vout && p_owner->p_spu_vout == p_vout )
1886 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1887 p_owner->i_spu_channel );
1888 vlc_object_release( p_vout );
1892 es_format_Clean( &p_dec->fmt_in );
1893 es_format_Clean( &p_dec->fmt_out );
1895 if( p_owner->p_packetizer )
1897 module_unneed( p_owner->p_packetizer,
1898 p_owner->p_packetizer->p_module );
1899 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1900 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1901 vlc_object_detach( p_owner->p_packetizer );
1902 vlc_object_release( p_owner->p_packetizer );
1905 vlc_cond_destroy( &p_owner->wait );
1906 vlc_mutex_destroy( &p_owner->lock );
1908 vlc_object_detach( p_dec );
1913 /*****************************************************************************
1914 * Buffers allocation callbacks for the decoders
1915 *****************************************************************************/
1916 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1918 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1919 aout_buffer_t *p_buffer;
1921 if( p_owner->p_aout_input != NULL &&
1922 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1923 p_dec->fmt_out.audio.i_original_channels !=
1924 p_owner->audio.i_original_channels ||
1925 p_dec->fmt_out.audio.i_bytes_per_frame !=
1926 p_owner->audio.i_bytes_per_frame ) )
1928 aout_input_t *p_aout_input = p_owner->p_aout_input;
1930 /* Parameters changed, restart the aout */
1931 vlc_mutex_lock( &p_owner->lock );
1933 DecoderFlushBuffering( p_dec );
1935 p_owner->p_aout_input = NULL;
1936 aout_DecDelete( p_owner->p_aout, p_aout_input );
1938 vlc_mutex_unlock( &p_owner->lock );
1941 if( p_owner->p_aout_input == NULL )
1943 const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1944 audio_sample_format_t format;
1945 aout_input_t *p_aout_input;
1946 aout_instance_t *p_aout;
1948 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1949 p_owner->audio = p_dec->fmt_out.audio;
1951 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1952 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1953 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1955 if ( i_force_dolby == 1 )
1957 format.i_original_channels = format.i_original_channels |
1958 AOUT_CHAN_DOLBYSTEREO;
1960 else /* i_force_dolby == 2 */
1962 format.i_original_channels = format.i_original_channels &
1963 ~AOUT_CHAN_DOLBYSTEREO;
1967 p_aout = p_owner->p_aout;
1968 p_aout_input = aout_DecNew( p_dec, &p_aout,
1969 &format, &p_dec->fmt_out.audio_replay_gain );
1971 vlc_mutex_lock( &p_owner->lock );
1972 p_owner->p_aout = p_aout;
1973 p_owner->p_aout_input = p_aout_input;
1974 vlc_mutex_unlock( &p_owner->lock );
1976 if( p_owner->p_aout_input == NULL )
1978 msg_Err( p_dec, "failed to create audio output" );
1979 p_dec->b_error = true;
1982 p_dec->fmt_out.audio.i_bytes_per_frame =
1983 p_owner->audio.i_bytes_per_frame;
1986 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1991 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1993 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1995 aout_DecDeleteBuffer( p_owner->p_aout,
1996 p_owner->p_aout_input, p_buffer );
2000 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2002 static picture_t *vout_new_buffer( decoder_t *p_dec )
2004 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2006 if( p_owner->p_vout == NULL ||
2007 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2008 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2009 p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2010 p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2012 vout_thread_t *p_vout;
2014 if( !p_dec->fmt_out.video.i_width ||
2015 !p_dec->fmt_out.video.i_height )
2017 /* Can't create a new vout without display size */
2021 if( !p_dec->fmt_out.video.i_visible_width ||
2022 !p_dec->fmt_out.video.i_visible_height )
2024 if( p_dec->fmt_in.video.i_visible_width &&
2025 p_dec->fmt_in.video.i_visible_height )
2027 p_dec->fmt_out.video.i_visible_width =
2028 p_dec->fmt_in.video.i_visible_width;
2029 p_dec->fmt_out.video.i_visible_height =
2030 p_dec->fmt_in.video.i_visible_height;
2034 p_dec->fmt_out.video.i_visible_width =
2035 p_dec->fmt_out.video.i_width;
2036 p_dec->fmt_out.video.i_visible_height =
2037 p_dec->fmt_out.video.i_height;
2041 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2042 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2044 p_dec->fmt_out.video.i_visible_height = 1080;
2045 p_dec->fmt_out.video.i_sar_num *= 135;
2046 p_dec->fmt_out.video.i_sar_den *= 136;
2047 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2050 if( !p_dec->fmt_out.video.i_sar_num ||
2051 !p_dec->fmt_out.video.i_sar_den )
2053 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2054 p_dec->fmt_out.video.i_visible_height;
2056 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2057 p_dec->fmt_out.video.i_visible_width;
2060 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2061 &p_dec->fmt_out.video.i_sar_den,
2062 p_dec->fmt_out.video.i_sar_num,
2063 p_dec->fmt_out.video.i_sar_den, 50000 );
2065 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2066 p_owner->video = p_dec->fmt_out.video;
2068 vlc_mutex_lock( &p_owner->lock );
2070 DecoderFlushBuffering( p_dec );
2072 p_vout = p_owner->p_vout;
2073 p_owner->p_vout = NULL;
2074 vlc_mutex_unlock( &p_owner->lock );
2076 p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2078 vlc_mutex_lock( &p_owner->lock );
2079 p_owner->p_vout = p_vout;
2080 vlc_mutex_unlock( &p_owner->lock );
2082 var_SetBool( p_owner->p_input, "intf-change-vout", true );
2083 if( p_vout == NULL )
2085 msg_Err( p_dec, "failed to create video output" );
2086 p_dec->b_error = true;
2090 if( p_owner->video.i_rmask )
2091 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2092 if( p_owner->video.i_gmask )
2093 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2094 if( p_owner->video.i_bmask )
2095 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2098 /* Get a new picture
2102 picture_t *p_picture;
2104 if( p_dec->b_die || p_dec->b_error )
2107 /* The video filter chain required that there is always 1 free buffer
2108 * that it will use as temporary one. It will release the temporary
2109 * buffer once its work is done, so this check is safe even if we don't
2110 * lock around both count() and create().
2112 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2114 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2119 if( DecoderIsFlushing( p_dec ) )
2123 DecoderSignalBuffering( p_dec, true );
2125 /* Check the decoder doesn't leak pictures */
2126 vout_FixLeaks( p_owner->p_vout, false );
2128 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2129 msleep( VOUT_OUTMEM_SLEEP );
2133 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2135 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2138 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2140 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2143 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2145 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2148 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2150 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2151 vout_thread_t *p_vout = NULL;
2152 subpicture_t *p_subpic;
2153 int i_attempts = 30;
2155 while( i_attempts-- )
2157 if( p_dec->b_die || p_dec->b_error )
2160 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2164 msleep( VOUT_DISPLAY_DELAY );
2169 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2173 if( p_owner->p_spu_vout != p_vout )
2175 vlc_mutex_lock( &p_owner->lock );
2177 DecoderFlushBuffering( p_dec );
2179 vlc_mutex_unlock( &p_owner->lock );
2181 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2182 &p_owner->i_spu_channel );
2183 p_owner->i_spu_order = 0;
2184 p_owner->p_spu_vout = p_vout;
2187 p_subpic = subpicture_New();
2190 p_subpic->i_channel = p_owner->i_spu_channel;
2191 p_subpic->i_order = p_owner->i_spu_order++;
2192 p_subpic->b_subtitle = true;
2195 vlc_object_release( p_vout );
2200 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2202 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2203 vout_thread_t *p_vout = NULL;
2205 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2206 if( !p_vout || p_owner->p_spu_vout != p_vout )
2209 vlc_object_release( p_vout );
2210 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2214 subpicture_Delete( p_subpic );
2216 vlc_object_release( p_vout );