1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc/decoder.h>
34 #include <vlc/input.h>
36 #include "stream_output.h"
37 #include "input_internal.h"
39 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
40 static void DeleteDecoder( decoder_t * );
42 static int DecoderThread( decoder_t * );
43 static int DecoderDecode( decoder_t * p_dec, block_t *p_block );
45 /* Buffers allocation callbacks for the decoders */
46 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
47 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
49 static picture_t *vout_new_buffer( decoder_t * );
50 static void vout_del_buffer( decoder_t *, picture_t * );
51 static void vout_link_picture( decoder_t *, picture_t * );
52 static void vout_unlink_picture( decoder_t *, picture_t * );
54 static es_format_t null_es_format = {0};
56 struct decoder_owner_sys_t
58 vlc_bool_t b_own_thread;
60 input_thread_t *p_input;
62 aout_instance_t *p_aout;
63 aout_input_t *p_aout_input;
65 vout_thread_t *p_vout;
67 sout_instance_t *p_sout;
68 sout_packetizer_input_t *p_sout_input;
70 /* Some decoders require already packetized data (ie. not truncated) */
71 decoder_t *p_packetizer;
73 /* Current format in use by the output */
84 * Spawns a new decoder thread
86 * \param p_input the input thread
87 * \param p_es the es descriptor
88 * \return the spawned decoder object
90 decoder_t *input_DecoderNew( input_thread_t *p_input,
91 es_format_t *fmt, vlc_bool_t b_force_decoder )
93 decoder_t *p_dec = NULL;
96 /* If we are in sout mode, search for packetizer module */
97 if( p_input->p_sout && !b_force_decoder )
99 /* Create the decoder configuration structure */
100 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
103 msg_Err( p_input, "could not create packetizer" );
109 /* Create the decoder configuration structure */
110 p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
113 msg_Err( p_input, "could not create decoder" );
118 if( !p_dec->p_module )
120 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
121 "VLC probably does not support this sound or video format.",
122 (char*)&p_dec->fmt_in.i_codec );
124 DeleteDecoder( p_dec );
125 vlc_object_destroy( p_dec );
129 if( p_input->p_sout && p_input->input.b_can_pace_control &&
132 msg_Dbg( p_input, "stream out mode -> no decoder thread" );
133 p_dec->p_owner->b_own_thread = VLC_FALSE;
137 var_Get( p_input, "minimize-threads", &val );
138 p_dec->p_owner->b_own_thread = !val.b_bool;
141 if( p_dec->p_owner->b_own_thread )
144 if( fmt->i_cat == AUDIO_ES )
145 i_priority = VLC_THREAD_PRIORITY_AUDIO;
147 i_priority = VLC_THREAD_PRIORITY_VIDEO;
149 /* Spawn the decoder thread */
150 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
151 i_priority, VLC_FALSE ) )
153 msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
154 p_dec->p_module->psz_object_name );
155 module_Unneed( p_dec, p_dec->p_module );
156 DeleteDecoder( p_dec );
157 vlc_object_destroy( p_dec );
166 * Kills a decoder thread and waits until it's finished
168 * \param p_input the input thread
169 * \param p_es the es descriptor
172 void input_DecoderDelete( decoder_t *p_dec )
174 p_dec->b_die = VLC_TRUE;
176 if( p_dec->p_owner->b_own_thread )
178 /* Make sure the thread leaves the function by
179 * sending it an empty block. */
180 block_t *p_block = block_New( p_dec, 0 );
181 input_DecoderDecode( p_dec, p_block );
183 vlc_thread_join( p_dec );
185 /* Don't module_Unneed() here because of the dll loader that wants
186 * close() in the same thread than open()/decode() */
190 module_Unneed( p_dec, p_dec->p_module );
193 /* Delete decoder configuration */
194 DeleteDecoder( p_dec );
196 /* Delete the decoder */
197 vlc_object_destroy( p_dec );
201 * Put a block_t in the decoder's fifo.
203 * \param p_dec the decoder object
204 * \param p_block the data block
206 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
208 if( p_dec->p_owner->b_own_thread )
210 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
212 if( p_dec->p_owner->p_input->b_out_pace_control )
215 while( !p_dec->b_die && !p_dec->b_error &&
216 p_dec->p_owner->p_fifo->i_depth > 10 )
224 if( p_dec->b_error || p_block->i_buffer <= 0 )
226 block_Release( p_block );
230 DecoderDecode( p_dec, p_block );
235 void input_DecoderDiscontinuity( decoder_t * p_dec )
240 if( p_dec->p_owner->b_own_thread )
242 block_FifoEmpty( p_dec->p_owner->p_fifo );
245 /* Send a special block */
246 p_null = block_New( p_dec, 128 );
247 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
248 memset( p_null->p_buffer, 0, p_null->i_buffer );
250 input_DecoderDecode( p_dec, p_null );
255 * Create a NULL packet for padding in case of a data loss
257 * \param p_input the input thread
258 * \param p_es es descriptor
261 static void input_NullPacket( input_thread_t * p_input,
262 es_descriptor_t * p_es )
265 block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
268 memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
269 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
271 block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
277 * Send a NULL packet to the decoders
279 * \param p_input the input thread
282 void input_EscapeDiscontinuity( input_thread_t * p_input )
285 unsigned int i_es, i;
287 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
289 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
291 if( p_es->p_dec != NULL )
293 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
295 input_NullPacket( p_input, p_es );
303 * Send a NULL packet to the audio decoders
305 * \param p_input the input thread
308 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
311 unsigned int i_es, i;
313 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
315 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
317 if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
319 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
321 input_NullPacket( p_input, p_es );
330 * Create a decoder object
332 * \param p_input the input thread
333 * \param p_es the es descriptor
334 * \param i_object_type Object type as define in include/vlc_objects.h
335 * \return the decoder object
337 static decoder_t * CreateDecoder( input_thread_t *p_input,
338 es_format_t *fmt, int i_object_type )
342 p_dec = vlc_object_create( p_input, i_object_type );
345 msg_Err( p_input, "out of memory" );
349 p_dec->pf_decode_audio = 0;
350 p_dec->pf_decode_video = 0;
351 p_dec->pf_decode_sub = 0;
352 p_dec->pf_packetize = 0;
354 /* Initialize the decoder fifo */
355 p_dec->p_module = NULL;
358 es_format_Copy( &p_dec->fmt_in, fmt );
359 es_format_Copy( &p_dec->fmt_out, &null_es_format );
361 /* Allocate our private structure for the decoder */
362 p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
363 if( p_dec->p_owner == NULL )
365 msg_Err( p_dec, "out of memory" );
368 p_dec->p_owner->b_own_thread = VLC_TRUE;
369 p_dec->p_owner->p_input = p_input;
370 p_dec->p_owner->p_aout = NULL;
371 p_dec->p_owner->p_aout_input = NULL;
372 p_dec->p_owner->p_vout = NULL;
373 p_dec->p_owner->p_sout = p_input->p_sout;
374 p_dec->p_owner->p_sout_input = NULL;
375 p_dec->p_owner->p_packetizer = NULL;
379 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
381 msg_Err( p_dec, "out of memory" );
384 /* Set buffers allocation callbacks for the decoders */
385 p_dec->pf_aout_buffer_new = aout_new_buffer;
386 p_dec->pf_aout_buffer_del = aout_del_buffer;
387 p_dec->pf_vout_buffer_new = vout_new_buffer;
388 p_dec->pf_vout_buffer_del = vout_del_buffer;
389 p_dec->pf_picture_link = vout_link_picture;
390 p_dec->pf_picture_unlink = vout_unlink_picture;
392 vlc_object_attach( p_dec, p_input );
394 /* Find a suitable decoder/packetizer module */
395 if( i_object_type == VLC_OBJECT_DECODER )
396 p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
398 p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
400 /* Check if decoder requires already packetized data */
401 if( i_object_type == VLC_OBJECT_DECODER &&
402 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
404 p_dec->p_owner->p_packetizer =
405 vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
406 if( p_dec->p_owner->p_packetizer )
408 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
411 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
414 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
416 p_dec->p_owner->p_packetizer->p_module =
417 module_Need( p_dec->p_owner->p_packetizer,
418 "packetizer", "$packetizer", 0 );
420 if( !p_dec->p_owner->p_packetizer->p_module )
422 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
423 vlc_object_detach( p_dec->p_owner->p_packetizer );
424 vlc_object_destroy( p_dec->p_owner->p_packetizer );
433 * The decoding main loop
435 * \param p_dec the decoder
438 static int DecoderThread( decoder_t * p_dec )
442 /* The decoder's main loop */
443 while( !p_dec->b_die && !p_dec->b_error )
445 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
450 if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
456 while( !p_dec->b_die )
458 /* Trash all received PES packets */
459 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
460 if( p_block ) block_Release( p_block );
463 /* We do it here because of the dll loader that wants close() in the
464 * same thread than open()/decode() */
465 module_Unneed( p_dec, p_dec->p_module );
473 * \param p_dec the decoder object
474 * \param p_block the block to decode
475 * \return VLC_SUCCESS or an error code
477 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
479 if( p_block->i_buffer <= 0 )
481 block_Release( p_block );
485 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
487 block_t *p_sout_block;
489 while( (p_sout_block = p_dec->pf_packetize( p_dec, &p_block )) )
491 if( !p_dec->p_owner->p_sout_input )
493 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
495 p_dec->p_owner->sout.i_group =p_dec->fmt_in.i_group;
496 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
497 if( p_dec->fmt_in.psz_language )
499 p_dec->p_owner->sout.psz_language =
500 strdup( p_dec->fmt_in.psz_language );
503 p_dec->p_owner->p_sout_input =
504 sout_InputNew( p_dec->p_owner->p_sout,
505 &p_dec->p_owner->sout );
507 if( p_dec->p_owner->p_sout_input == NULL )
509 msg_Err( p_dec, "cannot create packetizer output" );
510 p_dec->b_error = VLC_TRUE;
512 while( p_sout_block )
514 block_t *p_next = p_sout_block->p_next;
515 block_Release( p_sout_block );
516 p_sout_block = p_next;
522 while( p_sout_block )
524 block_t *p_next = p_sout_block->p_next;
526 p_sout_block->p_next = NULL;
528 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
531 p_sout_block = p_next;
534 /* For now it's enough, as only sout inpact on this flag */
535 if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
536 p_dec->p_owner->p_input->b_out_pace_control )
538 msg_Dbg( p_dec, "switching to synch mode" );
539 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
541 else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
542 !p_dec->p_owner->p_input->b_out_pace_control )
544 msg_Dbg( p_dec, "switching to asynch mode" );
545 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
549 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
551 aout_buffer_t *p_aout_buf;
553 if( p_dec->p_owner->p_packetizer )
555 block_t *p_packetized_block;
556 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
558 while( (p_packetized_block =
559 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
562 p_dec->pf_decode_audio( p_dec, &p_packetized_block )) )
564 aout_DecPlay( p_dec->p_owner->p_aout,
565 p_dec->p_owner->p_aout_input, p_aout_buf );
569 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
571 aout_DecPlay( p_dec->p_owner->p_aout,
572 p_dec->p_owner->p_aout_input, p_aout_buf );
575 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
579 if( p_dec->p_owner->p_packetizer )
581 block_t *p_packetized_block;
582 decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
584 while( (p_packetized_block =
585 p_packetizer->pf_packetize( p_packetizer, &p_block )) )
588 p_dec->pf_decode_video( p_dec, &p_packetized_block )) )
590 vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
592 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
596 else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
598 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
599 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
602 else if( p_dec->fmt_in.i_cat == SPU_ES )
604 p_dec->pf_decode_sub( p_dec, &p_block );
608 msg_Err( p_dec, "unknown ES format" );
612 return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
616 * Destroys a decoder object
618 * \param p_dec the decoder object
621 static void DeleteDecoder( decoder_t * p_dec )
623 vlc_object_detach( p_dec );
625 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
626 (char*)&p_dec->fmt_in.i_codec,
627 p_dec->p_owner->p_fifo->i_depth );
629 /* Free all packets still in the decoder fifo. */
630 block_FifoEmpty( p_dec->p_owner->p_fifo );
631 block_FifoRelease( p_dec->p_owner->p_fifo );
634 if( p_dec->p_owner->p_aout_input )
635 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
637 if( p_dec->p_owner->p_vout )
641 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
642 /* Hack to make sure all the the pictures are freed by the decoder */
643 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
646 if( p_pic->i_status == RESERVED_PICTURE )
647 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
648 if( p_pic->i_refcount > 0 )
649 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
653 /* We are about to die. Reattach video output to p_vlc. */
654 vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
657 if( p_dec->p_owner->p_sout_input )
659 sout_InputDelete( p_dec->p_owner->p_sout_input );
660 es_format_Clean( &p_dec->p_owner->sout );
663 es_format_Clean( &p_dec->fmt_in );
664 es_format_Clean( &p_dec->fmt_out );
666 if( p_dec->p_owner->p_packetizer )
668 module_Unneed( p_dec->p_owner->p_packetizer,
669 p_dec->p_owner->p_packetizer->p_module );
670 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
671 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
672 vlc_object_detach( p_dec->p_owner->p_packetizer );
673 vlc_object_destroy( p_dec->p_owner->p_packetizer );
676 free( p_dec->p_owner );
679 /*****************************************************************************
680 * Buffers allocation callbacks for the decoders
681 *****************************************************************************/
682 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
684 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
685 aout_buffer_t *p_buffer;
687 if( p_sys->p_aout_input != NULL &&
688 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
689 p_dec->fmt_out.audio.i_original_channels !=
690 p_sys->audio.i_original_channels ||
691 p_dec->fmt_out.audio.i_bytes_per_frame !=
692 p_sys->audio.i_bytes_per_frame ) )
694 /* Parameters changed, restart the aout */
695 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
696 p_sys->p_aout_input = NULL;
699 if( p_sys->p_aout_input == NULL )
701 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
702 p_sys->audio = p_dec->fmt_out.audio;
703 p_sys->p_aout_input =
704 aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
705 if( p_sys->p_aout_input == NULL )
707 msg_Err( p_dec, "failed to create audio output" );
708 p_dec->b_error = VLC_TRUE;
711 p_dec->fmt_out.audio.i_bytes_per_frame =
712 p_sys->audio.i_bytes_per_frame;
715 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
721 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
723 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
724 p_dec->p_owner->p_aout_input, p_buffer );
727 static picture_t *vout_new_buffer( decoder_t *p_dec )
729 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
732 if( p_sys->p_vout == NULL ||
733 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
734 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
735 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
736 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
738 if( !p_dec->fmt_out.video.i_width ||
739 !p_dec->fmt_out.video.i_height )
741 /* Can't create a new vout without display size */
745 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
746 p_sys->video = p_dec->fmt_out.video;
748 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
749 p_sys->video.i_width,
750 p_sys->video.i_height,
751 p_sys->video.i_chroma,
752 p_sys->video.i_aspect );
754 if( p_sys->p_vout == NULL )
756 msg_Err( p_dec, "failed to create video output" );
757 p_dec->b_error = VLC_TRUE;
762 /* Get a new picture */
763 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
765 int i_pic, i_ready_pic = 0;
767 if( p_dec->b_die || p_dec->b_error )
772 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
773 /* Check the decoder doesn't leak pictures */
774 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
777 if( p_pic->i_status == READY_PICTURE && i_ready_pic++ > 0 ) break;
779 if( p_pic->i_status != DISPLAYED_PICTURE &&
780 p_pic->i_status != RESERVED_PICTURE &&
781 p_pic->i_status != READY_PICTURE ) break;
783 if( !p_pic->i_refcount ) break;
785 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
787 msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
789 /* Just free all the pictures */
790 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
793 if( p_pic->i_status == RESERVED_PICTURE )
794 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
795 if( p_pic->i_refcount > 0 )
796 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
801 msleep( VOUT_OUTMEM_SLEEP );
807 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
809 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
812 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
814 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
817 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
819 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );