1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
5 * $Id: es_out.c,v 1.21 2004/01/25 17:16:05 zorglub 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"
36 /*****************************************************************************
38 *****************************************************************************/
42 es_descriptor_t *p_es;
47 input_thread_t *p_input;
70 es_out_id_t *p_es_audio;
71 es_out_id_t *p_es_video;
72 es_out_id_t *p_es_sub;
75 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
76 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
77 static void EsOutDel ( es_out_t *, es_out_id_t * );
78 static int EsOutControl( es_out_t *, int i_query, va_list );
82 * Create a new es_out structure
84 * \param p_input The related input thread
85 * \return the new es_out_t
87 es_out_t *input_EsOutNew( input_thread_t *p_input )
89 es_out_t *out = malloc( sizeof( es_out_t ) );
90 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
93 out->pf_add = EsOutAdd;
94 out->pf_send = EsOutSend;
95 out->pf_del = EsOutDel;
96 out->pf_control = EsOutControl;
99 p_sys->p_input = p_input;
100 p_sys->b_pcr_set = VLC_FALSE;
102 p_sys->b_active = VLC_FALSE;
103 p_sys->i_mode = ES_OUT_MODE_AUTO;
114 var_Get( p_input, "audio-channel", &val );
115 p_sys->i_audio_last = val.i_int;
117 var_Get( p_input, "spu-channel", &val );
118 p_sys->i_sub_last = val.i_int;
120 p_sys->p_es_audio = NULL;
121 p_sys->p_es_video = NULL;
122 p_sys->p_es_sub = NULL;
128 * Deletes an es_out structure
130 * \param out the es_out structure to destroy
133 void input_EsOutDelete( es_out_t *out )
135 es_out_sys_t *p_sys = out->p_sys;
138 for( i = 0; i < p_sys->i_es; i++ )
140 free( p_sys->es[i] );
153 * \param out the es_out
155 * \return a program descriptor for the new program
157 static pgrm_descriptor_t *EsOutAddProgram( es_out_t *out, int i_group )
159 input_thread_t *p_input = out->p_sys->p_input;
160 pgrm_descriptor_t *p_prgm;
161 es_descriptor_t *p_pmt;
163 /* FIXME we should use a object variable but a lot of place in src/input
164 * have to be changed */
165 int i_select = config_GetInt( p_input, "program" );
168 p_prgm = input_AddProgram( p_input, i_group, 0 );
170 /* XXX welcome to kludge, add a dummy es, if you want to understand
171 * why have a look at input_SetProgram. Basicaly, it assume the first
172 * es to be the PMT, how that is stupid, nevertheless it is needed for
173 * the old ts demuxer */
174 p_pmt = input_AddES( p_input, p_prgm, 0, UNKNOWN_ES, NULL, 0 );
175 p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
177 /* Select i_select or the first by default */
178 if( p_input->stream.p_selected_program == NULL &&
179 ( i_select <= 0 || i_select == i_group ) )
181 p_input->stream.p_selected_program = p_prgm;
188 * Select an ES given the current mode
189 * XXX: you need to take a the lock before (stream.stream_lock)
191 * \param out The es_out structure
192 * \param es es_out_id structure
196 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
198 es_out_sys_t *p_sys = out->p_sys;
199 input_thread_t *p_input = p_sys->p_input;
201 int i_cat = es->p_es->i_cat;
203 if( !p_sys->b_active ||
204 ( !b_force && es->p_es->fmt.i_priority < 0 ) )
209 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
211 if( !es->p_es->p_dec )
213 input_SelectES( p_input, es->p_es );
216 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
220 if( es->p_es->p_pgrm != NULL &&
221 es->p_es->p_pgrm != p_input->stream.p_selected_program )
226 if( i_cat == AUDIO_ES )
228 if( p_sys->p_es_audio &&
229 p_sys->p_es_audio->p_es->fmt.i_priority >=
230 es->p_es->fmt.i_priority )
234 i_wanted = p_sys->i_audio_last >= 0 ?
235 p_sys->i_audio_last : es->i_channel;
237 else if( i_cat == SPU_ES )
239 if( p_sys->p_es_sub &&
240 p_sys->p_es_sub->p_es->fmt.i_priority >=
241 es->p_es->fmt.i_priority )
245 i_wanted = p_sys->i_sub_last;
247 else if( i_cat == VIDEO_ES )
249 i_wanted = es->i_channel;
252 if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
254 input_SelectES( p_input, es->p_es );
258 /* FIXME TODO handle priority here */
259 if( es->p_es->p_dec )
261 if( i_cat == AUDIO_ES )
263 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
264 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
266 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
268 p_sys->p_es_audio = es;
270 else if( i_cat == SPU_ES )
272 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
273 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
275 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
277 p_sys->p_es_sub = es;
279 else if( i_cat == VIDEO_ES )
281 p_sys->p_es_video = es;
289 * \param out the es_out to add
290 * \param fmt the es_format of the es_out
291 * \return an es_out id
293 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
295 es_out_sys_t *p_sys = out->p_sys;
296 playlist_t *p_playlist = NULL;
297 input_thread_t *p_input = p_sys->p_input;
298 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
299 pgrm_descriptor_t *p_prgm = NULL;
300 char psz_cat[sizeof( _("Stream ") ) + 10];
301 input_info_category_t *p_cat;
303 vlc_mutex_lock( &p_input->stream.stream_lock );
304 if( fmt->i_group >= 0 )
307 p_prgm = input_FindProgram( p_input, fmt->i_group );
312 p_prgm = EsOutAddProgram( out, fmt->i_group );
317 fmt->i_id = out->p_sys->i_id - 1;
320 es->p_es = input_AddES( p_input,
324 fmt->psz_language, 0 );
325 es->p_es->i_stream_id = fmt->i_id;
326 es->p_es->i_fourcc = fmt->i_codec;
333 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
335 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
336 p_wf->nChannels = fmt->audio.i_channels;
337 p_wf->nSamplesPerSec = fmt->audio.i_rate;
338 p_wf->nAvgBytesPerSec = fmt->i_bitrate / 8;
339 p_wf->nBlockAlign = fmt->audio.i_blockalign;
340 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
341 p_wf->cbSize = fmt->i_extra;
342 if( fmt->i_extra > 0 )
344 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
346 es->p_es->p_waveformatex = p_wf;
348 es->i_channel = p_sys->i_audio;
353 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
355 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
356 p_bih->biWidth = fmt->video.i_width;
357 p_bih->biHeight = fmt->video.i_height;
359 p_bih->biBitCount = 24;
360 p_bih->biCompression = fmt->i_codec;
361 p_bih->biSizeImage = fmt->video.i_width *
363 p_bih->biXPelsPerMeter = 0;
364 p_bih->biYPelsPerMeter = 0;
365 p_bih->biClrUsed = 0;
366 p_bih->biClrImportant = 0;
368 if( fmt->i_extra > 0 )
370 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
372 es->p_es->p_bitmapinfoheader = p_bih;
374 es->i_channel = p_sys->i_video;
379 subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
380 memset( p_sub, 0, sizeof( subtitle_data_t ) );
381 if( fmt->i_extra > 0 )
383 p_sub->psz_header = malloc( fmt->i_extra + 1 );
384 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
385 /* just to be sure */
386 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
388 /* FIXME beuuuuuurk */
389 es->p_es->p_demux_data = p_sub;
391 es->i_channel = p_sys->i_sub;
400 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
401 /* Get a category and the playlist */
402 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) &&
403 ( p_playlist = (playlist_t *)vlc_object_find( p_input,
404 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ) ) )
406 /* Add information */
410 if( fmt->psz_description )
412 input_AddInfo( p_cat, _("Description"), "%s",
413 fmt->psz_description );
414 playlist_AddInfo( p_playlist, -1, psz_cat,
415 _("Description"), "%s",
416 fmt->psz_description );
418 input_AddInfo( p_cat, _("Codec"), "%.4s",
419 (char*)&fmt->i_codec );
420 playlist_AddInfo( p_playlist, -1, psz_cat,
421 _("Codec"),"%.4s",(char*)&fmt->i_codec );
423 input_AddInfo( p_cat, _("Type"), _("Audio") );
424 playlist_AddInfo( p_playlist, -1, psz_cat,
425 _("Type"), _("Audio") );
427 if( fmt->audio.i_channels > 0 )
429 input_AddInfo( p_cat, _("Channels"), "%d",
430 fmt->audio.i_channels );
431 playlist_AddInfo( p_playlist, -1, psz_cat,
432 _("Channels"), "%d", fmt->audio.i_channels );
434 if( fmt->psz_language )
436 input_AddInfo( p_cat, _("Language"), "%s",
438 playlist_AddInfo( p_playlist, -1, psz_cat,
442 if( fmt->audio.i_rate > 0 )
444 input_AddInfo( p_cat, _("Sample rate"), _("%d Hz"),
446 playlist_AddInfo( p_playlist, -1, psz_cat,
447 _("Sample rate"), _("%d Hz"),
450 if( fmt->i_bitrate > 0 )
452 input_AddInfo( p_cat, _("Bitrate"), _("%d bps"),
454 playlist_AddInfo( p_playlist, -1, psz_cat,
455 _("Bitrate"), _("%d bps"),
458 if( fmt->audio.i_bitspersample )
460 input_AddInfo( p_cat, _("Bits per sample"), "%d",
461 fmt->audio.i_bitspersample );
462 playlist_AddInfo( p_playlist, -1, psz_cat,
463 _("Bits per sample"), "%d",
464 fmt->audio.i_bitspersample );
468 if( fmt->psz_description )
470 input_AddInfo( p_cat, _("Description"), "%s",
471 fmt->psz_description );
472 playlist_AddInfo( p_playlist, -1, psz_cat,
473 _("Description"), "%s",
474 fmt->psz_description );
476 input_AddInfo( p_cat, _("Type"), _("Video") );
477 playlist_AddInfo( p_playlist, -1, psz_cat,
478 _("Type"), _("Video") );
480 input_AddInfo( p_cat, _("Codec"), "%.4s",
481 (char*)&fmt->i_codec );
482 playlist_AddInfo( p_playlist, -1, psz_cat,
484 (char*)&fmt->i_codec );
486 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
488 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
489 fmt->video.i_width, fmt->video.i_height );
490 playlist_AddInfo( p_playlist, -1, psz_cat,
491 _("Resolution"), "%dx%d",
492 fmt->video.i_width, fmt->video.i_height );
494 if( fmt->video.i_visible_width > 0 &&
495 fmt->video.i_visible_height > 0 )
497 input_AddInfo( p_cat, _("Display resolution"), "%dx%d",
498 fmt->video.i_visible_width,
499 fmt->video.i_visible_height);
500 playlist_AddInfo( p_playlist, -1, psz_cat,
501 _("Display resolution"), "%dx%d",
502 fmt->video.i_visible_width,
503 fmt->video.i_visible_height);
507 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
508 playlist_AddInfo( p_playlist, -1, psz_cat,
509 _("Type"), _("Subtitle") );
510 input_AddInfo( p_cat, _("Codec"), "%.4s",
511 (char*)&fmt->i_codec );
512 playlist_AddInfo( p_playlist, -1, psz_cat,
514 (char*)&fmt->i_codec );
520 if( p_playlist ) vlc_object_release( p_playlist );
524 * XXX change that when we do group too */
527 EsOutSelect( out, es, VLC_FALSE );
530 vlc_mutex_unlock( &p_input->stream.stream_lock );
532 es->p_es->fmt = *fmt;
534 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
535 p_sys->i_id++; /* always incremented */
553 * Send a block for the given es_out
555 * \param out the es_out to send from
556 * \param es the es_out_id
557 * \param p_block the data block to send
559 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
561 es_out_sys_t *p_sys = out->p_sys;
563 if( p_sys->b_pcr_set )
565 pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
566 input_thread_t *p_input = p_sys->p_input;
570 p_pgrm = p_sys->p_input->stream.p_selected_program;
573 if( p_block->i_dts > 0 && p_pgrm )
576 input_ClockGetTS( p_input, p_pgrm, p_block->i_dts * 9 / 100 );
578 if( p_block->i_pts > 0 && p_pgrm )
581 input_ClockGetTS( p_input, p_pgrm, p_block->i_pts * 9 / 100 );
585 vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
586 p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
587 if( es->p_es->p_dec &&
588 (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
590 input_DecodeBlock( es->p_es->p_dec, p_block );
594 block_Release( p_block );
596 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
601 /*****************************************************************************
603 *****************************************************************************/
604 static void EsOutDel( es_out_t *out, es_out_id_t *es )
606 es_out_sys_t *p_sys = out->p_sys;
608 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
610 switch( es->p_es->i_cat )
623 /* We don't try to reselect */
624 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
625 if( es->p_es->p_dec )
627 input_UnselectES( p_sys->p_input, es->p_es );
630 if( es->p_es->p_waveformatex )
632 free( es->p_es->p_waveformatex );
633 es->p_es->p_waveformatex = NULL;
635 if( es->p_es->p_bitmapinfoheader )
637 free( es->p_es->p_bitmapinfoheader );
638 es->p_es->p_bitmapinfoheader = NULL;
640 input_DelES( p_sys->p_input, es->p_es );
642 if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
643 if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
644 if( p_sys->p_es_sub == es ) p_sys->p_es_sub = NULL;
646 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
652 * Control query handler
654 * \param out the es_out to control
655 * \param i_query A es_out query as defined in include/ninput.h
656 * \param args a variable list of arguments for the query
657 * \return VLC_SUCCESS or an error code
659 static int EsOutControl( es_out_t *out, int i_query, va_list args )
661 es_out_sys_t *p_sys = out->p_sys;
669 case ES_OUT_SET_ES_STATE:
670 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
671 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
672 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
673 if( b && es->p_es->p_dec == NULL )
675 input_SelectES( p_sys->p_input, es->p_es );
676 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
677 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
679 else if( !b && es->p_es->p_dec )
681 input_UnselectES( p_sys->p_input, es->p_es );
682 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
685 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
688 case ES_OUT_GET_ES_STATE:
689 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
690 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
692 *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
695 case ES_OUT_SET_ACTIVE:
697 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
703 val.b_bool = VLC_TRUE;
704 var_Set( p_sys->p_input, "intf-change", val );
709 case ES_OUT_GET_ACTIVE:
710 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
711 *pb = p_sys->b_active;
714 case ES_OUT_SET_MODE:
715 i = (int) va_arg( args, int );
716 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
717 i == ES_OUT_MODE_AUTO )
723 /* Reapply policy mode */
724 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
725 for( i = 0; i < p_sys->i_es; i++ )
727 if( p_sys->es[i]->p_es->p_dec )
729 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
732 for( i = 0; i < p_sys->i_es; i++ )
734 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
736 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
738 val.b_bool = VLC_TRUE;
739 var_Set( p_sys->p_input, "intf-change", val );
745 case ES_OUT_GET_MODE:
746 pi = (int*) va_arg( args, int* );
751 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
754 for( i = 0; i < p_sys->i_es; i++ )
756 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
757 if( p_sys->es[i]->p_es->p_dec )
759 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
761 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
766 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
767 EsOutSelect( out, es, VLC_TRUE );
768 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
773 case ES_OUT_SET_GROUP_PCR:
775 pgrm_descriptor_t *p_prgm = NULL;
778 if( i_query == ES_OUT_SET_PCR )
780 p_prgm = p_sys->p_input->stream.p_selected_program;
784 int i_group = (int)va_arg( args, int );
785 p_prgm = input_FindProgram( p_sys->p_input, i_group );
788 /* we create the requested program */
789 p_prgm = EsOutAddProgram( out, i_group );
792 i_pcr = (int64_t)va_arg( args, int64_t );
797 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr * 9 / 100);
799 p_sys->b_pcr_set = VLC_TRUE;
803 case ES_OUT_RESET_PCR:
804 for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
806 p_sys->p_input->stream.pp_programs[i]->i_synchro_state = SYNCHRO_REINIT;
808 p_sys->b_pcr_set = VLC_TRUE;
812 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );