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 *****************************************************************************/
35 #include <vlc_block.h>
39 #include <vlc_codec.h>
42 #include <vlc_interface.h>
43 #include "audio_output/aout_internal.h"
44 #include "stream_output/stream_output.h"
45 #include "input_internal.h"
47 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
48 static void DeleteDecoder( decoder_t * );
50 static int DecoderThread( decoder_t * );
51 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
53 /* Buffers allocation callbacks for the decoders */
54 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
55 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
57 static picture_t *vout_new_buffer( decoder_t * );
58 static void vout_del_buffer( decoder_t *, picture_t * );
59 static void vout_link_picture( decoder_t *, picture_t * );
60 static void vout_unlink_picture( decoder_t *, picture_t * );
62 static subpicture_t *spu_new_buffer( decoder_t * );
63 static void spu_del_buffer( decoder_t *, subpicture_t * );
65 static es_format_t null_es_format;
67 struct decoder_owner_sys_t
69 vlc_bool_t b_own_thread;
71 int64_t i_preroll_end;
73 input_thread_t *p_input;
75 aout_instance_t *p_aout;
76 aout_input_t *p_aout_input;
78 vout_thread_t *p_vout;
80 vout_thread_t *p_spu_vout;
83 sout_instance_t *p_sout;
84 sout_packetizer_input_t *p_sout_input;
86 /* Some decoders require already packetized data (ie. not truncated) */
87 decoder_t *p_packetizer;
89 /* Current format in use by the output */
98 vlc_bool_t b_cc_supported;
100 vlc_bool_t pb_cc_present[4];
105 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
107 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
108 "VLC probably does not support this sound or video format.",
110 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module"),
111 _("VLC does not support the audio or video format \"%4.4s\". "
112 "Unfortunately there is no way for you to fix this."), (char*)&codec );
115 /* decoder_GetInputAttachment:
117 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
118 const char *psz_name )
120 input_attachment_t *p_attachment;
121 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
125 /* decoder_GetInputAttachments:
127 int decoder_GetInputAttachments( decoder_t *p_dec,
128 input_attachment_t ***ppp_attachment,
131 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
132 ppp_attachment, pi_attachment );
134 /* decoder_GetDisplayDate:
136 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
142 * Spawns a new decoder thread
144 * \param p_input the input thread
145 * \param p_es the es descriptor
146 * \return the spawned decoder object
148 decoder_t *input_DecoderNew( input_thread_t *p_input,
149 es_format_t *fmt, vlc_bool_t b_force_decoder )
151 decoder_t *p_dec = NULL;
154 /* If we are in sout mode, search for packetizer module */
155 if( p_input->p->p_sout && !b_force_decoder )
157 /* Create the decoder configuration structure */
158 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
161 msg_Err( p_input, "could not create packetizer" );
162 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
163 _("VLC could not open the packetizer module.") );
169 /* Create the decoder configuration structure */
170 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
173 msg_Err( p_input, "could not create decoder" );
174 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
175 _("VLC could not open the decoder module.") );
180 if( !p_dec->p_module )
182 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
184 DeleteDecoder( p_dec );
185 vlc_object_destroy( p_dec );
189 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
192 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
193 p_dec->p_owner->b_own_thread = VLC_FALSE;
197 var_Get( p_input, "minimize-threads", &val );
198 p_dec->p_owner->b_own_thread = !val.b_bool;
201 if( p_dec->p_owner->b_own_thread )
204 if( fmt->i_cat == AUDIO_ES )
205 i_priority = VLC_THREAD_PRIORITY_AUDIO;
207 i_priority = VLC_THREAD_PRIORITY_VIDEO;
209 /* Spawn the decoder thread */
210 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
211 i_priority, VLC_FALSE ) )
213 msg_Err( p_dec, "cannot spawn decoder thread" );
214 module_Unneed( p_dec, p_dec->p_module );
215 DeleteDecoder( p_dec );
216 vlc_object_destroy( p_dec );
225 * Kills a decoder thread and waits until it's finished
227 * \param p_input the input thread
228 * \param p_es the es descriptor
231 void input_DecoderDelete( decoder_t *p_dec )
233 vlc_object_kill( p_dec );
235 if( p_dec->p_owner->b_own_thread )
237 /* Make sure the thread leaves the function by
238 * sending it an empty block. */
239 block_t *p_block = block_New( p_dec, 0 );
240 input_DecoderDecode( p_dec, p_block );
242 vlc_thread_join( p_dec );
244 /* Don't module_Unneed() here because of the dll loader that wants
245 * close() in the same thread than open()/decode() */
250 input_DecoderDecode( p_dec, NULL );
252 module_Unneed( p_dec, p_dec->p_module );
256 if( p_dec->p_owner->b_cc_supported )
259 for( i = 0; i < 4; i++ )
260 input_DecoderSetCcState( p_dec, VLC_FALSE, i );
263 /* Delete decoder configuration */
264 DeleteDecoder( p_dec );
266 /* Delete the decoder */
267 vlc_object_destroy( p_dec );
271 * Put a block_t in the decoder's fifo.
273 * \param p_dec the decoder object
274 * \param p_block the data block
276 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
278 if( p_dec->p_owner->b_own_thread )
280 if( p_dec->p_owner->p_input->p->b_out_pace_control )
283 while( !p_dec->b_die && !p_dec->b_error &&
284 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
289 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
291 /* FIXME: ideally we would check the time amount of data
292 * in the fifo instead of its size. */
293 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
294 "consumed quickly enough), resetting fifo!" );
295 block_FifoEmpty( p_dec->p_owner->p_fifo );
298 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
302 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
304 if( p_block ) block_Release( p_block );
308 DecoderDecode( p_dec, p_block );
313 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
318 if( p_dec->p_owner->b_own_thread && b_flush )
319 block_FifoEmpty( p_dec->p_owner->p_fifo );
321 /* Send a special block */
322 p_null = block_New( p_dec, 128 );
323 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
324 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
325 if( p_dec->p_owner->p_packetizer && b_flush )
326 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
327 memset( p_null->p_buffer, 0, p_null->i_buffer );
329 input_DecoderDecode( p_dec, p_null );
332 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
334 if( p_dec->p_owner->b_own_thread
335 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
342 void input_DecoderIsCcPresent( decoder_t *p_dec, vlc_bool_t pb_present[4] )
346 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
347 for( i = 0; i < 4; i++ )
348 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
349 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
351 int input_DecoderSetCcState( decoder_t *p_dec, vlc_bool_t b_decode, int i_channel )
353 decoder_owner_sys_t *p_owner = p_dec->p_owner;
355 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
357 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
362 static const vlc_fourcc_t fcc[4] = {
363 VLC_FOURCC('c', 'c', '1', ' '),
364 VLC_FOURCC('c', 'c', '2', ' '),
365 VLC_FOURCC('c', 'c', '3', ' '),
366 VLC_FOURCC('c', 'c', '4', ' '),
371 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
372 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
375 msg_Err( p_dec, "could not create decoder" );
376 intf_UserFatal( p_dec, VLC_FALSE, _("Streaming / Transcoding failed"),
377 _("VLC could not open the decoder module.") );
380 else if( !p_cc->p_module )
382 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
383 DeleteDecoder( p_cc );
384 vlc_object_destroy( p_cc );
388 vlc_mutex_lock( &p_owner->lock_cc );
389 p_dec->p_owner->pp_cc[i_channel] = p_cc;
390 vlc_mutex_unlock( &p_owner->lock_cc );
396 vlc_mutex_lock( &p_owner->lock_cc );
397 p_cc = p_dec->p_owner->pp_cc[i_channel];
398 p_dec->p_owner->pp_cc[i_channel] = NULL;
399 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
403 vlc_object_kill( p_cc );
404 module_Unneed( p_cc, p_cc->p_module );
405 DeleteDecoder( p_cc );
406 vlc_object_destroy( p_cc );
411 int input_DecoderGetCcState( decoder_t *p_dec, vlc_bool_t *pb_decode, int i_channel )
413 decoder_owner_sys_t *p_owner = p_dec->p_owner;
415 *pb_decode = VLC_FALSE;
416 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
419 vlc_mutex_lock( &p_owner->lock_cc );
420 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
421 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
426 * Create a decoder object
428 * \param p_input the input thread
429 * \param p_es the es descriptor
430 * \param i_object_type Object type as define in include/vlc_objects.h
431 * \return the decoder object
433 static decoder_t * CreateDecoder( input_thread_t *p_input,
434 es_format_t *fmt, int i_object_type )
437 decoder_owner_sys_t *p_owner;
440 p_dec = vlc_object_create( p_input, i_object_type );
443 msg_Err( p_input, "out of memory" );
447 p_dec->pf_decode_audio = 0;
448 p_dec->pf_decode_video = 0;
449 p_dec->pf_decode_sub = 0;
450 p_dec->pf_get_cc = 0;
451 p_dec->pf_packetize = 0;
453 /* Initialize the decoder fifo */
454 p_dec->p_module = NULL;
456 memset( &null_es_format, 0, sizeof(es_format_t) );
457 es_format_Copy( &p_dec->fmt_in, fmt );
458 es_format_Copy( &p_dec->fmt_out, &null_es_format );
460 /* Allocate our private structure for the decoder */
461 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
462 if( p_dec->p_owner == NULL )
464 msg_Err( p_dec, "out of memory" );
467 p_dec->p_owner->b_own_thread = VLC_TRUE;
468 p_dec->p_owner->i_preroll_end = -1;
469 p_dec->p_owner->p_input = p_input;
470 p_dec->p_owner->p_aout = NULL;
471 p_dec->p_owner->p_aout_input = NULL;
472 p_dec->p_owner->p_vout = NULL;
473 p_dec->p_owner->p_spu_vout = NULL;
474 p_dec->p_owner->i_spu_channel = 0;
475 p_dec->p_owner->p_sout = p_input->p->p_sout;
476 p_dec->p_owner->p_sout_input = NULL;
477 p_dec->p_owner->p_packetizer = NULL;
480 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
482 msg_Err( p_dec, "out of memory" );
486 /* Set buffers allocation callbacks for the decoders */
487 p_dec->pf_aout_buffer_new = aout_new_buffer;
488 p_dec->pf_aout_buffer_del = aout_del_buffer;
489 p_dec->pf_vout_buffer_new = vout_new_buffer;
490 p_dec->pf_vout_buffer_del = vout_del_buffer;
491 p_dec->pf_picture_link = vout_link_picture;
492 p_dec->pf_picture_unlink = vout_unlink_picture;
493 p_dec->pf_spu_buffer_new = spu_new_buffer;
494 p_dec->pf_spu_buffer_del = spu_del_buffer;
496 vlc_object_attach( p_dec, p_input );
498 /* Find a suitable decoder/packetizer module */
499 if( i_object_type == VLC_OBJECT_DECODER )
500 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
502 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
504 /* Check if decoder requires already packetized data */
505 if( i_object_type == VLC_OBJECT_DECODER &&
506 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
508 p_dec->p_owner->p_packetizer =
509 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
510 if( p_dec->p_owner->p_packetizer )
512 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
515 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
518 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
520 p_dec->p_owner->p_packetizer->p_module =
521 module_Need( p_dec->p_owner->p_packetizer,
522 "packetizer", "$packetizer", 0 );
524 if( !p_dec->p_owner->p_packetizer->p_module )
526 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
527 vlc_object_detach( p_dec->p_owner->p_packetizer );
528 vlc_object_destroy( p_dec->p_owner->p_packetizer );
533 /* Copy ourself the input replay gain */
534 if( fmt->i_cat == AUDIO_ES )
536 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
538 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
540 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
541 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
543 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
545 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
546 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
551 p_owner->b_cc_supported = VLC_FALSE;
552 if( i_object_type == VLC_OBJECT_DECODER )
554 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
555 p_owner->b_cc_supported = VLC_TRUE;
556 if( p_dec->pf_get_cc )
557 p_owner->b_cc_supported = VLC_TRUE;
560 vlc_mutex_init( p_dec, &p_owner->lock_cc );
561 for( i = 0; i < 4; i++ )
563 p_owner->pb_cc_present[i] = VLC_FALSE;
564 p_owner->pp_cc[i] = NULL;
570 * The decoding main loop
572 * \param p_dec the decoder
575 static int DecoderThread( decoder_t * p_dec )
579 /* The decoder's main loop */
580 while( !p_dec->b_die && !p_dec->b_error )
582 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
587 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
593 while( !p_dec->b_die )
595 /* Trash all received PES packets */
596 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
597 if( p_block ) block_Release( p_block );
600 /* We do it here because of the dll loader that wants close() in the
601 * same thread than open()/decode() */
602 module_Unneed( p_dec, p_dec->p_module );
607 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
609 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
610 *pi_preroll = INT64_MAX;
611 else if( p->i_pts > 0 )
612 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
613 else if( p->i_dts > 0 )
614 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
616 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
618 input_thread_t *p_input = p_dec->p_owner->p_input;
619 const int i_rate = p_block->i_rate;
620 aout_buffer_t *p_aout_buf;
622 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
624 vlc_mutex_lock( &p_input->p->counters.counters_lock );
625 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
626 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
628 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
630 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
631 p_dec->p_owner->p_aout_input, p_aout_buf );
635 if( p_dec->p_owner->i_preroll_end > 0 )
637 /* FIXME TODO flush audio output (don't know how to do that) */
638 msg_Dbg( p_dec, "End of audio preroll" );
639 p_dec->p_owner->i_preroll_end = -1;
641 aout_DecPlay( p_dec->p_owner->p_aout,
642 p_dec->p_owner->p_aout_input,
643 p_aout_buf, i_rate );
646 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
649 vlc_bool_t pb_present[4];
653 assert( p_dec_cc->pf_get_cc != NULL );
655 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
656 if( !p_dec->p_owner->b_cc_supported )
659 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
663 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
664 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
666 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
667 if( p_dec->p_owner->pp_cc[i] )
671 for( i = 0; i < 4; i++ )
673 if( !p_dec->p_owner->pp_cc[i] )
676 if( i_cc_decoder > 1 )
677 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
679 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
682 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
684 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
686 vlc_mutex_lock( &p_vout->picture_lock );
688 if( p_pic->i_status == READY_PICTURE )
690 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
693 else if( p_pic->i_refcount > 0 )
695 p_pic->i_status = DISPLAYED_PICTURE;
699 p_pic->i_status = DESTROYED_PICTURE;
700 p_vout->i_heap_size--;
703 vlc_mutex_unlock( &p_vout->picture_lock );
705 static void VoutFlushPicture( vout_thread_t *p_vout )
708 vlc_mutex_lock( &p_vout->picture_lock );
709 for( i = 0; i < p_vout->render.i_pictures; i++ )
711 picture_t *p_pic = p_vout->render.pp_picture[i];
713 if( p_pic->i_status == READY_PICTURE ||
714 p_pic->i_status == DISPLAYED_PICTURE )
716 /* We cannot change picture status if it is in READY_PICTURE state,
717 * Just make sure they won't be displayed */
721 vlc_mutex_unlock( &p_vout->picture_lock );
723 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
725 input_thread_t *p_input = p_dec->p_owner->p_input;
728 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
730 vlc_mutex_lock( &p_input->p->counters.counters_lock );
731 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
732 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
734 if( p_pic->date < p_dec->p_owner->i_preroll_end )
736 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
740 if( p_dec->p_owner->i_preroll_end > 0 )
742 msg_Dbg( p_dec, "End of video preroll" );
743 if( p_dec->p_owner->p_vout )
744 VoutFlushPicture( p_dec->p_owner->p_vout );
746 p_dec->p_owner->i_preroll_end = -1;
749 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
750 DecoderGetCc( p_dec, p_dec );
752 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
754 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
761 * \param p_dec the decoder object
762 * \param p_block the block to decode
763 * \return VLC_SUCCESS or an error code
765 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
767 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
768 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
770 if( p_block && p_block->i_buffer <= 0 )
772 block_Release( p_block );
776 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
778 block_t *p_sout_block;
780 while( ( p_sout_block =
781 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
783 if( !p_dec->p_owner->p_sout_input )
785 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
787 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
788 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
789 if( p_dec->fmt_in.psz_language )
791 if( p_dec->p_owner->sout.psz_language )
792 free( p_dec->p_owner->sout.psz_language );
793 p_dec->p_owner->sout.psz_language =
794 strdup( p_dec->fmt_in.psz_language );
797 p_dec->p_owner->p_sout_input =
798 sout_InputNew( p_dec->p_owner->p_sout,
799 &p_dec->p_owner->sout );
801 if( p_dec->p_owner->p_sout_input == NULL )
803 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
804 (char *)&p_dec->p_owner->sout.i_codec );
805 p_dec->b_error = VLC_TRUE;
807 while( p_sout_block )
809 block_t *p_next = p_sout_block->p_next;
810 block_Release( p_sout_block );
811 p_sout_block = p_next;
817 while( p_sout_block )
819 block_t *p_next = p_sout_block->p_next;
821 p_sout_block->p_next = NULL;
822 p_sout_block->i_rate = i_rate;
824 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
827 p_sout_block = p_next;
830 /* For now it's enough, as only sout inpact on this flag */
831 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
832 p_dec->p_owner->p_input->p->b_out_pace_control )
834 msg_Dbg( p_dec, "switching to sync mode" );
835 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
837 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
838 !p_dec->p_owner->p_input->p->b_out_pace_control )
840 msg_Dbg( p_dec, "switching to async mode" );
841 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
845 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
848 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
850 if( p_dec->p_owner->p_packetizer )
852 block_t *p_packetized_block;
853 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
855 while( (p_packetized_block =
856 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
858 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
860 es_format_Clean( &p_dec->fmt_in );
861 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
864 while( p_packetized_block )
866 block_t *p_next = p_packetized_block->p_next;
867 p_packetized_block->p_next = NULL;
868 p_packetized_block->i_rate = i_rate;
870 DecoderDecodeAudio( p_dec, p_packetized_block );
872 p_packetized_block = p_next;
878 DecoderDecodeAudio( p_dec, p_block );
881 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
884 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
886 if( p_dec->p_owner->p_packetizer )
888 block_t *p_packetized_block;
889 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
891 while( (p_packetized_block =
892 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
894 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
896 es_format_Clean( &p_dec->fmt_in );
897 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
899 if( p_packetizer->pf_get_cc )
900 DecoderGetCc( p_dec, p_packetizer );
902 while( p_packetized_block )
904 block_t *p_next = p_packetized_block->p_next;
905 p_packetized_block->p_next = NULL;
906 p_packetized_block->i_rate = i_rate;
908 DecoderDecodeVideo( p_dec, p_packetized_block );
910 p_packetized_block = p_next;
916 DecoderDecodeVideo( p_dec, p_block );
919 else if( p_dec->fmt_in.i_cat == SPU_ES )
921 input_thread_t *p_input = p_dec->p_owner->p_input;
922 vout_thread_t *p_vout;
926 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
928 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
930 vlc_mutex_lock( &p_input->p->counters.counters_lock );
931 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
932 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
934 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
935 if( p_vout && p_sys->p_spu_vout == p_vout )
937 /* Prerool does not work very well with subtitle */
938 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
939 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
940 spu_DestroySubpicture( p_vout->p_spu, p_spu );
942 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
946 msg_Warn( p_dec, "no vout found, leaking subpicture" );
949 vlc_object_release( p_vout );
954 msg_Err( p_dec, "unknown ES format" );
958 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
962 * Destroys a decoder object
964 * \param p_dec the decoder object
967 static void DeleteDecoder( decoder_t * p_dec )
969 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
970 (char*)&p_dec->fmt_in.i_codec,
971 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
973 /* Free all packets still in the decoder fifo. */
974 block_FifoEmpty( p_dec->p_owner->p_fifo );
975 block_FifoRelease( p_dec->p_owner->p_fifo );
978 if( p_dec->p_owner->p_aout_input )
979 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
981 if( p_dec->p_owner->p_vout )
985 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
986 /* Hack to make sure all the the pictures are freed by the decoder */
987 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
990 if( p_pic->i_status == RESERVED_PICTURE )
991 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
992 if( p_pic->i_refcount > 0 )
993 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
997 /* We are about to die. Reattach video output to p_vlc. */
998 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
1001 if( p_dec->p_owner->p_sout_input )
1003 sout_InputDelete( p_dec->p_owner->p_sout_input );
1004 es_format_Clean( &p_dec->p_owner->sout );
1007 if( p_dec->fmt_in.i_cat == SPU_ES )
1009 vout_thread_t *p_vout;
1011 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1014 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1015 p_dec->p_owner->i_spu_channel );
1016 vlc_object_release( p_vout );
1020 es_format_Clean( &p_dec->fmt_in );
1021 es_format_Clean( &p_dec->fmt_out );
1023 if( p_dec->p_owner->p_packetizer )
1025 module_Unneed( p_dec->p_owner->p_packetizer,
1026 p_dec->p_owner->p_packetizer->p_module );
1027 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1028 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1029 vlc_object_detach( p_dec->p_owner->p_packetizer );
1030 vlc_object_destroy( p_dec->p_owner->p_packetizer );
1033 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1035 vlc_object_detach( p_dec );
1037 free( p_dec->p_owner );
1040 /*****************************************************************************
1041 * Buffers allocation callbacks for the decoders
1042 *****************************************************************************/
1043 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1045 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1046 aout_buffer_t *p_buffer;
1048 if( p_sys->p_aout_input != NULL &&
1049 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1050 p_dec->fmt_out.audio.i_original_channels !=
1051 p_sys->audio.i_original_channels ||
1052 p_dec->fmt_out.audio.i_bytes_per_frame !=
1053 p_sys->audio.i_bytes_per_frame ) )
1055 /* Parameters changed, restart the aout */
1056 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1057 p_sys->p_aout_input = NULL;
1060 if( p_sys->p_aout_input == NULL )
1062 audio_sample_format_t format;
1063 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1065 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1066 p_sys->audio = p_dec->fmt_out.audio;
1068 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1069 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1070 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1072 if ( i_force_dolby == 1 )
1074 format.i_original_channels = format.i_original_channels |
1075 AOUT_CHAN_DOLBYSTEREO;
1077 else /* i_force_dolby == 2 */
1079 format.i_original_channels = format.i_original_channels &
1080 ~AOUT_CHAN_DOLBYSTEREO;
1084 p_sys->p_aout_input =
1085 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1086 if( p_sys->p_aout_input == NULL )
1088 msg_Err( p_dec, "failed to create audio output" );
1089 p_dec->b_error = VLC_TRUE;
1092 p_dec->fmt_out.audio.i_bytes_per_frame =
1093 p_sys->audio.i_bytes_per_frame;
1096 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
1102 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1104 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1105 p_dec->p_owner->p_aout_input, p_buffer );
1108 static picture_t *vout_new_buffer( decoder_t *p_dec )
1110 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1113 if( p_sys->p_vout == NULL ||
1114 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1115 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1116 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1117 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1119 if( !p_dec->fmt_out.video.i_width ||
1120 !p_dec->fmt_out.video.i_height )
1122 /* Can't create a new vout without display size */
1126 if( !p_dec->fmt_out.video.i_visible_width ||
1127 !p_dec->fmt_out.video.i_visible_height )
1129 if( p_dec->fmt_in.video.i_visible_width &&
1130 p_dec->fmt_in.video.i_visible_height )
1132 p_dec->fmt_out.video.i_visible_width =
1133 p_dec->fmt_in.video.i_visible_width;
1134 p_dec->fmt_out.video.i_visible_height =
1135 p_dec->fmt_in.video.i_visible_height;
1139 p_dec->fmt_out.video.i_visible_width =
1140 p_dec->fmt_out.video.i_width;
1141 p_dec->fmt_out.video.i_visible_height =
1142 p_dec->fmt_out.video.i_height;
1146 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1147 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1149 p_dec->fmt_out.video.i_visible_height = 1080;
1150 p_dec->fmt_out.video.i_sar_num *= 135;
1151 p_dec->fmt_out.video.i_sar_den *= 136;
1152 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1155 if( !p_dec->fmt_out.video.i_sar_num ||
1156 !p_dec->fmt_out.video.i_sar_den )
1158 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1159 p_dec->fmt_out.video.i_visible_height;
1161 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1162 p_dec->fmt_out.video.i_visible_width;
1165 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1166 &p_dec->fmt_out.video.i_sar_den,
1167 p_dec->fmt_out.video.i_sar_num,
1168 p_dec->fmt_out.video.i_sar_den, 50000 );
1170 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1171 p_sys->video = p_dec->fmt_out.video;
1173 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1174 &p_dec->fmt_out.video );
1175 if( p_sys->p_vout == NULL )
1177 msg_Err( p_dec, "failed to create video output" );
1178 p_dec->b_error = VLC_TRUE;
1182 if( p_sys->video.i_rmask )
1183 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1184 if( p_sys->video.i_gmask )
1185 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1186 if( p_sys->video.i_bmask )
1187 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1190 /* Get a new picture */
1191 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1193 int i_pic, i_ready_pic = 0;
1195 if( p_dec->b_die || p_dec->b_error )
1200 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1201 /* Check the decoder doesn't leak pictures */
1202 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1205 if( p_pic->i_status == READY_PICTURE )
1207 if( i_ready_pic++ > 0 ) break;
1211 if( p_pic->i_status != DISPLAYED_PICTURE &&
1212 p_pic->i_status != RESERVED_PICTURE &&
1213 p_pic->i_status != READY_PICTURE ) break;
1215 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1218 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1220 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1222 /* Just free all the pictures */
1223 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1226 if( p_pic->i_status == RESERVED_PICTURE )
1227 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1228 if( p_pic->i_refcount > 0 )
1229 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1234 msleep( VOUT_OUTMEM_SLEEP );
1240 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1242 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1245 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1247 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1250 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1252 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1255 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1257 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1258 vout_thread_t *p_vout = NULL;
1259 subpicture_t *p_subpic;
1260 int i_attempts = 30;
1262 while( i_attempts-- )
1264 if( p_dec->b_die || p_dec->b_error ) break;
1266 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1269 msleep( VOUT_DISPLAY_DELAY );
1274 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1278 if( p_sys->p_spu_vout != p_vout )
1280 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1281 &p_sys->i_spu_channel );
1282 p_sys->p_spu_vout = p_vout;
1285 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1288 p_subpic->i_channel = p_sys->i_spu_channel;
1291 vlc_object_release( p_vout );
1296 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1298 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1299 vout_thread_t *p_vout = NULL;
1301 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1302 if( !p_vout || p_sys->p_spu_vout != p_vout )
1305 vlc_object_release( p_vout );
1306 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1310 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1312 vlc_object_release( p_vout );