es_out.c 29 KB
Newer Older
1
2
3
/*****************************************************************************
 * es_out.c: Es Out handler for input.
 *****************************************************************************
zorglub's avatar
zorglub committed
4
 * Copyright (C) 2003-2004 VideoLAN
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/decoder.h>

Laurent Aimar's avatar
Laurent Aimar committed
33
34
#include "input_internal.h"

zorglub's avatar
zorglub committed
35
#include "vlc_playlist.h"
36
#include "iso_lang.h"
37
38
39
40

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
typedef struct
{
    /* Program ID */
    int i_id;

    /* Number of es for this pgrm */
    int i_es;

    vlc_bool_t b_selected;

    /* Clock for this program */
    input_clock_t clock;

} es_out_pgrm_t;

56
57
struct es_out_id_t
{
Laurent Aimar's avatar
Laurent Aimar committed
58
59
60
61
62
63
64
65
66
    /* ES ID */
    int       i_id;
    es_out_pgrm_t *p_pgrm;

    /* Channel in the track type */
    int         i_channel;
    es_format_t fmt;
    char        *psz_description;
    decoder_t   *p_dec;
67
68
};

69
70
71
72
struct es_out_sys_t
{
    input_thread_t *p_input;

Laurent Aimar's avatar
Laurent Aimar committed
73
74
75
76
77
    /* all programs */
    int           i_pgrm;
    es_out_pgrm_t **pgrm;
    es_out_pgrm_t *p_pgrm;  /* Master program */

78
    /* all es */
79
    int         i_id;
80
81
    int         i_es;
    es_out_id_t **es;
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    /* mode gestion */
    vlc_bool_t  b_active;
    int         i_mode;

    /* es count */
    int         i_audio;
    int         i_video;
    int         i_sub;

    /* es to select */
    int         i_audio_last;
    int         i_sub_last;

    /* current main es */
    es_out_id_t *p_es_audio;
    es_out_id_t *p_es_video;
    es_out_id_t *p_es_sub;
100
101
102
103

    /* delay */
    int64_t i_audio_delay;
    int64_t i_spu_delay;
104
105
106
107
108
};

static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
static void         EsOutDel    ( es_out_t *, es_out_id_t * );
Laurent Aimar's avatar
Laurent Aimar committed
109
static void         EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
110
111
static int          EsOutControl( es_out_t *, int i_query, va_list );

Laurent Aimar's avatar
Laurent Aimar committed
112
113
114

static void EsSelect( es_out_t *out, es_out_id_t *es );
static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update );
115
static char *LanguageGetName( const char *psz_code );
116

Laurent Aimar's avatar
Laurent Aimar committed
117
118
119
/*****************************************************************************
 * input_EsOutNew:
 *****************************************************************************/
120
121
es_out_t *input_EsOutNew( input_thread_t *p_input )
{
122
123
124
    es_out_t     *out = malloc( sizeof( es_out_t ) );
    es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
    vlc_value_t  val;
125
126
127
128
129

    out->pf_add     = EsOutAdd;
    out->pf_send    = EsOutSend;
    out->pf_del     = EsOutDel;
    out->pf_control = EsOutControl;
130
131
132
133
134
135
136
137
    out->p_sys      = p_sys;

    p_sys->p_input = p_input;

    p_sys->b_active = VLC_FALSE;
    p_sys->i_mode   = ES_OUT_MODE_AUTO;


Laurent Aimar's avatar
Laurent Aimar committed
138
139
140
141
142
    p_sys->i_pgrm   = 0;
    p_sys->pgrm     = NULL;
    p_sys->p_pgrm   = NULL;

    p_sys->i_id    = 0;
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    p_sys->i_es    = 0;
    p_sys->es      = NULL;

    p_sys->i_audio = 0;
    p_sys->i_video = 0;
    p_sys->i_sub   = 0;

    var_Get( p_input, "audio-channel", &val );
    p_sys->i_audio_last = val.i_int;

    var_Get( p_input, "spu-channel", &val );
    p_sys->i_sub_last = val.i_int;

    p_sys->p_es_audio = NULL;
    p_sys->p_es_video = NULL;
    p_sys->p_es_sub   = NULL;
159

160
161
162
    p_sys->i_audio_delay= 0;
    p_sys->i_spu_delay  = 0;

163
164
165
    return out;
}

Laurent Aimar's avatar
Laurent Aimar committed
166
167
168
/*****************************************************************************
 * input_EsOutDelete:
 *****************************************************************************/
169
170
171
172
173
void input_EsOutDelete( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
    int i;

174
    for( i = 0; i < p_sys->i_es; i++ )
175
    {
Laurent Aimar's avatar
Laurent Aimar committed
176
177
178
179
180
181
182
183
        if( p_sys->es[i]->p_dec )
        {
            input_DecoderDelete( p_sys->es[i]->p_dec );
        }
        if( p_sys->es[i]->psz_description )
            free( p_sys->es[i]->psz_description );
        es_format_Clean( &p_sys->es[i]->fmt );

184
        free( p_sys->es[i] );
185
    }
186
187
    if( p_sys->es )
        free( p_sys->es );
Laurent Aimar's avatar
Laurent Aimar committed
188
189
190
191

    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        free( p_sys->pgrm[i] );
192
    }
Laurent Aimar's avatar
Laurent Aimar committed
193
194
195
    if( p_sys->pgrm )
        free( p_sys->pgrm );

196
197
198
    free( p_sys );
    free( out );
}
zorglub's avatar
zorglub committed
199

Laurent Aimar's avatar
Laurent Aimar committed
200
es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
201
{
Laurent Aimar's avatar
Laurent Aimar committed
202
203
204
205
206
207
    int i;
    if( i_id < 0 )
    {
        /* Special HACK, -i_id is tha cat of the stream */
        return (es_out_id_t*)((uint8_t*)NULL-i_id);
    }
208

Laurent Aimar's avatar
Laurent Aimar committed
209
210
211
212
213
214
215
    for( i = 0; i < out->p_sys->i_es; i++ )
    {
        if( out->p_sys->es[i]->i_id == i_id )
            return out->p_sys->es[i];
    }
    return NULL;
}
216

Laurent Aimar's avatar
Laurent Aimar committed
217
218
219
220
void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;
221

Laurent Aimar's avatar
Laurent Aimar committed
222
    for( i = 0; i < p_sys->i_es; i++ )
223
    {
Laurent Aimar's avatar
Laurent Aimar committed
224
        es_out_id_t *es = p_sys->es[i];
225

Laurent Aimar's avatar
Laurent Aimar committed
226
227
228
229
230
231
232
        /* Send a dummy block to let decoder know that
         * there is a discontinuity */
        if( es->p_dec && ( !b_audio || es->fmt.i_cat == AUDIO_ES ) )
        {
            input_DecoderDiscontinuity( es->p_dec );
        }
    }
233
}
234

235
236
237
238
239
240
241
242
243
244
void input_EsOutSetDelay( es_out_t *out, int i_cat, int64_t i_delay )
{
    es_out_sys_t *p_sys = out->p_sys;

    if( i_cat == AUDIO_ES )
        p_sys->i_audio_delay = i_delay;
    else if( i_cat == SPU_ES )
        p_sys->i_spu_delay = i_delay;
}

Laurent Aimar's avatar
Laurent Aimar committed
245
/*****************************************************************************
zorglub's avatar
zorglub committed
246
 *
Laurent Aimar's avatar
Laurent Aimar committed
247
248
 *****************************************************************************/
static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es )
249
250
251
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
Laurent Aimar's avatar
Laurent Aimar committed
252
    vlc_value_t       val, text;
253

Laurent Aimar's avatar
Laurent Aimar committed
254
    char *psz_var;
255

Laurent Aimar's avatar
Laurent Aimar committed
256
257
258
259
260
261
262
    if( es->fmt.i_cat == AUDIO_ES )
        psz_var = "audio-es";
    else if( es->fmt.i_cat == VIDEO_ES )
        psz_var = "video-es";
    else if( es->fmt.i_cat == SPU_ES )
        psz_var = "spu-es";
    else
263
        return;
Laurent Aimar's avatar
Laurent Aimar committed
264
265
266
267
268
269
270
271
272
273
274

    /* Get the number of ES already added */
    var_Change( p_input, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
    if( val.i_int == 0 )
    {
        vlc_value_t val2;

        /* First one, we need to add the "Disable" choice */
        val2.i_int = -1; text.psz_string = _("Disable");
        var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val2, &text );
        val.i_int++;
275
276
    }

Laurent Aimar's avatar
Laurent Aimar committed
277
278
    /* Take care of the ES description */
    if( es->psz_description && *es->psz_description )
279
    {
Laurent Aimar's avatar
Laurent Aimar committed
280
        text.psz_string = strdup( es->psz_description );
281
    }
Laurent Aimar's avatar
Laurent Aimar committed
282
    else
283
    {
Laurent Aimar's avatar
Laurent Aimar committed
284
285
286
        text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
        sprintf( text.psz_string, _("Track %i"), val.i_int );
    }
287

Laurent Aimar's avatar
Laurent Aimar committed
288
289
    val.i_int = es->i_id;
    var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );
290

Laurent Aimar's avatar
Laurent Aimar committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    free( text.psz_string );

    var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
}

/* EsOutProgramSelect:
 *  Select a program and update the object variable
 */
static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    vlc_value_t       val;
    int               i;

    if( p_sys->p_pgrm == p_pgrm )
        return; /* Nothing to do */

    if( p_sys->p_pgrm )
    {
        es_out_pgrm_t *old = p_sys->p_pgrm;
        msg_Dbg( p_input, "Unselecting program id=%d", old->i_id );
313

Laurent Aimar's avatar
Laurent Aimar committed
314
        for( i = 0; i < p_sys->i_es; i++ )
315
        {
Laurent Aimar's avatar
Laurent Aimar committed
316
317
318
            if( p_sys->es[i]->p_pgrm == old && p_sys->es[i]->p_dec &&
                p_sys->i_mode != ES_OUT_MODE_ALL )
                EsUnselect( out, p_sys->es[i], VLC_TRUE );
319
320
321
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
322
323
324
325
326
327
328
    msg_Dbg( p_input, "Selecting program id=%d", p_pgrm->i_id );

    /* Mark it selected */
    p_pgrm->b_selected = VLC_TRUE;

    /* Switch master stream */
    if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
329
    {
Laurent Aimar's avatar
Laurent Aimar committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
        p_sys->p_pgrm->clock.b_master = VLC_FALSE;
    }
    p_pgrm->clock.b_master = VLC_TRUE;
    p_sys->p_pgrm = p_pgrm;

    /* Update "program" */
    val.i_int = p_pgrm->i_id;
    var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );

    /* Update "es-*" */
    var_Change( p_input, "audio-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
    var_Change( p_input, "video-es", VLC_VAR_CLEARCHOICES, NULL, NULL );
    var_Change( p_input, "spu-es",   VLC_VAR_CLEARCHOICES, NULL, NULL );
    for( i = 0; i < p_sys->i_es; i++ )
    {
        EsOutESVarUpdate( out, p_sys->es[i] );
        EsOutSelect( out, p_sys->es[i], VLC_FALSE );
    }

    var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
}

/* EsOutAddProgram:
 *  Add a program
 */
static es_out_pgrm_t *EsOutProgramAdd( es_out_t *out, int i_group )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    vlc_value_t       val;

    es_out_pgrm_t *p_pgrm = malloc( sizeof( es_out_pgrm_t ) );

    /* Init */
    p_pgrm->i_id = i_group;
    p_pgrm->i_es = 0;
    p_pgrm->b_selected = VLC_FALSE;
    input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->input.i_cr_average );

    /* Append it */
    TAB_APPEND( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );

    /* Update "program" variable */
    val.i_int = i_group;
    var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, NULL );

    if( i_group == var_GetInteger( p_input, "program" ) )
    {
        EsOutProgramSelect( out, p_pgrm );
    }
    else
    {
        var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
383
    }
Laurent Aimar's avatar
Laurent Aimar committed
384
    return p_pgrm;
385
386
}

Laurent Aimar's avatar
Laurent Aimar committed
387
388
/* EsOutAdd:
 *  Add an es_out
zorglub's avatar
zorglub committed
389
 */
390
391
392
393
static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
Laurent Aimar's avatar
Laurent Aimar committed
394

395
    es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
Laurent Aimar's avatar
Laurent Aimar committed
396
397
    es_out_pgrm_t     *p_pgrm = NULL;
    int i;
398

Laurent Aimar's avatar
Laurent Aimar committed
399
    if( fmt->i_group < 0 )
400
    {
Laurent Aimar's avatar
Laurent Aimar committed
401
402
403
        msg_Err( p_input, "invakud group number" );
        return NULL;
    }
404

Laurent Aimar's avatar
Laurent Aimar committed
405
406
407
408
    /* Search the program */
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( fmt->i_group == p_sys->pgrm[i]->i_id )
409
        {
Laurent Aimar's avatar
Laurent Aimar committed
410
411
            p_pgrm = p_sys->pgrm[i];
            break;
412
413
        }
    }
Laurent Aimar's avatar
Laurent Aimar committed
414
    if( p_pgrm == NULL )
415
    {
Laurent Aimar's avatar
Laurent Aimar committed
416
417
        /* Create a new one */
        p_pgrm = EsOutProgramAdd( out, fmt->i_group );
418
    }
419

Laurent Aimar's avatar
Laurent Aimar committed
420
421
    /* Increase ref count for program */
    p_pgrm->i_es++;
422

Laurent Aimar's avatar
Laurent Aimar committed
423
424
425
426
427
428
    /* Set up ES */
    if( fmt->i_id < 0 )
        fmt->i_id = out->p_sys->i_id;
    es->i_id = fmt->i_id;
    es->p_pgrm = p_pgrm;
    es_format_Copy( &es->fmt, fmt );
429
430
    switch( fmt->i_cat )
    {
431
432
433
    case AUDIO_ES:
        es->i_channel = p_sys->i_audio;
        break;
434

435
436
437
    case VIDEO_ES:
        es->i_channel = p_sys->i_video;
        break;
438

439
    case SPU_ES:
440
441
        es->i_channel = p_sys->i_sub;
        break;
442

443
444
445
    default:
        es->i_channel = 0;
        break;
446
    }
Laurent Aimar's avatar
Laurent Aimar committed
447
448
449
450
451
    es->psz_description = LanguageGetName( fmt->psz_language );
    es->p_dec = NULL;

    if( es->p_pgrm == p_sys->p_pgrm )
        EsOutESVarUpdate( out, es );
452

Laurent Aimar's avatar
Laurent Aimar committed
453
#if 0
454
    /* Add stream info */
455
    sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
456
457
458
459
460
461
462
463
464
465
466
467
468
469

    input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
                   "%.4s", (char*)&fmt->i_codec );

    if( *psz_description )
        input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
                       "%s", psz_description );

    if( fmt->psz_description && *fmt->psz_description )
        input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
                       "%s", fmt->psz_description );

    /* Add information */
    switch( fmt->i_cat )
470
    {
471
472
473
    case AUDIO_ES:
        input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                       _("Type"), _("Audio") );
zorglub's avatar
zorglub committed
474

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
        if( fmt->audio.i_channels > 0 )
            input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
                           "%d", fmt->audio.i_channels );

        if( fmt->audio.i_rate > 0 )
            input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
                           _("%d Hz"), fmt->audio.i_rate );

        if( fmt->audio.i_bitspersample > 0 )
            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                           _("Bits per sample"), "%d",
                           fmt->audio.i_bitspersample );

        if( fmt->i_bitrate > 0 )
            input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
                           _("%d bps"), fmt->i_bitrate );
        break;

    case VIDEO_ES:
        input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                       _("Type"), _("Video") );

        if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                           _("Resolution"), "%dx%d",
                           fmt->video.i_width, fmt->video.i_height );

        if( fmt->video.i_visible_width > 0 &&
            fmt->video.i_visible_height > 0 )
            input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                           _("Display resolution"), "%dx%d",
                           fmt->video.i_visible_width,
                           fmt->video.i_visible_height);
        break;

    case SPU_ES:
        input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                       _("Type"), _("Subtitle") );
        break;

    default:
        break;
517
    }
Laurent Aimar's avatar
Laurent Aimar committed
518
    free( psz_description );
Laurent Aimar's avatar
Laurent Aimar committed
519
#endif
520

Laurent Aimar's avatar
Laurent Aimar committed
521
522
    /* Select it if needed */
    EsOutSelect( out, es, VLC_FALSE );
523

524
525
526
527

    TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
    p_sys->i_id++;  /* always incremented */
    switch( fmt->i_cat )
528
    {
529
530
531
532
533
534
535
536
537
        case AUDIO_ES:
            p_sys->i_audio++;
            break;
        case SPU_ES:
            p_sys->i_sub++;
            break;
        case VIDEO_ES:
            p_sys->i_video++;
            break;
538
    }
539
540

    return es;
541
542
}

Laurent Aimar's avatar
Laurent Aimar committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
static void EsSelect( es_out_t *out, es_out_id_t *es )
{
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;
    vlc_value_t    val;
    char           *psz_var;

    if( es->p_dec )
    {
        msg_Warn( p_input, "ES 0x%x is already selected", es->i_id );
        return;
    }

    if( es->fmt.i_cat == VIDEO_ES || es->fmt.i_cat == SPU_ES )
    {
        if( !var_GetBool( p_input, "video" ) || ( p_input->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
        {
            msg_Dbg( p_input, "video is disabled, not selecting ES 0x%x", es->i_id );
            return;
        }
    }
    else if( es->fmt.i_cat == AUDIO_ES )
    {
        var_Get( p_input, "audio", &val );
        if( !var_GetBool( p_input, "audio" ) || ( p_input->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
        {
            msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x", es->i_id );
            return;
        }
    }

    es->p_dec = input_DecoderNew( p_input, &es->fmt, VLC_FALSE );
    if( es->p_dec == NULL || es->p_pgrm != p_sys->p_pgrm )
        return;

    if( es->fmt.i_cat == VIDEO_ES )
        psz_var = "video-es";
    else if( es->fmt.i_cat == AUDIO_ES )
        psz_var = "audio-es";
    else if( es->fmt.i_cat == SPU_ES )
        psz_var = "spu-es";
    else
        return;

    /* Mark it as selected */
    val.i_int = es->i_id;
    var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );


    var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
}

static void EsUnselect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_update )
{
    es_out_sys_t   *p_sys = out->p_sys;
    input_thread_t *p_input = p_sys->p_input;
    vlc_value_t    val;
    char           *psz_var;

    if( es->p_dec == NULL )
    {
        msg_Warn( p_input, "ES 0x%x is already unselected", es->i_id );
        return;
    }

    input_DecoderDelete( es->p_dec );
    es->p_dec = NULL;

    if( !b_update )
        return;

    /* Update var */
    if( es->p_dec == NULL )
        return;
    if( es->fmt.i_cat == VIDEO_ES )
        psz_var = "video-es";
    else if( es->fmt.i_cat == AUDIO_ES )
        psz_var = "audio-es";
    else if( es->fmt.i_cat == SPU_ES )
        psz_var = "spu-es";
    else
        return;

    /* Mark it as selected */
    val.i_int = -1;
    var_Change( p_input, psz_var, VLC_VAR_SETVALUE, &val, NULL );

    var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
}

/**
 * Select an ES given the current mode
 * XXX: you need to take a the lock before (stream.stream_lock)
 *
 * \param out The es_out structure
 * \param es es_out_id structure
 * \param b_force ...
 * \return nothing
 */
static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
{
    es_out_sys_t      *p_sys = out->p_sys;

    int i_cat = es->fmt.i_cat;

    if( !p_sys->b_active ||
        ( !b_force && es->fmt.i_priority < 0 ) )
    {
        return;
    }

    if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
    {
        if( !es->p_dec )
            EsSelect( out, es );
    }
    else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
    {
        int i_wanted  = -1;

        if( es->p_pgrm != p_sys->p_pgrm )
            return;

        if( i_cat == AUDIO_ES )
        {
            if( p_sys->p_es_audio &&
                p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
            {
                return;
            }
            i_wanted  = p_sys->i_audio_last >= 0 ?
                            p_sys->i_audio_last : es->i_channel;
        }
        else if( i_cat == SPU_ES )
        {
            if( p_sys->p_es_sub &&
                p_sys->p_es_sub->fmt.i_priority >=
                    es->fmt.i_priority )
            {
                return;
            }
            i_wanted  = p_sys->i_sub_last;
        }
        else if( i_cat == VIDEO_ES )
        {
            i_wanted  = es->i_channel;
        }

        if( i_wanted == es->i_channel && es->p_dec == NULL )
            EsSelect( out, es );
    }

    /* FIXME TODO handle priority here */
    if( es->p_dec )
    {
        if( i_cat == AUDIO_ES )
        {
            if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
                p_sys->p_es_audio && p_sys->p_es_audio->p_dec )
            {
                EsUnselect( out, p_sys->p_es_audio, VLC_FALSE );
            }
            p_sys->p_es_audio = es;
        }
        else if( i_cat == SPU_ES )
        {
            if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
                p_sys->p_es_sub && p_sys->p_es_sub->p_dec )
            {
                EsUnselect( out, p_sys->p_es_sub, VLC_FALSE );
            }
            p_sys->p_es_sub = es;
        }
        else if( i_cat == VIDEO_ES )
        {
            p_sys->p_es_video = es;
        }
    }
}

zorglub's avatar
zorglub committed
723
724
725
726
727
728
729
/**
 * Send a block for the given es_out
 *
 * \param out the es_out to send from
 * \param es the es_out_id
 * \param p_block the data block to send
 */
730
static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
731
{
732
    es_out_sys_t *p_sys = out->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
733
    input_thread_t    *p_input = p_sys->p_input;
Laurent Aimar's avatar
Laurent Aimar committed
734
    es_out_pgrm_t *p_pgrm = es->p_pgrm;
735
736
737
738
739
740
741
742
    int64_t i_delay;

    if( es->fmt.i_cat == AUDIO_ES )
        i_delay = p_sys->i_audio_delay;
    else if( es->fmt.i_cat == SPU_ES )
        i_delay = p_sys->i_spu_delay;
    else
        i_delay = 0;
743

Laurent Aimar's avatar
Laurent Aimar committed
744
    /* +11 -> avoid null value with non null dts/pts */
Laurent Aimar's avatar
Laurent Aimar committed
745
    if( p_block->i_dts > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
746
747
    {
        p_block->i_dts =
748
749
            input_ClockGetTS( p_input, &p_pgrm->clock,
                              ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
Laurent Aimar's avatar
Laurent Aimar committed
750
    }
Laurent Aimar's avatar
Laurent Aimar committed
751
    if( p_block->i_pts > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
752
753
    {
        p_block->i_pts =
754
755
            input_ClockGetTS( p_input, &p_pgrm->clock,
                              ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
756
    }
gbazin's avatar
   
gbazin committed
757

Laurent Aimar's avatar
Laurent Aimar committed
758
759
760
761
    p_block->i_rate = p_input->i_rate;

    /* TODO handle mute */
    if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES || p_input->i_rate == INPUT_RATE_DEFAULT ) )
762
    {
Laurent Aimar's avatar
Laurent Aimar committed
763
        input_DecoderDecode( es->p_dec, p_block );
764
765
766
    }
    else
    {
767
        block_Release( p_block );
768
    }
gbazin's avatar
   
gbazin committed
769

770
771
772
773
774
775
    return VLC_SUCCESS;
}

/*****************************************************************************
 * EsOutDel:
 *****************************************************************************/
776
static void EsOutDel( es_out_t *out, es_out_id_t *es )
777
778
779
{
    es_out_sys_t *p_sys = out->p_sys;

Laurent Aimar's avatar
Laurent Aimar committed
780
781
782
783
    /* We don't try to reselect */
    if( es->p_dec )
        EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );

784
785
    TAB_REMOVE( p_sys->i_es, p_sys->es, es );

Laurent Aimar's avatar
Laurent Aimar committed
786
787
788
789
790
791
792
793
794
795
796
    es->p_pgrm->i_es--;
    if( es->p_pgrm->i_es == 0 )
    {
        msg_Err( p_sys->p_input, "Program doesn't have es anymore, clenaing TODO ?" );
    }

    if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
    if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
    if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;

    switch( es->fmt.i_cat )
797
798
799
800
801
802
803
804
805
806
807
    {
        case AUDIO_ES:
            p_sys->i_audio--;
            break;
        case SPU_ES:
            p_sys->i_sub--;
            break;
        case VIDEO_ES:
            p_sys->i_video--;
            break;
    }
808

Laurent Aimar's avatar
Laurent Aimar committed
809
810
    if( es->psz_description )
        free( es->psz_description );
811

Laurent Aimar's avatar
Laurent Aimar committed
812
    es_format_Clean( &es->fmt );
813

814
    free( es );
815
816
}

zorglub's avatar
zorglub committed
817
818
819
820
821
822
823
824
/**
 * Control query handler
 *
 * \param out the es_out to control
 * \param i_query A es_out query as defined in include/ninput.h
 * \param args a variable list of arguments for the query
 * \return VLC_SUCCESS or an error code
 */
825
826
827
828
static int EsOutControl( es_out_t *out, int i_query, va_list args )
{
    es_out_sys_t *p_sys = out->p_sys;
    vlc_bool_t  b, *pb;
829
830
831
832
    int         i, *pi;

    es_out_id_t *es;

833
834
    switch( i_query )
    {
835
836
        case ES_OUT_SET_ES_STATE:
            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
837
            b = (vlc_bool_t) va_arg( args, vlc_bool_t );
Laurent Aimar's avatar
Laurent Aimar committed
838
            if( b && es->p_dec == NULL )
839
            {
Laurent Aimar's avatar
Laurent Aimar committed
840
841
                EsSelect( out, es );
                return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
842
            }
Laurent Aimar's avatar
Laurent Aimar committed
843
            else if( !b && es->p_dec )
844
            {
Laurent Aimar's avatar
Laurent Aimar committed
845
                EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
846
847
                return VLC_SUCCESS;
            }
Laurent Aimar's avatar
Laurent Aimar committed
848
849
            return VLC_SUCCESS;

850
851
        case ES_OUT_GET_ES_STATE:
            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
852
853
            pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );

Laurent Aimar's avatar
Laurent Aimar committed
854
            *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
855
856
857
            return VLC_SUCCESS;

        case ES_OUT_SET_ACTIVE:
858
        {
859
860
            b = (vlc_bool_t) va_arg( args, vlc_bool_t );
            p_sys->b_active = b;
Laurent Aimar's avatar
Laurent Aimar committed
861
            /* Needed ? */
862
            if( b )
Laurent Aimar's avatar
Laurent Aimar committed
863
                var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
864
            return VLC_SUCCESS;
865
        }
866
867
868
869
870
871
872
873

        case ES_OUT_GET_ACTIVE:
            pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
            *pb = p_sys->b_active;
            return VLC_SUCCESS;

        case ES_OUT_SET_MODE:
            i = (int) va_arg( args, int );
gbazin's avatar
   
gbazin committed
874
875
            if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
                i == ES_OUT_MODE_AUTO )
876
877
878
879
880
881
            {
                p_sys->i_mode = i;

                /* Reapply policy mode */
                for( i = 0; i < p_sys->i_es; i++ )
                {
Laurent Aimar's avatar
Laurent Aimar committed
882
                    if( p_sys->es[i]->p_dec )
883
                    {
Laurent Aimar's avatar
Laurent Aimar committed
884
                        EsUnselect( out, p_sys->es[i], p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
                    }
                }
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    EsOutSelect( out, p_sys->es[i], VLC_FALSE );
                }
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case ES_OUT_GET_MODE:
            pi = (int*) va_arg( args, int* );
            *pi = p_sys->i_mode;
            return VLC_SUCCESS;

        case ES_OUT_SET_ES:
            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
Laurent Aimar's avatar
Laurent Aimar committed
902
            /* Special case NULL, NULL+i_cat */
903
904
905
906
            if( es == NULL )
            {
                for( i = 0; i < p_sys->i_es; i++ )
                {
Laurent Aimar's avatar
Laurent Aimar committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
                    if( p_sys->es[i]->p_dec )
                        EsUnselect( out, p_sys->es[i],
                                    p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
                }
            }
            else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
            {
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    if( p_sys->es[i]->p_dec && p_sys->es[i]->fmt.i_cat == AUDIO_ES )
                        EsUnselect( out, p_sys->es[i], p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
                }
            }
            else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
            {
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    if( p_sys->es[i]->p_dec && p_sys->es[i]->fmt.i_cat == VIDEO_ES )
                        EsUnselect( out, p_sys->es[i], p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
                }
            }
            else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
            {
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    if( p_sys->es[i]->p_dec && p_sys->es[i]->fmt.i_cat == SPU_ES )
                        EsUnselect( out, p_sys->es[i], p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
934
935
936
937
                }
            }
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
938
939
940
941
942
943
944
945
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    if( es == p_sys->es[i] )
                    {
                        EsOutSelect( out, es, VLC_TRUE );
                        break;
                    }
                }
946
            }
947
948
            return VLC_SUCCESS;

949
        case ES_OUT_SET_PCR:
950
        case ES_OUT_SET_GROUP_PCR:
951
        {
Laurent Aimar's avatar
Laurent Aimar committed
952
953
954
            es_out_pgrm_t *p_pgrm = NULL;
            int            i_group = 0;
            int64_t        i_pcr;
955

956
957
            if( i_query == ES_OUT_SET_PCR )
            {
Laurent Aimar's avatar
Laurent Aimar committed
958
                p_pgrm = p_sys->p_pgrm;
959
960
961
            }
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
962
963
964
                int i;
                i_group = (int)va_arg( args, int );
                for( i = 0; i < p_sys->i_pgrm; i++ )
965
                {
Laurent Aimar's avatar
Laurent Aimar committed
966
967
968
969
970
                    if( p_sys->pgrm[i]->i_id == i_group )
                    {
                        p_pgrm = p_sys->pgrm[i];
                        break;
                    }
971
                }
972
            }
Laurent Aimar's avatar
Laurent Aimar committed
973
974
975
            if( p_pgrm == NULL )
                p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */

976
            i_pcr = (int64_t)va_arg( args, int64_t );
977
            /* search program */
978
                /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
Laurent Aimar's avatar
Laurent Aimar committed
979
                input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock, (i_pcr + 11 ) * 9 / 100);
980
981
982
983
            return VLC_SUCCESS;
        }

        case ES_OUT_RESET_PCR:
Laurent Aimar's avatar
Laurent Aimar committed
984
            for( i = 0; i < p_sys->i_pgrm; i++ )
985
            {
Laurent Aimar's avatar
Laurent Aimar committed
986
987
                p_sys->pgrm[i]->clock.i_synchro_state =  SYNCHRO_REINIT;
                p_sys->pgrm[i]->clock.last_pts = 0;
988
989
990
            }
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
        case ES_OUT_GET_GROUP:
            pi = (int*) va_arg( args, int* );
            if( p_sys->p_pgrm )
                *pi = p_sys->p_pgrm->i_id;
            else
                *pi = -1;    /* FIXME */
            return VLC_SUCCESS;

        case ES_OUT_SET_GROUP:
        {
            int j;
            i = (int) va_arg( args, int );
            for( j = 0; j < p_sys->i_pgrm; j++ )
            {
                es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
                if( p_pgrm->i_id == i )
                {
                    EsOutProgramSelect( out, p_pgrm );
                    return VLC_SUCCESS;
                }
            }
            return VLC_EGENERIC;
        }

1015
1016
1017
1018
1019
        default:
            msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
            return VLC_EGENERIC;
    }
}
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

/****************************************************************************
 * LanguageGetName: try to expend iso639 into plain name
 ****************************************************************************/
static char *LanguageGetName( const char *psz_code )
{
    const iso639_lang_t *pl;

    if( psz_code == NULL )
    {
        return strdup( "" );
    }

    if( strlen( psz_code ) == 2 )
    {
        pl = GetLang_1( psz_code );
    }
    else if( strlen( psz_code ) == 3 )
    {
        pl = GetLang_2B( psz_code );
        if( !strcmp( pl->psz_iso639_1, "??" ) )
        {
            pl = GetLang_2T( psz_code );
        }
    }
    else
    {
        return strdup( psz_code );
    }

    if( !strcmp( pl->psz_iso639_1, "??" ) )
    {
       return strdup( psz_code );
    }
    else
    {
        if( *pl->psz_native_name )
        {
            return strdup( pl->psz_native_name );
        }
        return strdup( pl->psz_eng_name );
    }
}