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., 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 if( p_dec->p_owner->sout.psz_language )
541 free( p_dec->p_owner->sout.psz_language );
542 p_dec->p_owner->sout.psz_language =
543 strdup( p_dec->fmt_in.psz_language );
546 p_dec->p_owner->p_sout_input =
547 sout_InputNew( p_dec->p_owner->p_sout,
548 &p_dec->p_owner->sout );
550 if( p_dec->p_owner->p_sout_input == NULL )
552 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
553 (char *)&p_dec->p_owner->sout.i_codec );
554 p_dec->b_error = VLC_TRUE;
556 while( p_sout_block )
558 block_t *p_next = p_sout_block->p_next;
559 block_Release( p_sout_block );
560 p_sout_block = p_next;
566 while( p_sout_block )
568 block_t *p_next = p_sout_block->p_next;
570 p_sout_block->p_next = NULL;
571 p_sout_block->i_rate = i_rate;
573 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
576 p_sout_block = p_next;
579 /* For now it's enough, as only sout inpact on this flag */
580 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
581 p_dec->p_owner->p_input->b_out_pace_control )
583 msg_Dbg( p_dec, "switching to synch mode" );
584 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
586 else 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 asynch mode" );
590 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
594 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
596 aout_buffer_t *p_aout_buf;
598 if( p_dec->p_owner->p_packetizer )
600 block_t *p_packetized_block;
601 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
603 while( (p_packetized_block =
604 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
606 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
608 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
609 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
610 memcpy( p_dec->fmt_in.p_extra,
611 p_packetizer->fmt_out.p_extra,
612 p_dec->fmt_in.i_extra );
615 while( p_packetized_block )
617 block_t *p_next = p_packetized_block->p_next;
618 p_packetized_block->p_next = NULL;
619 p_packetized_block->i_rate = i_rate;
621 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
622 &p_packetized_block )) )
624 /* FIXME the best would be to handle the case start_date < preroll < end_date
625 * but that's not easy with non raw audio stream */
626 if( p_dec->p_owner->i_preroll_end > 0 &&
627 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
629 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
630 p_dec->p_owner->p_aout_input, p_aout_buf );
634 p_dec->p_owner->i_preroll_end = -1;
635 aout_DecPlay( p_dec->p_owner->p_aout,
636 p_dec->p_owner->p_aout_input,
641 p_packetized_block = p_next;
645 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
647 if( p_dec->p_owner->i_preroll_end > 0 &&
648 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
650 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
651 p_dec->p_owner->p_aout_input, p_aout_buf );
655 p_dec->p_owner->i_preroll_end = -1;
656 aout_DecPlay( p_dec->p_owner->p_aout,
657 p_dec->p_owner->p_aout_input,
662 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
666 if( p_dec->p_owner->p_packetizer )
668 block_t *p_packetized_block;
669 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
671 while( (p_packetized_block =
672 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
674 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
676 p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
677 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
678 memcpy( p_dec->fmt_in.p_extra,
679 p_packetizer->fmt_out.p_extra,
680 p_dec->fmt_in.i_extra );
683 while( p_packetized_block )
685 block_t *p_next = p_packetized_block->p_next;
686 p_packetized_block->p_next = NULL;
687 p_packetized_block->i_rate = i_rate;
689 while( (p_pic = p_dec->pf_decode_video( p_dec,
690 &p_packetized_block )) )
692 if( p_dec->p_owner->i_preroll_end > 0 &&
693 p_pic->date < p_dec->p_owner->i_preroll_end )
695 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
699 p_dec->p_owner->i_preroll_end = -1;
700 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
702 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
706 p_packetized_block = p_next;
710 else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
712 if( p_dec->p_owner->i_preroll_end > 0 &&
713 p_pic->date < p_dec->p_owner->i_preroll_end )
715 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
719 p_dec->p_owner->i_preroll_end = -1;
720 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
721 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
725 else if( p_dec->fmt_in.i_cat == SPU_ES )
727 vout_thread_t *p_vout;
729 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
731 if( p_dec->p_owner->i_preroll_end > 0 &&
732 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
733 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
735 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
739 p_dec->p_owner->i_preroll_end = -1;
740 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
743 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
744 vlc_object_release( p_vout );
750 msg_Err( p_dec, "unknown ES format" );
754 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
758 * Destroys a decoder object
760 * \param p_dec the decoder object
763 static void DeleteDecoder( decoder_t * p_dec )
765 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
766 (char*)&p_dec->fmt_in.i_codec,
767 p_dec->p_owner->p_fifo->i_depth );
769 /* Free all packets still in the decoder fifo. */
770 block_FifoEmpty( p_dec->p_owner->p_fifo );
771 block_FifoRelease( p_dec->p_owner->p_fifo );
774 if( p_dec->p_owner->p_aout_input )
775 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
777 if( p_dec->p_owner->p_vout )
781 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
782 /* Hack to make sure all the the pictures are freed by the decoder */
783 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
786 if( p_pic->i_status == RESERVED_PICTURE )
787 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
788 if( p_pic->i_refcount > 0 )
789 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
793 /* We are about to die. Reattach video output to p_vlc. */
794 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
797 if( p_dec->p_owner->p_sout_input )
799 sout_InputDelete( p_dec->p_owner->p_sout_input );
800 es_format_Clean( &p_dec->p_owner->sout );
803 if( p_dec->fmt_in.i_cat == SPU_ES )
805 vout_thread_t *p_vout;
807 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
810 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
811 p_dec->p_owner->i_spu_channel );
812 vlc_object_release( p_vout );
816 es_format_Clean( &p_dec->fmt_in );
817 es_format_Clean( &p_dec->fmt_out );
819 if( p_dec->p_owner->p_packetizer )
821 module_Unneed( p_dec->p_owner->p_packetizer,
822 p_dec->p_owner->p_packetizer->p_module );
823 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
824 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
825 vlc_object_detach( p_dec->p_owner->p_packetizer );
826 vlc_object_destroy( p_dec->p_owner->p_packetizer );
829 vlc_object_detach( p_dec );
831 free( p_dec->p_owner );
834 /*****************************************************************************
835 * Buffers allocation callbacks for the decoders
836 *****************************************************************************/
837 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
839 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
840 aout_buffer_t *p_buffer;
842 if( p_sys->p_aout_input != NULL &&
843 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
844 p_dec->fmt_out.audio.i_original_channels !=
845 p_sys->audio.i_original_channels ||
846 p_dec->fmt_out.audio.i_bytes_per_frame !=
847 p_sys->audio.i_bytes_per_frame ) )
849 /* Parameters changed, restart the aout */
850 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
851 p_sys->p_aout_input = NULL;
854 if( p_sys->p_aout_input == NULL )
856 audio_sample_format_t format;
857 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
859 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
860 p_sys->audio = p_dec->fmt_out.audio;
862 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
863 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
864 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
866 if ( i_force_dolby > 0 )
868 format.i_original_channels = format.i_original_channels |
869 AOUT_CHAN_DOLBYSTEREO;
873 format.i_original_channels = format.i_original_channels &
874 ~AOUT_CHAN_DOLBYSTEREO;
878 p_sys->p_aout_input =
879 aout_DecNew( p_dec, &p_sys->p_aout, &format );
880 if( p_sys->p_aout_input == NULL )
882 msg_Err( p_dec, "failed to create audio output" );
883 p_dec->b_error = VLC_TRUE;
886 p_dec->fmt_out.audio.i_bytes_per_frame =
887 p_sys->audio.i_bytes_per_frame;
890 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
896 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
898 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
899 p_dec->p_owner->p_aout_input, p_buffer );
902 static picture_t *vout_new_buffer( decoder_t *p_dec )
904 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
907 if( p_sys->p_vout == NULL ||
908 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
909 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
910 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
911 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
913 if( !p_dec->fmt_out.video.i_width ||
914 !p_dec->fmt_out.video.i_height )
916 /* Can't create a new vout without display size */
920 if( !p_dec->fmt_out.video.i_sar_num ||
921 !p_dec->fmt_out.video.i_sar_den )
923 p_dec->fmt_out.video.i_sar_num =
924 p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
926 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
927 p_dec->fmt_out.video.i_width;
930 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
931 &p_dec->fmt_out.video.i_sar_den,
932 p_dec->fmt_out.video.i_sar_num,
933 p_dec->fmt_out.video.i_sar_den, 0 );
935 if( !p_dec->fmt_out.video.i_visible_width ||
936 !p_dec->fmt_out.video.i_visible_height )
938 p_dec->fmt_out.video.i_visible_width =
939 p_dec->fmt_out.video.i_width;
940 p_dec->fmt_out.video.i_visible_height =
941 p_dec->fmt_out.video.i_height;
944 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
945 p_sys->video = p_dec->fmt_out.video;
947 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
948 &p_dec->fmt_out.video );
949 if( p_sys->p_vout == NULL )
951 msg_Err( p_dec, "failed to create video output" );
952 p_dec->b_error = VLC_TRUE;
956 if( p_sys->video.i_rmask )
957 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
958 if( p_sys->video.i_gmask )
959 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
960 if( p_sys->video.i_bmask )
961 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
964 /* Get a new picture */
965 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
967 int i_pic, i_ready_pic = 0;
969 if( p_dec->b_die || p_dec->b_error )
974 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
975 /* Check the decoder doesn't leak pictures */
976 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
979 if( p_pic->i_status == READY_PICTURE )
981 if( i_ready_pic++ > 0 ) break;
985 if( p_pic->i_status != DISPLAYED_PICTURE &&
986 p_pic->i_status != RESERVED_PICTURE &&
987 p_pic->i_status != READY_PICTURE ) break;
989 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
992 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
994 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
996 /* Just free all the pictures */
997 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1000 if( p_pic->i_status == RESERVED_PICTURE )
1001 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1002 if( p_pic->i_refcount > 0 )
1003 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1008 msleep( VOUT_OUTMEM_SLEEP );
1014 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1016 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1019 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1021 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1024 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1026 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1029 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1031 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1032 vout_thread_t *p_vout = NULL;
1033 subpicture_t *p_subpic;
1034 int i_attempts = 30;
1036 while( i_attempts-- )
1038 if( p_dec->b_die || p_dec->b_error ) break;
1040 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1043 msleep( VOUT_DISPLAY_DELAY );
1048 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1052 if( p_sys->p_spu_vout != p_vout )
1054 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1055 &p_sys->i_spu_channel );
1056 p_sys->p_spu_vout = p_vout;
1059 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1062 p_subpic->i_channel = p_sys->i_spu_channel;
1065 vlc_object_release( p_vout );
1070 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1072 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1073 vout_thread_t *p_vout = NULL;
1075 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1076 if( !p_vout || p_sys->p_spu_vout != p_vout )
1079 vlc_object_release( p_vout );
1080 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1084 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1086 vlc_object_release( p_vout );