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 /* Initialize the decoder fifo */
388 p_dec->p_module = NULL;
391 es_format_Copy( &p_dec->fmt_in, fmt );
392 es_format_Copy( &p_dec->fmt_out, &null_es_format );
394 /* Allocate our private structure for the decoder */
395 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
396 if( p_dec->p_owner == NULL )
398 msg_Err( p_dec, "out of memory" );
401 p_dec->p_owner->b_own_thread = VLC_TRUE;
402 p_dec->p_owner->i_preroll_end = -1;
403 p_dec->p_owner->p_input = p_input;
404 p_dec->p_owner->p_aout = NULL;
405 p_dec->p_owner->p_aout_input = NULL;
406 p_dec->p_owner->p_vout = NULL;
407 p_dec->p_owner->p_spu_vout = NULL;
408 p_dec->p_owner->i_spu_channel = 0;
409 p_dec->p_owner->p_sout = p_input->p_sout;
410 p_dec->p_owner->p_sout_input = NULL;
411 p_dec->p_owner->p_packetizer = NULL;
414 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
416 msg_Err( p_dec, "out of memory" );
420 /* Set buffers allocation callbacks for the decoders */
421 p_dec->pf_aout_buffer_new = aout_new_buffer;
422 p_dec->pf_aout_buffer_del = aout_del_buffer;
423 p_dec->pf_vout_buffer_new = vout_new_buffer;
424 p_dec->pf_vout_buffer_del = vout_del_buffer;
425 p_dec->pf_picture_link = vout_link_picture;
426 p_dec->pf_picture_unlink = vout_unlink_picture;
427 p_dec->pf_spu_buffer_new = spu_new_buffer;
428 p_dec->pf_spu_buffer_del = spu_del_buffer;
430 vlc_object_attach( p_dec, p_input );
432 stats_Create( p_dec->p_parent, "decoded_audio",
433 VLC_VAR_INTEGER, STATS_COUNTER );
434 stats_Create( p_dec->p_parent, "decoded_video",
435 VLC_VAR_INTEGER, STATS_COUNTER );
436 stats_Create( p_dec->p_parent, "decoded_sub",
437 VLC_VAR_INTEGER, STATS_COUNTER );
438 /* Find a suitable decoder/packetizer module */
439 if( i_object_type == VLC_OBJECT_DECODER )
440 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
442 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
444 /* Check if decoder requires already packetized data */
445 if( i_object_type == VLC_OBJECT_DECODER &&
446 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
448 p_dec->p_owner->p_packetizer =
449 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
450 if( p_dec->p_owner->p_packetizer )
452 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
455 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
458 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
460 p_dec->p_owner->p_packetizer->p_module =
461 module_Need( p_dec->p_owner->p_packetizer,
462 "packetizer", "$packetizer", 0 );
464 if( !p_dec->p_owner->p_packetizer->p_module )
466 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
467 vlc_object_detach( p_dec->p_owner->p_packetizer );
468 vlc_object_destroy( p_dec->p_owner->p_packetizer );
477 * The decoding main loop
479 * \param p_dec the decoder
482 static int DecoderThread( decoder_t * p_dec )
486 /* The decoder's main loop */
487 while( !p_dec->b_die && !p_dec->b_error )
489 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
494 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
500 while( !p_dec->b_die )
502 /* Trash all received PES packets */
503 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
504 if( p_block ) block_Release( p_block );
507 /* We do it here because of the dll loader that wants close() in the
508 * same thread than open()/decode() */
509 module_Unneed( p_dec, p_dec->p_module );
517 * \param p_dec the decoder object
518 * \param p_block the block to decode
519 * \return VLC_SUCCESS or an error code
521 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
523 int i_rate = p_block ? p_block->i_rate : 1000;
525 if( p_block && p_block->i_buffer <= 0 )
527 block_Release( p_block );
531 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
533 block_t *p_sout_block;
535 while( ( p_sout_block =
536 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
538 if( !p_dec->p_owner->p_sout_input )
540 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
542 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
543 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
544 if( p_dec->fmt_in.psz_language )
546 if( p_dec->p_owner->sout.psz_language )
547 free( p_dec->p_owner->sout.psz_language );
548 p_dec->p_owner->sout.psz_language =
549 strdup( p_dec->fmt_in.psz_language );
552 p_dec->p_owner->p_sout_input =
553 sout_InputNew( p_dec->p_owner->p_sout,
554 &p_dec->p_owner->sout );
556 if( p_dec->p_owner->p_sout_input == NULL )
558 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
559 (char *)&p_dec->p_owner->sout.i_codec );
560 p_dec->b_error = VLC_TRUE;
562 while( p_sout_block )
564 block_t *p_next = p_sout_block->p_next;
565 block_Release( p_sout_block );
566 p_sout_block = p_next;
572 while( p_sout_block )
574 block_t *p_next = p_sout_block->p_next;
576 p_sout_block->p_next = NULL;
577 p_sout_block->i_rate = i_rate;
579 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
582 p_sout_block = p_next;
585 /* For now it's enough, as only sout inpact on this flag */
586 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
587 p_dec->p_owner->p_input->b_out_pace_control )
589 msg_Dbg( p_dec, "switching to synch mode" );
590 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
592 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
593 !p_dec->p_owner->p_input->b_out_pace_control )
595 msg_Dbg( p_dec, "switching to asynch mode" );
596 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
600 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
602 aout_buffer_t *p_aout_buf;
604 if( p_dec->p_owner->p_packetizer )
606 block_t *p_packetized_block;
607 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
609 while( (p_packetized_block =
610 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
612 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
614 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
615 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
616 memcpy( p_dec->fmt_in.p_extra,
617 p_packetizer->fmt_out.p_extra,
618 p_dec->fmt_in.i_extra );
621 while( p_packetized_block )
623 block_t *p_next = p_packetized_block->p_next;
624 p_packetized_block->p_next = NULL;
625 p_packetized_block->i_rate = i_rate;
627 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
628 &p_packetized_block )) )
630 stats_UpdateInteger( p_dec->p_parent, "decoded_audio", 1, NULL );
631 /* FIXME the best would be to handle the case start_date < preroll < end_date
632 * but that's not easy with non raw audio stream */
633 if( p_dec->p_owner->i_preroll_end > 0 &&
634 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
636 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
637 p_dec->p_owner->p_aout_input, p_aout_buf );
641 p_dec->p_owner->i_preroll_end = -1;
642 aout_DecPlay( p_dec->p_owner->p_aout,
643 p_dec->p_owner->p_aout_input,
648 p_packetized_block = p_next;
652 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
654 stats_UpdateInteger( p_dec->p_parent, "decoded_audio", 1, NULL );
655 if( p_dec->p_owner->i_preroll_end > 0 &&
656 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
658 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
659 p_dec->p_owner->p_aout_input, p_aout_buf );
663 p_dec->p_owner->i_preroll_end = -1;
664 aout_DecPlay( p_dec->p_owner->p_aout,
665 p_dec->p_owner->p_aout_input,
670 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
674 if( p_dec->p_owner->p_packetizer )
676 block_t *p_packetized_block;
677 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
679 while( (p_packetized_block =
680 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
682 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
684 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
685 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
686 memcpy( p_dec->fmt_in.p_extra,
687 p_packetizer->fmt_out.p_extra,
688 p_dec->fmt_in.i_extra );
691 while( p_packetized_block )
693 block_t *p_next = p_packetized_block->p_next;
694 p_packetized_block->p_next = NULL;
695 p_packetized_block->i_rate = i_rate;
697 while( (p_pic = p_dec->pf_decode_video( p_dec,
698 &p_packetized_block )) )
700 stats_UpdateInteger( p_dec->p_parent, "decoded_video",
702 if( p_dec->p_owner->i_preroll_end > 0 &&
703 p_pic->date < p_dec->p_owner->i_preroll_end )
705 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
709 p_dec->p_owner->i_preroll_end = -1;
710 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
712 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
716 p_packetized_block = p_next;
720 else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
722 stats_UpdateInteger( p_dec->p_parent, "decoded_video", 1 , NULL);
723 if( p_dec->p_owner->i_preroll_end > 0 &&
724 p_pic->date < p_dec->p_owner->i_preroll_end )
726 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
730 p_dec->p_owner->i_preroll_end = -1;
731 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
732 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
736 else if( p_dec->fmt_in.i_cat == SPU_ES )
738 vout_thread_t *p_vout;
740 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
742 stats_UpdateInteger( p_dec->p_parent, "decoded_sub", 1 , NULL);
743 if( p_dec->p_owner->i_preroll_end > 0 &&
744 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
745 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
747 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
751 p_dec->p_owner->i_preroll_end = -1;
752 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
755 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
756 vlc_object_release( p_vout );
762 msg_Err( p_dec, "unknown ES format" );
766 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
770 * Destroys a decoder object
772 * \param p_dec the decoder object
775 static void DeleteDecoder( decoder_t * p_dec )
777 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
778 (char*)&p_dec->fmt_in.i_codec,
779 p_dec->p_owner->p_fifo->i_depth );
781 /* Free all packets still in the decoder fifo. */
782 block_FifoEmpty( p_dec->p_owner->p_fifo );
783 block_FifoRelease( p_dec->p_owner->p_fifo );
786 if( p_dec->p_owner->p_aout_input )
787 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
789 if( p_dec->p_owner->p_vout )
793 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
794 /* Hack to make sure all the the pictures are freed by the decoder */
795 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
798 if( p_pic->i_status == RESERVED_PICTURE )
799 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
800 if( p_pic->i_refcount > 0 )
801 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
805 /* We are about to die. Reattach video output to p_vlc. */
806 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
809 if( p_dec->p_owner->p_sout_input )
811 sout_InputDelete( p_dec->p_owner->p_sout_input );
812 es_format_Clean( &p_dec->p_owner->sout );
815 if( p_dec->fmt_in.i_cat == SPU_ES )
817 vout_thread_t *p_vout;
819 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
822 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
823 p_dec->p_owner->i_spu_channel );
824 vlc_object_release( p_vout );
828 es_format_Clean( &p_dec->fmt_in );
829 es_format_Clean( &p_dec->fmt_out );
831 if( p_dec->p_owner->p_packetizer )
833 module_Unneed( p_dec->p_owner->p_packetizer,
834 p_dec->p_owner->p_packetizer->p_module );
835 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
836 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
837 vlc_object_detach( p_dec->p_owner->p_packetizer );
838 vlc_object_destroy( p_dec->p_owner->p_packetizer );
841 vlc_object_detach( p_dec );
843 free( p_dec->p_owner );
846 /*****************************************************************************
847 * Buffers allocation callbacks for the decoders
848 *****************************************************************************/
849 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
851 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
852 aout_buffer_t *p_buffer;
854 if( p_sys->p_aout_input != NULL &&
855 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
856 p_dec->fmt_out.audio.i_original_channels !=
857 p_sys->audio.i_original_channels ||
858 p_dec->fmt_out.audio.i_bytes_per_frame !=
859 p_sys->audio.i_bytes_per_frame ) )
861 /* Parameters changed, restart the aout */
862 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
863 p_sys->p_aout_input = NULL;
866 if( p_sys->p_aout_input == NULL )
868 audio_sample_format_t format;
869 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
871 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
872 p_sys->audio = p_dec->fmt_out.audio;
874 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
875 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
876 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
878 if ( i_force_dolby == 1 )
880 format.i_original_channels = format.i_original_channels |
881 AOUT_CHAN_DOLBYSTEREO;
883 else /* i_force_dolby == 2 */
885 format.i_original_channels = format.i_original_channels &
886 ~AOUT_CHAN_DOLBYSTEREO;
890 p_sys->p_aout_input =
891 aout_DecNew( p_dec, &p_sys->p_aout, &format );
892 if( p_sys->p_aout_input == NULL )
894 msg_Err( p_dec, "failed to create audio output" );
895 p_dec->b_error = VLC_TRUE;
898 p_dec->fmt_out.audio.i_bytes_per_frame =
899 p_sys->audio.i_bytes_per_frame;
902 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
908 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
910 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
911 p_dec->p_owner->p_aout_input, p_buffer );
914 static picture_t *vout_new_buffer( decoder_t *p_dec )
916 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
919 if( p_sys->p_vout == NULL ||
920 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
921 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
922 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
923 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
925 if( !p_dec->fmt_out.video.i_width ||
926 !p_dec->fmt_out.video.i_height )
928 /* Can't create a new vout without display size */
932 if( !p_dec->fmt_out.video.i_visible_width ||
933 !p_dec->fmt_out.video.i_visible_height )
935 p_dec->fmt_out.video.i_visible_width =
936 p_dec->fmt_out.video.i_width;
937 p_dec->fmt_out.video.i_visible_height =
938 p_dec->fmt_out.video.i_height;
941 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
942 var_CreateGetBool( p_dec, "hdtv-fix" ) )
944 p_dec->fmt_out.video.i_visible_height = 1080;
945 p_dec->fmt_out.video.i_sar_num *= 135;
946 p_dec->fmt_out.video.i_sar_den *= 136;
947 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
950 if( !p_dec->fmt_out.video.i_sar_num ||
951 !p_dec->fmt_out.video.i_sar_den )
953 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
954 p_dec->fmt_out.video.i_visible_height;
956 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
957 p_dec->fmt_out.video.i_visible_width;
960 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
961 &p_dec->fmt_out.video.i_sar_den,
962 p_dec->fmt_out.video.i_sar_num,
963 p_dec->fmt_out.video.i_sar_den, 50000 );
965 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
966 p_sys->video = p_dec->fmt_out.video;
968 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
969 &p_dec->fmt_out.video );
970 if( p_sys->p_vout == NULL )
972 msg_Err( p_dec, "failed to create video output" );
973 p_dec->b_error = VLC_TRUE;
977 if( p_sys->video.i_rmask )
978 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
979 if( p_sys->video.i_gmask )
980 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
981 if( p_sys->video.i_bmask )
982 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
985 /* Get a new picture */
986 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
988 int i_pic, i_ready_pic = 0;
990 if( p_dec->b_die || p_dec->b_error )
995 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
996 /* Check the decoder doesn't leak pictures */
997 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1000 if( p_pic->i_status == READY_PICTURE )
1002 if( i_ready_pic++ > 0 ) break;
1006 if( p_pic->i_status != DISPLAYED_PICTURE &&
1007 p_pic->i_status != RESERVED_PICTURE &&
1008 p_pic->i_status != READY_PICTURE ) break;
1010 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1013 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1015 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1017 /* Just free all the pictures */
1018 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1021 if( p_pic->i_status == RESERVED_PICTURE )
1022 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1023 if( p_pic->i_refcount > 0 )
1024 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1029 msleep( VOUT_OUTMEM_SLEEP );
1035 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1037 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1040 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1042 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1045 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1047 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1050 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1052 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1053 vout_thread_t *p_vout = NULL;
1054 subpicture_t *p_subpic;
1055 int i_attempts = 30;
1057 while( i_attempts-- )
1059 if( p_dec->b_die || p_dec->b_error ) break;
1061 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1064 msleep( VOUT_DISPLAY_DELAY );
1069 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1073 if( p_sys->p_spu_vout != p_vout )
1075 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1076 &p_sys->i_spu_channel );
1077 p_sys->p_spu_vout = p_vout;
1080 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1083 p_subpic->i_channel = p_sys->i_spu_channel;
1086 vlc_object_release( p_vout );
1091 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1093 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1094 vout_thread_t *p_vout = NULL;
1096 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1097 if( !p_vout || p_sys->p_spu_vout != p_vout )
1100 vlc_object_release( p_vout );
1101 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1105 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1107 vlc_object_release( p_vout );