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 *****************************************************************************/
31 #include <vlc_block.h>
35 #include <vlc_codec.h>
38 #include <vlc_interface.h>
39 #include "audio_output/aout_internal.h"
40 #include "stream_output/stream_output.h"
41 #include "input_internal.h"
43 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
44 static void DeleteDecoder( decoder_t * );
46 static int DecoderThread( decoder_t * );
47 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
49 /* Buffers allocation callbacks for the decoders */
50 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
51 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
53 static picture_t *vout_new_buffer( decoder_t * );
54 static void vout_del_buffer( decoder_t *, picture_t * );
55 static void vout_link_picture( decoder_t *, picture_t * );
56 static void vout_unlink_picture( decoder_t *, picture_t * );
58 static subpicture_t *spu_new_buffer( decoder_t * );
59 static void spu_del_buffer( decoder_t *, subpicture_t * );
61 static es_format_t null_es_format;
63 struct decoder_owner_sys_t
65 vlc_bool_t b_own_thread;
67 int64_t i_preroll_end;
69 input_thread_t *p_input;
71 aout_instance_t *p_aout;
72 aout_input_t *p_aout_input;
74 vout_thread_t *p_vout;
76 vout_thread_t *p_spu_vout;
79 sout_instance_t *p_sout;
80 sout_packetizer_input_t *p_sout_input;
82 /* Some decoders require already packetized data (ie. not truncated) */
83 decoder_t *p_packetizer;
85 /* Current format in use by the output */
94 vlc_bool_t b_cc_supported;
96 vlc_bool_t pb_cc_present[4];
101 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
103 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
104 "VLC probably does not support this sound or video format.",
106 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module"),
107 _("VLC does not support the audio or video format \"%4.4s\". "
108 "Unfortunately there is no way for you to fix this."), (char*)&codec );
111 /* decoder_GetInputAttachment:
113 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
114 const char *psz_name )
116 input_attachment_t *p_attachment;
117 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
121 /* decoder_GetInputAttachments:
123 int decoder_GetInputAttachments( decoder_t *p_dec,
124 input_attachment_t ***ppp_attachment,
127 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
128 ppp_attachment, pi_attachment );
130 /* decoder_GetDisplayDate:
132 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
138 * Spawns a new decoder thread
140 * \param p_input the input thread
141 * \param p_es the es descriptor
142 * \return the spawned decoder object
144 decoder_t *input_DecoderNew( input_thread_t *p_input,
145 es_format_t *fmt, vlc_bool_t b_force_decoder )
147 decoder_t *p_dec = NULL;
150 /* If we are in sout mode, search for packetizer module */
151 if( p_input->p->p_sout && !b_force_decoder )
153 /* Create the decoder configuration structure */
154 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
157 msg_Err( p_input, "could not create packetizer" );
158 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
159 _("VLC could not open the packetizer module.") );
165 /* Create the decoder configuration structure */
166 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
169 msg_Err( p_input, "could not create decoder" );
170 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
171 _("VLC could not open the decoder module.") );
176 if( !p_dec->p_module )
178 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
180 DeleteDecoder( p_dec );
181 vlc_object_destroy( p_dec );
185 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
188 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
189 p_dec->p_owner->b_own_thread = VLC_FALSE;
193 var_Get( p_input, "minimize-threads", &val );
194 p_dec->p_owner->b_own_thread = !val.b_bool;
197 if( p_dec->p_owner->b_own_thread )
200 if( fmt->i_cat == AUDIO_ES )
201 i_priority = VLC_THREAD_PRIORITY_AUDIO;
203 i_priority = VLC_THREAD_PRIORITY_VIDEO;
205 /* Spawn the decoder thread */
206 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
207 i_priority, VLC_FALSE ) )
209 msg_Err( p_dec, "cannot spawn decoder thread" );
210 module_Unneed( p_dec, p_dec->p_module );
211 DeleteDecoder( p_dec );
212 vlc_object_destroy( p_dec );
221 * Kills a decoder thread and waits until it's finished
223 * \param p_input the input thread
224 * \param p_es the es descriptor
227 void input_DecoderDelete( decoder_t *p_dec )
229 vlc_object_kill( p_dec );
231 if( p_dec->p_owner->b_own_thread )
233 /* Make sure the thread leaves the function by
234 * sending it an empty block. */
235 block_t *p_block = block_New( p_dec, 0 );
236 input_DecoderDecode( p_dec, p_block );
238 vlc_thread_join( p_dec );
240 /* Don't module_Unneed() here because of the dll loader that wants
241 * close() in the same thread than open()/decode() */
246 input_DecoderDecode( p_dec, NULL );
248 module_Unneed( p_dec, p_dec->p_module );
252 if( p_dec->p_owner->b_cc_supported )
255 for( i = 0; i < 4; i++ )
256 input_DecoderSetCcState( p_dec, VLC_FALSE, i );
259 /* Delete decoder configuration */
260 DeleteDecoder( p_dec );
262 /* Delete the decoder */
263 vlc_object_destroy( p_dec );
267 * Put a block_t in the decoder's fifo.
269 * \param p_dec the decoder object
270 * \param p_block the data block
272 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
274 if( p_dec->p_owner->b_own_thread )
276 if( p_dec->p_owner->p_input->p->b_out_pace_control )
279 while( !p_dec->b_die && !p_dec->b_error &&
280 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
285 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
287 /* FIXME: ideally we would check the time amount of data
288 * in the fifo instead of its size. */
289 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
290 "consumed quickly enough), resetting fifo!" );
291 block_FifoEmpty( p_dec->p_owner->p_fifo );
294 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
298 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
300 if( p_block ) block_Release( p_block );
304 DecoderDecode( p_dec, p_block );
309 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
314 if( p_dec->p_owner->b_own_thread && b_flush )
315 block_FifoEmpty( p_dec->p_owner->p_fifo );
317 /* Send a special block */
318 p_null = block_New( p_dec, 128 );
319 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
320 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
321 if( p_dec->p_owner->p_packetizer && b_flush )
322 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
323 memset( p_null->p_buffer, 0, p_null->i_buffer );
325 input_DecoderDecode( p_dec, p_null );
328 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
330 if( p_dec->p_owner->b_own_thread
331 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
338 void input_DecoderIsCcPresent( decoder_t *p_dec, vlc_bool_t pb_present[4] )
342 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
343 for( i = 0; i < 4; i++ )
344 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
345 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
347 int input_DecoderSetCcState( decoder_t *p_dec, vlc_bool_t b_decode, int i_channel )
349 decoder_owner_sys_t *p_owner = p_dec->p_owner;
351 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
353 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
358 static const vlc_fourcc_t fcc[4] = {
359 VLC_FOURCC('c', 'c', '1', ' '),
360 VLC_FOURCC('c', 'c', '2', ' '),
361 VLC_FOURCC('c', 'c', '3', ' '),
362 VLC_FOURCC('c', 'c', '4', ' '),
367 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
368 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
371 msg_Err( p_dec, "could not create decoder" );
372 intf_UserFatal( p_dec, VLC_FALSE, _("Streaming / Transcoding failed"),
373 _("VLC could not open the decoder module.") );
376 else if( !p_cc->p_module )
378 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
379 DeleteDecoder( p_cc );
380 vlc_object_destroy( p_cc );
384 vlc_mutex_lock( &p_owner->lock_cc );
385 p_dec->p_owner->pp_cc[i_channel] = p_cc;
386 vlc_mutex_unlock( &p_owner->lock_cc );
392 vlc_mutex_lock( &p_owner->lock_cc );
393 p_cc = p_dec->p_owner->pp_cc[i_channel];
394 p_dec->p_owner->pp_cc[i_channel] = NULL;
395 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
399 vlc_object_kill( p_cc );
400 module_Unneed( p_cc, p_cc->p_module );
401 DeleteDecoder( p_cc );
402 vlc_object_destroy( p_cc );
407 int input_DecoderGetCcState( decoder_t *p_dec, vlc_bool_t *pb_decode, int i_channel )
409 decoder_owner_sys_t *p_owner = p_dec->p_owner;
411 *pb_decode = VLC_FALSE;
412 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
415 vlc_mutex_lock( &p_owner->lock_cc );
416 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
417 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
422 * Create a decoder object
424 * \param p_input the input thread
425 * \param p_es the es descriptor
426 * \param i_object_type Object type as define in include/vlc_objects.h
427 * \return the decoder object
429 static decoder_t * CreateDecoder( input_thread_t *p_input,
430 es_format_t *fmt, int i_object_type )
433 decoder_owner_sys_t *p_owner;
436 p_dec = vlc_object_create( p_input, i_object_type );
439 msg_Err( p_input, "out of memory" );
443 p_dec->pf_decode_audio = 0;
444 p_dec->pf_decode_video = 0;
445 p_dec->pf_decode_sub = 0;
446 p_dec->pf_get_cc = 0;
447 p_dec->pf_packetize = 0;
449 /* Initialize the decoder fifo */
450 p_dec->p_module = NULL;
452 memset( &null_es_format, 0, sizeof(es_format_t) );
453 es_format_Copy( &p_dec->fmt_in, fmt );
454 es_format_Copy( &p_dec->fmt_out, &null_es_format );
456 /* Allocate our private structure for the decoder */
457 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
458 if( p_dec->p_owner == NULL )
460 msg_Err( p_dec, "out of memory" );
463 p_dec->p_owner->b_own_thread = VLC_TRUE;
464 p_dec->p_owner->i_preroll_end = -1;
465 p_dec->p_owner->p_input = p_input;
466 p_dec->p_owner->p_aout = NULL;
467 p_dec->p_owner->p_aout_input = NULL;
468 p_dec->p_owner->p_vout = NULL;
469 p_dec->p_owner->p_spu_vout = NULL;
470 p_dec->p_owner->i_spu_channel = 0;
471 p_dec->p_owner->p_sout = p_input->p->p_sout;
472 p_dec->p_owner->p_sout_input = NULL;
473 p_dec->p_owner->p_packetizer = NULL;
476 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
478 msg_Err( p_dec, "out of memory" );
482 /* Set buffers allocation callbacks for the decoders */
483 p_dec->pf_aout_buffer_new = aout_new_buffer;
484 p_dec->pf_aout_buffer_del = aout_del_buffer;
485 p_dec->pf_vout_buffer_new = vout_new_buffer;
486 p_dec->pf_vout_buffer_del = vout_del_buffer;
487 p_dec->pf_picture_link = vout_link_picture;
488 p_dec->pf_picture_unlink = vout_unlink_picture;
489 p_dec->pf_spu_buffer_new = spu_new_buffer;
490 p_dec->pf_spu_buffer_del = spu_del_buffer;
492 vlc_object_attach( p_dec, p_input );
494 /* Find a suitable decoder/packetizer module */
495 if( i_object_type == VLC_OBJECT_DECODER )
496 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
498 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
500 /* Check if decoder requires already packetized data */
501 if( i_object_type == VLC_OBJECT_DECODER &&
502 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
504 p_dec->p_owner->p_packetizer =
505 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
506 if( p_dec->p_owner->p_packetizer )
508 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
511 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
514 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
516 p_dec->p_owner->p_packetizer->p_module =
517 module_Need( p_dec->p_owner->p_packetizer,
518 "packetizer", "$packetizer", 0 );
520 if( !p_dec->p_owner->p_packetizer->p_module )
522 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
523 vlc_object_detach( p_dec->p_owner->p_packetizer );
524 vlc_object_destroy( p_dec->p_owner->p_packetizer );
529 /* Copy ourself the input replay gain */
530 if( fmt->i_cat == AUDIO_ES )
532 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
534 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
536 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
537 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
539 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
541 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
542 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
547 p_owner->b_cc_supported = VLC_FALSE;
548 if( i_object_type == VLC_OBJECT_DECODER )
550 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
551 p_owner->b_cc_supported = VLC_TRUE;
552 if( p_dec->pf_get_cc )
553 p_owner->b_cc_supported = VLC_TRUE;
556 vlc_mutex_init( p_dec, &p_owner->lock_cc );
557 for( i = 0; i < 4; i++ )
559 p_owner->pb_cc_present[i] = VLC_FALSE;
560 p_owner->pp_cc[i] = NULL;
566 * The decoding main loop
568 * \param p_dec the decoder
571 static int DecoderThread( decoder_t * p_dec )
575 /* The decoder's main loop */
576 while( !p_dec->b_die && !p_dec->b_error )
578 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
583 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
589 while( !p_dec->b_die )
591 /* Trash all received PES packets */
592 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
593 if( p_block ) block_Release( p_block );
596 /* We do it here because of the dll loader that wants close() in the
597 * same thread than open()/decode() */
598 module_Unneed( p_dec, p_dec->p_module );
603 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
605 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
606 *pi_preroll = INT64_MAX;
607 else if( p->i_pts > 0 )
608 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
609 else if( p->i_dts > 0 )
610 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
612 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
614 input_thread_t *p_input = p_dec->p_owner->p_input;
615 const int i_rate = p_block->i_rate;
616 aout_buffer_t *p_aout_buf;
618 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
620 vlc_mutex_lock( &p_input->p->counters.counters_lock );
621 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
622 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
624 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
626 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
627 p_dec->p_owner->p_aout_input, p_aout_buf );
631 if( p_dec->p_owner->i_preroll_end > 0 )
633 /* FIXME TODO flush audio output (don't know how to do that) */
634 msg_Dbg( p_dec, "End of audio preroll" );
635 p_dec->p_owner->i_preroll_end = -1;
637 aout_DecPlay( p_dec->p_owner->p_aout,
638 p_dec->p_owner->p_aout_input,
639 p_aout_buf, i_rate );
642 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
645 vlc_bool_t pb_present[4];
649 assert( p_dec_cc->pf_get_cc != NULL );
651 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
652 if( !p_dec->p_owner->b_cc_supported )
655 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
659 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
660 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
662 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
663 if( p_dec->p_owner->pp_cc[i] )
667 for( i = 0; i < 4; i++ )
669 if( !p_dec->p_owner->pp_cc[i] )
672 if( i_cc_decoder > 1 )
673 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
675 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
678 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
680 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
682 vlc_mutex_lock( &p_vout->picture_lock );
684 if( p_pic->i_status == READY_PICTURE )
686 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
689 else if( p_pic->i_refcount > 0 )
691 p_pic->i_status = DISPLAYED_PICTURE;
695 p_pic->i_status = DESTROYED_PICTURE;
696 p_vout->i_heap_size--;
699 vlc_mutex_unlock( &p_vout->picture_lock );
701 static void VoutFlushPicture( vout_thread_t *p_vout )
704 vlc_mutex_lock( &p_vout->picture_lock );
705 for( i = 0; i < p_vout->render.i_pictures; i++ )
707 picture_t *p_pic = p_vout->render.pp_picture[i];
709 if( p_pic->i_status == READY_PICTURE ||
710 p_pic->i_status == DISPLAYED_PICTURE )
712 /* We cannot change picture status if it is in READY_PICTURE state,
713 * Just make sure they won't be displayed */
717 vlc_mutex_unlock( &p_vout->picture_lock );
719 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
721 input_thread_t *p_input = p_dec->p_owner->p_input;
724 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
726 vlc_mutex_lock( &p_input->p->counters.counters_lock );
727 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
728 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
730 if( p_pic->date < p_dec->p_owner->i_preroll_end )
732 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
736 if( p_dec->p_owner->i_preroll_end > 0 )
738 msg_Dbg( p_dec, "End of video preroll" );
739 if( p_dec->p_owner->p_vout )
740 VoutFlushPicture( p_dec->p_owner->p_vout );
742 p_dec->p_owner->i_preroll_end = -1;
745 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
746 DecoderGetCc( p_dec, p_dec );
748 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
750 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
757 * \param p_dec the decoder object
758 * \param p_block the block to decode
759 * \return VLC_SUCCESS or an error code
761 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
763 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
764 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
766 if( p_block && p_block->i_buffer <= 0 )
768 block_Release( p_block );
772 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
774 block_t *p_sout_block;
776 while( ( p_sout_block =
777 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
779 if( !p_dec->p_owner->p_sout_input )
781 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
783 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
784 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
785 if( p_dec->fmt_in.psz_language )
787 if( p_dec->p_owner->sout.psz_language )
788 free( p_dec->p_owner->sout.psz_language );
789 p_dec->p_owner->sout.psz_language =
790 strdup( p_dec->fmt_in.psz_language );
793 p_dec->p_owner->p_sout_input =
794 sout_InputNew( p_dec->p_owner->p_sout,
795 &p_dec->p_owner->sout );
797 if( p_dec->p_owner->p_sout_input == NULL )
799 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
800 (char *)&p_dec->p_owner->sout.i_codec );
801 p_dec->b_error = VLC_TRUE;
803 while( p_sout_block )
805 block_t *p_next = p_sout_block->p_next;
806 block_Release( p_sout_block );
807 p_sout_block = p_next;
813 while( p_sout_block )
815 block_t *p_next = p_sout_block->p_next;
817 p_sout_block->p_next = NULL;
818 p_sout_block->i_rate = i_rate;
820 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
823 p_sout_block = p_next;
826 /* For now it's enough, as only sout inpact on this flag */
827 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
828 p_dec->p_owner->p_input->p->b_out_pace_control )
830 msg_Dbg( p_dec, "switching to sync mode" );
831 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
833 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
834 !p_dec->p_owner->p_input->p->b_out_pace_control )
836 msg_Dbg( p_dec, "switching to async mode" );
837 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
841 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
844 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
846 if( p_dec->p_owner->p_packetizer )
848 block_t *p_packetized_block;
849 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
851 while( (p_packetized_block =
852 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
854 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
856 es_format_Clean( &p_dec->fmt_in );
857 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
860 while( p_packetized_block )
862 block_t *p_next = p_packetized_block->p_next;
863 p_packetized_block->p_next = NULL;
864 p_packetized_block->i_rate = i_rate;
866 DecoderDecodeAudio( p_dec, p_packetized_block );
868 p_packetized_block = p_next;
874 DecoderDecodeAudio( p_dec, p_block );
877 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
880 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
882 if( p_dec->p_owner->p_packetizer )
884 block_t *p_packetized_block;
885 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
887 while( (p_packetized_block =
888 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
890 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
892 es_format_Clean( &p_dec->fmt_in );
893 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
895 if( p_packetizer->pf_get_cc )
896 DecoderGetCc( p_dec, p_packetizer );
898 while( p_packetized_block )
900 block_t *p_next = p_packetized_block->p_next;
901 p_packetized_block->p_next = NULL;
902 p_packetized_block->i_rate = i_rate;
904 DecoderDecodeVideo( p_dec, p_packetized_block );
906 p_packetized_block = p_next;
912 DecoderDecodeVideo( p_dec, p_block );
915 else if( p_dec->fmt_in.i_cat == SPU_ES )
917 input_thread_t *p_input = p_dec->p_owner->p_input;
918 vout_thread_t *p_vout;
922 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
924 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
926 vlc_mutex_lock( &p_input->p->counters.counters_lock );
927 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
928 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
930 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
931 if( p_vout && p_sys->p_spu_vout == p_vout )
933 /* Prerool does not work very well with subtitle */
934 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
935 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
936 spu_DestroySubpicture( p_vout->p_spu, p_spu );
938 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
942 msg_Warn( p_dec, "no vout found, leaking subpicture" );
945 vlc_object_release( p_vout );
950 msg_Err( p_dec, "unknown ES format" );
954 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
958 * Destroys a decoder object
960 * \param p_dec the decoder object
963 static void DeleteDecoder( decoder_t * p_dec )
965 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
966 (char*)&p_dec->fmt_in.i_codec,
967 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
969 /* Free all packets still in the decoder fifo. */
970 block_FifoEmpty( p_dec->p_owner->p_fifo );
971 block_FifoRelease( p_dec->p_owner->p_fifo );
974 if( p_dec->p_owner->p_aout_input )
975 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
977 if( p_dec->p_owner->p_vout )
981 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
982 /* Hack to make sure all the the pictures are freed by the decoder */
983 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
986 if( p_pic->i_status == RESERVED_PICTURE )
987 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
988 if( p_pic->i_refcount > 0 )
989 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
993 /* We are about to die. Reattach video output to p_vlc. */
994 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
997 if( p_dec->p_owner->p_sout_input )
999 sout_InputDelete( p_dec->p_owner->p_sout_input );
1000 es_format_Clean( &p_dec->p_owner->sout );
1003 if( p_dec->fmt_in.i_cat == SPU_ES )
1005 vout_thread_t *p_vout;
1007 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1010 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1011 p_dec->p_owner->i_spu_channel );
1012 vlc_object_release( p_vout );
1016 es_format_Clean( &p_dec->fmt_in );
1017 es_format_Clean( &p_dec->fmt_out );
1019 if( p_dec->p_owner->p_packetizer )
1021 module_Unneed( p_dec->p_owner->p_packetizer,
1022 p_dec->p_owner->p_packetizer->p_module );
1023 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1024 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1025 vlc_object_detach( p_dec->p_owner->p_packetizer );
1026 vlc_object_destroy( p_dec->p_owner->p_packetizer );
1029 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1031 vlc_object_detach( p_dec );
1033 free( p_dec->p_owner );
1036 /*****************************************************************************
1037 * Buffers allocation callbacks for the decoders
1038 *****************************************************************************/
1039 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1041 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1042 aout_buffer_t *p_buffer;
1044 if( p_sys->p_aout_input != NULL &&
1045 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1046 p_dec->fmt_out.audio.i_original_channels !=
1047 p_sys->audio.i_original_channels ||
1048 p_dec->fmt_out.audio.i_bytes_per_frame !=
1049 p_sys->audio.i_bytes_per_frame ) )
1051 /* Parameters changed, restart the aout */
1052 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1053 p_sys->p_aout_input = NULL;
1056 if( p_sys->p_aout_input == NULL )
1058 audio_sample_format_t format;
1059 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1061 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1062 p_sys->audio = p_dec->fmt_out.audio;
1064 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1065 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1066 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1068 if ( i_force_dolby == 1 )
1070 format.i_original_channels = format.i_original_channels |
1071 AOUT_CHAN_DOLBYSTEREO;
1073 else /* i_force_dolby == 2 */
1075 format.i_original_channels = format.i_original_channels &
1076 ~AOUT_CHAN_DOLBYSTEREO;
1080 p_sys->p_aout_input =
1081 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1082 if( p_sys->p_aout_input == NULL )
1084 msg_Err( p_dec, "failed to create audio output" );
1085 p_dec->b_error = VLC_TRUE;
1088 p_dec->fmt_out.audio.i_bytes_per_frame =
1089 p_sys->audio.i_bytes_per_frame;
1092 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
1098 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1100 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1101 p_dec->p_owner->p_aout_input, p_buffer );
1104 static picture_t *vout_new_buffer( decoder_t *p_dec )
1106 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1109 if( p_sys->p_vout == NULL ||
1110 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1111 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1112 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1113 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1115 if( !p_dec->fmt_out.video.i_width ||
1116 !p_dec->fmt_out.video.i_height )
1118 /* Can't create a new vout without display size */
1122 if( !p_dec->fmt_out.video.i_visible_width ||
1123 !p_dec->fmt_out.video.i_visible_height )
1125 if( p_dec->fmt_in.video.i_visible_width &&
1126 p_dec->fmt_in.video.i_visible_height )
1128 p_dec->fmt_out.video.i_visible_width =
1129 p_dec->fmt_in.video.i_visible_width;
1130 p_dec->fmt_out.video.i_visible_height =
1131 p_dec->fmt_in.video.i_visible_height;
1135 p_dec->fmt_out.video.i_visible_width =
1136 p_dec->fmt_out.video.i_width;
1137 p_dec->fmt_out.video.i_visible_height =
1138 p_dec->fmt_out.video.i_height;
1142 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1143 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1145 p_dec->fmt_out.video.i_visible_height = 1080;
1146 p_dec->fmt_out.video.i_sar_num *= 135;
1147 p_dec->fmt_out.video.i_sar_den *= 136;
1148 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1151 if( !p_dec->fmt_out.video.i_sar_num ||
1152 !p_dec->fmt_out.video.i_sar_den )
1154 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1155 p_dec->fmt_out.video.i_visible_height;
1157 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1158 p_dec->fmt_out.video.i_visible_width;
1161 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1162 &p_dec->fmt_out.video.i_sar_den,
1163 p_dec->fmt_out.video.i_sar_num,
1164 p_dec->fmt_out.video.i_sar_den, 50000 );
1166 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1167 p_sys->video = p_dec->fmt_out.video;
1169 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1170 &p_dec->fmt_out.video );
1171 if( p_sys->p_vout == NULL )
1173 msg_Err( p_dec, "failed to create video output" );
1174 p_dec->b_error = VLC_TRUE;
1178 if( p_sys->video.i_rmask )
1179 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1180 if( p_sys->video.i_gmask )
1181 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1182 if( p_sys->video.i_bmask )
1183 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1186 /* Get a new picture */
1187 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1189 int i_pic, i_ready_pic = 0;
1191 if( p_dec->b_die || p_dec->b_error )
1196 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1197 /* Check the decoder doesn't leak pictures */
1198 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1201 if( p_pic->i_status == READY_PICTURE )
1203 if( i_ready_pic++ > 0 ) break;
1207 if( p_pic->i_status != DISPLAYED_PICTURE &&
1208 p_pic->i_status != RESERVED_PICTURE &&
1209 p_pic->i_status != READY_PICTURE ) break;
1211 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1214 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1216 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1218 /* Just free all the pictures */
1219 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1222 if( p_pic->i_status == RESERVED_PICTURE )
1223 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1224 if( p_pic->i_refcount > 0 )
1225 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1230 msleep( VOUT_OUTMEM_SLEEP );
1236 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1238 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1241 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1243 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1246 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1248 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1251 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1253 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1254 vout_thread_t *p_vout = NULL;
1255 subpicture_t *p_subpic;
1256 int i_attempts = 30;
1258 while( i_attempts-- )
1260 if( p_dec->b_die || p_dec->b_error ) break;
1262 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1265 msleep( VOUT_DISPLAY_DELAY );
1270 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1274 if( p_sys->p_spu_vout != p_vout )
1276 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1277 &p_sys->i_spu_channel );
1278 p_sys->p_spu_vout = p_vout;
1281 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1284 p_subpic->i_channel = p_sys->i_spu_channel;
1287 vlc_object_release( p_vout );
1292 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1294 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1295 vout_thread_t *p_vout = NULL;
1297 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1298 if( !p_vout || p_sys->p_spu_vout != p_vout )
1301 vlc_object_release( p_vout );
1302 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1306 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1308 vlc_object_release( p_vout );