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_block.h>
36 #include <vlc_codec.h>
39 #include <vlc_interface.h>
40 #include "audio_output/aout_internal.h"
41 #include "stream_output/stream_output.h"
42 #include "input_internal.h"
44 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
45 static void DeleteDecoder( decoder_t * );
47 static int DecoderThread( decoder_t * );
48 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
50 /* Buffers allocation callbacks for the decoders */
51 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
52 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
54 static picture_t *vout_new_buffer( decoder_t * );
55 static void vout_del_buffer( decoder_t *, picture_t * );
56 static void vout_link_picture( decoder_t *, picture_t * );
57 static void vout_unlink_picture( decoder_t *, picture_t * );
59 static subpicture_t *spu_new_buffer( decoder_t * );
60 static void spu_del_buffer( decoder_t *, subpicture_t * );
62 static es_format_t null_es_format;
64 struct decoder_owner_sys_t
66 vlc_bool_t b_own_thread;
68 int64_t i_preroll_end;
70 input_thread_t *p_input;
72 aout_instance_t *p_aout;
73 aout_input_t *p_aout_input;
75 vout_thread_t *p_vout;
77 vout_thread_t *p_spu_vout;
80 sout_instance_t *p_sout;
81 sout_packetizer_input_t *p_sout_input;
83 /* Some decoders require already packetized data (ie. not truncated) */
84 decoder_t *p_packetizer;
86 /* Current format in use by the output */
97 * Spawns a new decoder thread
99 * \param p_input the input thread
100 * \param p_es the es descriptor
101 * \return the spawned decoder object
103 decoder_t *input_DecoderNew( input_thread_t *p_input,
104 es_format_t *fmt, vlc_bool_t b_force_decoder )
106 decoder_t *p_dec = NULL;
109 /* If we are in sout mode, search for packetizer module */
110 if( p_input->p->p_sout && !b_force_decoder )
112 /* Create the decoder configuration structure */
113 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
116 msg_Err( p_input, "could not create packetizer" );
117 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
118 _("VLC could not open the packetizer module.") );
124 /* Create the decoder configuration structure */
125 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
128 msg_Err( p_input, "could not create decoder" );
129 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
130 _("VLC could not open the decoder module.") );
135 if( !p_dec->p_module )
137 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
138 "VLC probably does not support this sound or video format.",
139 (char*)&p_dec->fmt_in.i_codec );
140 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
141 "for format"), _("VLC probably does not support the \"%4.4s\" "
142 "audio or video format. Unfortunately there is no way for you "
143 "to fix this."), (char*)&p_dec->fmt_in.i_codec );
145 DeleteDecoder( p_dec );
146 vlc_object_destroy( p_dec );
150 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
153 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
154 p_dec->p_owner->b_own_thread = VLC_FALSE;
158 var_Get( p_input, "minimize-threads", &val );
159 p_dec->p_owner->b_own_thread = !val.b_bool;
162 if( p_dec->p_owner->b_own_thread )
165 if( fmt->i_cat == AUDIO_ES )
166 i_priority = VLC_THREAD_PRIORITY_AUDIO;
168 i_priority = VLC_THREAD_PRIORITY_VIDEO;
170 /* Spawn the decoder thread */
171 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
172 i_priority, VLC_FALSE ) )
174 msg_Err( p_dec, "cannot spawn decoder thread" );
175 module_Unneed( p_dec, p_dec->p_module );
176 DeleteDecoder( p_dec );
177 vlc_object_destroy( p_dec );
186 * Kills a decoder thread and waits until it's finished
188 * \param p_input the input thread
189 * \param p_es the es descriptor
192 void input_DecoderDelete( decoder_t *p_dec )
194 vlc_object_kill( p_dec );
196 if( p_dec->p_owner->b_own_thread )
198 /* Make sure the thread leaves the function by
199 * sending it an empty block. */
200 block_t *p_block = block_New( p_dec, 0 );
201 input_DecoderDecode( p_dec, p_block );
203 vlc_thread_join( p_dec );
205 /* Don't module_Unneed() here because of the dll loader that wants
206 * close() in the same thread than open()/decode() */
211 input_DecoderDecode( p_dec, NULL );
213 module_Unneed( p_dec, p_dec->p_module );
216 /* Delete decoder configuration */
217 DeleteDecoder( p_dec );
219 /* Delete the decoder */
220 vlc_object_destroy( p_dec );
224 * Put a block_t in the decoder's fifo.
226 * \param p_dec the decoder object
227 * \param p_block the data block
229 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
231 if( p_dec->p_owner->b_own_thread )
233 if( p_dec->p_owner->p_input->p->b_out_pace_control )
236 while( !p_dec->b_die && !p_dec->b_error &&
237 p_dec->p_owner->p_fifo->i_depth > 10 )
242 else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
244 /* FIXME: ideally we would check the time amount of data
245 * in the fifo instead of its size. */
246 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
247 "consumed quickly enough), resetting fifo!" );
248 block_FifoEmpty( p_dec->p_owner->p_fifo );
251 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
255 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
257 if( p_block ) block_Release( p_block );
261 DecoderDecode( p_dec, p_block );
266 void input_DecoderDiscontinuity( decoder_t * p_dec )
271 if( p_dec->p_owner->b_own_thread )
273 block_FifoEmpty( p_dec->p_owner->p_fifo );
276 /* Send a special block */
277 p_null = block_New( p_dec, 128 );
278 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
279 memset( p_null->p_buffer, 0, p_null->i_buffer );
281 input_DecoderDecode( p_dec, p_null );
284 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
286 if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
293 void input_DecoderPreroll( decoder_t *p_dec, int64_t i_preroll_end )
295 p_dec->p_owner->i_preroll_end = i_preroll_end;
298 * Create a decoder object
300 * \param p_input the input thread
301 * \param p_es the es descriptor
302 * \param i_object_type Object type as define in include/vlc_objects.h
303 * \return the decoder object
305 static decoder_t * CreateDecoder( input_thread_t *p_input,
306 es_format_t *fmt, int i_object_type )
310 p_dec = vlc_object_create( p_input, i_object_type );
313 msg_Err( p_input, "out of memory" );
317 p_dec->pf_decode_audio = 0;
318 p_dec->pf_decode_video = 0;
319 p_dec->pf_decode_sub = 0;
320 p_dec->pf_packetize = 0;
322 /* Initialize the decoder fifo */
323 p_dec->p_module = NULL;
325 memset( &null_es_format, 0, sizeof(es_format_t) );
326 es_format_Copy( &p_dec->fmt_in, fmt );
327 es_format_Copy( &p_dec->fmt_out, &null_es_format );
329 /* Allocate our private structure for the decoder */
330 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
331 if( p_dec->p_owner == NULL )
333 msg_Err( p_dec, "out of memory" );
336 p_dec->p_owner->b_own_thread = VLC_TRUE;
337 p_dec->p_owner->i_preroll_end = -1;
338 p_dec->p_owner->p_input = p_input;
339 p_dec->p_owner->p_aout = NULL;
340 p_dec->p_owner->p_aout_input = NULL;
341 p_dec->p_owner->p_vout = NULL;
342 p_dec->p_owner->p_spu_vout = NULL;
343 p_dec->p_owner->i_spu_channel = 0;
344 p_dec->p_owner->p_sout = p_input->p->p_sout;
345 p_dec->p_owner->p_sout_input = NULL;
346 p_dec->p_owner->p_packetizer = NULL;
349 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
351 msg_Err( p_dec, "out of memory" );
355 /* Set buffers allocation callbacks for the decoders */
356 p_dec->pf_aout_buffer_new = aout_new_buffer;
357 p_dec->pf_aout_buffer_del = aout_del_buffer;
358 p_dec->pf_vout_buffer_new = vout_new_buffer;
359 p_dec->pf_vout_buffer_del = vout_del_buffer;
360 p_dec->pf_picture_link = vout_link_picture;
361 p_dec->pf_picture_unlink = vout_unlink_picture;
362 p_dec->pf_spu_buffer_new = spu_new_buffer;
363 p_dec->pf_spu_buffer_del = spu_del_buffer;
365 vlc_object_attach( p_dec, p_input );
367 /* Find a suitable decoder/packetizer module */
368 if( i_object_type == VLC_OBJECT_DECODER )
369 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
371 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
373 /* Check if decoder requires already packetized data */
374 if( i_object_type == VLC_OBJECT_DECODER &&
375 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
377 p_dec->p_owner->p_packetizer =
378 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
379 if( p_dec->p_owner->p_packetizer )
381 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
384 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
387 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
389 p_dec->p_owner->p_packetizer->p_module =
390 module_Need( p_dec->p_owner->p_packetizer,
391 "packetizer", "$packetizer", 0 );
393 if( !p_dec->p_owner->p_packetizer->p_module )
395 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
396 vlc_object_detach( p_dec->p_owner->p_packetizer );
397 vlc_object_destroy( p_dec->p_owner->p_packetizer );
406 * The decoding main loop
408 * \param p_dec the decoder
411 static int DecoderThread( decoder_t * p_dec )
415 /* The decoder's main loop */
416 while( !p_dec->b_die && !p_dec->b_error )
418 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
423 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
429 while( !p_dec->b_die )
431 /* Trash all received PES packets */
432 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
433 if( p_block ) block_Release( p_block );
436 /* We do it here because of the dll loader that wants close() in the
437 * same thread than open()/decode() */
438 module_Unneed( p_dec, p_dec->p_module );
446 * \param p_dec the decoder object
447 * \param p_block the block to decode
448 * \return VLC_SUCCESS or an error code
450 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
453 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
454 else if( p->i_dts > 0 )
455 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
457 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
459 input_thread_t *p_input = p_dec->p_owner->p_input;
460 aout_buffer_t *p_aout_buf;
462 //DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
463 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
465 vlc_mutex_lock( &p_input->p->counters.counters_lock );
466 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
467 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
469 /* FIXME the best would be to handle the case
470 * start_date < preroll < end_date
471 * but that's not easy with non raw audio stream */
472 // if( p_dec->p_owner->i_preroll_end > 0 )
473 // msg_Err( p_dec, "Prerolling %lld - %lld", p_dec->p_owner->i_preroll_end, p_aout_buf->start_date );
475 if( p_dec->p_owner->i_preroll_end > 0 &&
476 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
478 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
479 p_dec->p_owner->p_aout_input, p_aout_buf );
483 p_dec->p_owner->i_preroll_end = -1;
484 aout_DecPlay( p_dec->p_owner->p_aout,
485 p_dec->p_owner->p_aout_input,
490 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
492 input_thread_t *p_input = p_dec->p_owner->p_input;
495 //DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
496 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
498 vlc_mutex_lock( &p_input->p->counters.counters_lock );
499 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
500 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
502 if( p_dec->p_owner->i_preroll_end > 0 &&
503 p_pic->date < p_dec->p_owner->i_preroll_end )
505 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
509 p_dec->p_owner->i_preroll_end = -1;
510 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
512 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
518 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
520 int i_rate = p_block ? p_block->i_rate : 1000;
522 if( p_block && p_block->i_buffer <= 0 )
524 block_Release( p_block );
528 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
530 block_t *p_sout_block;
532 while( ( p_sout_block =
533 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
535 if( !p_dec->p_owner->p_sout_input )
537 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
539 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
540 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
541 if( p_dec->fmt_in.psz_language )
543 if( p_dec->p_owner->sout.psz_language )
544 free( p_dec->p_owner->sout.psz_language );
545 p_dec->p_owner->sout.psz_language =
546 strdup( p_dec->fmt_in.psz_language );
549 p_dec->p_owner->p_sout_input =
550 sout_InputNew( p_dec->p_owner->p_sout,
551 &p_dec->p_owner->sout );
553 if( p_dec->p_owner->p_sout_input == NULL )
555 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
556 (char *)&p_dec->p_owner->sout.i_codec );
557 p_dec->b_error = VLC_TRUE;
559 while( p_sout_block )
561 block_t *p_next = p_sout_block->p_next;
562 block_Release( p_sout_block );
563 p_sout_block = p_next;
569 while( p_sout_block )
571 block_t *p_next = p_sout_block->p_next;
573 p_sout_block->p_next = NULL;
574 p_sout_block->i_rate = i_rate;
576 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
579 p_sout_block = p_next;
582 /* For now it's enough, as only sout inpact on this flag */
583 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
584 p_dec->p_owner->p_input->p->b_out_pace_control )
586 msg_Dbg( p_dec, "switching to sync mode" );
587 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
589 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
590 !p_dec->p_owner->p_input->p->b_out_pace_control )
592 msg_Dbg( p_dec, "switching to async mode" );
593 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
597 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
599 if( p_dec->p_owner->p_packetizer )
601 block_t *p_packetized_block;
602 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
604 while( (p_packetized_block =
605 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
607 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
609 es_format_Clean( &p_dec->fmt_in );
610 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
613 while( p_packetized_block )
615 block_t *p_next = p_packetized_block->p_next;
616 p_packetized_block->p_next = NULL;
617 p_packetized_block->i_rate = i_rate;
619 DecoderDecodeAudio( p_dec, p_packetized_block );
621 p_packetized_block = p_next;
627 DecoderDecodeAudio( p_dec, p_block );
630 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
632 if( p_dec->p_owner->p_packetizer )
634 block_t *p_packetized_block;
635 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
637 while( (p_packetized_block =
638 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
640 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
642 es_format_Clean( &p_dec->fmt_in );
643 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
646 while( p_packetized_block )
648 block_t *p_next = p_packetized_block->p_next;
649 p_packetized_block->p_next = NULL;
650 p_packetized_block->i_rate = i_rate;
652 DecoderDecodeVideo( p_dec, p_packetized_block );
654 p_packetized_block = p_next;
660 DecoderDecodeVideo( p_dec, p_block );
663 else if( p_dec->fmt_in.i_cat == SPU_ES )
665 input_thread_t *p_input = p_dec->p_owner->p_input;
666 vout_thread_t *p_vout;
668 //DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
669 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
671 vlc_mutex_lock( &p_input->p->counters.counters_lock );
672 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
673 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
675 if( p_dec->p_owner->i_preroll_end > 0 &&
676 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
677 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
679 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
683 p_dec->p_owner->i_preroll_end = -1;
684 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
687 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
688 vlc_object_release( p_vout );
694 msg_Err( p_dec, "unknown ES format" );
698 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
702 * Destroys a decoder object
704 * \param p_dec the decoder object
707 static void DeleteDecoder( decoder_t * p_dec )
709 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
710 (char*)&p_dec->fmt_in.i_codec,
711 p_dec->p_owner->p_fifo->i_depth );
713 /* Free all packets still in the decoder fifo. */
714 block_FifoEmpty( p_dec->p_owner->p_fifo );
715 block_FifoRelease( p_dec->p_owner->p_fifo );
718 if( p_dec->p_owner->p_aout_input )
719 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
721 if( p_dec->p_owner->p_vout )
725 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
726 /* Hack to make sure all the the pictures are freed by the decoder */
727 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
730 if( p_pic->i_status == RESERVED_PICTURE )
731 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
732 if( p_pic->i_refcount > 0 )
733 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
737 /* We are about to die. Reattach video output to p_vlc. */
738 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
741 if( p_dec->p_owner->p_sout_input )
743 sout_InputDelete( p_dec->p_owner->p_sout_input );
744 es_format_Clean( &p_dec->p_owner->sout );
747 if( p_dec->fmt_in.i_cat == SPU_ES )
749 vout_thread_t *p_vout;
751 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
754 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
755 p_dec->p_owner->i_spu_channel );
756 vlc_object_release( p_vout );
760 es_format_Clean( &p_dec->fmt_in );
761 es_format_Clean( &p_dec->fmt_out );
763 if( p_dec->p_owner->p_packetizer )
765 module_Unneed( p_dec->p_owner->p_packetizer,
766 p_dec->p_owner->p_packetizer->p_module );
767 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
768 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
769 vlc_object_detach( p_dec->p_owner->p_packetizer );
770 vlc_object_destroy( p_dec->p_owner->p_packetizer );
773 vlc_object_detach( p_dec );
775 free( p_dec->p_owner );
778 /*****************************************************************************
779 * Buffers allocation callbacks for the decoders
780 *****************************************************************************/
781 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
783 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
784 aout_buffer_t *p_buffer;
786 if( p_sys->p_aout_input != NULL &&
787 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
788 p_dec->fmt_out.audio.i_original_channels !=
789 p_sys->audio.i_original_channels ||
790 p_dec->fmt_out.audio.i_bytes_per_frame !=
791 p_sys->audio.i_bytes_per_frame ) )
793 /* Parameters changed, restart the aout */
794 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
795 p_sys->p_aout_input = NULL;
798 if( p_sys->p_aout_input == NULL )
800 audio_sample_format_t format;
801 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
803 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
804 p_sys->audio = p_dec->fmt_out.audio;
806 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
807 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
808 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
810 if ( i_force_dolby == 1 )
812 format.i_original_channels = format.i_original_channels |
813 AOUT_CHAN_DOLBYSTEREO;
815 else /* i_force_dolby == 2 */
817 format.i_original_channels = format.i_original_channels &
818 ~AOUT_CHAN_DOLBYSTEREO;
822 p_sys->p_aout_input =
823 aout_DecNew( p_dec, &p_sys->p_aout, &format );
824 if( p_sys->p_aout_input == NULL )
826 msg_Err( p_dec, "failed to create audio output" );
827 p_dec->b_error = VLC_TRUE;
830 p_dec->fmt_out.audio.i_bytes_per_frame =
831 p_sys->audio.i_bytes_per_frame;
834 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
840 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
842 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
843 p_dec->p_owner->p_aout_input, p_buffer );
846 static picture_t *vout_new_buffer( decoder_t *p_dec )
848 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
851 if( p_sys->p_vout == NULL ||
852 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
853 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
854 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
855 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
857 if( !p_dec->fmt_out.video.i_width ||
858 !p_dec->fmt_out.video.i_height )
860 /* Can't create a new vout without display size */
864 if( !p_dec->fmt_out.video.i_visible_width ||
865 !p_dec->fmt_out.video.i_visible_height )
867 if( p_dec->fmt_in.video.i_visible_width &&
868 p_dec->fmt_in.video.i_visible_height )
870 p_dec->fmt_out.video.i_visible_width =
871 p_dec->fmt_in.video.i_visible_width;
872 p_dec->fmt_out.video.i_visible_height =
873 p_dec->fmt_in.video.i_visible_height;
877 p_dec->fmt_out.video.i_visible_width =
878 p_dec->fmt_out.video.i_width;
879 p_dec->fmt_out.video.i_visible_height =
880 p_dec->fmt_out.video.i_height;
884 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
885 var_CreateGetBool( p_dec, "hdtv-fix" ) )
887 p_dec->fmt_out.video.i_visible_height = 1080;
888 p_dec->fmt_out.video.i_sar_num *= 135;
889 p_dec->fmt_out.video.i_sar_den *= 136;
890 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
893 if( !p_dec->fmt_out.video.i_sar_num ||
894 !p_dec->fmt_out.video.i_sar_den )
896 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
897 p_dec->fmt_out.video.i_visible_height;
899 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
900 p_dec->fmt_out.video.i_visible_width;
903 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
904 &p_dec->fmt_out.video.i_sar_den,
905 p_dec->fmt_out.video.i_sar_num,
906 p_dec->fmt_out.video.i_sar_den, 50000 );
908 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
909 p_sys->video = p_dec->fmt_out.video;
911 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
912 &p_dec->fmt_out.video );
913 if( p_sys->p_vout == NULL )
915 msg_Err( p_dec, "failed to create video output" );
916 p_dec->b_error = VLC_TRUE;
920 if( p_sys->video.i_rmask )
921 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
922 if( p_sys->video.i_gmask )
923 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
924 if( p_sys->video.i_bmask )
925 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
928 /* Get a new picture */
929 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
931 int i_pic, i_ready_pic = 0;
933 if( p_dec->b_die || p_dec->b_error )
938 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
939 /* Check the decoder doesn't leak pictures */
940 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
943 if( p_pic->i_status == READY_PICTURE )
945 if( i_ready_pic++ > 0 ) break;
949 if( p_pic->i_status != DISPLAYED_PICTURE &&
950 p_pic->i_status != RESERVED_PICTURE &&
951 p_pic->i_status != READY_PICTURE ) break;
953 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
956 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
958 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
960 /* Just free all the pictures */
961 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
964 if( p_pic->i_status == RESERVED_PICTURE )
965 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
966 if( p_pic->i_refcount > 0 )
967 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
972 msleep( VOUT_OUTMEM_SLEEP );
978 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
980 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
983 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
985 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
988 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
990 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
993 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
995 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
996 vout_thread_t *p_vout = NULL;
997 subpicture_t *p_subpic;
1000 while( i_attempts-- )
1002 if( p_dec->b_die || p_dec->b_error ) break;
1004 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1007 msleep( VOUT_DISPLAY_DELAY );
1012 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1016 if( p_sys->p_spu_vout != p_vout )
1018 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1019 &p_sys->i_spu_channel );
1020 p_sys->p_spu_vout = p_vout;
1023 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1026 p_subpic->i_channel = p_sys->i_spu_channel;
1029 vlc_object_release( p_vout );
1034 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1036 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1037 vout_thread_t *p_vout = NULL;
1039 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1040 if( !p_vout || p_sys->p_spu_vout != p_vout )
1043 vlc_object_release( p_vout );
1044 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1048 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1050 vlc_object_release( p_vout );