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 "for format"), _("VLC probably does not support the \"%4.4s\" "
108 "audio or video format. Unfortunately there is no way for you "
109 "to fix this."), (char*)&codec );
112 /* decoder_GetInputAttachment:
114 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
115 const char *psz_name )
117 input_attachment_t *p_attachment;
118 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
122 /* decoder_GetInputAttachments:
124 int decoder_GetInputAttachments( decoder_t *p_dec,
125 input_attachment_t ***ppp_attachment,
128 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
129 ppp_attachment, pi_attachment );
131 /* decoder_GetDisplayDate:
133 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
139 * Spawns a new decoder thread
141 * \param p_input the input thread
142 * \param p_es the es descriptor
143 * \return the spawned decoder object
145 decoder_t *input_DecoderNew( input_thread_t *p_input,
146 es_format_t *fmt, vlc_bool_t b_force_decoder )
148 decoder_t *p_dec = NULL;
151 /* If we are in sout mode, search for packetizer module */
152 if( p_input->p->p_sout && !b_force_decoder )
154 /* Create the decoder configuration structure */
155 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
158 msg_Err( p_input, "could not create packetizer" );
159 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
160 _("VLC could not open the packetizer module.") );
166 /* Create the decoder configuration structure */
167 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
170 msg_Err( p_input, "could not create decoder" );
171 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
172 _("VLC could not open the decoder module.") );
177 if( !p_dec->p_module )
179 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
181 DeleteDecoder( p_dec );
182 vlc_object_destroy( p_dec );
186 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
189 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
190 p_dec->p_owner->b_own_thread = VLC_FALSE;
194 var_Get( p_input, "minimize-threads", &val );
195 p_dec->p_owner->b_own_thread = !val.b_bool;
198 if( p_dec->p_owner->b_own_thread )
201 if( fmt->i_cat == AUDIO_ES )
202 i_priority = VLC_THREAD_PRIORITY_AUDIO;
204 i_priority = VLC_THREAD_PRIORITY_VIDEO;
206 /* Spawn the decoder thread */
207 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
208 i_priority, VLC_FALSE ) )
210 msg_Err( p_dec, "cannot spawn decoder thread" );
211 module_Unneed( p_dec, p_dec->p_module );
212 DeleteDecoder( p_dec );
213 vlc_object_destroy( p_dec );
222 * Kills a decoder thread and waits until it's finished
224 * \param p_input the input thread
225 * \param p_es the es descriptor
228 void input_DecoderDelete( decoder_t *p_dec )
230 vlc_object_kill( p_dec );
232 if( p_dec->p_owner->b_own_thread )
234 /* Make sure the thread leaves the function by
235 * sending it an empty block. */
236 block_t *p_block = block_New( p_dec, 0 );
237 input_DecoderDecode( p_dec, p_block );
239 vlc_thread_join( p_dec );
241 /* Don't module_Unneed() here because of the dll loader that wants
242 * close() in the same thread than open()/decode() */
247 input_DecoderDecode( p_dec, NULL );
249 module_Unneed( p_dec, p_dec->p_module );
253 if( p_dec->p_owner->b_cc_supported )
256 for( i = 0; i < 4; i++ )
257 input_DecoderSetCcState( p_dec, VLC_FALSE, i );
260 /* Delete decoder configuration */
261 DeleteDecoder( p_dec );
263 /* Delete the decoder */
264 vlc_object_destroy( p_dec );
268 * Put a block_t in the decoder's fifo.
270 * \param p_dec the decoder object
271 * \param p_block the data block
273 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
275 if( p_dec->p_owner->b_own_thread )
277 if( p_dec->p_owner->p_input->p->b_out_pace_control )
280 while( !p_dec->b_die && !p_dec->b_error &&
281 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
286 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
288 /* FIXME: ideally we would check the time amount of data
289 * in the fifo instead of its size. */
290 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
291 "consumed quickly enough), resetting fifo!" );
292 block_FifoEmpty( p_dec->p_owner->p_fifo );
295 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
299 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
301 if( p_block ) block_Release( p_block );
305 DecoderDecode( p_dec, p_block );
310 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
315 if( p_dec->p_owner->b_own_thread && b_flush )
316 block_FifoEmpty( p_dec->p_owner->p_fifo );
318 /* Send a special block */
319 p_null = block_New( p_dec, 128 );
320 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
321 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
322 if( p_dec->p_owner->p_packetizer && b_flush )
323 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
324 memset( p_null->p_buffer, 0, p_null->i_buffer );
326 input_DecoderDecode( p_dec, p_null );
329 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
331 if( p_dec->p_owner->b_own_thread
332 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
339 void input_DecoderIsCcPresent( decoder_t *p_dec, vlc_bool_t pb_present[4] )
343 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
344 for( i = 0; i < 4; i++ )
345 pb_present[i] = p_dec->p_owner->pb_cc_present[i];
346 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
348 int input_DecoderSetCcState( decoder_t *p_dec, vlc_bool_t b_decode, int i_channel )
350 decoder_owner_sys_t *p_owner = p_dec->p_owner;
352 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
354 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
359 static const vlc_fourcc_t fcc[4] = {
360 VLC_FOURCC('c', 'c', '1', ' '),
361 VLC_FOURCC('c', 'c', '2', ' '),
362 VLC_FOURCC('c', 'c', '3', ' '),
363 VLC_FOURCC('c', 'c', '4', ' '),
368 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
369 p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
372 msg_Err( p_dec, "could not create decoder" );
373 intf_UserFatal( p_dec, VLC_FALSE, _("Streaming / Transcoding failed"),
374 _("VLC could not open the decoder module.") );
377 else if( !p_cc->p_module )
379 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
380 DeleteDecoder( p_cc );
381 vlc_object_destroy( p_cc );
385 vlc_mutex_lock( &p_owner->lock_cc );
386 p_dec->p_owner->pp_cc[i_channel] = p_cc;
387 vlc_mutex_unlock( &p_owner->lock_cc );
393 vlc_mutex_lock( &p_owner->lock_cc );
394 p_cc = p_dec->p_owner->pp_cc[i_channel];
395 p_dec->p_owner->pp_cc[i_channel] = NULL;
396 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
400 vlc_object_kill( p_cc );
401 module_Unneed( p_cc, p_cc->p_module );
402 DeleteDecoder( p_cc );
403 vlc_object_destroy( p_cc );
408 int input_DecoderGetCcState( decoder_t *p_dec, vlc_bool_t *pb_decode, int i_channel )
410 decoder_owner_sys_t *p_owner = p_dec->p_owner;
412 *pb_decode = VLC_FALSE;
413 if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
416 vlc_mutex_lock( &p_owner->lock_cc );
417 *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
418 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
423 * Create a decoder object
425 * \param p_input the input thread
426 * \param p_es the es descriptor
427 * \param i_object_type Object type as define in include/vlc_objects.h
428 * \return the decoder object
430 static decoder_t * CreateDecoder( input_thread_t *p_input,
431 es_format_t *fmt, int i_object_type )
434 decoder_owner_sys_t *p_owner;
437 p_dec = vlc_object_create( p_input, i_object_type );
440 msg_Err( p_input, "out of memory" );
444 p_dec->pf_decode_audio = 0;
445 p_dec->pf_decode_video = 0;
446 p_dec->pf_decode_sub = 0;
447 p_dec->pf_get_cc = 0;
448 p_dec->pf_packetize = 0;
450 /* Initialize the decoder fifo */
451 p_dec->p_module = NULL;
453 memset( &null_es_format, 0, sizeof(es_format_t) );
454 es_format_Copy( &p_dec->fmt_in, fmt );
455 es_format_Copy( &p_dec->fmt_out, &null_es_format );
457 /* Allocate our private structure for the decoder */
458 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
459 if( p_dec->p_owner == NULL )
461 msg_Err( p_dec, "out of memory" );
464 p_dec->p_owner->b_own_thread = VLC_TRUE;
465 p_dec->p_owner->i_preroll_end = -1;
466 p_dec->p_owner->p_input = p_input;
467 p_dec->p_owner->p_aout = NULL;
468 p_dec->p_owner->p_aout_input = NULL;
469 p_dec->p_owner->p_vout = NULL;
470 p_dec->p_owner->p_spu_vout = NULL;
471 p_dec->p_owner->i_spu_channel = 0;
472 p_dec->p_owner->p_sout = p_input->p->p_sout;
473 p_dec->p_owner->p_sout_input = NULL;
474 p_dec->p_owner->p_packetizer = NULL;
477 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
479 msg_Err( p_dec, "out of memory" );
483 /* Set buffers allocation callbacks for the decoders */
484 p_dec->pf_aout_buffer_new = aout_new_buffer;
485 p_dec->pf_aout_buffer_del = aout_del_buffer;
486 p_dec->pf_vout_buffer_new = vout_new_buffer;
487 p_dec->pf_vout_buffer_del = vout_del_buffer;
488 p_dec->pf_picture_link = vout_link_picture;
489 p_dec->pf_picture_unlink = vout_unlink_picture;
490 p_dec->pf_spu_buffer_new = spu_new_buffer;
491 p_dec->pf_spu_buffer_del = spu_del_buffer;
493 vlc_object_attach( p_dec, p_input );
495 /* Find a suitable decoder/packetizer module */
496 if( i_object_type == VLC_OBJECT_DECODER )
497 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
499 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
501 /* Check if decoder requires already packetized data */
502 if( i_object_type == VLC_OBJECT_DECODER &&
503 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
505 p_dec->p_owner->p_packetizer =
506 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
507 if( p_dec->p_owner->p_packetizer )
509 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
512 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
515 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
517 p_dec->p_owner->p_packetizer->p_module =
518 module_Need( p_dec->p_owner->p_packetizer,
519 "packetizer", "$packetizer", 0 );
521 if( !p_dec->p_owner->p_packetizer->p_module )
523 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
524 vlc_object_detach( p_dec->p_owner->p_packetizer );
525 vlc_object_destroy( p_dec->p_owner->p_packetizer );
530 /* Copy ourself the input replay gain */
531 if( fmt->i_cat == AUDIO_ES )
533 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
535 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
537 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
538 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
540 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
542 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
543 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
548 p_owner->b_cc_supported = VLC_FALSE;
549 if( i_object_type == VLC_OBJECT_DECODER )
551 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
552 p_owner->b_cc_supported = VLC_TRUE;
553 if( p_dec->pf_get_cc )
554 p_owner->b_cc_supported = VLC_TRUE;
557 vlc_mutex_init( p_dec, &p_owner->lock_cc );
558 for( i = 0; i < 4; i++ )
560 p_owner->pb_cc_present[i] = VLC_FALSE;
561 p_owner->pp_cc[i] = NULL;
567 * The decoding main loop
569 * \param p_dec the decoder
572 static int DecoderThread( decoder_t * p_dec )
576 /* The decoder's main loop */
577 while( !p_dec->b_die && !p_dec->b_error )
579 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
584 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
590 while( !p_dec->b_die )
592 /* Trash all received PES packets */
593 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
594 if( p_block ) block_Release( p_block );
597 /* We do it here because of the dll loader that wants close() in the
598 * same thread than open()/decode() */
599 module_Unneed( p_dec, p_dec->p_module );
604 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
606 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
607 *pi_preroll = INT64_MAX;
608 else if( p->i_pts > 0 )
609 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
610 else if( p->i_dts > 0 )
611 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
613 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
615 input_thread_t *p_input = p_dec->p_owner->p_input;
616 const int i_rate = p_block->i_rate;
617 aout_buffer_t *p_aout_buf;
619 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
621 vlc_mutex_lock( &p_input->p->counters.counters_lock );
622 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
623 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
625 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
627 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
628 p_dec->p_owner->p_aout_input, p_aout_buf );
632 if( p_dec->p_owner->i_preroll_end > 0 )
634 /* FIXME TODO flush audio output (don't know how to do that) */
635 msg_Dbg( p_dec, "End of audio preroll" );
636 p_dec->p_owner->i_preroll_end = -1;
638 aout_DecPlay( p_dec->p_owner->p_aout,
639 p_dec->p_owner->p_aout_input,
640 p_aout_buf, i_rate );
643 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
646 vlc_bool_t pb_present[4];
650 assert( p_dec_cc->pf_get_cc != NULL );
652 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
653 if( !p_dec->p_owner->b_cc_supported )
656 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
660 vlc_mutex_lock( &p_dec->p_owner->lock_cc );
661 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
663 p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
664 if( p_dec->p_owner->pp_cc[i] )
668 for( i = 0; i < 4; i++ )
670 if( !p_dec->p_owner->pp_cc[i] )
673 if( i_cc_decoder > 1 )
674 DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
676 DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
679 vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
681 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
683 vlc_mutex_lock( &p_vout->picture_lock );
685 if( p_pic->i_status == READY_PICTURE )
687 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
690 else if( p_pic->i_refcount > 0 )
692 p_pic->i_status = DISPLAYED_PICTURE;
696 p_pic->i_status = DESTROYED_PICTURE;
697 p_vout->i_heap_size--;
700 vlc_mutex_unlock( &p_vout->picture_lock );
702 static void VoutFlushPicture( vout_thread_t *p_vout )
705 vlc_mutex_lock( &p_vout->picture_lock );
706 for( i = 0; i < p_vout->render.i_pictures; i++ )
708 picture_t *p_pic = p_vout->render.pp_picture[i];
710 if( p_pic->i_status == READY_PICTURE ||
711 p_pic->i_status == DISPLAYED_PICTURE )
713 /* We cannot change picture status if it is in READY_PICTURE state,
714 * Just make sure they won't be displayed */
718 vlc_mutex_unlock( &p_vout->picture_lock );
720 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
722 input_thread_t *p_input = p_dec->p_owner->p_input;
725 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
727 vlc_mutex_lock( &p_input->p->counters.counters_lock );
728 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
729 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
731 if( p_pic->date < p_dec->p_owner->i_preroll_end )
733 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
737 if( p_dec->p_owner->i_preroll_end > 0 )
739 msg_Dbg( p_dec, "End of video preroll" );
740 if( p_dec->p_owner->p_vout )
741 VoutFlushPicture( p_dec->p_owner->p_vout );
743 p_dec->p_owner->i_preroll_end = -1;
746 if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
747 DecoderGetCc( p_dec, p_dec );
749 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
751 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
758 * \param p_dec the decoder object
759 * \param p_block the block to decode
760 * \return VLC_SUCCESS or an error code
762 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
764 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
765 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
767 if( p_block && p_block->i_buffer <= 0 )
769 block_Release( p_block );
773 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
775 block_t *p_sout_block;
777 while( ( p_sout_block =
778 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
780 if( !p_dec->p_owner->p_sout_input )
782 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
784 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
785 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
786 if( p_dec->fmt_in.psz_language )
788 if( p_dec->p_owner->sout.psz_language )
789 free( p_dec->p_owner->sout.psz_language );
790 p_dec->p_owner->sout.psz_language =
791 strdup( p_dec->fmt_in.psz_language );
794 p_dec->p_owner->p_sout_input =
795 sout_InputNew( p_dec->p_owner->p_sout,
796 &p_dec->p_owner->sout );
798 if( p_dec->p_owner->p_sout_input == NULL )
800 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
801 (char *)&p_dec->p_owner->sout.i_codec );
802 p_dec->b_error = VLC_TRUE;
804 while( p_sout_block )
806 block_t *p_next = p_sout_block->p_next;
807 block_Release( p_sout_block );
808 p_sout_block = p_next;
814 while( p_sout_block )
816 block_t *p_next = p_sout_block->p_next;
818 p_sout_block->p_next = NULL;
819 p_sout_block->i_rate = i_rate;
821 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
824 p_sout_block = p_next;
827 /* For now it's enough, as only sout inpact on this flag */
828 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
829 p_dec->p_owner->p_input->p->b_out_pace_control )
831 msg_Dbg( p_dec, "switching to sync mode" );
832 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
834 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
835 !p_dec->p_owner->p_input->p->b_out_pace_control )
837 msg_Dbg( p_dec, "switching to async mode" );
838 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
842 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
845 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
847 if( p_dec->p_owner->p_packetizer )
849 block_t *p_packetized_block;
850 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
852 while( (p_packetized_block =
853 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
855 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
857 es_format_Clean( &p_dec->fmt_in );
858 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
861 while( p_packetized_block )
863 block_t *p_next = p_packetized_block->p_next;
864 p_packetized_block->p_next = NULL;
865 p_packetized_block->i_rate = i_rate;
867 DecoderDecodeAudio( p_dec, p_packetized_block );
869 p_packetized_block = p_next;
875 DecoderDecodeAudio( p_dec, p_block );
878 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
881 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
883 if( p_dec->p_owner->p_packetizer )
885 block_t *p_packetized_block;
886 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
888 while( (p_packetized_block =
889 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
891 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
893 es_format_Clean( &p_dec->fmt_in );
894 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
896 if( p_packetizer->pf_get_cc )
897 DecoderGetCc( p_dec, p_packetizer );
899 while( p_packetized_block )
901 block_t *p_next = p_packetized_block->p_next;
902 p_packetized_block->p_next = NULL;
903 p_packetized_block->i_rate = i_rate;
905 DecoderDecodeVideo( p_dec, p_packetized_block );
907 p_packetized_block = p_next;
913 DecoderDecodeVideo( p_dec, p_block );
916 else if( p_dec->fmt_in.i_cat == SPU_ES )
918 input_thread_t *p_input = p_dec->p_owner->p_input;
919 vout_thread_t *p_vout;
923 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
925 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
927 vlc_mutex_lock( &p_input->p->counters.counters_lock );
928 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
929 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
931 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
932 if( p_vout && p_sys->p_spu_vout == p_vout )
934 /* Prerool does not work very well with subtitle */
935 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
936 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
937 spu_DestroySubpicture( p_vout->p_spu, p_spu );
939 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
943 msg_Warn( p_dec, "no vout found, leaking subpicture" );
946 vlc_object_release( p_vout );
951 msg_Err( p_dec, "unknown ES format" );
955 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
959 * Destroys a decoder object
961 * \param p_dec the decoder object
964 static void DeleteDecoder( decoder_t * p_dec )
966 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
967 (char*)&p_dec->fmt_in.i_codec,
968 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
970 /* Free all packets still in the decoder fifo. */
971 block_FifoEmpty( p_dec->p_owner->p_fifo );
972 block_FifoRelease( p_dec->p_owner->p_fifo );
975 if( p_dec->p_owner->p_aout_input )
976 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
978 if( p_dec->p_owner->p_vout )
982 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
983 /* Hack to make sure all the the pictures are freed by the decoder */
984 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
987 if( p_pic->i_status == RESERVED_PICTURE )
988 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
989 if( p_pic->i_refcount > 0 )
990 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
994 /* We are about to die. Reattach video output to p_vlc. */
995 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
998 if( p_dec->p_owner->p_sout_input )
1000 sout_InputDelete( p_dec->p_owner->p_sout_input );
1001 es_format_Clean( &p_dec->p_owner->sout );
1004 if( p_dec->fmt_in.i_cat == SPU_ES )
1006 vout_thread_t *p_vout;
1008 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1011 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1012 p_dec->p_owner->i_spu_channel );
1013 vlc_object_release( p_vout );
1017 es_format_Clean( &p_dec->fmt_in );
1018 es_format_Clean( &p_dec->fmt_out );
1020 if( p_dec->p_owner->p_packetizer )
1022 module_Unneed( p_dec->p_owner->p_packetizer,
1023 p_dec->p_owner->p_packetizer->p_module );
1024 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1025 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1026 vlc_object_detach( p_dec->p_owner->p_packetizer );
1027 vlc_object_destroy( p_dec->p_owner->p_packetizer );
1030 vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1032 vlc_object_detach( p_dec );
1034 free( p_dec->p_owner );
1037 /*****************************************************************************
1038 * Buffers allocation callbacks for the decoders
1039 *****************************************************************************/
1040 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1042 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1043 aout_buffer_t *p_buffer;
1045 if( p_sys->p_aout_input != NULL &&
1046 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1047 p_dec->fmt_out.audio.i_original_channels !=
1048 p_sys->audio.i_original_channels ||
1049 p_dec->fmt_out.audio.i_bytes_per_frame !=
1050 p_sys->audio.i_bytes_per_frame ) )
1052 /* Parameters changed, restart the aout */
1053 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1054 p_sys->p_aout_input = NULL;
1057 if( p_sys->p_aout_input == NULL )
1059 audio_sample_format_t format;
1060 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1062 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1063 p_sys->audio = p_dec->fmt_out.audio;
1065 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1066 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1067 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1069 if ( i_force_dolby == 1 )
1071 format.i_original_channels = format.i_original_channels |
1072 AOUT_CHAN_DOLBYSTEREO;
1074 else /* i_force_dolby == 2 */
1076 format.i_original_channels = format.i_original_channels &
1077 ~AOUT_CHAN_DOLBYSTEREO;
1081 p_sys->p_aout_input =
1082 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1083 if( p_sys->p_aout_input == NULL )
1085 msg_Err( p_dec, "failed to create audio output" );
1086 p_dec->b_error = VLC_TRUE;
1089 p_dec->fmt_out.audio.i_bytes_per_frame =
1090 p_sys->audio.i_bytes_per_frame;
1093 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
1099 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1101 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1102 p_dec->p_owner->p_aout_input, p_buffer );
1105 static picture_t *vout_new_buffer( decoder_t *p_dec )
1107 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1110 if( p_sys->p_vout == NULL ||
1111 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1112 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1113 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1114 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1116 if( !p_dec->fmt_out.video.i_width ||
1117 !p_dec->fmt_out.video.i_height )
1119 /* Can't create a new vout without display size */
1123 if( !p_dec->fmt_out.video.i_visible_width ||
1124 !p_dec->fmt_out.video.i_visible_height )
1126 if( p_dec->fmt_in.video.i_visible_width &&
1127 p_dec->fmt_in.video.i_visible_height )
1129 p_dec->fmt_out.video.i_visible_width =
1130 p_dec->fmt_in.video.i_visible_width;
1131 p_dec->fmt_out.video.i_visible_height =
1132 p_dec->fmt_in.video.i_visible_height;
1136 p_dec->fmt_out.video.i_visible_width =
1137 p_dec->fmt_out.video.i_width;
1138 p_dec->fmt_out.video.i_visible_height =
1139 p_dec->fmt_out.video.i_height;
1143 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1144 var_CreateGetBool( p_dec, "hdtv-fix" ) )
1146 p_dec->fmt_out.video.i_visible_height = 1080;
1147 p_dec->fmt_out.video.i_sar_num *= 135;
1148 p_dec->fmt_out.video.i_sar_den *= 136;
1149 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1152 if( !p_dec->fmt_out.video.i_sar_num ||
1153 !p_dec->fmt_out.video.i_sar_den )
1155 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1156 p_dec->fmt_out.video.i_visible_height;
1158 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1159 p_dec->fmt_out.video.i_visible_width;
1162 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1163 &p_dec->fmt_out.video.i_sar_den,
1164 p_dec->fmt_out.video.i_sar_num,
1165 p_dec->fmt_out.video.i_sar_den, 50000 );
1167 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1168 p_sys->video = p_dec->fmt_out.video;
1170 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1171 &p_dec->fmt_out.video );
1172 if( p_sys->p_vout == NULL )
1174 msg_Err( p_dec, "failed to create video output" );
1175 p_dec->b_error = VLC_TRUE;
1179 if( p_sys->video.i_rmask )
1180 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1181 if( p_sys->video.i_gmask )
1182 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1183 if( p_sys->video.i_bmask )
1184 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1187 /* Get a new picture */
1188 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1190 int i_pic, i_ready_pic = 0;
1192 if( p_dec->b_die || p_dec->b_error )
1197 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1198 /* Check the decoder doesn't leak pictures */
1199 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1202 if( p_pic->i_status == READY_PICTURE )
1204 if( i_ready_pic++ > 0 ) break;
1208 if( p_pic->i_status != DISPLAYED_PICTURE &&
1209 p_pic->i_status != RESERVED_PICTURE &&
1210 p_pic->i_status != READY_PICTURE ) break;
1212 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1215 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1217 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1219 /* Just free all the pictures */
1220 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1223 if( p_pic->i_status == RESERVED_PICTURE )
1224 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1225 if( p_pic->i_refcount > 0 )
1226 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1231 msleep( VOUT_OUTMEM_SLEEP );
1237 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1239 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1242 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1244 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1247 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1249 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1252 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1254 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1255 vout_thread_t *p_vout = NULL;
1256 subpicture_t *p_subpic;
1257 int i_attempts = 30;
1259 while( i_attempts-- )
1261 if( p_dec->b_die || p_dec->b_error ) break;
1263 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1266 msleep( VOUT_DISPLAY_DELAY );
1271 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1275 if( p_sys->p_spu_vout != p_vout )
1277 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1278 &p_sys->i_spu_channel );
1279 p_sys->p_spu_vout = p_vout;
1282 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1285 p_subpic->i_channel = p_sys->i_spu_channel;
1288 vlc_object_release( p_vout );
1293 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1295 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1296 vout_thread_t *p_vout = NULL;
1298 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1299 if( !p_vout || p_sys->p_spu_vout != p_vout )
1302 vlc_object_release( p_vout );
1303 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1307 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1309 vlc_object_release( p_vout );