]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* all: implement more es_out_* control, audio-channel and spu-channel are
[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.2 2003/11/27 04:11:40 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_id_t
39 {
40     int             i_channel;
41     es_descriptor_t *p_es;
42 };
43
44 struct es_out_sys_t
45 {
46     input_thread_t *p_input;
47
48     /* all es */
49     int         i_id;
50
51     int         i_es;
52     es_out_id_t **es;
53
54     /* mode gestion */
55     vlc_bool_t  b_active;
56     int         i_mode;
57
58     /* es count */
59     int         i_audio;
60     int         i_video;
61     int         i_sub;
62
63     /* es to select */
64     int         i_audio_last;
65     int         i_sub_last;
66
67     /* current main es */
68     es_out_id_t *p_es_audio;
69     es_out_id_t *p_es_video;
70     es_out_id_t *p_es_sub;
71 };
72
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 );
77
78
79 /*****************************************************************************
80  * input_EsOutNew:
81  *****************************************************************************/
82 es_out_t *input_EsOutNew( input_thread_t *p_input )
83 {
84     es_out_t     *out = malloc( sizeof( es_out_t ) );
85     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
86     vlc_value_t  val;
87
88     out->pf_add     = EsOutAdd;
89     out->pf_send    = EsOutSend;
90     out->pf_del     = EsOutDel;
91     out->pf_control = EsOutControl;
92     out->p_sys      = p_sys;
93
94     p_sys->p_input = p_input;
95
96     p_sys->b_active = VLC_FALSE;
97     p_sys->i_mode   = ES_OUT_MODE_AUTO;
98
99     p_sys->i_id    = 1;
100
101     p_sys->i_es    = 0;
102     p_sys->es      = NULL;
103
104     p_sys->i_audio = 0;
105     p_sys->i_video = 0;
106     p_sys->i_sub   = 0;
107
108     var_Get( p_input, "audio-channel", &val );
109     p_sys->i_audio_last = val.i_int;
110
111     var_Get( p_input, "spu-channel", &val );
112     p_sys->i_sub_last = val.i_int;
113
114     p_sys->p_es_audio = NULL;
115     p_sys->p_es_video = NULL;
116     p_sys->p_es_sub   = NULL;
117
118     return out;
119 }
120
121 /*****************************************************************************
122  * input_EsOutDelete:
123  *****************************************************************************/
124 void input_EsOutDelete( es_out_t *out )
125 {
126     es_out_sys_t *p_sys = out->p_sys;
127     int i;
128
129     for( i = 0; i < p_sys->i_es; i++ )
130     {
131         free( p_sys->es[i] );
132     }
133     if( p_sys->es )
134     {
135         free( p_sys->es );
136     }
137     free( p_sys );
138     free( out );
139 }
140
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 )
146 {
147     es_out_sys_t      *p_sys = out->p_sys;
148     input_thread_t    *p_input = p_sys->p_input;
149
150     int i_cat = es->p_es->i_cat;
151
152     if( !p_sys->b_active || ( !b_force && es->p_es->fmt.i_priority < 0 ) )
153     {
154         return;
155     }
156
157     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
158     {
159         input_SelectES( p_input, es->p_es );
160     }
161     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
162     {
163         int i_wanted  = -1;
164
165         if( i_cat == AUDIO_ES )
166         {
167             if( p_sys->p_es_audio && p_sys->p_es_audio->p_es->fmt.i_priority >= es->p_es->fmt.i_priority )
168             {
169                 return;
170             }
171             i_wanted  = p_sys->i_audio_last >= 0 ? p_sys->i_audio_last : es->i_channel;
172         }
173         else if( i_cat == SPU_ES )
174         {
175             if( p_sys->p_es_sub && p_sys->p_es_sub->p_es->fmt.i_priority >= es->p_es->fmt.i_priority )
176             {
177                 return;
178             }
179             i_wanted  = p_sys->i_sub_last;
180         }
181         else if( i_cat == VIDEO_ES )
182         {
183             i_wanted  = es->i_channel;
184         }
185
186         if( i_wanted == es->i_channel )
187         {
188             input_SelectES( p_input, es->p_es );
189         }
190     }
191
192     /* FIXME TODO handle priority here */
193     if( es->p_es->p_dec )
194     {
195         if( i_cat == AUDIO_ES )
196         {
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 )
199             {
200                 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
201             }
202             p_sys->p_es_audio = es;
203         }
204         else if( i_cat == SPU_ES )
205         {
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 )
208             {
209                 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
210             }
211             p_sys->p_es_sub = es;
212         }
213         else if( i_cat == VIDEO_ES )
214         {
215             p_sys->p_es_video = es;
216         }
217     }
218 }
219
220 /*****************************************************************************
221  * EsOutAdd:
222  *****************************************************************************/
223 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
224 {
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;
231
232     vlc_mutex_lock( &p_input->stream.stream_lock );
233     if( fmt->i_group >= 0 )
234     {
235         /* search program */
236         p_prgm = input_FindProgram( p_input, fmt->i_group );
237
238         if( p_prgm == NULL )
239         {
240             /* create it */
241             p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
242
243             /* Select the first by default */
244             if( p_input->stream.p_selected_program == NULL )
245             {
246                 p_input->stream.p_selected_program = p_prgm;
247             }
248         }
249     }
250
251     es->p_es = input_AddES( p_input,
252                             p_prgm,
253                             out->p_sys->i_id,
254                             fmt->i_cat,
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;
258
259     switch( fmt->i_cat )
260     {
261         case AUDIO_ES:
262         {
263             WAVEFORMATEX *p_wf =
264                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
265
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 )
274             {
275                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
276             }
277             es->p_es->p_waveformatex = p_wf;
278
279             es->i_channel = p_sys->i_audio;
280             break;
281         }
282         case VIDEO_ES:
283         {
284             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
285                                               fmt->i_extra );
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;
289             p_bih->biPlanes         = 1;
290             p_bih->biBitCount       = 24;
291             p_bih->biCompression    = fmt->i_codec;
292             p_bih->biSizeImage      = fmt->video.i_width *
293                                           fmt->video.i_height;
294             p_bih->biXPelsPerMeter  = 0;
295             p_bih->biYPelsPerMeter  = 0;
296             p_bih->biClrUsed        = 0;
297             p_bih->biClrImportant   = 0;
298
299             if( fmt->i_extra > 0 )
300             {
301                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
302             }
303             es->p_es->p_bitmapinfoheader = p_bih;
304
305             es->i_channel = p_sys->i_video;
306             break;
307         }
308         case SPU_ES:
309         {
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 )
313             {
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';
318             }
319             /* FIXME beuuuuuurk */
320             es->p_es->p_demux_data = p_sub;
321
322             es->i_channel = p_sys->i_sub;
323             break;
324         }
325
326         default:
327             es->i_channel = 0;
328             break;
329     }
330
331     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
332     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
333     {
334         /* Add information */
335         switch( fmt->i_cat )
336         {
337             case AUDIO_ES:
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 )
342                 {
343                     input_AddInfo( p_cat, _("Channels"), "%d",
344                                    fmt->audio.i_channels );
345                 }
346                 if( fmt->audio.i_rate > 0 )
347                 {
348                     input_AddInfo( p_cat, _("Sample Rate"), "%d",
349                                    fmt->audio.i_rate );
350                 }
351                 if( fmt->i_bitrate > 0 )
352                 {
353                     input_AddInfo( p_cat, _("Bitrate"), "%d",
354                                    fmt->i_bitrate );
355                 }
356                 if( fmt->audio.i_bitspersample )
357                 {
358                     input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
359                                    fmt->audio.i_bitspersample );
360                 }
361                 break;
362             case VIDEO_ES:
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 )
367                 {
368                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
369                                    fmt->video.i_width, fmt->video.i_height );
370                 }
371                 if( fmt->video.i_visible_width > 0 &&
372                     fmt->video.i_visible_height > 0 )
373                 {
374                     input_AddInfo( p_cat, _("Display Resolution"), "%dx%d",
375                                    fmt->video.i_visible_width,
376                                    fmt->video.i_visible_height);
377                 }
378                 break;
379             case SPU_ES:
380                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
381                 input_AddInfo( p_cat, _("Codec"), "%.4s",
382                                (char*)&fmt->i_codec );
383                 break;
384             default:
385
386                 break;
387         }
388     }
389
390
391     /* Apply mode
392      * XXX change that when we do group too */
393     if( 1 )
394     {
395         EsOutSelect( out, es, VLC_FALSE );
396     }
397
398     vlc_mutex_unlock( &p_input->stream.stream_lock );
399
400     es->p_es->fmt = *fmt;
401
402     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
403     p_sys->i_id++;  /* always incremented */
404     switch( fmt->i_cat )
405     {
406         case AUDIO_ES:
407             p_sys->i_audio++;
408             break;
409         case SPU_ES:
410             p_sys->i_sub++;
411             break;
412         case VIDEO_ES:
413             p_sys->i_video++;
414             break;
415     }
416
417     return es;
418 }
419
420 /*****************************************************************************
421  * EsOutSend:
422  *****************************************************************************/
423 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
424 {
425     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
426     if( es->p_es->p_dec )
427     {
428         input_DecodeBlock( es->p_es->p_dec, p_block );
429     }
430     else
431     {
432         block_Release( p_block );
433     }
434     vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
435     return VLC_SUCCESS;
436 }
437
438 /*****************************************************************************
439  * EsOutDel:
440  *****************************************************************************/
441 static void EsOutDel( es_out_t *out, es_out_id_t *es )
442 {
443     es_out_sys_t *p_sys = out->p_sys;
444
445     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
446
447     switch( es->p_es->i_cat )
448     {
449         case AUDIO_ES:
450             p_sys->i_audio--;
451             break;
452         case SPU_ES:
453             p_sys->i_sub--;
454             break;
455         case VIDEO_ES:
456             p_sys->i_video--;
457             break;
458     }
459
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 )
463     {
464         input_UnselectES( p_sys->p_input, es->p_es );
465     }
466
467     if( es->p_es->p_waveformatex )
468     {
469         free( es->p_es->p_waveformatex );
470         es->p_es->p_waveformatex = NULL;
471     }
472     if( es->p_es->p_bitmapinfoheader )
473     {
474         free( es->p_es->p_bitmapinfoheader );
475         es->p_es->p_bitmapinfoheader = NULL;
476     }
477     input_DelES( p_sys->p_input, es->p_es );
478
479     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
480
481     free( es );
482 }
483
484 /*****************************************************************************
485  * EsOutControl:
486  *****************************************************************************/
487 static int EsOutControl( es_out_t *out, int i_query, va_list args )
488 {
489     es_out_sys_t *p_sys = out->p_sys;
490     vlc_bool_t  b, *pb;
491     int         i, *pi;
492
493     es_out_id_t *es;
494
495     switch( i_query )
496     {
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 )
502             {
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;
506             }
507             else if( !b && es->p_es->p_dec )
508             {
509                 input_UnselectES( p_sys->p_input, es->p_es );
510                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
511                 return VLC_SUCCESS;
512             }
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 * );
516
517             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
518             return VLC_SUCCESS;
519
520         case ES_OUT_SET_ACTIVE:
521             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
522             p_sys->b_active = b;
523             return VLC_SUCCESS;
524
525         case ES_OUT_GET_ACTIVE:
526             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
527             *pb = p_sys->b_active;
528             return VLC_SUCCESS;
529
530         case ES_OUT_SET_MODE:
531             i = (int) va_arg( args, int );
532             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL || i == ES_OUT_MODE_AUTO )
533             {
534                 p_sys->i_mode = i;
535
536                 /* Reapply policy mode */
537                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
538                 for( i = 0; i < p_sys->i_es; i++ )
539                 {
540                     if( p_sys->es[i]->p_es->p_dec )
541                     {
542                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
543                     }
544                 }
545                 for( i = 0; i < p_sys->i_es; i++ )
546                 {
547                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
548                 }
549                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
550                 return VLC_SUCCESS;
551             }
552             return VLC_EGENERIC;
553
554         case ES_OUT_GET_MODE:
555             pi = (int*) va_arg( args, int* );
556             *pi = p_sys->i_mode;
557             return VLC_SUCCESS;
558
559         case ES_OUT_SET_ES:
560             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
561             if( es == NULL )
562             {
563                 for( i = 0; i < p_sys->i_es; i++ )
564                 {
565                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
566                     if( p_sys->es[i]->p_es->p_dec )
567                     {
568                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
569                     }
570                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
571                 }
572             }
573             else
574             {
575                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
576                 EsOutSelect( out, es, VLC_TRUE );
577                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
578             }
579             return VLC_SUCCESS;
580
581         default:
582             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
583             return VLC_EGENERIC;
584     }
585 }
586
587
588
589