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 *****************************************************************************/
36 #include <vlc_block.h>
40 #include <vlc_codec.h>
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
48 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
49 static void DeleteDecoder( decoder_t * );
51 static int DecoderThread( decoder_t * );
52 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
54 /* Buffers allocation callbacks for the decoders */
55 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
56 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
58 static picture_t *vout_new_buffer( decoder_t * );
59 static void vout_del_buffer( decoder_t *, picture_t * );
60 static void vout_link_picture( decoder_t *, picture_t * );
61 static void vout_unlink_picture( decoder_t *, picture_t * );
63 static subpicture_t *spu_new_buffer( decoder_t * );
64 static void spu_del_buffer( decoder_t *, subpicture_t * );
66 static es_format_t null_es_format;
68 struct decoder_owner_sys_t
70 vlc_bool_t b_own_thread;
72 int64_t i_preroll_end;
74 input_thread_t *p_input;
76 aout_instance_t *p_aout;
77 aout_input_t *p_aout_input;
79 vout_thread_t *p_vout;
81 vout_thread_t *p_spu_vout;
84 sout_instance_t *p_sout;
85 sout_packetizer_input_t *p_sout_input;
87 /* Some decoders require already packetized data (ie. not truncated) */
88 decoder_t *p_packetizer;
90 /* Current format in use by the output */
99 vlc_bool_t b_cc_supported;
101 vlc_bool_t pb_cc_present[4];
106 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
108 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
109 "VLC probably does not support this sound or video format.",
111 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module"),
112 _("VLC does not support the audio or video format \"%4.4s\". "
113 "Unfortunately there is no way for you to fix this."), (char*)&codec );
116 /* decoder_GetInputAttachment:
118 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
119 const char *psz_name )
121 input_attachment_t *p_attachment;
122 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
126 /* decoder_GetInputAttachments:
128 int decoder_GetInputAttachments( decoder_t *p_dec,
129 input_attachment_t ***ppp_attachment,
132 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
133 ppp_attachment, pi_attachment );
135 /* decoder_GetDisplayDate:
137 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
143 * Spawns a new decoder thread
145 * \param p_input the input thread
146 * \param p_es the es descriptor
147 * \return the spawned decoder object
149 decoder_t *input_DecoderNew( input_thread_t *p_input,
150 es_format_t *fmt, vlc_bool_t b_force_decoder )
152 decoder_t *p_dec = NULL;
155 /* If we are in sout mode, search for packetizer module */
156 if( p_input->p->p_sout && !b_force_decoder )
158 /* Create the decoder configuration structure */
159 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
162 msg_Err( p_input, "could not create packetizer" );
163 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
164 _("VLC could not open the packetizer module.") );
170 /* Create the decoder configuration structure */
171 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
174 msg_Err( p_input, "could not create decoder" );
175 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
176 _("VLC could not open the decoder module.") );
181 if( !p_dec->p_module )
183 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
185 DeleteDecoder( p_dec );
186 vlc_object_release( p_dec );
190 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
193 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
194 p_dec->p_owner->b_own_thread = VLC_FALSE;
198 var_Get( p_input, "minimize-threads", &val );
199 p_dec->p_owner->b_own_thread = !val.b_bool;
202 if( p_dec->p_owner->b_own_thread )
205 if( fmt->i_cat == AUDIO_ES )
206 i_priority = VLC_THREAD_PRIORITY_AUDIO;
208 i_priority = VLC_THREAD_PRIORITY_VIDEO;
210 /* Spawn the decoder thread */
211 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
212 i_priority, VLC_FALSE ) )
214 msg_Err( p_dec, "cannot spawn decoder thread" );
215 module_Unneed( p_dec, p_dec->p_module );
216 DeleteDecoder( p_dec );
217 vlc_object_release( p_dec );
226 * Kills a decoder thread and waits until it's finished
228 * \param p_input the input thread
229 * \param p_es the es descriptor
232 void input_DecoderDelete( decoder_t *p_dec )
234 vlc_object_kill( p_dec );
236 if( p_dec->p_owner->b_own_thread )
238 /* Make sure the thread leaves the function by
239 * sending it an empty block. */
240 block_t *p_block = block_New( p_dec, 0 );
241 input_DecoderDecode( p_dec, p_block );
243 vlc_thread_join( p_dec );
245 /* Don't module_Unneed() here because of the dll loader that wants
246 * close() in the same thread than open()/decode() */
251 input_DecoderDecode( p_dec, NULL );
253 module_Unneed( p_dec, p_dec->p_module );
257 if( p_dec->p_owner->b_cc_supported )
260 for( i = 0; i < 4; i++ )
261 input_DecoderSetCcState( p_dec, VLC_FALSE, i );
264 /* Delete decoder configuration */
265 DeleteDecoder( p_dec );
267 /* Delete the decoder */
268 vlc_object_release( p_dec );
272 * Put a block_t in the decoder's fifo.
274 * \param p_dec the decoder object
275 * \param p_block the data block
277 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
279 if( p_dec->p_owner->b_own_thread )
281 if( p_dec->p_owner->p_input->p->b_out_pace_control )
284 while( !p_dec->b_die && !p_dec->b_error &&
285 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
290 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
292 /* FIXME: ideally we would check the time amount of data
293 * in the fifo instead of its size. */
294 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
295 "consumed quickly enough), resetting fifo!" );
296 block_FifoEmpty( p_dec->p_owner->p_fifo );
299 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
303 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
305 if( p_block ) block_Release( p_block );
309 DecoderDecode( p_dec, p_block );
314 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
319 if( p_dec->p_owner->b_own_thread && b_flush )
320 block_FifoEmpty( p_dec->p_owner->p_fifo );
322 /* Send a special block */
323 p_null = block_New( p_dec, 128 );
324 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
325 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
326 if( p_dec->p_owner->p_packetizer && b_flush )
327 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
328 memset( p_null->p_buffer, 0, p_null->i_buffer );
330 input_DecoderDecode( p_dec, p_null );
333 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
335 if( p_dec->p_owner->b_own_thread
336 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
343 void input_DecoderIsCcPresent( decoder_t *p_dec, vlc_bool_t pb_present[4] )
347 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
348 for( i = 0; i < 4; i++ )
349 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
350 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
352 int input_DecoderSetCcState( decoder_t *p_dec, vlc_bool_t b_decode, int i_channel )
354 decoder_owner_sys_t *p_owner = p_dec->p_owner;
356 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
358 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
363 static const vlc_fourcc_t fcc[4] = {
364 VLC_FOURCC('c', 'c', '1', ' '),
365 VLC_FOURCC('c', 'c', '2', ' '),
366 VLC_FOURCC('c', 'c', '3', ' '),
367 VLC_FOURCC('c', 'c', '4', ' '),
372 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
373 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
376 msg_Err( p_dec, "could not create decoder" );
377 intf_UserFatal( p_dec, VLC_FALSE, _("Streaming / Transcoding failed"),
378 _("VLC could not open the decoder module.") );
381 else if( !p_cc->p_module )
383 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
384 DeleteDecoder( p_cc );
385 vlc_object_release( p_cc );
389 vlc_mutex_lock( &p_owner->lock_cc );
390 p_dec->p_owner->pp_cc[i_channel] = p_cc;
391 vlc_mutex_unlock( &p_owner->lock_cc );
397 vlc_mutex_lock( &p_owner->lock_cc );
398 p_cc = p_dec->p_owner->pp_cc[i_channel];
399 p_dec->p_owner->pp_cc[i_channel] = NULL;
400 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
404 vlc_object_kill( p_cc );
405 module_Unneed( p_cc, p_cc->p_module );
406 DeleteDecoder( p_cc );
407 vlc_object_release( p_cc );
412 int input_DecoderGetCcState( decoder_t *p_dec, vlc_bool_t *pb_decode, int i_channel )
414 decoder_owner_sys_t *p_owner = p_dec->p_owner;
416 *pb_decode = VLC_FALSE;
417 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
420 vlc_mutex_lock( &p_owner->lock_cc );
421 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
422 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
427 * Create a decoder object
429 * \param p_input the input thread
430 * \param p_es the es descriptor
431 * \param i_object_type Object type as define in include/vlc_objects.h
432 * \return the decoder object
434 static decoder_t * CreateDecoder( input_thread_t *p_input,
435 es_format_t *fmt, int i_object_type )
438 decoder_owner_sys_t *p_owner;
441 p_dec = vlc_object_create( p_input, i_object_type );
444 msg_Err( p_input, "out of memory" );
448 p_dec->pf_decode_audio = 0;
449 p_dec->pf_decode_video = 0;
450 p_dec->pf_decode_sub = 0;
451 p_dec->pf_get_cc = 0;
452 p_dec->pf_packetize = 0;
454 /* Initialize the decoder fifo */
455 p_dec->p_module = NULL;
457 memset( &null_es_format, 0, sizeof(es_format_t) );
458 es_format_Copy( &p_dec->fmt_in, fmt );
459 es_format_Copy( &p_dec->fmt_out, &null_es_format );
461 /* Allocate our private structure for the decoder */
462 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
463 if( p_dec->p_owner == NULL )
465 msg_Err( p_dec, "out of memory" );
468 p_dec->p_owner->b_own_thread = VLC_TRUE;
469 p_dec->p_owner->i_preroll_end = -1;
470 p_dec->p_owner->p_input = p_input;
471 p_dec->p_owner->p_aout = NULL;
472 p_dec->p_owner->p_aout_input = NULL;
473 p_dec->p_owner->p_vout = NULL;
474 p_dec->p_owner->p_spu_vout = NULL;
475 p_dec->p_owner->i_spu_channel = 0;
476 p_dec->p_owner->p_sout = p_input->p->p_sout;
477 p_dec->p_owner->p_sout_input = NULL;
478 p_dec->p_owner->p_packetizer = NULL;
481 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
483 msg_Err( p_dec, "out of memory" );
487 /* Set buffers allocation callbacks for the decoders */
488 p_dec->pf_aout_buffer_new = aout_new_buffer;
489 p_dec->pf_aout_buffer_del = aout_del_buffer;
490 p_dec->pf_vout_buffer_new = vout_new_buffer;
491 p_dec->pf_vout_buffer_del = vout_del_buffer;
492 p_dec->pf_picture_link = vout_link_picture;
493 p_dec->pf_picture_unlink = vout_unlink_picture;
494 p_dec->pf_spu_buffer_new = spu_new_buffer;
495 p_dec->pf_spu_buffer_del = spu_del_buffer;
497 vlc_object_attach( p_dec, p_input );
499 /* Find a suitable decoder/packetizer module */
500 if( i_object_type == VLC_OBJECT_DECODER )
501 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
503 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
505 /* Check if decoder requires already packetized data */
506 if( i_object_type == VLC_OBJECT_DECODER &&
507 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
509 p_dec->p_owner->p_packetizer =
510 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
511 if( p_dec->p_owner->p_packetizer )
513 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
516 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
519 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
521 p_dec->p_owner->p_packetizer->p_module =
522 module_Need( p_dec->p_owner->p_packetizer,
523 "packetizer", "$packetizer", 0 );
525 if( !p_dec->p_owner->p_packetizer->p_module )
527 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
528 vlc_object_detach( p_dec->p_owner->p_packetizer );
529 vlc_object_release( p_dec->p_owner->p_packetizer );
534 /* Copy ourself the input replay gain */
535 if( fmt->i_cat == AUDIO_ES )
537 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
539 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
541 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
542 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
544 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
546 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
547 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
552 p_owner->b_cc_supported = VLC_FALSE;
553 if( i_object_type == VLC_OBJECT_DECODER )
555 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
556 p_owner->b_cc_supported = VLC_TRUE;
557 if( p_dec->pf_get_cc )
558 p_owner->b_cc_supported = VLC_TRUE;
561 vlc_mutex_init( p_dec, &p_owner->lock_cc );
562 for( i = 0; i < 4; i++ )
564 p_owner->pb_cc_present[i] = VLC_FALSE;
565 p_owner->pp_cc[i] = NULL;
571 * The decoding main loop
573 * \param p_dec the decoder
576 static int DecoderThread( decoder_t * p_dec )
580 /* The decoder's main loop */
581 while( !p_dec->b_die && !p_dec->b_error )
583 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
588 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
594 while( !p_dec->b_die )
596 /* Trash all received PES packets */
597 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
598 if( p_block ) block_Release( p_block );
601 /* We do it here because of the dll loader that wants close() in the
602 * same thread than open()/decode() */
603 module_Unneed( p_dec, p_dec->p_module );
608 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
610 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
611 *pi_preroll = INT64_MAX;
612 else if( p->i_pts > 0 )
613 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
614 else if( p->i_dts > 0 )
615 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
617 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
619 input_thread_t *p_input = p_dec->p_owner->p_input;
620 const int i_rate = p_block->i_rate;
621 aout_buffer_t *p_aout_buf;
623 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
625 vlc_mutex_lock( &p_input->p->counters.counters_lock );
626 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
627 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
629 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
631 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
632 p_dec->p_owner->p_aout_input, p_aout_buf );
636 if( p_dec->p_owner->i_preroll_end > 0 )
638 /* FIXME TODO flush audio output (don't know how to do that) */
639 msg_Dbg( p_dec, "End of audio preroll" );
640 p_dec->p_owner->i_preroll_end = -1;
642 aout_DecPlay( p_dec->p_owner->p_aout,
643 p_dec->p_owner->p_aout_input,
644 p_aout_buf, i_rate );
647 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
650 vlc_bool_t pb_present[4];
654 assert( p_dec_cc->pf_get_cc != NULL );
656 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
657 if( !p_dec->p_owner->b_cc_supported )
660 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
664 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
665 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
667 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
668 if( p_dec->p_owner->pp_cc[i] )
672 for( i = 0; i < 4; i++ )
674 if( !p_dec->p_owner->pp_cc[i] )
677 if( i_cc_decoder > 1 )
678 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
680 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
683 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
685 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
687 vlc_mutex_lock( &p_vout->picture_lock );
689 if( p_pic->i_status == READY_PICTURE )
691 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
694 else if( p_pic->i_refcount > 0 )
696 p_pic->i_status = DISPLAYED_PICTURE;
700 p_pic->i_status = DESTROYED_PICTURE;
701 p_vout->i_heap_size--;
704 vlc_mutex_unlock( &p_vout->picture_lock );
706 static void VoutFlushPicture( vout_thread_t *p_vout )
709 vlc_mutex_lock( &p_vout->picture_lock );
710 for( i = 0; i < p_vout->render.i_pictures; i++ )
712 picture_t *p_pic = p_vout->render.pp_picture[i];
714 if( p_pic->i_status == READY_PICTURE ||
715 p_pic->i_status == DISPLAYED_PICTURE )
717 /* We cannot change picture status if it is in READY_PICTURE state,
718 * Just make sure they won't be displayed */
722 vlc_mutex_unlock( &p_vout->picture_lock );
724 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
726 input_thread_t *p_input = p_dec->p_owner->p_input;
729 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
731 vlc_mutex_lock( &p_input->p->counters.counters_lock );
732 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
733 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
735 if( p_pic->date < p_dec->p_owner->i_preroll_end )
737 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
741 if( p_dec->p_owner->i_preroll_end > 0 )
743 msg_Dbg( p_dec, "End of video preroll" );
744 if( p_dec->p_owner->p_vout )
745 VoutFlushPicture( p_dec->p_owner->p_vout );
747 p_dec->p_owner->i_preroll_end = -1;
750 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
751 DecoderGetCc( p_dec, p_dec );
753 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
755 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
762 * \param p_dec the decoder object
763 * \param p_block the block to decode
764 * \return VLC_SUCCESS or an error code
766 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
768 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
769 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
771 if( p_block && p_block->i_buffer <= 0 )
773 block_Release( p_block );
777 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
779 block_t *p_sout_block;
781 while( ( p_sout_block =
782 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
784 if( !p_dec->p_owner->p_sout_input )
786 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
788 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
789 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
790 if( p_dec->fmt_in.psz_language )
792 if( p_dec->p_owner->sout.psz_language )
793 free( p_dec->p_owner->sout.psz_language );
794 p_dec->p_owner->sout.psz_language =
795 strdup( p_dec->fmt_in.psz_language );
798 p_dec->p_owner->p_sout_input =
799 sout_InputNew( p_dec->p_owner->p_sout,
800 &p_dec->p_owner->sout );
802 if( p_dec->p_owner->p_sout_input == NULL )
804 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
805 (char *)&p_dec->p_owner->sout.i_codec );
806 p_dec->b_error = VLC_TRUE;
808 while( p_sout_block )
810 block_t *p_next = p_sout_block->p_next;
811 block_Release( p_sout_block );
812 p_sout_block = p_next;
818 while( p_sout_block )
820 block_t *p_next = p_sout_block->p_next;
822 p_sout_block->p_next = NULL;
823 p_sout_block->i_rate = i_rate;
825 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
828 p_sout_block = p_next;
831 /* For now it's enough, as only sout inpact on this flag */
832 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
833 p_dec->p_owner->p_input->p->b_out_pace_control )
835 msg_Dbg( p_dec, "switching to sync mode" );
836 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
838 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
839 !p_dec->p_owner->p_input->p->b_out_pace_control )
841 msg_Dbg( p_dec, "switching to async mode" );
842 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
846 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
849 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
851 if( p_dec->p_owner->p_packetizer )
853 block_t *p_packetized_block;
854 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
856 while( (p_packetized_block =
857 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
859 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
861 es_format_Clean( &p_dec->fmt_in );
862 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
865 while( p_packetized_block )
867 block_t *p_next = p_packetized_block->p_next;
868 p_packetized_block->p_next = NULL;
869 p_packetized_block->i_rate = i_rate;
871 DecoderDecodeAudio( p_dec, p_packetized_block );
873 p_packetized_block = p_next;
879 DecoderDecodeAudio( p_dec, p_block );
882 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
885 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
887 if( p_dec->p_owner->p_packetizer )
889 block_t *p_packetized_block;
890 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
892 while( (p_packetized_block =
893 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
895 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
897 es_format_Clean( &p_dec->fmt_in );
898 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
900 if( p_packetizer->pf_get_cc )
901 DecoderGetCc( p_dec, p_packetizer );
903 while( p_packetized_block )
905 block_t *p_next = p_packetized_block->p_next;
906 p_packetized_block->p_next = NULL;
907 p_packetized_block->i_rate = i_rate;
909 DecoderDecodeVideo( p_dec, p_packetized_block );
911 p_packetized_block = p_next;
917 DecoderDecodeVideo( p_dec, p_block );
920 else if( p_dec->fmt_in.i_cat == SPU_ES )
922 input_thread_t *p_input = p_dec->p_owner->p_input;
923 vout_thread_t *p_vout;
927 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
929 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
931 vlc_mutex_lock( &p_input->p->counters.counters_lock );
932 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
933 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
935 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
936 if( p_vout && p_sys->p_spu_vout == p_vout )
938 /* Prerool does not work very well with subtitle */
939 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
940 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
941 spu_DestroySubpicture( p_vout->p_spu, p_spu );
943 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
947 msg_Warn( p_dec, "no vout found, leaking subpicture" );
950 vlc_object_release( p_vout );
955 msg_Err( p_dec, "unknown ES format" );
959 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
963 * Destroys a decoder object
965 * \param p_dec the decoder object
968 static void DeleteDecoder( decoder_t * p_dec )
970 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
971 (char*)&p_dec->fmt_in.i_codec,
972 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
974 /* Free all packets still in the decoder fifo. */
975 block_FifoEmpty( p_dec->p_owner->p_fifo );
976 block_FifoRelease( p_dec->p_owner->p_fifo );
979 if( p_dec->p_owner->p_aout_input )
980 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
982 if( p_dec->p_owner->p_vout )
986 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
987 /* Hack to make sure all the the pictures are freed by the decoder */
988 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
991 if( p_pic->i_status == RESERVED_PICTURE )
992 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
993 if( p_pic->i_refcount > 0 )
994 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
998 /* We are about to die. Reattach video output to p_vlc. */
999 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
1002 if( p_dec->p_owner->p_sout_input )
1004 sout_InputDelete( p_dec->p_owner->p_sout_input );
1005 es_format_Clean( &p_dec->p_owner->sout );
1008 if( p_dec->fmt_in.i_cat == SPU_ES )
1010 vout_thread_t *p_vout;
1012 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1015 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1016 p_dec->p_owner->i_spu_channel );
1017 vlc_object_release( p_vout );
1021 es_format_Clean( &p_dec->fmt_in );
1022 es_format_Clean( &p_dec->fmt_out );
1024 if( p_dec->p_owner->p_packetizer )
1026 module_Unneed( p_dec->p_owner->p_packetizer,
1027 p_dec->p_owner->p_packetizer->p_module );
1028 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1029 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1030 vlc_object_detach( p_dec->p_owner->p_packetizer );
1031 vlc_object_release( p_dec->p_owner->p_packetizer );
1034 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1036 vlc_object_detach( p_dec );
1038 free( p_dec->p_owner );
1041 /*****************************************************************************
1042 * Buffers allocation callbacks for the decoders
1043 *****************************************************************************/
1044 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1046 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1047 aout_buffer_t *p_buffer;
1049 if( p_sys->p_aout_input != NULL &&
1050 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1051 p_dec->fmt_out.audio.i_original_channels !=
1052 p_sys->audio.i_original_channels ||
1053 p_dec->fmt_out.audio.i_bytes_per_frame !=
1054 p_sys->audio.i_bytes_per_frame ) )
1056 /* Parameters changed, restart the aout */
1057 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1058 p_sys->p_aout_input = NULL;
1061 if( p_sys->p_aout_input == NULL )
1063 audio_sample_format_t format;
1064 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1066 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1067 p_sys->audio = p_dec->fmt_out.audio;
1069 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1070 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1071 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1073 if ( i_force_dolby == 1 )
1075 format.i_original_channels = format.i_original_channels |
1076 AOUT_CHAN_DOLBYSTEREO;
1078 else /* i_force_dolby == 2 */
1080 format.i_original_channels = format.i_original_channels &
1081 ~AOUT_CHAN_DOLBYSTEREO;
1085 p_sys->p_aout_input =
1086 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1087 if( p_sys->p_aout_input == NULL )
1089 msg_Err( p_dec, "failed to create audio output" );
1090 p_dec->b_error = VLC_TRUE;
1093 p_dec->fmt_out.audio.i_bytes_per_frame =
1094 p_sys->audio.i_bytes_per_frame;
1097 p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
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 );