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 )
144 * Spawns a new decoder thread
146 * \param p_input the input thread
147 * \param p_es the es descriptor
148 * \return the spawned decoder object
150 decoder_t *input_DecoderNew( input_thread_t *p_input,
151 es_format_t *fmt, vlc_bool_t b_force_decoder )
153 decoder_t *p_dec = NULL;
156 /* If we are in sout mode, search for packetizer module */
157 if( p_input->p->p_sout && !b_force_decoder )
159 /* Create the decoder configuration structure */
160 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
163 msg_Err( p_input, "could not create packetizer" );
164 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
165 _("VLC could not open the packetizer module.") );
171 /* Create the decoder configuration structure */
172 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
175 msg_Err( p_input, "could not create decoder" );
176 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
177 _("VLC could not open the decoder module.") );
182 if( !p_dec->p_module )
184 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
186 DeleteDecoder( p_dec );
187 vlc_object_release( p_dec );
191 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
194 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
195 p_dec->p_owner->b_own_thread = VLC_FALSE;
199 var_Get( p_input, "minimize-threads", &val );
200 p_dec->p_owner->b_own_thread = !val.b_bool;
203 if( p_dec->p_owner->b_own_thread )
206 if( fmt->i_cat == AUDIO_ES )
207 i_priority = VLC_THREAD_PRIORITY_AUDIO;
209 i_priority = VLC_THREAD_PRIORITY_VIDEO;
211 /* Spawn the decoder thread */
212 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
213 i_priority, VLC_FALSE ) )
215 msg_Err( p_dec, "cannot spawn decoder thread" );
216 module_Unneed( p_dec, p_dec->p_module );
217 DeleteDecoder( p_dec );
218 vlc_object_release( p_dec );
227 * Kills a decoder thread and waits until it's finished
229 * \param p_input the input thread
230 * \param p_es the es descriptor
233 void input_DecoderDelete( decoder_t *p_dec )
235 vlc_object_kill( p_dec );
237 if( p_dec->p_owner->b_own_thread )
239 /* Make sure the thread leaves the function by
240 * sending it an empty block. */
241 block_t *p_block = block_New( p_dec, 0 );
242 input_DecoderDecode( p_dec, p_block );
244 vlc_thread_join( p_dec );
246 /* Don't module_Unneed() here because of the dll loader that wants
247 * close() in the same thread than open()/decode() */
252 input_DecoderDecode( p_dec, NULL );
254 module_Unneed( p_dec, p_dec->p_module );
258 if( p_dec->p_owner->b_cc_supported )
261 for( i = 0; i < 4; i++ )
262 input_DecoderSetCcState( p_dec, VLC_FALSE, i );
265 /* Delete decoder configuration */
266 DeleteDecoder( p_dec );
268 /* Delete the decoder */
269 vlc_object_release( p_dec );
273 * Put a block_t in the decoder's fifo.
275 * \param p_dec the decoder object
276 * \param p_block the data block
278 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
280 if( p_dec->p_owner->b_own_thread )
282 if( p_dec->p_owner->p_input->p->b_out_pace_control )
285 while( !p_dec->b_die && !p_dec->b_error &&
286 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
291 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
293 /* FIXME: ideally we would check the time amount of data
294 * in the fifo instead of its size. */
295 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
296 "consumed quickly enough), resetting fifo!" );
297 block_FifoEmpty( p_dec->p_owner->p_fifo );
300 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
304 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
306 if( p_block ) block_Release( p_block );
310 DecoderDecode( p_dec, p_block );
315 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
320 if( p_dec->p_owner->b_own_thread && b_flush )
321 block_FifoEmpty( p_dec->p_owner->p_fifo );
323 /* Send a special block */
324 p_null = block_New( p_dec, 128 );
325 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
326 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
327 if( p_dec->p_owner->p_packetizer && b_flush )
328 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
329 memset( p_null->p_buffer, 0, p_null->i_buffer );
331 input_DecoderDecode( p_dec, p_null );
334 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
336 if( p_dec->p_owner->b_own_thread
337 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
344 void input_DecoderIsCcPresent( decoder_t *p_dec, vlc_bool_t pb_present[4] )
348 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
349 for( i = 0; i < 4; i++ )
350 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
351 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
353 int input_DecoderSetCcState( decoder_t *p_dec, vlc_bool_t b_decode, int i_channel )
355 decoder_owner_sys_t *p_owner = p_dec->p_owner;
357 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
359 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
364 static const vlc_fourcc_t fcc[4] = {
365 VLC_FOURCC('c', 'c', '1', ' '),
366 VLC_FOURCC('c', 'c', '2', ' '),
367 VLC_FOURCC('c', 'c', '3', ' '),
368 VLC_FOURCC('c', 'c', '4', ' '),
373 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
374 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
377 msg_Err( p_dec, "could not create decoder" );
378 intf_UserFatal( p_dec, VLC_FALSE, _("Streaming / Transcoding failed"),
379 _("VLC could not open the decoder module.") );
382 else if( !p_cc->p_module )
384 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
385 DeleteDecoder( p_cc );
386 vlc_object_release( p_cc );
390 vlc_mutex_lock( &p_owner->lock_cc );
391 p_dec->p_owner->pp_cc[i_channel] = p_cc;
392 vlc_mutex_unlock( &p_owner->lock_cc );
398 vlc_mutex_lock( &p_owner->lock_cc );
399 p_cc = p_dec->p_owner->pp_cc[i_channel];
400 p_dec->p_owner->pp_cc[i_channel] = NULL;
401 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
405 vlc_object_kill( p_cc );
406 module_Unneed( p_cc, p_cc->p_module );
407 DeleteDecoder( p_cc );
408 vlc_object_release( p_cc );
413 int input_DecoderGetCcState( decoder_t *p_dec, vlc_bool_t *pb_decode, int i_channel )
415 decoder_owner_sys_t *p_owner = p_dec->p_owner;
417 *pb_decode = VLC_FALSE;
418 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
421 vlc_mutex_lock( &p_owner->lock_cc );
422 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
423 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
428 * Create a decoder object
430 * \param p_input the input thread
431 * \param p_es the es descriptor
432 * \param i_object_type Object type as define in include/vlc_objects.h
433 * \return the decoder object
435 static decoder_t * CreateDecoder( input_thread_t *p_input,
436 es_format_t *fmt, int i_object_type )
439 decoder_owner_sys_t *p_owner;
442 p_dec = vlc_object_create( p_input, i_object_type );
445 msg_Err( p_input, "out of memory" );
449 p_dec->pf_decode_audio = 0;
450 p_dec->pf_decode_video = 0;
451 p_dec->pf_decode_sub = 0;
452 p_dec->pf_get_cc = 0;
453 p_dec->pf_packetize = 0;
455 /* Initialize the decoder fifo */
456 p_dec->p_module = NULL;
458 memset( &null_es_format, 0, sizeof(es_format_t) );
459 es_format_Copy( &p_dec->fmt_in, fmt );
460 es_format_Copy( &p_dec->fmt_out, &null_es_format );
462 /* Allocate our private structure for the decoder */
463 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
464 if( p_dec->p_owner == NULL )
466 msg_Err( p_dec, "out of memory" );
469 p_dec->p_owner->b_own_thread = VLC_TRUE;
470 p_dec->p_owner->i_preroll_end = -1;
471 p_dec->p_owner->p_input = p_input;
472 p_dec->p_owner->p_aout = NULL;
473 p_dec->p_owner->p_aout_input = NULL;
474 p_dec->p_owner->p_vout = NULL;
475 p_dec->p_owner->p_spu_vout = NULL;
476 p_dec->p_owner->i_spu_channel = 0;
477 p_dec->p_owner->p_sout = p_input->p->p_sout;
478 p_dec->p_owner->p_sout_input = NULL;
479 p_dec->p_owner->p_packetizer = NULL;
482 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
484 msg_Err( p_dec, "out of memory" );
488 /* Set buffers allocation callbacks for the decoders */
489 p_dec->pf_aout_buffer_new = aout_new_buffer;
490 p_dec->pf_aout_buffer_del = aout_del_buffer;
491 p_dec->pf_vout_buffer_new = vout_new_buffer;
492 p_dec->pf_vout_buffer_del = vout_del_buffer;
493 p_dec->pf_picture_link = vout_link_picture;
494 p_dec->pf_picture_unlink = vout_unlink_picture;
495 p_dec->pf_spu_buffer_new = spu_new_buffer;
496 p_dec->pf_spu_buffer_del = spu_del_buffer;
498 vlc_object_attach( p_dec, p_input );
500 /* Find a suitable decoder/packetizer module */
501 if( i_object_type == VLC_OBJECT_DECODER )
502 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
504 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
506 /* Check if decoder requires already packetized data */
507 if( i_object_type == VLC_OBJECT_DECODER &&
508 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
510 p_dec->p_owner->p_packetizer =
511 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
512 if( p_dec->p_owner->p_packetizer )
514 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
517 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
520 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
522 p_dec->p_owner->p_packetizer->p_module =
523 module_Need( p_dec->p_owner->p_packetizer,
524 "packetizer", "$packetizer", 0 );
526 if( !p_dec->p_owner->p_packetizer->p_module )
528 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
529 vlc_object_detach( p_dec->p_owner->p_packetizer );
530 vlc_object_release( p_dec->p_owner->p_packetizer );
535 /* Copy ourself the input replay gain */
536 if( fmt->i_cat == AUDIO_ES )
538 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
540 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
542 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
543 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
545 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
547 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
548 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
553 p_owner->b_cc_supported = VLC_FALSE;
554 if( i_object_type == VLC_OBJECT_DECODER )
556 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
557 p_owner->b_cc_supported = VLC_TRUE;
558 if( p_dec->pf_get_cc )
559 p_owner->b_cc_supported = VLC_TRUE;
562 vlc_mutex_init( p_dec, &p_owner->lock_cc );
563 for( i = 0; i < 4; i++ )
565 p_owner->pb_cc_present[i] = VLC_FALSE;
566 p_owner->pp_cc[i] = NULL;
572 * The decoding main loop
574 * \param p_dec the decoder
577 static int DecoderThread( decoder_t * p_dec )
581 /* The decoder's main loop */
582 while( !p_dec->b_die && !p_dec->b_error )
584 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
589 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
595 while( !p_dec->b_die )
597 /* Trash all received PES packets */
598 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
599 if( p_block ) block_Release( p_block );
602 /* We do it here because of the dll loader that wants close() in the
603 * same thread than open()/decode() */
604 module_Unneed( p_dec, p_dec->p_module );
609 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
611 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
612 *pi_preroll = INT64_MAX;
613 else if( p->i_pts > 0 )
614 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
615 else if( p->i_dts > 0 )
616 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
618 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
620 input_thread_t *p_input = p_dec->p_owner->p_input;
621 const int i_rate = p_block->i_rate;
622 aout_buffer_t *p_aout_buf;
624 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
626 vlc_mutex_lock( &p_input->p->counters.counters_lock );
627 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
628 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
630 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
632 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
633 p_dec->p_owner->p_aout_input, p_aout_buf );
637 if( p_dec->p_owner->i_preroll_end > 0 )
639 /* FIXME TODO flush audio output (don't know how to do that) */
640 msg_Dbg( p_dec, "End of audio preroll" );
641 p_dec->p_owner->i_preroll_end = -1;
643 aout_DecPlay( p_dec->p_owner->p_aout,
644 p_dec->p_owner->p_aout_input,
645 p_aout_buf, i_rate );
648 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
651 vlc_bool_t pb_present[4];
655 assert( p_dec_cc->pf_get_cc != NULL );
657 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
658 if( !p_dec->p_owner->b_cc_supported )
661 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
665 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
666 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
668 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
669 if( p_dec->p_owner->pp_cc[i] )
673 for( i = 0; i < 4; i++ )
675 if( !p_dec->p_owner->pp_cc[i] )
678 if( i_cc_decoder > 1 )
679 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
681 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
684 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
686 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
688 vlc_mutex_lock( &p_vout->picture_lock );
690 if( p_pic->i_status == READY_PICTURE )
692 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
695 else if( p_pic->i_refcount > 0 )
697 p_pic->i_status = DISPLAYED_PICTURE;
701 p_pic->i_status = DESTROYED_PICTURE;
702 p_vout->i_heap_size--;
705 vlc_mutex_unlock( &p_vout->picture_lock );
707 static void VoutFlushPicture( vout_thread_t *p_vout )
710 vlc_mutex_lock( &p_vout->picture_lock );
711 for( i = 0; i < p_vout->render.i_pictures; i++ )
713 picture_t *p_pic = p_vout->render.pp_picture[i];
715 if( p_pic->i_status == READY_PICTURE ||
716 p_pic->i_status == DISPLAYED_PICTURE )
718 /* We cannot change picture status if it is in READY_PICTURE state,
719 * Just make sure they won't be displayed */
723 vlc_mutex_unlock( &p_vout->picture_lock );
725 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
727 input_thread_t *p_input = p_dec->p_owner->p_input;
730 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
732 vlc_mutex_lock( &p_input->p->counters.counters_lock );
733 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
734 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
736 if( p_pic->date < p_dec->p_owner->i_preroll_end )
738 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
742 if( p_dec->p_owner->i_preroll_end > 0 )
744 msg_Dbg( p_dec, "End of video preroll" );
745 if( p_dec->p_owner->p_vout )
746 VoutFlushPicture( p_dec->p_owner->p_vout );
748 p_dec->p_owner->i_preroll_end = -1;
751 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
752 DecoderGetCc( p_dec, p_dec );
754 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
756 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
763 * \param p_dec the decoder object
764 * \param p_block the block to decode
765 * \return VLC_SUCCESS or an error code
767 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
769 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
770 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
772 if( p_block && p_block->i_buffer <= 0 )
774 block_Release( p_block );
778 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
780 block_t *p_sout_block;
782 while( ( p_sout_block =
783 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
785 if( !p_dec->p_owner->p_sout_input )
787 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
789 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
790 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
791 if( p_dec->fmt_in.psz_language )
793 if( p_dec->p_owner->sout.psz_language )
794 free( p_dec->p_owner->sout.psz_language );
795 p_dec->p_owner->sout.psz_language =
796 strdup( p_dec->fmt_in.psz_language );
799 p_dec->p_owner->p_sout_input =
800 sout_InputNew( p_dec->p_owner->p_sout,
801 &p_dec->p_owner->sout );
803 if( p_dec->p_owner->p_sout_input == NULL )
805 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
806 (char *)&p_dec->p_owner->sout.i_codec );
807 p_dec->b_error = VLC_TRUE;
809 while( p_sout_block )
811 block_t *p_next = p_sout_block->p_next;
812 block_Release( p_sout_block );
813 p_sout_block = p_next;
819 while( p_sout_block )
821 block_t *p_next = p_sout_block->p_next;
823 p_sout_block->p_next = NULL;
824 p_sout_block->i_rate = i_rate;
826 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
829 p_sout_block = p_next;
832 /* For now it's enough, as only sout inpact on this flag */
833 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 sync mode" );
837 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
839 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
840 !p_dec->p_owner->p_input->p->b_out_pace_control )
842 msg_Dbg( p_dec, "switching to async mode" );
843 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
847 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
850 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
852 if( p_dec->p_owner->p_packetizer )
854 block_t *p_packetized_block;
855 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
857 while( (p_packetized_block =
858 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
860 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
862 es_format_Clean( &p_dec->fmt_in );
863 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
866 while( p_packetized_block )
868 block_t *p_next = p_packetized_block->p_next;
869 p_packetized_block->p_next = NULL;
870 p_packetized_block->i_rate = i_rate;
872 DecoderDecodeAudio( p_dec, p_packetized_block );
874 p_packetized_block = p_next;
880 DecoderDecodeAudio( p_dec, p_block );
883 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
886 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
888 if( p_dec->p_owner->p_packetizer )
890 block_t *p_packetized_block;
891 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
893 while( (p_packetized_block =
894 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
896 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
898 es_format_Clean( &p_dec->fmt_in );
899 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
901 if( p_packetizer->pf_get_cc )
902 DecoderGetCc( p_dec, p_packetizer );
904 while( p_packetized_block )
906 block_t *p_next = p_packetized_block->p_next;
907 p_packetized_block->p_next = NULL;
908 p_packetized_block->i_rate = i_rate;
910 DecoderDecodeVideo( p_dec, p_packetized_block );
912 p_packetized_block = p_next;
918 DecoderDecodeVideo( p_dec, p_block );
921 else if( p_dec->fmt_in.i_cat == SPU_ES )
923 input_thread_t *p_input = p_dec->p_owner->p_input;
924 vout_thread_t *p_vout;
928 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
930 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
932 vlc_mutex_lock( &p_input->p->counters.counters_lock );
933 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
934 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
936 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
937 if( p_vout && p_sys->p_spu_vout == p_vout )
939 /* Prerool does not work very well with subtitle */
940 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
941 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
942 spu_DestroySubpicture( p_vout->p_spu, p_spu );
944 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
948 msg_Warn( p_dec, "no vout found, leaking subpicture" );
951 vlc_object_release( p_vout );
956 msg_Err( p_dec, "unknown ES format" );
960 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
964 * Destroys a decoder object
966 * \param p_dec the decoder object
969 static void DeleteDecoder( decoder_t * p_dec )
971 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
972 (char*)&p_dec->fmt_in.i_codec,
973 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
975 /* Free all packets still in the decoder fifo. */
976 block_FifoEmpty( p_dec->p_owner->p_fifo );
977 block_FifoRelease( p_dec->p_owner->p_fifo );
980 if( p_dec->p_owner->p_aout_input )
981 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
983 if( p_dec->p_owner->p_vout )
987 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
988 /* Hack to make sure all the the pictures are freed by the decoder */
989 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
992 if( p_pic->i_status == RESERVED_PICTURE )
993 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
994 if( p_pic->i_refcount > 0 )
995 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
999 /* We are about to die. Reattach video output to p_vlc. */
1000 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
1003 if( p_dec->p_owner->p_sout_input )
1005 sout_InputDelete( p_dec->p_owner->p_sout_input );
1006 es_format_Clean( &p_dec->p_owner->sout );
1009 if( p_dec->fmt_in.i_cat == SPU_ES )
1011 vout_thread_t *p_vout;
1013 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1016 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1017 p_dec->p_owner->i_spu_channel );
1018 vlc_object_release( p_vout );
1022 es_format_Clean( &p_dec->fmt_in );
1023 es_format_Clean( &p_dec->fmt_out );
1025 if( p_dec->p_owner->p_packetizer )
1027 module_Unneed( p_dec->p_owner->p_packetizer,
1028 p_dec->p_owner->p_packetizer->p_module );
1029 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1030 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1031 vlc_object_detach( p_dec->p_owner->p_packetizer );
1032 vlc_object_release( p_dec->p_owner->p_packetizer );
1035 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1037 vlc_object_detach( p_dec );
1039 free( p_dec->p_owner );
1042 /*****************************************************************************
1043 * Buffers allocation callbacks for the decoders
1044 *****************************************************************************/
1045 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1047 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1048 aout_buffer_t *p_buffer;
1050 if( p_sys->p_aout_input != NULL &&
1051 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1052 p_dec->fmt_out.audio.i_original_channels !=
1053 p_sys->audio.i_original_channels ||
1054 p_dec->fmt_out.audio.i_bytes_per_frame !=
1055 p_sys->audio.i_bytes_per_frame ) )
1057 /* Parameters changed, restart the aout */
1058 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1059 p_sys->p_aout_input = NULL;
1062 if( p_sys->p_aout_input == NULL )
1064 audio_sample_format_t format;
1065 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1067 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1068 p_sys->audio = p_dec->fmt_out.audio;
1070 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1071 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1072 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1074 if ( i_force_dolby == 1 )
1076 format.i_original_channels = format.i_original_channels |
1077 AOUT_CHAN_DOLBYSTEREO;
1079 else /* i_force_dolby == 2 */
1081 format.i_original_channels = format.i_original_channels &
1082 ~AOUT_CHAN_DOLBYSTEREO;
1086 p_sys->p_aout_input =
1087 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1088 if( p_sys->p_aout_input == NULL )
1090 msg_Err( p_dec, "failed to create audio output" );
1091 p_dec->b_error = VLC_TRUE;
1094 p_dec->fmt_out.audio.i_bytes_per_frame =
1095 p_sys->audio.i_bytes_per_frame;
1098 p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
1103 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1105 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1106 p_dec->p_owner->p_aout_input, p_buffer );
1109 static picture_t *vout_new_buffer( decoder_t *p_dec )
1111 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1114 if( p_sys->p_vout == NULL ||
1115 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1116 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1117 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1118 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1120 if( !p_dec->fmt_out.video.i_width ||
1121 !p_dec->fmt_out.video.i_height )
1123 /* Can't create a new vout without display size */
1127 if( !p_dec->fmt_out.video.i_visible_width ||
1128 !p_dec->fmt_out.video.i_visible_height )
1130 if( p_dec->fmt_in.video.i_visible_width &&
1131 p_dec->fmt_in.video.i_visible_height )
1133 p_dec->fmt_out.video.i_visible_width =
1134 p_dec->fmt_in.video.i_visible_width;
1135 p_dec->fmt_out.video.i_visible_height =
1136 p_dec->fmt_in.video.i_visible_height;
1140 p_dec->fmt_out.video.i_visible_width =
1141 p_dec->fmt_out.video.i_width;
1142 p_dec->fmt_out.video.i_visible_height =
1143 p_dec->fmt_out.video.i_height;
1147 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1148 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1150 p_dec->fmt_out.video.i_visible_height = 1080;
1151 p_dec->fmt_out.video.i_sar_num *= 135;
1152 p_dec->fmt_out.video.i_sar_den *= 136;
1153 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1156 if( !p_dec->fmt_out.video.i_sar_num ||
1157 !p_dec->fmt_out.video.i_sar_den )
1159 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1160 p_dec->fmt_out.video.i_visible_height;
1162 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1163 p_dec->fmt_out.video.i_visible_width;
1166 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1167 &p_dec->fmt_out.video.i_sar_den,
1168 p_dec->fmt_out.video.i_sar_num,
1169 p_dec->fmt_out.video.i_sar_den, 50000 );
1171 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1172 p_sys->video = p_dec->fmt_out.video;
1174 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1175 &p_dec->fmt_out.video );
1176 if( p_sys->p_vout == NULL )
1178 msg_Err( p_dec, "failed to create video output" );
1179 p_dec->b_error = VLC_TRUE;
1183 if( p_sys->video.i_rmask )
1184 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1185 if( p_sys->video.i_gmask )
1186 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1187 if( p_sys->video.i_bmask )
1188 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1191 /* Get a new picture */
1192 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1194 int i_pic, i_ready_pic = 0;
1196 if( p_dec->b_die || p_dec->b_error )
1201 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1202 /* Check the decoder doesn't leak pictures */
1203 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1206 if( p_pic->i_status == READY_PICTURE )
1208 if( i_ready_pic++ > 0 ) break;
1212 if( p_pic->i_status != DISPLAYED_PICTURE &&
1213 p_pic->i_status != RESERVED_PICTURE &&
1214 p_pic->i_status != READY_PICTURE ) break;
1216 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1219 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1221 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1223 /* Just free all the pictures */
1224 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1227 if( p_pic->i_status == RESERVED_PICTURE )
1228 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1229 if( p_pic->i_refcount > 0 )
1230 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1235 msleep( VOUT_OUTMEM_SLEEP );
1241 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1243 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1246 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1248 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1251 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1253 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1256 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1258 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1259 vout_thread_t *p_vout = NULL;
1260 subpicture_t *p_subpic;
1261 int i_attempts = 30;
1263 while( i_attempts-- )
1265 if( p_dec->b_die || p_dec->b_error ) break;
1267 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1270 msleep( VOUT_DISPLAY_DELAY );
1275 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1279 if( p_sys->p_spu_vout != p_vout )
1281 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1282 &p_sys->i_spu_channel );
1283 p_sys->p_spu_vout = p_vout;
1286 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1289 p_subpic->i_channel = p_sys->i_spu_channel;
1292 vlc_object_release( p_vout );
1297 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1299 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1300 vout_thread_t *p_vout = NULL;
1302 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1303 if( !p_vout || p_sys->p_spu_vout != p_vout )
1306 vlc_object_release( p_vout );
1307 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1311 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1313 vlc_object_release( p_vout );