1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: es_out.c,v 1.3 2003/11/27 05:46:01 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>
35 /*****************************************************************************
37 *****************************************************************************/
41 es_descriptor_t *p_es;
46 input_thread_t *p_input;
68 es_out_id_t *p_es_audio;
69 es_out_id_t *p_es_video;
70 es_out_id_t *p_es_sub;
73 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
74 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
75 static void EsOutDel ( es_out_t *, es_out_id_t * );
76 static int EsOutControl( es_out_t *, int i_query, va_list );
79 /*****************************************************************************
81 *****************************************************************************/
82 es_out_t *input_EsOutNew( input_thread_t *p_input )
84 es_out_t *out = malloc( sizeof( es_out_t ) );
85 es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
88 out->pf_add = EsOutAdd;
89 out->pf_send = EsOutSend;
90 out->pf_del = EsOutDel;
91 out->pf_control = EsOutControl;
94 p_sys->p_input = p_input;
96 p_sys->b_active = VLC_FALSE;
97 p_sys->i_mode = ES_OUT_MODE_AUTO;
108 var_Get( p_input, "audio-channel", &val );
109 p_sys->i_audio_last = val.i_int;
111 var_Get( p_input, "spu-channel", &val );
112 p_sys->i_sub_last = val.i_int;
114 p_sys->p_es_audio = NULL;
115 p_sys->p_es_video = NULL;
116 p_sys->p_es_sub = NULL;
121 /*****************************************************************************
123 *****************************************************************************/
124 void input_EsOutDelete( es_out_t *out )
126 es_out_sys_t *p_sys = out->p_sys;
129 for( i = 0; i < p_sys->i_es; i++ )
131 free( p_sys->es[i] );
141 /*****************************************************************************
142 * EsOutSelect: Select an ES given the current mode
143 * XXX: you need to take a the lock before (stream.stream_lock)
144 *****************************************************************************/
145 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
147 es_out_sys_t *p_sys = out->p_sys;
148 input_thread_t *p_input = p_sys->p_input;
150 int i_cat = es->p_es->i_cat;
152 if( !p_sys->b_active || ( !b_force && es->p_es->fmt.i_priority < 0 ) )
157 if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
159 input_SelectES( p_input, es->p_es );
161 else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
165 if( i_cat == AUDIO_ES )
167 if( p_sys->p_es_audio && p_sys->p_es_audio->p_es->fmt.i_priority >= es->p_es->fmt.i_priority )
171 i_wanted = p_sys->i_audio_last >= 0 ? p_sys->i_audio_last : es->i_channel;
173 else if( i_cat == SPU_ES )
175 if( p_sys->p_es_sub && p_sys->p_es_sub->p_es->fmt.i_priority >= es->p_es->fmt.i_priority )
179 i_wanted = p_sys->i_sub_last;
181 else if( i_cat == VIDEO_ES )
183 i_wanted = es->i_channel;
186 if( i_wanted == es->i_channel )
188 input_SelectES( p_input, es->p_es );
192 /* FIXME TODO handle priority here */
193 if( es->p_es->p_dec )
195 if( i_cat == AUDIO_ES )
197 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
198 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
200 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
202 p_sys->p_es_audio = es;
204 else if( i_cat == SPU_ES )
206 if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
207 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
209 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
211 p_sys->p_es_sub = es;
213 else if( i_cat == VIDEO_ES )
215 p_sys->p_es_video = es;
220 /*****************************************************************************
222 *****************************************************************************/
223 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
225 es_out_sys_t *p_sys = out->p_sys;
226 input_thread_t *p_input = p_sys->p_input;
227 es_out_id_t *es = malloc( sizeof( es_out_id_t ) );
228 pgrm_descriptor_t *p_prgm = NULL;
229 char psz_cat[strlen( "Stream " ) + 10];
230 input_info_category_t *p_cat;
232 vlc_mutex_lock( &p_input->stream.stream_lock );
233 if( fmt->i_group >= 0 )
236 p_prgm = input_FindProgram( p_input, fmt->i_group );
241 p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
243 /* Select the first by default */
244 if( p_input->stream.p_selected_program == NULL )
246 p_input->stream.p_selected_program = p_prgm;
251 es->p_es = input_AddES( p_input,
255 fmt->psz_description, 0 );
256 es->p_es->i_stream_id = out->p_sys->i_id;
257 es->p_es->i_fourcc = fmt->i_codec;
264 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
266 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
267 p_wf->nChannels = fmt->audio.i_channels;
268 p_wf->nSamplesPerSec = fmt->audio.i_rate;
269 p_wf->nAvgBytesPerSec = fmt->i_bitrate / 8;
270 p_wf->nBlockAlign = fmt->audio.i_blockalign;
271 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
272 p_wf->cbSize = fmt->i_extra;
273 if( fmt->i_extra > 0 )
275 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
277 es->p_es->p_waveformatex = p_wf;
279 es->i_channel = p_sys->i_audio;
284 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
286 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
287 p_bih->biWidth = fmt->video.i_width;
288 p_bih->biHeight = fmt->video.i_height;
290 p_bih->biBitCount = 24;
291 p_bih->biCompression = fmt->i_codec;
292 p_bih->biSizeImage = fmt->video.i_width *
294 p_bih->biXPelsPerMeter = 0;
295 p_bih->biYPelsPerMeter = 0;
296 p_bih->biClrUsed = 0;
297 p_bih->biClrImportant = 0;
299 if( fmt->i_extra > 0 )
301 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
303 es->p_es->p_bitmapinfoheader = p_bih;
305 es->i_channel = p_sys->i_video;
310 subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
311 memset( p_sub, 0, sizeof( subtitle_data_t ) );
312 if( fmt->i_extra > 0 )
314 p_sub->psz_header = malloc( fmt->i_extra + 1 );
315 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
316 /* just to be sure */
317 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
319 /* FIXME beuuuuuurk */
320 es->p_es->p_demux_data = p_sub;
322 es->i_channel = p_sys->i_sub;
331 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
332 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
334 /* Add information */
338 input_AddInfo( p_cat, _("Type"), _("Audio") );
339 input_AddInfo( p_cat, _("Codec"), "%.4s",
340 (char*)&fmt->i_codec );
341 if( fmt->audio.i_channels > 0 )
343 input_AddInfo( p_cat, _("Channels"), "%d",
344 fmt->audio.i_channels );
346 if( fmt->audio.i_rate > 0 )
348 input_AddInfo( p_cat, _("Sample Rate"), "%d",
351 if( fmt->i_bitrate > 0 )
353 input_AddInfo( p_cat, _("Bitrate"), "%d",
356 if( fmt->audio.i_bitspersample )
358 input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
359 fmt->audio.i_bitspersample );
363 input_AddInfo( p_cat, _("Type"), _("Video") );
364 input_AddInfo( p_cat, _("Codec"), "%.4s",
365 (char*)&fmt->i_codec );
366 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
368 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
369 fmt->video.i_width, fmt->video.i_height );
371 if( fmt->video.i_visible_width > 0 &&
372 fmt->video.i_visible_height > 0 )
374 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d",
375 fmt->video.i_visible_width,
376 fmt->video.i_visible_height);
380 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
381 input_AddInfo( p_cat, _("Codec"), "%.4s",
382 (char*)&fmt->i_codec );
392 * XXX change that when we do group too */
395 EsOutSelect( out, es, VLC_FALSE );
398 vlc_mutex_unlock( &p_input->stream.stream_lock );
400 es->p_es->fmt = *fmt;
402 TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
403 p_sys->i_id++; /* always incremented */
420 /*****************************************************************************
422 *****************************************************************************/
423 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
425 vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
426 if( es->p_es->p_dec )
428 input_DecodeBlock( es->p_es->p_dec, p_block );
432 block_Release( p_block );
434 vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
438 /*****************************************************************************
440 *****************************************************************************/
441 static void EsOutDel( es_out_t *out, es_out_id_t *es )
443 es_out_sys_t *p_sys = out->p_sys;
445 TAB_REMOVE( p_sys->i_es, p_sys->es, es );
447 switch( es->p_es->i_cat )
460 /* We don't try to reselect */
461 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
462 if( es->p_es->p_dec )
464 input_UnselectES( p_sys->p_input, es->p_es );
467 if( es->p_es->p_waveformatex )
469 free( es->p_es->p_waveformatex );
470 es->p_es->p_waveformatex = NULL;
472 if( es->p_es->p_bitmapinfoheader )
474 free( es->p_es->p_bitmapinfoheader );
475 es->p_es->p_bitmapinfoheader = NULL;
477 input_DelES( p_sys->p_input, es->p_es );
479 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
484 /*****************************************************************************
486 *****************************************************************************/
487 static int EsOutControl( es_out_t *out, int i_query, va_list args )
489 es_out_sys_t *p_sys = out->p_sys;
497 case ES_OUT_SET_ES_STATE:
498 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
499 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
500 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
501 if( b && es->p_es->p_dec == NULL )
503 input_SelectES( p_sys->p_input, es->p_es );
504 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
505 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
507 else if( !b && es->p_es->p_dec )
509 input_UnselectES( p_sys->p_input, es->p_es );
510 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
513 case ES_OUT_GET_ES_STATE:
514 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
515 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
517 *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
520 case ES_OUT_SET_ACTIVE:
522 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
528 val.b_bool = VLC_TRUE;
529 var_Set( p_sys->p_input, "intf-change", val );
534 case ES_OUT_GET_ACTIVE:
535 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
536 *pb = p_sys->b_active;
539 case ES_OUT_SET_MODE:
540 i = (int) va_arg( args, int );
541 if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL || i == ES_OUT_MODE_AUTO )
547 /* Reapply policy mode */
548 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
549 for( i = 0; i < p_sys->i_es; i++ )
551 if( p_sys->es[i]->p_es->p_dec )
553 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
556 for( i = 0; i < p_sys->i_es; i++ )
558 EsOutSelect( out, p_sys->es[i], VLC_FALSE );
560 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
562 val.b_bool = VLC_TRUE;
563 var_Set( p_sys->p_input, "intf-change", val );
569 case ES_OUT_GET_MODE:
570 pi = (int*) va_arg( args, int* );
575 es = (es_out_id_t*) va_arg( args, es_out_id_t * );
578 for( i = 0; i < p_sys->i_es; i++ )
580 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
581 if( p_sys->es[i]->p_es->p_dec )
583 input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
585 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
590 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
591 EsOutSelect( out, es, VLC_TRUE );
592 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
597 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );