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 */
95 /* decoder_GetInputAttachment:
97 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec, const char *psz_name )
99 input_attachment_t *p_attachment;
100 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
106 * Spawns a new decoder thread
108 * \param p_input the input thread
109 * \param p_es the es descriptor
110 * \return the spawned decoder object
112 decoder_t *input_DecoderNew( input_thread_t *p_input,
113 es_format_t *fmt, vlc_bool_t b_force_decoder )
115 decoder_t *p_dec = NULL;
118 /* If we are in sout mode, search for packetizer module */
119 if( p_input->p->p_sout && !b_force_decoder )
121 /* Create the decoder configuration structure */
122 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
125 msg_Err( p_input, "could not create packetizer" );
126 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
127 _("VLC could not open the packetizer module.") );
133 /* Create the decoder configuration structure */
134 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
137 msg_Err( p_input, "could not create decoder" );
138 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
139 _("VLC could not open the decoder module.") );
144 if( !p_dec->p_module )
146 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
147 "VLC probably does not support this sound or video format.",
148 (char*)&p_dec->fmt_in.i_codec );
149 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
150 "for format"), _("VLC probably does not support the \"%4.4s\" "
151 "audio or video format. Unfortunately there is no way for you "
152 "to fix this."), (char*)&p_dec->fmt_in.i_codec );
154 DeleteDecoder( p_dec );
155 vlc_object_destroy( p_dec );
159 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
162 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
163 p_dec->p_owner->b_own_thread = VLC_FALSE;
167 var_Get( p_input, "minimize-threads", &val );
168 p_dec->p_owner->b_own_thread = !val.b_bool;
171 if( p_dec->p_owner->b_own_thread )
174 if( fmt->i_cat == AUDIO_ES )
175 i_priority = VLC_THREAD_PRIORITY_AUDIO;
177 i_priority = VLC_THREAD_PRIORITY_VIDEO;
179 /* Spawn the decoder thread */
180 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
181 i_priority, VLC_FALSE ) )
183 msg_Err( p_dec, "cannot spawn decoder thread" );
184 module_Unneed( p_dec, p_dec->p_module );
185 DeleteDecoder( p_dec );
186 vlc_object_destroy( p_dec );
195 * Kills a decoder thread and waits until it's finished
197 * \param p_input the input thread
198 * \param p_es the es descriptor
201 void input_DecoderDelete( decoder_t *p_dec )
203 vlc_object_kill( p_dec );
205 if( p_dec->p_owner->b_own_thread )
207 /* Make sure the thread leaves the function by
208 * sending it an empty block. */
209 block_t *p_block = block_New( p_dec, 0 );
210 input_DecoderDecode( p_dec, p_block );
212 vlc_thread_join( p_dec );
214 /* Don't module_Unneed() here because of the dll loader that wants
215 * close() in the same thread than open()/decode() */
220 input_DecoderDecode( p_dec, NULL );
222 module_Unneed( p_dec, p_dec->p_module );
225 /* Delete decoder configuration */
226 DeleteDecoder( p_dec );
228 /* Delete the decoder */
229 vlc_object_destroy( p_dec );
233 * Put a block_t in the decoder's fifo.
235 * \param p_dec the decoder object
236 * \param p_block the data block
238 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
240 if( p_dec->p_owner->b_own_thread )
242 if( p_dec->p_owner->p_input->p->b_out_pace_control )
245 while( !p_dec->b_die && !p_dec->b_error &&
246 p_dec->p_owner->p_fifo->i_depth > 10 )
251 else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
253 /* FIXME: ideally we would check the time amount of data
254 * in the fifo instead of its size. */
255 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
256 "consumed quickly enough), resetting fifo!" );
257 block_FifoEmpty( p_dec->p_owner->p_fifo );
260 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
264 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
266 if( p_block ) block_Release( p_block );
270 DecoderDecode( p_dec, p_block );
275 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
280 if( p_dec->p_owner->b_own_thread && b_flush )
281 block_FifoEmpty( p_dec->p_owner->p_fifo );
283 /* Send a special block */
284 p_null = block_New( p_dec, 128 );
285 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
286 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
287 if( p_dec->p_owner->p_packetizer && b_flush )
288 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
289 memset( p_null->p_buffer, 0, p_null->i_buffer );
291 input_DecoderDecode( p_dec, p_null );
294 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
296 if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
304 * Create a decoder object
306 * \param p_input the input thread
307 * \param p_es the es descriptor
308 * \param i_object_type Object type as define in include/vlc_objects.h
309 * \return the decoder object
311 static decoder_t * CreateDecoder( input_thread_t *p_input,
312 es_format_t *fmt, int i_object_type )
316 p_dec = vlc_object_create( p_input, i_object_type );
319 msg_Err( p_input, "out of memory" );
323 p_dec->pf_decode_audio = 0;
324 p_dec->pf_decode_video = 0;
325 p_dec->pf_decode_sub = 0;
326 p_dec->pf_packetize = 0;
328 /* Initialize the decoder fifo */
329 p_dec->p_module = NULL;
331 memset( &null_es_format, 0, sizeof(es_format_t) );
332 es_format_Copy( &p_dec->fmt_in, fmt );
333 es_format_Copy( &p_dec->fmt_out, &null_es_format );
335 /* Allocate our private structure for the decoder */
336 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
337 if( p_dec->p_owner == NULL )
339 msg_Err( p_dec, "out of memory" );
342 p_dec->p_owner->b_own_thread = VLC_TRUE;
343 p_dec->p_owner->i_preroll_end = -1;
344 p_dec->p_owner->p_input = p_input;
345 p_dec->p_owner->p_aout = NULL;
346 p_dec->p_owner->p_aout_input = NULL;
347 p_dec->p_owner->p_vout = NULL;
348 p_dec->p_owner->p_spu_vout = NULL;
349 p_dec->p_owner->i_spu_channel = 0;
350 p_dec->p_owner->p_sout = p_input->p->p_sout;
351 p_dec->p_owner->p_sout_input = NULL;
352 p_dec->p_owner->p_packetizer = NULL;
355 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
357 msg_Err( p_dec, "out of memory" );
361 /* Set buffers allocation callbacks for the decoders */
362 p_dec->pf_aout_buffer_new = aout_new_buffer;
363 p_dec->pf_aout_buffer_del = aout_del_buffer;
364 p_dec->pf_vout_buffer_new = vout_new_buffer;
365 p_dec->pf_vout_buffer_del = vout_del_buffer;
366 p_dec->pf_picture_link = vout_link_picture;
367 p_dec->pf_picture_unlink = vout_unlink_picture;
368 p_dec->pf_spu_buffer_new = spu_new_buffer;
369 p_dec->pf_spu_buffer_del = spu_del_buffer;
371 vlc_object_attach( p_dec, p_input );
373 /* Find a suitable decoder/packetizer module */
374 if( i_object_type == VLC_OBJECT_DECODER )
375 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
377 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
379 /* Check if decoder requires already packetized data */
380 if( i_object_type == VLC_OBJECT_DECODER &&
381 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
383 p_dec->p_owner->p_packetizer =
384 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
385 if( p_dec->p_owner->p_packetizer )
387 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
390 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
393 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
395 p_dec->p_owner->p_packetizer->p_module =
396 module_Need( p_dec->p_owner->p_packetizer,
397 "packetizer", "$packetizer", 0 );
399 if( !p_dec->p_owner->p_packetizer->p_module )
401 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
402 vlc_object_detach( p_dec->p_owner->p_packetizer );
403 vlc_object_destroy( p_dec->p_owner->p_packetizer );
412 * The decoding main loop
414 * \param p_dec the decoder
417 static int DecoderThread( decoder_t * p_dec )
421 /* The decoder's main loop */
422 while( !p_dec->b_die && !p_dec->b_error )
424 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
429 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
435 while( !p_dec->b_die )
437 /* Trash all received PES packets */
438 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
439 if( p_block ) block_Release( p_block );
442 /* We do it here because of the dll loader that wants close() in the
443 * same thread than open()/decode() */
444 module_Unneed( p_dec, p_dec->p_module );
449 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
451 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
452 *pi_preroll = INT64_MAX;
453 else if( p->i_pts > 0 )
454 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
455 else if( p->i_dts > 0 )
456 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
458 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
460 input_thread_t *p_input = p_dec->p_owner->p_input;
461 aout_buffer_t *p_aout_buf;
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 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
471 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
472 p_dec->p_owner->p_aout_input, p_aout_buf );
476 if( p_dec->p_owner->i_preroll_end > 0 )
478 /* FIXME TODO flush audio output (don't know how to do that) */
479 msg_Dbg( p_dec, "End of audio preroll" );
480 p_dec->p_owner->i_preroll_end = -1;
482 aout_DecPlay( p_dec->p_owner->p_aout,
483 p_dec->p_owner->p_aout_input,
487 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
489 vlc_mutex_lock( &p_vout->picture_lock );
491 if( p_pic->i_status == READY_PICTURE )
493 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
496 else if( p_pic->i_refcount > 0 )
498 p_pic->i_status = DISPLAYED_PICTURE;
502 p_pic->i_status = DESTROYED_PICTURE;
503 p_vout->i_heap_size--;
506 vlc_mutex_unlock( &p_vout->picture_lock );
508 static void VoutFlushPicture( vout_thread_t *p_vout )
511 vlc_mutex_lock( &p_vout->picture_lock );
512 for( i = 0; i < p_vout->render.i_pictures; i++ )
514 picture_t *p_pic = p_vout->render.pp_picture[i];
516 if( p_pic->i_status != READY_PICTURE )
518 /* We cannot change picture status if it is in READY_PICTURE state,
519 * Just make sure they won't be displayed */
523 vlc_mutex_unlock( &p_vout->picture_lock );
525 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
527 input_thread_t *p_input = p_dec->p_owner->p_input;
530 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
532 vlc_mutex_lock( &p_input->p->counters.counters_lock );
533 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
534 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
536 if( p_pic->date < p_dec->p_owner->i_preroll_end )
538 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
542 if( p_dec->p_owner->i_preroll_end > 0 )
544 msg_Dbg( p_dec, "End of video preroll" );
545 if( p_dec->p_owner->p_vout )
546 VoutFlushPicture( p_dec->p_owner->p_vout );
548 p_dec->p_owner->i_preroll_end = -1;
551 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
553 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
560 * \param p_dec the decoder object
561 * \param p_block the block to decode
562 * \return VLC_SUCCESS or an error code
564 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
566 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
568 if( p_block && p_block->i_buffer <= 0 )
570 block_Release( p_block );
574 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
576 block_t *p_sout_block;
578 while( ( p_sout_block =
579 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
581 if( !p_dec->p_owner->p_sout_input )
583 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
585 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
586 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
587 if( p_dec->fmt_in.psz_language )
589 if( p_dec->p_owner->sout.psz_language )
590 free( p_dec->p_owner->sout.psz_language );
591 p_dec->p_owner->sout.psz_language =
592 strdup( p_dec->fmt_in.psz_language );
595 p_dec->p_owner->p_sout_input =
596 sout_InputNew( p_dec->p_owner->p_sout,
597 &p_dec->p_owner->sout );
599 if( p_dec->p_owner->p_sout_input == NULL )
601 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
602 (char *)&p_dec->p_owner->sout.i_codec );
603 p_dec->b_error = VLC_TRUE;
605 while( p_sout_block )
607 block_t *p_next = p_sout_block->p_next;
608 block_Release( p_sout_block );
609 p_sout_block = p_next;
615 while( p_sout_block )
617 block_t *p_next = p_sout_block->p_next;
619 p_sout_block->p_next = NULL;
620 p_sout_block->i_rate = i_rate;
622 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
625 p_sout_block = p_next;
628 /* For now it's enough, as only sout inpact on this flag */
629 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
630 p_dec->p_owner->p_input->p->b_out_pace_control )
632 msg_Dbg( p_dec, "switching to sync mode" );
633 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
635 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
636 !p_dec->p_owner->p_input->p->b_out_pace_control )
638 msg_Dbg( p_dec, "switching to async mode" );
639 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
643 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
645 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
647 if( p_dec->p_owner->p_packetizer )
649 block_t *p_packetized_block;
650 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
652 while( (p_packetized_block =
653 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
655 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
657 es_format_Clean( &p_dec->fmt_in );
658 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
661 while( p_packetized_block )
663 block_t *p_next = p_packetized_block->p_next;
664 p_packetized_block->p_next = NULL;
665 p_packetized_block->i_rate = i_rate;
667 DecoderDecodeAudio( p_dec, p_packetized_block );
669 p_packetized_block = p_next;
675 DecoderDecodeAudio( p_dec, p_block );
678 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
680 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
682 if( p_dec->p_owner->p_packetizer )
684 block_t *p_packetized_block;
685 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
687 while( (p_packetized_block =
688 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
690 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
692 es_format_Clean( &p_dec->fmt_in );
693 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
696 while( p_packetized_block )
698 block_t *p_next = p_packetized_block->p_next;
699 p_packetized_block->p_next = NULL;
700 p_packetized_block->i_rate = i_rate;
702 DecoderDecodeVideo( p_dec, p_packetized_block );
704 p_packetized_block = p_next;
710 DecoderDecodeVideo( p_dec, p_block );
713 else if( p_dec->fmt_in.i_cat == SPU_ES )
715 input_thread_t *p_input = p_dec->p_owner->p_input;
716 vout_thread_t *p_vout;
719 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
721 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
723 vlc_mutex_lock( &p_input->p->counters.counters_lock );
724 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
725 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
727 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
728 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
730 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
734 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
737 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
738 vlc_object_release( p_vout );
744 msg_Err( p_dec, "unknown ES format" );
748 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
752 * Destroys a decoder object
754 * \param p_dec the decoder object
757 static void DeleteDecoder( decoder_t * p_dec )
759 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
760 (char*)&p_dec->fmt_in.i_codec,
761 p_dec->p_owner->p_fifo->i_depth );
763 /* Free all packets still in the decoder fifo. */
764 block_FifoEmpty( p_dec->p_owner->p_fifo );
765 block_FifoRelease( p_dec->p_owner->p_fifo );
768 if( p_dec->p_owner->p_aout_input )
769 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
771 if( p_dec->p_owner->p_vout )
775 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
776 /* Hack to make sure all the the pictures are freed by the decoder */
777 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
780 if( p_pic->i_status == RESERVED_PICTURE )
781 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
782 if( p_pic->i_refcount > 0 )
783 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
787 /* We are about to die. Reattach video output to p_vlc. */
788 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
791 if( p_dec->p_owner->p_sout_input )
793 sout_InputDelete( p_dec->p_owner->p_sout_input );
794 es_format_Clean( &p_dec->p_owner->sout );
797 if( p_dec->fmt_in.i_cat == SPU_ES )
799 vout_thread_t *p_vout;
801 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
804 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
805 p_dec->p_owner->i_spu_channel );
806 vlc_object_release( p_vout );
810 es_format_Clean( &p_dec->fmt_in );
811 es_format_Clean( &p_dec->fmt_out );
813 if( p_dec->p_owner->p_packetizer )
815 module_Unneed( p_dec->p_owner->p_packetizer,
816 p_dec->p_owner->p_packetizer->p_module );
817 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
818 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
819 vlc_object_detach( p_dec->p_owner->p_packetizer );
820 vlc_object_destroy( p_dec->p_owner->p_packetizer );
823 vlc_object_detach( p_dec );
825 free( p_dec->p_owner );
828 /*****************************************************************************
829 * Buffers allocation callbacks for the decoders
830 *****************************************************************************/
831 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
833 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
834 aout_buffer_t *p_buffer;
836 if( p_sys->p_aout_input != NULL &&
837 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
838 p_dec->fmt_out.audio.i_original_channels !=
839 p_sys->audio.i_original_channels ||
840 p_dec->fmt_out.audio.i_bytes_per_frame !=
841 p_sys->audio.i_bytes_per_frame ) )
843 /* Parameters changed, restart the aout */
844 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
845 p_sys->p_aout_input = NULL;
848 if( p_sys->p_aout_input == NULL )
850 audio_sample_format_t format;
851 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
853 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
854 p_sys->audio = p_dec->fmt_out.audio;
856 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
857 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
858 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
860 if ( i_force_dolby == 1 )
862 format.i_original_channels = format.i_original_channels |
863 AOUT_CHAN_DOLBYSTEREO;
865 else /* i_force_dolby == 2 */
867 format.i_original_channels = format.i_original_channels &
868 ~AOUT_CHAN_DOLBYSTEREO;
872 p_sys->p_aout_input =
873 aout_DecNew( p_dec, &p_sys->p_aout, &format );
874 if( p_sys->p_aout_input == NULL )
876 msg_Err( p_dec, "failed to create audio output" );
877 p_dec->b_error = VLC_TRUE;
880 p_dec->fmt_out.audio.i_bytes_per_frame =
881 p_sys->audio.i_bytes_per_frame;
884 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
890 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
892 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
893 p_dec->p_owner->p_aout_input, p_buffer );
896 static picture_t *vout_new_buffer( decoder_t *p_dec )
898 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
901 if( p_sys->p_vout == NULL ||
902 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
903 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
904 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
905 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
907 if( !p_dec->fmt_out.video.i_width ||
908 !p_dec->fmt_out.video.i_height )
910 /* Can't create a new vout without display size */
914 if( !p_dec->fmt_out.video.i_visible_width ||
915 !p_dec->fmt_out.video.i_visible_height )
917 if( p_dec->fmt_in.video.i_visible_width &&
918 p_dec->fmt_in.video.i_visible_height )
920 p_dec->fmt_out.video.i_visible_width =
921 p_dec->fmt_in.video.i_visible_width;
922 p_dec->fmt_out.video.i_visible_height =
923 p_dec->fmt_in.video.i_visible_height;
927 p_dec->fmt_out.video.i_visible_width =
928 p_dec->fmt_out.video.i_width;
929 p_dec->fmt_out.video.i_visible_height =
930 p_dec->fmt_out.video.i_height;
934 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
935 var_CreateGetBool( p_dec, "hdtv-fix" ) )
937 p_dec->fmt_out.video.i_visible_height = 1080;
938 p_dec->fmt_out.video.i_sar_num *= 135;
939 p_dec->fmt_out.video.i_sar_den *= 136;
940 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
943 if( !p_dec->fmt_out.video.i_sar_num ||
944 !p_dec->fmt_out.video.i_sar_den )
946 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
947 p_dec->fmt_out.video.i_visible_height;
949 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
950 p_dec->fmt_out.video.i_visible_width;
953 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
954 &p_dec->fmt_out.video.i_sar_den,
955 p_dec->fmt_out.video.i_sar_num,
956 p_dec->fmt_out.video.i_sar_den, 50000 );
958 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
959 p_sys->video = p_dec->fmt_out.video;
961 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
962 &p_dec->fmt_out.video );
963 if( p_sys->p_vout == NULL )
965 msg_Err( p_dec, "failed to create video output" );
966 p_dec->b_error = VLC_TRUE;
970 if( p_sys->video.i_rmask )
971 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
972 if( p_sys->video.i_gmask )
973 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
974 if( p_sys->video.i_bmask )
975 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
978 /* Get a new picture */
979 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
981 int i_pic, i_ready_pic = 0;
983 if( p_dec->b_die || p_dec->b_error )
988 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
989 /* Check the decoder doesn't leak pictures */
990 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
993 if( p_pic->i_status == READY_PICTURE )
995 if( i_ready_pic++ > 0 ) break;
999 if( p_pic->i_status != DISPLAYED_PICTURE &&
1000 p_pic->i_status != RESERVED_PICTURE &&
1001 p_pic->i_status != READY_PICTURE ) break;
1003 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1006 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1008 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1010 /* Just free all the pictures */
1011 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1014 if( p_pic->i_status == RESERVED_PICTURE )
1015 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1016 if( p_pic->i_refcount > 0 )
1017 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1022 msleep( VOUT_OUTMEM_SLEEP );
1028 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1030 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1033 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1035 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1038 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1040 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1043 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1045 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1046 vout_thread_t *p_vout = NULL;
1047 subpicture_t *p_subpic;
1048 int i_attempts = 30;
1050 while( i_attempts-- )
1052 if( p_dec->b_die || p_dec->b_error ) break;
1054 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1057 msleep( VOUT_DISPLAY_DELAY );
1062 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1066 if( p_sys->p_spu_vout != p_vout )
1068 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1069 &p_sys->i_spu_channel );
1070 p_sys->p_spu_vout = p_vout;
1073 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1076 p_subpic->i_channel = p_sys->i_spu_channel;
1079 vlc_object_release( p_vout );
1084 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1086 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1087 vout_thread_t *p_vout = NULL;
1089 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1090 if( !p_vout || p_sys->p_spu_vout != p_vout )
1093 vlc_object_release( p_vout );
1094 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1098 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1100 vlc_object_release( p_vout );