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 *****************************************************************************/
31 #include <vlc_block.h>
35 #include <vlc_codec.h>
38 #include <vlc_interface.h>
39 #include "audio_output/aout_internal.h"
40 #include "stream_output/stream_output.h"
41 #include "input_internal.h"
43 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
44 static void DeleteDecoder( decoder_t * );
46 static int DecoderThread( decoder_t * );
47 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
49 /* Buffers allocation callbacks for the decoders */
50 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
51 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
53 static picture_t *vout_new_buffer( decoder_t * );
54 static void vout_del_buffer( decoder_t *, picture_t * );
55 static void vout_link_picture( decoder_t *, picture_t * );
56 static void vout_unlink_picture( decoder_t *, picture_t * );
58 static subpicture_t *spu_new_buffer( decoder_t * );
59 static void spu_del_buffer( decoder_t *, subpicture_t * );
61 static es_format_t null_es_format;
63 struct decoder_owner_sys_t
65 vlc_bool_t b_own_thread;
67 int64_t i_preroll_end;
69 input_thread_t *p_input;
71 aout_instance_t *p_aout;
72 aout_input_t *p_aout_input;
74 vout_thread_t *p_vout;
76 vout_thread_t *p_spu_vout;
79 sout_instance_t *p_sout;
80 sout_packetizer_input_t *p_sout_input;
82 /* Some decoders require already packetized data (ie. not truncated) */
83 decoder_t *p_packetizer;
85 /* Current format in use by the output */
94 /* decoder_GetInputAttachment:
96 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
97 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 ) )
104 /* decoder_GetInputAttachments:
106 int decoder_GetInputAttachments( decoder_t *p_dec,
107 input_attachment_t ***ppp_attachment,
110 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
111 ppp_attachment, pi_attachment );
113 /* decoder_GetDisplayDate:
115 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
121 * Spawns a new decoder thread
123 * \param p_input the input thread
124 * \param p_es the es descriptor
125 * \return the spawned decoder object
127 decoder_t *input_DecoderNew( input_thread_t *p_input,
128 es_format_t *fmt, vlc_bool_t b_force_decoder )
130 decoder_t *p_dec = NULL;
133 /* If we are in sout mode, search for packetizer module */
134 if( p_input->p->p_sout && !b_force_decoder )
136 /* Create the decoder configuration structure */
137 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
140 msg_Err( p_input, "could not create packetizer" );
141 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
142 _("VLC could not open the packetizer module.") );
148 /* Create the decoder configuration structure */
149 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
152 msg_Err( p_input, "could not create decoder" );
153 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
154 _("VLC could not open the decoder module.") );
159 if( !p_dec->p_module )
161 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
162 "VLC probably does not support this sound or video format.",
163 (char*)&p_dec->fmt_in.i_codec );
164 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
165 "for format"), _("VLC probably does not support the \"%4.4s\" "
166 "audio or video format. Unfortunately there is no way for you "
167 "to fix this."), (char*)&p_dec->fmt_in.i_codec );
169 DeleteDecoder( p_dec );
170 vlc_object_destroy( p_dec );
174 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
177 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
178 p_dec->p_owner->b_own_thread = VLC_FALSE;
182 var_Get( p_input, "minimize-threads", &val );
183 p_dec->p_owner->b_own_thread = !val.b_bool;
186 if( p_dec->p_owner->b_own_thread )
189 if( fmt->i_cat == AUDIO_ES )
190 i_priority = VLC_THREAD_PRIORITY_AUDIO;
192 i_priority = VLC_THREAD_PRIORITY_VIDEO;
194 /* Spawn the decoder thread */
195 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
196 i_priority, VLC_FALSE ) )
198 msg_Err( p_dec, "cannot spawn decoder thread" );
199 module_Unneed( p_dec, p_dec->p_module );
200 DeleteDecoder( p_dec );
201 vlc_object_destroy( p_dec );
210 * Kills a decoder thread and waits until it's finished
212 * \param p_input the input thread
213 * \param p_es the es descriptor
216 void input_DecoderDelete( decoder_t *p_dec )
218 vlc_object_kill( p_dec );
220 if( p_dec->p_owner->b_own_thread )
222 /* Make sure the thread leaves the function by
223 * sending it an empty block. */
224 block_t *p_block = block_New( p_dec, 0 );
225 input_DecoderDecode( p_dec, p_block );
227 vlc_thread_join( p_dec );
229 /* Don't module_Unneed() here because of the dll loader that wants
230 * close() in the same thread than open()/decode() */
235 input_DecoderDecode( p_dec, NULL );
237 module_Unneed( p_dec, p_dec->p_module );
240 /* Delete decoder configuration */
241 DeleteDecoder( p_dec );
243 /* Delete the decoder */
244 vlc_object_destroy( p_dec );
248 * Put a block_t in the decoder's fifo.
250 * \param p_dec the decoder object
251 * \param p_block the data block
253 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
255 if( p_dec->p_owner->b_own_thread )
257 if( p_dec->p_owner->p_input->p->b_out_pace_control )
260 while( !p_dec->b_die && !p_dec->b_error &&
261 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
266 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
268 /* FIXME: ideally we would check the time amount of data
269 * in the fifo instead of its size. */
270 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
271 "consumed quickly enough), resetting fifo!" );
272 block_FifoEmpty( p_dec->p_owner->p_fifo );
275 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
279 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
281 if( p_block ) block_Release( p_block );
285 DecoderDecode( p_dec, p_block );
290 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
295 if( p_dec->p_owner->b_own_thread && b_flush )
296 block_FifoEmpty( p_dec->p_owner->p_fifo );
298 /* Send a special block */
299 p_null = block_New( p_dec, 128 );
300 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
301 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
302 if( p_dec->p_owner->p_packetizer && b_flush )
303 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
304 memset( p_null->p_buffer, 0, p_null->i_buffer );
306 input_DecoderDecode( p_dec, p_null );
309 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
311 if( p_dec->p_owner->b_own_thread
312 && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
320 * Create a decoder object
322 * \param p_input the input thread
323 * \param p_es the es descriptor
324 * \param i_object_type Object type as define in include/vlc_objects.h
325 * \return the decoder object
327 static decoder_t * CreateDecoder( input_thread_t *p_input,
328 es_format_t *fmt, int i_object_type )
332 p_dec = vlc_object_create( p_input, i_object_type );
335 msg_Err( p_input, "out of memory" );
339 p_dec->pf_decode_audio = 0;
340 p_dec->pf_decode_video = 0;
341 p_dec->pf_decode_sub = 0;
342 p_dec->pf_packetize = 0;
344 /* Initialize the decoder fifo */
345 p_dec->p_module = NULL;
347 memset( &null_es_format, 0, sizeof(es_format_t) );
348 es_format_Copy( &p_dec->fmt_in, fmt );
349 es_format_Copy( &p_dec->fmt_out, &null_es_format );
351 /* Allocate our private structure for the decoder */
352 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
353 if( p_dec->p_owner == NULL )
355 msg_Err( p_dec, "out of memory" );
358 p_dec->p_owner->b_own_thread = VLC_TRUE;
359 p_dec->p_owner->i_preroll_end = -1;
360 p_dec->p_owner->p_input = p_input;
361 p_dec->p_owner->p_aout = NULL;
362 p_dec->p_owner->p_aout_input = NULL;
363 p_dec->p_owner->p_vout = NULL;
364 p_dec->p_owner->p_spu_vout = NULL;
365 p_dec->p_owner->i_spu_channel = 0;
366 p_dec->p_owner->p_sout = p_input->p->p_sout;
367 p_dec->p_owner->p_sout_input = NULL;
368 p_dec->p_owner->p_packetizer = NULL;
371 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
373 msg_Err( p_dec, "out of memory" );
377 /* Set buffers allocation callbacks for the decoders */
378 p_dec->pf_aout_buffer_new = aout_new_buffer;
379 p_dec->pf_aout_buffer_del = aout_del_buffer;
380 p_dec->pf_vout_buffer_new = vout_new_buffer;
381 p_dec->pf_vout_buffer_del = vout_del_buffer;
382 p_dec->pf_picture_link = vout_link_picture;
383 p_dec->pf_picture_unlink = vout_unlink_picture;
384 p_dec->pf_spu_buffer_new = spu_new_buffer;
385 p_dec->pf_spu_buffer_del = spu_del_buffer;
387 vlc_object_attach( p_dec, p_input );
389 /* Find a suitable decoder/packetizer module */
390 if( i_object_type == VLC_OBJECT_DECODER )
391 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
393 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
395 /* Check if decoder requires already packetized data */
396 if( i_object_type == VLC_OBJECT_DECODER &&
397 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
399 p_dec->p_owner->p_packetizer =
400 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
401 if( p_dec->p_owner->p_packetizer )
403 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
406 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
409 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
411 p_dec->p_owner->p_packetizer->p_module =
412 module_Need( p_dec->p_owner->p_packetizer,
413 "packetizer", "$packetizer", 0 );
415 if( !p_dec->p_owner->p_packetizer->p_module )
417 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
418 vlc_object_detach( p_dec->p_owner->p_packetizer );
419 vlc_object_destroy( p_dec->p_owner->p_packetizer );
424 /* Copy ourself the input replay gain */
425 if( fmt->i_cat == AUDIO_ES )
428 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
430 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
432 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
433 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
435 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
437 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
438 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
446 * The decoding main loop
448 * \param p_dec the decoder
451 static int DecoderThread( decoder_t * p_dec )
455 /* The decoder's main loop */
456 while( !p_dec->b_die && !p_dec->b_error )
458 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
463 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
469 while( !p_dec->b_die )
471 /* Trash all received PES packets */
472 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
473 if( p_block ) block_Release( p_block );
476 /* We do it here because of the dll loader that wants close() in the
477 * same thread than open()/decode() */
478 module_Unneed( p_dec, p_dec->p_module );
483 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
485 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
486 *pi_preroll = INT64_MAX;
487 else if( p->i_pts > 0 )
488 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
489 else if( p->i_dts > 0 )
490 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
492 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
494 input_thread_t *p_input = p_dec->p_owner->p_input;
495 const int i_rate = p_block->i_rate;
496 aout_buffer_t *p_aout_buf;
498 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
500 vlc_mutex_lock( &p_input->p->counters.counters_lock );
501 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
502 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
504 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
506 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
507 p_dec->p_owner->p_aout_input, p_aout_buf );
511 if( p_dec->p_owner->i_preroll_end > 0 )
513 /* FIXME TODO flush audio output (don't know how to do that) */
514 msg_Dbg( p_dec, "End of audio preroll" );
515 p_dec->p_owner->i_preroll_end = -1;
517 aout_DecPlay( p_dec->p_owner->p_aout,
518 p_dec->p_owner->p_aout_input,
519 p_aout_buf, i_rate );
522 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
524 vlc_mutex_lock( &p_vout->picture_lock );
526 if( p_pic->i_status == READY_PICTURE )
528 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
531 else if( p_pic->i_refcount > 0 )
533 p_pic->i_status = DISPLAYED_PICTURE;
537 p_pic->i_status = DESTROYED_PICTURE;
538 p_vout->i_heap_size--;
541 vlc_mutex_unlock( &p_vout->picture_lock );
543 static void VoutFlushPicture( vout_thread_t *p_vout )
546 vlc_mutex_lock( &p_vout->picture_lock );
547 for( i = 0; i < p_vout->render.i_pictures; i++ )
549 picture_t *p_pic = p_vout->render.pp_picture[i];
551 if( p_pic->i_status == READY_PICTURE ||
552 p_pic->i_status == DISPLAYED_PICTURE )
554 /* We cannot change picture status if it is in READY_PICTURE state,
555 * Just make sure they won't be displayed */
559 vlc_mutex_unlock( &p_vout->picture_lock );
561 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
563 input_thread_t *p_input = p_dec->p_owner->p_input;
566 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
568 vlc_mutex_lock( &p_input->p->counters.counters_lock );
569 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
570 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
572 if( p_pic->date < p_dec->p_owner->i_preroll_end )
574 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
578 if( p_dec->p_owner->i_preroll_end > 0 )
580 msg_Dbg( p_dec, "End of video preroll" );
581 if( p_dec->p_owner->p_vout )
582 VoutFlushPicture( p_dec->p_owner->p_vout );
584 p_dec->p_owner->i_preroll_end = -1;
587 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
589 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
596 * \param p_dec the decoder object
597 * \param p_block the block to decode
598 * \return VLC_SUCCESS or an error code
600 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
602 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
603 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
605 if( p_block && p_block->i_buffer <= 0 )
607 block_Release( p_block );
611 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
613 block_t *p_sout_block;
615 while( ( p_sout_block =
616 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
618 if( !p_dec->p_owner->p_sout_input )
620 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
622 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
623 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
624 if( p_dec->fmt_in.psz_language )
626 if( p_dec->p_owner->sout.psz_language )
627 free( p_dec->p_owner->sout.psz_language );
628 p_dec->p_owner->sout.psz_language =
629 strdup( p_dec->fmt_in.psz_language );
632 p_dec->p_owner->p_sout_input =
633 sout_InputNew( p_dec->p_owner->p_sout,
634 &p_dec->p_owner->sout );
636 if( p_dec->p_owner->p_sout_input == NULL )
638 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
639 (char *)&p_dec->p_owner->sout.i_codec );
640 p_dec->b_error = VLC_TRUE;
642 while( p_sout_block )
644 block_t *p_next = p_sout_block->p_next;
645 block_Release( p_sout_block );
646 p_sout_block = p_next;
652 while( p_sout_block )
654 block_t *p_next = p_sout_block->p_next;
656 p_sout_block->p_next = NULL;
657 p_sout_block->i_rate = i_rate;
659 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
662 p_sout_block = p_next;
665 /* For now it's enough, as only sout inpact on this flag */
666 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
667 p_dec->p_owner->p_input->p->b_out_pace_control )
669 msg_Dbg( p_dec, "switching to sync mode" );
670 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
672 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
673 !p_dec->p_owner->p_input->p->b_out_pace_control )
675 msg_Dbg( p_dec, "switching to async mode" );
676 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
680 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
683 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
685 if( p_dec->p_owner->p_packetizer )
687 block_t *p_packetized_block;
688 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
690 while( (p_packetized_block =
691 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
693 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
695 es_format_Clean( &p_dec->fmt_in );
696 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
699 while( p_packetized_block )
701 block_t *p_next = p_packetized_block->p_next;
702 p_packetized_block->p_next = NULL;
703 p_packetized_block->i_rate = i_rate;
705 DecoderDecodeAudio( p_dec, p_packetized_block );
707 p_packetized_block = p_next;
713 DecoderDecodeAudio( p_dec, p_block );
716 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
719 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
721 if( p_dec->p_owner->p_packetizer )
723 block_t *p_packetized_block;
724 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
726 while( (p_packetized_block =
727 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
729 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
731 es_format_Clean( &p_dec->fmt_in );
732 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
735 while( p_packetized_block )
737 block_t *p_next = p_packetized_block->p_next;
738 p_packetized_block->p_next = NULL;
739 p_packetized_block->i_rate = i_rate;
741 DecoderDecodeVideo( p_dec, p_packetized_block );
743 p_packetized_block = p_next;
749 DecoderDecodeVideo( p_dec, p_block );
752 else if( p_dec->fmt_in.i_cat == SPU_ES )
754 input_thread_t *p_input = p_dec->p_owner->p_input;
755 vout_thread_t *p_vout;
759 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
761 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
763 vlc_mutex_lock( &p_input->p->counters.counters_lock );
764 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
765 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
767 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
768 if( p_vout && p_sys->p_spu_vout == p_vout )
770 /* Prerool does not work very well with subtitle */
771 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
772 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
773 spu_DestroySubpicture( p_vout->p_spu, p_spu );
775 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
779 msg_Warn( p_dec, "no vout found, leaking subpicture" );
782 vlc_object_release( p_vout );
787 msg_Err( p_dec, "unknown ES format" );
791 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
795 * Destroys a decoder object
797 * \param p_dec the decoder object
800 static void DeleteDecoder( decoder_t * p_dec )
802 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
803 (char*)&p_dec->fmt_in.i_codec,
804 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
806 /* Free all packets still in the decoder fifo. */
807 block_FifoEmpty( p_dec->p_owner->p_fifo );
808 block_FifoRelease( p_dec->p_owner->p_fifo );
811 if( p_dec->p_owner->p_aout_input )
812 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
814 if( p_dec->p_owner->p_vout )
818 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
819 /* Hack to make sure all the the pictures are freed by the decoder */
820 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
823 if( p_pic->i_status == RESERVED_PICTURE )
824 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
825 if( p_pic->i_refcount > 0 )
826 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
830 /* We are about to die. Reattach video output to p_vlc. */
831 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
834 if( p_dec->p_owner->p_sout_input )
836 sout_InputDelete( p_dec->p_owner->p_sout_input );
837 es_format_Clean( &p_dec->p_owner->sout );
840 if( p_dec->fmt_in.i_cat == SPU_ES )
842 vout_thread_t *p_vout;
844 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
847 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
848 p_dec->p_owner->i_spu_channel );
849 vlc_object_release( p_vout );
853 es_format_Clean( &p_dec->fmt_in );
854 es_format_Clean( &p_dec->fmt_out );
856 if( p_dec->p_owner->p_packetizer )
858 module_Unneed( p_dec->p_owner->p_packetizer,
859 p_dec->p_owner->p_packetizer->p_module );
860 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
861 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
862 vlc_object_detach( p_dec->p_owner->p_packetizer );
863 vlc_object_destroy( p_dec->p_owner->p_packetizer );
866 vlc_object_detach( p_dec );
868 free( p_dec->p_owner );
871 /*****************************************************************************
872 * Buffers allocation callbacks for the decoders
873 *****************************************************************************/
874 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
876 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
877 aout_buffer_t *p_buffer;
879 if( p_sys->p_aout_input != NULL &&
880 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
881 p_dec->fmt_out.audio.i_original_channels !=
882 p_sys->audio.i_original_channels ||
883 p_dec->fmt_out.audio.i_bytes_per_frame !=
884 p_sys->audio.i_bytes_per_frame ) )
886 /* Parameters changed, restart the aout */
887 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
888 p_sys->p_aout_input = NULL;
891 if( p_sys->p_aout_input == NULL )
893 audio_sample_format_t format;
894 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
896 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
897 p_sys->audio = p_dec->fmt_out.audio;
899 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
900 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
901 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
903 if ( i_force_dolby == 1 )
905 format.i_original_channels = format.i_original_channels |
906 AOUT_CHAN_DOLBYSTEREO;
908 else /* i_force_dolby == 2 */
910 format.i_original_channels = format.i_original_channels &
911 ~AOUT_CHAN_DOLBYSTEREO;
915 p_sys->p_aout_input =
916 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
917 if( p_sys->p_aout_input == NULL )
919 msg_Err( p_dec, "failed to create audio output" );
920 p_dec->b_error = VLC_TRUE;
923 p_dec->fmt_out.audio.i_bytes_per_frame =
924 p_sys->audio.i_bytes_per_frame;
927 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
933 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
935 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
936 p_dec->p_owner->p_aout_input, p_buffer );
939 static picture_t *vout_new_buffer( decoder_t *p_dec )
941 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
944 if( p_sys->p_vout == NULL ||
945 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
946 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
947 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
948 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
950 if( !p_dec->fmt_out.video.i_width ||
951 !p_dec->fmt_out.video.i_height )
953 /* Can't create a new vout without display size */
957 if( !p_dec->fmt_out.video.i_visible_width ||
958 !p_dec->fmt_out.video.i_visible_height )
960 if( p_dec->fmt_in.video.i_visible_width &&
961 p_dec->fmt_in.video.i_visible_height )
963 p_dec->fmt_out.video.i_visible_width =
964 p_dec->fmt_in.video.i_visible_width;
965 p_dec->fmt_out.video.i_visible_height =
966 p_dec->fmt_in.video.i_visible_height;
970 p_dec->fmt_out.video.i_visible_width =
971 p_dec->fmt_out.video.i_width;
972 p_dec->fmt_out.video.i_visible_height =
973 p_dec->fmt_out.video.i_height;
977 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
978 var_CreateGetBool( p_dec, "hdtv-fix" ) )
980 p_dec->fmt_out.video.i_visible_height = 1080;
981 p_dec->fmt_out.video.i_sar_num *= 135;
982 p_dec->fmt_out.video.i_sar_den *= 136;
983 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
986 if( !p_dec->fmt_out.video.i_sar_num ||
987 !p_dec->fmt_out.video.i_sar_den )
989 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
990 p_dec->fmt_out.video.i_visible_height;
992 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
993 p_dec->fmt_out.video.i_visible_width;
996 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
997 &p_dec->fmt_out.video.i_sar_den,
998 p_dec->fmt_out.video.i_sar_num,
999 p_dec->fmt_out.video.i_sar_den, 50000 );
1001 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1002 p_sys->video = p_dec->fmt_out.video;
1004 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1005 &p_dec->fmt_out.video );
1006 if( p_sys->p_vout == NULL )
1008 msg_Err( p_dec, "failed to create video output" );
1009 p_dec->b_error = VLC_TRUE;
1013 if( p_sys->video.i_rmask )
1014 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1015 if( p_sys->video.i_gmask )
1016 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1017 if( p_sys->video.i_bmask )
1018 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1021 /* Get a new picture */
1022 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1024 int i_pic, i_ready_pic = 0;
1026 if( p_dec->b_die || p_dec->b_error )
1031 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1032 /* Check the decoder doesn't leak pictures */
1033 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1036 if( p_pic->i_status == READY_PICTURE )
1038 if( i_ready_pic++ > 0 ) break;
1042 if( p_pic->i_status != DISPLAYED_PICTURE &&
1043 p_pic->i_status != RESERVED_PICTURE &&
1044 p_pic->i_status != READY_PICTURE ) break;
1046 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1049 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1051 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1053 /* Just free all the pictures */
1054 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1057 if( p_pic->i_status == RESERVED_PICTURE )
1058 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1059 if( p_pic->i_refcount > 0 )
1060 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1065 msleep( VOUT_OUTMEM_SLEEP );
1071 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1073 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1076 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1078 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1081 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1083 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1086 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1088 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1089 vout_thread_t *p_vout = NULL;
1090 subpicture_t *p_subpic;
1091 int i_attempts = 30;
1093 while( i_attempts-- )
1095 if( p_dec->b_die || p_dec->b_error ) break;
1097 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1100 msleep( VOUT_DISPLAY_DELAY );
1105 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1109 if( p_sys->p_spu_vout != p_vout )
1111 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1112 &p_sys->i_spu_channel );
1113 p_sys->p_spu_vout = p_vout;
1116 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1119 p_subpic->i_channel = p_sys->i_spu_channel;
1122 vlc_object_release( p_vout );
1127 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1129 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1130 vout_thread_t *p_vout = NULL;
1132 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1133 if( !p_vout || p_sys->p_spu_vout != p_vout )
1136 vlc_object_release( p_vout );
1137 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1141 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1143 vlc_object_release( p_vout );