1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_block.h>
40 #include <vlc_codec.h>
43 #include <vlc_dialog.h>
44 #include <vlc_modules.h>
46 #include "audio_output/aout_internal.h"
47 #include "stream_output/stream_output.h"
48 #include "input_internal.h"
54 #include "../video_output/vout_control.h"
56 struct decoder_owner_sys_t
58 int64_t i_preroll_end;
60 input_thread_t *p_input;
61 input_resource_t*p_resource;
62 input_clock_t *p_clock;
65 vout_thread_t *p_spu_vout;
69 sout_instance_t *p_sout;
70 sout_packetizer_input_t *p_sout_input;
74 /* Some decoders require already packetized data (ie. not truncated) */
75 decoder_t *p_packetizer;
78 /* Current format in use by the output */
82 bool b_fmt_description;
83 vlc_meta_t *p_description;
88 /* Lock for communication with decoder thread */
90 vlc_cond_t wait_request;
91 vlc_cond_t wait_acknowledge;
93 /* -- These variables need locking on write(only) -- */
94 audio_output_t *p_aout;
96 vout_thread_t *p_vout;
98 /* -- Theses variables need locking on read *and* write -- */
121 decoder_t *pp_decoder[4];
128 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
129 * a bogus PTS and won't be displayed */
130 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
133 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
135 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
137 decoder_owner_sys_t *p_owner = p_dec->p_owner;
139 vlc_assert_locked( &p_owner->lock );
141 es_format_Clean( &p_owner->fmt );
142 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
144 /* Move p_description */
145 if( p_dec->p_description != NULL )
147 if( p_owner->p_description != NULL )
148 vlc_meta_Delete( p_owner->p_description );
149 p_owner->p_description = p_dec->p_description;
150 p_dec->p_description = NULL;
153 p_owner->b_fmt_description = true;
156 static bool DecoderIsFlushing( decoder_t *p_dec )
158 decoder_owner_sys_t *p_owner = p_dec->p_owner;
161 vlc_mutex_lock( &p_owner->lock );
163 b_flushing = p_owner->b_flushing;
165 vlc_mutex_unlock( &p_owner->lock );
170 static void DecoderSignalWait( decoder_t *p_dec )
172 decoder_owner_sys_t *p_owner = p_dec->p_owner;
174 vlc_mutex_lock( &p_owner->lock );
176 if( p_owner->b_waiting )
178 p_owner->b_has_data = true;
179 vlc_cond_signal( &p_owner->wait_acknowledge );
182 vlc_mutex_unlock( &p_owner->lock );
185 static block_t *DecoderBlockFlushNew()
187 block_t *p_null = block_Alloc( 128 );
191 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
192 BLOCK_FLAG_CORRUPTED |
193 BLOCK_FLAG_CORE_FLUSH;
194 memset( p_null->p_buffer, 0, p_null->i_buffer );
199 /*****************************************************************************
200 * Buffers allocation callbacks for the decoders
201 *****************************************************************************/
202 static vout_thread_t *aout_request_vout( void *p_private,
203 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
205 decoder_t *p_dec = p_private;
206 decoder_owner_sys_t *p_owner = p_dec->p_owner;
207 input_thread_t *p_input = p_owner->p_input;
209 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
211 if( p_input != NULL )
212 input_SendEventVout( p_input );
217 static int aout_update_format( decoder_t *p_dec )
219 decoder_owner_sys_t *p_owner = p_dec->p_owner;
222 && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) )
224 audio_output_t *p_aout = p_owner->p_aout;
226 /* Parameters changed, restart the aout */
227 vlc_mutex_lock( &p_owner->lock );
229 aout_DecDelete( p_owner->p_aout );
230 p_owner->p_aout = NULL;
232 vlc_mutex_unlock( &p_owner->lock );
233 input_resource_PutAout( p_owner->p_resource, p_aout );
236 if( p_owner->p_aout == NULL )
238 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
240 audio_sample_format_t format = p_dec->fmt_out.audio;
241 aout_FormatPrepare( &format );
243 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
245 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
246 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
248 if( i_force_dolby == 1 )
250 format.i_original_channels = format.i_original_channels |
251 AOUT_CHAN_DOLBYSTEREO;
253 else /* i_force_dolby == 2 */
255 format.i_original_channels = format.i_original_channels &
256 ~AOUT_CHAN_DOLBYSTEREO;
260 aout_request_vout_t request_vout = {
261 .pf_request_vout = aout_request_vout,
264 audio_output_t *p_aout;
266 p_aout = input_resource_GetAout( p_owner->p_resource );
269 if( aout_DecNew( p_aout, &format,
270 &p_dec->fmt_out.audio_replay_gain,
273 input_resource_PutAout( p_owner->p_resource, p_aout );
278 vlc_mutex_lock( &p_owner->lock );
280 p_owner->p_aout = p_aout;
282 DecoderUpdateFormatLocked( p_dec );
283 aout_FormatPrepare( &p_owner->fmt.audio );
285 if( unlikely(p_owner->b_paused) && p_aout != NULL )
286 /* fake pause if needed */
287 aout_DecChangePause( p_aout, true, mdate() );
289 vlc_mutex_unlock( &p_owner->lock );
291 if( p_owner->p_input != NULL )
292 input_SendEventAout( p_owner->p_input );
296 msg_Err( p_dec, "failed to create audio output" );
297 p_dec->b_error = true;
301 p_dec->fmt_out.audio.i_bytes_per_frame =
302 p_owner->fmt.audio.i_bytes_per_frame;
303 p_dec->fmt_out.audio.i_frame_length =
304 p_owner->fmt.audio.i_frame_length;
309 static int vout_update_format( decoder_t *p_dec )
311 decoder_owner_sys_t *p_owner = p_dec->p_owner;
313 if( p_owner->p_vout == NULL
314 || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
315 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
316 || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
317 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
318 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
319 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
320 || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
321 || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
322 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
323 p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation )
325 vout_thread_t *p_vout;
327 if( !p_dec->fmt_out.video.i_width ||
328 !p_dec->fmt_out.video.i_height )
330 /* Can't create a new vout without display size */
334 video_format_t fmt = p_dec->fmt_out.video;
335 fmt.i_chroma = p_dec->fmt_out.i_codec;
337 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
339 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
340 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
342 while( fmt.i_width % dsc->p[i].w.den )
344 while( fmt.i_height % dsc->p[i].h.den )
349 if( !fmt.i_visible_width || !fmt.i_visible_height )
351 if( p_dec->fmt_in.video.i_visible_width &&
352 p_dec->fmt_in.video.i_visible_height )
354 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
355 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
356 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
357 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
361 fmt.i_visible_width = fmt.i_width;
362 fmt.i_visible_height = fmt.i_height;
368 if( fmt.i_visible_height == 1088 &&
369 var_CreateGetBool( p_dec, "hdtv-fix" ) )
371 fmt.i_visible_height = 1080;
372 if( !(fmt.i_sar_num % 136))
374 fmt.i_sar_num *= 135;
375 fmt.i_sar_den *= 136;
377 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
380 if( !fmt.i_sar_num || !fmt.i_sar_den )
386 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
387 fmt.i_sar_num, fmt.i_sar_den, 50000 );
389 vlc_mutex_lock( &p_owner->lock );
391 p_vout = p_owner->p_vout;
392 p_owner->p_vout = NULL;
393 vlc_mutex_unlock( &p_owner->lock );
396 switch( p_dec->fmt_in.i_codec )
400 case VLC_CODEC_DIRAC: /* FIXME valid ? */
413 p_vout = input_resource_RequestVout( p_owner->p_resource,
416 p_dec->i_extra_picture_buffers + 1,
418 vlc_mutex_lock( &p_owner->lock );
419 p_owner->p_vout = p_vout;
421 DecoderUpdateFormatLocked( p_dec );
422 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
423 vlc_mutex_unlock( &p_owner->lock );
425 if( p_owner->p_input != NULL )
426 input_SendEventVout( p_owner->p_input );
429 msg_Err( p_dec, "failed to create video output" );
430 p_dec->b_error = true;
437 static picture_t *vout_new_buffer( decoder_t *p_dec )
439 decoder_owner_sys_t *p_owner = p_dec->p_owner;
443 if( DecoderIsFlushing( p_dec ) || p_dec->b_error )
446 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
451 DecoderSignalWait( p_dec );
453 /* Check the decoder doesn't leak pictures */
454 vout_FixLeaks( p_owner->p_vout );
456 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
457 msleep( VOUT_OUTMEM_SLEEP );
461 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
462 const subpicture_updater_t *p_updater )
464 decoder_owner_sys_t *p_owner = p_dec->p_owner;
465 vout_thread_t *p_vout = NULL;
466 subpicture_t *p_subpic;
469 while( i_attempts-- )
471 if( DecoderIsFlushing( p_dec ) || p_dec->b_error )
474 p_vout = input_resource_HoldVout( p_owner->p_resource );
478 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
483 msg_Warn( p_dec, "no vout found, dropping subpicture" );
487 if( p_owner->p_spu_vout != p_vout )
489 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
490 p_owner->i_spu_order = 0;
491 p_owner->p_spu_vout = p_vout;
494 p_subpic = subpicture_New( p_updater );
497 p_subpic->i_channel = p_owner->i_spu_channel;
498 p_subpic->i_order = p_owner->i_spu_order++;
499 p_subpic->b_subtitle = true;
502 vlc_object_release( p_vout );
507 static int DecoderGetInputAttachments( decoder_t *p_dec,
508 input_attachment_t ***ppp_attachment,
511 input_thread_t *p_input = p_dec->p_owner->p_input;
513 if( unlikely(p_input == NULL) )
515 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
516 ppp_attachment, pi_attachment );
519 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
521 decoder_owner_sys_t *p_owner = p_dec->p_owner;
523 vlc_mutex_lock( &p_owner->lock );
524 if( p_owner->b_waiting || p_owner->b_paused )
525 i_ts = VLC_TS_INVALID;
526 vlc_mutex_unlock( &p_owner->lock );
528 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
531 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) ) {
532 msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
533 return VLC_TS_INVALID;
539 static int DecoderGetDisplayRate( decoder_t *p_dec )
541 decoder_owner_sys_t *p_owner = p_dec->p_owner;
543 if( !p_owner->p_clock )
544 return INPUT_RATE_DEFAULT;
545 return input_clock_GetRate( p_owner->p_clock );
548 /*****************************************************************************
550 *****************************************************************************/
551 picture_t *decoder_NewPicture( decoder_t *p_decoder )
553 if( decoder_UpdateVideoFormat( p_decoder ) )
556 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
558 msg_Warn( p_decoder, "can't get output picture" );
562 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
564 if( decoder_UpdateAudioFormat( dec ) )
567 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
568 / dec->fmt_out.audio.i_frame_length;
569 block_t *block = block_Alloc( length );
570 if( likely(block != NULL) )
572 block->i_nb_samples = samples;
573 block->i_pts = block->i_length = 0;
578 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder,
579 const subpicture_updater_t *p_dyn )
581 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder, p_dyn );
583 msg_Warn( p_decoder, "can't get output subpicture" );
587 /* decoder_GetInputAttachments:
589 int decoder_GetInputAttachments( decoder_t *p_dec,
590 input_attachment_t ***ppp_attachment,
593 if( !p_dec->pf_get_attachments )
596 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
598 /* decoder_GetDisplayDate:
600 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
602 if( !p_dec->pf_get_display_date )
603 return VLC_TS_INVALID;
605 return p_dec->pf_get_display_date( p_dec, i_ts );
607 /* decoder_GetDisplayRate:
609 int decoder_GetDisplayRate( decoder_t *p_dec )
611 if( !p_dec->pf_get_display_rate )
612 return INPUT_RATE_DEFAULT;
614 return p_dec->pf_get_display_rate( p_dec );
617 static bool DecoderWaitUnblock( decoder_t *p_dec )
619 decoder_owner_sys_t *p_owner = p_dec->p_owner;
621 vlc_assert_locked( &p_owner->lock );
625 if( p_owner->b_flushing )
627 if( p_owner->b_paused )
629 if( p_owner->b_waiting && !p_owner->b_has_data )
631 if( p_owner->pause.i_ignore > 0 )
633 p_owner->pause.i_ignore--;
639 if( !p_owner->b_waiting || !p_owner->b_has_data )
642 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
645 return p_owner->b_flushing;
648 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
650 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
651 *pi_preroll = INT64_MAX;
652 else if( p->i_dts > VLC_TS_INVALID )
653 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
654 else if( p->i_pts > VLC_TS_INVALID )
655 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
658 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
659 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
661 decoder_owner_sys_t *p_owner = p_dec->p_owner;
662 input_clock_t *p_clock = p_owner->p_clock;
664 vlc_assert_locked( &p_owner->lock );
666 const mtime_t i_es_delay = p_owner->i_ts_delay;
671 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
674 if( *pi_ts0 > VLC_TS_INVALID )
676 *pi_ts0 += i_es_delay;
677 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
678 *pi_ts1 += i_es_delay;
679 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
681 msg_Err(p_dec, "Could not convert timestamps %"PRId64
682 ", %"PRId64"", *pi_ts0, *pi_ts1);
684 msg_Err(p_dec, "Could not convert timestamp %"PRId64, *pi_ts0);
685 *pi_ts0 = VLC_TS_INVALID;
690 i_rate = input_clock_GetRate( p_clock );
693 /* Do not create ephemere data because of rounding errors */
694 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
698 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
699 / INPUT_RATE_DEFAULT;
706 * If *pb_reject, it does nothing, otherwise it waits for the given
707 * deadline or a flush request (in which case it set *pi_reject to true.
709 static void DecoderWaitDate( decoder_t *p_dec,
710 bool *pb_reject, mtime_t i_deadline )
712 decoder_owner_sys_t *p_owner = p_dec->p_owner;
714 vlc_assert_locked( &p_owner->lock );
716 if( *pb_reject || i_deadline < 0 )
721 if( p_owner->b_flushing )
727 while( vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
734 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
736 decoder_owner_sys_t *p_owner = p_dec->p_owner;
738 assert( p_owner->p_clock );
739 assert( !p_sout_block->p_next );
741 vlc_mutex_lock( &p_owner->lock );
743 if( p_owner->b_waiting )
745 p_owner->b_has_data = true;
746 vlc_cond_signal( &p_owner->wait_acknowledge );
749 bool b_reject = DecoderWaitUnblock( p_dec );
751 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
752 &p_sout_block->i_length, NULL, INT64_MAX );
754 vlc_mutex_unlock( &p_owner->lock );
758 /* FIXME --VLC_TS_INVALID inspect stream_output*/
759 return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
763 block_Release( p_sout_block );
768 /* This function process a block for sout
770 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
772 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
773 block_t *p_sout_block;
775 while( ( p_sout_block =
776 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
778 if( p_owner->p_sout_input == NULL )
780 vlc_mutex_lock( &p_owner->lock );
781 DecoderUpdateFormatLocked( p_dec );
782 vlc_mutex_unlock( &p_owner->lock );
784 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
785 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
786 if( p_dec->fmt_in.psz_language )
788 free( p_owner->fmt.psz_language );
789 p_owner->fmt.psz_language =
790 strdup( p_dec->fmt_in.psz_language );
793 p_owner->p_sout_input =
794 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
796 if( p_owner->p_sout_input == NULL )
798 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
799 (char *)&p_owner->fmt.i_codec );
800 p_dec->b_error = true;
802 block_ChainRelease(p_sout_block);
807 while( p_sout_block )
809 block_t *p_next = p_sout_block->p_next;
811 p_sout_block->p_next = NULL;
813 if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
815 msg_Err( p_dec, "cannot continue streaming due to errors" );
817 p_dec->b_error = true;
820 block_ChainRelease( p_next );
824 p_sout_block = p_next;
830 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
832 decoder_owner_sys_t *p_owner = p_dec->p_owner;
835 bool b_processed = false;
839 assert( p_dec_cc->pf_get_cc != NULL );
841 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
842 if( !p_owner->cc.b_supported )
845 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
849 vlc_mutex_lock( &p_owner->lock );
850 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
852 p_owner->cc.pb_present[i] |= pb_present[i];
853 if( p_owner->cc.pp_decoder[i] )
857 for( i = 0; i < 4; i++ )
859 if( !p_owner->cc.pp_decoder[i] )
862 block_FifoPut( p_owner->cc.pp_decoder[i]->p_owner->p_fifo,
863 (i_cc_decoder > 1) ? block_Duplicate(p_cc) : p_cc);
868 vlc_mutex_unlock( &p_owner->lock );
871 block_Release( p_cc );
874 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
875 int *pi_played_sum, int *pi_lost_sum )
877 decoder_owner_sys_t *p_owner = p_dec->p_owner;
878 vout_thread_t *p_vout = p_owner->p_vout;
880 if( p_picture->date <= VLC_TS_INVALID )
882 msg_Warn( p_dec, "non-dated video buffer received" );
884 picture_Release( p_picture );
889 vlc_mutex_lock( &p_owner->lock );
891 if( p_owner->b_waiting && !p_owner->b_first )
893 p_owner->b_has_data = true;
894 vlc_cond_signal( &p_owner->wait_acknowledge );
896 bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
898 bool b_reject = DecoderWaitUnblock( p_dec );
900 if( p_owner->b_waiting )
902 assert( p_owner->b_first );
903 msg_Dbg( p_dec, "Received first picture" );
904 p_owner->b_first = false;
905 p_picture->b_force = true;
908 const bool b_dated = p_picture->date > VLC_TS_INVALID;
909 int i_rate = INPUT_RATE_DEFAULT;
910 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
911 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
913 vlc_mutex_unlock( &p_owner->lock );
916 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
921 if( i_rate != p_owner->i_last_rate || b_first_after_wait )
923 /* Be sure to not display old picture after our own */
924 vout_Flush( p_vout, p_picture->date );
925 p_owner->i_last_rate = i_rate;
927 vout_PutPicture( p_vout, p_picture );
932 msg_Warn( p_dec, "early picture skipped" );
934 msg_Warn( p_dec, "non-dated video buffer received" );
937 picture_Release( p_picture );
941 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
943 *pi_played_sum += i_tmp_display;
944 *pi_lost_sum += i_tmp_lost;
947 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
949 decoder_owner_sys_t *p_owner = p_dec->p_owner;
955 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
957 vout_thread_t *p_vout = p_owner->p_vout;
958 if( DecoderIsFlushing( p_dec ) )
959 { /* It prevent freezing VLC in case of broken decoder */
960 picture_Release( p_pic );
962 block_Release( p_block );
968 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
970 picture_Release( p_pic );
974 if( p_owner->i_preroll_end > VLC_TS_INVALID )
976 msg_Dbg( p_dec, "End of video preroll" );
978 vout_Flush( p_vout, VLC_TS_INVALID+1 );
980 p_owner->i_preroll_end = VLC_TS_INVALID;
983 if( p_dec->pf_get_cc &&
984 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
985 DecoderGetCc( p_dec, p_dec );
987 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
990 /* Update ugly stat */
991 input_thread_t *p_input = p_owner->p_input;
993 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
995 vlc_mutex_lock( &p_input->p->counters.counters_lock );
996 stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
997 stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
998 stats_Update( p_input->p->counters.p_displayed_pictures,
1000 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1004 /* This function process a video block
1006 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1008 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1010 if( p_owner->p_packetizer )
1012 block_t *p_packetized_block;
1013 decoder_t *p_packetizer = p_owner->p_packetizer;
1015 while( (p_packetized_block =
1016 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1018 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1020 es_format_Clean( &p_dec->fmt_in );
1021 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1024 /* If the packetizer provides aspect ratio information, pass it
1025 * to the decoder as a hint if the decoder itself can't provide
1026 * it. Copy it regardless of the current value of the decoder input
1027 * format aspect ratio, to properly propagate changes in aspect
1029 if( p_packetizer->fmt_out.video.i_sar_num > 0 &&
1030 p_packetizer->fmt_out.video.i_sar_den > 0)
1032 p_dec->fmt_in.video.i_sar_num =
1033 p_packetizer->fmt_out.video.i_sar_num;
1034 p_dec->fmt_in.video.i_sar_den=
1035 p_packetizer->fmt_out.video.i_sar_den;
1038 if( p_packetizer->pf_get_cc )
1039 DecoderGetCc( p_dec, p_packetizer );
1041 while( p_packetized_block )
1043 block_t *p_next = p_packetized_block->p_next;
1044 p_packetized_block->p_next = NULL;
1046 DecoderDecodeVideo( p_dec, p_packetized_block );
1048 p_packetized_block = p_next;
1051 /* The packetizer does not output a block that tell the decoder to flush
1055 block_t *p_null = DecoderBlockFlushNew();
1057 DecoderDecodeVideo( p_dec, p_null );
1062 DecoderDecodeVideo( p_dec, p_block );
1065 if( b_flush && p_owner->p_vout )
1066 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1069 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1070 int *pi_played_sum, int *pi_lost_sum )
1072 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1073 audio_output_t *p_aout = p_owner->p_aout;
1076 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1078 msg_Warn( p_dec, "non-dated audio buffer received" );
1080 block_Release( p_audio );
1085 vlc_mutex_lock( &p_owner->lock );
1087 if( p_owner->b_waiting )
1089 p_owner->b_has_data = true;
1090 vlc_cond_signal( &p_owner->wait_acknowledge );
1093 bool b_reject = DecoderWaitUnblock( p_dec );
1094 bool b_paused = p_owner->b_paused;
1097 int i_rate = INPUT_RATE_DEFAULT;
1099 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1100 &i_rate, AOUT_MAX_ADVANCE_TIME );
1102 if( p_audio->i_pts <= VLC_TS_INVALID
1103 || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1104 || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1107 DecoderWaitDate( p_dec, &b_reject,
1108 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1110 if( unlikely(p_owner->b_paused != b_paused) )
1111 goto race; /* race with input thread? retry... */
1112 if( p_aout == NULL )
1117 assert( !p_owner->b_paused );
1118 if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1119 *pi_played_sum += 1;
1120 *pi_lost_sum += aout_DecGetResetLost( p_aout );
1124 msg_Dbg( p_dec, "discarded audio buffer" );
1126 block_Release( p_audio );
1128 vlc_mutex_unlock( &p_owner->lock );
1131 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1133 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1134 block_t *p_aout_buf;
1139 if( p_block == NULL )
1140 return; /* TODO: remove this check, drain audio decoders properly */
1142 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1144 if( DecoderIsFlushing( p_dec ) )
1146 /* It prevent freezing VLC in case of broken decoder */
1147 block_Release( p_aout_buf );
1149 block_Release( p_block );
1154 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1155 p_aout_buf->i_pts < p_owner->i_preroll_end )
1157 block_Release( p_aout_buf );
1161 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1163 msg_Dbg( p_dec, "End of audio preroll" );
1164 if( p_owner->p_aout )
1165 aout_DecFlush( p_owner->p_aout );
1167 p_owner->i_preroll_end = VLC_TS_INVALID;
1170 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1173 /* Update ugly stat */
1174 input_thread_t *p_input = p_owner->p_input;
1176 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1178 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1179 stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1180 stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1181 stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1182 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1186 /* This function process a audio block
1188 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1190 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1192 if( p_owner->p_packetizer )
1194 block_t *p_packetized_block;
1195 decoder_t *p_packetizer = p_owner->p_packetizer;
1197 while( (p_packetized_block =
1198 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1200 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1202 es_format_Clean( &p_dec->fmt_in );
1203 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1206 while( p_packetized_block )
1208 block_t *p_next = p_packetized_block->p_next;
1209 p_packetized_block->p_next = NULL;
1211 DecoderDecodeAudio( p_dec, p_packetized_block );
1213 p_packetized_block = p_next;
1216 /* The packetizer does not output a block that tell the decoder to flush
1220 block_t *p_null = DecoderBlockFlushNew();
1222 DecoderDecodeAudio( p_dec, p_null );
1227 DecoderDecodeAudio( p_dec, p_block );
1230 if( b_flush && p_owner->p_aout )
1231 aout_DecFlush( p_owner->p_aout );
1234 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1236 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1237 vout_thread_t *p_vout = p_owner->p_spu_vout;
1240 if( p_subpic->i_start <= VLC_TS_INVALID )
1242 msg_Warn( p_dec, "non-dated spu buffer received" );
1243 subpicture_Delete( p_subpic );
1248 vlc_mutex_lock( &p_owner->lock );
1250 if( p_owner->b_waiting )
1252 p_owner->b_has_data = true;
1253 vlc_cond_signal( &p_owner->wait_acknowledge );
1256 bool b_reject = DecoderWaitUnblock( p_dec );
1258 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1261 if( p_subpic->i_start <= VLC_TS_INVALID )
1264 DecoderWaitDate( p_dec, &b_reject,
1265 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1266 vlc_mutex_unlock( &p_owner->lock );
1269 vout_PutSubpicture( p_vout, p_subpic );
1271 subpicture_Delete( p_subpic );
1274 /* This function process a subtitle block
1276 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1278 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1280 input_thread_t *p_input = p_owner->p_input;
1281 vout_thread_t *p_vout;
1282 subpicture_t *p_spu;
1284 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1286 if( p_input != NULL )
1288 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1289 stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1290 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1293 p_vout = input_resource_HoldVout( p_owner->p_resource );
1294 if( p_vout && p_owner->p_spu_vout == p_vout )
1296 /* Preroll does not work very well with subtitle */
1297 if( p_spu->i_start > VLC_TS_INVALID &&
1298 p_spu->i_start < p_owner->i_preroll_end &&
1299 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1301 subpicture_Delete( p_spu );
1305 DecoderPlaySpu( p_dec, p_spu );
1310 subpicture_Delete( p_spu );
1313 vlc_object_release( p_vout );
1316 if( b_flush && p_owner->p_spu_vout )
1318 p_vout = input_resource_HoldVout( p_owner->p_resource );
1320 if( p_vout && p_owner->p_spu_vout == p_vout )
1321 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1324 vlc_object_release( p_vout );
1329 static void DecoderProcessOnFlush( decoder_t *p_dec )
1331 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1333 vlc_mutex_lock( &p_owner->lock );
1335 if( p_owner->b_flushing )
1337 p_owner->b_flushing = false;
1338 vlc_cond_signal( &p_owner->wait_acknowledge );
1340 vlc_mutex_unlock( &p_owner->lock );
1346 * \param p_dec the decoder object
1347 * \param p_block the block to decode
1348 * \return VLC_SUCCESS or an error code
1350 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1352 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1353 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1355 if( p_dec->b_error )
1358 block_Release( p_block );
1362 if( p_block && p_block->i_buffer <= 0 )
1364 assert( !b_flush_request );
1365 block_Release( p_block );
1370 if( p_owner->b_packetizer )
1373 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1375 DecoderProcessSout( p_dec, p_block );
1380 bool b_flush = false;
1384 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1385 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1387 b_flush = !b_flushing && b_flush_request;
1389 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1392 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1394 DecoderProcessAudio( p_dec, p_block, b_flush );
1396 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1398 DecoderProcessVideo( p_dec, p_block, b_flush );
1400 else if( p_dec->fmt_out.i_cat == SPU_ES )
1402 DecoderProcessSpu( p_dec, p_block, b_flush );
1406 msg_Err( p_dec, "unknown ES format" );
1407 p_dec->b_error = true;
1413 if( b_flush_request )
1414 DecoderProcessOnFlush( p_dec );
1419 * The decoding main loop
1421 * \param p_dec the decoder
1423 static void *DecoderThread( void *p_data )
1425 decoder_t *p_dec = (decoder_t *)p_data;
1426 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1428 /* The decoder's main loop */
1433 vlc_fifo_Lock( p_owner->p_fifo );
1434 vlc_fifo_CleanupPush( p_owner->p_fifo );
1436 while( vlc_fifo_IsEmpty( p_owner->p_fifo ) )
1438 if( p_owner->b_woken )
1440 vlc_fifo_Wait( p_owner->p_fifo );
1441 /* Make sure there is no cancellation point other than this one^^.
1442 * If you need one, be sure to push cleanup of p_block. */
1445 p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1446 if( p_block != NULL )
1447 vlc_cond_signal( &p_owner->wait_acknowledge );
1449 p_owner->b_woken = false;
1452 if( p_block == NULL || p_block->i_flags & BLOCK_FLAG_CORE_EOS )
1453 DecoderSignalWait( p_dec );
1457 int canc = vlc_savecancel();
1459 if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
1461 /* calling DecoderProcess() with NULL block will make
1462 * decoders/packetizers flush their buffers */
1463 block_Release( p_block );
1467 DecoderProcess( p_dec, p_block );
1469 vlc_restorecancel( canc );
1476 * Create a decoder object
1478 * \param p_input the input thread
1479 * \param p_es the es descriptor
1480 * \param b_packetizer instead of a decoder
1481 * \return the decoder object
1483 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1484 input_thread_t *p_input,
1485 const es_format_t *fmt, bool b_packetizer,
1486 input_resource_t *p_resource,
1487 sout_instance_t *p_sout )
1490 decoder_owner_sys_t *p_owner;
1491 es_format_t null_es_format;
1493 p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
1497 p_dec->pf_decode_audio = NULL;
1498 p_dec->pf_decode_video = NULL;
1499 p_dec->pf_decode_sub = NULL;
1500 p_dec->pf_get_cc = NULL;
1501 p_dec->pf_packetize = NULL;
1503 /* Initialize the decoder */
1504 p_dec->p_module = NULL;
1506 memset( &null_es_format, 0, sizeof(es_format_t) );
1507 es_format_Copy( &p_dec->fmt_in, fmt );
1508 es_format_Copy( &p_dec->fmt_out, &null_es_format );
1510 p_dec->p_description = NULL;
1512 /* Allocate our private structure for the decoder */
1513 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
1514 if( unlikely(p_owner == NULL) )
1516 vlc_object_release( p_dec );
1519 p_owner->i_preroll_end = VLC_TS_INVALID;
1520 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1521 p_owner->p_input = p_input;
1522 p_owner->p_resource = p_resource;
1523 p_owner->p_aout = NULL;
1524 p_owner->p_vout = NULL;
1525 p_owner->p_spu_vout = NULL;
1526 p_owner->i_spu_channel = 0;
1527 p_owner->i_spu_order = 0;
1528 p_owner->p_sout = p_sout;
1529 p_owner->p_sout_input = NULL;
1530 p_owner->p_packetizer = NULL;
1531 p_owner->b_packetizer = b_packetizer;
1532 es_format_Init( &p_owner->fmt, UNKNOWN_ES, 0 );
1535 p_owner->b_woken = false;
1536 p_owner->p_fifo = block_FifoNew();
1537 if( unlikely(p_owner->p_fifo == NULL) )
1540 vlc_object_release( p_dec );
1544 /* Set buffers allocation callbacks for the decoders */
1545 p_dec->pf_aout_format_update = aout_update_format;
1546 p_dec->pf_vout_format_update = vout_update_format;
1547 p_dec->pf_vout_buffer_new = vout_new_buffer;
1548 p_dec->pf_spu_buffer_new = spu_new_buffer;
1550 p_dec->pf_get_attachments = DecoderGetInputAttachments;
1551 p_dec->pf_get_display_date = DecoderGetDisplayDate;
1552 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
1554 /* Find a suitable decoder/packetizer module */
1556 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
1558 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
1560 /* Check if decoder requires already packetized data */
1561 if( !b_packetizer &&
1562 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
1564 p_owner->p_packetizer =
1565 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1566 if( p_owner->p_packetizer )
1568 es_format_Copy( &p_owner->p_packetizer->fmt_in,
1571 es_format_Copy( &p_owner->p_packetizer->fmt_out,
1574 p_owner->p_packetizer->p_module =
1575 module_need( p_owner->p_packetizer,
1576 "packetizer", "$packetizer", false );
1578 if( !p_owner->p_packetizer->p_module )
1580 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1581 vlc_object_release( p_owner->p_packetizer );
1582 p_owner->p_packetizer = NULL;
1587 /* Copy ourself the input replay gain */
1588 if( fmt->i_cat == AUDIO_ES )
1590 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1592 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1594 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1595 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1597 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1599 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1600 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1606 vlc_mutex_init( &p_owner->lock );
1607 vlc_cond_init( &p_owner->wait_request );
1608 vlc_cond_init( &p_owner->wait_acknowledge );
1610 p_owner->b_fmt_description = false;
1611 p_owner->p_description = NULL;
1613 p_owner->b_paused = false;
1614 p_owner->pause.i_date = VLC_TS_INVALID;
1615 p_owner->pause.i_ignore = 0;
1617 p_owner->b_waiting = false;
1618 p_owner->b_first = true;
1619 p_owner->b_has_data = false;
1621 p_owner->b_flushing = false;
1624 p_owner->cc.b_supported = false;
1627 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
1628 p_owner->cc.b_supported = true;
1629 if( p_dec->pf_get_cc )
1630 p_owner->cc.b_supported = true;
1633 for( unsigned i = 0; i < 4; i++ )
1635 p_owner->cc.pb_present[i] = false;
1636 p_owner->cc.pp_decoder[i] = NULL;
1638 p_owner->i_ts_delay = 0;
1643 * Destroys a decoder object
1645 * \param p_dec the decoder object
1648 static void DeleteDecoder( decoder_t * p_dec )
1650 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1652 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1653 (char*)&p_dec->fmt_in.i_codec,
1654 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1656 /* Free all packets still in the decoder fifo. */
1657 block_FifoRelease( p_owner->p_fifo );
1660 if( p_owner->p_aout )
1662 /* TODO: REVISIT gap-less audio */
1663 aout_DecFlush( p_owner->p_aout );
1664 aout_DecDelete( p_owner->p_aout );
1665 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1666 if( p_owner->p_input != NULL )
1667 input_SendEventAout( p_owner->p_input );
1669 if( p_owner->p_vout )
1671 /* Hack to make sure all the the pictures are freed by the decoder
1672 * and that the vout is not paused anymore */
1673 vout_Reset( p_owner->p_vout );
1676 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1678 if( p_owner->p_input != NULL )
1679 input_SendEventVout( p_owner->p_input );
1683 if( p_owner->p_sout_input )
1685 sout_InputDelete( p_owner->p_sout_input );
1688 es_format_Clean( &p_owner->fmt );
1690 if( p_dec->fmt_out.i_cat == SPU_ES )
1692 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1695 if( p_owner->p_spu_vout == p_vout )
1696 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1697 vlc_object_release( p_vout );
1701 es_format_Clean( &p_dec->fmt_in );
1702 es_format_Clean( &p_dec->fmt_out );
1703 if( p_dec->p_description )
1704 vlc_meta_Delete( p_dec->p_description );
1705 if( p_owner->p_description )
1706 vlc_meta_Delete( p_owner->p_description );
1708 if( p_owner->p_packetizer )
1710 module_unneed( p_owner->p_packetizer,
1711 p_owner->p_packetizer->p_module );
1712 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1713 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1714 if( p_owner->p_packetizer->p_description )
1715 vlc_meta_Delete( p_owner->p_packetizer->p_description );
1716 vlc_object_release( p_owner->p_packetizer );
1719 vlc_cond_destroy( &p_owner->wait_acknowledge );
1720 vlc_cond_destroy( &p_owner->wait_request );
1721 vlc_mutex_destroy( &p_owner->lock );
1723 vlc_object_release( p_dec );
1729 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
1731 if (codec != VLC_FOURCC('u','n','d','f')) {
1732 const char *desc = vlc_fourcc_GetDescription(p_dec->fmt_in.i_cat, codec);
1733 if (!desc || !*desc)
1734 desc = N_("No description for this codec");
1735 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&codec, desc );
1736 dialog_Fatal( p_dec, _("Codec not supported"),
1737 _("VLC could not decode the format \"%4.4s\" (%s)"),
1738 (char*)&codec, desc );
1740 msg_Err( p_dec, "could not identify codec" );
1741 dialog_Fatal( p_dec, _("Unidentified codec"),
1742 _("VLC could not identify the audio or video codec" ) );
1746 /* TODO: pass p_sout through p_resource? -- Courmisch */
1747 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1748 const es_format_t *fmt, input_clock_t *p_clock,
1749 input_resource_t *p_resource,
1750 sout_instance_t *p_sout )
1752 decoder_t *p_dec = NULL;
1753 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1756 /* Create the decoder configuration structure */
1757 p_dec = CreateDecoder( p_parent, p_input, fmt,
1758 p_sout != NULL, p_resource, p_sout );
1761 msg_Err( p_parent, "could not create %s", psz_type );
1762 dialog_Fatal( p_parent, _("Streaming / Transcoding failed"),
1763 _("VLC could not open the %s module."),
1764 vlc_gettext( psz_type ) );
1768 if( !p_dec->p_module )
1770 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
1772 DeleteDecoder( p_dec );
1776 p_dec->p_owner->p_clock = p_clock;
1777 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1779 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1780 i_priority = VLC_THREAD_PRIORITY_AUDIO;
1782 i_priority = VLC_THREAD_PRIORITY_VIDEO;
1784 /* Spawn the decoder thread */
1785 if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
1787 msg_Err( p_dec, "cannot spawn decoder thread" );
1788 module_unneed( p_dec, p_dec->p_module );
1789 DeleteDecoder( p_dec );
1798 * Spawns a new decoder thread from the input thread
1800 * \param p_input the input thread
1801 * \param p_es the es descriptor
1802 * \return the spawned decoder object
1804 decoder_t *input_DecoderNew( input_thread_t *p_input,
1805 es_format_t *fmt, input_clock_t *p_clock,
1806 sout_instance_t *p_sout )
1808 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
1809 p_input->p->p_resource, p_sout );
1813 * Spawn a decoder thread outside of the input thread.
1815 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
1816 input_resource_t *p_resource )
1818 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
1823 * Kills a decoder thread and waits until it's finished
1825 * \param p_input the input thread
1826 * \param p_es the es descriptor
1829 void input_DecoderDelete( decoder_t *p_dec )
1831 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1833 vlc_cancel( p_owner->thread );
1835 /* Make sure we aren't paused/waiting/decoding anymore */
1836 vlc_mutex_lock( &p_owner->lock );
1837 p_owner->b_paused = false;
1838 p_owner->b_waiting = false;
1839 p_owner->b_flushing = true;
1840 vlc_cond_signal( &p_owner->wait_request );
1841 vlc_mutex_unlock( &p_owner->lock );
1843 vlc_join( p_owner->thread, NULL );
1845 module_unneed( p_dec, p_dec->p_module );
1848 if( p_dec->p_owner->cc.b_supported )
1851 for( i = 0; i < 4; i++ )
1852 input_DecoderSetCcState( p_dec, false, i );
1855 /* Delete decoder */
1856 DeleteDecoder( p_dec );
1860 * Put a block_t in the decoder's fifo.
1861 * Thread-safe w.r.t. the decoder. May be a cancellation point.
1863 * \param p_dec the decoder object
1864 * \param p_block the data block
1866 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
1868 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1870 vlc_fifo_Lock( p_owner->p_fifo );
1873 /* FIXME: ideally we would check the time amount of data
1874 * in the FIFO instead of its size. */
1875 /* 400 MiB, i.e. ~ 50mb/s for 60s */
1876 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
1878 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
1879 "consumed quickly enough), resetting fifo!" );
1880 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
1884 if( !p_owner->b_waiting )
1885 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
1886 * Locking is not necessary as b_waiting is only read, not written by
1887 * the decoder thread. */
1888 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
1889 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_acknowledge );
1892 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
1893 vlc_fifo_Unlock( p_owner->p_fifo );
1896 bool input_DecoderIsEmpty( decoder_t * p_dec )
1898 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1899 assert( !p_owner->b_waiting );
1901 bool b_empty = block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
1905 vlc_mutex_lock( &p_owner->lock );
1906 /* TODO subtitles support */
1907 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout )
1908 b_empty = vout_IsEmpty( p_owner->p_vout );
1909 else if( p_owner->fmt.i_cat == AUDIO_ES && p_owner->p_aout )
1910 b_empty = aout_DecIsEmpty( p_owner->p_aout );
1911 vlc_mutex_unlock( &p_owner->lock );
1917 * Signals that there are no further blocks to decode, and requests that the
1918 * decoder drain all pending buffers. This is used to ensure that all
1919 * intermediate buffers empty and no samples get lost at the end of the stream.
1921 * @note The function does not actually wait for draining. It just signals that
1922 * draining should be performed once the decoder has emptied FIFO.
1924 void input_DecoderDrain( decoder_t *p_dec )
1926 block_t *p_block = block_Alloc(0);
1927 if( unlikely(p_block == NULL) )
1930 p_block->i_flags |= BLOCK_FLAG_CORE_EOS;
1931 input_DecoderDecode( p_dec, p_block, false );
1934 static void DecoderFlush( decoder_t *p_dec )
1936 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1938 vlc_assert_locked( &p_owner->lock );
1940 /* Empty the fifo */
1941 block_FifoEmpty( p_owner->p_fifo );
1943 /* Monitor for flush end */
1944 p_owner->b_flushing = true;
1945 vlc_cond_signal( &p_owner->wait_request );
1947 /* Send a special block */
1948 block_t *p_null = DecoderBlockFlushNew();
1951 input_DecoderDecode( p_dec, p_null, false );
1954 while( p_owner->b_flushing )
1955 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
1959 * Requests that the decoder immediately discard all pending buffers.
1960 * This is useful when seeking or when deselecting a stream.
1962 void input_DecoderFlush( decoder_t *p_dec )
1964 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1966 vlc_mutex_lock( &p_owner->lock );
1967 DecoderFlush( p_dec );
1968 vlc_mutex_unlock( &p_owner->lock );
1971 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
1973 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1976 vlc_mutex_lock( &p_owner->lock );
1977 for( i = 0; i < 4; i++ )
1978 pb_present[i] = p_owner->cc.pb_present[i];
1979 vlc_mutex_unlock( &p_owner->lock );
1982 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
1984 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1986 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
1988 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
1989 return VLC_EGENERIC;
1993 static const vlc_fourcc_t fcc[4] = {
1994 VLC_FOURCC('c', 'c', '1', ' '),
1995 VLC_FOURCC('c', 'c', '2', ' '),
1996 VLC_FOURCC('c', 'c', '3', ' '),
1997 VLC_FOURCC('c', 'c', '4', ' '),
2002 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
2003 p_cc = input_DecoderNew( p_owner->p_input, &fmt,
2004 p_dec->p_owner->p_clock, p_owner->p_sout );
2007 msg_Err( p_dec, "could not create decoder" );
2008 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
2009 _("VLC could not open the decoder module.") );
2010 return VLC_EGENERIC;
2012 else if( !p_cc->p_module )
2014 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
2015 input_DecoderDelete(p_cc);
2016 return VLC_EGENERIC;
2018 p_cc->p_owner->p_clock = p_owner->p_clock;
2020 vlc_mutex_lock( &p_owner->lock );
2021 p_owner->cc.pp_decoder[i_channel] = p_cc;
2022 vlc_mutex_unlock( &p_owner->lock );
2028 vlc_mutex_lock( &p_owner->lock );
2029 p_cc = p_owner->cc.pp_decoder[i_channel];
2030 p_owner->cc.pp_decoder[i_channel] = NULL;
2031 vlc_mutex_unlock( &p_owner->lock );
2034 input_DecoderDelete(p_cc);
2039 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
2041 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2044 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
2045 return VLC_EGENERIC;
2047 vlc_mutex_lock( &p_owner->lock );
2048 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2049 vlc_mutex_unlock( &p_owner->lock );
2050 return VLC_EGENERIC;
2053 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2055 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2057 vlc_mutex_lock( &p_owner->lock );
2058 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2059 * while the input is paused (e.g. add sub file), then b_paused is
2060 * (incorrectly) false. */
2061 if( likely(p_owner->b_paused != b_paused) ) {
2062 p_owner->b_paused = b_paused;
2063 p_owner->pause.i_date = i_date;
2064 p_owner->pause.i_ignore = 0;
2065 vlc_cond_signal( &p_owner->wait_request );
2067 /* XXX only audio and video output have to be paused.
2068 * - for sout it is useless
2069 * - for subs, it is done by the vout
2071 if( p_owner->fmt.i_cat == AUDIO_ES )
2073 if( p_owner->p_aout )
2074 aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
2076 else if( p_owner->fmt.i_cat == VIDEO_ES )
2078 if( p_owner->p_vout )
2079 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
2082 vlc_mutex_unlock( &p_owner->lock );
2085 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2087 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2089 vlc_mutex_lock( &p_owner->lock );
2090 p_owner->i_ts_delay = i_delay;
2091 vlc_mutex_unlock( &p_owner->lock );
2094 void input_DecoderStartWait( decoder_t *p_dec )
2096 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2098 assert( !p_owner->b_waiting );
2100 vlc_mutex_lock( &p_owner->lock );
2101 p_owner->b_first = true;
2102 p_owner->b_has_data = false;
2103 p_owner->b_waiting = true;
2104 vlc_cond_signal( &p_owner->wait_request );
2105 vlc_mutex_unlock( &p_owner->lock );
2108 void input_DecoderStopWait( decoder_t *p_dec )
2110 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2112 assert( p_owner->b_waiting );
2114 vlc_mutex_lock( &p_owner->lock );
2115 p_owner->b_waiting = false;
2116 vlc_cond_signal( &p_owner->wait_request );
2117 vlc_mutex_unlock( &p_owner->lock );
2120 void input_DecoderWait( decoder_t *p_dec )
2122 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2124 assert( p_owner->b_waiting );
2126 vlc_mutex_lock( &p_owner->lock );
2127 while( !p_owner->b_has_data )
2129 vlc_fifo_Lock( p_owner->p_fifo );
2130 p_owner->b_woken = true;
2131 vlc_fifo_Signal( p_owner->p_fifo );
2132 vlc_fifo_Unlock( p_owner->p_fifo );
2133 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2135 vlc_mutex_unlock( &p_owner->lock );
2138 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2140 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2144 vlc_mutex_lock( &p_owner->lock );
2145 if( p_owner->fmt.i_cat == VIDEO_ES )
2147 if( p_owner->b_paused && p_owner->p_vout )
2149 vout_NextPicture( p_owner->p_vout, pi_duration );
2150 p_owner->pause.i_ignore++;
2151 vlc_cond_signal( &p_owner->wait_request );
2156 /* TODO subtitle should not be flushed */
2157 p_owner->b_waiting = false;
2158 DecoderFlush( p_dec );
2160 vlc_mutex_unlock( &p_owner->lock );
2163 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2165 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2168 vlc_mutex_lock( &p_owner->lock );
2169 b_changed = p_owner->b_fmt_description;
2173 es_format_Copy( p_fmt, &p_owner->fmt );
2178 if( p_owner->p_description )
2180 *pp_meta = vlc_meta_New();
2182 vlc_meta_Merge( *pp_meta, p_owner->p_description );
2185 p_owner->b_fmt_description = false;
2187 vlc_mutex_unlock( &p_owner->lock );
2191 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2193 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2195 return block_FifoSize( p_owner->p_fifo );
2198 void input_DecoderGetObjects( decoder_t *p_dec,
2199 vout_thread_t **pp_vout, audio_output_t **pp_aout )
2201 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2203 vlc_mutex_lock( &p_owner->lock );
2205 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
2207 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
2208 vlc_mutex_unlock( &p_owner->lock );