input_programs.c 16.3 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
5
 * $Id: input_programs.c,v 1.10 2000/12/21 13:25:51 massiot Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Authors:
 *
 * 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
 *****************************************************************************/
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "defs.h"

#include <stdlib.h>

#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
#include "debug.h"

#include "intf_msg.h"

#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
#include "input.h"

44 45
#include "main.h"                                     /* --noaudio --novideo */

46 47 48 49
/*
 * NOTICE : all of these functions expect you to have taken the lock on
 * p_input->stream.lock
 */
50 51 52 53

/*****************************************************************************
 * input_InitStream: init the stream descriptor of the given input
 *****************************************************************************/
54
void input_InitStream( input_thread_t * p_input, size_t i_data_len )
55
{
56
    p_input->stream.i_stream_id = 0;
57 58 59 60 61 62 63 64
    p_input->stream.i_pgrm_number = 0;
    p_input->stream.pp_programs = NULL;

    if( i_data_len )
    {
        p_input->stream.p_demux_data = malloc( i_data_len );
        memset( p_input->stream.p_demux_data, 0, i_data_len );
    }
65 66 67 68 69 70 71 72
}

/*****************************************************************************
 * input_AddProgram: add and init a program descriptor
 *****************************************************************************
 * This program descriptor will be referenced in the given stream descriptor
 *****************************************************************************/
pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
73
                                      u16 i_pgrm_id, size_t i_data_len )
74
{
75 76
    /* Where to add the pgrm */
    int i_pgrm_index = p_input->stream.i_pgrm_number;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);

    /* Add an entry to the list of program associated with the stream */
    p_input->stream.i_pgrm_number++;
    p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
                                           p_input->stream.i_pgrm_number
                                            * sizeof(pgrm_descriptor_t *) );

    /* Allocate the structure to store this description */
    p_input->stream.pp_programs[i_pgrm_index] =
                                        malloc( sizeof(pgrm_descriptor_t) );

    /* Init this entry */
    p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
    p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
93
    p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
94 95 96 97 98 99 100 101 102 103

    p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
    p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;

    p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
    p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
    p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
    p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
    p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
                                                = SYNCHRO_NOT_STARTED;
104
    p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
105 106 107 108 109 110

    p_input->stream.pp_programs[i_pgrm_index]->p_vout
                                            = p_input->p_default_vout;
    p_input->stream.pp_programs[i_pgrm_index]->p_aout
                                            = p_input->p_default_aout;

111 112 113 114 115 116 117 118
    if( i_data_len )
    {
        p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
            malloc( i_data_len );
        memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
                i_data_len );
    }

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
    return p_input->stream.pp_programs[i_pgrm_index];
}

/*****************************************************************************
 * input_DelProgram: destroy a program descriptor
 *****************************************************************************
 * All ES descriptions referenced in the descriptor will be deleted.
 *****************************************************************************/
void input_DelProgram( input_thread_t * p_input, u16 i_pgrm_id )
{
    int i_index, i_pgrm_index = -1;
    pgrm_descriptor_t * p_pgrm = NULL;

    intf_DbgMsg("Deleting description for pgrm %d", i_pgrm_id);

    /* Find where this program is described */
    for( i_index = 0; i_index < p_input->stream.i_pgrm_number; i_index++ )
    {
        if( p_input->stream.pp_programs[i_index]->i_number == i_pgrm_id )
        {
            i_pgrm_index = i_index;
            p_pgrm = p_input->stream.pp_programs[ i_pgrm_index ];
            break;
        }
    }

    /* Make sure that the pgrm exists */
    ASSERT(i_pgrm_index >= 0);
    ASSERT(p_pgrm);

    /* Free the structures that describe the es that belongs to that program */
    for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
    {
152
        input_DelES( p_input, p_pgrm->pp_es[i_index]->i_id );
153 154 155 156 157
    }

    /* Free the table of es descriptors */
    free( p_pgrm->pp_es );

158 159 160 161 162 163
    /* Free the demux data */
    if( p_pgrm->p_demux_data != NULL )
    {
        free( p_pgrm->p_demux_data );
    }

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    /* Free the description of this stream */
    free( p_pgrm );

    /* Remove this program from the stream's list of programs */
    p_input->stream.i_pgrm_number--;
    p_input->stream.pp_programs[i_pgrm_index] =
        p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
    p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
                                           p_input->stream.i_pgrm_number
                                            * sizeof(pgrm_descriptor_t *) );
}

/*****************************************************************************
 * input_AddES:
 *****************************************************************************
 * Reserve a slot in the table of ES descriptors for the ES and add it to the
 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
 * alone (PSI ?)
 *****************************************************************************/
es_descriptor_t * input_AddES( input_thread_t * p_input,
184 185
                               pgrm_descriptor_t * p_pgrm, u16 i_es_id,
                               size_t i_data_len )
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
{
    int i_index;
    es_descriptor_t * p_es = NULL;

    intf_DbgMsg("Adding description for ES %d", i_es_id);

    /* Find an empty slot to store the description of that es */
    for( i_index = 0; i_index < INPUT_MAX_ES &&
         p_input->p_es[i_index].i_id != EMPTY_ID; i_index++ );

    if( i_index >= INPUT_MAX_ES )
    {
        /* No slot is empty */
        intf_ErrMsg("Stream carries too many ES for our decoder");
    }
    else
    {
        /* Reserve the slot for that ES */
        p_es = &p_input->p_es[i_index];
        p_es->i_id = i_es_id;
        intf_DbgMsg("Slot %d in p_es table assigned to ES %d",
207
                    i_index, i_es_id);
208 209 210

        /* Init its values */
        p_es->b_discontinuity = 0;
211
        p_es->p_pes = NULL;
212 213
        p_es->p_decoder_fifo = NULL;

214 215 216 217 218 219
        if( i_data_len )
        {
            p_es->p_demux_data = malloc( i_data_len );
            memset( p_es->p_demux_data, 0, i_data_len );
        }

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        /* Add this ES to the program definition if one is given */
        if( p_pgrm )
        {
            p_pgrm->i_es_number++;
            p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                     p_pgrm->i_es_number
                                      * sizeof(es_descriptor_t *) );
            p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
            p_es->p_pgrm = p_pgrm;
        }
        else
        {
            p_es->p_pgrm = NULL;
        }
    }

    return p_es;
}

/*****************************************************************************
 * input_DelES:
 *****************************************************************************/
242
void input_DelES( input_thread_t * p_input, u16 i_id )
243 244
{
    int                     i_index;
245 246
    pgrm_descriptor_t *     p_pgrm = NULL;
    es_descriptor_t *       p_es = NULL;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

    /* Look for the description of the ES */
    for( i_index = 0; i_index < INPUT_MAX_ES; i_index++ )
    {
        if( p_input->p_es[i_index].i_id == i_id )
        {
            p_es = &p_input->p_es[i_index];
            p_pgrm = p_input->p_es[i_index].p_pgrm;
            break;
        }
    }

    ASSERT( p_es );

    /* Remove this ES from the description of the program if it is associated to
     * one */
    if( p_pgrm )
    {
        for( i_index = 0; ; i_index++ )
        {
267
            if( p_pgrm->pp_es[i_index]->i_id == i_id )
268 269
            {
                p_pgrm->i_es_number--;
270
                p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
271 272
                p_pgrm->pp_es = realloc( p_pgrm->pp_es,
                                         p_pgrm->i_es_number
273
                                          * sizeof(es_descriptor_t *));
274 275 276 277 278 279 280 281
                break;
            }
        }
    }

    /* The table of stream descriptors is static, so don't free memory
     * but just mark the slot as unused */
    p_es->i_id = EMPTY_ID;
282 283 284 285 286 287 288 289

    /* Free the demux data */
    if( p_es->p_demux_data != NULL )
    {
        free( p_es->p_demux_data );
    }
}

290 291 292 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
#ifdef STATS
/*****************************************************************************
 * input_DumpStream: dumps the contents of a stream descriptor
 *****************************************************************************/
void input_DumpStream( input_thread_t * p_input )
{
    int i, j;
#define S   p_input->stream
    intf_Msg( "input info: Dumping stream ID 0x%x\n", S.i_stream_id );
    if( S.b_seekable )
        intf_Msg( "input info: seekable stream, position: %d/%d\n",
                  S.i_tell, S.i_size );
    else
        intf_Msg( "input info: %s\n", S.b_pace_control ? "pace controlled" :
                  "pace un-controlled" );
#undef S
    for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
    {
#define P   p_input->stream.pp_programs[i]
        intf_Msg( "input info: Dumping program 0x%x, version %d (%s)\n",
                  P->i_number, P->i_version,
                  P->b_is_ok ? "complete" : "partial" );
        if( P->i_synchro_state == SYNCHRO_OK )
            intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)\n",
                      P->delta_absolute, P->delta_cr );
#undef P
        for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
        {
#define ES  p_input->stream.pp_programs[i]->pp_es[j]
            intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s\n",
                      ES->i_id, ES->i_stream_id, ES->i_type,
                      ES->p_decoder_fifo != NULL ? "selected" : "not selected");
#undef ES
        }
    }
}
#endif

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
/*****************************************************************************
 * InitDecConfig: initializes a decoder_config_t
 *****************************************************************************/
static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
                          decoder_config_t * p_config )
{
    p_config->i_stream_id = p_es->i_stream_id;
    p_config->i_type = p_es->i_type;
    p_config->p_stream_ctrl =
        &p_input->stream.control;

    /* Decoder FIFO */
    if( (p_config->p_decoder_fifo =
            (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        return( -1 );
    }

    vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
    vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
    p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
350
    p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
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 383 384 385 386 387 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
    p_config->p_decoder_fifo->pf_delete_pes =
        p_input->p_plugin->pf_delete_pes;
    p_es->p_decoder_fifo = p_config->p_decoder_fifo;

    p_config->pf_init_bit_stream = InitBitstream;

    return( 0 );
}

/*****************************************************************************
 * GetVdecConfig: returns a valid vdec_config_t
 *****************************************************************************/
static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
                                      es_descriptor_t * p_es )
{
    vdec_config_t *     p_config;

    p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
    p_config->p_vout = p_input->p_default_vout;
    if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
    {
        free( p_config );
        return NULL;
    }

    return( p_config );
}

/*****************************************************************************
 * GetAdecConfig: returns a valid adec_config_t
 *****************************************************************************/
static adec_config_t * GetAdecConfig( input_thread_t * p_input,
                                      es_descriptor_t * p_es )
{
    adec_config_t *     p_config;

    p_config = (adec_config_t *)malloc( sizeof(adec_config_t) );
    p_config->p_aout = p_input->p_default_aout;
    if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
    {
        free( p_config );
        return NULL;
    }

    return( p_config );
}

/*****************************************************************************
 * input_SelectES: selects an ES and spawns the associated decoder
 *****************************************************************************/
int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
{
    int                 i;
    es_descriptor_t **  p_spot = NULL;

#ifdef DEBUG_INPUT
    intf_DbgMsg( "Selecting ES %d", p_es->i_id );
#endif

    if( p_es->p_decoder_fifo != NULL )
    {
        intf_ErrMsg( "ES %d is already selected", p_es->i_id );
        return( -1 );
    }

    /* Find a free spot in pp_selected_es. */
    for( i = 0; i < INPUT_MAX_SELECTED_ES; i++ )
    {
        if( p_input->pp_selected_es[i] == NULL )
        {
            p_spot = &p_input->pp_selected_es[i];
            break;
        }
    }

    if( p_spot == NULL )
    {
        intf_ErrMsg( "Too many ES selected" );
        return( -1 );
    }

    switch( p_es->i_type )
    {
    case MPEG1_AUDIO_ES:
    case MPEG2_AUDIO_ES:
437 438 439 440 441
        if( p_main->b_audio )
        {
            p_es->thread_id = adec_CreateThread( GetAdecConfig( p_input,
                                                                p_es ) );
        }
442 443 444 445
        break;

    case MPEG1_VIDEO_ES:
    case MPEG2_VIDEO_ES:
446 447 448 449 450
        if( p_main->b_video )
        {
            p_es->thread_id = vpar_CreateThread( GetVdecConfig( p_input,
                                                                p_es ) );
        }
451 452
        break;

453
    case AC3_AUDIO_ES:
454 455 456 457 458
        if( p_main->b_audio )
        {
            p_es->thread_id = ac3dec_CreateThread( GetAdecConfig( p_input,
                                                                  p_es ) );
        }
459 460 461
        break;

    case DVD_SPU_ES:
462 463 464 465 466
        if( p_main->b_video )
        {
            p_es->thread_id = spudec_CreateThread( GetVdecConfig( p_input,
                                                                  p_es ) );
        }
467 468
        break;

469 470 471 472 473 474
    default:
        intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
        return( -1 );
        break;
    }

475 476 477 478
    if( p_es->p_decoder_fifo != NULL )
    {
        *p_spot = p_es;
    }
479
    return( 0 );
480
}