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 );
115 * Spawns a new decoder thread
117 * \param p_input the input thread
118 * \param p_es the es descriptor
119 * \return the spawned decoder object
121 decoder_t *input_DecoderNew( input_thread_t *p_input,
122 es_format_t *fmt, vlc_bool_t b_force_decoder )
124 decoder_t *p_dec = NULL;
127 /* If we are in sout mode, search for packetizer module */
128 if( p_input->p->p_sout && !b_force_decoder )
130 /* Create the decoder configuration structure */
131 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
134 msg_Err( p_input, "could not create packetizer" );
135 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
136 _("VLC could not open the packetizer module.") );
142 /* Create the decoder configuration structure */
143 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
146 msg_Err( p_input, "could not create decoder" );
147 intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"),
148 _("VLC could not open the decoder module.") );
153 if( !p_dec->p_module )
155 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
156 "VLC probably does not support this sound or video format.",
157 (char*)&p_dec->fmt_in.i_codec );
158 intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
159 "for format"), _("VLC probably does not support the \"%4.4s\" "
160 "audio or video format. Unfortunately there is no way for you "
161 "to fix this."), (char*)&p_dec->fmt_in.i_codec );
163 DeleteDecoder( p_dec );
164 vlc_object_destroy( p_dec );
168 if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
171 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
172 p_dec->p_owner->b_own_thread = VLC_FALSE;
176 var_Get( p_input, "minimize-threads", &val );
177 p_dec->p_owner->b_own_thread = !val.b_bool;
180 if( p_dec->p_owner->b_own_thread )
183 if( fmt->i_cat == AUDIO_ES )
184 i_priority = VLC_THREAD_PRIORITY_AUDIO;
186 i_priority = VLC_THREAD_PRIORITY_VIDEO;
188 /* Spawn the decoder thread */
189 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
190 i_priority, VLC_FALSE ) )
192 msg_Err( p_dec, "cannot spawn decoder thread" );
193 module_Unneed( p_dec, p_dec->p_module );
194 DeleteDecoder( p_dec );
195 vlc_object_destroy( p_dec );
204 * Kills a decoder thread and waits until it's finished
206 * \param p_input the input thread
207 * \param p_es the es descriptor
210 void input_DecoderDelete( decoder_t *p_dec )
212 vlc_object_kill( p_dec );
214 if( p_dec->p_owner->b_own_thread )
216 /* Make sure the thread leaves the function by
217 * sending it an empty block. */
218 block_t *p_block = block_New( p_dec, 0 );
219 input_DecoderDecode( p_dec, p_block );
221 vlc_thread_join( p_dec );
223 /* Don't module_Unneed() here because of the dll loader that wants
224 * close() in the same thread than open()/decode() */
229 input_DecoderDecode( p_dec, NULL );
231 module_Unneed( p_dec, p_dec->p_module );
234 /* Delete decoder configuration */
235 DeleteDecoder( p_dec );
237 /* Delete the decoder */
238 vlc_object_destroy( p_dec );
242 * Put a block_t in the decoder's fifo.
244 * \param p_dec the decoder object
245 * \param p_block the data block
247 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
249 if( p_dec->p_owner->b_own_thread )
251 if( p_dec->p_owner->p_input->p->b_out_pace_control )
254 while( !p_dec->b_die && !p_dec->b_error &&
255 block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
260 else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
262 /* FIXME: ideally we would check the time amount of data
263 * in the fifo instead of its size. */
264 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
265 "consumed quickly enough), resetting fifo!" );
266 block_FifoEmpty( p_dec->p_owner->p_fifo );
269 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
273 if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
275 if( p_block ) block_Release( p_block );
279 DecoderDecode( p_dec, p_block );
284 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
289 if( p_dec->p_owner->b_own_thread && b_flush )
290 block_FifoEmpty( p_dec->p_owner->p_fifo );
292 /* Send a special block */
293 p_null = block_New( p_dec, 128 );
294 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
295 /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
296 if( p_dec->p_owner->p_packetizer && b_flush )
297 p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
298 memset( p_null->p_buffer, 0, p_null->i_buffer );
300 input_DecoderDecode( p_dec, p_null );
303 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
305 if( p_dec->p_owner->b_own_thread
306 && block_FifoCount( p_dec->p_owner->p_fifo ) > 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 );
418 /* Copy ourself the input replay gain */
419 if( fmt->i_cat == AUDIO_ES )
422 for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
424 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
426 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
427 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
429 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
431 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
432 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
440 * The decoding main loop
442 * \param p_dec the decoder
445 static int DecoderThread( decoder_t * p_dec )
449 /* The decoder's main loop */
450 while( !p_dec->b_die && !p_dec->b_error )
452 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
457 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
463 while( !p_dec->b_die )
465 /* Trash all received PES packets */
466 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
467 if( p_block ) block_Release( p_block );
470 /* We do it here because of the dll loader that wants close() in the
471 * same thread than open()/decode() */
472 module_Unneed( p_dec, p_dec->p_module );
477 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
479 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
480 *pi_preroll = INT64_MAX;
481 else if( p->i_pts > 0 )
482 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
483 else if( p->i_dts > 0 )
484 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
486 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
488 input_thread_t *p_input = p_dec->p_owner->p_input;
489 const int i_rate = p_block->i_rate;
490 aout_buffer_t *p_aout_buf;
492 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
494 vlc_mutex_lock( &p_input->p->counters.counters_lock );
495 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
496 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
498 if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
500 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
501 p_dec->p_owner->p_aout_input, p_aout_buf );
505 if( p_dec->p_owner->i_preroll_end > 0 )
507 /* FIXME TODO flush audio output (don't know how to do that) */
508 msg_Dbg( p_dec, "End of audio preroll" );
509 p_dec->p_owner->i_preroll_end = -1;
511 aout_DecPlay( p_dec->p_owner->p_aout,
512 p_dec->p_owner->p_aout_input,
513 p_aout_buf, i_rate );
516 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
518 vlc_mutex_lock( &p_vout->picture_lock );
520 if( p_pic->i_status == READY_PICTURE )
522 /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
525 else if( p_pic->i_refcount > 0 )
527 p_pic->i_status = DISPLAYED_PICTURE;
531 p_pic->i_status = DESTROYED_PICTURE;
532 p_vout->i_heap_size--;
535 vlc_mutex_unlock( &p_vout->picture_lock );
537 static void VoutFlushPicture( vout_thread_t *p_vout )
540 vlc_mutex_lock( &p_vout->picture_lock );
541 for( i = 0; i < p_vout->render.i_pictures; i++ )
543 picture_t *p_pic = p_vout->render.pp_picture[i];
545 if( p_pic->i_status == READY_PICTURE ||
546 p_pic->i_status == DISPLAYED_PICTURE )
548 /* We cannot change picture status if it is in READY_PICTURE state,
549 * Just make sure they won't be displayed */
553 vlc_mutex_unlock( &p_vout->picture_lock );
555 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
557 input_thread_t *p_input = p_dec->p_owner->p_input;
560 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
562 vlc_mutex_lock( &p_input->p->counters.counters_lock );
563 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
564 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
566 if( p_pic->date < p_dec->p_owner->i_preroll_end )
568 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
572 if( p_dec->p_owner->i_preroll_end > 0 )
574 msg_Dbg( p_dec, "End of video preroll" );
575 if( p_dec->p_owner->p_vout )
576 VoutFlushPicture( p_dec->p_owner->p_vout );
578 p_dec->p_owner->i_preroll_end = -1;
581 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
583 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
590 * \param p_dec the decoder object
591 * \param p_block the block to decode
592 * \return VLC_SUCCESS or an error code
594 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
596 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
597 const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
599 if( p_block && p_block->i_buffer <= 0 )
601 block_Release( p_block );
605 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
607 block_t *p_sout_block;
609 while( ( p_sout_block =
610 p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
612 if( !p_dec->p_owner->p_sout_input )
614 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
616 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
617 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
618 if( p_dec->fmt_in.psz_language )
620 if( p_dec->p_owner->sout.psz_language )
621 free( p_dec->p_owner->sout.psz_language );
622 p_dec->p_owner->sout.psz_language =
623 strdup( p_dec->fmt_in.psz_language );
626 p_dec->p_owner->p_sout_input =
627 sout_InputNew( p_dec->p_owner->p_sout,
628 &p_dec->p_owner->sout );
630 if( p_dec->p_owner->p_sout_input == NULL )
632 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
633 (char *)&p_dec->p_owner->sout.i_codec );
634 p_dec->b_error = VLC_TRUE;
636 while( p_sout_block )
638 block_t *p_next = p_sout_block->p_next;
639 block_Release( p_sout_block );
640 p_sout_block = p_next;
646 while( p_sout_block )
648 block_t *p_next = p_sout_block->p_next;
650 p_sout_block->p_next = NULL;
651 p_sout_block->i_rate = i_rate;
653 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
656 p_sout_block = p_next;
659 /* For now it's enough, as only sout inpact on this flag */
660 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
661 p_dec->p_owner->p_input->p->b_out_pace_control )
663 msg_Dbg( p_dec, "switching to sync mode" );
664 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
666 else 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 async mode" );
670 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
674 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
676 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
678 if( p_dec->p_owner->p_packetizer )
680 block_t *p_packetized_block;
681 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
683 while( (p_packetized_block =
684 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
686 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
688 es_format_Clean( &p_dec->fmt_in );
689 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
692 while( p_packetized_block )
694 block_t *p_next = p_packetized_block->p_next;
695 p_packetized_block->p_next = NULL;
696 p_packetized_block->i_rate = i_rate;
698 DecoderDecodeAudio( p_dec, p_packetized_block );
700 p_packetized_block = p_next;
706 DecoderDecodeAudio( p_dec, p_block );
709 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
711 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
713 if( p_dec->p_owner->p_packetizer )
715 block_t *p_packetized_block;
716 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
718 while( (p_packetized_block =
719 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
721 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
723 es_format_Clean( &p_dec->fmt_in );
724 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
727 while( p_packetized_block )
729 block_t *p_next = p_packetized_block->p_next;
730 p_packetized_block->p_next = NULL;
731 p_packetized_block->i_rate = i_rate;
733 DecoderDecodeVideo( p_dec, p_packetized_block );
735 p_packetized_block = p_next;
741 DecoderDecodeVideo( p_dec, p_block );
744 else if( p_dec->fmt_in.i_cat == SPU_ES )
746 input_thread_t *p_input = p_dec->p_owner->p_input;
747 vout_thread_t *p_vout;
750 DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
752 while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
754 vlc_mutex_lock( &p_input->p->counters.counters_lock );
755 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
756 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
758 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
759 if( p_vout && p_sys->p_spu_vout == p_vout )
761 /* Prerool does not work very well with subtitle */
762 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
763 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
764 spu_DestroySubpicture( p_vout->p_spu, p_spu );
766 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
770 msg_Warn( p_dec, "no vout found, leaking subpicture" );
773 vlc_object_release( p_vout );
778 msg_Err( p_dec, "unknown ES format" );
782 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
786 * Destroys a decoder object
788 * \param p_dec the decoder object
791 static void DeleteDecoder( decoder_t * p_dec )
793 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
794 (char*)&p_dec->fmt_in.i_codec,
795 (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
797 /* Free all packets still in the decoder fifo. */
798 block_FifoEmpty( p_dec->p_owner->p_fifo );
799 block_FifoRelease( p_dec->p_owner->p_fifo );
802 if( p_dec->p_owner->p_aout_input )
803 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
805 if( p_dec->p_owner->p_vout )
809 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
810 /* Hack to make sure all the the pictures are freed by the decoder */
811 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
814 if( p_pic->i_status == RESERVED_PICTURE )
815 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
816 if( p_pic->i_refcount > 0 )
817 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
821 /* We are about to die. Reattach video output to p_vlc. */
822 vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
825 if( p_dec->p_owner->p_sout_input )
827 sout_InputDelete( p_dec->p_owner->p_sout_input );
828 es_format_Clean( &p_dec->p_owner->sout );
831 if( p_dec->fmt_in.i_cat == SPU_ES )
833 vout_thread_t *p_vout;
835 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
838 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
839 p_dec->p_owner->i_spu_channel );
840 vlc_object_release( p_vout );
844 es_format_Clean( &p_dec->fmt_in );
845 es_format_Clean( &p_dec->fmt_out );
847 if( p_dec->p_owner->p_packetizer )
849 module_Unneed( p_dec->p_owner->p_packetizer,
850 p_dec->p_owner->p_packetizer->p_module );
851 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
852 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
853 vlc_object_detach( p_dec->p_owner->p_packetizer );
854 vlc_object_destroy( p_dec->p_owner->p_packetizer );
857 vlc_object_detach( p_dec );
859 free( p_dec->p_owner );
862 /*****************************************************************************
863 * Buffers allocation callbacks for the decoders
864 *****************************************************************************/
865 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
867 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
868 aout_buffer_t *p_buffer;
870 if( p_sys->p_aout_input != NULL &&
871 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
872 p_dec->fmt_out.audio.i_original_channels !=
873 p_sys->audio.i_original_channels ||
874 p_dec->fmt_out.audio.i_bytes_per_frame !=
875 p_sys->audio.i_bytes_per_frame ) )
877 /* Parameters changed, restart the aout */
878 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
879 p_sys->p_aout_input = NULL;
882 if( p_sys->p_aout_input == NULL )
884 audio_sample_format_t format;
885 int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
887 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
888 p_sys->audio = p_dec->fmt_out.audio;
890 memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
891 if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
892 == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
894 if ( i_force_dolby == 1 )
896 format.i_original_channels = format.i_original_channels |
897 AOUT_CHAN_DOLBYSTEREO;
899 else /* i_force_dolby == 2 */
901 format.i_original_channels = format.i_original_channels &
902 ~AOUT_CHAN_DOLBYSTEREO;
906 p_sys->p_aout_input =
907 aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
908 if( p_sys->p_aout_input == NULL )
910 msg_Err( p_dec, "failed to create audio output" );
911 p_dec->b_error = VLC_TRUE;
914 p_dec->fmt_out.audio.i_bytes_per_frame =
915 p_sys->audio.i_bytes_per_frame;
918 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
924 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
926 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
927 p_dec->p_owner->p_aout_input, p_buffer );
930 static picture_t *vout_new_buffer( decoder_t *p_dec )
932 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
935 if( p_sys->p_vout == NULL ||
936 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
937 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
938 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
939 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
941 if( !p_dec->fmt_out.video.i_width ||
942 !p_dec->fmt_out.video.i_height )
944 /* Can't create a new vout without display size */
948 if( !p_dec->fmt_out.video.i_visible_width ||
949 !p_dec->fmt_out.video.i_visible_height )
951 if( p_dec->fmt_in.video.i_visible_width &&
952 p_dec->fmt_in.video.i_visible_height )
954 p_dec->fmt_out.video.i_visible_width =
955 p_dec->fmt_in.video.i_visible_width;
956 p_dec->fmt_out.video.i_visible_height =
957 p_dec->fmt_in.video.i_visible_height;
961 p_dec->fmt_out.video.i_visible_width =
962 p_dec->fmt_out.video.i_width;
963 p_dec->fmt_out.video.i_visible_height =
964 p_dec->fmt_out.video.i_height;
968 if( p_dec->fmt_out.video.i_visible_height == 1088 &&
969 var_CreateGetBool( p_dec, "hdtv-fix" ) )
971 p_dec->fmt_out.video.i_visible_height = 1080;
972 p_dec->fmt_out.video.i_sar_num *= 135;
973 p_dec->fmt_out.video.i_sar_den *= 136;
974 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
977 if( !p_dec->fmt_out.video.i_sar_num ||
978 !p_dec->fmt_out.video.i_sar_den )
980 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
981 p_dec->fmt_out.video.i_visible_height;
983 p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
984 p_dec->fmt_out.video.i_visible_width;
987 vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
988 &p_dec->fmt_out.video.i_sar_den,
989 p_dec->fmt_out.video.i_sar_num,
990 p_dec->fmt_out.video.i_sar_den, 50000 );
992 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
993 p_sys->video = p_dec->fmt_out.video;
995 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
996 &p_dec->fmt_out.video );
997 if( p_sys->p_vout == NULL )
999 msg_Err( p_dec, "failed to create video output" );
1000 p_dec->b_error = VLC_TRUE;
1004 if( p_sys->video.i_rmask )
1005 p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1006 if( p_sys->video.i_gmask )
1007 p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1008 if( p_sys->video.i_bmask )
1009 p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1012 /* Get a new picture */
1013 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1015 int i_pic, i_ready_pic = 0;
1017 if( p_dec->b_die || p_dec->b_error )
1022 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1023 /* Check the decoder doesn't leak pictures */
1024 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1027 if( p_pic->i_status == READY_PICTURE )
1029 if( i_ready_pic++ > 0 ) break;
1033 if( p_pic->i_status != DISPLAYED_PICTURE &&
1034 p_pic->i_status != RESERVED_PICTURE &&
1035 p_pic->i_status != READY_PICTURE ) break;
1037 if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1040 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1042 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1044 /* Just free all the pictures */
1045 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1048 if( p_pic->i_status == RESERVED_PICTURE )
1049 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1050 if( p_pic->i_refcount > 0 )
1051 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1056 msleep( VOUT_OUTMEM_SLEEP );
1062 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1064 VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1067 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1069 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1072 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1074 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1077 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1079 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1080 vout_thread_t *p_vout = NULL;
1081 subpicture_t *p_subpic;
1082 int i_attempts = 30;
1084 while( i_attempts-- )
1086 if( p_dec->b_die || p_dec->b_error ) break;
1088 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1091 msleep( VOUT_DISPLAY_DELAY );
1096 msg_Warn( p_dec, "no vout found, dropping subpicture" );
1100 if( p_sys->p_spu_vout != p_vout )
1102 spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1103 &p_sys->i_spu_channel );
1104 p_sys->p_spu_vout = p_vout;
1107 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1110 p_subpic->i_channel = p_sys->i_spu_channel;
1113 vlc_object_release( p_vout );
1118 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1120 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1121 vout_thread_t *p_vout = NULL;
1123 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1124 if( !p_vout || p_sys->p_spu_vout != p_vout )
1127 vlc_object_release( p_vout );
1128 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1132 spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1134 vlc_object_release( p_vout );