1 /*****************************************************************************
2 * es_out.c: Es Out handler for input.
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: es_out.c,v 1.1 2003/11/24 20:50:45 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 *****************************************************************************/
40 input_thread_t *p_input;
51 es_descriptor_t *p_es;
54 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
55 static int EsOutSend ( es_out_t *, es_out_id_t *, block_t * );
56 static int EsOutSendPES( es_out_t *, es_out_id_t *, pes_packet_t * );
57 static void EsOutDel ( es_out_t *, es_out_id_t * );
58 static int EsOutControl( es_out_t *, int i_query, va_list );
61 /*****************************************************************************
63 *****************************************************************************/
64 es_out_t *input_EsOutNew( input_thread_t *p_input )
66 es_out_t *out = malloc( sizeof( es_out_t ) );
68 out->pf_add = EsOutAdd;
69 out->pf_send = EsOutSend;
70 out->pf_send_pes= EsOutSendPES;
71 out->pf_del = EsOutDel;
72 out->pf_control = EsOutControl;
74 out->p_sys = malloc( sizeof( es_out_sys_t ) );
75 out->p_sys->p_input = p_input;
77 out->p_sys->id = NULL;
78 out->p_sys->i_audio = -1;
79 out->p_sys->i_video = -1;
83 /*****************************************************************************
85 *****************************************************************************/
86 void input_EsOutDelete( es_out_t *out )
88 es_out_sys_t *p_sys = out->p_sys;
91 for( i = 0; i < p_sys->i_id; i++ )
103 /*****************************************************************************
105 *****************************************************************************/
106 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
108 es_out_sys_t *p_sys = out->p_sys;
109 input_thread_t *p_input = p_sys->p_input;
110 es_out_id_t *id = malloc( sizeof( es_out_id_t ) );
111 pgrm_descriptor_t *p_prgm = NULL;
112 char psz_cat[strlen( "Stream " ) + 10];
113 input_info_category_t *p_cat;
115 vlc_mutex_lock( &p_input->stream.stream_lock );
116 if( fmt->i_group >= 0 )
119 p_prgm = input_FindProgram( p_input, fmt->i_group );
124 p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
126 /* Select the first by default */
127 if( p_input->stream.p_selected_program == NULL )
129 p_input->stream.p_selected_program = p_prgm;
134 id->p_es = input_AddES( p_input,
136 1 + out->p_sys->i_id,
138 fmt->psz_description, 0 );
139 id->p_es->i_stream_id = 1 + out->p_sys->i_id;
140 id->p_es->i_fourcc = fmt->i_codec;
147 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
149 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
150 p_wf->nChannels = fmt->audio.i_channels;
151 p_wf->nSamplesPerSec = fmt->audio.i_rate;
152 p_wf->nAvgBytesPerSec = fmt->i_bitrate / 8;
153 p_wf->nBlockAlign = fmt->audio.i_blockalign;
154 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
155 p_wf->cbSize = fmt->i_extra;
156 if( fmt->i_extra > 0 )
158 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
160 id->p_es->p_waveformatex = p_wf;
165 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
167 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
168 p_bih->biWidth = fmt->video.i_width;
169 p_bih->biHeight = fmt->video.i_height;
171 p_bih->biBitCount = 24;
172 p_bih->biCompression = fmt->i_codec;
173 p_bih->biSizeImage = fmt->video.i_width *
175 p_bih->biXPelsPerMeter = 0;
176 p_bih->biYPelsPerMeter = 0;
177 p_bih->biClrUsed = 0;
178 p_bih->biClrImportant = 0;
180 if( fmt->i_extra > 0 )
182 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
184 id->p_es->p_bitmapinfoheader = p_bih;
189 subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
190 memset( p_sub, 0, sizeof( subtitle_data_t ) );
191 if( fmt->i_extra > 0 )
193 p_sub->psz_header = malloc( fmt->i_extra );
194 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
196 /* FIXME beuuuuuurk */
197 id->p_es->p_demux_data = p_sub;
204 if( fmt->i_cat == AUDIO_ES && fmt->i_priority > out->p_sys->i_audio )
206 if( out->p_sys->i_audio >= 0 )
208 msg_Err( p_input, "FIXME unselect es in es_out_Add" );
210 input_SelectES( p_input, id->p_es );
211 if( id->p_es->p_dec )
213 out->p_sys->i_audio = fmt->i_priority;
216 else if( fmt->i_cat == VIDEO_ES && fmt->i_priority > out->p_sys->i_video )
218 if( out->p_sys->i_video >= 0 )
220 msg_Err( p_input, "FIXME unselect es in es_out_Add" );
222 input_SelectES( p_input, id->p_es );
223 if( id->p_es->p_dec )
225 out->p_sys->i_video = fmt->i_priority;
229 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id );
230 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
232 /* Add information */
236 input_AddInfo( p_cat, _("Type"), _("Audio") );
237 input_AddInfo( p_cat, _("Codec"), "%.4s",
238 (char*)&fmt->i_codec );
239 if( fmt->audio.i_channels > 0 )
241 input_AddInfo( p_cat, _("Channels"), "%d",
242 fmt->audio.i_channels );
244 if( fmt->audio.i_rate > 0 )
246 input_AddInfo( p_cat, _("Sample Rate"), "%d",
249 if( fmt->i_bitrate > 0 )
251 input_AddInfo( p_cat, _("Bitrate"), "%d",
254 if( fmt->audio.i_bitspersample )
256 input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
257 fmt->audio.i_bitspersample );
261 input_AddInfo( p_cat, _("Type"), _("Video") );
262 input_AddInfo( p_cat, _("Codec"), "%.4s",
263 (char*)&fmt->i_codec );
264 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
266 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
267 fmt->video.i_width, fmt->video.i_height );
269 if( fmt->video.i_visible_width > 0 &&
270 fmt->video.i_visible_height > 0 )
272 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d",
273 fmt->video.i_visible_width,
274 fmt->video.i_visible_height);
278 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
279 input_AddInfo( p_cat, _("Codec"), "%.4s",
280 (char*)&fmt->i_codec );
287 vlc_mutex_unlock( &p_input->stream.stream_lock );
289 id->p_es->fmt = *fmt;
291 TAB_APPEND( out->p_sys->i_id, out->p_sys->id, id );
295 /*****************************************************************************
297 *****************************************************************************/
298 static int EsOutSend( es_out_t *out, es_out_id_t *id, block_t *p_block )
300 if( id->p_es->p_dec )
302 input_DecodeBlock( id->p_es->p_dec, p_block );
306 block_Release( p_block );
311 /*****************************************************************************
313 *****************************************************************************/
314 static int EsOutSendPES( es_out_t *out, es_out_id_t *id, pes_packet_t *p_pes )
316 if( id->p_es->p_dec )
318 input_DecodePES( id->p_es->p_dec, p_pes );
322 input_DeletePES( out->p_sys->p_input->p_method_data, p_pes );
327 /*****************************************************************************
329 *****************************************************************************/
330 static void EsOutDel( es_out_t *out, es_out_id_t *id )
332 es_out_sys_t *p_sys = out->p_sys;
334 TAB_REMOVE( p_sys->i_id, p_sys->id, id );
336 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
337 if( id->p_es->p_dec )
339 input_UnselectES( p_sys->p_input, id->p_es );
341 if( id->p_es->p_waveformatex )
343 free( id->p_es->p_waveformatex );
344 id->p_es->p_waveformatex = NULL;
346 if( id->p_es->p_bitmapinfoheader )
348 free( id->p_es->p_bitmapinfoheader );
349 id->p_es->p_bitmapinfoheader = NULL;
351 input_DelES( p_sys->p_input, id->p_es );
352 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
357 /*****************************************************************************
359 *****************************************************************************/
360 static int EsOutControl( es_out_t *out, int i_query, va_list args )
362 es_out_sys_t *p_sys = out->p_sys;
367 case ES_OUT_SET_SELECT:
368 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
369 id = (es_out_id_t*) va_arg( args, es_out_id_t * );
370 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
371 if( b && id->p_es->p_dec == NULL )
373 input_SelectES( p_sys->p_input, id->p_es );
374 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
375 return id->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
377 else if( !b && id->p_es->p_dec )
379 input_UnselectES( p_sys->p_input, id->p_es );
380 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
383 case ES_OUT_GET_SELECT:
384 id = (es_out_id_t*) va_arg( args, es_out_id_t * );
385 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
387 *pb = id->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
391 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );