]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* moved EsOut* to es_out.c and introduced internal input_EsOutNew and
[vlc] / src / input / es_out.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
32
33 #include "codecs.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 struct es_out_sys_t
39 {
40     input_thread_t *p_input;
41
42     int         i_id;
43     es_out_id_t **id;
44
45     vlc_bool_t  i_audio;
46     vlc_bool_t  i_video;
47 };
48
49 struct es_out_id_t
50 {
51     es_descriptor_t *p_es;
52 };
53
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 );
59
60
61 /*****************************************************************************
62  * input_EsOutNew:
63  *****************************************************************************/
64 es_out_t *input_EsOutNew( input_thread_t *p_input )
65 {
66     es_out_t *out = malloc( sizeof( es_out_t ) );
67
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;
73
74     out->p_sys = malloc( sizeof( es_out_sys_t ) );
75     out->p_sys->p_input = p_input;
76     out->p_sys->i_id    = 0;
77     out->p_sys->id      = NULL;
78     out->p_sys->i_audio = -1;
79     out->p_sys->i_video = -1;
80     return out;
81 }
82
83 /*****************************************************************************
84  * input_EsOutDelete:
85  *****************************************************************************/
86 void input_EsOutDelete( es_out_t *out )
87 {
88     es_out_sys_t *p_sys = out->p_sys;
89     int i;
90
91     for( i = 0; i < p_sys->i_id; i++ )
92     {
93         free( p_sys->id[i] );
94     }
95     if( p_sys->id )
96     {
97         free( p_sys->id );
98     }
99     free( p_sys );
100     free( out );
101 }
102
103 /*****************************************************************************
104  * EsOutAdd:
105  *****************************************************************************/
106 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
107 {
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;
114
115     vlc_mutex_lock( &p_input->stream.stream_lock );
116     if( fmt->i_group >= 0 )
117     {
118         /* search program */
119         p_prgm = input_FindProgram( p_input, fmt->i_group );
120
121         if( p_prgm == NULL )
122         {
123             /* create it */
124             p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
125
126             /* Select the first by default */
127             if( p_input->stream.p_selected_program == NULL )
128             {
129                 p_input->stream.p_selected_program = p_prgm;
130             }
131         }
132     }
133
134     id->p_es = input_AddES( p_input,
135                             p_prgm,
136                             1 + out->p_sys->i_id,
137                             fmt->i_cat,
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;
141
142     switch( fmt->i_cat )
143     {
144         case AUDIO_ES:
145         {
146             WAVEFORMATEX *p_wf =
147                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
148
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 )
157             {
158                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
159             }
160             id->p_es->p_waveformatex = p_wf;
161             break;
162         }
163         case VIDEO_ES:
164         {
165             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
166                                               fmt->i_extra );
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;
170             p_bih->biPlanes         = 1;
171             p_bih->biBitCount       = 24;
172             p_bih->biCompression    = fmt->i_codec;
173             p_bih->biSizeImage      = fmt->video.i_width *
174                                           fmt->video.i_height;
175             p_bih->biXPelsPerMeter  = 0;
176             p_bih->biYPelsPerMeter  = 0;
177             p_bih->biClrUsed        = 0;
178             p_bih->biClrImportant   = 0;
179
180             if( fmt->i_extra > 0 )
181             {
182                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
183             }
184             id->p_es->p_bitmapinfoheader = p_bih;
185             break;
186         }
187         case SPU_ES:
188         {
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 )
192             {
193                 p_sub->psz_header = malloc( fmt->i_extra  );
194                 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
195             }
196             /* FIXME beuuuuuurk */
197             id->p_es->p_demux_data = p_sub;
198             break;
199         }
200         default:
201             break;
202     }
203
204     if( fmt->i_cat == AUDIO_ES && fmt->i_priority > out->p_sys->i_audio )
205     {
206         if( out->p_sys->i_audio >= 0 )
207         {
208             msg_Err( p_input, "FIXME unselect es in es_out_Add" );
209         }
210         input_SelectES( p_input, id->p_es );
211         if( id->p_es->p_dec )
212         {
213             out->p_sys->i_audio = fmt->i_priority;
214         }
215     }
216     else if( fmt->i_cat == VIDEO_ES && fmt->i_priority > out->p_sys->i_video )
217     {
218         if( out->p_sys->i_video >= 0 )
219         {
220             msg_Err( p_input, "FIXME unselect es in es_out_Add" );
221         }
222         input_SelectES( p_input, id->p_es );
223         if( id->p_es->p_dec )
224         {
225             out->p_sys->i_video = fmt->i_priority;
226         }
227     }
228
229     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id );
230     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
231     {
232         /* Add information */
233         switch( fmt->i_cat )
234         {
235             case AUDIO_ES:
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 )
240                 {
241                     input_AddInfo( p_cat, _("Channels"), "%d",
242                                    fmt->audio.i_channels );
243                 }
244                 if( fmt->audio.i_rate > 0 )
245                 {
246                     input_AddInfo( p_cat, _("Sample Rate"), "%d",
247                                    fmt->audio.i_rate );
248                 }
249                 if( fmt->i_bitrate > 0 )
250                 {
251                     input_AddInfo( p_cat, _("Bitrate"), "%d",
252                                    fmt->i_bitrate );
253                 }
254                 if( fmt->audio.i_bitspersample )
255                 {
256                     input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
257                                    fmt->audio.i_bitspersample );
258                 }
259                 break;
260             case VIDEO_ES:
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 )
265                 {
266                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
267                                    fmt->video.i_width, fmt->video.i_height );
268                 }
269                 if( fmt->video.i_visible_width > 0 &&
270                     fmt->video.i_visible_height > 0 )
271                 {
272                     input_AddInfo( p_cat, _("Display Resolution"), "%dx%d",
273                                    fmt->video.i_visible_width,
274                                    fmt->video.i_visible_height);
275                 }
276                 break;
277             case SPU_ES:
278                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
279                 input_AddInfo( p_cat, _("Codec"), "%.4s",
280                                (char*)&fmt->i_codec );
281                 break;
282             default:
283
284                 break;
285         }
286     }
287     vlc_mutex_unlock( &p_input->stream.stream_lock );
288
289     id->p_es->fmt = *fmt;
290
291     TAB_APPEND( out->p_sys->i_id, out->p_sys->id, id );
292     return id;
293 }
294
295 /*****************************************************************************
296  * EsOutSend:
297  *****************************************************************************/
298 static int EsOutSend( es_out_t *out, es_out_id_t *id, block_t *p_block )
299 {
300     if( id->p_es->p_dec )
301     {
302         input_DecodeBlock( id->p_es->p_dec, p_block );
303     }
304     else
305     {
306         block_Release( p_block );
307     }
308     return VLC_SUCCESS;
309 }
310
311 /*****************************************************************************
312  * EsOutSendPES:
313  *****************************************************************************/
314 static int EsOutSendPES( es_out_t *out, es_out_id_t *id, pes_packet_t *p_pes )
315 {
316     if( id->p_es->p_dec )
317     {
318         input_DecodePES( id->p_es->p_dec, p_pes );
319     }
320     else
321     {
322         input_DeletePES( out->p_sys->p_input->p_method_data, p_pes );
323     }
324     return VLC_SUCCESS;
325 }
326
327 /*****************************************************************************
328  * EsOutDel:
329  *****************************************************************************/
330 static void EsOutDel( es_out_t *out, es_out_id_t *id )
331 {
332     es_out_sys_t *p_sys = out->p_sys;
333
334     TAB_REMOVE( p_sys->i_id, p_sys->id, id );
335
336     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
337     if( id->p_es->p_dec )
338     {
339         input_UnselectES( p_sys->p_input, id->p_es );
340     }
341     if( id->p_es->p_waveformatex )
342     {
343         free( id->p_es->p_waveformatex );
344         id->p_es->p_waveformatex = NULL;
345     }
346     if( id->p_es->p_bitmapinfoheader )
347     {
348         free( id->p_es->p_bitmapinfoheader );
349         id->p_es->p_bitmapinfoheader = NULL;
350     }
351     input_DelES( p_sys->p_input, id->p_es );
352     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
353
354     free( id );
355 }
356
357 /*****************************************************************************
358  * EsOutControl:
359  *****************************************************************************/
360 static int EsOutControl( es_out_t *out, int i_query, va_list args )
361 {
362     es_out_sys_t *p_sys = out->p_sys;
363     vlc_bool_t  b, *pb;
364     es_out_id_t *id;
365     switch( i_query )
366     {
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 )
372             {
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;
376             }
377             else if( !b && id->p_es->p_dec )
378             {
379                 input_UnselectES( p_sys->p_input, id->p_es );
380                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
381                 return VLC_SUCCESS;
382             }
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 * );
386
387             *pb = id->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
388             return VLC_SUCCESS;
389
390         default:
391             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
392             return VLC_EGENERIC;
393     }
394 }
395