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 *****************************************************************************/
32 #include <vlc/decoder.h>
34 #include <vlc/input.h>
35 #include <vlc_interaction.h>
37 #include "stream_output.h"
38 #include "input_internal.h"
40 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
41 static void DeleteDecoder( decoder_t * );
43 static int DecoderThread( decoder_t * );
44 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
46 /* Buffers allocation callbacks for the decoders */
47 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
48 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
50 static picture_t *vout_new_buffer( decoder_t * );
51 static void vout_del_buffer( decoder_t *, picture_t * );
52 static void vout_link_picture( decoder_t *, picture_t * );
53 static void vout_unlink_picture( decoder_t *, picture_t * );
55 static subpicture_t *spu_new_buffer( decoder_t * );
56 static void spu_del_buffer( decoder_t *, subpicture_t * );
58 static es_format_t null_es_format = {0};
60 struct decoder_owner_sys_t
62 vlc_bool_t b_own_thread;
64 int64_t i_preroll_end;
66 input_thread_t *p_input;
68 aout_instance_t *p_aout;
69 aout_input_t *p_aout_input;
71 vout_thread_t *p_vout;
73 vout_thread_t *p_spu_vout;
76 sout_instance_t *p_sout;
77 sout_packetizer_input_t *p_sout_input;
79 /* Some decoders require already packetized data (ie. not truncated) */
80 decoder_t *p_packetizer;
82 /* Current format in use by the output */
93 * Spawns a new decoder thread
95 * \param p_input the input thread
96 * \param p_es the es descriptor
97 * \return the spawned decoder object
99 decoder_t *input_DecoderNew( input_thread_t *p_input,
100 es_format_t *fmt, vlc_bool_t b_force_decoder )
102 decoder_t *p_dec = NULL;
105 /* If we are in sout mode, search for packetizer module */
106 if( p_input->p_sout && !b_force_decoder )
108 /* Create the decoder configuration structure */
109 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
112 msg_Err( p_input, "could not create packetizer" );
113 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
114 _("VLC could not open the packetizer module.") );
120 /* Create the decoder configuration structure */
121 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
124 msg_Err( p_input, "could not create decoder" );
125 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
126 _("VLC could not open the decoder module.") );
131 if( !p_dec->p_module )
133 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
134 "VLC probably does not support this sound or video format.",
135 (char*)&p_dec->fmt_in.i_codec );
136 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
137 "for FOURCC \"4.4s\" found"), _("VLC probably does not support this "
138 "audio or video format. Regrettably, there is no way for you to "
141 DeleteDecoder( p_dec );
142 vlc_object_destroy( p_dec );
146 if( p_input->p_sout && p_input->input.b_can_pace_control &&
149 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
150 p_dec->p_owner->b_own_thread = VLC_FALSE;
154 var_Get( p_input, "minimize-threads", &val );
155 p_dec->p_owner->b_own_thread = !val.b_bool;
158 if( p_dec->p_owner->b_own_thread )
161 if( fmt->i_cat == AUDIO_ES )
162 i_priority = VLC_THREAD_PRIORITY_AUDIO;
164 i_priority = VLC_THREAD_PRIORITY_VIDEO;
166 /* Spawn the decoder thread */
167 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
168 i_priority, VLC_FALSE ) )
170 msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
171 p_dec->p_module->psz_object_name );
172 module_Unneed( p_dec, p_dec->p_module );
173 DeleteDecoder( p_dec );
174 vlc_object_destroy( p_dec );
183 * Kills a decoder thread and waits until it's finished
185 * \param p_input the input thread
186 * \param p_es the es descriptor
189 void input_DecoderDelete( decoder_t *p_dec )
191 p_dec->b_die = VLC_TRUE;
193 if( p_dec->p_owner->b_own_thread )
195 /* Make sure the thread leaves the function by
196 * sending it an empty block. */
197 block_t *p_block = block_New( p_dec, 0 );
198 input_DecoderDecode( p_dec, p_block );
200 vlc_thread_join( p_dec );
202 /* Don't module_Unneed() here because of the dll loader that wants
203 * close() in the same thread than open()/decode() */
208 input_DecoderDecode( p_dec, NULL );
210 module_Unneed( p_dec, p_dec->p_module );
213 /* Delete decoder configuration */
214 DeleteDecoder( p_dec );
216 /* Delete the decoder */
217 vlc_object_destroy( p_dec );
221 * Put a block_t in the decoder's fifo.
223 * \param p_dec the decoder object
224 * \param p_block the data block
226 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
228 if( p_dec->p_owner->b_own_thread )
230 if( p_dec->p_owner->p_input->b_out_pace_control )
233 while( !p_dec->b_die && !p_dec->b_error &&
234 p_dec->p_owner->p_fifo->i_depth > 10 )
239 else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
241 /* FIXME: ideally we would check the time amount of data
242 * in the fifo instead of its size. */
243 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
244 "consumed quickly enough), resetting fifo!" );
245 block_FifoEmpty( p_dec->p_owner->p_fifo );
248 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
252 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
254 if( p_block ) block_Release( p_block );
258 DecoderDecode( p_dec, p_block );
263 void input_DecoderDiscontinuity( decoder_t * p_dec )
268 if( p_dec->p_owner->b_own_thread )
270 block_FifoEmpty( p_dec->p_owner->p_fifo );
273 /* Send a special block */
274 p_null = block_New( p_dec, 128 );
275 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
276 memset( p_null->p_buffer, 0, p_null->i_buffer );
278 input_DecoderDecode( p_dec, p_null );
281 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
283 if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
290 void input_DecoderPreroll( decoder_t *p_dec, int64_t i_preroll_end )
292 p_dec->p_owner->i_preroll_end = i_preroll_end;
297 * Create a NULL packet for padding in case of a data loss
299 * \param p_input the input thread
300 * \param p_es es descriptor
303 static void input_NullPacket( input_thread_t * p_input,
304 es_descriptor_t * p_es )
307 block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
310 memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
311 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
313 block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
319 * Send a NULL packet to the decoders
321 * \param p_input the input thread
324 void input_EscapeDiscontinuity( input_thread_t * p_input )
327 unsigned int i_es, i;
329 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
331 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
333 if( p_es->p_dec != NULL )
335 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
337 input_NullPacket( p_input, p_es );
345 * Send a NULL packet to the audio decoders
347 * \param p_input the input thread
350 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
353 unsigned int i_es, i;
355 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
357 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
359 if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
361 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
363 input_NullPacket( p_input, p_es );
372 * Create a decoder object
374 * \param p_input the input thread
375 * \param p_es the es descriptor
376 * \param i_object_type Object type as define in include/vlc_objects.h
377 * \return the decoder object
379 static decoder_t * CreateDecoder( input_thread_t *p_input,
380 es_format_t *fmt, int i_object_type )
384 p_dec = vlc_object_create( p_input, i_object_type );
387 msg_Err( p_input, "out of memory" );
391 p_dec->pf_decode_audio = 0;
392 p_dec->pf_decode_video = 0;
393 p_dec->pf_decode_sub = 0;
394 p_dec->pf_packetize = 0;
396 /* Initialize the decoder fifo */
397 p_dec->p_module = NULL;
400 es_format_Copy( &p_dec->fmt_in, fmt );
401 es_format_Copy( &p_dec->fmt_out, &null_es_format );
403 /* Allocate our private structure for the decoder */
404 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
405 if( p_dec->p_owner == NULL )
407 msg_Err( p_dec, "out of memory" );
410 p_dec->p_owner->b_own_thread = VLC_TRUE;
411 p_dec->p_owner->i_preroll_end = -1;
412 p_dec->p_owner->p_input = p_input;
413 p_dec->p_owner->p_aout = NULL;
414 p_dec->p_owner->p_aout_input = NULL;
415 p_dec->p_owner->p_vout = NULL;
416 p_dec->p_owner->p_spu_vout = NULL;
417 p_dec->p_owner->i_spu_channel = 0;
418 p_dec->p_owner->p_sout = p_input->p_sout;
419 p_dec->p_owner->p_sout_input = NULL;
420 p_dec->p_owner->p_packetizer = NULL;
423 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
425 msg_Err( p_dec, "out of memory" );
429 /* Set buffers allocation callbacks for the decoders */
430 p_dec->pf_aout_buffer_new = aout_new_buffer;
431 p_dec->pf_aout_buffer_del = aout_del_buffer;
432 p_dec->pf_vout_buffer_new = vout_new_buffer;
433 p_dec->pf_vout_buffer_del = vout_del_buffer;
434 p_dec->pf_picture_link = vout_link_picture;
435 p_dec->pf_picture_unlink = vout_unlink_picture;
436 p_dec->pf_spu_buffer_new = spu_new_buffer;
437 p_dec->pf_spu_buffer_del = spu_del_buffer;
439 vlc_object_attach( p_dec, p_input );
441 /* Find a suitable decoder/packetizer module */
442 if( i_object_type == VLC_OBJECT_DECODER )
443 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
445 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
447 /* Check if decoder requires already packetized data */
448 if( i_object_type == VLC_OBJECT_DECODER &&
449 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
451 p_dec->p_owner->p_packetizer =
452 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
453 if( p_dec->p_owner->p_packetizer )
455 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
458 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
461 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
463 p_dec->p_owner->p_packetizer->p_module =
464 module_Need( p_dec->p_owner->p_packetizer,
465 "packetizer", "$packetizer", 0 );
467 if( !p_dec->p_owner->p_packetizer->p_module )
469 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
470 vlc_object_detach( p_dec->p_owner->p_packetizer );
471 vlc_object_destroy( p_dec->p_owner->p_packetizer );
480 * The decoding main loop
482 * \param p_dec the decoder
485 static int DecoderThread( decoder_t * p_dec )
489 /* The decoder's main loop */
490 while( !p_dec->b_die && !p_dec->b_error )
492 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
497 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
503 while( !p_dec->b_die )
505 /* Trash all received PES packets */
506 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
507 if( p_block ) block_Release( p_block );
510 /* We do it here because of the dll loader that wants close() in the
511 * same thread than open()/decode() */
512 module_Unneed( p_dec, p_dec->p_module );
520 * \param p_dec the decoder object
521 * \param p_block the block to decode
522 * \return VLC_SUCCESS or an error code
524 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
526 int i_rate = p_block ? p_block->i_rate : 1000;
528 if( p_block && p_block->i_buffer <= 0 )
530 block_Release( p_block );
534 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
536 block_t *p_sout_block;
538 while( ( p_sout_block =
539 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
541 if( !p_dec->p_owner->p_sout_input )
543 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
545 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
546 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
547 if( p_dec->fmt_in.psz_language )
549 if( p_dec->p_owner->sout.psz_language )
550 free( p_dec->p_owner->sout.psz_language );
551 p_dec->p_owner->sout.psz_language =
552 strdup( p_dec->fmt_in.psz_language );
555 p_dec->p_owner->p_sout_input =
556 sout_InputNew( p_dec->p_owner->p_sout,
557 &p_dec->p_owner->sout );
559 if( p_dec->p_owner->p_sout_input == NULL )
561 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
562 (char *)&p_dec->p_owner->sout.i_codec );
563 p_dec->b_error = VLC_TRUE;
565 while( p_sout_block )
567 block_t *p_next = p_sout_block->p_next;
568 block_Release( p_sout_block );
569 p_sout_block = p_next;
575 while( p_sout_block )
577 block_t *p_next = p_sout_block->p_next;
579 p_sout_block->p_next = NULL;
580 p_sout_block->i_rate = i_rate;
582 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
585 p_sout_block = p_next;
588 /* For now it's enough, as only sout inpact on this flag */
589 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
590 p_dec->p_owner->p_input->b_out_pace_control )
592 msg_Dbg( p_dec, "switching to synch mode" );
593 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
595 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
596 !p_dec->p_owner->p_input->b_out_pace_control )
598 msg_Dbg( p_dec, "switching to asynch mode" );
599 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
603 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
605 aout_buffer_t *p_aout_buf;
607 if( p_dec->p_owner->p_packetizer )
609 block_t *p_packetized_block;
610 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
612 while( (p_packetized_block =
613 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
615 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
617 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
618 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
619 memcpy( p_dec->fmt_in.p_extra,
620 p_packetizer->fmt_out.p_extra,
621 p_dec->fmt_in.i_extra );
624 while( p_packetized_block )
626 block_t *p_next = p_packetized_block->p_next;
627 p_packetized_block->p_next = NULL;
628 p_packetized_block->i_rate = i_rate;
630 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
631 &p_packetized_block )) )
633 input_thread_t *p_i =(input_thread_t*)(p_dec->p_parent);
634 vlc_mutex_lock( &p_i->counters.counters_lock );
635 stats_UpdateInteger( p_dec,
636 p_i->counters.p_decoded_audio, 1, NULL );
637 vlc_mutex_unlock( &p_i->counters.counters_lock );
639 /* FIXME the best would be to handle the case
640 * start_date < preroll < end_date
641 * but that's not easy with non raw audio stream */
642 if( p_dec->p_owner->i_preroll_end > 0 &&
643 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
645 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
646 p_dec->p_owner->p_aout_input, p_aout_buf );
650 p_dec->p_owner->i_preroll_end = -1;
651 aout_DecPlay( p_dec->p_owner->p_aout,
652 p_dec->p_owner->p_aout_input,
657 p_packetized_block = p_next;
661 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
663 input_thread_t *p_i = (input_thread_t*)(p_dec->p_parent);
664 vlc_mutex_lock( &p_i->counters.counters_lock );
665 stats_UpdateInteger( p_dec,
666 p_i->counters.p_decoded_audio, 1, NULL );
667 vlc_mutex_unlock( &p_i->counters.counters_lock );
669 if( p_dec->p_owner->i_preroll_end > 0 &&
670 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
672 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
673 p_dec->p_owner->p_aout_input, p_aout_buf );
677 p_dec->p_owner->i_preroll_end = -1;
678 aout_DecPlay( p_dec->p_owner->p_aout,
679 p_dec->p_owner->p_aout_input,
684 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
688 if( p_dec->p_owner->p_packetizer )
690 block_t *p_packetized_block;
691 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
693 while( (p_packetized_block =
694 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
696 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
698 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
699 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
700 memcpy( p_dec->fmt_in.p_extra,
701 p_packetizer->fmt_out.p_extra,
702 p_dec->fmt_in.i_extra );
705 while( p_packetized_block )
707 block_t *p_next = p_packetized_block->p_next;
708 p_packetized_block->p_next = NULL;
709 p_packetized_block->i_rate = i_rate;
711 while( (p_pic = p_dec->pf_decode_video( p_dec,
712 &p_packetized_block )) )
714 input_thread_t *p_i =(input_thread_t*)(p_dec->p_parent);
715 vlc_mutex_lock( &p_i->counters.counters_lock );
716 stats_UpdateInteger( p_dec,
717 p_i->counters.p_decoded_video, 1, NULL );
718 vlc_mutex_unlock( &p_i->counters.counters_lock );
720 if( p_dec->p_owner->i_preroll_end > 0 &&
721 p_pic->date < p_dec->p_owner->i_preroll_end )
723 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
727 p_dec->p_owner->i_preroll_end = -1;
728 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
730 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
734 p_packetized_block = p_next;
738 else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
740 input_thread_t *p_i = (input_thread_t*)(p_dec->p_parent);
741 vlc_mutex_lock( &p_i->counters.counters_lock );
742 stats_UpdateInteger( p_dec,
743 p_i->counters.p_decoded_video, 1, NULL );
744 vlc_mutex_unlock( &p_i->counters.counters_lock );
746 if( p_dec->p_owner->i_preroll_end > 0 &&
747 p_pic->date < p_dec->p_owner->i_preroll_end )
749 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
753 p_dec->p_owner->i_preroll_end = -1;
754 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
755 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
759 else if( p_dec->fmt_in.i_cat == SPU_ES )
761 vout_thread_t *p_vout;
763 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
765 input_thread_t *p_i = (input_thread_t*)(p_dec->p_parent);
766 vlc_mutex_lock( &p_i->counters.counters_lock );
767 stats_UpdateInteger( p_dec,
768 p_i->counters.p_decoded_sub, 1, NULL );
769 vlc_mutex_unlock( &p_i->counters.counters_lock );
771 if( p_dec->p_owner->i_preroll_end > 0 &&
772 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
773 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
775 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
779 p_dec->p_owner->i_preroll_end = -1;
780 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
783 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
784 vlc_object_release( p_vout );
790 msg_Err( p_dec, "unknown ES format" );
794 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
798 * Destroys a decoder object
800 * \param p_dec the decoder object
803 static void DeleteDecoder( decoder_t * p_dec )
805 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
806 (char*)&p_dec->fmt_in.i_codec,
807 p_dec->p_owner->p_fifo->i_depth );
809 /* Free all packets still in the decoder fifo. */
810 block_FifoEmpty( p_dec->p_owner->p_fifo );
811 block_FifoRelease( p_dec->p_owner->p_fifo );
814 if( p_dec->p_owner->p_aout_input )
815 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
817 if( p_dec->p_owner->p_vout )
821 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
822 /* Hack to make sure all the the pictures are freed by the decoder */
823 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
826 if( p_pic->i_status == RESERVED_PICTURE )
827 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
828 if( p_pic->i_refcount > 0 )
829 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
833 /* We are about to die. Reattach video output to p_vlc. */
834 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
837 if( p_dec->p_owner->p_sout_input )
839 sout_InputDelete( p_dec->p_owner->p_sout_input );
840 es_format_Clean( &p_dec->p_owner->sout );
843 if( p_dec->fmt_in.i_cat == SPU_ES )
845 vout_thread_t *p_vout;
847 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
850 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
851 p_dec->p_owner->i_spu_channel );
852 vlc_object_release( p_vout );
856 es_format_Clean( &p_dec->fmt_in );
857 es_format_Clean( &p_dec->fmt_out );
859 if( p_dec->p_owner->p_packetizer )
861 module_Unneed( p_dec->p_owner->p_packetizer,
862 p_dec->p_owner->p_packetizer->p_module );
863 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
864 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
865 vlc_object_detach( p_dec->p_owner->p_packetizer );
866 vlc_object_destroy( p_dec->p_owner->p_packetizer );
869 vlc_object_detach( p_dec );
871 free( p_dec->p_owner );
874 /*****************************************************************************
875 * Buffers allocation callbacks for the decoders
876 *****************************************************************************/
877 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
879 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
880 aout_buffer_t *p_buffer;
882 if( p_sys->p_aout_input != NULL &&
883 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
884 p_dec->fmt_out.audio.i_original_channels !=
885 p_sys->audio.i_original_channels ||
886 p_dec->fmt_out.audio.i_bytes_per_frame !=
887 p_sys->audio.i_bytes_per_frame ) )
889 /* Parameters changed, restart the aout */
890 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
891 p_sys->p_aout_input = NULL;
894 if( p_sys->p_aout_input == NULL )
896 audio_sample_format_t format;
897 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
899 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
900 p_sys->audio = p_dec->fmt_out.audio;
902 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
903 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
904 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
906 if ( i_force_dolby == 1 )
908 format.i_original_channels = format.i_original_channels |
909 AOUT_CHAN_DOLBYSTEREO;
911 else /* i_force_dolby == 2 */
913 format.i_original_channels = format.i_original_channels &
914 ~AOUT_CHAN_DOLBYSTEREO;
918 p_sys->p_aout_input =
919 aout_DecNew( p_dec, &p_sys->p_aout, &format );
920 if( p_sys->p_aout_input == NULL )
922 msg_Err( p_dec, "failed to create audio output" );
923 p_dec->b_error = VLC_TRUE;
926 p_dec->fmt_out.audio.i_bytes_per_frame =
927 p_sys->audio.i_bytes_per_frame;
930 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
936 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
938 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
939 p_dec->p_owner->p_aout_input, p_buffer );
942 static picture_t *vout_new_buffer( decoder_t *p_dec )
944 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
947 if( p_sys->p_vout == NULL ||
948 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
949 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
950 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
951 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
953 if( !p_dec->fmt_out.video.i_width ||
954 !p_dec->fmt_out.video.i_height )
956 /* Can't create a new vout without display size */
960 if( !p_dec->fmt_out.video.i_visible_width ||
961 !p_dec->fmt_out.video.i_visible_height )
963 if( p_dec->fmt_in.video.i_visible_width &&
964 p_dec->fmt_in.video.i_visible_height )
966 p_dec->fmt_out.video.i_visible_width =
967 p_dec->fmt_in.video.i_visible_width;
968 p_dec->fmt_out.video.i_visible_height =
969 p_dec->fmt_in.video.i_visible_height;
973 p_dec->fmt_out.video.i_visible_width =
974 p_dec->fmt_out.video.i_width;
975 p_dec->fmt_out.video.i_visible_height =
976 p_dec->fmt_out.video.i_height;
980 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
981 var_CreateGetBool( p_dec, "hdtv-fix" ) )
983 p_dec->fmt_out.video.i_visible_height = 1080;
984 p_dec->fmt_out.video.i_sar_num *= 135;
985 p_dec->fmt_out.video.i_sar_den *= 136;
986 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
989 if( !p_dec->fmt_out.video.i_sar_num ||
990 !p_dec->fmt_out.video.i_sar_den )
992 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
993 p_dec->fmt_out.video.i_visible_height;
995 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
996 p_dec->fmt_out.video.i_visible_width;
999 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1000 &p_dec->fmt_out.video.i_sar_den,
1001 p_dec->fmt_out.video.i_sar_num,
1002 p_dec->fmt_out.video.i_sar_den, 50000 );
1004 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1005 p_sys->video = p_dec->fmt_out.video;
1007 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1008 &p_dec->fmt_out.video );
1009 if( p_sys->p_vout == NULL )
1011 msg_Err( p_dec, "failed to create video output" );
1012 p_dec->b_error = VLC_TRUE;
1016 if( p_sys->video.i_rmask )
1017 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1018 if( p_sys->video.i_gmask )
1019 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1020 if( p_sys->video.i_bmask )
1021 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1024 /* Get a new picture */
1025 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1027 int i_pic, i_ready_pic = 0;
1029 if( p_dec->b_die || p_dec->b_error )
1034 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1035 /* Check the decoder doesn't leak pictures */
1036 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1039 if( p_pic->i_status == READY_PICTURE )
1041 if( i_ready_pic++ > 0 ) break;
1045 if( p_pic->i_status != DISPLAYED_PICTURE &&
1046 p_pic->i_status != RESERVED_PICTURE &&
1047 p_pic->i_status != READY_PICTURE ) break;
1049 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1052 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1054 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1056 /* Just free all the pictures */
1057 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1060 if( p_pic->i_status == RESERVED_PICTURE )
1061 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1062 if( p_pic->i_refcount > 0 )
1063 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1068 msleep( VOUT_OUTMEM_SLEEP );
1074 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1076 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1079 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1081 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1084 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1086 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1089 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1091 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1092 vout_thread_t *p_vout = NULL;
1093 subpicture_t *p_subpic;
1094 int i_attempts = 30;
1096 while( i_attempts-- )
1098 if( p_dec->b_die || p_dec->b_error ) break;
1100 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1103 msleep( VOUT_DISPLAY_DELAY );
1108 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1112 if( p_sys->p_spu_vout != p_vout )
1114 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1115 &p_sys->i_spu_channel );
1116 p_sys->p_spu_vout = p_vout;
1119 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1122 p_subpic->i_channel = p_sys->i_spu_channel;
1125 vlc_object_release( p_vout );
1130 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1132 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1133 vout_thread_t *p_vout = NULL;
1135 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1136 if( !p_vout || p_sys->p_spu_vout != p_vout )
1139 vlc_object_release( p_vout );
1140 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1144 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1146 vlc_object_release( p_vout );