real.c 52 KB
Newer Older
1 2 3
/*****************************************************************************
 * real.c: Real demuxer.
 *****************************************************************************
4
 * Copyright (C) 2004, 2006-2007 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26 27 28 29 30 31 32 33
/**
 * Status of this demuxer:
 * Real Media format
 * -----------------
 *
 * version v3 w/ 14_4/lpcJ is ok.
 * version v4/5: - atrac3 is ok.
 *               - cook is ok.
 *               - raac, racp are ok.
 *               - dnet is twisted "The byte order of the data is reversed
34 35
 *                                  from standard AC3" but ok
 *               - 28_8 is ok.
36
 *               - sipr is ok.
37 38 39 40 41
 *               - ralf is unsupported, but hardly any sample exist.
 *               - mp3 is unsupported, one sample exists...
 *
 * Real Audio Only
 * ---------------
42 43
 * v3 and v4/5 headers are parsed.
 * Doesn't work yet...
44 45
 */

46 47 48
/*****************************************************************************
 * Preamble
 *****************************************************************************/
49

50 51 52 53
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

54
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
55
#include <vlc_common.h>
56
#include <vlc_plugin.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
57

58 59 60
#include <vlc_demux.h>
#include <vlc_charset.h>
#include <vlc_meta.h>
61

62 63
#include <assert.h>

64 65 66 67 68 69
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close  ( vlc_object_t * );

70 71
vlc_module_begin ()
    set_description( N_("Real demuxer" ) )
72
    set_capability( "demux", 0 )
73 74 75
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_callbacks( Open, Close )
76
    add_shortcut( "real", "rm" )
77
vlc_module_end ()
78 79 80 81 82 83 84 85 86 87 88 89

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

typedef struct
{
    int         i_id;
    es_format_t fmt;

    es_out_id_t *p_es;

90
    unsigned    i_frame_size;
91 92

    int         i_frame_num;
93
    unsigned    i_frame_pos;
94 95
    int         i_frame_slice;
    int         i_frame_slice_count;
96 97
    block_t     *p_frame;

98 99 100 101 102 103 104
    int         i_subpacket_h;
    int         i_subpacket_size;
    int         i_coded_frame_size;

    int         i_subpacket;
    int         i_subpackets;
    block_t     **p_subpackets;
105
    mtime_t     *p_subpackets_timecode;
106
    int         i_out_subpacket;
107

108 109
    block_t     *p_sipr_packet;
    int         i_sipr_subpacket_count;
110
    mtime_t     i_last_dts;
111 112
} real_track_t;

Wang Bo's avatar
Wang Bo committed
113 114
typedef struct
{
115 116 117
    uint32_t i_file_offset;
    uint32_t i_time_offset;
    uint32_t i_frame_index;
118
} real_index_t;
Wang Bo's avatar
Wang Bo committed
119

120 121 122 123 124 125 126 127
struct demux_sys_t
{
    int64_t  i_data_offset;
    int64_t  i_data_size;
    uint32_t i_data_packets_count;
    uint32_t i_data_packets;
    int64_t  i_data_offset_next;

128
    bool     b_real_audio;
129

130
    int64_t i_our_duration;
131

Felix Paul Kühne's avatar
Felix Paul Kühne committed
132 133 134 135 136
    char* psz_title;
    char* psz_artist;
    char* psz_copyright;
    char* psz_description;

137 138 139
    int          i_track;
    real_track_t **track;

140
    size_t     i_buffer;
141 142 143
    uint8_t buffer[65536];

    int64_t     i_pcr;
Wang Bo's avatar
Wang Bo committed
144

145
    int64_t     i_index_offset;
146
    bool        b_seek;
147
    real_index_t *p_index;
148 149
};

150 151
static const unsigned char i_subpacket_size_sipr[4] = { 29, 19, 37, 20 };

152 153 154 155 156 157 158 159 160
static int Demux( demux_t * );
static int Control( demux_t *, int i_query, va_list args );


static void DemuxVideo( demux_t *, real_track_t *tk, mtime_t i_dts, unsigned i_flags );
static void DemuxAudio( demux_t *, real_track_t *tk, mtime_t i_pts, unsigned i_flags );

static int ControlSeekByte( demux_t *, int64_t i_bytes );
static int ControlSeekTime( demux_t *, mtime_t i_time );
161 162

static int HeaderRead( demux_t *p_demux );
163 164 165 166 167 168 169
static int CodecParse( demux_t *p_demux, int i_len, int i_num );

static void     RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
static int      RLength( const uint8_t **pp_data, int *pi_data );
static uint8_t  R8( const uint8_t **pp_data, int *pi_data );
static uint16_t R16( const uint8_t **pp_data, int *pi_data );
static uint32_t R32( const uint8_t **pp_data, int *pi_data );
170
static void     SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize);
171 172 173 174 175 176 177 178 179

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

180
    const uint8_t *p_peek;
181
    bool           b_real_audio = false;
182

183 184
    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
        return VLC_EGENERIC;
185 186

    /* Real Audio */
187 188
    if( !memcmp( p_peek, ".ra", 3 ) )
    {
189
        msg_Err( p_demux, ".ra files unsupported" );
190
        b_real_audio = true;
191
    }
192
    /* Real Media Format */
193 194 195 196
    else if( memcmp( p_peek, ".RMF", 4 ) )
    {
        return VLC_EGENERIC;
    }
197 198 199 200

    /* Fill p_demux field */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
201

202 203
    p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
    if( !p_sys )
204
        return VLC_ENOMEM;
205

206 207 208
    p_sys->i_data_offset = 0;
    p_sys->i_track = 0;
    p_sys->track   = NULL;
209
    p_sys->i_pcr   = VLC_TS_INVALID;
210

211 212
    p_sys->b_seek  = false;
    p_sys->b_real_audio = b_real_audio;
213 214

    /* Parse the headers */
215
    /* Real Audio files */
216
    if( b_real_audio )
217
    {
218
        CodecParse( p_demux, 32, 0 ); /* At least 32 */
219 220 221 222 223
        return VLC_EGENERIC;                     /* We don't know how to read
                                                    correctly the data yet */
    }
    /* RMF files */
    else if( HeaderRead( p_demux ) )
224
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
225
        msg_Err( p_demux, "invalid header" );
226
        Close( p_this );
227 228 229 230 231 232 233 234 235 236 237 238 239 240
        return VLC_EGENERIC;
    }

    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;

241
    for( int i = 0; i < p_sys->i_track; i++ )
242 243 244
    {
        real_track_t *tk = p_sys->track[i];

245
        es_format_Clean( &tk->fmt );
246

247 248 249 250
        if( tk->p_frame )
            block_Release( tk->p_frame );

        for( int j = 0; j < tk->i_subpackets; j++ )
251
        {
252 253
            if( tk->p_subpackets[ j ] )
                block_Release( tk->p_subpackets[ j ] );
254
        }
255 256
        free( tk->p_subpackets );
        free( tk->p_subpackets_timecode );
257 258
        if( tk->p_sipr_packet )
            block_Release( tk->p_sipr_packet );
259 260
        free( tk );
    }
261 262
    if( p_sys->i_track > 0 )
        free( p_sys->track );
263

264 265 266 267
    free( p_sys->psz_title );
    free( p_sys->psz_artist );
    free( p_sys->psz_copyright );
    free( p_sys->psz_description );
Wang Bo's avatar
Wang Bo committed
268
    free( p_sys->p_index );
269

270 271 272 273 274 275 276
    free( p_sys );
}


/*****************************************************************************
 * Demux:
 *****************************************************************************/
277 278 279 280 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
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t     header[18];

    if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
        p_sys->i_data_packets_count )
    {
        if( stream_Read( p_demux->s, header, 18 ) < 18 )
            return 0;

        if( memcmp( header, "DATA", 4 ) )
            return 0;

        p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
        p_sys->i_data_size   = GetDWBE( &header[4] );
        p_sys->i_data_packets_count = GetDWBE( &header[10] );
        p_sys->i_data_packets = 0;
        p_sys->i_data_offset_next = GetDWBE( &header[14] );

        msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
                 p_sys->i_data_packets_count,
                 (unsigned int)p_sys->i_data_offset_next );
    }

    /* Read Packet Header */
    if( stream_Read( p_demux->s, header, 12 ) < 12 )
        return 0;
    //const int i_version = GetWBE( &header[0] );
306
    const size_t  i_size = GetWBE( &header[2] ) - 12;
307
    const int     i_id   = GetWBE( &header[4] );
308
    const int64_t i_pts  = VLC_TS_0 + 1000 * GetDWBE( &header[6] );
309 310 311
    const int     i_flags= header[11]; /* flags 0x02 -> keyframe */

    p_sys->i_data_packets++;
312
    if( i_size > sizeof(p_sys->buffer) )
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 344 345
    {
        msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
        return 1;
    }

    p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
    if( p_sys->i_buffer < i_size )
        return 0;

    real_track_t *tk = NULL;
    for( int i = 0; i < p_sys->i_track; i++ )
    {
        if( p_sys->track[i]->i_id == i_id )
            tk = p_sys->track[i];
    }

    if( !tk )
    {
        msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
        return 1;
    }

    if( tk->fmt.i_cat == VIDEO_ES )
    {
        DemuxVideo( p_demux, tk, i_pts, i_flags );
    }
    else
    {
        assert( tk->fmt.i_cat == AUDIO_ES );
        DemuxAudio( p_demux, tk, i_pts, i_flags );
    }

    /* Update PCR */
346
    mtime_t i_pcr = VLC_TS_INVALID;
347 348 349 350
    for( int i = 0; i < p_sys->i_track; i++ )
    {
        real_track_t *tk = p_sys->track[i];

351
        if( i_pcr <= VLC_TS_INVALID || ( tk->i_last_dts > VLC_TS_INVALID && tk->i_last_dts < i_pcr ) )
352 353
            i_pcr = tk->i_last_dts;
    }
354
    if( i_pcr > VLC_TS_INVALID && i_pcr != p_sys->i_pcr )
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    {
        p_sys->i_pcr = i_pcr;
        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
    }
    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;
    int64_t *pi64;

    switch( i_query )
    {
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
        case DEMUX_CAN_SEEK:
        {
            bool *b = va_arg( args, bool * );

            if( stream_Size( p_demux->s ) == 0 )
                *b = true; /* FIXME: kludge for Real-dialectal RTSP */
            else
            if( p_sys->p_index == NULL )
                *b = false;
            else
            if( stream_Control( p_demux->s, STREAM_CAN_SEEK, &b ) )
                *b = false;

            return VLC_SUCCESS;
        }

390 391 392 393 394 395 396
        case DEMUX_GET_POSITION:
            pf = (double*) va_arg( args, double* );

            /* read stream size maybe failed in rtsp streaming, 
               so use duration to determin the position at first  */
            if( p_sys->i_our_duration > 0 )
            {
397 398 399 400
                if( p_sys->i_pcr > VLC_TS_INVALID )
                    *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
                else
                    *pf = 0.0;
401 402 403 404 405 406
                return VLC_SUCCESS;
            }

            i64 = stream_Size( p_demux->s );
            if( i64 > 0 )
                *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
407 408
            else
                *pf = 0.0;
409 410 411 412 413 414 415
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );

            if( p_sys->i_our_duration > 0 )
            {
416
                *pi64 = p_sys->i_pcr > VLC_TS_INVALID ? p_sys->i_pcr : 0;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
                return VLC_SUCCESS;
            }

            /* same as GET_POSTION */
            i64 = stream_Size( p_demux->s );
            if( p_sys->i_our_duration > 0 && i64 > 0 )
            {
                *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
                return VLC_SUCCESS;
            }

            *pi64 = 0;
            return VLC_EGENERIC;

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

            if( !p_sys->p_index && i64 != 0 )
            {
                /* TODO seek */
                msg_Err( p_demux,"Seek No Index Real File failed!" );
                return VLC_EGENERIC; // no index!
            }
            else if( i64 == 0 )
            {
                /* it is a rtsp stream , it is specials in access/rtsp/... */
                msg_Dbg(p_demux, "Seek in real rtsp stream!");
445
                p_sys->i_pcr = VLC_TS_0 + INT64_C(1000) * ( p_sys->i_our_duration * f  );
446
                p_sys->b_seek = true;
447
                return stream_Seek( p_demux->s, p_sys->i_pcr - VLC_TS_0 );
448 449 450 451 452 453 454 455 456 457 458 459 460 461
            }
            return ControlSeekByte( p_demux, i64 );

        case DEMUX_SET_TIME:
            if( !p_sys->p_index )
                return VLC_EGENERIC;

            i64 = (int64_t) va_arg( args, int64_t );
            return ControlSeekTime( p_demux, i64 );

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
 
            if( p_sys->i_our_duration <= 0 )
462 463
            {
                *pi64 = 0;
464
                return VLC_EGENERIC;
465
            }
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

            /* our stored duration is in ms, so... */
            *pi64 = INT64_C(1000) * p_sys->i_our_duration;
            return VLC_SUCCESS;

        case DEMUX_GET_META:
        {
            vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );

            /* the core will crash if we provide NULL strings, so check
             * every string first */
            if( p_sys->psz_title )
                vlc_meta_SetTitle( p_meta, p_sys->psz_title );
            if( p_sys->psz_artist )
                vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
            if( p_sys->psz_copyright )
                vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
            if( p_sys->psz_description )
                vlc_meta_SetDescription( p_meta, p_sys->psz_description );
            return VLC_SUCCESS;
        }

        case DEMUX_GET_FPS:
        default:
            return VLC_EGENERIC;
    }
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Helpers: demux
 *****************************************************************************/
498
static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
499 500 501
{
    demux_sys_t *p_sys = p_demux->p_sys;

502
    if( i_dts > VLC_TS_INVALID )
503 504
        tk->i_last_dts = i_dts;

505
    if( p_sys->i_pcr > VLC_TS_INVALID || i_dts <= VLC_TS_INVALID )
506 507 508 509 510 511 512 513 514 515 516 517 518 519
        return;

    p_sys->i_pcr = i_dts;
    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
}

static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    const uint8_t *p_data = p_sys->buffer;
    int     i_data = p_sys->i_buffer;

    while( i_data > 1 )
520
    {
521 522 523 524 525 526 527 528 529
        uint8_t i_hdr = R8( &p_data, &i_data );
        uint8_t i_type = i_hdr >> 6;

        uint8_t i_seq;
        int i_len;
        int i_pos;
        int i_frame_num;

        if( i_type == 1 )
530
        {
531 532 533 534 535 536
            R8( &p_data, &i_data );
            i_len = i_data;
            i_pos = 0;
            i_frame_num = -1;
            i_seq = 1;
            i_hdr &= ~0x3f;
537
        }
538
        else if( i_type == 3 )
539
        {
540 541 542 543 544
            i_len = RLength( &p_data, &i_data );
            i_pos = RLength( &p_data, &i_data );
            i_frame_num = R8( &p_data, &i_data );
            i_seq = 1;
            i_hdr &= ~0x3f;
545
        }
546 547 548 549 550
        else
        {
            assert( i_type == 0 || i_type == 2 );
            i_seq = R8( &p_data, &i_data );
            i_len = RLength( &p_data, &i_data );
551

552 553 554
            i_pos = RLength( &p_data, &i_data );
            i_frame_num = R8( &p_data, &i_data );
        }
555

556 557 558 559 560 561 562
        if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
        {
            tk->i_frame_slice = 0;
            tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
            tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
            tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
            tk->i_frame_num = i_frame_num;
563

564 565
            if( tk->p_frame )
                block_Release( tk->p_frame );
566

567
            tk->p_frame = block_Alloc( tk->i_frame_size );
568 569
            if( !tk->p_frame )
            {
570
                tk->i_frame_size = 0;
571 572
                return;
            }
573

574
            tk->p_frame->i_dts = i_dts;
575
            tk->p_frame->i_pts = VLC_TS_INVALID;
576 577
            if( i_flags & 0x02 )
                tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
578

579
            i_dts = VLC_TS_INVALID;
580
        }
581

582 583 584 585 586 587 588 589 590 591 592
        int i_frame_data;
        if( i_type == 3 )
        {
            i_frame_data = i_len;
        }
        else
        {
            i_frame_data = i_data;
            if( i_type == 2 && i_frame_data > i_pos )
                i_frame_data = i_pos;
        }
593 594
        if( i_frame_data > i_data )
            break;
595 596 597

        /* */
        tk->i_frame_slice++;
598
        if( tk->i_frame_slice > tk->i_frame_slice_count || !tk->p_frame )
599
            break;
600

601 602 603
        /* */
        SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
        SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 4], tk->i_frame_pos - (2*4 * tk->i_frame_slice_count + 1) );
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
        if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
            break;

        memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
        RVoid( &p_data, &i_data, i_frame_data );
        tk->i_frame_pos += i_frame_data;

        if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
        {
            /* Fix the buffer once the real number of slice is known */
            tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
            tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );

            memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice      ],
                     &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
                     tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );

            /* Send it */
            CheckPcr( p_demux, tk, tk->p_frame->i_dts );
            es_out_Send( p_demux->out, tk->p_es, tk->p_frame );

            tk->i_frame_size = 0;
            tk->p_frame = NULL;
        }
629
    }
630 631 632 633 634 635
}

static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t *p_buf = p_sys->buffer;
636

637 638
    /* Sanity check */
    if( (i_flags & 2) || p_sys->b_seek )
639
    {
640
        tk->i_subpacket = 0;
641 642
        tk->i_out_subpacket = 0;
        p_sys->b_seek = false;
643 644
    }

645
    if( tk->fmt.i_codec == VLC_CODEC_COOK ||
646
        tk->fmt.i_codec == VLC_CODEC_ATRAC3 )
647
    {
648
        const int i_num = tk->i_frame_size / tk->i_subpacket_size;
649 650
        const int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );

651
        for( int i = 0; i < i_num; i++ )
652
        {
653 654 655 656 657
            int i_index = tk->i_subpacket_h * i +
                          ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
            if( i_index >= tk->i_subpackets )
                return;

658
            block_t *p_block = block_Alloc( tk->i_subpacket_size );
659 660 661 662
            if( !p_block )
                return;
            if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
                return;
663

664 665
            memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
            p_block->i_dts =
666
            p_block->i_pts = VLC_TS_INVALID;
667

668
            p_buf += tk->i_subpacket_size;
669

670
            if( tk->p_subpackets[i_index] != NULL )
671
            {
672 673
                msg_Dbg(p_demux, "p_subpackets[ %d ] not null!",  i_index );
                block_Release( tk->p_subpackets[i_index] );
674 675
            }

676 677 678 679 680 681 682 683
            tk->p_subpackets[i_index] = p_block;
            if( tk->i_subpacket == 0 )
                tk->p_subpackets_timecode[0] = i_pts;
            tk->i_subpacket++;
        }
    }
    else
    {
684
        const int y = tk->i_subpacket / (tk->i_subpacket_h / 2);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
685
        assert( tk->fmt.i_codec == VLC_CODEC_RA_288 );
686

687 688
        for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
        {
689 690 691 692
            int i_index = (i * 2 * tk->i_frame_size / tk->i_coded_frame_size) + y;
            if( i_index >= tk->i_subpackets )
                return;

693
            block_t *p_block = block_Alloc( tk->i_coded_frame_size);
694 695
            if( !p_block )
                return;
696
            if( &p_buf[tk->i_coded_frame_size] > &p_sys->buffer[p_sys->i_buffer] )
697
                return;
698

699 700
            memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
            p_block->i_dts =
701
            p_block->i_pts = i_index == 0 ? i_pts : VLC_TS_INVALID;
702

703
            p_buf += tk->i_coded_frame_size;
704

705
            if( tk->p_subpackets[i_index] != NULL )
706
            {
707 708
                msg_Dbg(p_demux, "p_subpackets[ %d ] not null!",  i_index );
                block_Release( tk->p_subpackets[i_index] );
709 710
            }

711 712 713 714
            tk->p_subpackets[i_index] = p_block;
            tk->i_subpacket++;
        }
    }
715

716 717 718 719
    while( tk->i_out_subpacket != tk->i_subpackets &&
           tk->p_subpackets[tk->i_out_subpacket] )
    {
        block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
Laurent Aimar's avatar
Laurent Aimar committed
720 721
        tk->p_subpackets[tk->i_out_subpacket] = NULL;

722 723 724 725
        if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
        {
            p_block->i_dts =
            p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
726

727 728 729
            tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
        }
        tk->i_out_subpacket++;
730

731 732 733
        CheckPcr( p_demux, tk, p_block->i_pts );
        es_out_Send( p_demux->out, tk->p_es, p_block );
    }
734

735 736 737 738 739 740
    if( tk->i_subpacket == tk->i_subpackets &&
        tk->i_out_subpacket != tk->i_subpackets )
    {
        msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
                  "this shouldn't happen" );
    }
741

742 743 744 745 746 747
    if( tk->i_subpacket == tk->i_subpackets )
    {
        tk->i_subpacket = 0;
        tk->i_out_subpacket = 0;
    }
}
748

749 750 751
static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
{
    demux_sys_t *p_sys = p_demux->p_sys;
752

753 754
    if( p_sys->i_buffer < 2 )
        return;
755

756
    unsigned i_sub = (p_sys->buffer[1] >> 4)&0x0f;
757 758
    if( p_sys->i_buffer < 2+2*i_sub )
        return;
759

760 761
    uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];

762
    for( unsigned i = 0; i < i_sub; i++ )
763 764
    {
        const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
765
        block_t *p_block = block_Alloc( i_sub_size );
766 767 768 769 770 771 772 773 774 775
        if( !p_block )
            break;

        if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
            break;

        memcpy( p_block->p_buffer, p_sub, i_sub_size );
        p_sub += i_sub_size;

        p_block->i_dts =
776
        p_block->i_pts = i == 0 ? i_pts : VLC_TS_INVALID;
777 778 779

        CheckPcr( p_demux, tk, p_block->i_pts );
        es_out_Send( p_demux->out, tk->p_es, p_block );
780
    }
781 782 783 784 785 786 787 788
}
static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    if( p_sys->i_buffer <= 0 )
        return;

789
    block_t *p_block = block_Alloc( p_sys->i_buffer );
790 791 792
    if( !p_block )
        return;

793
    if( tk->fmt.i_codec == VLC_CODEC_A52 )
794
    {
795 796 797 798 799
        uint8_t *p_src = p_sys->buffer;
        uint8_t *p_dst = p_block->p_buffer;

        /* byte swap data */
        while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
800
        {
801 802
            *p_dst++ = p_src[1];
            *p_dst++ = p_src[0];
803

804 805 806 807 808 809 810 811 812
            p_src += 2;
        }
    }
    else
    {
        memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
    }
    p_block->i_dts =
    p_block->i_pts = i_pts;
813

814 815 816
    CheckPcr( p_demux, tk, p_block->i_pts );
    es_out_Send( p_demux->out, tk->p_es, p_block );
}
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
// Sipr packet re-ordering code and index table borrowed from
// the MPlayer Realmedia demuxer.
static const uint8_t sipr_swap_index_table[38][2] = {
    {  0, 63 }, {  1, 22 }, {  2, 44 }, {  3, 90 },
    {  5, 81 }, {  7, 31 }, {  8, 86 }, {  9, 58 },
    { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
    { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
    { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
    { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
    { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
    { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
    { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
    { 67, 83 }, { 77, 80 }
};

static void SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize)
{
    int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket

    for (n = 0; n < 38; n++) {
        int j;
        int i = bs * sipr_swap_index_table[n][0];
        int o = bs * sipr_swap_index_table[n][1];

        /* swap 4 bit-nibbles of block 'i' with 'o' */
        for (j = 0; j < bs; j++, i++, o++) {
           int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
                y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;

            buf[o >> 1] = (x << (4 * (o & 1))) |
                (buf[o >> 1] & (0xF << (4 * !(o & 1))));
            buf[i >> 1] = (y << (4 * (i & 1))) |
                (buf[i >> 1] & (0xF << (4 * !(i & 1))));
        }
    }
}

static void DemuxAudioSipr( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t *p_block = tk->p_sipr_packet;

860 861
    if( p_sys->i_buffer < tk->i_frame_size
     || tk->i_sipr_subpacket_count >= tk->i_subpacket_h )
862 863 864 865
        return;

    if( !p_block )
    {
866
        p_block = block_Alloc( tk->i_frame_size * tk->i_subpacket_h );
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
        if( !p_block )
            return;
        tk->p_sipr_packet = p_block;
    }
    memcpy( p_block->p_buffer + tk->i_sipr_subpacket_count * tk->i_frame_size,
            p_sys->buffer, tk->i_frame_size );
    if (!tk->i_sipr_subpacket_count)
    {
        p_block->i_dts =
        p_block->i_pts = i_pts;
    }

    if( ++tk->i_sipr_subpacket_count < tk->i_subpacket_h )
        return;

    SiprPacketReorder(p_block->p_buffer, tk->i_subpacket_h, tk->i_frame_size);
    CheckPcr( p_demux, tk, p_block->i_pts );
    es_out_Send( p_demux->out, tk->p_es, p_block );
    tk->i_sipr_subpacket_count = 0;
    tk->p_sipr_packet = NULL;
}

889
static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
890 891 892
{
    switch( tk->fmt.i_codec )
    {
893 894 895
    case VLC_CODEC_COOK:
    case VLC_CODEC_ATRAC3:
    case VLC_CODEC_RA_288:
896 897
        DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
        break;
898
    case VLC_CODEC_MP4A:
899 900
        DemuxAudioMethod2( p_demux, tk, i_pts );
        break;
901 902 903
    case VLC_CODEC_SIPR:
        DemuxAudioSipr( p_demux, tk, i_pts );
        break;
904 905 906 907 908
    default:
        DemuxAudioMethod3( p_demux, tk, i_pts );
        break;
    }
}
909

910
/*****************************************************************************
911
 * Helpers: seek/control
912
 *****************************************************************************/
913
static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
914 915
{
    demux_sys_t *p_sys = p_demux->p_sys;
916

917 918 919 920 921 922 923 924 925 926
    p_sys->b_seek = true;
    p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
    for( int i = 0; i < p_sys->i_track; i++ )
        p_sys->track[i]->i_last_dts = 0;
    return stream_Seek( p_demux->s, p_index->i_file_offset );
}
static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    real_index_t *p_index = p_sys->p_index;
927

928 929 930
    while( p_index->i_file_offset != 0 )
    {
        if( p_index->i_time_offset * INT64_C(1000) > i_time )
931
        {
932 933 934
            if( p_index != p_sys->p_index )
                p_index--;
            break;
935
        }
936
        p_index++;
937
    }
938 939 940
    if( p_index->i_file_offset == 0 )
        return VLC_EGENERIC;
    return ControlGoToIndex( p_demux, p_index );
941
}
942
static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
Wang Bo's avatar
Wang Bo committed
943 944
{
    demux_sys_t *p_sys = p_demux->p_sys;
945
    real_index_t *p_index = p_sys->p_index;
Wang Bo's avatar
Wang Bo committed
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960
    while( p_index->i_file_offset != 0 )
    {
        if( p_index->i_file_offset > i_bytes )
        {
            if( p_index != p_sys->p_index )
                p_index--;
            break;
        }
        p_index++;
    }
    if( p_index->i_file_offset == 0 )
        return VLC_EGENERIC;
    return ControlGoToIndex( p_demux, p_index );
}
Wang Bo's avatar
Wang Bo committed
961

962 963 964
/*****************************************************************************
 * Helpers: header reading
 *****************************************************************************/
Wang Bo's avatar
Wang Bo committed
965

966 967 968 969 970 971 972 973 974
/**
 * This function will read a pascal string with size stored in 2 bytes from
 * a stream_t.
 *
 * FIXME what is the right charset ?
 */
static char *StreamReadString2( stream_t *s )
{
    uint8_t p_tmp[2];
Wang Bo's avatar
Wang Bo committed
975

976 977
    if( stream_Read( s, p_tmp, 2 ) < 2 )
        return NULL;
Wang Bo's avatar
Wang Bo committed
978

979 980 981
    const int i_length = GetWBE( p_tmp );
    if( i_length <= 0 )
        return NULL;
Wang Bo's avatar
Wang Bo committed
982

983
    char *psz_string = xcalloc( 1, i_length + 1 );
Wang Bo's avatar
Wang Bo committed
984

985
    stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
Wang Bo's avatar
Wang Bo committed
986

987 988 989 990
    if( psz_string )
        EnsureUTF8( psz_string );
    return psz_string;
}
Wang Bo's avatar
Wang Bo committed
991

992 993 994 995 996 997 998 999 1000 1001
/**
 * This function will read a pascal string with size stored in 1 byte from a
 * memory buffer.
 *
 * FIXME what is the right charset ?
 */
static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
{
    const uint8_t *p_data = *pp_data;
    int           i_data = *pi_data;
Wang Bo's avatar
Wang Bo committed
1002

1003
    char *psz_string = NULL;
Wang Bo's avatar
Wang Bo committed
1004

1005 1006
    if( i_data < 1 )
        goto exit;
Wang Bo's avatar
Wang Bo committed
1007

1008 1009 1010
    int i_length = *p_data++; i_data--;
    if( i_length > i_data )
        i_length = i_data;
Wang Bo's avatar
Wang Bo committed
1011

1012
    if( i_length > 0 )
Wang Bo's avatar
Wang Bo committed
1013
    {
1014 1015 1016
        psz_string = strndup( (const char*)p_data, i_length );
        if( psz_string )
            EnsureUTF8( psz_string );
Wang Bo's avatar
Wang Bo committed
1017

1018 1019
        p_data += i_length;
        i_data -= i_length;
Wang Bo's avatar
Wang Bo committed
1020
    }
1021 1022 1023 1024 1025

exit:
    *pp_data = p_data;
    *pi_data = i_data;
    return psz_string;
Wang Bo's avatar
Wang Bo committed
1026 1027
}

1028 1029 1030
/**
 * This function parses(skip) the .RMF identification chunk.
 */
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
static int HeaderRMF( demux_t *p_demux )
{
    uint8_t p_buffer[8];

    if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
        return VLC_EGENERIC;

    msg_Dbg( p_demux, "    - file version=0x%x num headers=%d",
             GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
    return VLC_SUCCESS;
}
1042 1043 1044
/**
 * This function parses the PROP properties chunk.
 */
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
static int HeaderPROP( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    uint8_t p_buffer[40];
    int i_flags;

    if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
        return VLC_EGENERIC;

    msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
             GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
    msg_Dbg( p_demux, "    - max packet size=%d avg bitrate=%d",
             GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
    msg_Dbg( p_demux, "    - packets count=%d", GetDWBE(&p_buffer[16]) );
    msg_Dbg( p_demux, "    - duration=%d ms", GetDWBE(&p_buffer[20]) );
    msg_Dbg( p_demux, "    - preroll=%d ms", GetDWBE(&p_buffer[24]) );
    msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&p_buffer[28]) );
    msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&p_buffer[32]) );
    msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&p_buffer[36]) );

    /* set the duration for export in control */
    p_sys->i_our_duration = GetDWBE(&p_buffer[20]);

    p_sys->i_index_offset = GetDWBE(&p_buffer[28]);

    i_flags = GetWBE(&p_buffer[38]);
    msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
             i_flags,
             i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
             i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
             i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );

    return VLC_SUCCESS;
}
1080 1081 1082
/**
 * This functions parses the CONT commentairs chunk.
 */
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static int HeaderCONT( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    /* */
    p_sys->psz_title = StreamReadString2( p_demux->s );
    if( p_sys->psz_title )
        msg_Dbg( p_demux, "    - title=`%s'", p_sys->psz_title );

    /* */
    p_sys->psz_artist = StreamReadString2( p_demux->s );
    if( p_sys->psz_artist )
        msg_Dbg( p_demux, "    - artist=`%s'", p_sys->psz_artist );

    /* */
    p_sys->psz_copyright = StreamReadString2( p_demux->s );
    if( p_sys->psz_copyright )
        msg_Dbg( p_demux, "    - copyright=`%s'", p_sys->psz_copyright );

    /* */
    p_sys->psz_description = StreamReadString2( p_demux->s );
    if( p_sys->psz_description )
        msg_Dbg( p_demux, "    - comment=`%s'", p_sys->psz_description );

    return VLC_SUCCESS;
}
1109 1110 1111
/**
 * This function parses the MDPR (Media properties) chunk.
 */
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
static int HeaderMDPR( demux_t *p_demux )
{
    uint8_t p_buffer[30];

    if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
        return VLC_EGENERIC;

    const int i_num = GetWBE( &p_buffer[0] );
    msg_Dbg( p_demux, "    - id=0x%x", i_num );
    msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
             GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
    msg_Dbg( p_demux, "    - max packet size=%d avg packet size=%d",
             GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
    msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&p_buffer[18]) );
    msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&p_buffer[22]) );
    msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&p_buffer[26]) );

    /* */
    const uint8_t *p_peek;
    int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
    int i_peek = i_peek_org;
    if( i_peek <= 0 )
        return VLC_EGENERIC;

    char *psz_name = MemoryReadString1( &p_peek, &i_peek );
    if( psz_name )
    {
        msg_Dbg( p_demux, "    - name=`%s'", psz_name );
        free( psz_name );
    }
    char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
    if( psz_mime )
    {
        msg_Dbg( p_demux, "    - mime=`%s'", psz_mime );
        free( psz_mime );
    }
    const int i_skip = i_peek_org - i_peek;
    if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
        return VLC_EGENERIC;

    /* */
    if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
        return VLC_EGENERIC;

    const uint32_t i_size = GetDWBE( p_buffer );
    if( i_size > 0 )
    {
        CodecParse( p_demux, i_size, i_num );
1160 1161
        unsigned size = stream_Read( p_demux->s, NULL, i_size );
        if( size < i_size )
1162 1163 1164 1165
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
1166 1167 1168
/**
 * This function parses DATA chunk (it contains the actual movie data).
 */
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t p_buffer[8];

    if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
        return VLC_EGENERIC;

    p_sys->i_data_offset    = stream_Tell( p_demux->s ) - 10;
    p_sys->i_data_size      = i_size;
    p_sys->i_data_packets_count = GetDWBE( p_buffer );
    p_sys->i_data_packets   = 0;
    p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );

    msg_Dbg( p_demux, "    - packets count=%d next=%u",
             p_sys->i_data_packets_count,
             (unsigned int)p_sys->i_data_offset_next );
    return VLC_SUCCESS;
}
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
/**
 * This function parses the INDX (movie index chunk).
 * It is optional but seeking without it is ... hard.
 */
static void HeaderINDX( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t       buffer[20];

    uint32_t      i_index_count;

    if( p_sys->i_index_offset == 0 )
        return;

    stream_Seek( p_demux->s, p_sys->i_index_offset );

    if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
        return ;

    const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
    const uint32_t i_size      = GetDWBE( &buffer[4] );
    int i_version   = GetWBE( &buffer[8] );

    msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
                 (char*)&i_id, i_size, i_version );

    if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
        return;

    i_index_count = GetDWBE( &buffer[10] );

    msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );

    if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
        return;

    if( GetDWBE( &buffer[16] ) > 0 )
        msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
                        GetDWBE( &buffer[16] )  );

    /* One extra entry is allocated (that MUST be set to 0) to identify the
     * end of the index.
     * TODO add a clean entry count (easier to build index on the fly) */
    p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
    if( !p_sys->p_index )
        return;

    for( unsigned int i = 0; i < i_index_count; i++ )
    {
        uint8_t p_entry[14];

        if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
            return ;

        if( GetWBE( &p_entry[0] ) != 0 )
        {
            msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
                              GetWBE( &p_entry[0] ) );
            return;
        }

        real_index_t *p_idx = &p_sys->p_index[i];
        
        p_idx->i_time_offset = GetDWBE( &p_entry[2] );
        p_idx->i_file_offset = GetDWBE( &p_entry[6] );
        p_idx->i_frame_index = GetDWBE( &p_entry[10] );

1255
#if 0
1256 1257 1258 1259 1260
        msg_Dbg( p_demux,
                 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
                 p_idx->i_time_offset,
                 p_idx->i_file_offset,
                 p_idx->i_frame_index );
1261
#endif
1262 1263 1264
    }
}

1265

1266 1267 1268 1269
/**
 * This function parses the complete RM headers and move the
 * stream pointer to the data to be read.
 */
1270 1271 1272 1273 1274 1275
static int HeaderRead( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    for( ;; )
    {
1276 1277 1278
        const int64_t i_stream_position = stream_Tell( p_demux->s );
        uint8_t header[100];    /* FIXME */

1279 1280 1281
        /* Read the header */
        if( stream_Read( p_demux->s, header, 10 ) < 10 )
            return VLC_EGENERIC;
1282 1283 1284 1285 1286

        const uint32_t i_id = VLC_FOURCC( header[0], header[1],
                                          header[2], header[3] );
        const uint32_t i_size = GetDWBE( &header[4] );
        const int i_version = GetWBE( &header[8] );
1287

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1288
        msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1289 1290
                 (char*)&i_id, i_size, i_version );

1291
        /* */
1292
        if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1293 1294 1295 1296
        {
            msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
            return VLC_EGENERIC;
        }
1297

1298 1299
        int i_ret;
        switch( i_id )
1300
        {
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
        case VLC_FOURCC('.','R','M','F'):
            i_ret = HeaderRMF( p_demux );
            break;
        case VLC_FOURCC('P','R','O','P'):
            i_ret = HeaderPROP( p_demux );
            break;
        case VLC_FOURCC('C','O','N','T'):
            i_ret = HeaderCONT( p_demux );
            break;
        case VLC_FOURCC('M','D','P','R'):
            i_ret = HeaderMDPR( p_demux );
            break;
        case VLC_FOURCC('D','A','T','A'):
            i_ret = HeaderDATA( p_demux, i_size );
            break;
        default:
1317
            /* unknown header */
1318 1319 1320
            msg_Dbg( p_demux, "unknown chunk" );
            i_ret = VLC_SUCCESS;
            break;
1321
        }
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
        if( i_ret )
            return i_ret;

        if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
            break;

        /* Skip unread data */
        const int64_t i_stream_current = stream_Tell( p_demux->s );
        const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;

        if( i_stream_skip > 0 )
1333
        {
1334 1335
            if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
                return VLC_EGENERIC;
1336
        }
1337
        else if( i_stream_skip < 0 )
1338
        {
1339 1340 1341
            return VLC_EGENERIC;
        }
    }
1342

1343 1344 1345 1346
    /* read index if possible */
    if( p_sys->i_index_offset > 0 )
    {
        const int64_t i_position = stream_Tell( p_demux->s );
1347

1348
        HeaderINDX( p_demux );
1349

1350 1351 1352 1353 1354
        if( stream_Seek( p_demux->s, i_position ) )
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
1355

1356 1357 1358
static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
{
    demux_sys_t *p_sys = p_demux->p_sys;
Wang Bo's avatar
Wang Bo committed
1359

1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    /* Title */
    p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
    if( p_sys->psz_title )
        msg_Dbg( p_demux, "    - title=`%s'", p_sys->psz_title );

    /* Authors */
    p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
    if( p_sys->psz_artist )
        msg_Dbg( p_demux, "    - artist=`%s'", p_sys->psz_artist );

    /* Copyright */
    p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
    if( p_sys->psz_copyright )
        msg_Dbg( p_demux, "    - copyright=`%s'", p_sys->psz_copyright );

    /* Comment */
    p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
    if( p_sys->psz_description )
        msg_Dbg( p_demux, "    - Comment=`%s'", p_sys->psz_description );
1379 1380
}

1381
static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1382 1383 1384
{
    demux_sys_t *p_sys = p_demux->p_sys;

1385 1386
    if( i_data < 34 )
        return VLC_EGENERIC;
1387

1388 1389 1390 1391 1392 1393
    /* */
    es_format_t fmt;
    es_format_Init( &fmt, VIDEO_ES,
                    VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
    fmt.video.i_width = GetWBE( &p_data[12] );
    fmt.video.i_height= GetWBE( &p_data[14] );
1394 1395
    fmt.video.i_visible_width = fmt.video.i_width;
    fmt.video.i_visible_height = fmt.video.i_height;
1396 1397 1398
    fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
    fmt.video.i_frame_rate_base = 1 << 16;

1399 1400
    fmt.i_extra = i_data - 26;
    fmt.p_extra = malloc( fmt.i_extra );
1401 1402
    if( !fmt.p_extra )
        return VLC_ENOMEM;
1403

1404
    memcpy( fmt.p_extra, &p_data[26], fmt.i_extra );
1405

1406
    //msg_Dbg( p_demux, "    - video 0x%08x 0x%08x", dw0, dw1 );
1407

1408 1409
    /* */
    switch( GetDWBE( &p_data[30] ) )
1410
    {
1411 1412
    case 0x10003000:
    case 0x10003001:
1413
        fmt.i_codec = VLC_CODEC_RV13;
1414 1415 1416 1417 1418
        break;
    case 0x20001000:
    case 0x20100001:
    case 0x20200002:
    case 0x20201002:
1419
        fmt.i_codec = VLC_CODEC_RV20;
1420 1421
        break;
    case 0x30202002:
1422
        fmt.i_codec = VLC_CODEC_RV30;
1423 1424
        break;
    case 0x40000000:
1425
        fmt.i_codec = VLC_CODEC_RV40;
1426
        break;
1427
    }
1428 1429
    msg_Dbg( p_demux, "    - video %4.4s %dx%d - %8.8x",
             (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1430

1431 1432
    real_track_t *tk = malloc( sizeof( *tk ) );
    if( !tk )
1433
    {
1434 1435
        es_format_Clean( &fmt );
        return VLC_ENOMEM;
1436
    }
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    tk->i_out_subpacket = 0;
    tk->i_subpacket = 0;
    tk->i_subpackets = 0;
    tk->p_subpackets = NULL;
    tk->p_subpackets_timecode = NULL;
    tk->i_id = i_tk_id;
    tk->fmt = fmt;
    tk->i_frame_num = -1;
    tk->i_frame_size = 0;
    tk->p_frame = NULL;
    tk->i_last_dts = 0;
1448
    tk->p_sipr_packet = NULL;
1449 1450 1451 1452
    tk->p_es = es_out_Add( p_demux->out, &fmt );

    TAB_APPEND( p_sys->i_track, p_sys->track, tk );
    return VLC_SUCCESS;
1453
}
1454
static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1455 1456 1457 1458
{
    demux_sys_t *p_sys = p_demux->p_sys;
    es_format_t fmt;

1459 1460
    if( i_data < 6 )
        return VLC_EGENERIC;
1461

1462 1463 1464 1465 1466
    int i_flavor = 0;
    int i_coded_frame_size = 0;
    int i_subpacket_h = 0;
    int i_frame_size = 0;
    int i_subpacket_size = 0;
1467
    char p_genr[4] = {0,0,0,0};
1468 1469 1470 1471 1472 1473 1474 1475 1476
    int i_version = GetWBE( &p_data[4] );
    int i_extra_codec = 0;

    msg_Dbg( p_demux, "    - audio version=%d", i_version );

    es_format_Init( &fmt, AUDIO_ES, 0 );

    RVoid( &p_data, &i_data, 6 );                         /* 4 + version */
    if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1477
    {
1478 1479 1480 1481
        RVoid( &p_data, &i_data, 2 + 10 + 4 );

        /* Meta Datas */
        CodecMetaRead( p_demux, &p_data, &i_data );
1482

1483 1484 1485 1486
        RVoid( &p_data, &i_data, 1 + 1 );
        if( i_data >= 4 )
            memcpy( &fmt.i_codec, p_data, 4 );
        RVoid( &p_data, &i_data, 4 );
1487

1488 1489
        fmt.audio.i_channels = 1;      /* This is always the case in rm3 */
        fmt.audio.i_rate = 8000;
1490