]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* src/input/es_out.c: cosmetic changes.
[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.8 2003/12/07 17:17:04 gbazin 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     vlc_bool_t      b_pcr_set;
48
49     /* all es */
50     int         i_id;
51
52     int         i_es;
53     es_out_id_t **es;
54
55     /* mode gestion */
56     vlc_bool_t  b_active;
57     int         i_mode;
58
59     /* es count */
60     int         i_audio;
61     int         i_video;
62     int         i_sub;
63
64     /* es to select */
65     int         i_audio_last;
66     int         i_sub_last;
67
68     /* current main es */
69     es_out_id_t *p_es_audio;
70     es_out_id_t *p_es_video;
71     es_out_id_t *p_es_sub;
72 };
73
74 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
75 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
76 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
77 static int          EsOutControl( es_out_t *, int i_query, va_list );
78
79
80 /*****************************************************************************
81  * input_EsOutNew:
82  *****************************************************************************/
83 es_out_t *input_EsOutNew( input_thread_t *p_input )
84 {
85     es_out_t     *out = malloc( sizeof( es_out_t ) );
86     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
87     vlc_value_t  val;
88
89     out->pf_add     = EsOutAdd;
90     out->pf_send    = EsOutSend;
91     out->pf_del     = EsOutDel;
92     out->pf_control = EsOutControl;
93     out->p_sys      = p_sys;
94
95     p_sys->p_input = p_input;
96     p_sys->b_pcr_set = VLC_FALSE;
97
98     p_sys->b_active = VLC_FALSE;
99     p_sys->i_mode   = ES_OUT_MODE_AUTO;
100
101     p_sys->i_id    = 1;
102
103     p_sys->i_es    = 0;
104     p_sys->es      = NULL;
105
106     p_sys->i_audio = 0;
107     p_sys->i_video = 0;
108     p_sys->i_sub   = 0;
109
110     var_Get( p_input, "audio-channel", &val );
111     p_sys->i_audio_last = val.i_int;
112
113     var_Get( p_input, "spu-channel", &val );
114     p_sys->i_sub_last = val.i_int;
115
116     p_sys->p_es_audio = NULL;
117     p_sys->p_es_video = NULL;
118     p_sys->p_es_sub   = NULL;
119
120     return out;
121 }
122
123 /*****************************************************************************
124  * input_EsOutDelete:
125  *****************************************************************************/
126 void input_EsOutDelete( es_out_t *out )
127 {
128     es_out_sys_t *p_sys = out->p_sys;
129     int i;
130
131     for( i = 0; i < p_sys->i_es; i++ )
132     {
133         free( p_sys->es[i] );
134     }
135     if( p_sys->es )
136     {
137         free( p_sys->es );
138     }
139     free( p_sys );
140     free( out );
141 }
142
143 /*****************************************************************************
144  * EsOutSelect: Select an ES given the current mode
145  * XXX: you need to take a the lock before (stream.stream_lock)
146  *****************************************************************************/
147 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
148 {
149     es_out_sys_t      *p_sys = out->p_sys;
150     input_thread_t    *p_input = p_sys->p_input;
151
152     int i_cat = es->p_es->i_cat;
153
154     if( !p_sys->b_active || ( !b_force && es->p_es->fmt.i_priority < 0 ) )
155     {
156         return;
157     }
158
159     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
160     {
161         input_SelectES( p_input, es->p_es );
162     }
163     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
164     {
165         int i_wanted  = -1;
166
167         if( i_cat == AUDIO_ES )
168         {
169             if( p_sys->p_es_audio &&
170                 p_sys->p_es_audio->p_es->fmt.i_priority >=
171                     es->p_es->fmt.i_priority )
172             {
173                 return;
174             }
175             i_wanted  = p_sys->i_audio_last >= 0 ?
176                             p_sys->i_audio_last : es->i_channel;
177         }
178         else if( i_cat == SPU_ES )
179         {
180             if( p_sys->p_es_sub &&
181                 p_sys->p_es_sub->p_es->fmt.i_priority >=
182                     es->p_es->fmt.i_priority )
183             {
184                 return;
185             }
186             i_wanted  = p_sys->i_sub_last;
187         }
188         else if( i_cat == VIDEO_ES )
189         {
190             i_wanted  = es->i_channel;
191         }
192
193         if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
194         {
195             input_SelectES( p_input, es->p_es );
196         }
197     }
198
199     /* FIXME TODO handle priority here */
200     if( es->p_es->p_dec )
201     {
202         if( i_cat == AUDIO_ES )
203         {
204             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
205                 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
206             {
207                 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
208             }
209             p_sys->p_es_audio = es;
210         }
211         else if( i_cat == SPU_ES )
212         {
213             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
214                 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
215             {
216                 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
217             }
218             p_sys->p_es_sub = es;
219         }
220         else if( i_cat == VIDEO_ES )
221         {
222             p_sys->p_es_video = es;
223         }
224     }
225 }
226
227 /*****************************************************************************
228  * EsOutAdd:
229  *****************************************************************************/
230 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
231 {
232     es_out_sys_t      *p_sys = out->p_sys;
233     input_thread_t    *p_input = p_sys->p_input;
234     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
235     pgrm_descriptor_t *p_prgm = NULL;
236     char              psz_cat[sizeof( "Stream " ) + 10];
237     input_info_category_t *p_cat;
238
239     vlc_mutex_lock( &p_input->stream.stream_lock );
240     if( fmt->i_group >= 0 )
241     {
242         /* search program */
243         p_prgm = input_FindProgram( p_input, fmt->i_group );
244
245         if( p_prgm == NULL )
246         {
247             /* create it */
248             p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
249
250             /* Select the first by default */
251             if( p_input->stream.p_selected_program == NULL )
252             {
253                 p_input->stream.p_selected_program = p_prgm;
254             }
255         }
256     }
257
258     es->p_es = input_AddES( p_input,
259                             p_prgm,
260                             out->p_sys->i_id,
261                             fmt->i_cat,
262                             fmt->psz_language, 0 );
263     es->p_es->i_stream_id = out->p_sys->i_id;
264     es->p_es->i_fourcc = fmt->i_codec;
265
266     switch( fmt->i_cat )
267     {
268         case AUDIO_ES:
269         {
270             WAVEFORMATEX *p_wf =
271                 malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
272
273             p_wf->wFormatTag        = WAVE_FORMAT_UNKNOWN;
274             p_wf->nChannels         = fmt->audio.i_channels;
275             p_wf->nSamplesPerSec    = fmt->audio.i_rate;
276             p_wf->nAvgBytesPerSec   = fmt->i_bitrate / 8;
277             p_wf->nBlockAlign       = fmt->audio.i_blockalign;
278             p_wf->wBitsPerSample    = fmt->audio.i_bitspersample;
279             p_wf->cbSize            = fmt->i_extra;
280             if( fmt->i_extra > 0 )
281             {
282                 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
283             }
284             es->p_es->p_waveformatex = p_wf;
285
286             es->i_channel = p_sys->i_audio;
287             break;
288         }
289         case VIDEO_ES:
290         {
291             BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
292                                               fmt->i_extra );
293             p_bih->biSize           = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
294             p_bih->biWidth          = fmt->video.i_width;
295             p_bih->biHeight         = fmt->video.i_height;
296             p_bih->biPlanes         = 1;
297             p_bih->biBitCount       = 24;
298             p_bih->biCompression    = fmt->i_codec;
299             p_bih->biSizeImage      = fmt->video.i_width *
300                                           fmt->video.i_height;
301             p_bih->biXPelsPerMeter  = 0;
302             p_bih->biYPelsPerMeter  = 0;
303             p_bih->biClrUsed        = 0;
304             p_bih->biClrImportant   = 0;
305
306             if( fmt->i_extra > 0 )
307             {
308                 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
309             }
310             es->p_es->p_bitmapinfoheader = p_bih;
311
312             es->i_channel = p_sys->i_video;
313             break;
314         }
315         case SPU_ES:
316         {
317             subtitle_data_t *p_sub = malloc( sizeof( subtitle_data_t ) );
318             memset( p_sub, 0, sizeof( subtitle_data_t ) );
319             if( fmt->i_extra > 0 )
320             {
321                 p_sub->psz_header = malloc( fmt->i_extra  + 1 );
322                 memcpy( p_sub->psz_header, fmt->p_extra , fmt->i_extra );
323                 /* just to be sure */
324                 ((uint8_t*)fmt->p_extra)[fmt->i_extra] = '\0';
325             }
326             /* FIXME beuuuuuurk */
327             es->p_es->p_demux_data = p_sub;
328
329             es->i_channel = p_sys->i_sub;
330             break;
331         }
332
333         default:
334             es->i_channel = 0;
335             break;
336     }
337
338     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
339     if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
340     {
341         /* Add information */
342         switch( fmt->i_cat )
343         {
344             case AUDIO_ES:
345                 if( fmt->psz_description )
346                 {
347                     input_AddInfo( p_cat, _("Description"), "%s", 
348                                    fmt->psz_description );
349                 }
350                 input_AddInfo( p_cat, _("Codec"), "%.4s",
351                                (char*)&fmt->i_codec );
352                 input_AddInfo( p_cat, _("Type"), _("Audio") );
353                 if( fmt->audio.i_channels > 0 )
354                 {
355                     input_AddInfo( p_cat, _("Channels"), "%d",
356                                    fmt->audio.i_channels );
357                 }
358                 if( fmt->psz_language )
359                 {
360                     input_AddInfo( p_cat, _("Language"), "%s", 
361                                    fmt->psz_language );
362                 }
363                 if( fmt->audio.i_rate > 0 )
364                 {
365                   input_AddInfo( p_cat, _("Sample Rate"), _("%d Hz"),
366                                    fmt->audio.i_rate );
367                 }
368                 if( fmt->i_bitrate > 0 )
369                 {
370                   input_AddInfo( p_cat, _("Bitrate"), _("%d bps"), 
371                                    fmt->i_bitrate );
372                 }
373                 if( fmt->audio.i_bitspersample )
374                 {
375                     input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
376                                    fmt->audio.i_bitspersample );
377                 }
378                 break;
379             case VIDEO_ES:
380                 if( fmt->psz_description )
381                 {
382                     input_AddInfo( p_cat, _("Description"), "%s", 
383                                    fmt->psz_description );
384                 }
385                 input_AddInfo( p_cat, _("Type"), _("Video") );
386                 input_AddInfo( p_cat, _("Codec"), "%.4s",
387                                (char*)&fmt->i_codec );
388                 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
389                 {
390                     input_AddInfo( p_cat, _("Resolution"), "%dx%d",
391                                    fmt->video.i_width, fmt->video.i_height );
392                 }
393                 if( fmt->video.i_visible_width > 0 &&
394                     fmt->video.i_visible_height > 0 )
395                 {
396                     input_AddInfo( p_cat, _("Display Resolution"), "%dx%d",
397                                    fmt->video.i_visible_width,
398                                    fmt->video.i_visible_height);
399                 }
400                 break;
401             case SPU_ES:
402                 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
403                 input_AddInfo( p_cat, _("Codec"), "%.4s",
404                                (char*)&fmt->i_codec );
405                 break;
406             default:
407
408                 break;
409         }
410     }
411
412
413     /* Apply mode
414      * XXX change that when we do group too */
415     if( 1 )
416     {
417         EsOutSelect( out, es, VLC_FALSE );
418     }
419
420     vlc_mutex_unlock( &p_input->stream.stream_lock );
421
422     es->p_es->fmt = *fmt;
423
424     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
425     p_sys->i_id++;  /* always incremented */
426     switch( fmt->i_cat )
427     {
428         case AUDIO_ES:
429             p_sys->i_audio++;
430             break;
431         case SPU_ES:
432             p_sys->i_sub++;
433             break;
434         case VIDEO_ES:
435             p_sys->i_video++;
436             break;
437     }
438
439     return es;
440 }
441
442 /*****************************************************************************
443  * EsOutSend:
444  *****************************************************************************/
445 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
446 {
447     es_out_sys_t *p_sys = out->p_sys;
448
449     if( p_sys->b_pcr_set && p_sys->p_input->stream.p_selected_program )
450     {
451         input_thread_t *p_input = p_sys->p_input;
452
453         if( p_block->i_dts > 0 )
454         {
455             p_block->i_dts =
456                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
457                                   p_block->i_dts * 9 / 100 );
458         }
459         if( p_block->i_pts > 0 )
460         {
461             p_block->i_pts =
462                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
463                                   p_block->i_pts * 9 / 100 );
464         }
465     }
466
467     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
468     if( es->p_es->p_dec )
469     {
470         input_DecodeBlock( es->p_es->p_dec, p_block );
471     }
472     else
473     {
474         block_Release( p_block );
475     }
476     vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
477
478     return VLC_SUCCESS;
479 }
480
481 /*****************************************************************************
482  * EsOutDel:
483  *****************************************************************************/
484 static void EsOutDel( es_out_t *out, es_out_id_t *es )
485 {
486     es_out_sys_t *p_sys = out->p_sys;
487
488     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
489
490     switch( es->p_es->i_cat )
491     {
492         case AUDIO_ES:
493             p_sys->i_audio--;
494             break;
495         case SPU_ES:
496             p_sys->i_sub--;
497             break;
498         case VIDEO_ES:
499             p_sys->i_video--;
500             break;
501     }
502
503     /* We don't try to reselect */
504     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
505     if( es->p_es->p_dec )
506     {
507         input_UnselectES( p_sys->p_input, es->p_es );
508     }
509
510     if( es->p_es->p_waveformatex )
511     {
512         free( es->p_es->p_waveformatex );
513         es->p_es->p_waveformatex = NULL;
514     }
515     if( es->p_es->p_bitmapinfoheader )
516     {
517         free( es->p_es->p_bitmapinfoheader );
518         es->p_es->p_bitmapinfoheader = NULL;
519     }
520     input_DelES( p_sys->p_input, es->p_es );
521
522     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
523
524     free( es );
525 }
526
527 /*****************************************************************************
528  * EsOutControl:
529  *****************************************************************************/
530 static int EsOutControl( es_out_t *out, int i_query, va_list args )
531 {
532     es_out_sys_t *p_sys = out->p_sys;
533     vlc_bool_t  b, *pb;
534     int         i, *pi;
535     int         i_group;
536     int64_t     i_pcr;
537
538     es_out_id_t *es;
539
540     switch( i_query )
541     {
542         case ES_OUT_SET_ES_STATE:
543             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
544             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
545             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
546             if( b && es->p_es->p_dec == NULL )
547             {
548                 input_SelectES( p_sys->p_input, es->p_es );
549                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
550                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
551             }
552             else if( !b && es->p_es->p_dec )
553             {
554                 input_UnselectES( p_sys->p_input, es->p_es );
555                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
556                 return VLC_SUCCESS;
557             }
558             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
559             return VLC_SUCCESS;
560
561         case ES_OUT_GET_ES_STATE:
562             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
563             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
564
565             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
566             return VLC_SUCCESS;
567
568         case ES_OUT_SET_ACTIVE:
569         {
570             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
571             p_sys->b_active = b;
572
573             if( b )
574             {
575                 vlc_value_t val;
576                 val.b_bool = VLC_TRUE;
577                 var_Set( p_sys->p_input, "intf-change", val );
578             }
579             return VLC_SUCCESS;
580         }
581
582         case ES_OUT_GET_ACTIVE:
583             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
584             *pb = p_sys->b_active;
585             return VLC_SUCCESS;
586
587         case ES_OUT_SET_MODE:
588             i = (int) va_arg( args, int );
589             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
590                 i == ES_OUT_MODE_AUTO )
591             {
592                 vlc_value_t val;
593
594                 p_sys->i_mode = i;
595
596                 /* Reapply policy mode */
597                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
598                 for( i = 0; i < p_sys->i_es; i++ )
599                 {
600                     if( p_sys->es[i]->p_es->p_dec )
601                     {
602                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
603                     }
604                 }
605                 for( i = 0; i < p_sys->i_es; i++ )
606                 {
607                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
608                 }
609                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
610
611                 val.b_bool = VLC_TRUE;
612                 var_Set( p_sys->p_input, "intf-change", val );
613
614                 return VLC_SUCCESS;
615             }
616             return VLC_EGENERIC;
617
618         case ES_OUT_GET_MODE:
619             pi = (int*) va_arg( args, int* );
620             *pi = p_sys->i_mode;
621             return VLC_SUCCESS;
622
623         case ES_OUT_SET_ES:
624             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
625             if( es == NULL )
626             {
627                 for( i = 0; i < p_sys->i_es; i++ )
628                 {
629                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
630                     if( p_sys->es[i]->p_es->p_dec )
631                     {
632                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
633                     }
634                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
635                 }
636             }
637             else
638             {
639                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
640                 EsOutSelect( out, es, VLC_TRUE );
641                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
642             }
643             return VLC_SUCCESS;
644
645         case ES_OUT_SET_PCR:
646         {
647             pgrm_descriptor_t *p_prgm = NULL;
648
649             i_group = (int)va_arg( args, int );
650             i_pcr   = (int64_t)va_arg( args, int64_t );
651
652             /* search program */
653             if( ( p_prgm = input_FindProgram( p_sys->p_input, i_group ) ) )
654             {
655                 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr );
656             }
657             p_sys->b_pcr_set = VLC_TRUE;
658             return VLC_SUCCESS;
659         }
660
661         case ES_OUT_RESET_PCR:
662             /* FIXME do it for all program */
663             if( p_sys->p_input->stream.p_selected_program )
664             {
665                 p_sys->p_input->stream.p_selected_program->i_synchro_state =
666                     SYNCHRO_REINIT;
667             }
668             p_sys->b_pcr_set = VLC_TRUE;
669             return VLC_SUCCESS;
670
671         default:
672             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
673             return VLC_EGENERIC;
674     }
675 }