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