]> git.sesse.net Git - vlc/blob - src/input/es_out.c
* toolbox:
[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.10 2003/12/22 02:24:50 sam 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     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
523     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
524     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
525
526     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
527
528     free( es );
529 }
530
531 /*****************************************************************************
532  * EsOutControl:
533  *****************************************************************************/
534 static int EsOutControl( es_out_t *out, int i_query, va_list args )
535 {
536     es_out_sys_t *p_sys = out->p_sys;
537     vlc_bool_t  b, *pb;
538     int         i, *pi;
539     int         i_group;
540     int64_t     i_pcr;
541
542     es_out_id_t *es;
543
544     switch( i_query )
545     {
546         case ES_OUT_SET_ES_STATE:
547             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
548             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
549             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
550             if( b && es->p_es->p_dec == NULL )
551             {
552                 input_SelectES( p_sys->p_input, es->p_es );
553                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
554                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
555             }
556             else if( !b && es->p_es->p_dec )
557             {
558                 input_UnselectES( p_sys->p_input, es->p_es );
559                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
560                 return VLC_SUCCESS;
561             }
562             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
563             return VLC_SUCCESS;
564
565         case ES_OUT_GET_ES_STATE:
566             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
567             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
568
569             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
570             return VLC_SUCCESS;
571
572         case ES_OUT_SET_ACTIVE:
573         {
574             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
575             p_sys->b_active = b;
576
577             if( b )
578             {
579                 vlc_value_t val;
580                 val.b_bool = VLC_TRUE;
581                 var_Set( p_sys->p_input, "intf-change", val );
582             }
583             return VLC_SUCCESS;
584         }
585
586         case ES_OUT_GET_ACTIVE:
587             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
588             *pb = p_sys->b_active;
589             return VLC_SUCCESS;
590
591         case ES_OUT_SET_MODE:
592             i = (int) va_arg( args, int );
593             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
594                 i == ES_OUT_MODE_AUTO )
595             {
596                 vlc_value_t val;
597
598                 p_sys->i_mode = i;
599
600                 /* Reapply policy mode */
601                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
602                 for( i = 0; i < p_sys->i_es; i++ )
603                 {
604                     if( p_sys->es[i]->p_es->p_dec )
605                     {
606                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
607                     }
608                 }
609                 for( i = 0; i < p_sys->i_es; i++ )
610                 {
611                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
612                 }
613                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
614
615                 val.b_bool = VLC_TRUE;
616                 var_Set( p_sys->p_input, "intf-change", val );
617
618                 return VLC_SUCCESS;
619             }
620             return VLC_EGENERIC;
621
622         case ES_OUT_GET_MODE:
623             pi = (int*) va_arg( args, int* );
624             *pi = p_sys->i_mode;
625             return VLC_SUCCESS;
626
627         case ES_OUT_SET_ES:
628             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
629             if( es == NULL )
630             {
631                 for( i = 0; i < p_sys->i_es; i++ )
632                 {
633                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
634                     if( p_sys->es[i]->p_es->p_dec )
635                     {
636                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
637                     }
638                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
639                 }
640             }
641             else
642             {
643                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
644                 EsOutSelect( out, es, VLC_TRUE );
645                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
646             }
647             return VLC_SUCCESS;
648
649         case ES_OUT_SET_PCR:
650         {
651             pgrm_descriptor_t *p_prgm = NULL;
652
653             i_group = (int)va_arg( args, int );
654             i_pcr   = (int64_t)va_arg( args, int64_t );
655
656             /* search program */
657             if( ( p_prgm = input_FindProgram( p_sys->p_input, i_group ) ) )
658             {
659                 input_ClockManageRef( p_sys->p_input, p_prgm, i_pcr );
660             }
661             p_sys->b_pcr_set = VLC_TRUE;
662             return VLC_SUCCESS;
663         }
664
665         case ES_OUT_RESET_PCR:
666             /* FIXME do it for all program */
667             if( p_sys->p_input->stream.p_selected_program )
668             {
669                 p_sys->p_input->stream.p_selected_program->i_synchro_state =
670                     SYNCHRO_REINIT;
671             }
672             p_sys->b_pcr_set = VLC_TRUE;
673             return VLC_SUCCESS;
674
675         default:
676             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
677             return VLC_EGENERIC;
678     }
679 }