pva.c 13.6 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3
/*****************************************************************************
 * pva.c: PVA demuxer
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2004 VLC authors and VideoLAN
5
 * $Id$
Laurent Aimar's avatar
Laurent Aimar committed
6 7 8
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Laurent Aimar's avatar
Laurent Aimar committed
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Laurent Aimar's avatar
Laurent Aimar committed
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Laurent Aimar's avatar
Laurent Aimar committed
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_demux.h>
Laurent Aimar's avatar
Laurent Aimar committed
35 36 37 38 39 40 41

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close  ( vlc_object_t * );

42 43 44 45 46 47 48 49
vlc_module_begin ()
    set_description( N_("PVA demuxer" ) )
    set_capability( "demux", 10 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_callbacks( Open, Close )
    add_shortcut( "pva" )
vlc_module_end ()
Laurent Aimar's avatar
Laurent Aimar committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

struct demux_sys_t
{
    es_out_id_t *p_video;
    es_out_id_t *p_audio;

    /* counter */
    int          i_vc;
    int          i_ac;

    /* audio/video block */
    block_t     *p_pes; /* audio */
    block_t     *p_es;  /* video */

    int64_t     b_pcr_audio;
};

static int  Demux   ( demux_t *p_demux );
static int  Control ( demux_t *p_demux, int i_query, va_list args );

static int  ReSynch ( demux_t * );
static void ParsePES( demux_t * );

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
    es_format_t  fmt;
85
    const uint8_t *p_peek;
Laurent Aimar's avatar
Laurent Aimar committed
86

87
    if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
88 89 90
    if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
    {
        /* In case we had forced this demuxer we try to resynch */
91
        if( !p_demux->b_force || ReSynch( p_demux ) )
Laurent Aimar's avatar
Laurent Aimar committed
92 93 94
            return VLC_EGENERIC;
    }

95 96 97 98
    p_sys = malloc( sizeof( demux_sys_t ) );
    if( unlikely(p_sys == NULL) )
        return VLC_ENOMEM;

Laurent Aimar's avatar
Laurent Aimar committed
99 100 101
    /* Fill p_demux field */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
102
    p_demux->p_sys = p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
103 104

    /* Register one audio and one video stream */
105
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_MPGA );
106
    fmt.b_packetized = false;
Laurent Aimar's avatar
Laurent Aimar committed
107 108
    p_sys->p_audio = es_out_Add( p_demux->out, &fmt );

109
    es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_MPGV );
110
    fmt.b_packetized = false;
Laurent Aimar's avatar
Laurent Aimar committed
111 112 113 114 115 116 117
    p_sys->p_video = es_out_Add( p_demux->out, &fmt );

    p_sys->i_vc    = -1;
    p_sys->i_ac    = -1;
    p_sys->p_pes   = NULL;
    p_sys->p_es    = NULL;

118
    p_sys->b_pcr_audio = false;
Laurent Aimar's avatar
Laurent Aimar committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

    if( p_sys->p_es )  block_Release( p_sys->p_es );
    if( p_sys->p_pes ) block_Release( p_sys->p_pes );

    free( p_sys );
}


/*****************************************************************************
 * Demux:
140 141
 *****************************************************************************
 * See http://multimedia.cx/mirror/av_format_v1.pdf
Laurent Aimar's avatar
Laurent Aimar committed
142 143 144 145 146
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

147
    const uint8_t *p_peek;
Laurent Aimar's avatar
Laurent Aimar committed
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    int         i_size;
    block_t     *p_frame;
    int64_t     i_pts;
    int         i_skip;

    if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
    {
        msg_Warn( p_demux, "eof ?" );
        return 0;
    }
    if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
    {
        msg_Warn( p_demux, "lost synchro" );
        if( ReSynch( p_demux ) )
        {
            return -1;
        }
        if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
        {
            msg_Warn( p_demux, "eof ?" );
            return 0;
        }
    }

    i_size = GetWBE( &p_peek[6] );
    switch( p_peek[2] )
    {
        case 0x01:  /* VideoStream */
            if( p_sys->i_vc < 0 )
            {
                msg_Dbg( p_demux, "first packet for video" );
            }
            else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
            {
                msg_Dbg( p_demux, "packet lost (video)" );
                if( p_sys->p_es )
                {
                    block_ChainRelease( p_sys->p_es );
                    p_sys->p_es = NULL;
                }
            }
            p_sys->i_vc = p_peek[3];

            /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
            i_pts = -1;
            i_skip = 8;
            if( p_peek[5]&0x10 )
            {
                int i_pre = p_peek[5]&0x3;

                if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
                {
                    i_pts = GetDWBE( &p_frame->p_buffer[8] );
                    if( p_frame->i_buffer > 12 )
                    {
                        p_frame->p_buffer += 12;
                        p_frame->i_buffer -= 12;
                        block_ChainAppend( &p_sys->p_es, p_frame );
                    }
                    else
                    {
                        block_Release( p_frame );
                    }
                }
                i_size -= 4 + i_pre;
                i_skip  = 0;
                /* Set PCR */
                if( ( p_frame = p_sys->p_es ) )
                {

218
                    if( p_frame->i_pts > VLC_TS_INVALID && !p_sys->b_pcr_audio )
Laurent Aimar's avatar
Laurent Aimar committed
219 220 221 222 223 224 225 226 227 228 229 230 231
                    {
                        es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
                    }
                    es_out_Send( p_demux->out, p_sys->p_video, p_frame );

                    p_sys->p_es = NULL;
                }
            }

            if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
            {
                p_frame->p_buffer += i_skip;
                p_frame->i_buffer -= i_skip;
232 233
                if( i_pts >= 0 )
                    p_frame->i_pts = VLC_TS_0 + i_pts * 100 / 9;
Laurent Aimar's avatar
Laurent Aimar committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                block_ChainAppend( &p_sys->p_es, p_frame );
            }
            break;

        case 0x02:  /* MainAudioStream */
            if( p_sys->i_ac < 0 )
            {
                msg_Dbg( p_demux, "first packet for audio" );
            }
            else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
            {
                msg_Dbg( p_demux, "packet lost (audio)" );
                if( p_sys->p_pes )
                {
                    block_ChainRelease( p_sys->p_pes );
                    p_sys->p_pes = NULL;
                }
            }
            p_sys->i_ac = p_peek[3];

            if( p_peek[5]&0x10 && p_sys->p_pes )
            {
                ParsePES( p_demux );
            }
            if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
            {
                p_frame->p_buffer += 8;
                p_frame->i_buffer -= 8;
                /* XXX this a hack, some streams aren't compliant and
263
                 * don't set pes_start flag */
Laurent Aimar's avatar
Laurent Aimar committed
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 292
                if( p_sys->p_pes && p_frame->i_buffer > 4 &&
                    p_frame->p_buffer[0] == 0x00 &&
                    p_frame->p_buffer[1] == 0x00 &&
                    p_frame->p_buffer[2] == 0x01 )
                {
                    ParsePES( p_demux );
                }
                block_ChainAppend( &p_sys->p_pes, p_frame );
            }
            break;

        default:
            msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
            stream_Read( p_demux->s, NULL, i_size + 8 );
            break;
    }
    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    /* demux_sys_t *p_sys = p_demux->p_sys; */
    double f, *pf;
    int64_t i64;
    switch( i_query )
    {
293 294 295
        case DEMUX_CAN_SEEK:
            return stream_vaControl( p_demux->s, i_query, args );

Laurent Aimar's avatar
Laurent Aimar committed
296 297 298 299
        case DEMUX_GET_POSITION:
            if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
            {
                pf = (double*) va_arg( args, double* );
300 301
                double current = stream_Tell( p_demux->s );
                *pf = current / (double)i64;
Laurent Aimar's avatar
Laurent Aimar committed
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 339 340 341 342 343
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_SET_POSITION:
            f = (double) va_arg( args, double );
            i64 = stream_Size( p_demux->s );

            if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
            {
                return VLC_EGENERIC;
            }
            return VLC_SUCCESS;

#if 0
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_time < 0 )
            {
                *pi64 = 0;
                return VLC_EGENERIC;
            }
            *pi64 = p_sys->i_time;
            return VLC_SUCCESS;

#if 0
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_mux_rate > 0 )
            {
                *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
                return VLC_SUCCESS;
            }
            *pi64 = 0;
            return VLC_EGENERIC;

#endif
        case DEMUX_GET_FPS:
            pf = (double*)va_arg( args, double * );
            *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
            return VLC_SUCCESS;
#endif
344
        case DEMUX_SET_TIME:
Laurent Aimar's avatar
Laurent Aimar committed
345 346 347 348 349 350 351 352 353 354
        default:
            return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * ReSynch:
 *****************************************************************************/
static int ReSynch( demux_t *p_demux )
{
355
    for( ;; )
Laurent Aimar's avatar
Laurent Aimar committed
356
    {
357 358 359 360 361 362
        const uint8_t *p_peek;
        int i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
        if( i_peek < 8 )
            break;

        int i_skip = 0;
Laurent Aimar's avatar
Laurent Aimar committed
363 364 365 366 367

        while( i_skip < i_peek - 5 )
        {
            if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
            {
368 369 370
                if( i_skip > 0
                 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
                    return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
371 372 373 374 375 376
                return VLC_SUCCESS;
            }
            p_peek++;
            i_skip++;
        }

377 378
        if( stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
            break;
Laurent Aimar's avatar
Laurent Aimar committed
379 380 381 382 383 384 385 386 387 388 389
    }

    return VLC_EGENERIC;
}

static void ParsePES( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t     *p_pes = p_sys->p_pes;
    uint8_t     hdr[30];

390
    unsigned    i_skip;
Laurent Aimar's avatar
Laurent Aimar committed
391 392 393 394 395 396 397 398
    mtime_t     i_dts = -1;
    mtime_t     i_pts = -1;

    p_sys->p_pes = NULL;

    /* FIXME find real max size */
    block_ChainExtract( p_pes, hdr, 30 );

399
    /* See §2.4.3.6 of ISO 13818-1 */
Laurent Aimar's avatar
Laurent Aimar committed
400 401 402 403 404 405 406
    if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
    {
        msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
                  hdr[0], hdr[1],hdr[2],hdr[3] );
        block_ChainRelease( p_pes );
        return;
    }
407 408
    // hdr[4] i_pes_size, 2 bytes
    // hdr[6] Marker -> original_or_copy
Laurent Aimar's avatar
Laurent Aimar committed
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 437 438 439

    /* we assume mpeg2 PES */
    i_skip = hdr[8] + 9;
    if( hdr[7]&0x80 )    /* has pts */
    {
        i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
                 (mtime_t)(hdr[10] << 22)|
                ((mtime_t)(hdr[11]&0xfe) << 14)|
                 (mtime_t)(hdr[12] << 7)|
                 (mtime_t)(hdr[12] >> 1);

        if( hdr[7]&0x40 )    /* has dts */
        {
             i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
                     (mtime_t)(hdr[15] << 22)|
                    ((mtime_t)(hdr[16]&0xfe) << 14)|
                     (mtime_t)(hdr[17] << 7)|
                     (mtime_t)(hdr[18] >> 1);
        }
    }

    p_pes = block_ChainGather( p_pes );
    if( p_pes->i_buffer <= i_skip )
    {
        block_ChainRelease( p_pes );
        return;
    }

    p_pes->i_buffer -= i_skip;
    p_pes->p_buffer += i_skip;

440 441 442 443
    if( i_dts >= 0 )
        p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
    if( i_pts >= 0 )
        p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
Laurent Aimar's avatar
Laurent Aimar committed
444 445 446 447 448

    /* Set PCR */
    if( p_pes->i_pts > 0 )
    {
        es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
449
        p_sys->b_pcr_audio = true;
Laurent Aimar's avatar
Laurent Aimar committed
450 451 452 453
    }
    es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
}