1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 /* Find a suitable decoder/packetizer module */
433 if( i_object_type == VLC_OBJECT_DECODER )
434 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
436 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
438 /* Check if decoder requires already packetized data */
439 if( i_object_type == VLC_OBJECT_DECODER &&
440 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
442 p_dec->p_owner->p_packetizer =
443 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
444 if( p_dec->p_owner->p_packetizer )
446 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
449 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
452 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
454 p_dec->p_owner->p_packetizer->p_module =
455 module_Need( p_dec->p_owner->p_packetizer,
456 "packetizer", "$packetizer", 0 );
458 if( !p_dec->p_owner->p_packetizer->p_module )
460 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
461 vlc_object_detach( p_dec->p_owner->p_packetizer );
462 vlc_object_destroy( p_dec->p_owner->p_packetizer );
471 * The decoding main loop
473 * \param p_dec the decoder
476 static int DecoderThread( decoder_t * p_dec )
480 /* The decoder's main loop */
481 while( !p_dec->b_die && !p_dec->b_error )
483 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
488 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
494 while( !p_dec->b_die )
496 /* Trash all received PES packets */
497 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
498 if( p_block ) block_Release( p_block );
501 /* We do it here because of the dll loader that wants close() in the
502 * same thread than open()/decode() */
503 module_Unneed( p_dec, p_dec->p_module );
511 * \param p_dec the decoder object
512 * \param p_block the block to decode
513 * \return VLC_SUCCESS or an error code
515 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
517 int i_rate = p_block ? p_block->i_rate : 1000;
519 if( p_block && p_block->i_buffer <= 0 )
521 block_Release( p_block );
525 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
527 block_t *p_sout_block;
529 while( ( p_sout_block =
530 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
532 if( !p_dec->p_owner->p_sout_input )
534 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
536 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
537 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
538 if( p_dec->fmt_in.psz_language )
540 p_dec->p_owner->sout.psz_language =
541 strdup( p_dec->fmt_in.psz_language );
544 p_dec->p_owner->p_sout_input =
545 sout_InputNew( p_dec->p_owner->p_sout,
546 &p_dec->p_owner->sout );
548 if( p_dec->p_owner->p_sout_input == NULL )
550 msg_Err( p_dec, "cannot create packetizer output" );
551 p_dec->b_error = VLC_TRUE;
553 while( p_sout_block )
555 block_t *p_next = p_sout_block->p_next;
556 block_Release( p_sout_block );
557 p_sout_block = p_next;
563 while( p_sout_block )
565 block_t *p_next = p_sout_block->p_next;
567 p_sout_block->p_next = NULL;
568 p_sout_block->i_rate = i_rate;
570 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
573 p_sout_block = p_next;
576 /* For now it's enough, as only sout inpact on this flag */
577 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
578 p_dec->p_owner->p_input->b_out_pace_control )
580 msg_Dbg( p_dec, "switching to synch mode" );
581 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
583 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
584 !p_dec->p_owner->p_input->b_out_pace_control )
586 msg_Dbg( p_dec, "switching to asynch mode" );
587 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
591 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
593 aout_buffer_t *p_aout_buf;
595 if( p_dec->p_owner->p_packetizer )
597 block_t *p_packetized_block;
598 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
600 while( (p_packetized_block =
601 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
603 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
605 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
606 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
607 memcpy( p_dec->fmt_in.p_extra,
608 p_packetizer->fmt_out.p_extra,
609 p_dec->fmt_in.i_extra );
612 while( p_packetized_block )
614 block_t *p_next = p_packetized_block->p_next;
615 p_packetized_block->p_next = NULL;
616 p_packetized_block->i_rate = i_rate;
618 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
619 &p_packetized_block )) )
621 /* FIXME the best would be to handle the case start_date < preroll < end_date
622 * but that's not easy with non raw audio stream */
623 if( p_dec->p_owner->i_preroll_end > 0 &&
624 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
626 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
627 p_dec->p_owner->p_aout_input, p_aout_buf );
631 p_dec->p_owner->i_preroll_end = -1;
632 aout_DecPlay( p_dec->p_owner->p_aout,
633 p_dec->p_owner->p_aout_input,
638 p_packetized_block = p_next;
642 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
644 if( p_dec->p_owner->i_preroll_end > 0 &&
645 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
647 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
648 p_dec->p_owner->p_aout_input, p_aout_buf );
652 p_dec->p_owner->i_preroll_end = -1;
653 aout_DecPlay( p_dec->p_owner->p_aout,
654 p_dec->p_owner->p_aout_input,
659 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
663 if( p_dec->p_owner->p_packetizer )
665 block_t *p_packetized_block;
666 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
668 while( (p_packetized_block =
669 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
671 while( p_packetized_block )
673 block_t *p_next = p_packetized_block->p_next;
674 p_packetized_block->p_next = NULL;
675 p_packetized_block->i_rate = i_rate;
677 while( (p_pic = p_dec->pf_decode_video( p_dec,
678 &p_packetized_block )) )
680 if( p_dec->p_owner->i_preroll_end > 0 &&
681 p_pic->date < p_dec->p_owner->i_preroll_end )
683 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
687 p_dec->p_owner->i_preroll_end = -1;
688 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
690 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
694 p_packetized_block = p_next;
698 else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
700 if( p_dec->p_owner->i_preroll_end > 0 &&
701 p_pic->date < p_dec->p_owner->i_preroll_end )
703 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
707 p_dec->p_owner->i_preroll_end = -1;
708 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
709 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
713 else if( p_dec->fmt_in.i_cat == SPU_ES )
715 vout_thread_t *p_vout;
717 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
719 if( p_dec->p_owner->i_preroll_end > 0 &&
720 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
721 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
723 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
727 p_dec->p_owner->i_preroll_end = -1;
728 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
731 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
732 vlc_object_release( p_vout );
738 msg_Err( p_dec, "unknown ES format" );
742 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
746 * Destroys a decoder object
748 * \param p_dec the decoder object
751 static void DeleteDecoder( decoder_t * p_dec )
753 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
754 (char*)&p_dec->fmt_in.i_codec,
755 p_dec->p_owner->p_fifo->i_depth );
757 /* Free all packets still in the decoder fifo. */
758 block_FifoEmpty( p_dec->p_owner->p_fifo );
759 block_FifoRelease( p_dec->p_owner->p_fifo );
762 if( p_dec->p_owner->p_aout_input )
763 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
765 if( p_dec->p_owner->p_vout )
769 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
770 /* Hack to make sure all the the pictures are freed by the decoder */
771 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
774 if( p_pic->i_status == RESERVED_PICTURE )
775 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
776 if( p_pic->i_refcount > 0 )
777 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
781 /* We are about to die. Reattach video output to p_vlc. */
782 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
785 if( p_dec->p_owner->p_sout_input )
787 sout_InputDelete( p_dec->p_owner->p_sout_input );
788 es_format_Clean( &p_dec->p_owner->sout );
791 if( p_dec->fmt_in.i_cat == SPU_ES )
793 vout_thread_t *p_vout;
795 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
798 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
799 p_dec->p_owner->i_spu_channel );
800 vlc_object_release( p_vout );
804 es_format_Clean( &p_dec->fmt_in );
805 es_format_Clean( &p_dec->fmt_out );
807 if( p_dec->p_owner->p_packetizer )
809 module_Unneed( p_dec->p_owner->p_packetizer,
810 p_dec->p_owner->p_packetizer->p_module );
811 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
812 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
813 vlc_object_detach( p_dec->p_owner->p_packetizer );
814 vlc_object_destroy( p_dec->p_owner->p_packetizer );
817 vlc_object_detach( p_dec );
819 free( p_dec->p_owner );
822 /*****************************************************************************
823 * Buffers allocation callbacks for the decoders
824 *****************************************************************************/
825 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
827 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
828 aout_buffer_t *p_buffer;
830 if( p_sys->p_aout_input != NULL &&
831 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
832 p_dec->fmt_out.audio.i_original_channels !=
833 p_sys->audio.i_original_channels ||
834 p_dec->fmt_out.audio.i_bytes_per_frame !=
835 p_sys->audio.i_bytes_per_frame ) )
837 /* Parameters changed, restart the aout */
838 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
839 p_sys->p_aout_input = NULL;
842 if( p_sys->p_aout_input == NULL )
844 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
845 p_sys->audio = p_dec->fmt_out.audio;
846 p_sys->p_aout_input =
847 aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
848 if( p_sys->p_aout_input == NULL )
850 msg_Err( p_dec, "failed to create audio output" );
851 p_dec->b_error = VLC_TRUE;
854 p_dec->fmt_out.audio.i_bytes_per_frame =
855 p_sys->audio.i_bytes_per_frame;
858 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
864 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
866 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
867 p_dec->p_owner->p_aout_input, p_buffer );
870 static picture_t *vout_new_buffer( decoder_t *p_dec )
872 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
875 if( p_sys->p_vout == NULL ||
876 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
877 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
878 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
879 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
881 if( !p_dec->fmt_out.video.i_width ||
882 !p_dec->fmt_out.video.i_height )
884 /* Can't create a new vout without display size */
888 if( !p_dec->fmt_out.video.i_sar_num ||
889 !p_dec->fmt_out.video.i_sar_den )
891 p_dec->fmt_out.video.i_sar_num =
892 p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
894 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
895 p_dec->fmt_out.video.i_width;
898 vlc_reduce( &p_dec->fmt_out.video.i_sar_num,
899 &p_dec->fmt_out.video.i_sar_den,
900 p_dec->fmt_out.video.i_sar_num,
901 p_dec->fmt_out.video.i_sar_den, 0 );
903 if( !p_dec->fmt_out.video.i_visible_width ||
904 !p_dec->fmt_out.video.i_visible_height )
906 p_dec->fmt_out.video.i_visible_width =
907 p_dec->fmt_out.video.i_width;
908 p_dec->fmt_out.video.i_visible_height =
909 p_dec->fmt_out.video.i_height;
912 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
913 p_sys->video = p_dec->fmt_out.video;
915 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
916 &p_dec->fmt_out.video );
917 if( p_sys->p_vout == NULL )
919 msg_Err( p_dec, "failed to create video output" );
920 p_dec->b_error = VLC_TRUE;
924 if( p_sys->video.i_rmask )
925 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
926 if( p_sys->video.i_gmask )
927 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
928 if( p_sys->video.i_bmask )
929 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
932 /* Get a new picture */
933 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
935 int i_pic, i_ready_pic = 0;
937 if( p_dec->b_die || p_dec->b_error )
942 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
943 /* Check the decoder doesn't leak pictures */
944 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
947 if( p_pic->i_status == READY_PICTURE )
949 if( i_ready_pic++ > 0 ) break;
953 if( p_pic->i_status != DISPLAYED_PICTURE &&
954 p_pic->i_status != RESERVED_PICTURE &&
955 p_pic->i_status != READY_PICTURE ) break;
957 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
960 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
962 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
964 /* Just free all the pictures */
965 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
968 if( p_pic->i_status == RESERVED_PICTURE )
969 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
970 if( p_pic->i_refcount > 0 )
971 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
976 msleep( VOUT_OUTMEM_SLEEP );
982 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
984 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
987 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
989 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
992 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
994 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
997 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
999 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1000 vout_thread_t *p_vout = NULL;
1001 subpicture_t *p_subpic;
1002 int i_attempts = 30;
1004 while( i_attempts-- )
1006 if( p_dec->b_die || p_dec->b_error ) break;
1008 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1011 msleep( VOUT_DISPLAY_DELAY );
1016 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1020 if( p_sys->p_spu_vout != p_vout )
1022 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1023 &p_sys->i_spu_channel );
1024 p_sys->p_spu_vout = p_vout;
1027 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1030 p_subpic->i_channel = p_sys->i_spu_channel;
1033 vlc_object_release( p_vout );
1038 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1040 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1041 vout_thread_t *p_vout = NULL;
1043 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1044 if( !p_vout || p_sys->p_spu_vout != p_vout )
1047 vlc_object_release( p_vout );
1048 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1052 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1054 vlc_object_release( p_vout );