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>
36 #include "stream_output.h"
37 #include "input_internal.h"
39 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
40 static void DeleteDecoder( decoder_t * );
42 static int DecoderThread( decoder_t * );
43 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
45 /* Buffers allocation callbacks for the decoders */
46 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
47 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
49 static picture_t *vout_new_buffer( decoder_t * );
50 static void vout_del_buffer( decoder_t *, picture_t * );
51 static void vout_link_picture( decoder_t *, picture_t * );
52 static void vout_unlink_picture( decoder_t *, picture_t * );
54 static subpicture_t *spu_new_buffer( decoder_t * );
55 static void spu_del_buffer( decoder_t *, subpicture_t * );
57 static es_format_t null_es_format = {0};
59 struct decoder_owner_sys_t
61 vlc_bool_t b_own_thread;
63 int64_t i_preroll_end;
65 input_thread_t *p_input;
67 aout_instance_t *p_aout;
68 aout_input_t *p_aout_input;
70 vout_thread_t *p_vout;
72 vout_thread_t *p_spu_vout;
75 sout_instance_t *p_sout;
76 sout_packetizer_input_t *p_sout_input;
78 /* Some decoders require already packetized data (ie. not truncated) */
79 decoder_t *p_packetizer;
81 /* Current format in use by the output */
92 * Spawns a new decoder thread
94 * \param p_input the input thread
95 * \param p_es the es descriptor
96 * \return the spawned decoder object
98 decoder_t *input_DecoderNew( input_thread_t *p_input,
99 es_format_t *fmt, vlc_bool_t b_force_decoder )
101 decoder_t *p_dec = NULL;
104 /* If we are in sout mode, search for packetizer module */
105 if( p_input->p_sout && !b_force_decoder )
107 /* Create the decoder configuration structure */
108 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
111 msg_Err( p_input, "could not create packetizer" );
117 /* Create the decoder configuration structure */
118 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
121 msg_Err( p_input, "could not create decoder" );
126 if( !p_dec->p_module )
128 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
129 "VLC probably does not support this sound or video format.",
130 (char*)&p_dec->fmt_in.i_codec );
132 DeleteDecoder( p_dec );
133 vlc_object_destroy( p_dec );
137 if( p_input->p_sout && p_input->input.b_can_pace_control &&
140 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
141 p_dec->p_owner->b_own_thread = VLC_FALSE;
145 var_Get( p_input, "minimize-threads", &val );
146 p_dec->p_owner->b_own_thread = !val.b_bool;
149 if( p_dec->p_owner->b_own_thread )
152 if( fmt->i_cat == AUDIO_ES )
153 i_priority = VLC_THREAD_PRIORITY_AUDIO;
155 i_priority = VLC_THREAD_PRIORITY_VIDEO;
157 /* Spawn the decoder thread */
158 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
159 i_priority, VLC_FALSE ) )
161 msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
162 p_dec->p_module->psz_object_name );
163 module_Unneed( p_dec, p_dec->p_module );
164 DeleteDecoder( p_dec );
165 vlc_object_destroy( p_dec );
174 * Kills a decoder thread and waits until it's finished
176 * \param p_input the input thread
177 * \param p_es the es descriptor
180 void input_DecoderDelete( decoder_t *p_dec )
182 p_dec->b_die = VLC_TRUE;
184 if( p_dec->p_owner->b_own_thread )
186 /* Make sure the thread leaves the function by
187 * sending it an empty block. */
188 block_t *p_block = block_New( p_dec, 0 );
189 input_DecoderDecode( p_dec, p_block );
191 vlc_thread_join( p_dec );
193 /* Don't module_Unneed() here because of the dll loader that wants
194 * close() in the same thread than open()/decode() */
199 input_DecoderDecode( p_dec, NULL );
201 module_Unneed( p_dec, p_dec->p_module );
204 /* Delete decoder configuration */
205 DeleteDecoder( p_dec );
207 /* Delete the decoder */
208 vlc_object_destroy( p_dec );
212 * Put a block_t in the decoder's fifo.
214 * \param p_dec the decoder object
215 * \param p_block the data block
217 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
219 if( p_dec->p_owner->b_own_thread )
221 if( p_dec->p_owner->p_input->b_out_pace_control )
224 while( !p_dec->b_die && !p_dec->b_error &&
225 p_dec->p_owner->p_fifo->i_depth > 10 )
230 else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
232 /* FIXME: ideally we would check the time amount of data
233 * in the fifo instead of its size. */
234 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
235 "consummed quickly enough), resetting fifo!" );
236 block_FifoEmpty( p_dec->p_owner->p_fifo );
239 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
243 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
245 if( p_block ) block_Release( p_block );
249 DecoderDecode( p_dec, p_block );
254 void input_DecoderDiscontinuity( decoder_t * p_dec )
259 if( p_dec->p_owner->b_own_thread )
261 block_FifoEmpty( p_dec->p_owner->p_fifo );
264 /* Send a special block */
265 p_null = block_New( p_dec, 128 );
266 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
267 memset( p_null->p_buffer, 0, p_null->i_buffer );
269 input_DecoderDecode( p_dec, p_null );
272 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
274 if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
281 void input_DecoderPreroll( decoder_t *p_dec, int64_t i_preroll_end )
283 p_dec->p_owner->i_preroll_end = i_preroll_end;
288 * Create a NULL packet for padding in case of a data loss
290 * \param p_input the input thread
291 * \param p_es es descriptor
294 static void input_NullPacket( input_thread_t * p_input,
295 es_descriptor_t * p_es )
298 block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
301 memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
302 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
304 block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
310 * Send a NULL packet to the decoders
312 * \param p_input the input thread
315 void input_EscapeDiscontinuity( input_thread_t * p_input )
318 unsigned int i_es, i;
320 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
322 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
324 if( p_es->p_dec != NULL )
326 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
328 input_NullPacket( p_input, p_es );
336 * Send a NULL packet to the audio decoders
338 * \param p_input the input thread
341 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
344 unsigned int i_es, i;
346 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
348 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
350 if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
352 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
354 input_NullPacket( p_input, p_es );
363 * Create a decoder object
365 * \param p_input the input thread
366 * \param p_es the es descriptor
367 * \param i_object_type Object type as define in include/vlc_objects.h
368 * \return the decoder object
370 static decoder_t * CreateDecoder( input_thread_t *p_input,
371 es_format_t *fmt, int i_object_type )
375 p_dec = vlc_object_create( p_input, i_object_type );
378 msg_Err( p_input, "out of memory" );
382 p_dec->pf_decode_audio = 0;
383 p_dec->pf_decode_video = 0;
384 p_dec->pf_decode_sub = 0;
385 p_dec->pf_packetize = 0;
387 stats_Create( p_dec, "decoded_audio", VLC_VAR_INTEGER, STATS_COUNTER );
388 stats_Create( p_dec, "decoded_video", VLC_VAR_INTEGER, STATS_COUNTER );
389 stats_Create( p_dec, "decoded_sub", VLC_VAR_INTEGER, STATS_COUNTER );
391 /* Initialize the decoder fifo */
392 p_dec->p_module = NULL;
395 es_format_Copy( &p_dec->fmt_in, fmt );
396 es_format_Copy( &p_dec->fmt_out, &null_es_format );
398 /* Allocate our private structure for the decoder */
399 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
400 if( p_dec->p_owner == NULL )
402 msg_Err( p_dec, "out of memory" );
405 p_dec->p_owner->b_own_thread = VLC_TRUE;
406 p_dec->p_owner->i_preroll_end = -1;
407 p_dec->p_owner->p_input = p_input;
408 p_dec->p_owner->p_aout = NULL;
409 p_dec->p_owner->p_aout_input = NULL;
410 p_dec->p_owner->p_vout = NULL;
411 p_dec->p_owner->p_spu_vout = NULL;
412 p_dec->p_owner->i_spu_channel = 0;
413 p_dec->p_owner->p_sout = p_input->p_sout;
414 p_dec->p_owner->p_sout_input = NULL;
415 p_dec->p_owner->p_packetizer = NULL;
418 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
420 msg_Err( p_dec, "out of memory" );
424 /* Set buffers allocation callbacks for the decoders */
425 p_dec->pf_aout_buffer_new = aout_new_buffer;
426 p_dec->pf_aout_buffer_del = aout_del_buffer;
427 p_dec->pf_vout_buffer_new = vout_new_buffer;
428 p_dec->pf_vout_buffer_del = vout_del_buffer;
429 p_dec->pf_picture_link = vout_link_picture;
430 p_dec->pf_picture_unlink = vout_unlink_picture;
431 p_dec->pf_spu_buffer_new = spu_new_buffer;
432 p_dec->pf_spu_buffer_del = spu_del_buffer;
434 vlc_object_attach( p_dec, p_input );
436 /* Find a suitable decoder/packetizer module */
437 if( i_object_type == VLC_OBJECT_DECODER )
438 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
440 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
442 /* Check if decoder requires already packetized data */
443 if( i_object_type == VLC_OBJECT_DECODER &&
444 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
446 p_dec->p_owner->p_packetizer =
447 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
448 if( p_dec->p_owner->p_packetizer )
450 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
453 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
456 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
458 p_dec->p_owner->p_packetizer->p_module =
459 module_Need( p_dec->p_owner->p_packetizer,
460 "packetizer", "$packetizer", 0 );
462 if( !p_dec->p_owner->p_packetizer->p_module )
464 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
465 vlc_object_detach( p_dec->p_owner->p_packetizer );
466 vlc_object_destroy( p_dec->p_owner->p_packetizer );
475 * The decoding main loop
477 * \param p_dec the decoder
480 static int DecoderThread( decoder_t * p_dec )
484 /* The decoder's main loop */
485 while( !p_dec->b_die && !p_dec->b_error )
487 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
492 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
498 while( !p_dec->b_die )
500 /* Trash all received PES packets */
501 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
502 if( p_block ) block_Release( p_block );
505 /* We do it here because of the dll loader that wants close() in the
506 * same thread than open()/decode() */
507 module_Unneed( p_dec, p_dec->p_module );
515 * \param p_dec the decoder object
516 * \param p_block the block to decode
517 * \return VLC_SUCCESS or an error code
519 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
521 int i_rate = p_block ? p_block->i_rate : 1000;
523 if( p_block && p_block->i_buffer <= 0 )
525 block_Release( p_block );
529 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
531 block_t *p_sout_block;
533 while( ( p_sout_block =
534 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
536 if( !p_dec->p_owner->p_sout_input )
538 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
540 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
541 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
542 if( p_dec->fmt_in.psz_language )
544 if( p_dec->p_owner->sout.psz_language )
545 free( p_dec->p_owner->sout.psz_language );
546 p_dec->p_owner->sout.psz_language =
547 strdup( p_dec->fmt_in.psz_language );
550 p_dec->p_owner->p_sout_input =
551 sout_InputNew( p_dec->p_owner->p_sout,
552 &p_dec->p_owner->sout );
554 if( p_dec->p_owner->p_sout_input == NULL )
556 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
557 (char *)&p_dec->p_owner->sout.i_codec );
558 p_dec->b_error = VLC_TRUE;
560 while( p_sout_block )
562 block_t *p_next = p_sout_block->p_next;
563 block_Release( p_sout_block );
564 p_sout_block = p_next;
570 while( p_sout_block )
572 block_t *p_next = p_sout_block->p_next;
574 p_sout_block->p_next = NULL;
575 p_sout_block->i_rate = i_rate;
577 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
580 p_sout_block = p_next;
583 /* For now it's enough, as only sout inpact on this flag */
584 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
585 p_dec->p_owner->p_input->b_out_pace_control )
587 msg_Dbg( p_dec, "switching to synch mode" );
588 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
590 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
591 !p_dec->p_owner->p_input->b_out_pace_control )
593 msg_Dbg( p_dec, "switching to asynch mode" );
594 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
598 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
600 aout_buffer_t *p_aout_buf;
602 if( p_dec->p_owner->p_packetizer )
604 block_t *p_packetized_block;
605 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
607 while( (p_packetized_block =
608 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
610 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
612 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
613 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
614 memcpy( p_dec->fmt_in.p_extra,
615 p_packetizer->fmt_out.p_extra,
616 p_dec->fmt_in.i_extra );
619 while( p_packetized_block )
621 block_t *p_next = p_packetized_block->p_next;
622 p_packetized_block->p_next = NULL;
623 p_packetized_block->i_rate = i_rate;
625 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
626 &p_packetized_block )) )
628 stats_UpdateInteger( p_dec, "decoded_audio", 1 );
629 /* FIXME the best would be to handle the case start_date < preroll < end_date
630 * but that's not easy with non raw audio stream */
631 if( p_dec->p_owner->i_preroll_end > 0 &&
632 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
634 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
635 p_dec->p_owner->p_aout_input, p_aout_buf );
639 p_dec->p_owner->i_preroll_end = -1;
640 aout_DecPlay( p_dec->p_owner->p_aout,
641 p_dec->p_owner->p_aout_input,
646 p_packetized_block = p_next;
650 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
652 stats_UpdateInteger( p_dec, "decoded_audio", 1 );
653 if( p_dec->p_owner->i_preroll_end > 0 &&
654 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
656 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
657 p_dec->p_owner->p_aout_input, p_aout_buf );
661 p_dec->p_owner->i_preroll_end = -1;
662 aout_DecPlay( p_dec->p_owner->p_aout,
663 p_dec->p_owner->p_aout_input,
668 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
672 if( p_dec->p_owner->p_packetizer )
674 block_t *p_packetized_block;
675 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
677 while( (p_packetized_block =
678 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
680 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
682 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
683 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
684 memcpy( p_dec->fmt_in.p_extra,
685 p_packetizer->fmt_out.p_extra,
686 p_dec->fmt_in.i_extra );
689 while( p_packetized_block )
691 block_t *p_next = p_packetized_block->p_next;
692 p_packetized_block->p_next = NULL;
693 p_packetized_block->i_rate = i_rate;
695 while( (p_pic = p_dec->pf_decode_video( p_dec,
696 &p_packetized_block )) )
698 stats_UpdateInteger( p_dec, "decoded_video", 1 );
699 if( p_dec->p_owner->i_preroll_end > 0 &&
700 p_pic->date < p_dec->p_owner->i_preroll_end )
702 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
706 p_dec->p_owner->i_preroll_end = -1;
707 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
709 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
713 p_packetized_block = p_next;
717 else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
719 stats_UpdateInteger( p_dec, "decoded_video", 1 );
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, p_pic->date );
729 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
733 else if( p_dec->fmt_in.i_cat == SPU_ES )
735 vout_thread_t *p_vout;
737 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
739 stats_UpdateInteger( p_dec, "decoded_sub", 1 );
740 if( p_dec->p_owner->i_preroll_end > 0 &&
741 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
742 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
744 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
748 p_dec->p_owner->i_preroll_end = -1;
749 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
752 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
753 vlc_object_release( p_vout );
759 msg_Err( p_dec, "unknown ES format" );
763 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
767 * Destroys a decoder object
769 * \param p_dec the decoder object
772 static void DeleteDecoder( decoder_t * p_dec )
774 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
775 (char*)&p_dec->fmt_in.i_codec,
776 p_dec->p_owner->p_fifo->i_depth );
778 /* Free all packets still in the decoder fifo. */
779 block_FifoEmpty( p_dec->p_owner->p_fifo );
780 block_FifoRelease( p_dec->p_owner->p_fifo );
783 if( p_dec->p_owner->p_aout_input )
784 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
786 if( p_dec->p_owner->p_vout )
790 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
791 /* Hack to make sure all the the pictures are freed by the decoder */
792 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
795 if( p_pic->i_status == RESERVED_PICTURE )
796 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
797 if( p_pic->i_refcount > 0 )
798 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
802 /* We are about to die. Reattach video output to p_vlc. */
803 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
806 if( p_dec->p_owner->p_sout_input )
808 sout_InputDelete( p_dec->p_owner->p_sout_input );
809 es_format_Clean( &p_dec->p_owner->sout );
812 if( p_dec->fmt_in.i_cat == SPU_ES )
814 vout_thread_t *p_vout;
816 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
819 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
820 p_dec->p_owner->i_spu_channel );
821 vlc_object_release( p_vout );
825 es_format_Clean( &p_dec->fmt_in );
826 es_format_Clean( &p_dec->fmt_out );
828 if( p_dec->p_owner->p_packetizer )
830 module_Unneed( p_dec->p_owner->p_packetizer,
831 p_dec->p_owner->p_packetizer->p_module );
832 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
833 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
834 vlc_object_detach( p_dec->p_owner->p_packetizer );
835 vlc_object_destroy( p_dec->p_owner->p_packetizer );
838 vlc_object_detach( p_dec );
840 free( p_dec->p_owner );
843 /*****************************************************************************
844 * Buffers allocation callbacks for the decoders
845 *****************************************************************************/
846 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
848 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
849 aout_buffer_t *p_buffer;
851 if( p_sys->p_aout_input != NULL &&
852 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
853 p_dec->fmt_out.audio.i_original_channels !=
854 p_sys->audio.i_original_channels ||
855 p_dec->fmt_out.audio.i_bytes_per_frame !=
856 p_sys->audio.i_bytes_per_frame ) )
858 /* Parameters changed, restart the aout */
859 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
860 p_sys->p_aout_input = NULL;
863 if( p_sys->p_aout_input == NULL )
865 audio_sample_format_t format;
866 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
868 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
869 p_sys->audio = p_dec->fmt_out.audio;
871 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
872 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
873 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
875 if ( i_force_dolby == 1 )
877 format.i_original_channels = format.i_original_channels |
878 AOUT_CHAN_DOLBYSTEREO;
880 else /* i_force_dolby == 2 */
882 format.i_original_channels = format.i_original_channels &
883 ~AOUT_CHAN_DOLBYSTEREO;
887 p_sys->p_aout_input =
888 aout_DecNew( p_dec, &p_sys->p_aout, &format );
889 if( p_sys->p_aout_input == NULL )
891 msg_Err( p_dec, "failed to create audio output" );
892 p_dec->b_error = VLC_TRUE;
895 p_dec->fmt_out.audio.i_bytes_per_frame =
896 p_sys->audio.i_bytes_per_frame;
899 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
905 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
907 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
908 p_dec->p_owner->p_aout_input, p_buffer );
911 static picture_t *vout_new_buffer( decoder_t *p_dec )
913 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
916 if( p_sys->p_vout == NULL ||
917 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
918 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
919 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
920 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
922 if( !p_dec->fmt_out.video.i_width ||
923 !p_dec->fmt_out.video.i_height )
925 /* Can't create a new vout without display size */
929 if( !p_dec->fmt_out.video.i_visible_width ||
930 !p_dec->fmt_out.video.i_visible_height )
932 p_dec->fmt_out.video.i_visible_width =
933 p_dec->fmt_out.video.i_width;
934 p_dec->fmt_out.video.i_visible_height =
935 p_dec->fmt_out.video.i_height;
938 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
939 var_CreateGetBool( p_dec, "hdtv-fix" ) )
941 p_dec->fmt_out.video.i_visible_height = 1080;
942 p_dec->fmt_out.video.i_sar_num *= 135;
943 p_dec->fmt_out.video.i_sar_den *= 136;
944 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
947 if( !p_dec->fmt_out.video.i_sar_num ||
948 !p_dec->fmt_out.video.i_sar_den )
950 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
951 p_dec->fmt_out.video.i_visible_height;
953 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
954 p_dec->fmt_out.video.i_visible_width;
957 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
958 &p_dec->fmt_out.video.i_sar_den,
959 p_dec->fmt_out.video.i_sar_num,
960 p_dec->fmt_out.video.i_sar_den, 50000 );
962 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
963 p_sys->video = p_dec->fmt_out.video;
965 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
966 &p_dec->fmt_out.video );
967 if( p_sys->p_vout == NULL )
969 msg_Err( p_dec, "failed to create video output" );
970 p_dec->b_error = VLC_TRUE;
974 if( p_sys->video.i_rmask )
975 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
976 if( p_sys->video.i_gmask )
977 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
978 if( p_sys->video.i_bmask )
979 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
982 /* Get a new picture */
983 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
985 int i_pic, i_ready_pic = 0;
987 if( p_dec->b_die || p_dec->b_error )
992 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
993 /* Check the decoder doesn't leak pictures */
994 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
997 if( p_pic->i_status == READY_PICTURE )
999 if( i_ready_pic++ > 0 ) break;
1003 if( p_pic->i_status != DISPLAYED_PICTURE &&
1004 p_pic->i_status != RESERVED_PICTURE &&
1005 p_pic->i_status != READY_PICTURE ) break;
1007 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1010 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1012 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1014 /* Just free all the pictures */
1015 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1018 if( p_pic->i_status == RESERVED_PICTURE )
1019 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1020 if( p_pic->i_refcount > 0 )
1021 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1026 msleep( VOUT_OUTMEM_SLEEP );
1032 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1034 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1037 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1039 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1042 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1044 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1047 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1049 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1050 vout_thread_t *p_vout = NULL;
1051 subpicture_t *p_subpic;
1052 int i_attempts = 30;
1054 while( i_attempts-- )
1056 if( p_dec->b_die || p_dec->b_error ) break;
1058 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1061 msleep( VOUT_DISPLAY_DELAY );
1066 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1070 if( p_sys->p_spu_vout != p_vout )
1072 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1073 &p_sys->i_spu_channel );
1074 p_sys->p_spu_vout = p_vout;
1077 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1080 p_subpic->i_channel = p_sys->i_spu_channel;
1083 vlc_object_release( p_vout );
1088 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1090 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1091 vout_thread_t *p_vout = NULL;
1093 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1094 if( !p_vout || p_sys->p_spu_vout != p_vout )
1097 vlc_object_release( p_vout );
1098 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1102 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1104 vlc_object_release( p_vout );