es_out.c 47.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * es_out.c: Es Out handler for input.
 *****************************************************************************
4
 * Copyright (C) 2003-2004 the VideoLAN team
5 6 7
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30
#include <vlc/vlc.h>

#include <stdio.h>
31 32
#include <stdlib.h>

Clément Stenac's avatar
Clément Stenac committed
33 34 35
#include <vlc_input.h>
#include <vlc_es_out.h>
#include <vlc_block.h>
36 37 38 39 40 41

#include "input_internal.h"

#include "vlc_playlist.h"
#include "iso_lang.h"
/* FIXME we should find a better way than including that */
42
#include "../text/iso-639_def.h"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
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;

60 61
    char    *psz_now_playing;

62 63 64 65 66 67 68 69
} es_out_pgrm_t;

struct es_out_id_t
{
    /* ES ID */
    int       i_id;
    es_out_pgrm_t *p_pgrm;

70 71 72 73
    /* Signal a discontinuity in the timeline for every PID */
    vlc_bool_t b_discontinuity;

    /* Misc. */
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    int64_t i_preroll_end;

    /* Channel in the track type */
    int         i_channel;
    es_format_t fmt;
    char        *psz_language;
    char        *psz_language_code;
    decoder_t   *p_dec;
};

struct es_out_sys_t
{
    input_thread_t *p_input;

    /* all programs */
    int           i_pgrm;
    es_out_pgrm_t **pgrm;
    es_out_pgrm_t **pp_selected_pgrm; /* --programs */
    es_out_pgrm_t *p_pgrm;  /* Master program */

    /* all es */
    int         i_id;
    int         i_es;
    es_out_id_t **es;

    /* mode gestion */
    vlc_bool_t  b_active;
    int         i_mode;

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

    /* es to select */
109 110
    int         i_audio_last, i_audio_id;
    int         i_sub_last, i_sub_id;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    char        **ppsz_audio_language;
    char        **ppsz_sub_language;

    /* current main es */
    es_out_id_t *p_es_audio;
    es_out_id_t *p_es_video;
    es_out_id_t *p_es_sub;

    /* delay */
    int64_t i_audio_delay;
    int64_t i_spu_delay;
};

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 * );
static void         EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force );
static int          EsOutControl( es_out_t *, int i_query, va_list );

static void         EsOutAddInfo( es_out_t *, es_out_id_t *es );

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 );
static char *LanguageGetName( const char *psz_code );
static char *LanguageGetCode( const char *psz_lang );
static char **LanguageSplit( const char *psz_langs );
static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );

/*****************************************************************************
 * input_EsOutNew:
 *****************************************************************************/
es_out_t *input_EsOutNew( input_thread_t *p_input )
{
    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;
    int i;

    out->pf_add     = EsOutAdd;
    out->pf_send    = EsOutSend;
    out->pf_del     = EsOutDel;
    out->pf_control = EsOutControl;
    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;


    p_sys->i_pgrm   = 0;
    p_sys->pgrm     = NULL;
    p_sys->p_pgrm   = NULL;

    p_sys->i_id    = 0;
    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-track", &val );
    p_sys->i_audio_last = val.i_int;

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

    var_Get( p_input, "audio-language", &val );
    p_sys->ppsz_audio_language = LanguageSplit(val.psz_string);
    if( p_sys->ppsz_audio_language )
    {
        for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
185
            msg_Dbg( p_input, "selected audio language[%d] %s",
186 187
                     i, p_sys->ppsz_audio_language[i] );
    }
188
    if( val.psz_string ) free( val.psz_string );
189 190 191 192 193 194

    var_Get( p_input, "sub-language", &val );
    p_sys->ppsz_sub_language = LanguageSplit(val.psz_string);
    if( p_sys->ppsz_sub_language )
    {
        for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
195
            msg_Dbg( p_input, "selected subtitle language[%d] %s",
196 197
                     i, p_sys->ppsz_sub_language[i] );
    }
198
    if( val.psz_string ) free( val.psz_string );
199

200 201 202 203 204 205
    var_Get( p_input, "audio-track-id", &val );
    p_sys->i_audio_id = val.i_int;

    var_Get( p_input, "sub-track-id", &val );
    p_sys->i_sub_id = val.i_int;

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    p_sys->p_es_audio = NULL;
    p_sys->p_es_video = NULL;
    p_sys->p_es_sub   = NULL;

    p_sys->i_audio_delay= 0;
    p_sys->i_spu_delay  = 0;

    return out;
}

/*****************************************************************************
 * input_EsOutDelete:
 *****************************************************************************/
void input_EsOutDelete( es_out_t *out )
{
    es_out_sys_t *p_sys = out->p_sys;
    int i;

    for( i = 0; i < p_sys->i_es; i++ )
    {
        if( p_sys->es[i]->p_dec )
        {
            input_DecoderDelete( p_sys->es[i]->p_dec );
        }
        if( p_sys->es[i]->psz_language )
            free( p_sys->es[i]->psz_language );
        if( p_sys->es[i]->psz_language_code )
            free( p_sys->es[i]->psz_language_code );
        es_format_Clean( &p_sys->es[i]->fmt );

        free( p_sys->es[i] );
    }
    if( p_sys->ppsz_audio_language )
    {
        for( i = 0; p_sys->ppsz_audio_language[i]; i++ )
            free( p_sys->ppsz_audio_language[i] );
        free( p_sys->ppsz_audio_language );
    }
    if( p_sys->ppsz_sub_language )
    {
        for( i = 0; p_sys->ppsz_sub_language[i]; i++ )
            free( p_sys->ppsz_sub_language[i] );
        free( p_sys->ppsz_sub_language );
    }

    if( p_sys->es )
        free( p_sys->es );

    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
256 257
        if( p_sys->pgrm[i]->psz_now_playing )
            free( p_sys->pgrm[i]->psz_now_playing );
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        free( p_sys->pgrm[i] );
    }
    if( p_sys->pgrm )
        free( p_sys->pgrm );

    free( p_sys );
    free( out );
}

es_out_id_t *input_EsOutGetFromID( es_out_t *out, int i_id )
{
    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);
    }

    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;
}

void input_EsOutDiscontinuity( es_out_t *out, vlc_bool_t b_audio )
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;

    for( i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *es = p_sys->es[i];
292
        es->b_discontinuity = VLC_TRUE; /* signal discontinuity */
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        /* 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 );
        }
    }
}

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;
}

vlc_bool_t input_EsOutDecodersEmpty( es_out_t *out )
{
    es_out_sys_t      *p_sys = out->p_sys;
    int i;

    for( i = 0; i < p_sys->i_es; i++ )
    {
        es_out_id_t *es = p_sys->es[i];

        if( es->p_dec && !input_DecoderEmpty( es->p_dec ) )
            return VLC_FALSE;
    }
    return VLC_TRUE;
}

/*****************************************************************************
 *
 *****************************************************************************/
static void EsOutESVarUpdate( es_out_t *out, es_out_id_t *es,
                              vlc_bool_t b_delete )
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    vlc_value_t       val, text;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
338
    const char *psz_var;
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

    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
        return;

    if( b_delete )
    {
        val.i_int = es->i_id;
        var_Change( p_input, psz_var, VLC_VAR_DELCHOICE, &val, NULL );
        var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
        return;
    }

    /* 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++;
    }

    /* Take care of the ES description */
    if( es->fmt.psz_description && *es->fmt.psz_description )
    {
        if( es->psz_language && *es->psz_language )
        {
374 375 376 377
            text.psz_string = malloc( strlen( es->fmt.psz_description) +
                                      strlen( es->psz_language ) + 10 );
            sprintf( text.psz_string, "%s - [%s]", es->fmt.psz_description,
                                                   es->psz_language );
378 379 380 381 382 383 384 385
        }
        else text.psz_string = strdup( es->fmt.psz_description );
    }
    else
    {
        if( es->psz_language && *es->psz_language )
        {
            char *temp;
386 387
            text.psz_string = malloc( strlen( _("Track %i") )+
                                      strlen( es->psz_language ) + 30 );
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
            asprintf( &temp,  _("Track %i"), val.i_int );
            sprintf( text.psz_string, "%s - [%s]", temp, es->psz_language );
            free( temp );
        }
        else
        {
            text.psz_string = malloc( strlen( _("Track %i") ) + 20 );
            sprintf( text.psz_string, _("Track %i"), val.i_int );
        }
    }

    val.i_int = es->i_id;
    var_Change( p_input, psz_var, VLC_VAR_ADDCHOICE, &val, &text );

    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;
Clément Stenac's avatar
Clément Stenac committed
423
        msg_Dbg( p_input, "unselecting program id=%d", old->i_id );
424 425 426 427 428 429 430 431 432 433 434 435 436

        for( i = 0; i < p_sys->i_es; i++ )
        {
            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 );
        }

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

Clément Stenac's avatar
Clément Stenac committed
437
    msg_Dbg( p_input, "selecting program id=%d", p_pgrm->i_id );
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

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

    /* Switch master stream */
    if( p_sys->p_pgrm && p_sys->p_pgrm->clock.b_master )
    {
        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++ )
    {
        if( p_sys->es[i]->p_pgrm == p_sys->p_pgrm )
            EsOutESVarUpdate( out, p_sys->es[i], VLC_FALSE );
        EsOutSelect( out, p_sys->es[i], VLC_FALSE );
    }

465 466 467 468 469 470
    /* Update now playing if defined per program */
    if( p_pgrm->psz_now_playing )
    {
        char *psz_cat = malloc( strlen(_("Program")) + 10 );

        sprintf( psz_cat, "%s %d", _("Program"), p_pgrm->i_id );
471 472
        input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
                       _(VLC_META_NOW_PLAYING), "%s", p_pgrm->psz_now_playing );
473 474 475 476
        free( psz_cat );
    }


477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    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;
495
    p_pgrm->psz_now_playing = NULL;
Clément Stenac's avatar
Clément Stenac committed
496
    input_ClockInit( &p_pgrm->clock, VLC_FALSE, p_input->p->input.i_cr_average );
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

    /* 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 );
    }
    return p_pgrm;
}

516 517 518
/* EsOutDelProgram:
 *  Delete a program
 */
519
static int EsOutProgramDel( es_out_t *out, int i_group )
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
{
    es_out_sys_t      *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    es_out_pgrm_t     *p_pgrm = NULL;
    vlc_value_t       val;
    int               i;

    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( p_sys->pgrm[i]->i_id == i_group )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }

536 537
    if( p_pgrm == NULL )
        return VLC_EGENERIC;
538 539 540 541 542

    if( p_pgrm->i_es )
    {
        msg_Dbg( p_input, "can't delete program %d which still has %i ES",
                 i_group, p_pgrm->i_es );
543
        return VLC_EGENERIC;
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    }

    TAB_REMOVE( p_sys->i_pgrm, p_sys->pgrm, p_pgrm );

    /* If program is selected we need to unselect it */
    if( p_sys->p_pgrm == p_pgrm ) p_sys->p_pgrm = 0;

    if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
    free( p_pgrm );

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

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

    return VLC_SUCCESS;
561 562
}

563 564 565 566 567
/* EsOutProgramMeta:
 */
static void EsOutProgramMeta( es_out_t *out, int i_group, vlc_meta_t *p_meta )
{
    es_out_sys_t      *p_sys = out->p_sys;
568
    es_out_pgrm_t     *p_pgrm = NULL;
569 570
    input_thread_t    *p_input = p_sys->p_input;
    char              *psz_cat = malloc( strlen(_("Program")) + 10 );
Clément Stenac's avatar
Clément Stenac committed
571
    char              *psz_title = NULL;
572 573
    char              *psz_now_playing = NULL;
    char              *psz_provider = NULL;
574 575 576 577 578
    int i;

    msg_Dbg( p_input, "EsOutProgramMeta: number=%d", i_group );
    sprintf( psz_cat, "%s %d", _("Program"), i_group );

Clément Stenac's avatar
Clément Stenac committed
579
    if( p_meta->psz_title ) psz_title = p_meta->psz_title;
Clément Stenac's avatar
Clément Stenac committed
580
    if( p_meta->psz_publisher ) psz_provider = p_meta->psz_publisher;
581
    if( p_meta->psz_nowplaying ) psz_now_playing = p_meta->psz_nowplaying;
582

Clément Stenac's avatar
Clément Stenac committed
583
    if( !psz_title && !psz_now_playing )
584 585 586 587 588 589 590 591 592 593 594 595 596 597
    {
        free( psz_cat );
        return;
    }

    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( p_sys->pgrm[i]->i_id == i_group )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }

598 599 600 601 602
    if( p_pgrm == NULL ) {
	free( psz_cat );
	msg_Dbg( p_input, "Trying to add meta for non-existing program" );
	return;
    }
603 604

    /* Update the description text of the program */
Clément Stenac's avatar
Clément Stenac committed
605
    if( psz_title && *psz_title )
606 607 608 609 610 611 612 613 614 615
    {
        vlc_value_t val;
        vlc_value_t text;

        /* ugly but it works */
        val.i_int = i_group;
        var_Change( p_input, "program", VLC_VAR_DELCHOICE, &val, NULL );

        if( psz_provider && *psz_provider )
        {
Clément Stenac's avatar
Clément Stenac committed
616
            asprintf( &text.psz_string, "%s [%s]", psz_title, psz_provider );
617 618 619 620 621
            var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
            free( text.psz_string );
        }
        else
        {
Clément Stenac's avatar
Clément Stenac committed
622
            text.psz_string = psz_title;
623 624 625 626 627
            var_Change( p_input, "program", VLC_VAR_ADDCHOICE, &val, &text );
        }
    }
    if( psz_now_playing )
    {
628
        if( p_pgrm->psz_now_playing ) free( p_pgrm->psz_now_playing );
629 630 631 632
        p_pgrm->psz_now_playing = strdup(psz_now_playing);

        if( p_sys->p_pgrm == p_pgrm )
        {
Clément Stenac's avatar
Clément Stenac committed
633
            vlc_meta_SetNowPlaying( p_input->p->input.p_item->p_meta,
Clément Stenac's avatar
Clément Stenac committed
634
                                    psz_now_playing );
635
        }
636 637 638 639
    }
    free( psz_cat );
}

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
/* EsOutAdd:
 *  Add an es_out
 */
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;

    es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
    es_out_pgrm_t     *p_pgrm = NULL;
    int i;

    if( fmt->i_group < 0 )
    {
        msg_Err( p_input, "invalid group number" );
        return NULL;
    }

    /* Search the program */
    for( i = 0; i < p_sys->i_pgrm; i++ )
    {
        if( fmt->i_group == p_sys->pgrm[i]->i_id )
        {
            p_pgrm = p_sys->pgrm[i];
            break;
        }
    }
    if( p_pgrm == NULL )
    {
        /* Create a new one */
        p_pgrm = EsOutProgramAdd( out, fmt->i_group );
    }

    /* Increase ref count for program */
    p_pgrm->i_es++;

    /* 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 );
    es->i_preroll_end = -1;
683
    es->b_discontinuity = VLC_FALSE;
684 685 686 687 688 689 690 691 692

    switch( fmt->i_cat )
    {
    case AUDIO_ES:
        es->i_channel = p_sys->i_audio;
        break;

    case VIDEO_ES:
        es->i_channel = p_sys->i_video;
693
        if( fmt->video.i_frame_rate && fmt->video.i_frame_rate_base )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
694 695 696 697
            vlc_ureduce( &es->fmt.video.i_frame_rate,
                         &es->fmt.video.i_frame_rate_base,
                         fmt->video.i_frame_rate,
                         fmt->video.i_frame_rate_base, 0 );
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 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
        break;

    case SPU_ES:
        es->i_channel = p_sys->i_sub;
        break;

    default:
        es->i_channel = 0;
        break;
    }
    es->psz_language = LanguageGetName( fmt->psz_language ); /* remember so we only need to do it once */
    es->psz_language_code = LanguageGetCode( fmt->psz_language );
    es->p_dec = NULL;

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

    /* Select it if needed */
    EsOutSelect( out, es, VLC_FALSE );


    TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
    p_sys->i_id++;  /* always incremented */
    switch( fmt->i_cat )
    {
        case AUDIO_ES:
            p_sys->i_audio++;
            break;
        case SPU_ES:
            p_sys->i_sub++;
            break;
        case VIDEO_ES:
            p_sys->i_video++;
            break;
    }

    EsOutAddInfo( out, es );

    return es;
}

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;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
744
    const char     *psz_var;
745 746 747 748 749 750 751 752 753 754

    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" ) ||
Clément Stenac's avatar
Clément Stenac committed
755
            ( p_input->p->p_sout && !var_GetBool( p_input, "sout-video" ) ) )
756 757 758 759 760 761 762 763 764 765
        {
            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" ) ||
Clément Stenac's avatar
Clément Stenac committed
766
            ( p_input->p->p_sout && !var_GetBool( p_input, "sout-audio" ) ) )
767 768 769 770 771 772
        {
            msg_Dbg( p_input, "audio is disabled, not selecting ES 0x%x",
                     es->i_id );
            return;
        }
    }
773 774 775 776
    if( es->fmt.i_cat == SPU_ES )
    {
        var_Get( p_input, "spu", &val );
        if( !var_GetBool( p_input, "spu" ) ||
Clément Stenac's avatar
Clément Stenac committed
777
            ( p_input->p->p_sout && !var_GetBool( p_input, "sout-spu" ) ) )
778 779 780 781
        {
            msg_Dbg( p_input, "spu is disabled, not selecting ES 0x%x",
                     es->i_id );
            return;
782
        }
783
    }
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

    es->i_preroll_end = -1;
    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;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
812
    const char     *psz_var;
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

    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_PARTIAL )
    {
        vlc_value_t val;
        int i;
        var_Get( p_sys->p_input, "programs", &val );
        for ( i = 0; i < val.p_list->i_count; i++ )
        {
            if ( val.p_list->p_values[i].i_int == es->p_pgrm->i_id || b_force )
            {
                if( !es->p_dec )
                    EsSelect( out, es );
                break;
            }
        }
        var_Change( p_sys->p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
    }
    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 )
        {
            int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
                                     es->psz_language_code );

            if( p_sys->p_es_audio &&
                p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
            {
                int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
                                         p_sys->p_es_audio->psz_language_code );

                if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
                    return;
                i_wanted = es->i_channel;
            }
            else
            {
                /* Select audio if (no audio selected yet)
                 * - no audio-language
                 * - no audio code for the ES
                 * - audio code in the requested list */
                if( idx1 >= 0 ||
                    !strcmp( es->psz_language_code, "??" ) ||
                    !p_sys->ppsz_audio_language )
                    i_wanted = es->i_channel;
            }

            if( p_sys->i_audio_last >= 0 )
                i_wanted = p_sys->i_audio_last;
923 924 925 926 927 928 929 930

            if( p_sys->i_audio_id >= 0 )
            {
                if( es->i_id == p_sys->i_audio_id )
                    i_wanted = es->i_channel;
                else
                    return;
            }
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
        }
        else if( i_cat == SPU_ES )
        {
            int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
                                     es->psz_language_code );

            if( p_sys->p_es_sub &&
                p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
            {
                int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
                                         p_sys->p_es_sub->psz_language_code );

                msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
                        idx1, es->psz_language_code, idx2,
                        p_sys->p_es_sub->psz_language_code );

                if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
                    return;
                /* We found a SPU that matches our language request */
                i_wanted  = es->i_channel;
            }
            else if( idx1 >= 0 )
            {
                msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
                        idx1, es->psz_language_code );

                i_wanted  = es->i_channel;
            }
            if( p_sys->i_sub_last >= 0 )
                i_wanted  = p_sys->i_sub_last;
961 962 963 964 965 966 967 968

            if( p_sys->i_sub_id >= 0 )
            {
                if( es->i_id == p_sys->i_sub_id )
                    i_wanted = es->i_channel;
                else
                    return;
            }
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
        }
        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 != es &&
                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 != es &&
                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;
        }
    }
}

/**
 * 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
 */
static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
{
    es_out_sys_t *p_sys = out->p_sys;
    input_thread_t    *p_input = p_sys->p_input;
    es_out_pgrm_t *p_pgrm = es->p_pgrm;
    int64_t i_delay;
1024
    int i_total=0;
1025 1026 1027 1028 1029 1030 1031 1032

    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;

1033
    if( p_input->p_libvlc->b_stats )
1034
    {
Clément Stenac's avatar
Clément Stenac committed
1035 1036
        vlc_mutex_lock( &p_input->p->counters.counters_lock );
        stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
1037
                             p_block->i_buffer, &i_total );
Clément Stenac's avatar
Clément Stenac committed
1038
        stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
1039
                           (float)i_total, NULL );
Clément Stenac's avatar
Clément Stenac committed
1040
        vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1041
    }
1042

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    /* Mark preroll blocks */
    if( es->i_preroll_end >= 0 )
    {
        int64_t i_date = p_block->i_pts;
        if( i_date <= 0 )
            i_date = p_block->i_dts;

        if( i_date < es->i_preroll_end )
            p_block->i_flags |= BLOCK_FLAG_PREROLL;
        else
            es->i_preroll_end = -1;
    }

    /* +11 -> avoid null value with non null dts/pts */
    if( p_block->i_dts > 0 )
    {
        p_block->i_dts =
            input_ClockGetTS( p_input, &p_pgrm->clock,
                              ( p_block->i_dts + 11 ) * 9 / 100 ) + i_delay;
    }
    if( p_block->i_pts > 0 )
    {
        p_block->i_pts =
            input_ClockGetTS( p_input, &p_pgrm->clock,
                              ( p_block->i_pts + 11 ) * 9 / 100 ) + i_delay;
    }
    if ( es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) )
    {
        mtime_t current_date = mdate();
        if( !p_block->i_pts
               || p_block->i_pts > current_date + 10000000
               || current_date > p_block->i_pts )
        {
            /* ETSI EN 300 472 Annex A : do not take into account the PTS
             * for teletext streams. */
            p_block->i_pts = current_date + 400000
                               + p_input->i_pts_delay + i_delay;
        }
    }

Clément Stenac's avatar
Clément Stenac committed
1083
    p_block->i_rate = p_input->p->i_rate;
1084

1085 1086
    /* TODO handle mute */
    if( es->p_dec && ( es->fmt.i_cat != AUDIO_ES ||
Clément Stenac's avatar
Clément Stenac committed
1087
        p_input->p->i_rate == INPUT_RATE_DEFAULT ) )
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    {
        input_DecoderDecode( es->p_dec, p_block );
    }
    else
    {
        block_Release( p_block );
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * EsOutDel:
 *****************************************************************************/
static void EsOutDel( es_out_t *out, es_out_id_t *es )
{
    es_out_sys_t *p_sys = out->p_sys;
1105 1106
    vlc_bool_t b_reselect = VLC_FALSE;
    int i;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

    /* We don't try to reselect */
    if( es->p_dec )
        EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );

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

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

    es->p_pgrm->i_es--;
1118 1119
    if( es->p_pgrm->i_es == 0 )
    {
1120
        msg_Dbg( p_sys->p_input, "Program doesn't contain anymore ES" );
1121
    }
1122

1123 1124 1125
    if( p_sys->p_es_audio == es || p_sys->p_es_video == es ||
        p_sys->p_es_sub == es ) b_reselect = VLC_TRUE;

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    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 )
    {
        case AUDIO_ES:
            p_sys->i_audio--;
            break;
        case SPU_ES:
            p_sys->i_sub--;
            break;
        case VIDEO_ES:
            p_sys->i_video--;
            break;
    }

1143 1144 1145 1146 1147 1148 1149 1150
    /* Re-select another track when needed */
    if( b_reselect )
        for( i = 0; i < p_sys->i_es; i++ )
        {
            if( es->fmt.i_cat == p_sys->es[i]->fmt.i_cat )
                EsOutSelect( out, p_sys->es[i], VLC_FALSE );
        }

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
    if( es->psz_language )
        free( es->psz_language );
    if( es->psz_language_code )
        free( es->psz_language_code );

    es_format_Clean( &es->fmt );

    free( es );
}

/**
 * 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
 */
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;
    int         i, *pi;

    es_out_id_t *es;

    switch( i_query )
    {
        case ES_OUT_SET_ES_STATE:
            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
            b = (vlc_bool_t) va_arg( args, vlc_bool_t );
            if( b && es->p_dec == NULL )
            {
                EsSelect( out, es );
                return es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
            }
            else if( !b && es->p_dec )
            {
                EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
                return VLC_SUCCESS;
            }
            return VLC_SUCCESS;

        case ES_OUT_GET_ES_STATE:
            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
            pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );

            *pb = es->p_dec ? VLC_TRUE : VLC_FALSE;
            return VLC_SUCCESS;

        case ES_OUT_SET_ACTIVE:
        {
            b = (vlc_bool_t) va_arg( args, vlc_bool_t );
            p_sys->b_active = b;
            /* Needed ? */
            if( b )
                var_SetBool( p_sys->p_input, "intf-change", VLC_TRUE );
            return VLC_SUCCESS;
        }

        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 );
            if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
                i == ES_OUT_MODE_AUTO || i == ES_OUT_MODE_PARTIAL )
            {
                p_sys->i_mode = i;

                /* Reapply policy mode */
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    if( p_sys->es[i]->p_dec )
                    {
                        EsUnselect( out, p_sys->es[i],
                                    p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
                    }
                }
                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 * );
            /* Special case NULL, NULL+i_cat */
            if( es == NULL )
            {
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    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 );
                }
            }
            else
            {
                for( i = 0; i < p_sys->i_es; i++ )
                {
                    if( es == p_sys->es[i] )
                    {
                        EsOutSelect( out, es, VLC_TRUE );
                        break;
                    }
                }
            }
            return VLC_SUCCESS;

        case ES_OUT_SET_PCR:
        case ES_OUT_SET_GROUP_PCR:
        {
            es_out_pgrm_t *p_pgrm = NULL;
            int            i_group = 0;
            int64_t        i_pcr;

            if( i_query == ES_OUT_SET_PCR )
            {
                p_pgrm = p_sys->p_pgrm;
            }
            else
            {
                int i;
                i_group = (int)va_arg( args, int );
                for( i = 0; i < p_sys->i_pgrm; i++ )
                {
                    if( p_sys->pgrm[i]->i_id == i_group )
                    {
                        p_pgrm = p_sys->pgrm[i];
                        break;
                    }
                }
            }
            if( p_pgrm == NULL )
                p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */

            i_pcr = (int64_t)va_arg( args, int64_t );
            /* search program */
            /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
            input_ClockSetPCR( p_sys->p_input, &p_pgrm->clock,
                               (i_pcr + 11 ) * 9 / 100);
            return VLC_SUCCESS;
        }

        case ES_OUT_RESET_PCR:
            for( i = 0; i < p_sys->i_pgrm; i++ )
            {
                p_sys->pgrm[i]->clock.i_synchro_state =  SYNCHRO_REINIT;
                p_sys->pgrm[i]->clock.last_pts = 0;
            }
            return VLC_SUCCESS;

        case ES_OUT_GET_TS:
            if( p_sys->p_pgrm )
            {
                int64_t i_ts = (int64_t)va_arg( args, int64_t );
                int64_t *pi_ts = (int64_t *)va_arg( args, int64_t * );
                *pi_ts = input_ClockGetTS( p_sys->p_input,
                                           &p_sys->p_pgrm->clock,
                                           ( i_ts + 11 ) * 9 / 100 );
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        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;
        }

        case ES_OUT_SET_FMT:
        {
            /* This ain't pretty but is need by some demuxers (eg. Ogg )
             * to update the p_extra data */
            es_format_t *p_fmt;
            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
            p_fmt = (es_format_t*) va_arg( args, es_format_t * );
            if( es == NULL ) return VLC_EGENERIC;

            if( p_fmt->i_extra )
            {
                es->fmt.i_extra = p_fmt->i_extra;
                es->fmt.p_extra = realloc( es->fmt.p_extra, p_fmt->i_extra );
                memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );

                if( !es->p_dec ) return VLC_SUCCESS;

1396 1397 1398 1399 1400 1401
#if 1
                input_DecoderDelete( es->p_dec );
                es->p_dec = input_DecoderNew( p_sys->p_input,
                                              &es->fmt, VLC_FALSE );

#else
1402 1403 1404 1405 1406
                es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
                es->p_dec->fmt_in.p_extra =
                    realloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
                memcpy( es->p_dec->fmt_in.p_extra,
                        p_fmt->p_extra, p_fmt->i_extra );
1407
#endif
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
            }

            return VLC_SUCCESS;
        }

        case ES_OUT_SET_NEXT_DISPLAY_TIME:
        {
            int64_t i_date;

            es = (es_out_id_t*) va_arg( args, es_out_id_t * );
            i_date = (int64_t)va_arg( args, int64_t );

            if( !es || !es->p_dec )
                return VLC_EGENERIC;

            es->i_preroll_end = i_date;
            input_DecoderPreroll( es->p_dec, i_date );

            return VLC_SUCCESS;
        }
1428 1429 1430 1431 1432 1433 1434 1435
        case ES_OUT_SET_GROUP_META:
        {
            int i_group = (int)va_arg( args, int );
            vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );

            EsOutProgramMeta( out, i_group, p_meta );
            return VLC_SUCCESS;
        }
1436 1437 1438 1439 1440 1441
        case ES_OUT_DEL_GROUP:
        {
            int i_group = (int)va_arg( args, int );

            return EsOutProgramDel( out, i_group );
        }
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522

        default:
            msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
            return VLC_EGENERIC;
    }
}

/****************************************************************************
 * 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 );
    }
}

/* Get a 2 char code */
static char *LanguageGetCode( const char *psz_lang )
{
    const iso639_lang_t *pl;

    if( psz_lang == NULL || *psz_lang == '\0' )
        return strdup("??");

    for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
    {
        if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
            !strcasecmp( pl->psz_native_name, psz_lang ) ||
            !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
            !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
            !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
            break;
    }

    if( pl->psz_iso639_1 != NULL )
        return strdup( pl->psz_iso639_1 );

    return strdup("??");
}

static char **LanguageSplit( const char *psz_langs )
{
    char *psz_dup;
    char *psz_parser;
    char **ppsz = NULL;
    int i_psz = 0;

1523
    if( psz_langs == NULL ) return NULL;
1524 1525 1526 1527 1528 1529 1530 1531 1532

    psz_parser = psz_dup = strdup(psz_langs);

    while( psz_parser && *psz_parser )
    {
        char *psz;
        char *psz_code;

        psz = strchr(psz_parser, ',' );
1533
        if( psz ) *psz++ = '\0';
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

        psz_code = LanguageGetCode( psz_parser );
        if( strcmp( psz_code, "??" ) )
        {
            TAB_APPEND( i_psz, ppsz, psz_code );
        }

        psz_parser = psz;
    }

    if( i_psz )
    {
        TAB_APPEND( i_psz, ppsz, NULL );
    }

1549
    free( psz_dup );
1550 1551 1552 1553 1554 1555 1556
    return ppsz;
}

static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
{
    int i;

1557
    if( !ppsz_langs || !psz_lang ) return -1;
1558 1559

    for( i = 0; ppsz_langs[i]; i++ )
1560
        if( !strcasecmp( ppsz_langs[i], psz_lang ) ) return i;
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574

    return -1;
}

/****************************************************************************
 * EsOutAddInfo:
 * - add meta info to the playlist item
 ****************************************************************************/
static void EsOutAddInfo( 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;
    es_format_t    *fmt = &es->fmt;
    char           *psz_cat;
1575
    lldiv_t         div;
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597

    /* Add stream info */
    asprintf( &psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );

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

    input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
                   "%s", es->psz_language );

    /* Add information */
    switch( fmt->i_cat )
    {
    case AUDIO_ES:
        input_Control( p_input, INPUT_ADD_INFO, psz_cat,
                       _("Type"), _("Audio") );

        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 )
1598
        {
1599 1600
            input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
                           _("%d Hz"), fmt->audio.i_rate );
1601 1602
            var_SetInteger( p_input, "sample-rate", fmt->audio.i_rate );
        }
1603 1604 1605 1606 1607 1608 1609

        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 )
1610
        {
1611 1612
            input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
                           _("%d kb/s"), fmt->i_bitrate / 1000 );
1613 1614
            var_SetInteger( p_input, "bit-rate", fmt->i_bitrate );
        }
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
        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);
1632
       if( fmt->video.i_frame_rate > 0 &&
1633
           fmt->video.i_frame_rate_base > 0 )
1634 1635 1636 1637
       {
           div = lldiv( (float)fmt->video.i_frame_rate /
                               fmt->video.i_frame_rate_base * 1000000,
                               1000000 );
1638
           input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1639 1640 1641 1642
                          _("Frame rate"), I64Fd".%06u",
                          div.quot, (unsigned int )div.rem );
       }
       break;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

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

    default:
        break;
    }

    free( psz_cat );
}