input_es.c 12.3 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3 4
/*****************************************************************************
 * input_es.c: Elementary Stream demux and packet management
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: input_es.c,v 1.3 2001/12/10 15:52:31 massiot Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
6
 *
7
 * Author: Christophe Massiot <massiot@via.ecp.fr>
Sam Hocevar's avatar
 
Sam Hocevar committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 *****************************************************************************/

Sam Hocevar's avatar
 
Sam Hocevar committed
24
#define MODULE_NAME mpeg_es
Sam Hocevar's avatar
 
Sam Hocevar committed
25 26 27 28 29 30 31 32 33 34 35 36 37
#include "modules_inner.h"

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
38 39

#ifdef HAVE_UNISTD_H
Sam Hocevar's avatar
 
Sam Hocevar committed
40
#   include <unistd.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
41 42
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
43 44
#include <fcntl.h>

45
#if defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
46
#   include <io.h>                                                 /* read() */
47 48 49 50
#else
#   include <sys/uio.h>                                      /* struct iovec */
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
51
#include "common.h"
52
#include "intf_msg.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
53 54 55 56
#include "threads.h"
#include "mtime.h"
#include "tests.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
57 58 59 60
#if defined( WIN32 )
#   include "input_iovec.h"
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
61 62 63
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
64
#include "input_ext-plugins.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
65 66 67 68 69

#include "input_es.h"

#include "debug.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
70 71 72
#include "modules.h"
#include "modules_export.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
73 74 75 76 77 78
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  ESProbe     ( probedata_t * );
static int  ESRead      ( struct input_thread_s *,
                          data_packet_t * p_packets[INPUT_READ_ONCE] );
79 80 81 82 83 84
static void ESInit          ( struct input_thread_s * );
static void ESEnd           ( struct input_thread_s * );
static void ESSeek          ( struct input_thread_s *, off_t );
static int  ESSetProgram    ( struct input_thread_s *, pgrm_descriptor_t * );
static void ESDemux         ( struct input_thread_s *, 
                                struct data_packet_s * );
85 86 87 88 89 90
static void ESNextDataPacket( struct bit_stream_s * );
static void ESInitBitstream( struct bit_stream_s *, struct decoder_fifo_s *,
                        void (* pf_bitstream_callback)( struct bit_stream_s *,
                                                        boolean_t ),
                        void * );

Sam Hocevar's avatar
 
Sam Hocevar committed
91 92 93 94 95 96 97 98 99 100

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
void _M( input_getfunctions )( function_list_t * p_function_list )
{
#define input p_function_list->functions.input
    p_function_list->pf_probe = ESProbe;
    input.pf_init             = ESInit;
101
    input.pf_open             = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
102
    input.pf_close            = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
103
    input.pf_end              = ESEnd;
104
    input.pf_init_bit_stream  = ESInitBitstream;
Sam Hocevar's avatar
 
Sam Hocevar committed
105
    input.pf_set_area         = NULL;
106
    input.pf_set_program      = ESSetProgram;
Sam Hocevar's avatar
 
Sam Hocevar committed
107 108
    input.pf_read             = ESRead;
    input.pf_demux            = ESDemux;
109 110 111 112
    input.pf_new_packet       = input_NetlistNewPacket;
    input.pf_new_pes          = input_NetlistNewPES;
    input.pf_delete_packet    = input_NetlistDeletePacket;
    input.pf_delete_pes       = input_NetlistDeletePES;
Sam Hocevar's avatar
 
Sam Hocevar committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126
    input.pf_rewind           = NULL;
    input.pf_seek             = ESSeek;
#undef input
}

/*
 * Data reading functions
 */

/*****************************************************************************
 * ESProbe: verifies that the stream is a ES stream
 *****************************************************************************/
static int ESProbe( probedata_t *p_data )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
127
    int i_score = 5;
Sam Hocevar's avatar
 
Sam Hocevar committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141

    if( TestMethod( INPUT_METHOD_VAR, "es" ) )
    {
        return( 999 );
    }

    return( i_score );
}

/*****************************************************************************
 * ESInit: initializes ES structures
 *****************************************************************************/
static void ESInit( input_thread_t * p_input )
{
142
    es_descriptor_t *   p_es;
Sam Hocevar's avatar
 
Sam Hocevar committed
143

144 145 146
    p_input->p_method_data = NULL;

    /* Initialize netlist */
147
    if( input_NetlistInit( p_input, NB_DATA, NB_DATA, NB_PES, ES_PACKET_SIZE,
148
                           INPUT_READ_ONCE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
149
    {
150
        intf_ErrMsg( "ES input : Could not initialize netlist" );
Sam Hocevar's avatar
 
Sam Hocevar committed
151 152 153
        return;
    }

154 155 156 157 158 159 160 161 162 163 164 165
    /* FIXME : detect if InitStream failed */
    input_InitStream( p_input, 0 );
    input_AddProgram( p_input, 0, 0 );
    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xE0, 0 );
    p_es->i_stream_id = 0xE0;
    p_es->i_type = MPEG1_VIDEO_ES;
    p_es->i_cat = VIDEO_ES;
    input_SelectES( p_input, p_es );
    p_input->stream.p_selected_area->i_tell = 0;
    p_input->stream.pp_programs[0]->b_is_ok = 1;
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

/*****************************************************************************
 * ESEnd: frees unused data
 *****************************************************************************/
static void ESEnd( input_thread_t * p_input )
{
}

/*****************************************************************************
 * ESRead: reads data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
 * EOF.
 *****************************************************************************/
static int ESRead( input_thread_t * p_input,
                   data_packet_t * pp_packets[INPUT_READ_ONCE] )
{
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    int             i_read;
    struct iovec  * p_iovec;

    /* Get iovecs */
    p_iovec = input_NetlistGetiovec( p_input->p_method_data );

    if ( p_iovec == NULL )
    {
        return( -1 ); /* empty netlist */
    }

    memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );

    i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
    if( i_read == -1 )
    {
        intf_ErrMsg( "input error: ES readv error" );
        return( -1 );
    }

204 205 206 207 208 209
    /* EOF */
    if( i_read == 0 && p_input->stream.b_seekable )
    {
        return( 1 );
    }

210 211 212 213
    input_NetlistMviovec( p_input->p_method_data,
             (int)(i_read/ES_PACKET_SIZE), pp_packets );

    p_input->stream.p_selected_area->i_tell += i_read;
Sam Hocevar's avatar
 
Sam Hocevar committed
214 215 216 217 218 219 220 221 222

    return( 0 );
}

/*****************************************************************************
 * ESSeek: changes the stream position indicator
 *****************************************************************************/
static void ESSeek( input_thread_t * p_input, off_t i_position )
{
223
    lseek( p_input->i_handle, i_position, SEEK_SET );
Sam Hocevar's avatar
 
Sam Hocevar committed
224 225 226 227

    p_input->stream.p_selected_area->i_tell = i_position;
}

228 229 230 231 232 233 234 235 236

/*****************************************************************************
 * ESSetProgram: Does nothing
 *****************************************************************************/
static int ESSetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
{
    return( 0 );
}

Sam Hocevar's avatar
 
Sam Hocevar committed
237
/*****************************************************************************
238
 * ESDemux: fakes a demultiplexer
Sam Hocevar's avatar
 
Sam Hocevar committed
239
 *****************************************************************************/
240 241 242 243 244
static void ESDemux( input_thread_t * p_input, data_packet_t * p_data )
{
    pes_packet_t *  p_pes = p_input->pf_new_pes( p_input->p_method_data );
    decoder_fifo_t * p_fifo =
        p_input->stream.pp_programs[0]->pp_es[0]->p_decoder_fifo;
Sam Hocevar's avatar
 
Sam Hocevar committed
245

246 247 248 249 250 251
    if( p_pes == NULL )
    {
        intf_ErrMsg("Out of memory");
        p_input->b_error = 1;
        return;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
252

253 254
    p_pes->i_rate = p_input->stream.control.i_rate;
    p_pes->p_first = p_data;
Sam Hocevar's avatar
 
Sam Hocevar committed
255

256 257 258 259 260 261 262 263 264 265
    if( (p_input->stream.pp_programs[0]->i_synchro_state == SYNCHRO_REINIT)
         | (input_ClockManageControl( p_input, p_input->stream.pp_programs[0],
                                  (mtime_t)0 ) == PAUSE_S) )
    {
        intf_WarnMsg( 2, "synchro reinit" );
        p_pes->i_pts = mdate() + DEFAULT_PTS_DELAY;
        p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_OK;
    }

    input_DecodePES( p_fifo, p_pes );
Sam Hocevar's avatar
 
Sam Hocevar committed
266

267 268 269 270 271 272 273
    vlc_mutex_lock( &p_fifo->data_lock );
    if( ( (DECODER_FIFO_END( *p_fifo ) - DECODER_FIFO_START( *p_fifo ))
            & FIFO_SIZE ) >= MAX_PACKETS_IN_FIFO )
    {
        vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
    }
    vlc_mutex_unlock( &p_fifo->data_lock );
Sam Hocevar's avatar
 
Sam Hocevar committed
274 275 276
}

/*****************************************************************************
277 278
 * ESNextDataPacket: signals the input thread if there isn't enough packets
 * available
Sam Hocevar's avatar
 
Sam Hocevar committed
279
 *****************************************************************************/
280
static void ESNextDataPacket( bit_stream_t * p_bit_stream )
Sam Hocevar's avatar
 
Sam Hocevar committed
281
{
282 283 284 285 286 287 288 289 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 328 329 330 331 332 333 334 335 336 337 338
    decoder_fifo_t *    p_fifo = p_bit_stream->p_decoder_fifo;
    boolean_t           b_new_pes;

    /* We are looking for the next data packet that contains real data,
     * and not just a PES header */
    do
    {
        /* We were reading the last data packet of this PES packet... It's
         * time to jump to the next PES packet */
        if( p_bit_stream->p_data->p_next == NULL )
        {
            /* We are going to read/write the start and end indexes of the
             * decoder fifo and to use the fifo's conditional variable,
             * that's why we need to take the lock before. */
            vlc_mutex_lock( &p_fifo->data_lock );

            /* Free the previous PES packet. */
            p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
                                   DECODER_FIFO_START( *p_fifo ) );
            DECODER_FIFO_INCSTART( *p_fifo );

            if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
            {
                /* Signal the input thread we're waiting. */
                vlc_cond_signal( &p_fifo->data_wait );

                /* Wait for the input to tell us when we receive a packet. */
                vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
            }

            /* The next byte could be found in the next PES packet */
            p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;

            vlc_mutex_unlock( &p_fifo->data_lock );

            b_new_pes = 1;
        }
        else
        {
            /* Perhaps the next data packet of the current PES packet contains
             * real data (ie its payload's size is greater than 0). */
            p_bit_stream->p_data = p_bit_stream->p_data->p_next;

            b_new_pes = 0;
        }
    } while ( p_bit_stream->p_data->p_payload_start
               == p_bit_stream->p_data->p_payload_end );

    /* We've found a data packet which contains interesting data... */
    p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
    p_bit_stream->p_end  = p_bit_stream->p_data->p_payload_end;

    /* Call back the decoder. */
    if( p_bit_stream->pf_bitstream_callback != NULL )
    {
        p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
339 340 341
}

/*****************************************************************************
342
 * ESInitBitstream: changes pf_next_data_packet
Sam Hocevar's avatar
 
Sam Hocevar committed
343
 *****************************************************************************/
344 345 346 347 348
static void ESInitBitstream( bit_stream_t * p_bit_stream,
                             decoder_fifo_t * p_decoder_fifo,
                        void (* pf_bitstream_callback)( struct bit_stream_s *,
                                                        boolean_t ),
                            void * p_callback_arg )
Sam Hocevar's avatar
 
Sam Hocevar committed
349
{
350 351 352
    InitBitstream( p_bit_stream, p_decoder_fifo, pf_bitstream_callback,
                   p_callback_arg );
    p_bit_stream->pf_next_data_packet = ESNextDataPacket;
Sam Hocevar's avatar
 
Sam Hocevar committed
353
}