1 /*****************************************************************************
2 * input_dec.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: input_dec.c,v 1.79 2003/11/24 23:22:01 gbazin Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <string.h> /* memcpy(), memset() */
32 #include <vlc/decoder.h>
35 #include "stream_output.h"
37 #include "input_ext-intf.h"
38 #include "input_ext-plugins.h"
42 static void input_NullPacket( input_thread_t *, es_descriptor_t * );
44 static decoder_t * CreateDecoder( input_thread_t *, es_descriptor_t *, int );
45 static int DecoderThread( decoder_t * );
46 static void DeleteDecoder( decoder_t * );
48 /* Buffers allocation callbacks for the decoders */
49 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
50 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
52 static picture_t *vout_new_buffer( decoder_t * );
53 static void vout_del_buffer( decoder_t *, picture_t * );
54 static void vout_link_picture( decoder_t *, picture_t * );
55 static void vout_unlink_picture( decoder_t *, picture_t * );
57 static es_format_t null_es_format = {0};
59 struct decoder_owner_sys_t
61 aout_instance_t *p_aout;
62 aout_input_t *p_aout_input;
64 vout_thread_t *p_vout;
66 sout_packetizer_input_t *p_sout;
68 /* Current format in use by the output */
77 input_buffers_t *p_method_data;
81 /*****************************************************************************
82 * input_RunDecoder: spawns a new decoder thread
83 *****************************************************************************/
84 decoder_t * input_RunDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
86 decoder_t *p_dec = NULL;
90 /* If we are in sout mode, search for packetizer module */
91 var_Get( p_input, "sout", &val );
92 if( !p_es->b_force_decoder && val.psz_string && *val.psz_string )
94 free( val.psz_string );
95 val.b_bool = VLC_TRUE;
97 if( p_es->i_cat == AUDIO_ES )
99 var_Get( p_input, "sout-audio", &val );
101 else if( p_es->i_cat == VIDEO_ES )
103 var_Get( p_input, "sout-video", &val );
108 /* Create the decoder configuration structure */
109 p_dec = CreateDecoder( p_input, p_es, VLC_OBJECT_PACKETIZER );
112 msg_Err( p_input, "could not create packetizer" );
117 module_Need( p_dec, "packetizer", "$packetizer" );
122 /* Create the decoder configuration structure */
123 p_dec = CreateDecoder( p_input, p_es, VLC_OBJECT_DECODER );
126 msg_Err( p_input, "could not create decoder" );
130 /* default Get a suitable decoder module */
131 p_dec->p_module = module_Need( p_dec, "decoder", "$codec" );
133 if( val.psz_string ) free( val.psz_string );
136 if( !p_dec || !p_dec->p_module )
138 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
139 "VLC probably does not support this sound or video format.",
140 (char*)&p_dec->fmt_in.i_codec );
142 DeleteDecoder( p_dec );
143 vlc_object_destroy( p_dec );
147 if ( p_es->i_cat == AUDIO_ES )
149 i_priority = VLC_THREAD_PRIORITY_AUDIO;
153 i_priority = VLC_THREAD_PRIORITY_VIDEO;
156 /* Spawn the decoder thread */
157 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
158 i_priority, VLC_FALSE ) )
160 msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
161 p_dec->p_module->psz_object_name );
162 module_Unneed( p_dec, p_dec->p_module );
163 DeleteDecoder( p_dec );
164 vlc_object_destroy( p_dec );
168 p_input->stream.b_changed = 1;
173 /*****************************************************************************
174 * input_EndDecoder: kills a decoder thread and waits until it's finished
175 *****************************************************************************/
176 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
178 decoder_t *p_dec = p_es->p_dec;
181 p_dec->b_die = VLC_TRUE;
183 /* Make sure the thread leaves the NextDataPacket() function by
184 * sending it a few null packets. */
185 for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
187 input_NullPacket( p_input, p_es );
190 if( p_es->p_pes != NULL )
192 input_DecodePES( p_es->p_dec, p_es->p_pes );
195 /* Waiting for the thread to exit */
196 /* I thought that unlocking was better since thread join can be long
197 * but it actually creates late pictures and freezes --stef */
198 /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
199 vlc_thread_join( p_dec );
200 /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
203 /* XXX We don't do it here because of dll loader that want close in the
204 * same thread than open/decode */
206 module_Unneed( p_dec, p_dec->p_module );
209 /* Delete decoder configuration */
210 DeleteDecoder( p_dec );
212 /* Delete the decoder */
213 vlc_object_destroy( p_dec );
215 /* Tell the input there is no more decoder */
218 p_input->stream.b_changed = 1;
221 /*****************************************************************************
223 *****************************************************************************
224 * Put a PES in the decoder's fifo.
225 *****************************************************************************/
226 void input_DecodePES( decoder_t * p_dec, pes_packet_t * p_pes )
228 data_packet_t *p_data;
231 for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
233 i_size += p_data->p_payload_end - p_data->p_payload_start;
237 block_t *p_block = block_New( p_dec, i_size );
240 uint8_t *p_buffer = p_block->p_buffer;
242 for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
244 int i_copy = p_data->p_payload_end - p_data->p_payload_start;
246 memcpy( p_buffer, p_data->p_payload_start, i_copy );
250 p_block->i_pts = p_pes->i_pts;
251 p_block->i_dts = p_pes->i_dts;
252 p_block->b_discontinuity = p_pes->b_discontinuity;
254 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
258 input_DeletePES( p_dec->p_owner->p_method_data, p_pes );
260 /*****************************************************************************
262 *****************************************************************************
263 * Put a block_t in the decoder's fifo.
264 *****************************************************************************/
265 void input_DecodeBlock( decoder_t * p_dec, block_t *p_block )
267 block_FifoPut( p_dec->p_owner->p_fifo, p_block );
270 /*****************************************************************************
271 * Create a NULL packet for padding in case of a data loss
272 *****************************************************************************/
273 static void input_NullPacket( input_thread_t * p_input,
274 es_descriptor_t * p_es )
276 block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
280 memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
281 p_block->b_discontinuity = 1;
283 block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
287 /*****************************************************************************
288 * input_EscapeDiscontinuity: send a NULL packet to the decoders
289 *****************************************************************************/
290 void input_EscapeDiscontinuity( input_thread_t * p_input )
292 unsigned int i_es, i;
294 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
296 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
298 if( p_es->p_dec != NULL )
300 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
302 input_NullPacket( p_input, p_es );
308 /*****************************************************************************
309 * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
310 *****************************************************************************/
311 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
313 unsigned int i_es, i;
315 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
317 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
319 if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
321 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
323 input_NullPacket( p_input, p_es );
329 /*****************************************************************************
330 * CreateDecoder: create a decoder object
331 *****************************************************************************/
332 static decoder_t * CreateDecoder( input_thread_t * p_input,
333 es_descriptor_t * p_es, int i_object_type )
337 p_dec = vlc_object_create( p_input, i_object_type );
340 msg_Err( p_input, "out of memory" );
344 p_dec->pf_decode_audio = 0;
345 p_dec->pf_decode_video = 0;
346 p_dec->pf_decode_sub = 0;
347 p_dec->pf_packetize = 0;
349 /* Select a new ES */
350 INSERT_ELEM( p_input->stream.pp_selected_es,
351 p_input->stream.i_selected_es_number,
352 p_input->stream.i_selected_es_number,
355 /* Initialize the decoder fifo */
356 p_dec->p_module = NULL;
358 p_dec->fmt_in = p_es->fmt;
360 if( p_es->p_waveformatex )
362 #define p_wf ((WAVEFORMATEX *)p_es->p_waveformatex)
363 p_dec->fmt_in.audio.i_channels = p_wf->nChannels;
364 p_dec->fmt_in.audio.i_rate = p_wf->nSamplesPerSec;
365 p_dec->fmt_in.i_bitrate = p_wf->nAvgBytesPerSec * 8;
366 p_dec->fmt_in.audio.i_blockalign = p_wf->nBlockAlign;
367 p_dec->fmt_in.audio.i_bitspersample = p_wf->wBitsPerSample;
368 p_dec->fmt_in.i_extra = p_wf->cbSize;
369 p_dec->fmt_in.p_extra = NULL;
372 p_dec->fmt_in.p_extra = malloc( p_wf->cbSize );
373 memcpy( p_dec->fmt_in.p_extra, &p_wf[1], p_wf->cbSize );
377 if( p_es->p_bitmapinfoheader )
379 #define p_bih ((BITMAPINFOHEADER *) p_es->p_bitmapinfoheader)
380 p_dec->fmt_in.i_extra = p_bih->biSize - sizeof(BITMAPINFOHEADER);
381 p_dec->fmt_in.p_extra = NULL;
382 if( p_dec->fmt_in.i_extra )
384 p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
385 memcpy( p_dec->fmt_in.p_extra, &p_bih[1], p_dec->fmt_in.i_extra );
388 p_dec->fmt_in.video.i_width = p_bih->biWidth;
389 p_dec->fmt_in.video.i_height = p_bih->biHeight;
394 * - 2: I'm not sure there isn't any endian problem here (spu)... */
395 if( p_es->i_cat == SPU_ES && p_es->p_demux_data )
397 if( ( p_es->i_fourcc == VLC_FOURCC( 's', 'p', 'u', ' ' ) ||
398 p_es->i_fourcc == VLC_FOURCC( 's', 'p', 'u', 'b' ) ) &&
399 *((uint32_t*)p_es->p_demux_data) == 0xBeef )
401 memcpy( p_dec->fmt_in.subs.spu.palette,
402 p_es->p_demux_data, 17 * 4 );
404 else if( p_es->i_fourcc == VLC_FOURCC( 'd', 'v', 'b', 's' ) )
406 dvb_spuinfo_t *p_dvbs = (dvb_spuinfo_t*)p_es->p_demux_data;
408 p_dec->fmt_in.subs.dvb.i_id = p_dvbs->i_id;
412 p_dec->fmt_in.i_cat = p_es->i_cat;
413 p_dec->fmt_in.i_codec = p_es->i_fourcc;
415 p_dec->fmt_out = null_es_format;
417 /* Allocate our private structure for the decoder */
418 p_dec->p_owner = (decoder_owner_sys_t*)malloc(sizeof(decoder_owner_sys_t));
419 if( p_dec->p_owner == NULL )
421 msg_Err( p_dec, "out of memory" );
424 p_dec->p_owner->p_aout = NULL;
425 p_dec->p_owner->p_aout_input = NULL;
426 p_dec->p_owner->p_vout = NULL;
427 p_dec->p_owner->p_sout = NULL;
429 if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
431 msg_Err( p_dec, "out of memory" );
434 p_dec->p_owner->p_method_data = p_input->p_method_data;
436 /* Set buffers allocation callbacks for the decoders */
437 p_dec->pf_aout_buffer_new = aout_new_buffer;
438 p_dec->pf_aout_buffer_del = aout_del_buffer;
439 p_dec->pf_vout_buffer_new = vout_new_buffer;
440 p_dec->pf_vout_buffer_del = vout_del_buffer;
441 p_dec->pf_picture_link = vout_link_picture;
442 p_dec->pf_picture_unlink = vout_unlink_picture;
444 vlc_object_attach( p_dec, p_input );
449 /*****************************************************************************
450 * DecoderThread: the decoding main loop
451 *****************************************************************************/
452 static int DecoderThread( decoder_t * p_dec )
456 /* The decoder's main loop */
457 while( !p_dec->b_die && !p_dec->b_error )
459 if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
465 if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
467 block_t *p_sout_block;
469 while( (p_sout_block = p_dec->pf_packetize( p_dec, &p_block )) )
471 if( !p_dec->p_owner->p_sout )
473 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
475 p_dec->p_owner->p_sout =
476 sout_InputNew( p_dec, &p_dec->p_owner->sout );
478 if( p_dec->p_owner->p_sout == NULL )
480 msg_Err( p_dec, "cannot create packetizer output" );
481 p_dec->b_error = VLC_TRUE;
483 while( p_sout_block )
485 block_t *p_next = p_sout_block->p_next;
486 block_Release( p_sout_block );
487 p_sout_block = p_next;
493 while( p_sout_block )
495 block_t *p_next = p_sout_block->p_next;
496 sout_buffer_t *p_sout_buffer;
499 sout_BufferNew( p_dec->p_owner->p_sout->p_sout,
500 p_sout_block->i_buffer );
501 if( p_sout_buffer == NULL )
503 msg_Err( p_dec, "cannot get sout buffer" );
507 memcpy( p_sout_buffer->p_buffer, p_sout_block->p_buffer,
508 p_sout_block->i_buffer );
510 p_sout_buffer->i_pts = p_sout_block->i_pts;
511 p_sout_buffer->i_dts = p_sout_block->i_dts;
512 p_sout_buffer->i_length = p_sout_block->i_length;
514 block_Release( p_sout_block );
516 sout_InputSendBuffer( p_dec->p_owner->p_sout, p_sout_buffer );
518 p_sout_block = p_next;
522 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
524 aout_buffer_t *p_aout_buf;
526 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
528 aout_DecPlay( p_dec->p_owner->p_aout,
529 p_dec->p_owner->p_aout_input, p_aout_buf );
532 else if( p_dec->fmt_in.i_cat == VIDEO_ES )
536 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
538 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
539 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
542 else if( p_dec->fmt_in.i_cat == SPU_ES )
544 p_dec->pf_decode_sub( p_dec, &p_block );
548 msg_Err( p_dec, "unknown ES format !!" );
554 while( !p_dec->b_die )
556 /* Trash all received PES packets */
557 p_block = block_FifoGet( p_dec->p_owner->p_fifo );
560 block_Release( p_block );
564 /* XXX We do it here because of dll loader that want close in the
565 * same thread than open/decode */
567 module_Unneed( p_dec, p_dec->p_module );
572 /*****************************************************************************
573 * DeleteDecoder: destroys a decoder object
574 *****************************************************************************/
575 static void DeleteDecoder( decoder_t * p_dec )
577 vlc_object_detach( p_dec );
580 "killing decoder fourcc `%4.4s', %d PES in FIFO",
581 (char*)&p_dec->fmt_in.i_codec,
582 p_dec->p_owner->p_fifo->i_depth );
584 /* Free all packets still in the decoder fifo. */
585 block_FifoEmpty( p_dec->p_owner->p_fifo );
586 block_FifoRelease( p_dec->p_owner->p_fifo );
589 if( p_dec->p_owner->p_aout_input )
590 aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
592 if( p_dec->p_owner->p_vout )
596 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
597 /* Hack to make sure all the the pictures are freed by the decoder */
598 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
601 if( p_pic->i_status == RESERVED_PICTURE )
602 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
603 if( p_pic->i_refcount > 0 )
604 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
608 /* We are about to die. Reattach video output to p_vlc. */
609 vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
612 if( p_dec->p_owner->p_sout )
613 sout_InputDelete( p_dec->p_owner->p_sout );
615 free( p_dec->p_owner );
618 /*****************************************************************************
619 * Buffers allocation callbacks for the decoders
620 *****************************************************************************/
621 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
623 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
624 aout_buffer_t *p_buffer;
626 if( p_sys->p_aout_input != NULL &&
627 ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
628 p_dec->fmt_out.audio.i_original_channels !=
629 p_sys->audio.i_original_channels ||
630 p_dec->fmt_out.audio.i_bytes_per_frame !=
631 p_sys->audio.i_bytes_per_frame ) )
633 /* Parameters changed, restart the aout */
634 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
635 p_sys->p_aout_input = NULL;
638 if( p_sys->p_aout_input == NULL )
640 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
641 p_sys->audio = p_dec->fmt_out.audio;
642 p_sys->p_aout_input =
643 aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
644 if( p_sys->p_aout_input == NULL )
646 msg_Err( p_dec, "failed to create audio output" );
647 p_dec->b_error = VLC_TRUE;
650 p_dec->fmt_out.audio.i_bytes_per_frame =
651 p_sys->audio.i_bytes_per_frame;
654 p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
660 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
662 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
663 p_dec->p_owner->p_aout_input, p_buffer );
666 static picture_t *vout_new_buffer( decoder_t *p_dec )
668 decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
671 if( p_sys->p_vout == NULL ||
672 p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
673 p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
674 p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
675 p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
677 if( !p_dec->fmt_out.video.i_width ||
678 !p_dec->fmt_out.video.i_height )
680 /* Can't create a new vout without display size */
684 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
685 p_sys->video = p_dec->fmt_out.video;
687 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
688 p_sys->video.i_width,
689 p_sys->video.i_height,
690 p_sys->video.i_chroma,
691 p_sys->video.i_aspect );
693 if( p_sys->p_vout == NULL )
695 msg_Err( p_dec, "failed to create video output" );
696 p_dec->b_error = VLC_TRUE;
701 /* Get a new picture */
702 while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
706 if( p_dec->b_die || p_dec->b_error )
711 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
712 /* Check the decoder doesn't leak pictures */
713 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
716 if( p_pic->i_status != DISPLAYED_PICTURE &&
717 p_pic->i_status != RESERVED_PICTURE ) break;
719 if( !p_pic->i_refcount ) break;
721 if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
723 msg_Err( p_dec, "decoder is leaking pictures, reseting the heap" );
725 /* Just free all the pictures */
726 for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
729 if( p_pic->i_status == RESERVED_PICTURE )
730 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
731 if( p_pic->i_refcount > 0 )
732 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
737 msleep( VOUT_OUTMEM_SLEEP );
743 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
745 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
748 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
750 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
753 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
755 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );