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,
98 const char *psz_name )
100 input_attachment_t *p_attachment;
101 if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
105 /* decoder_GetInputAttachments:
107 int decoder_GetInputAttachments( decoder_t *p_dec,
108 input_attachment_t ***ppp_attachment,
111 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
112 ppp_attachment, pi_attachment );
116 * Spawns a new decoder thread
118 * \param p_input the input thread
119 * \param p_es the es descriptor
120 * \return the spawned decoder object
122 decoder_t *input_DecoderNew( input_thread_t *p_input,
123 es_format_t *fmt, vlc_bool_t b_force_decoder )
125 decoder_t *p_dec = NULL;
128 /* If we are in sout mode, search for packetizer module */
129 if( p_input->p->p_sout && !b_force_decoder )
131 /* Create the decoder configuration structure */
132 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
135 msg_Err( p_input, "could not create packetizer" );
136 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
137 _("VLC could not open the packetizer module.") );
143 /* Create the decoder configuration structure */
144 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
147 msg_Err( p_input, "could not create decoder" );
148 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
149 _("VLC could not open the decoder module.") );
154 if( !p_dec->p_module )
156 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
157 "VLC probably does not support this sound or video format.",
158 (char*)&p_dec->fmt_in.i_codec );
159 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
160 "for format"), _("VLC probably does not support the \"%4.4s\" "
161 "audio or video format. Unfortunately there is no way for you "
162 "to fix this."), (char*)&p_dec->fmt_in.i_codec );
164 DeleteDecoder( p_dec );
165 vlc_object_destroy( p_dec );
169 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
172 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
173 p_dec->p_owner->b_own_thread = VLC_FALSE;
177 var_Get( p_input, "minimize-threads", &val );
178 p_dec->p_owner->b_own_thread = !val.b_bool;
181 if( p_dec->p_owner->b_own_thread )
184 if( fmt->i_cat == AUDIO_ES )
185 i_priority = VLC_THREAD_PRIORITY_AUDIO;
187 i_priority = VLC_THREAD_PRIORITY_VIDEO;
189 /* Spawn the decoder thread */
190 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
191 i_priority, VLC_FALSE ) )
193 msg_Err( p_dec, "cannot spawn decoder thread" );
194 module_Unneed( p_dec, p_dec->p_module );
195 DeleteDecoder( p_dec );
196 vlc_object_destroy( p_dec );
205 * Kills a decoder thread and waits until it's finished
207 * \param p_input the input thread
208 * \param p_es the es descriptor
211 void input_DecoderDelete( decoder_t *p_dec )
213 vlc_object_kill( p_dec );
215 if( p_dec->p_owner->b_own_thread )
217 /* Make sure the thread leaves the function by
218 * sending it an empty block. */
219 block_t *p_block = block_New( p_dec, 0 );
220 input_DecoderDecode( p_dec, p_block );
222 vlc_thread_join( p_dec );
224 /* Don't module_Unneed() here because of the dll loader that wants
225 * close() in the same thread than open()/decode() */
230 input_DecoderDecode( p_dec, NULL );
232 module_Unneed( p_dec, p_dec->p_module );
235 /* Delete decoder configuration */
236 DeleteDecoder( p_dec );
238 /* Delete the decoder */
239 vlc_object_destroy( p_dec );
243 * Put a block_t in the decoder's fifo.
245 * \param p_dec the decoder object
246 * \param p_block the data block
248 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
250 if( p_dec->p_owner->b_own_thread )
252 if( p_dec->p_owner->p_input->p->b_out_pace_control )
255 while( !p_dec->b_die && !p_dec->b_error &&
256 p_dec->p_owner->p_fifo->i_depth > 10 )
261 else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
263 /* FIXME: ideally we would check the time amount of data
264 * in the fifo instead of its size. */
265 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
266 "consumed quickly enough), resetting fifo!" );
267 block_FifoEmpty( p_dec->p_owner->p_fifo );
270 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
274 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
276 if( p_block ) block_Release( p_block );
280 DecoderDecode( p_dec, p_block );
285 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
290 if( p_dec->p_owner->b_own_thread && b_flush )
291 block_FifoEmpty( p_dec->p_owner->p_fifo );
293 /* Send a special block */
294 p_null = block_New( p_dec, 128 );
295 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
296 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
297 if( p_dec->p_owner->p_packetizer && b_flush )
298 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
299 memset( p_null->p_buffer, 0, p_null->i_buffer );
301 input_DecoderDecode( p_dec, p_null );
304 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
306 if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
314 * Create a decoder object
316 * \param p_input the input thread
317 * \param p_es the es descriptor
318 * \param i_object_type Object type as define in include/vlc_objects.h
319 * \return the decoder object
321 static decoder_t * CreateDecoder( input_thread_t *p_input,
322 es_format_t *fmt, int i_object_type )
326 p_dec = vlc_object_create( p_input, i_object_type );
329 msg_Err( p_input, "out of memory" );
333 p_dec->pf_decode_audio = 0;
334 p_dec->pf_decode_video = 0;
335 p_dec->pf_decode_sub = 0;
336 p_dec->pf_packetize = 0;
338 /* Initialize the decoder fifo */
339 p_dec->p_module = NULL;
341 memset( &null_es_format, 0, sizeof(es_format_t) );
342 es_format_Copy( &p_dec->fmt_in, fmt );
343 es_format_Copy( &p_dec->fmt_out, &null_es_format );
345 /* Allocate our private structure for the decoder */
346 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
347 if( p_dec->p_owner == NULL )
349 msg_Err( p_dec, "out of memory" );
352 p_dec->p_owner->b_own_thread = VLC_TRUE;
353 p_dec->p_owner->i_preroll_end = -1;
354 p_dec->p_owner->p_input = p_input;
355 p_dec->p_owner->p_aout = NULL;
356 p_dec->p_owner->p_aout_input = NULL;
357 p_dec->p_owner->p_vout = NULL;
358 p_dec->p_owner->p_spu_vout = NULL;
359 p_dec->p_owner->i_spu_channel = 0;
360 p_dec->p_owner->p_sout = p_input->p->p_sout;
361 p_dec->p_owner->p_sout_input = NULL;
362 p_dec->p_owner->p_packetizer = NULL;
365 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
367 msg_Err( p_dec, "out of memory" );
371 /* Set buffers allocation callbacks for the decoders */
372 p_dec->pf_aout_buffer_new = aout_new_buffer;
373 p_dec->pf_aout_buffer_del = aout_del_buffer;
374 p_dec->pf_vout_buffer_new = vout_new_buffer;
375 p_dec->pf_vout_buffer_del = vout_del_buffer;
376 p_dec->pf_picture_link = vout_link_picture;
377 p_dec->pf_picture_unlink = vout_unlink_picture;
378 p_dec->pf_spu_buffer_new = spu_new_buffer;
379 p_dec->pf_spu_buffer_del = spu_del_buffer;
381 vlc_object_attach( p_dec, p_input );
383 /* Find a suitable decoder/packetizer module */
384 if( i_object_type == VLC_OBJECT_DECODER )
385 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
387 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
389 /* Check if decoder requires already packetized data */
390 if( i_object_type == VLC_OBJECT_DECODER &&
391 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
393 p_dec->p_owner->p_packetizer =
394 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
395 if( p_dec->p_owner->p_packetizer )
397 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
400 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
403 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
405 p_dec->p_owner->p_packetizer->p_module =
406 module_Need( p_dec->p_owner->p_packetizer,
407 "packetizer", "$packetizer", 0 );
409 if( !p_dec->p_owner->p_packetizer->p_module )
411 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
412 vlc_object_detach( p_dec->p_owner->p_packetizer );
413 vlc_object_destroy( p_dec->p_owner->p_packetizer );
422 * The decoding main loop
424 * \param p_dec the decoder
427 static int DecoderThread( decoder_t * p_dec )
431 /* The decoder's main loop */
432 while( !p_dec->b_die && !p_dec->b_error )
434 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
439 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
445 while( !p_dec->b_die )
447 /* Trash all received PES packets */
448 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
449 if( p_block ) block_Release( p_block );
452 /* We do it here because of the dll loader that wants close() in the
453 * same thread than open()/decode() */
454 module_Unneed( p_dec, p_dec->p_module );
459 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
461 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
462 *pi_preroll = INT64_MAX;
463 else if( p->i_pts > 0 )
464 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
465 else if( p->i_dts > 0 )
466 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
468 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
470 input_thread_t *p_input = p_dec->p_owner->p_input;
471 const int i_rate = p_block->i_rate;
472 aout_buffer_t *p_aout_buf;
474 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
476 vlc_mutex_lock( &p_input->p->counters.counters_lock );
477 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
478 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
480 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
482 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
483 p_dec->p_owner->p_aout_input, p_aout_buf );
487 if( p_dec->p_owner->i_preroll_end > 0 )
489 /* FIXME TODO flush audio output (don't know how to do that) */
490 msg_Dbg( p_dec, "End of audio preroll" );
491 p_dec->p_owner->i_preroll_end = -1;
493 aout_DecPlay( p_dec->p_owner->p_aout,
494 p_dec->p_owner->p_aout_input,
495 p_aout_buf, i_rate );
498 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
500 vlc_mutex_lock( &p_vout->picture_lock );
502 if( p_pic->i_status == READY_PICTURE )
504 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
507 else if( p_pic->i_refcount > 0 )
509 p_pic->i_status = DISPLAYED_PICTURE;
513 p_pic->i_status = DESTROYED_PICTURE;
514 p_vout->i_heap_size--;
517 vlc_mutex_unlock( &p_vout->picture_lock );
519 static void VoutFlushPicture( vout_thread_t *p_vout )
522 vlc_mutex_lock( &p_vout->picture_lock );
523 for( i = 0; i < p_vout->render.i_pictures; i++ )
525 picture_t *p_pic = p_vout->render.pp_picture[i];
527 if( p_pic->i_status == READY_PICTURE ||
528 p_pic->i_status == DISPLAYED_PICTURE )
530 /* We cannot change picture status if it is in READY_PICTURE state,
531 * Just make sure they won't be displayed */
535 vlc_mutex_unlock( &p_vout->picture_lock );
537 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
539 input_thread_t *p_input = p_dec->p_owner->p_input;
542 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
544 vlc_mutex_lock( &p_input->p->counters.counters_lock );
545 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
546 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
548 if( p_pic->date < p_dec->p_owner->i_preroll_end )
550 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
554 if( p_dec->p_owner->i_preroll_end > 0 )
556 msg_Dbg( p_dec, "End of video preroll" );
557 if( p_dec->p_owner->p_vout )
558 VoutFlushPicture( p_dec->p_owner->p_vout );
560 p_dec->p_owner->i_preroll_end = -1;
563 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
565 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
572 * \param p_dec the decoder object
573 * \param p_block the block to decode
574 * \return VLC_SUCCESS or an error code
576 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
578 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
579 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
581 if( p_block && p_block->i_buffer <= 0 )
583 block_Release( p_block );
587 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
589 block_t *p_sout_block;
591 while( ( p_sout_block =
592 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
594 if( !p_dec->p_owner->p_sout_input )
596 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
598 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
599 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
600 if( p_dec->fmt_in.psz_language )
602 if( p_dec->p_owner->sout.psz_language )
603 free( p_dec->p_owner->sout.psz_language );
604 p_dec->p_owner->sout.psz_language =
605 strdup( p_dec->fmt_in.psz_language );
608 p_dec->p_owner->p_sout_input =
609 sout_InputNew( p_dec->p_owner->p_sout,
610 &p_dec->p_owner->sout );
612 if( p_dec->p_owner->p_sout_input == NULL )
614 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
615 (char *)&p_dec->p_owner->sout.i_codec );
616 p_dec->b_error = VLC_TRUE;
618 while( p_sout_block )
620 block_t *p_next = p_sout_block->p_next;
621 block_Release( p_sout_block );
622 p_sout_block = p_next;
628 while( p_sout_block )
630 block_t *p_next = p_sout_block->p_next;
632 p_sout_block->p_next = NULL;
633 p_sout_block->i_rate = i_rate;
635 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
638 p_sout_block = p_next;
641 /* For now it's enough, as only sout inpact on this flag */
642 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
643 p_dec->p_owner->p_input->p->b_out_pace_control )
645 msg_Dbg( p_dec, "switching to sync mode" );
646 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
648 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
649 !p_dec->p_owner->p_input->p->b_out_pace_control )
651 msg_Dbg( p_dec, "switching to async mode" );
652 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
656 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
658 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
660 if( p_dec->p_owner->p_packetizer )
662 block_t *p_packetized_block;
663 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
665 while( (p_packetized_block =
666 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
668 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
670 es_format_Clean( &p_dec->fmt_in );
671 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
674 while( p_packetized_block )
676 block_t *p_next = p_packetized_block->p_next;
677 p_packetized_block->p_next = NULL;
678 p_packetized_block->i_rate = i_rate;
680 DecoderDecodeAudio( p_dec, p_packetized_block );
682 p_packetized_block = p_next;
688 DecoderDecodeAudio( p_dec, p_block );
691 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
693 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
695 if( p_dec->p_owner->p_packetizer )
697 block_t *p_packetized_block;
698 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
700 while( (p_packetized_block =
701 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
703 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
705 es_format_Clean( &p_dec->fmt_in );
706 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
709 while( p_packetized_block )
711 block_t *p_next = p_packetized_block->p_next;
712 p_packetized_block->p_next = NULL;
713 p_packetized_block->i_rate = i_rate;
715 DecoderDecodeVideo( p_dec, p_packetized_block );
717 p_packetized_block = p_next;
723 DecoderDecodeVideo( p_dec, p_block );
726 else if( p_dec->fmt_in.i_cat == SPU_ES )
728 input_thread_t *p_input = p_dec->p_owner->p_input;
729 vout_thread_t *p_vout;
732 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
734 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
736 vlc_mutex_lock( &p_input->p->counters.counters_lock );
737 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
738 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
740 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
741 if( p_vout && p_sys->p_spu_vout == p_vout )
743 /* Prerool does not work very well with subtitle */
744 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
745 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
746 spu_DestroySubpicture( p_vout->p_spu, p_spu );
748 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
752 msg_Warn( p_dec, "no vout found, leaking subpicture" );
755 vlc_object_release( p_vout );
760 msg_Err( p_dec, "unknown ES format" );
764 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
768 * Destroys a decoder object
770 * \param p_dec the decoder object
773 static void DeleteDecoder( decoder_t * p_dec )
775 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
776 (char*)&p_dec->fmt_in.i_codec,
777 p_dec->p_owner->p_fifo->i_depth );
779 /* Free all packets still in the decoder fifo. */
780 block_FifoEmpty( p_dec->p_owner->p_fifo );
781 block_FifoRelease( p_dec->p_owner->p_fifo );
784 if( p_dec->p_owner->p_aout_input )
785 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
787 if( p_dec->p_owner->p_vout )
791 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
792 /* Hack to make sure all the the pictures are freed by the decoder */
793 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
796 if( p_pic->i_status == RESERVED_PICTURE )
797 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
798 if( p_pic->i_refcount > 0 )
799 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
803 /* We are about to die. Reattach video output to p_vlc. */
804 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
807 if( p_dec->p_owner->p_sout_input )
809 sout_InputDelete( p_dec->p_owner->p_sout_input );
810 es_format_Clean( &p_dec->p_owner->sout );
813 if( p_dec->fmt_in.i_cat == SPU_ES )
815 vout_thread_t *p_vout;
817 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
820 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
821 p_dec->p_owner->i_spu_channel );
822 vlc_object_release( p_vout );
826 es_format_Clean( &p_dec->fmt_in );
827 es_format_Clean( &p_dec->fmt_out );
829 if( p_dec->p_owner->p_packetizer )
831 module_Unneed( p_dec->p_owner->p_packetizer,
832 p_dec->p_owner->p_packetizer->p_module );
833 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
834 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
835 vlc_object_detach( p_dec->p_owner->p_packetizer );
836 vlc_object_destroy( p_dec->p_owner->p_packetizer );
839 vlc_object_detach( p_dec );
841 free( p_dec->p_owner );
844 /*****************************************************************************
845 * Buffers allocation callbacks for the decoders
846 *****************************************************************************/
847 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
849 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
850 aout_buffer_t *p_buffer;
852 if( p_sys->p_aout_input != NULL &&
853 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
854 p_dec->fmt_out.audio.i_original_channels !=
855 p_sys->audio.i_original_channels ||
856 p_dec->fmt_out.audio.i_bytes_per_frame !=
857 p_sys->audio.i_bytes_per_frame ) )
859 /* Parameters changed, restart the aout */
860 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
861 p_sys->p_aout_input = NULL;
864 if( p_sys->p_aout_input == NULL )
866 audio_sample_format_t format;
867 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
869 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
870 p_sys->audio = p_dec->fmt_out.audio;
872 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
873 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
874 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
876 if ( i_force_dolby == 1 )
878 format.i_original_channels = format.i_original_channels |
879 AOUT_CHAN_DOLBYSTEREO;
881 else /* i_force_dolby == 2 */
883 format.i_original_channels = format.i_original_channels &
884 ~AOUT_CHAN_DOLBYSTEREO;
888 p_sys->p_aout_input =
889 aout_DecNew( p_dec, &p_sys->p_aout, &format );
890 if( p_sys->p_aout_input == NULL )
892 msg_Err( p_dec, "failed to create audio output" );
893 p_dec->b_error = VLC_TRUE;
896 p_dec->fmt_out.audio.i_bytes_per_frame =
897 p_sys->audio.i_bytes_per_frame;
900 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
906 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
908 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
909 p_dec->p_owner->p_aout_input, p_buffer );
912 static picture_t *vout_new_buffer( decoder_t *p_dec )
914 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
917 if( p_sys->p_vout == NULL ||
918 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
919 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
920 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
921 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
923 if( !p_dec->fmt_out.video.i_width ||
924 !p_dec->fmt_out.video.i_height )
926 /* Can't create a new vout without display size */
930 if( !p_dec->fmt_out.video.i_visible_width ||
931 !p_dec->fmt_out.video.i_visible_height )
933 if( p_dec->fmt_in.video.i_visible_width &&
934 p_dec->fmt_in.video.i_visible_height )
936 p_dec->fmt_out.video.i_visible_width =
937 p_dec->fmt_in.video.i_visible_width;
938 p_dec->fmt_out.video.i_visible_height =
939 p_dec->fmt_in.video.i_visible_height;
943 p_dec->fmt_out.video.i_visible_width =
944 p_dec->fmt_out.video.i_width;
945 p_dec->fmt_out.video.i_visible_height =
946 p_dec->fmt_out.video.i_height;
950 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
951 var_CreateGetBool( p_dec, "hdtv-fix" ) )
953 p_dec->fmt_out.video.i_visible_height = 1080;
954 p_dec->fmt_out.video.i_sar_num *= 135;
955 p_dec->fmt_out.video.i_sar_den *= 136;
956 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
959 if( !p_dec->fmt_out.video.i_sar_num ||
960 !p_dec->fmt_out.video.i_sar_den )
962 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
963 p_dec->fmt_out.video.i_visible_height;
965 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
966 p_dec->fmt_out.video.i_visible_width;
969 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
970 &p_dec->fmt_out.video.i_sar_den,
971 p_dec->fmt_out.video.i_sar_num,
972 p_dec->fmt_out.video.i_sar_den, 50000 );
974 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
975 p_sys->video = p_dec->fmt_out.video;
977 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
978 &p_dec->fmt_out.video );
979 if( p_sys->p_vout == NULL )
981 msg_Err( p_dec, "failed to create video output" );
982 p_dec->b_error = VLC_TRUE;
986 if( p_sys->video.i_rmask )
987 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
988 if( p_sys->video.i_gmask )
989 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
990 if( p_sys->video.i_bmask )
991 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
994 /* Get a new picture */
995 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
997 int i_pic, i_ready_pic = 0;
999 if( p_dec->b_die || p_dec->b_error )
1004 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1005 /* Check the decoder doesn't leak pictures */
1006 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1009 if( p_pic->i_status == READY_PICTURE )
1011 if( i_ready_pic++ > 0 ) break;
1015 if( p_pic->i_status != DISPLAYED_PICTURE &&
1016 p_pic->i_status != RESERVED_PICTURE &&
1017 p_pic->i_status != READY_PICTURE ) break;
1019 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1022 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1024 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1026 /* Just free all the pictures */
1027 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1030 if( p_pic->i_status == RESERVED_PICTURE )
1031 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1032 if( p_pic->i_refcount > 0 )
1033 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1038 msleep( VOUT_OUTMEM_SLEEP );
1044 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1046 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1049 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1051 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1054 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1056 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1059 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1061 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1062 vout_thread_t *p_vout = NULL;
1063 subpicture_t *p_subpic;
1064 int i_attempts = 30;
1066 while( i_attempts-- )
1068 if( p_dec->b_die || p_dec->b_error ) break;
1070 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1073 msleep( VOUT_DISPLAY_DELAY );
1078 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1082 if( p_sys->p_spu_vout != p_vout )
1084 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1085 &p_sys->i_spu_channel );
1086 p_sys->p_spu_vout = p_vout;
1089 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1092 p_subpic->i_channel = p_sys->i_spu_channel;
1095 vlc_object_release( p_vout );
1100 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1102 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1103 vout_thread_t *p_vout = NULL;
1105 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1106 if( !p_vout || p_sys->p_spu_vout != p_vout )
1109 vlc_object_release( p_vout );
1110 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1114 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1116 vlc_object_release( p_vout );