1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
5 * $Id: es_out.c,v 1.24 2004/01/31 20:02:26 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
33 #include "vlc_playlist.h"
37 /*****************************************************************************
39 *****************************************************************************/
43 es_descriptor_t *p_es;
48 input_thread_t *p_input;
71 es_out_id_t *p_es_audio;
72 es_out_id_t *p_es_video;
73 es_out_id_t *p_es_sub;
76 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
77 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
78 static void EsOutDel ( es_out_t *, es_out_id_t * );
79 static int EsOutControl( es_out_t *, int i_query, va_list );
81 static char *LanguageGetName( const char *psz_code );
84 * Create a new es_out structure
86 * \param p_input The related input thread
87 * \return the new es_out_t
89 es_out_t *input_EsOutNew( input_thread_t *p_input )
91 es_out_t *out = malloc( sizeof( es_out_t ) );
92 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
95 out->pf_add = EsOutAdd;
96 out->pf_send = EsOutSend;
97 out->pf_del = EsOutDel;
98 out->pf_control = EsOutControl;
101 p_sys->p_input = p_input;
102 p_sys->b_pcr_set = VLC_FALSE;
104 p_sys->b_active = VLC_FALSE;
105 p_sys->i_mode = ES_OUT_MODE_AUTO;
116 var_Get( p_input, "audio-channel", &val );
117 p_sys->i_audio_last = val.i_int;
119 var_Get( p_input, "spu-channel", &val );
120 p_sys->i_sub_last = val.i_int;
122 p_sys->p_es_audio = NULL;
123 p_sys->p_es_video = NULL;
124 p_sys->p_es_sub = NULL;
130 * Deletes an es_out structure
132 * \param out the es_out structure to destroy
135 void input_EsOutDelete( es_out_t *out )
137 es_out_sys_t *p_sys = out->p_sys;
140 for( i = 0; i < p_sys->i_es; i++ )
142 free( p_sys->es[i] );
155 * \param out the es_out
157 * \return a program descriptor for the new program
159 static pgrm_descriptor_t *EsOutAddProgram( es_out_t *out, int i_group )
161 input_thread_t *p_input = out->p_sys->p_input;
162 pgrm_descriptor_t *p_prgm;
163 es_descriptor_t *p_pmt;
165 /* FIXME we should use a object variable but a lot of place in src/input
166 * have to be changed */
167 int i_select = config_GetInt( p_input, "program" );
170 p_prgm = input_AddProgram( p_input, i_group, 0 );
172 /* XXX welcome to kludge, add a dummy es, if you want to understand
173 * why have a look at input_SetProgram. Basicaly, it assume the first
174 * es to be the PMT, how that is stupid, nevertheless it is needed for
175 * the old ts demuxer */
176 p_pmt = input_AddES( p_input, p_prgm, 0, UNKNOWN_ES, NULL, 0 );
177 p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
179 /* Select i_select or the first by default */
180 if( p_input->stream.p_selected_program == NULL &&
181 ( i_select <= 0 || i_select == i_group ) )
183 p_input->stream.p_selected_program = p_prgm;
190 * Select an ES given the current mode
191 * XXX: you need to take a the lock before (stream.stream_lock)
193 * \param out The es_out structure
194 * \param es es_out_id structure
198 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
200 es_out_sys_t *p_sys = out->p_sys;
201 input_thread_t *p_input = p_sys->p_input;
203 int i_cat = es->p_es->i_cat;
205 if( !p_sys->b_active ||
206 ( !b_force && es->p_es->fmt.i_priority < 0 ) )
211 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
213 if( !es->p_es->p_dec )
215 input_SelectES( p_input, es->p_es );
218 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
222 if( es->p_es->p_pgrm != NULL &&
223 es->p_es->p_pgrm != p_input->stream.p_selected_program )
228 if( i_cat == AUDIO_ES )
230 if( p_sys->p_es_audio &&
231 p_sys->p_es_audio->p_es->fmt.i_priority >=
232 es->p_es->fmt.i_priority )
236 i_wanted = p_sys->i_audio_last >= 0 ?
237 p_sys->i_audio_last : es->i_channel;
239 else if( i_cat == SPU_ES )
241 if( p_sys->p_es_sub &&
242 p_sys->p_es_sub->p_es->fmt.i_priority >=
243 es->p_es->fmt.i_priority )
247 i_wanted = p_sys->i_sub_last;
249 else if( i_cat == VIDEO_ES )
251 i_wanted = es->i_channel;
254 if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
256 input_SelectES( p_input, es->p_es );
260 /* FIXME TODO handle priority here */
261 if( es->p_es->p_dec )
263 if( i_cat == AUDIO_ES )
265 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
266 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
268 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
270 p_sys->p_es_audio = es;
272 else if( i_cat == SPU_ES )
274 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
275 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
277 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
279 p_sys->p_es_sub = es;
281 else if( i_cat == VIDEO_ES )
283 p_sys->p_es_video = es;
291 * \param out the es_out to add
292 * \param fmt the es_format of the es_out
293 * \return an es_out id
295 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
297 es_out_sys_t *p_sys = out->p_sys;
298 playlist_t *p_playlist = NULL;
299 input_thread_t *p_input = p_sys->p_input;
300 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
301 pgrm_descriptor_t *p_prgm = NULL;
302 char psz_cat[sizeof( _("Stream ") ) + 10];
303 input_info_category_t *p_cat;
304 char *psz_description;
306 vlc_mutex_lock( &p_input->stream.stream_lock );
307 if( fmt->i_group >= 0 )
310 p_prgm = input_FindProgram( p_input, fmt->i_group );
315 p_prgm = EsOutAddProgram( out, fmt->i_group );
320 fmt->i_id = out->p_sys->i_id - 1;
323 psz_description = LanguageGetName( fmt->psz_language );
324 es->p_es = input_AddES( p_input,
328 psz_description, 0 );
329 es->p_es->i_stream_id = fmt->i_id;
330 es->p_es->i_fourcc = fmt->i_codec;
337 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
339 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
340 p_wf->nChannels = fmt->audio.i_channels;
341 p_wf->nSamplesPerSec = fmt->audio.i_rate;
342 p_wf->nAvgBytesPerSec = fmt->i_bitrate / 8;
343 p_wf->nBlockAlign = fmt->audio.i_blockalign;
344 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
345 p_wf->cbSize = fmt->i_extra;
346 if( fmt->i_extra > 0 )
348 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
350 es->p_es->p_waveformatex = p_wf;
352 es->i_channel = p_sys->i_audio;
357 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
359 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
360 p_bih->biWidth = fmt->video.i_width;
361 p_bih->biHeight = fmt->video.i_height;
363 p_bih->biBitCount = 24;
364 p_bih->biCompression = fmt->i_codec;
365 p_bih->biSizeImage = fmt->video.i_width *
367 p_bih->biXPelsPerMeter = 0;
368 p_bih->biYPelsPerMeter = 0;
369 p_bih->biClrUsed = 0;
370 p_bih->biClrImportant = 0;
372 if( fmt->i_extra > 0 )
374 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
376 es->p_es->p_bitmapinfoheader = p_bih;
378 es->i_channel = p_sys->i_video;
383 subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
384 memset( p_sub, 0, sizeof( subtitle_data_t ) );
385 if( fmt->i_extra > 0 )
387 p_sub->psz_header = malloc( fmt->i_extra + 1 );
388 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
389 /* just to be sure */
390 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
392 /* FIXME beuuuuuurk */
393 es->p_es->p_demux_data = p_sub;
395 es->i_channel = p_sys->i_sub;
404 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
405 /* Get a category and the playlist */
406 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) &&
407 ( p_playlist = (playlist_t *)vlc_object_find( p_input,
408 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ) ) )
410 /* Add information */
414 if( fmt->psz_description )
416 input_AddInfo( p_cat, _("Description"), "%s",
417 fmt->psz_description );
418 playlist_AddInfo( p_playlist, -1, psz_cat,
419 _("Description"), "%s",
420 fmt->psz_description );
422 input_AddInfo( p_cat, _("Codec"), "%.4s",
423 (char*)&fmt->i_codec );
424 playlist_AddInfo( p_playlist, -1, psz_cat,
425 _("Codec"),"%.4s",(char*)&fmt->i_codec );
427 input_AddInfo( p_cat, _("Type"), _("Audio") );
428 playlist_AddInfo( p_playlist, -1, psz_cat,
429 _("Type"), _("Audio") );
431 if( fmt->audio.i_channels > 0 )
433 input_AddInfo( p_cat, _("Channels"), "%d",
434 fmt->audio.i_channels );
435 playlist_AddInfo( p_playlist, -1, psz_cat,
436 _("Channels"), "%d", fmt->audio.i_channels );
438 if( fmt->audio.i_rate > 0 )
440 input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
442 playlist_AddInfo( p_playlist, -1, psz_cat,
443 _("Sample rate"), _("%d Hz"),
446 if( fmt->i_bitrate > 0 )
448 input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
450 playlist_AddInfo( p_playlist, -1, psz_cat,
451 _("Bitrate"), _("%d bps"),
454 if( fmt->audio.i_bitspersample )
456 input_AddInfo( p_cat, _("Bits per sample"), "%d",
457 fmt->audio.i_bitspersample );
458 playlist_AddInfo( p_playlist, -1, psz_cat,
459 _("Bits per sample"), "%d",
460 fmt->audio.i_bitspersample );
464 if( fmt->psz_description )
466 input_AddInfo( p_cat, _("Description"), "%s",
467 fmt->psz_description );
468 playlist_AddInfo( p_playlist, -1, psz_cat,
469 _("Description"), "%s",
470 fmt->psz_description );
472 input_AddInfo( p_cat, _("Type"), _("Video") );
473 playlist_AddInfo( p_playlist, -1, psz_cat,
474 _("Type"), _("Video") );
476 input_AddInfo( p_cat, _("Codec"), "%.4s",
477 (char*)&fmt->i_codec );
478 playlist_AddInfo( p_playlist, -1, psz_cat,
480 (char*)&fmt->i_codec );
482 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
484 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
485 fmt->video.i_width, fmt->video.i_height );
486 playlist_AddInfo( p_playlist, -1, psz_cat,
487 _("Resolution"), "%dx%d",
488 fmt->video.i_width, fmt->video.i_height );
490 if( fmt->video.i_visible_width > 0 &&
491 fmt->video.i_visible_height > 0 )
493 input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
494 fmt->video.i_visible_width,
495 fmt->video.i_visible_height);
496 playlist_AddInfo( p_playlist, -1, psz_cat,
497 _("Display resolution"), "%dx%d",
498 fmt->video.i_visible_width,
499 fmt->video.i_visible_height);
503 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
504 playlist_AddInfo( p_playlist, -1, psz_cat,
505 _("Type"), _("Subtitle") );
506 input_AddInfo( p_cat, _("Codec"), "%.4s",
507 (char*)&fmt->i_codec );
508 playlist_AddInfo( p_playlist, -1, psz_cat,
510 (char*)&fmt->i_codec );
516 if( p_playlist ) vlc_object_release( p_playlist );
518 if( *psz_description )
520 input_AddInfo( p_cat, _("Language"), psz_description );
521 playlist_AddInfo( p_playlist, -1, psz_cat, _("Language"), "%s", psz_description );
523 free( psz_description );
525 es_format_Copy( &es->p_es->fmt, fmt );
528 * XXX change that when we do group too */
531 EsOutSelect( out, es, VLC_FALSE );
533 vlc_mutex_unlock( &p_input->stream.stream_lock );
535 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
536 p_sys->i_id++; /* always incremented */
554 * Send a block for the given es_out
556 * \param out the es_out to send from
557 * \param es the es_out_id
558 * \param p_block the data block to send
560 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
562 es_out_sys_t *p_sys = out->p_sys;
564 if( p_sys->b_pcr_set )
566 pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
567 input_thread_t *p_input = p_sys->p_input;
571 p_pgrm = p_sys->p_input->stream.p_selected_program;
574 if( p_block->i_dts > 0 && p_pgrm )
577 input_ClockGetTS( p_input, p_pgrm, p_block->i_dts * 9 / 100 );
579 if( p_block->i_pts > 0 && p_pgrm )
582 input_ClockGetTS( p_input, p_pgrm, p_block->i_pts * 9 / 100 );
586 vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
587 p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
588 if( es->p_es->p_dec &&
589 (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
591 input_DecodeBlock( es->p_es->p_dec, p_block );
595 block_Release( p_block );
597 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
602 /*****************************************************************************
604 *****************************************************************************/
605 static void EsOutDel( es_out_t *out, es_out_id_t *es )
607 es_out_sys_t *p_sys = out->p_sys;
609 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
611 switch( es->p_es->i_cat )
624 /* We don't try to reselect */
625 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
626 if( es->p_es->p_dec )
628 input_UnselectES( p_sys->p_input, es->p_es );
631 if( es->p_es->p_waveformatex )
633 free( es->p_es->p_waveformatex );
634 es->p_es->p_waveformatex = NULL;
636 if( es->p_es->p_bitmapinfoheader )
638 free( es->p_es->p_bitmapinfoheader );
639 es->p_es->p_bitmapinfoheader = NULL;
641 input_DelES( p_sys->p_input, es->p_es );
643 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
644 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
645 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
647 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
653 * Control query handler
655 * \param out the es_out to control
656 * \param i_query A es_out query as defined in include/ninput.h
657 * \param args a variable list of arguments for the query
658 * \return VLC_SUCCESS or an error code
660 static int EsOutControl( es_out_t *out, int i_query, va_list args )
662 es_out_sys_t *p_sys = out->p_sys;
670 case ES_OUT_SET_ES_STATE:
671 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
672 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
673 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
674 if( b && es->p_es->p_dec == NULL )
676 input_SelectES( p_sys->p_input, es->p_es );
677 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
678 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
680 else if( !b && es->p_es->p_dec )
682 input_UnselectES( p_sys->p_input, es->p_es );
683 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
686 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
689 case ES_OUT_GET_ES_STATE:
690 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
691 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
693 *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
696 case ES_OUT_SET_ACTIVE:
698 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
704 val.b_bool = VLC_TRUE;
705 var_Set( p_sys->p_input, "intf-change", val );
710 case ES_OUT_GET_ACTIVE:
711 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
712 *pb = p_sys->b_active;
715 case ES_OUT_SET_MODE:
716 i = (int) va_arg( args, int );
717 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
718 i == ES_OUT_MODE_AUTO )
724 /* Reapply policy mode */
725 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
726 for( i = 0; i < p_sys->i_es; i++ )
728 if( p_sys->es[i]->p_es->p_dec )
730 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
733 for( i = 0; i < p_sys->i_es; i++ )
735 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
737 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
739 val.b_bool = VLC_TRUE;
740 var_Set( p_sys->p_input, "intf-change", val );
746 case ES_OUT_GET_MODE:
747 pi = (int*) va_arg( args, int* );
752 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
755 for( i = 0; i < p_sys->i_es; i++ )
757 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
758 if( p_sys->es[i]->p_es->p_dec )
760 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
762 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
767 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
768 EsOutSelect( out, es, VLC_TRUE );
769 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
774 case ES_OUT_SET_GROUP_PCR:
776 pgrm_descriptor_t *p_prgm = NULL;
779 if( i_query == ES_OUT_SET_PCR )
781 p_prgm = p_sys->p_input->stream.p_selected_program;
785 int i_group = (int)va_arg( args, int );
786 p_prgm = input_FindProgram( p_sys->p_input, i_group );
789 /* we create the requested program */
790 p_prgm = EsOutAddProgram( out, i_group );
793 i_pcr = (int64_t)va_arg( args, int64_t );
798 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr * 9 / 100);
800 p_sys->b_pcr_set = VLC_TRUE;
804 case ES_OUT_RESET_PCR:
805 for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
807 p_sys->p_input->stream.pp_programs[i]->i_synchro_state = SYNCHRO_REINIT;
809 p_sys->b_pcr_set = VLC_TRUE;
813 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
818 /****************************************************************************
819 * LanguageGetName: try to expend iso639 into plain name
820 ****************************************************************************/
821 static char *LanguageGetName( const char *psz_code )
823 const iso639_lang_t *pl;
825 if( psz_code == NULL )
830 if( strlen( psz_code ) == 2 )
832 pl = GetLang_1( psz_code );
834 else if( strlen( psz_code ) == 3 )
836 pl = GetLang_2B( psz_code );
837 if( !strcmp( pl->psz_iso639_1, "??" ) )
839 pl = GetLang_2T( psz_code );
844 return strdup( psz_code );
847 if( !strcmp( pl->psz_iso639_1, "??" ) )
849 return strdup( psz_code );
853 if( *pl->psz_native_name )
855 return strdup( pl->psz_native_name );
857 return strdup( pl->psz_eng_name );