1 /*****************************************************************************
2 * es.c: functions to handle elementary streams.
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: es.c,v 1.5 2003/01/29 11:17:44 gbazin Exp $
7 * Author: Stéphane Borel <stef@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 *****************************************************************************/
31 #include <vlc/input.h>
38 #include <sys/types.h>
43 #ifdef STRNCASECMP_IN_STRINGS_H
50 void dvdplay_LaunchDecoders( input_thread_t * p_input );
52 /*****************************************************************************
54 *****************************************************************************/
55 void dvdplay_DeleteES( input_thread_t* p_input )
57 free( p_input->stream.pp_selected_es );
59 p_input->stream.pp_selected_es = NULL;
60 p_input->stream.i_selected_es_number = 0;
62 while( p_input->stream.i_es_number )
64 input_DelES( p_input, p_input->stream.pp_es[0] );
67 free( p_input->stream.pp_es );
69 p_input->stream.pp_es = NULL;
70 p_input->stream.i_es_number = 0;
74 #define ADDES( id, fourcc, cat, lang, size ) \
75 msg_Dbg( p_input, "new es 0x%x", i_id ); \
76 p_es = input_AddES( p_input, NULL, id, size ); \
77 p_es->i_stream_id = i_id & 0xff; \
78 p_es->i_fourcc = (fourcc); \
79 p_es->i_cat = (cat); \
82 strcpy( p_es->psz_desc, DecodeLanguage( lang ) ); \
85 /*****************************************************************************
86 * dvdplay_Video: read video ES
87 *****************************************************************************/
88 void dvdplay_Video( input_thread_t * p_input )
91 es_descriptor_t * p_es;
92 video_attr_t * p_attr;
95 p_dvd = (dvd_data_t*)(p_input->p_access_data);
96 p_attr = dvdplay_video_attr( p_dvd->vmg );
98 /* ES 0 -> video MPEG2 */
101 if( p_attr->display_aspect_ratio )
103 ADDES( 0xe0, VLC_FOURCC('m','p','g','v'), VIDEO_ES, 0, sizeof(int) );
104 *(int*)(p_es->p_demux_data) = p_attr->display_aspect_ratio;
108 ADDES( 0xe0, VLC_FOURCC('m','p','g','v'), VIDEO_ES, 0, 0 );
113 /*****************************************************************************
114 * dvdplay_Audio: read audio ES
115 *****************************************************************************/
116 void dvdplay_Audio( input_thread_t * p_input )
119 es_descriptor_t * p_es;
120 audio_attr_t * p_attr;
128 p_dvd = (dvd_data_t*)(p_input->p_access_data);
129 p_dvd->i_audio_nb = 0;
130 dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
132 /* Audio ES, in the order they appear in .ifo */
133 for( i = 1 ; i <= i_audio_nr ; i++ )
135 if( ( i_id = dvdplay_audio_id( p_dvd->vmg, i-1 ) ) > 0 )
137 p_attr = dvdplay_audio_attr( p_dvd->vmg, i-1 );
138 i_channels = p_attr->channels;
139 i_lang = p_attr->lang_code;
143 switch( p_attr->audio_format )
146 ADDES( i_id, VLC_FOURCC('a','5','2','b'), AUDIO_ES, i_lang, 0 );
147 strcat( p_es->psz_desc, " (A52)" );
151 case 0x03: /* MPEG audio */
152 ADDES( i_id, VLC_FOURCC('m','p','g','a'), AUDIO_ES, i_lang, 0 );
153 strcat( p_es->psz_desc, " (mpeg)" );
156 case 0x04: /* LPCM */
157 ADDES( i_id, VLC_FOURCC('l','p','c','b'), AUDIO_ES, i_lang, 0 );
158 strcat( p_es->psz_desc, " (lpcm)" );
161 case 0x05: /* SDDS */
162 ADDES( i_id, VLC_FOURCC('s','d','d','b'), AUDIO_ES, i_lang, 0 );
163 strcat( p_es->psz_desc, " (sdds)" );
167 ADDES( i_id, VLC_FOURCC('d','t','s','b'), AUDIO_ES, i_lang, 0 );
168 strcat( p_es->psz_desc, " (dts)" );
173 msg_Warn( p_input, "unknown audio type %.2x",
174 p_attr->audio_format );
180 /*****************************************************************************
181 * dvdplay_Subp: read subpictures ES
182 *****************************************************************************/
183 void dvdplay_Subp( input_thread_t * p_input )
186 es_descriptor_t * p_es;
187 subp_attr_t * p_attr;
194 p_dvd = (dvd_data_t*)(p_input->p_access_data);
197 dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
198 pi_palette = dvdplay_subp_palette( p_dvd->vmg );
200 for( i = 1 ; i <= i_subp_nr; i++ )
202 if( ( i_id = dvdplay_subp_id( p_dvd->vmg, i-1 ) ) >= 0 )
204 p_attr = dvdplay_subp_attr( p_dvd->vmg, i-1 );
209 ADDES( i_id, VLC_FOURCC('s','p','u','b'), SPU_ES,
210 p_attr->lang_code, sizeof(int) + 16*sizeof(u32) );
211 *(int*)p_es->p_demux_data = 0xBeeF;
212 memcpy( (void*)p_es->p_demux_data + sizeof(int),
213 pi_palette, 16*sizeof(u32) );
217 ADDES( i_id, VLC_FOURCC('s','p','u','b'), SPU_ES,
218 p_attr->lang_code, 0 );
224 /*****************************************************************************
225 * dvdplay_LaunchDecoders
226 *****************************************************************************/
227 void dvdplay_LaunchDecoders( input_thread_t * p_input )
235 p_dvd = (dvd_data_t*)(p_input->p_access_data);
237 /* For audio: check user settings first, then check dvdplay settings. */
238 i_audio = config_GetInt( p_input, "audio-channel" );
239 if( i_audio <= 0 || i_audio > p_dvd->i_audio_nb )
241 /* if i_audio = -1 dvdplay_audio_info() will select a default channel,
242 * otherwise it warns libdvdplay that we have chosen another stream. */
245 dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
247 /* For spu: check user settings first, the check dvdplay settings. */
248 i_subp = config_GetInt( p_input, "spu-channel" );
249 if( i_subp <= 0 || i_subp > p_dvd->i_spu_nb )
253 dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
255 input_SelectES( p_input, p_input->stream.pp_es[0] );
257 if( i_audio > p_dvd->i_audio_nb ) i_audio = 1;
258 if( ( i_audio > 0 ) && ( p_dvd->i_audio_nb > 0 ) )
260 if( config_GetInt( p_input, "audio-type" ) == REQUESTED_A52 )
264 while( ( i_a52 < p_dvd->i_audio_nb ) &&
265 ( p_input->stream.pp_es[i_a52]->i_fourcc !=
266 VLC_FOURCC('a','5','2','b') ) )
270 if( p_input->stream.pp_es[i_a52]->i_fourcc ==
271 VLC_FOURCC('a','5','2','b') )
273 input_SelectES( p_input,
274 p_input->stream.pp_es[i_a52] );
276 /* warn libdvdplay that we have chosen another stream */
277 dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_a52 );
281 /* none found, select the default one */
282 input_SelectES( p_input,
283 p_input->stream.pp_es[i_audio] );
288 input_SelectES( p_input,
289 p_input->stream.pp_es[i_audio] );
293 if( i_subp > p_dvd->i_spu_nb ) i_subp = -1;
294 if( ( i_subp > 0 ) && ( p_dvd->i_spu_nb > 0 ) )
296 i_subp += p_dvd->i_audio_nb;
297 input_SelectES( p_input, p_input->stream.pp_es[i_subp] );
301 /*****************************************************************************
303 *****************************************************************************/
304 void dvdplay_ES( input_thread_t * p_input )
306 dvdplay_DeleteES ( p_input );
307 dvdplay_Video ( p_input );
308 dvdplay_Audio ( p_input );
309 dvdplay_Subp ( p_input );
310 dvdplay_LaunchDecoders( p_input );