real.c 36.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * real.c: Real demuxer.
 *****************************************************************************
4
 * Copyright (C) 2004, 2006 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
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
27 28 29
#include <vlc/vlc.h>

#include <stdio.h>
30 31 32
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/input.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
33
#include "charset.h"
34 35 36 37 38 39 40 41 42

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

vlc_module_begin();
    set_description( _("Real demuxer" ) );
43
    set_capability( "demux2", 15 );
44 45
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    set_callbacks( Open, Close );
    add_shortcut( "real" );
    add_shortcut( "rm" );
vlc_module_end();

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

typedef struct
{
    int         i_id;
    es_format_t fmt;

    es_out_id_t *p_es;

    int         i_frame;
    block_t     *p_frame;

65 66 67 68 69 70 71 72
    int         i_subpacket_h;
    int         i_subpacket_size;
    int         i_coded_frame_size;
    int         i_frame_size;

    int         i_subpacket;
    int         i_subpackets;
    block_t     **p_subpackets;
73
    int         i_out_subpacket;
74

75 76 77 78 79 80 81 82 83 84
} real_track_t;

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;

85 86 87
    int  i_our_duration;
    int  i_mux_rate;

Felix Paul Kühne's avatar
Felix Paul Kühne committed
88 89 90 91 92
    char* psz_title;
    char* psz_artist;
    char* psz_copyright;
    char* psz_description;

93 94 95 96 97 98 99 100 101 102 103 104
    int          i_track;
    real_track_t **track;

    uint8_t buffer[65536];

    int64_t     i_pcr;
};

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

static int HeaderRead( demux_t *p_demux );
105
static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
106 107 108 109 110 111 112 113 114 115 116

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

    uint8_t     *p_peek;

117
    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
118
    if( strncmp( (char *)p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
119 120 121 122 123

    /* Fill p_demux field */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
124
    memset( p_sys, 0, sizeof( demux_sys_t ) );
125 126 127
    p_sys->i_data_offset = 0;
    p_sys->i_track = 0;
    p_sys->track   = NULL;
128
    p_sys->i_pcr   = 1;
129 130 131 132 133 134


    /* Parse the headers */
    if( HeaderRead( p_demux ) )
    {
        int i;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
135
        msg_Err( p_demux, "invalid header" );
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        for( i = 0; i < p_sys->i_track; i++ )
        {
            real_track_t *tk = p_sys->track[i];

            if( tk->p_es )
            {
                es_out_Del( p_demux->out, tk->p_es );
            }
            free( tk );
        }
        if( p_sys->i_track > 0 )
        {
            free( p_sys->track );
        }
        free( p_sys );
        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;
    int i;

    for( i = 0; i < p_sys->i_track; i++ )
    {
        real_track_t *tk = p_sys->track[i];
169
        int j = tk->i_subpackets;
170

171 172
        if( tk->p_frame ) block_Release( tk->p_frame );
        es_format_Clean( &tk->fmt );
173

174
        while(  j-- )
175
        {
176 177
            if( tk->p_subpackets[ j ] )
                block_Release( tk->p_subpackets[ j ] );
178
        }
179
        if( tk->i_subpackets ) free( tk->p_subpackets );
180

181 182 183
        free( tk );
    }

184 185 186 187 188
    if( p_sys->psz_title ) free( p_sys->psz_title );
    if( p_sys->psz_artist ) free( p_sys->psz_artist );
    if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
    if( p_sys->psz_description ) free( p_sys->psz_description );

189
    if( p_sys->i_track > 0 ) free( p_sys->track );
190 191 192 193 194 195 196 197 198 199 200
    free( p_sys );
}


/*****************************************************************************
 * Demux:
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t     header[18];
201
    int         i_size, i_id, i_flags, i;
202 203 204 205
    int64_t     i_pts;
    real_track_t *tk = NULL;
    vlc_bool_t  b_selected;

206 207
    if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
        p_sys->i_data_packets_count )
208 209 210 211 212
    {
        if( stream_Read( p_demux->s, header, 18 ) < 18 )
        {
            return 0;
        }
213
        if( strncmp( (char *)header, "DATA", 4 ) )
214 215 216 217 218 219 220 221 222 223 224 225 226 227
        {
            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,
                 (uint32_t)p_sys->i_data_offset_next );
    }

228 229
    if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;

230 231 232
    i_size = GetWBE( &header[2] ) - 12;
    i_id   = GetWBE( &header[4] );
    i_pts  = 1000 * GetDWBE( &header[6] );
233
    i_pts += 1000; /* Avoid 0 pts */
234
    i_flags= header[11]; /* flags 0x02 -> keyframe */
235 236

#if 0
237 238
    msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
             p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
239 240
#endif

241 242 243 244 245 246
    p_sys->i_data_packets++;

    stream_Read( p_demux->s, p_sys->buffer, i_size );

    for( i = 0; i < p_sys->i_track; i++ )
    {
247
        if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
248 249 250 251 252 253 254 255 256 257 258
    }

    if( tk == NULL )
    {
        msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
        return 1;
    }
    es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );

    if( tk->fmt.i_cat == VIDEO_ES && b_selected )
    {
259
        uint8_t *p = p_sys->buffer;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

        while( p < &p_sys->buffer[i_size - 2] )
        {
            uint8_t h = *p++;
            int     i_len = 0;
            int     i_copy;
            int     i_subseq = 0;
            int     i_seqnum = 0;
            int     i_offset = 0;

            if( (h&0xc0) == 0x40 )
            {
                /* Short header */
                p++;
                i_len = &p_sys->buffer[i_size] - p;
            }
            else
            {
                if( (h&0x40) == 0 )
                {
                    i_subseq = (*p++)&0x7f;
                }
                i_len = (p[0] << 8)|p[1]; p += 2;
                if( (i_len&0xc000) == 0 )
                {
                    i_len <<= 16;
                    i_len |= (p[0] << 8)|p[1]; p += 2;
                    i_len &= 0x3fffffff;
                }
                else
                {
                    i_len &= 0x3fff;
                }

                i_offset = (p[0] << 8)|p[1]; p += 2;
                if( (i_offset&0xc000) == 0 )
                {
                    i_offset <<= 16;
                    i_offset |= (p[0] << 8)|p[1]; p += 2;
                    i_offset &= 0x3fffffff;
                }
                else
                {
                    i_offset &= 0x3fff;
                }
                i_seqnum = *p++;
            }
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
            i_copy = i_len - i_offset;
            if( i_copy > &p_sys->buffer[i_size] - p )
            {
                i_copy = &p_sys->buffer[i_size] - p;
            }
            else if( i_copy < 0 )
            {
                break;
            }

            msg_Dbg( p_demux, "    - len=%d offset=%d size=%d subseq=%d seqnum=%d",
                     i_len, i_offset, i_copy, i_subseq, i_seqnum );

            if( (h&0xc0) == 0x80 )
            {
                /* last fragment -> fixes */
                i_copy = i_offset;
                i_offset = i_len - i_copy;
326 327
                msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
                         i_copy, i_offset );
328 329 330 331 332 333 334 335 336 337 338 339
            }

            if( tk->p_frame &&
                ( tk->p_frame->i_dts != i_pts ||
                  tk->i_frame != i_len ) )
            {
                msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );

                if( p_sys->i_pcr < tk->p_frame->i_dts )
                {
                    p_sys->i_pcr = tk->p_frame->i_dts;

340 341
                    es_out_Control( p_demux->out, ES_OUT_SET_PCR,
                                    (int64_t)p_sys->i_pcr );
342 343 344 345 346 347 348
                }
                es_out_Send( p_demux->out, tk->p_es, tk->p_frame );

                tk->i_frame = 0;
                tk->p_frame = NULL;
            }

349
            if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
350 351 352 353 354 355 356 357 358 359 360
            {
                /* no fragment */
                i_len = i_copy;
                i_offset = 0;
            }


            if( tk->p_frame == NULL )
            {
                msg_Dbg( p_demux, "new frame size=%d", i_len );
                tk->i_frame = i_len;
361
                if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
                {
                    return -1;
                }
                memset( &tk->p_frame->p_buffer[8], 0, i_len );
                tk->p_frame->i_dts = i_pts;
                tk->p_frame->i_pts = i_pts;

                ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
                ((uint32_t*)tk->p_frame->p_buffer)[1] = 0;      /* chunk counts */
            }

            if( i_offset < tk->i_frame)
            {
                int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;

377 378
                msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
                         i_ck, i_offset, i_copy );
379 380 381

                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;

382
                memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
383 384 385 386 387 388 389 390
            }

            p += i_copy;

            if( (h&0xc0) != 0x80 )
            {
                break;
            }
391

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
#if 0
            if( tk->p_frame )
            {
                /* append data */
                int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;

                if( (h&0xc0) == 0x80 )
                {
                    /* last fragment */
                    i_copy = i_offset;
                    i_offset = i_len - i_offset;

                    ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
                    memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
                    p += i_copy;

                    if( p_sys->i_pcr < tk->p_frame->i_dts )
                    {
                        p_sys->i_pcr = tk->p_frame->i_dts;
411 412
                        es_out_Control( p_demux->out, ES_OUT_SET_PCR,
                                        (int64_t)p_sys->i_pcr );
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                    }
                    es_out_Send( p_demux->out, tk->p_es, tk->p_frame );

                    tk->i_frame = 0;
                    tk->p_frame = NULL;

                    continue;
                }

                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
                memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
                break;
            }

            if( (h&0xc0) != 0x00 )
            {
                block_t *p_frame;

                /* not fragmented */
432
                if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
                {
                    return -1;
                }
                p_frame->i_dts = i_pts;
                p_frame->i_pts = i_pts;

                ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
                ((uint32_t*)p_frame->p_buffer)[1] = 1;
                ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
                memcpy( &p_frame->p_buffer[8], p, i_copy );

                p += i_copy;

                if( p_sys->i_pcr < p_frame->i_dts )
                {
                    p_sys->i_pcr = p_frame->i_dts;
449 450
                    es_out_Control( p_demux->out, ES_OUT_SET_PCR,
                                    (int64_t)p_sys->i_pcr );
451 452 453 454 455 456 457
                }
                es_out_Send( p_demux->out, tk->p_es, p_frame );
            }
            else
            {
                /* First fragment */
                tk->i_frame = i_len;
458
                if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
                {
                    return -1;
                }
                memset( &tk->p_frame->p_buffer[8], 0, i_len );
                tk->p_frame->i_dts = i_pts;
                tk->p_frame->i_pts = i_pts;

                ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */
                ((uint32_t*)tk->p_frame->p_buffer)[1] = 1;      /* chunk counts */
                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
                memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );

                break;
            }
#endif
        }
    }
    else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
    {
478 479 480 481
        /* Set PCR */
        if( p_sys->i_pcr < i_pts )
        {
            p_sys->i_pcr = i_pts;
482 483
            es_out_Control( p_demux->out, ES_OUT_SET_PCR,
                            (int64_t)p_sys->i_pcr );
484
        }
485

486
        if( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
487
        {
488 489
            int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
            uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
490

491 492
            int i;
            for( i = 0; i < i_sub; i++ )
493
            {
494 495 496 497 498 499
                int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
                block_t *p_block = block_New( p_demux, i_sub_size );
                if( p_block )
                {
                    memcpy( p_block->p_buffer, p_sub, i_sub_size );
                    p_sub += i_sub_size;
500

501 502 503 504 505
                    p_block->i_dts =
                    p_block->i_pts = ( i == 0 ? i_pts : 0 );

                    es_out_Send( p_demux->out, tk->p_es, p_block );
                }
506 507
            }
        }
508 509
        else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
                 tk->fmt.i_codec == VLC_FOURCC('2','8','_','8') )
510 511 512
        {
            uint8_t *p_buf = p_sys->buffer;
            int y = tk->i_subpacket / (tk->i_frame_size /tk->i_subpacket_size);
513
            int i_index, i;
514

515 516
            /* Sanity check */
            if( i_flags & 2 ) y = tk->i_subpacket = 0;
517

518
            if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) )
519 520 521 522 523 524
            for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
            {
                block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
                memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
                p_buf += tk->i_subpacket_size;

525 526 527
                i_index = tk->i_subpacket_h * i +
                    ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);

528
                p_block->i_dts = p_block->i_pts = i_pts;
529
                tk->p_subpackets[i_index] = p_block;
530
                tk->i_subpacket++;
531 532
		msg_Err( p_demux, "PTS!DTS: %lld, %lld",
			 p_block->i_dts, p_block->i_pts);
533 534
            }

535 536
            if( tk->fmt.i_codec == VLC_FOURCC('2','8','_','8') )
            for( i = 0; i < tk->i_subpacket_h / 2; i++ )
537
            {
538 539 540
                block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
                memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
                p_buf += tk->i_coded_frame_size;
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
                i_index = (i * 2 * tk->i_frame_size) /
                    tk->i_coded_frame_size + y;

                p_block->i_dts = p_block->i_pts = i_pts;
                tk->p_subpackets[i_index] = p_block;
                tk->i_subpacket++;
            }

            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];
                tk->p_subpackets[tk->i_out_subpacket] = 0;

                if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
                es_out_Send( p_demux->out, tk->p_es, p_block );

                tk->i_out_subpacket++;
            }

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

            if( tk->i_subpacket == tk->i_subpackets )
            {
571
                tk->i_subpacket = 0;
572
                tk->i_out_subpacket = 0;
573 574
            }
        }
575 576
        else
        {
577
            block_t *p_block = block_New( p_demux, i_size );
578

579 580 581 582
            if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
            {
                uint8_t *src = p_sys->buffer;
                uint8_t *dst = p_block->p_buffer;
583

584 585 586 587 588 589 590 591 592 593 594 595 596
                /* byte swap data */
                while( dst < &p_block->p_buffer[i_size- 1])
                {
                    *dst++ = src[1];
                    *dst++ = src[0];

                    src += 2;
                }
            }
            else
            {
                memcpy( p_block->p_buffer, p_sys->buffer, i_size );
            }
597
            p_block->i_dts = p_block->i_pts = i_pts;
598
            es_out_Send( p_demux->out, tk->p_es, p_block );
599 600 601 602 603 604 605 606 607 608 609 610 611
        }
    }


    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;
612
#if 0
613
    double f, *pf;
614
    int64_t i64;
615
#endif
616
    int64_t *pi64;
617 618 619

    switch( i_query )
    {
620
#if 0
621 622 623 624 625 626 627 628 629 630 631 632
        case DEMUX_GET_POSITION:
            pf = (double*) va_arg( args, double* );
            i64 = stream_Size( p_demux->s );
            if( i64 > 0 )
            {
                *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
            }
            else
            {
                *pf = 0.0;
            }
            return VLC_SUCCESS;
633

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
        case DEMUX_SET_POSITION:
            f = (double) va_arg( args, double );
            i64 = stream_Size( p_demux->s );

            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );

            return stream_Seek( p_demux->s, (int64_t)(i64 * f) );

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_mux_rate > 0 )
            {
                *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
                return VLC_SUCCESS;
            }
            *pi64 = 0;
            return VLC_EGENERIC;
651
#endif
652 653 654

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
655 656 657 658
            
            /* the commented following lines are fen's implementation, which doesn't seem to
             * work for one reason or another -- FK */
            /*if( p_sys->i_mux_rate > 0 )
659 660
            {
                *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
661 662 663 664 665 666 667
                return VLC_SUCCESS;
            }*/
            if( p_sys->i_our_duration > 0 )
            {
                /* our stored duration is in ms, so... */
                *pi64 = (int64_t)1000 * p_sys->i_our_duration;
                
668 669 670 671
                return VLC_SUCCESS;
            }
            *pi64 = 0;
            return VLC_EGENERIC;
672 673 674

        case DEMUX_GET_META:
        {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
675 676 677 678 679 680 681 682 683 684 685 686
            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 );
687 688
            return VLC_SUCCESS;
        }
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

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

/*****************************************************************************
 * HeaderRead:
 *****************************************************************************/
static int HeaderRead( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t header[100];    /* FIXME */

    uint32_t    i_id;
    uint32_t    i_size;
    int64_t     i_skip;
    int         i_version;

    for( ;; )
    {
        /* Read the header */
        if( stream_Read( p_demux->s, header, 10 ) < 10 )
        {
            return VLC_EGENERIC;
        }
        i_id        = VLC_FOURCC( header[0], header[1], header[2], header[3] );
        i_size      = GetDWBE( &header[4] );
        i_version   = GetWBE( &header[8] );

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

725
        if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
726 727 728 729
        {
            msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
            return VLC_EGENERIC;
        }
730 731 732 733
        i_skip = i_size - 10;

        if( i_id == VLC_FOURCC('.','R','M','F') )
        {
734
            if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
735 736 737 738 739 740 741 742 743
            msg_Dbg( p_demux, "    - file version=0x%x num headers=%d",
                     GetDWBE( &header[0] ), GetDWBE( &header[4] ) );

            i_skip -= 8;
        }
        else if( i_id == VLC_FOURCC('P','R','O','P') )
        {
            int i_flags;

744 745
            if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;

746
            msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
747
                     GetDWBE(&header[0]), GetDWBE(&header[4]) );
748
            msg_Dbg( p_demux, "    - max packet size=%d avg bitrate=%d",
749 750 751 752 753 754 755
                     GetDWBE(&header[8]), GetDWBE(&header[12]) );
            msg_Dbg( p_demux, "    - packets count=%d", GetDWBE(&header[16]) );
            msg_Dbg( p_demux, "    - duration=%d ms", GetDWBE(&header[20]) );
            msg_Dbg( p_demux, "    - preroll=%d ms", GetDWBE(&header[24]) );
            msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&header[28]) );
            msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&header[32]) );
            msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&header[36]) );
756 757 758 759
            
            /* set the duration for export in control */
            p_sys->i_our_duration = (int)GetDWBE(&header[20]);
            
760
            i_flags = GetWBE(&header[38]);
761 762 763 764 765 766 767 768 769 770 771
            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" : "" );
            i_skip -= 40;
        }
        else if( i_id == VLC_FOURCC('C','O','N','T') )
        {
            int i_len;
            char *psz;
772 773 774
            
            /* FIXME FIXME: should convert from whatever the character
             * encoding of the input meta data is to UTF-8. */
775 776 777 778 779 780 781 782 783

            stream_Read( p_demux->s, header, 2 );
            if( ( i_len = GetWBE( header ) ) > 0 )
            {
                psz = malloc( i_len + 1 );
                stream_Read( p_demux->s, psz, i_len );
                psz[i_len] = '\0';

                msg_Dbg( p_demux, "    - title=`%s'", psz );
784
                EnsureUTF8( psz );
Felix Paul Kühne's avatar
Felix Paul Kühne committed
785
                asprintf( &p_sys->psz_title, psz );
786 787 788 789 790 791 792 793 794 795 796 797 798
                free( psz );
                i_skip -= i_len;
            }
            i_skip -= 2;

            stream_Read( p_demux->s, header, 2 );
            if( ( i_len = GetWBE( header ) ) > 0 )
            {
                psz = malloc( i_len + 1 );
                stream_Read( p_demux->s, psz, i_len );
                psz[i_len] = '\0';

                msg_Dbg( p_demux, "    - author=`%s'", psz );
799
                EnsureUTF8( psz );
Felix Paul Kühne's avatar
Felix Paul Kühne committed
800
                asprintf( &p_sys->psz_artist, psz );
801 802 803 804 805 806 807 808 809 810 811 812 813
                free( psz );
                i_skip -= i_len;
            }
            i_skip -= 2;

            stream_Read( p_demux->s, header, 2 );
            if( ( i_len = GetWBE( header ) ) > 0 )
            {
                psz = malloc( i_len + 1 );
                stream_Read( p_demux->s, psz, i_len );
                psz[i_len] = '\0';

                msg_Dbg( p_demux, "    - copyright=`%s'", psz );
814
                EnsureUTF8( psz );
Felix Paul Kühne's avatar
Felix Paul Kühne committed
815
                asprintf( &p_sys->psz_copyright, psz );
816 817 818 819 820 821 822 823 824 825 826 827 828
                free( psz );
                i_skip -= i_len;
            }
            i_skip -= 2;

            stream_Read( p_demux->s, header, 2 );
            if( ( i_len = GetWBE( header ) ) > 0 )
            {
                psz = malloc( i_len + 1 );
                stream_Read( p_demux->s, psz, i_len );
                psz[i_len] = '\0';

                msg_Dbg( p_demux, "    - comment=`%s'", psz );
829
                EnsureUTF8( psz );
Felix Paul Kühne's avatar
Felix Paul Kühne committed
830
                asprintf( &p_sys->psz_description, psz );
831 832 833 834 835 836 837
                free( psz );
                i_skip -= i_len;
            }
            i_skip -= 2;
        }
        else if( i_id == VLC_FOURCC('M','D','P','R') )
        {
838
            /* Media properties header */
839 840 841 842
            int  i_num;
            int  i_len;
            char *psz;

843
            if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
844 845
            i_num = GetWBE( header );
            msg_Dbg( p_demux, "    - id=0x%x", i_num );
846 847 848 849 850 851 852
            msg_Dbg( p_demux, "    - max bitrate=%d avg bitrate=%d",
                     GetDWBE(&header[2]), GetDWBE(&header[6]) );
            msg_Dbg( p_demux, "    - max packet size=%d avg packet size=%d",
                     GetDWBE(&header[10]), GetDWBE(&header[14]) );
            msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&header[18]) );
            msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&header[22]) );
            msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&header[26]) );
853
            
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
            i_skip -= 30;

            stream_Read( p_demux->s, header, 1 );
            if( ( i_len = header[0] ) > 0 )
            {
                psz = malloc( i_len + 1 );
                stream_Read( p_demux->s, psz, i_len );
                psz[i_len] = '\0';

                msg_Dbg( p_demux, "    - name=`%s'", psz );
                free( psz );
                i_skip -= i_len;
            }
            i_skip--;

            stream_Read( p_demux->s, header, 1 );
            if( ( i_len = header[0] ) > 0 )
            {
                psz = malloc( i_len + 1 );
                stream_Read( p_demux->s, psz, i_len );
                psz[i_len] = '\0';

                msg_Dbg( p_demux, "    - mime=`%s'", psz );
                free( psz );
                i_skip -= i_len;
            }
            i_skip--;

            stream_Read( p_demux->s, header, 4 );
            if( ( i_len = GetDWBE( header ) ) > 0 )
            {
885
                ReadCodecSpecificData( p_demux, i_len, i_num );
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
                stream_Read( p_demux->s, NULL, i_len );

                i_skip -= i_len;
            }
            i_skip -= 4;
        }
        else if( i_id == VLC_FOURCC('D','A','T','A') )
        {
            stream_Read( p_demux->s, header, 8 );

            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( header );
            p_sys->i_data_packets   = 0;
            p_sys->i_data_offset_next = GetDWBE( &header[4] );

            msg_Dbg( p_demux, "    - packets count=%d next=%u",
                     p_sys->i_data_packets_count,
                     (uint32_t)p_sys->i_data_offset_next );

            /* we have finished the header */
            break;
        }
        else
        {
            /* unknow header */
            msg_Dbg( p_demux, "unknown chunk" );
        }

915
        if( i_skip < 0 ) return VLC_EGENERIC;
916 917 918 919 920 921 922 923
        stream_Read( p_demux->s, NULL, i_skip );
    }

    /* TODO read index if possible */

    return VLC_SUCCESS;
}

924 925 926 927 928 929 930 931 932
static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    es_format_t fmt;
    real_track_t *tk;
    uint8_t *p_peek;

    msg_Dbg( p_demux, "    - specific data len=%d", i_len );
    if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
933

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
    if( !strncmp( (char *)&p_peek[4], "VIDO", 4 ) )
    {
        es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
                        p_peek[10], p_peek[11] ) );
        fmt.video.i_width = GetWBE( &p_peek[12] );
        fmt.video.i_height= GetWBE( &p_peek[14] );

        fmt.i_extra = 8;
        fmt.p_extra = malloc( 8 );
        ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
        ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );

        msg_Dbg( p_demux, "    - video 0x%08x 0x%08x",
                 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );

        if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
            GetDWBE( &p_peek[30] ) == 0x10003001 )
        {
            fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
        }
Clément Stenac's avatar
Clément Stenac committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967
        else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
                 GetDWBE( &p_peek[30] ) == 0x20100001 ||
                 GetDWBE( &p_peek[30] ) == 0x20200002 )
        {
            fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
        }
        else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
        {
            fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
        }
        else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
        {
            fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
        }
968 969 970 971 972

        msg_Dbg( p_demux, "    - video %4.4s %dx%d",
                 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );

        tk = malloc( sizeof( real_track_t ) );
973 974 975 976
        tk->i_out_subpacket = 0;
        tk->i_subpacket = 0;
        tk->i_subpackets = 0;
        tk->p_subpackets = NULL;
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
        tk->i_id = i_num;
        tk->fmt = fmt;
        tk->i_frame = 0;
        tk->p_frame = NULL;
        tk->p_es = es_out_Add( p_demux->out, &fmt );

        TAB_APPEND( p_sys->i_track, p_sys->track, tk );
    }
    else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
    {
        int i_version = GetWBE( &p_peek[4] );
        int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
        int i_frame_size, i_subpacket_size;

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

        p_peek += 6;
        es_format_Init( &fmt, AUDIO_ES, 0 );

        if( i_version == 3 )
        {
            msg_Dbg( p_demux, "    - audio version 3 is not supported!" );
            return VLC_EGENERIC;
        }

        p_peek += 2; /* 00 00 */
        p_peek += 4; /* .ra4 or .ra5 */
        p_peek += 4; /* ?? */
        p_peek += 2; /* version (4 or 5) */
        i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
        i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
        i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4;
        p_peek += 4; /* ?? */
        p_peek += 4; /* ?? */
        p_peek += 4; /* ?? */
        i_subpacket_h = GetWBE( p_peek ); p_peek += 2;
        i_frame_size = GetWBE( p_peek ); p_peek += 2;
        i_subpacket_size = GetWBE( p_peek ); p_peek += 2;
        p_peek += 2; /* ?? */

        if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */

        fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2;
        p_peek += 2; /* ?? */
        fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;
        fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2;
        fmt.audio.i_blockalign = i_frame_size;

        if( i_version == 5 )
        {
            p_peek += 4; /* genr */
            memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
        }
        else
        {
            p_peek += p_peek[0] + 1; /* descr 1 */
            memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
            p_peek += p_peek[0] + 1;
        }

        msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
                 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );

        p_peek += 3; /* ?? */
        if( i_version == 5 ) p_peek++;

        switch( fmt.i_codec )
        {
        case VLC_FOURCC( 'd', 'n', 'e', 't' ):
            fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
            break;

        case VLC_FOURCC( 'r', 'a', 'a', 'c' ):
        case VLC_FOURCC( 'r', 'a', 'c', 'p' ):
            fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
            if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
            if( fmt.i_extra > 0 )
            {
                fmt.p_extra = malloc( fmt.i_extra );
                memcpy( fmt.p_extra, p_peek, fmt.i_extra );
            }

            fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
            break;

        case VLC_FOURCC('c','o','o','k'):
1063 1064 1065 1066
            fmt.audio.i_blockalign = i_subpacket_size;
            if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
            fmt.p_extra = malloc( fmt.i_extra );
            memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
1067 1068
            break;

1069 1070 1071 1072
        case VLC_FOURCC('2','8','_','8'):
            fmt.audio.i_blockalign = i_coded_frame_size;
            break;

1073 1074 1075 1076 1077 1078 1079 1080
        default:
            msg_Dbg( p_demux, "    - unknown audio codec=%4.4s",
                     (char*)&fmt.i_codec );
            break;
        }

        if( fmt.i_codec != 0 )
        {
1081 1082
            int i;

1083 1084 1085 1086 1087 1088 1089
            msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );

            tk = malloc( sizeof( real_track_t ) );
            tk->i_id = i_num;
            tk->fmt = fmt;
            tk->i_frame = 0;
            tk->p_frame = NULL;
1090 1091 1092 1093 1094 1095

            tk->i_subpacket_h = i_subpacket_h;
            tk->i_subpacket_size = i_subpacket_size;
            tk->i_coded_frame_size = i_coded_frame_size;
            tk->i_frame_size = i_frame_size;

1096
            tk->i_out_subpacket = 0;
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
            tk->i_subpacket = 0;
            tk->i_subpackets = 0;
            tk->p_subpackets = NULL;
            if( fmt.i_codec == VLC_FOURCC('c','o','o','k') )
            {
                tk->i_subpackets =
                    i_subpacket_h * i_frame_size / tk->i_subpacket_size;
                tk->p_subpackets =
                    malloc( tk->i_subpackets * sizeof(block_t *) );
            }
1107 1108 1109 1110 1111 1112 1113 1114 1115
            else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
            {
                tk->i_subpackets =
                    i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
                tk->p_subpackets =
                    malloc( tk->i_subpackets * sizeof(block_t *) );
            }

            for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1116

1117 1118 1119 1120 1121 1122 1123 1124
            tk->p_es = es_out_Add( p_demux->out, &fmt );

            TAB_APPEND( p_sys->i_track, p_sys->track, tk );
        }
    }

    return VLC_SUCCESS;
}